2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_DEFAULT_PTR_H_
18 #define incl_HPHP_DEFAULT_PTR_H_
24 ///////////////////////////////////////////////////////////////////////////////
27 * Pointer which can be safely const-dereferenced when null to yield a
28 * default-constructed value.
35 default_ptr() : m_p
{fallback()} {}
37 /* implicit */ default_ptr(std::nullptr_t
) {}
39 /* implicit */ default_ptr(T
* p
) : m_p
{p
? p
: fallback()} {}
42 * Thread-safe allocation.
44 T
* ensureAllocated() {
45 if (auto p
= raw()) return p
;
47 T
* expected
= fallback();
48 if (!m_p
.compare_exchange_strong(
49 expected
, ptr
, std::memory_order_relaxed
)) {
50 // Already set by someone else, use theirs.
61 default_ptr
& operator=(std::nullptr_t
/*p*/) {
62 m_p
.store(fallback(), std::memory_order_relaxed
);
65 default_ptr
& operator=(T
* p
) {
66 m_p
.store(p
? p
: fallback(), std::memory_order_relaxed
);
73 const T
* get() const {
74 return m_p
.load(std::memory_order_relaxed
);
76 const T
& operator*() const {
79 const T
* operator->() const {
84 auto p
= m_p
.load(std::memory_order_relaxed
);
85 return p
== &s_default
? nullptr : p
;
87 explicit operator bool() const {
94 void reset(T
* p
= nullptr) {
102 static T
* fallback() {
103 return const_cast<T
*>(&s_default
);
107 std::atomic
<T
*> m_p
{const_cast<T
*>(&s_default
)};
108 static const T s_default
;
112 const T default_ptr
<T
>::s_default
;
114 ///////////////////////////////////////////////////////////////////////////////
117 #endif // incl_HPHP_DEFAULT_PTR_H_