1 // <experimental/memory> -*- C++ -*-
3 // Copyright (C) 2015-2019 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file experimental/memory
26 * This is a TS C++ Library header.
30 // N4336 Working Draft, C++ Extensions for Library Fundamentals, Version 2
33 #ifndef _GLIBCXX_EXPERIMENTAL_MEMORY
34 #define _GLIBCXX_EXPERIMENTAL_MEMORY 1
36 #pragma GCC system_header
38 #if __cplusplus >= 201402L
41 #include <type_traits>
43 #include <experimental/bits/shared_ptr.h>
44 #include <bits/functional_hash.h>
46 namespace std _GLIBCXX_VISIBILITY(default)
48 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50 namespace experimental
52 inline namespace fundamentals_v2
54 #define __cpp_lib_experimental_observer_ptr 201411
56 template <typename _Tp>
60 // publish our template parameter and variations thereof
61 using element_type = _Tp;
62 using __pointer = add_pointer_t<_Tp>; // exposition-only
63 using __reference = add_lvalue_reference_t<_Tp>; // exposition-only
65 // 3.2.2, observer_ptr constructors
67 constexpr observer_ptr() noexcept
71 // pointer-accepting c'tors
72 constexpr observer_ptr(nullptr_t) noexcept
76 constexpr explicit observer_ptr(__pointer __p) noexcept
80 // copying c'tors (in addition to compiler-generated copy c'tor)
81 template <typename _Up,
82 typename = typename enable_if<
83 is_convertible<typename add_pointer<_Up>::type, __pointer
86 constexpr observer_ptr(observer_ptr<_Up> __p) noexcept
91 // 3.2.3, observer_ptr observers
105 operator->() const noexcept
110 constexpr explicit operator bool() const noexcept
112 return get() != nullptr;
115 // 3.2.4, observer_ptr conversions
116 constexpr explicit operator __pointer() const noexcept
121 // 3.2.5, observer_ptr modifiers
125 __pointer __tmp = get();
131 reset(__pointer __p = nullptr) noexcept
137 swap(observer_ptr& __p) noexcept
139 std::swap(__t, __p.__t);
146 template<typename _Tp>
148 swap(observer_ptr<_Tp>& __p1, observer_ptr<_Tp>& __p2) noexcept
153 template<typename _Tp>
155 make_observer(_Tp* __p) noexcept
157 return observer_ptr<_Tp>(__p);
160 template<typename _Tp, typename _Up>
162 operator==(observer_ptr<_Tp> __p1, observer_ptr<_Up> __p2)
164 return __p1.get() == __p2.get();
167 template<typename _Tp, typename _Up>
169 operator!=(observer_ptr<_Tp> __p1, observer_ptr<_Up> __p2)
171 return !(__p1 == __p2);
174 template<typename _Tp>
176 operator==(observer_ptr<_Tp> __p, nullptr_t) noexcept
181 template<typename _Tp>
183 operator==(nullptr_t, observer_ptr<_Tp> __p) noexcept
188 template<typename _Tp>
190 operator!=(observer_ptr<_Tp> __p, nullptr_t) noexcept
195 template<typename _Tp>
197 operator!=(nullptr_t, observer_ptr<_Tp> __p) noexcept
202 template<typename _Tp, typename _Up>
204 operator<(observer_ptr<_Tp> __p1, observer_ptr<_Up> __p2)
206 return std::less<typename common_type<typename add_pointer<_Tp>::type,
207 typename add_pointer<_Up>::type
209 >{}(__p1.get(), __p2.get());
212 template<typename _Tp, typename _Up>
214 operator>(observer_ptr<_Tp> __p1, observer_ptr<_Up> __p2)
219 template<typename _Tp, typename _Up>
221 operator<=(observer_ptr<_Tp> __p1, observer_ptr<_Up> __p2)
223 return !(__p2 < __p1);
226 template<typename _Tp, typename _Up>
228 operator>=(observer_ptr<_Tp> __p1, observer_ptr<_Up> __p2)
230 return !(__p1 < __p2);
232 } // namespace fundamentals_v2
233 } // namespace experimental
235 template <typename _Tp>
236 struct hash<experimental::observer_ptr<_Tp>>
238 using result_type = size_t;
239 using argument_type = experimental::observer_ptr<_Tp>;
242 operator()(const experimental::observer_ptr<_Tp>& __t) const
243 noexcept(noexcept(hash<typename add_pointer<_Tp>::type> {}(__t.get())))
245 return hash<typename add_pointer<_Tp>::type> {}(__t.get());
250 _GLIBCXX_END_NAMESPACE_VERSION
253 #endif // __cplusplus <= 201103L
255 #endif // _GLIBCXX_EXPERIMENTAL_MEMORY