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 +----------------------------------------------------------------------+
23 ///////////////////////////////////////////////////////////////////////////////
26 * Pointer which can be safely const-dereferenced when null to yield a
27 * default-constructed value.
34 default_ptr() : m_p
{fallback()} {}
36 /* implicit */ default_ptr(std::nullptr_t
) {}
38 /* implicit */ default_ptr(T
* p
) : m_p
{p
? p
: fallback()} {}
41 * Thread-safe allocation.
43 T
* ensureAllocated() {
44 if (auto p
= raw()) return p
;
46 T
* expected
= fallback();
47 if (!m_p
.compare_exchange_strong(
48 expected
, ptr
, std::memory_order_acq_rel
)) {
49 // Already set by someone else, use theirs.
60 default_ptr
& operator=(std::nullptr_t
/*p*/) {
61 m_p
.store(fallback(), std::memory_order_release
);
64 default_ptr
& operator=(T
* p
) {
65 m_p
.store(p
? p
: fallback(), std::memory_order_release
);
72 const T
* get() const {
73 return m_p
.load(std::memory_order_acquire
);
75 const T
& operator*() const {
78 const T
* operator->() const {
83 auto p
= m_p
.load(std::memory_order_acquire
);
84 return p
== &s_default
? nullptr : p
;
86 explicit operator bool() const {
93 void reset(T
* p
= nullptr) {
101 static T
* fallback() {
102 return const_cast<T
*>(&s_default
);
106 std::atomic
<T
*> m_p
{const_cast<T
*>(&s_default
)};
107 static const T s_default
;
111 const T default_ptr
<T
>::s_default
;
113 ///////////////////////////////////////////////////////////////////////////////