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" |
11 #include "wtf/StdLibExtras.h" | 11 #include "wtf/StdLibExtras.h" |
12 #include "wtf/Utility.h" | 12 #include <utility> |
13 | 13 |
14 namespace WTF { | 14 namespace WTF { |
15 | 15 |
16 // This is a lightweight template similar to std::experimental::optional. | 16 // This is a lightweight template similar to std::experimental::optional. |
17 // It currently does not support assignment, swapping, comparison, etc. | 17 // It currently does not support assignment, swapping, comparison, etc. |
18 // | 18 // |
19 // Use this instead of OwnPtr for cases where you only want to conditionally | 19 // Use this instead of OwnPtr for cases where you only want to conditionally |
20 // construct a "scope" object. | 20 // construct a "scope" object. |
21 // | 21 // |
22 // Example: | 22 // Example: |
(...skipping 22 matching lines...) Expand all Loading... |
45 T& operator*() { ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); return *m_ptr; } | 45 T& operator*() { ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); return *m_ptr; } |
46 const T& operator*() const { ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); return
*m_ptr; } | 46 const T& operator*() const { ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); return
*m_ptr; } |
47 T* operator->() { ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); return m_ptr; } | 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; } | 48 const T* operator->() const { ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); retur
n m_ptr; } |
49 | 49 |
50 template <typename... Args> | 50 template <typename... Args> |
51 void emplace(Args&&... args) | 51 void emplace(Args&&... args) |
52 { | 52 { |
53 RELEASE_ASSERT(!m_ptr); | 53 RELEASE_ASSERT(!m_ptr); |
54 m_ptr = reinterpret_cast_ptr<T*>(&m_storage.buffer); | 54 m_ptr = reinterpret_cast_ptr<T*>(&m_storage.buffer); |
55 new (m_ptr) T(forward<Args>(args)...); | 55 new (m_ptr) T(std::forward<Args>(args)...); |
56 } | 56 } |
57 | 57 |
58 private: | 58 private: |
59 T* m_ptr; | 59 T* m_ptr; |
60 AlignedBuffer<sizeof(T), WTF_ALIGN_OF(T)> m_storage; | 60 AlignedBuffer<sizeof(T), WTF_ALIGN_OF(T)> m_storage; |
61 }; | 61 }; |
62 | 62 |
63 } // namespace WTF | 63 } // namespace WTF |
64 | 64 |
65 using WTF::Optional; | 65 using WTF::Optional; |
66 | 66 |
67 #endif // Optional_h | 67 #endif // Optional_h |
OLD | NEW |