| 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/Assertions.h" | 9 #include "wtf/Assertions.h" |
| 10 #include "wtf/Noncopyable.h" | 10 #include "wtf/Noncopyable.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 // Optional<DrawingRecorder> recorder; | 23 // Optional<DrawingRecorder> recorder; |
| 24 // if (shouldDraw) | 24 // if (shouldDraw) |
| 25 // recorder.emplace(constructor, args, here); | 25 // recorder.emplace(constructor, args, here); |
| 26 // // recorder destroyed at end of scope | 26 // // recorder destroyed at end of scope |
| 27 // | 27 // |
| 28 // Note in particular that unlike a pointer, though, dereferencing a const | 28 // Note in particular that unlike a pointer, though, dereferencing a const |
| 29 // optional yields a const reference. | 29 // optional yields a const reference. |
| 30 | 30 |
| 31 template <typename T> | 31 template <typename T> |
| 32 class Optional { | 32 class Optional { |
| 33 WTF_MAKE_NONCOPYABLE(Optional); | 33 WTF_MAKE_NONCOPYABLE(Optional); |
| 34 public: | |
| 35 Optional() : m_ptr(nullptr) { } | |
| 36 ~Optional() | |
| 37 { | |
| 38 if (m_ptr) | |
| 39 m_ptr->~T(); | |
| 40 } | |
| 41 | 34 |
| 42 typedef T* Optional::*UnspecifiedBoolType; | 35 public: |
| 43 operator UnspecifiedBoolType() const { return m_ptr ? &Optional::m_ptr : nul
lptr; } | 36 Optional() |
| 37 : m_ptr(nullptr) {} |
| 38 ~Optional() { |
| 39 if (m_ptr) |
| 40 m_ptr->~T(); |
| 41 } |
| 44 | 42 |
| 45 T& operator*() { ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); return *m_ptr; } | 43 typedef T* Optional::*UnspecifiedBoolType; |
| 46 const T& operator*() const { ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); return
*m_ptr; } | 44 operator UnspecifiedBoolType() const { return m_ptr ? &Optional::m_ptr : nullp
tr; } |
| 47 T* operator->() { ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); return m_ptr; } | |
| 48 const T* operator->() const { ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); retur
n m_ptr; } | |
| 49 | 45 |
| 50 template <typename... Args> | 46 T& operator*() { |
| 51 void emplace(Args&&... args) | 47 ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); |
| 52 { | 48 return *m_ptr; |
| 53 RELEASE_ASSERT(!m_ptr); | 49 } |
| 54 m_ptr = reinterpret_cast_ptr<T*>(&m_storage.buffer); | 50 const T& operator*() const { |
| 55 new (m_ptr) T(forward<Args>(args)...); | 51 ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); |
| 56 } | 52 return *m_ptr; |
| 53 } |
| 54 T* operator->() { |
| 55 ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); |
| 56 return m_ptr; |
| 57 } |
| 58 const T* operator->() const { |
| 59 ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); |
| 60 return m_ptr; |
| 61 } |
| 57 | 62 |
| 58 private: | 63 template <typename... Args> |
| 59 T* m_ptr; | 64 void emplace(Args&&... args) { |
| 60 AlignedBuffer<sizeof(T), WTF_ALIGN_OF(T)> m_storage; | 65 RELEASE_ASSERT(!m_ptr); |
| 66 m_ptr = reinterpret_cast_ptr<T*>(&m_storage.buffer); |
| 67 new (m_ptr) T(forward<Args>(args)...); |
| 68 } |
| 69 |
| 70 private: |
| 71 T* m_ptr; |
| 72 AlignedBuffer<sizeof(T), WTF_ALIGN_OF(T)> m_storage; |
| 61 }; | 73 }; |
| 62 | 74 |
| 63 } // namespace WTF | 75 } // namespace WTF |
| 64 | 76 |
| 65 using WTF::Optional; | 77 using WTF::Optional; |
| 66 | 78 |
| 67 #endif // Optional_h | 79 #endif // Optional_h |
| OLD | NEW |