2 //===----------------------------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_EXPERIMENTAL_MEMORY
11 #define _LIBCPP_EXPERIMENTAL_MEMORY
14 experimental/memory synopsis
16 namespace std::experimental::inline fundamentals_v2 {
18 template <class W> class observer_ptr {
20 using element_type = W;
21 using pointer = add_pointer_t<W>; // exposition-only
22 using reference = add_lvalue_reference_t<W>; // exposition-only
25 constexpr observer_ptr() noexcept;
27 // pointer-accepting ctors
28 constexpr observer_ptr(nullptr_t) noexcept;
29 constexpr explicit observer_ptr(pointer) noexcept;
31 // copying ctors (in addition to compiler-generated copy ctor)
32 template <class W2> constexpr observer_ptr(observer_ptr<W2>) noexcept;
35 constexpr pointer get() const noexcept;
36 constexpr reference operator*() const;
37 constexpr pointer operator->() const noexcept;
38 constexpr explicit operator bool() const noexcept;
41 constexpr explicit operator pointer() const noexcept;
44 constexpr pointer release() noexcept;
45 constexpr void reset(pointer = nullptr) noexcept;
46 constexpr void swap(observer_ptr&) noexcept;
53 #include <__cstddef/nullptr_t.h>
54 #include <__cstddef/size_t.h>
55 #include <__functional/hash.h>
56 #include <__functional/operations.h>
57 #include <__type_traits/add_lvalue_reference.h>
58 #include <__type_traits/add_pointer.h>
59 #include <__type_traits/common_type.h>
60 #include <__type_traits/enable_if.h>
61 #include <__type_traits/is_convertible.h>
64 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
65 # pragma GCC system_header
68 #ifdef _LIBCPP_ENABLE_EXPERIMENTAL
70 _LIBCPP_BEGIN_NAMESPACE_LFTS_V2
72 # if _LIBCPP_STD_VER >= 17
77 using element_type = _Wp;
80 _LIBCPP_HIDE_FROM_ABI constexpr observer_ptr() noexcept : __ptr_(nullptr) {}
81 _LIBCPP_HIDE_FROM_ABI constexpr observer_ptr(nullptr_t) noexcept : __ptr_(nullptr) {}
82 _LIBCPP_HIDE_FROM_ABI constexpr explicit observer_ptr(element_type* __p) noexcept : __ptr_(__p) {}
84 template <class _W2, __enable_if_t<is_convertible<_W2*, _Wp*>::value, int> = 0>
85 _LIBCPP_HIDE_FROM_ABI constexpr observer_ptr(observer_ptr<_W2> __other) noexcept : __ptr_(__other.get()) {}
88 _LIBCPP_HIDE_FROM_ABI constexpr element_type* get() const noexcept { return __ptr_; }
89 _LIBCPP_HIDE_FROM_ABI constexpr add_lvalue_reference_t<_Wp> operator*() const { return *__ptr_; }
90 _LIBCPP_HIDE_FROM_ABI constexpr element_type* operator->() const noexcept { return __ptr_; }
91 _LIBCPP_HIDE_FROM_ABI constexpr explicit operator bool() const noexcept { return __ptr_ != nullptr; }
94 _LIBCPP_HIDE_FROM_ABI constexpr explicit operator element_type*() const noexcept { return __ptr_; }
97 _LIBCPP_HIDE_FROM_ABI constexpr void reset(element_type* __p = nullptr) noexcept { __ptr_ = __p; }
98 _LIBCPP_HIDE_FROM_ABI constexpr void swap(observer_ptr& __other) noexcept {
99 observer_ptr __tmp = __other;
103 _LIBCPP_HIDE_FROM_ABI constexpr element_type* release() noexcept {
110 element_type* __ptr_;
116 _LIBCPP_HIDE_FROM_ABI constexpr void swap(observer_ptr<_Wp>& __a, observer_ptr<_Wp>& __b) noexcept {
121 _LIBCPP_HIDE_FROM_ABI observer_ptr<_Wp> make_observer(_Wp* __ptr) noexcept {
122 return observer_ptr<_Wp>{__ptr};
125 template <class _W1, class _W2>
126 _LIBCPP_HIDE_FROM_ABI bool operator==(observer_ptr<_W1> __a, observer_ptr<_W2> __b) {
127 return __a.get() == __b.get();
130 template <class _W1, class _W2>
131 _LIBCPP_HIDE_FROM_ABI bool operator!=(observer_ptr<_W1> __a, observer_ptr<_W2> __b) {
132 return !(__a == __b);
136 _LIBCPP_HIDE_FROM_ABI bool operator==(observer_ptr<_Wp> __p, nullptr_t) {
141 _LIBCPP_HIDE_FROM_ABI bool operator==(nullptr_t, observer_ptr<_Wp> __p) {
146 _LIBCPP_HIDE_FROM_ABI bool operator!=(observer_ptr<_Wp> __p, nullptr_t) {
151 _LIBCPP_HIDE_FROM_ABI bool operator!=(nullptr_t, observer_ptr<_Wp> __p) {
155 template <class _W1, class _W2>
156 _LIBCPP_HIDE_FROM_ABI bool operator<(observer_ptr<_W1> __a, observer_ptr<_W2> __b) {
157 return std::less<typename std::common_type<_W1*, _W2*>::type>()(__a.get(), __b.get());
160 template <class _W1, class _W2>
161 _LIBCPP_HIDE_FROM_ABI bool operator>(observer_ptr<_W1> __a, observer_ptr<_W2> __b) {
165 template <class _W1, class _W2>
166 _LIBCPP_HIDE_FROM_ABI bool operator<=(observer_ptr<_W1> __a, observer_ptr<_W2> __b) {
170 template <class _W1, class _W2>
171 _LIBCPP_HIDE_FROM_ABI bool operator>=(observer_ptr<_W1> __a, observer_ptr<_W2> __b) {
175 # endif // _LIBCPP_STD_VER >= 17
177 _LIBCPP_END_NAMESPACE_LFTS_V2
179 _LIBCPP_BEGIN_NAMESPACE_STD
183 # if _LIBCPP_STD_VER >= 17
185 struct hash<experimental::observer_ptr<_Tp>> {
186 _LIBCPP_HIDE_FROM_ABI size_t operator()(const experimental::observer_ptr<_Tp>& __ptr) const noexcept {
187 return hash<_Tp*>()(__ptr.get());
190 # endif // _LIBCPP_STD_VER >= 17
192 _LIBCPP_END_NAMESPACE_STD
194 #endif // _LIBCPP_ENABLE_EXPERIMENTAL
196 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
201 #endif /* _LIBCPP_EXPERIMENTAL_MEMORY */