| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef Optional_h | 5 #ifndef Optional_h |
| 6 #define Optional_h | 6 #define Optional_h |
| 7 | 7 |
| 8 #include "wtf/Alignment.h" | 8 #include "wtf/Alignment.h" |
| 9 #include "wtf/Allocator.h" | 9 #include "wtf/Allocator.h" |
| 10 #include "wtf/Assertions.h" | 10 #include "wtf/Assertions.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 // Optional<DrawingRecorder> recorder; | 26 // Optional<DrawingRecorder> recorder; |
| 27 // if (shouldDraw) | 27 // if (shouldDraw) |
| 28 // recorder.emplace(constructor, args, here); | 28 // recorder.emplace(constructor, args, here); |
| 29 // // recorder destroyed at end of scope | 29 // // recorder destroyed at end of scope |
| 30 // | 30 // |
| 31 // Note in particular that unlike a pointer, though, dereferencing a const | 31 // Note in particular that unlike a pointer, though, dereferencing a const |
| 32 // optional yields a const reference. | 32 // optional yields a const reference. |
| 33 | 33 |
| 34 template <typename T> | 34 template <typename T> |
| 35 class Optional final { | 35 class Optional final { |
| 36 DISALLOW_NEW(); | 36 DISALLOW_NEW(); |
| 37 WTF_MAKE_NONCOPYABLE(Optional); | 37 WTF_MAKE_NONCOPYABLE(Optional); |
| 38 public: | |
| 39 Optional() : m_ptr(nullptr) { } | |
| 40 ~Optional() | |
| 41 { | |
| 42 if (m_ptr) | |
| 43 m_ptr->~T(); | |
| 44 } | |
| 45 | 38 |
| 46 typedef T* Optional::*UnspecifiedBoolType; | 39 public: |
| 47 operator UnspecifiedBoolType() const { return m_ptr ? &Optional::m_ptr : nul
lptr; } | 40 Optional() : m_ptr(nullptr) {} |
| 41 ~Optional() { |
| 42 if (m_ptr) |
| 43 m_ptr->~T(); |
| 44 } |
| 48 | 45 |
| 49 T& operator*() { ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); return *m_ptr; } | 46 typedef T* Optional::*UnspecifiedBoolType; |
| 50 const T& operator*() const { ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); return
*m_ptr; } | 47 operator UnspecifiedBoolType() const { |
| 51 T* operator->() { ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); return m_ptr; } | 48 return m_ptr ? &Optional::m_ptr : nullptr; |
| 52 const T* operator->() const { ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); retur
n m_ptr; } | 49 } |
| 53 | 50 |
| 54 template <typename... Args> | 51 T& operator*() { |
| 55 void emplace(Args&&... args) | 52 ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); |
| 56 { | 53 return *m_ptr; |
| 57 RELEASE_ASSERT(!m_ptr); | 54 } |
| 58 m_ptr = reinterpret_cast_ptr<T*>(&m_storage.buffer); | 55 const T& operator*() const { |
| 59 new (m_ptr) T(std::forward<Args>(args)...); | 56 ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); |
| 60 } | 57 return *m_ptr; |
| 58 } |
| 59 T* operator->() { |
| 60 ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); |
| 61 return m_ptr; |
| 62 } |
| 63 const T* operator->() const { |
| 64 ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); |
| 65 return m_ptr; |
| 66 } |
| 61 | 67 |
| 62 private: | 68 template <typename... Args> |
| 63 T* m_ptr; | 69 void emplace(Args&&... args) { |
| 64 AlignedBuffer<sizeof(T), WTF_ALIGN_OF(T)> m_storage; | 70 RELEASE_ASSERT(!m_ptr); |
| 71 m_ptr = reinterpret_cast_ptr<T*>(&m_storage.buffer); |
| 72 new (m_ptr) T(std::forward<Args>(args)...); |
| 73 } |
| 74 |
| 75 private: |
| 76 T* m_ptr; |
| 77 AlignedBuffer<sizeof(T), WTF_ALIGN_OF(T)> m_storage; |
| 65 }; | 78 }; |
| 66 | 79 |
| 67 } // namespace WTF | 80 } // namespace WTF |
| 68 | 81 |
| 69 using WTF::Optional; | 82 using WTF::Optional; |
| 70 | 83 |
| 71 #endif // Optional_h | 84 #endif // Optional_h |
| OLD | NEW |