]>
Commit | Line | Data |
---|---|---|
5b9daa7e | 1 | // shared_ptr and weak_ptr implementation -*- C++ -*- |
b758b22a | 2 | |
83ffe9cd | 3 | // Copyright (C) 2007-2023 Free Software Foundation, Inc. |
b758b22a BK |
4 | // |
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 | |
748086b7 | 8 | // Free Software Foundation; either version 3, or (at your option) |
b758b22a BK |
9 | // any later version. |
10 | ||
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. | |
15 | ||
748086b7 JJ |
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. | |
19 | ||
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/>. | |
b758b22a | 24 | |
8e32aa11 BK |
25 | // GCC Note: Based on files from version 1.32.0 of the Boost library. |
26 | ||
b758b22a BK |
27 | // shared_count.hpp |
28 | // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd. | |
29 | ||
30 | // shared_ptr.hpp | |
31 | // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes. | |
32 | // Copyright (C) 2001, 2002, 2003 Peter Dimov | |
33 | ||
34 | // weak_ptr.hpp | |
35 | // Copyright (C) 2001, 2002, 2003 Peter Dimov | |
36 | ||
37 | // enable_shared_from_this.hpp | |
38 | // Copyright (C) 2002 Peter Dimov | |
39 | ||
40 | // Distributed under the Boost Software License, Version 1.0. (See | |
41 | // accompanying file LICENSE_1_0.txt or copy at | |
42 | // http://www.boost.org/LICENSE_1_0.txt) | |
43 | ||
6b4f8906 | 44 | /** @file |
b758b22a | 45 | * This is an internal header file, included by other library headers. |
f910786b | 46 | * Do not attempt to use it directly. @headername{memory} |
b758b22a BK |
47 | */ |
48 | ||
32fdf2f4 JW |
49 | #ifndef _SHARED_PTR_H |
50 | #define _SHARED_PTR_H 1 | |
51 | ||
b1e7c6fc | 52 | #include <iosfwd> // std::basic_ostream |
8e32aa11 | 53 | #include <bits/shared_ptr_base.h> |
aaf0ca6f | 54 | |
12ffa228 BK |
55 | namespace std _GLIBCXX_VISIBILITY(default) |
56 | { | |
57 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
5b9daa7e BK |
58 | |
59 | /** | |
60 | * @addtogroup pointer_abstractions | |
61 | * @{ | |
62 | */ | |
63 | ||
302b6996 JW |
64 | // 20.7.2.2.11 shared_ptr I/O |
65 | ||
66 | /// Write the stored pointer to an ostream. | |
67 | /// @relates shared_ptr | |
8e32aa11 | 68 | template<typename _Ch, typename _Tr, typename _Tp, _Lock_policy _Lp> |
d779a591 | 69 | inline std::basic_ostream<_Ch, _Tr>& |
8e32aa11 BK |
70 | operator<<(std::basic_ostream<_Ch, _Tr>& __os, |
71 | const __shared_ptr<_Tp, _Lp>& __p) | |
a25ce4db | 72 | { |
8e32aa11 BK |
73 | __os << __p.get(); |
74 | return __os; | |
a25ce4db | 75 | } |
459f9f82 | 76 | |
8e32aa11 BK |
77 | template<typename _Del, typename _Tp, _Lock_policy _Lp> |
78 | inline _Del* | |
cf70f97b | 79 | get_deleter(const __shared_ptr<_Tp, _Lp>& __p) noexcept |
70826946 | 80 | { |
0f3d27f0 | 81 | #if __cpp_rtti |
70826946 JW |
82 | return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del))); |
83 | #else | |
84 | return 0; | |
85 | #endif | |
86 | } | |
b758b22a | 87 | |
78a8b676 | 88 | /// 20.7.2.2.10 shared_ptr get_deleter |
302b6996 JW |
89 | |
90 | /// If `__p` has a deleter of type `_Del`, return a pointer to it. | |
efa9d8ee | 91 | /// @relates shared_ptr |
78a8b676 JW |
92 | template<typename _Del, typename _Tp> |
93 | inline _Del* | |
94 | get_deleter(const shared_ptr<_Tp>& __p) noexcept | |
95 | { | |
96 | #if __cpp_rtti | |
97 | return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del))); | |
98 | #else | |
99 | return 0; | |
100 | #endif | |
101 | } | |
a15024e6 | 102 | |
9a0b518a JW |
103 | /// @cond undocumented |
104 | ||
105 | // Constraint for overloads taking non-array types. | |
106 | #if __cpp_concepts && __cpp_lib_type_trait_variable_templates | |
107 | template<typename _Tp> | |
108 | requires (!is_array_v<_Tp>) | |
109 | using _NonArray = _Tp; | |
110 | #else | |
111 | template<typename _Tp> | |
112 | using _NonArray = __enable_if_t<!is_array<_Tp>::value, _Tp>; | |
113 | #endif | |
114 | ||
115 | #if __cpp_lib_shared_ptr_arrays >= 201707L | |
116 | // Constraint for overloads taking array types with unknown bound, U[]. | |
117 | #if __cpp_concepts | |
118 | template<typename _Tp> | |
119 | requires is_array_v<_Tp> && (extent_v<_Tp> == 0) | |
120 | using _UnboundedArray = _Tp; | |
121 | #else | |
122 | template<typename _Tp> | |
123 | using _UnboundedArray | |
124 | = __enable_if_t<__is_array_unknown_bounds<_Tp>::value, _Tp>; | |
125 | #endif | |
126 | ||
127 | // Constraint for overloads taking array types with known bound, U[N]. | |
128 | #if __cpp_concepts | |
129 | template<typename _Tp> | |
130 | requires (extent_v<_Tp> != 0) | |
131 | using _BoundedArray = _Tp; | |
132 | #else | |
133 | template<typename _Tp> | |
134 | using _BoundedArray | |
135 | = __enable_if_t<__is_array_known_bounds<_Tp>::value, _Tp>; | |
136 | #endif | |
137 | ||
138 | #if __cpp_lib_smart_ptr_for_overwrite | |
139 | // Constraint for overloads taking either non-array or bounded array, U[N]. | |
140 | #if __cpp_concepts | |
141 | template<typename _Tp> | |
142 | requires (!is_array_v<_Tp>) || (extent_v<_Tp> != 0) | |
143 | using _NotUnboundedArray = _Tp; | |
144 | #else | |
145 | template<typename _Tp> | |
146 | using _NotUnboundedArray | |
147 | = __enable_if_t<!__is_array_unknown_bounds<_Tp>::value, _Tp>; | |
148 | #endif | |
149 | #endif // smart_ptr_for_overwrite | |
150 | #endif // shared_ptr_arrays | |
151 | ||
152 | /// @endcond | |
153 | ||
8e32aa11 BK |
154 | /** |
155 | * @brief A smart pointer with reference-counted copy semantics. | |
30b300de JW |
156 | * @headerfile memory |
157 | * @since C++11 | |
8e32aa11 | 158 | * |
efa9d8ee JW |
159 | * A `shared_ptr` object is either empty or _owns_ a pointer passed |
160 | * to the constructor. Copies of a `shared_ptr` share ownership of | |
161 | * the same pointer. When the last `shared_ptr` that owns the pointer | |
162 | * is destroyed or reset, the owned pointer is freed (either by `delete` | |
163 | * or by invoking a custom deleter that was passed to the constructor). | |
164 | * | |
165 | * A `shared_ptr` also stores another pointer, which is usually | |
166 | * (but not always) the same pointer as it owns. The stored pointer | |
167 | * can be retrieved by calling the `get()` member function. | |
302b6996 JW |
168 | * |
169 | * The equality and relational operators for `shared_ptr` only compare | |
170 | * the stored pointer returned by `get()`, not the owned pointer. | |
171 | * To test whether two `shared_ptr` objects share ownership of the same | |
172 | * pointer see `std::shared_ptr::owner_before` and `std::owner_less`. | |
8e32aa11 | 173 | */ |
c8bd30dd | 174 | template<typename _Tp> |
8e32aa11 | 175 | class shared_ptr : public __shared_ptr<_Tp> |
459f9f82 | 176 | { |
a2e0054e JW |
177 | template<typename... _Args> |
178 | using _Constructible = typename enable_if< | |
179 | is_constructible<__shared_ptr<_Tp>, _Args...>::value | |
180 | >::type; | |
7663cae2 | 181 | |
a2e0054e JW |
182 | template<typename _Arg> |
183 | using _Assignable = typename enable_if< | |
184 | is_assignable<__shared_ptr<_Tp>&, _Arg>::value, shared_ptr& | |
185 | >::type; | |
d8af84e6 | 186 | |
459f9f82 | 187 | public: |
f21f4463 | 188 | |
302b6996 | 189 | /// The type pointed to by the stored pointer, remove_extent_t<_Tp> |
a2e0054e JW |
190 | using element_type = typename __shared_ptr<_Tp>::element_type; |
191 | ||
302b6996 | 192 | #if __cplusplus >= 201703L |
9a0b518a | 193 | # define __cpp_lib_shared_ptr_weak_type 201606L |
302b6996 | 194 | /// The corresponding weak_ptr type for this shared_ptr |
30b300de | 195 | /// @since C++17 |
f21f4463 JW |
196 | using weak_type = weak_ptr<_Tp>; |
197 | #endif | |
8e32aa11 BK |
198 | /** |
199 | * @brief Construct an empty %shared_ptr. | |
459f9f82 PC |
200 | * @post use_count()==0 && get()==0 |
201 | */ | |
a2e0054e | 202 | constexpr shared_ptr() noexcept : __shared_ptr<_Tp>() { } |
b758b22a | 203 | |
302b6996 | 204 | shared_ptr(const shared_ptr&) noexcept = default; ///< Copy constructor |
7897a1c0 | 205 | |
8e32aa11 BK |
206 | /** |
207 | * @brief Construct a %shared_ptr that owns the pointer @a __p. | |
c8bd30dd PC |
208 | * @param __p A pointer that is convertible to element_type*. |
209 | * @post use_count() == 1 && get() == __p | |
210 | * @throw std::bad_alloc, in which case @c delete @a __p is called. | |
459f9f82 | 211 | */ |
a2e0054e JW |
212 | template<typename _Yp, typename = _Constructible<_Yp*>> |
213 | explicit | |
214 | shared_ptr(_Yp* __p) : __shared_ptr<_Tp>(__p) { } | |
b758b22a | 215 | |
8e32aa11 BK |
216 | /** |
217 | * @brief Construct a %shared_ptr that owns the pointer @a __p | |
c8bd30dd PC |
218 | * and the deleter @a __d. |
219 | * @param __p A pointer. | |
220 | * @param __d A deleter. | |
221 | * @post use_count() == 1 && get() == __p | |
222 | * @throw std::bad_alloc, in which case @a __d(__p) is called. | |
8e32aa11 BK |
223 | * |
224 | * Requirements: _Deleter's copy constructor and destructor must | |
225 | * not throw | |
226 | * | |
227 | * __shared_ptr will release __p by calling __d(__p) | |
459f9f82 | 228 | */ |
a2e0054e JW |
229 | template<typename _Yp, typename _Deleter, |
230 | typename = _Constructible<_Yp*, _Deleter>> | |
231 | shared_ptr(_Yp* __p, _Deleter __d) | |
53d01fd9 | 232 | : __shared_ptr<_Tp>(__p, std::move(__d)) { } |
8e32aa11 | 233 | |
6d00745b JW |
234 | /** |
235 | * @brief Construct a %shared_ptr that owns a null pointer | |
236 | * and the deleter @a __d. | |
237 | * @param __p A null pointer constant. | |
238 | * @param __d A deleter. | |
239 | * @post use_count() == 1 && get() == __p | |
240 | * @throw std::bad_alloc, in which case @a __d(__p) is called. | |
241 | * | |
242 | * Requirements: _Deleter's copy constructor and destructor must | |
243 | * not throw | |
244 | * | |
245 | * The last owner will call __d(__p) | |
246 | */ | |
247 | template<typename _Deleter> | |
248 | shared_ptr(nullptr_t __p, _Deleter __d) | |
53d01fd9 | 249 | : __shared_ptr<_Tp>(__p, std::move(__d)) { } |
6d00745b | 250 | |
8e32aa11 BK |
251 | /** |
252 | * @brief Construct a %shared_ptr that owns the pointer @a __p | |
aaf0ca6f JW |
253 | * and the deleter @a __d. |
254 | * @param __p A pointer. | |
255 | * @param __d A deleter. | |
256 | * @param __a An allocator. | |
257 | * @post use_count() == 1 && get() == __p | |
258 | * @throw std::bad_alloc, in which case @a __d(__p) is called. | |
8e32aa11 BK |
259 | * |
260 | * Requirements: _Deleter's copy constructor and destructor must | |
261 | * not throw _Alloc's copy constructor and destructor must not | |
262 | * throw. | |
263 | * | |
264 | * __shared_ptr will release __p by calling __d(__p) | |
aaf0ca6f | 265 | */ |
a2e0054e JW |
266 | template<typename _Yp, typename _Deleter, typename _Alloc, |
267 | typename = _Constructible<_Yp*, _Deleter, _Alloc>> | |
268 | shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a) | |
53d01fd9 | 269 | : __shared_ptr<_Tp>(__p, std::move(__d), std::move(__a)) { } |
8e32aa11 | 270 | |
6d00745b JW |
271 | /** |
272 | * @brief Construct a %shared_ptr that owns a null pointer | |
273 | * and the deleter @a __d. | |
274 | * @param __p A null pointer constant. | |
275 | * @param __d A deleter. | |
276 | * @param __a An allocator. | |
277 | * @post use_count() == 1 && get() == __p | |
278 | * @throw std::bad_alloc, in which case @a __d(__p) is called. | |
279 | * | |
280 | * Requirements: _Deleter's copy constructor and destructor must | |
281 | * not throw _Alloc's copy constructor and destructor must not | |
282 | * throw. | |
283 | * | |
284 | * The last owner will call __d(__p) | |
285 | */ | |
286 | template<typename _Deleter, typename _Alloc> | |
403b89a8 | 287 | shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a) |
53d01fd9 | 288 | : __shared_ptr<_Tp>(__p, std::move(__d), std::move(__a)) { } |
6d00745b | 289 | |
8e32aa11 | 290 | // Aliasing constructor |
aaf0ca6f | 291 | |
8e32aa11 | 292 | /** |
fb3fc4bd JW |
293 | * @brief Constructs a `shared_ptr` instance that stores `__p` |
294 | * and shares ownership with `__r`. | |
295 | * @param __r A `shared_ptr`. | |
296 | * @param __p A pointer that will remain valid while `*__r` is valid. | |
297 | * @post `get() == __p && use_count() == __r.use_count()` | |
aaf0ca6f | 298 | * |
fb3fc4bd JW |
299 | * This can be used to construct a `shared_ptr` to a sub-object |
300 | * of an object managed by an existing `shared_ptr`. The complete | |
301 | * object will remain valid while any `shared_ptr` owns it, even | |
302 | * if they don't store a pointer to the complete object. | |
aaf0ca6f JW |
303 | * |
304 | * @code | |
fb3fc4bd | 305 | * shared_ptr<pair<int,int>> pii(new pair<int,int>()); |
aaf0ca6f JW |
306 | * shared_ptr<int> pi(pii, &pii->first); |
307 | * assert(pii.use_count() == 2); | |
308 | * @endcode | |
309 | */ | |
a2e0054e JW |
310 | template<typename _Yp> |
311 | shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) noexcept | |
8e32aa11 BK |
312 | : __shared_ptr<_Tp>(__r, __p) { } |
313 | ||
fb3fc4bd JW |
314 | #if __cplusplus > 201703L |
315 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
316 | // 2996. Missing rvalue overloads for shared_ptr operations | |
317 | /** | |
318 | * @brief Constructs a `shared_ptr` instance that stores `__p` | |
319 | * and shares ownership with `__r`. | |
320 | * @param __r A `shared_ptr`. | |
321 | * @param __p A pointer that will remain valid while `*__r` is valid. | |
322 | * @post `get() == __p && !__r.use_count() && !__r.get()` | |
30b300de | 323 | * @since C++17 |
fb3fc4bd JW |
324 | * |
325 | * This can be used to construct a `shared_ptr` to a sub-object | |
326 | * of an object managed by an existing `shared_ptr`. The complete | |
327 | * object will remain valid while any `shared_ptr` owns it, even | |
328 | * if they don't store a pointer to the complete object. | |
329 | * | |
330 | * @code | |
331 | * shared_ptr<pair<int,int>> pii(new pair<int,int>()); | |
332 | * shared_ptr<int> pi1(pii, &pii->first); | |
333 | * assert(pii.use_count() == 2); | |
334 | * shared_ptr<int> pi2(std::move(pii), &pii->second); | |
335 | * assert(pii.use_count() == 0); | |
336 | * @endcode | |
337 | */ | |
338 | template<typename _Yp> | |
339 | shared_ptr(shared_ptr<_Yp>&& __r, element_type* __p) noexcept | |
340 | : __shared_ptr<_Tp>(std::move(__r), __p) { } | |
341 | #endif | |
8e32aa11 BK |
342 | /** |
343 | * @brief If @a __r is empty, constructs an empty %shared_ptr; | |
344 | * otherwise construct a %shared_ptr that shares ownership | |
c8bd30dd | 345 | * with @a __r. |
8e32aa11 | 346 | * @param __r A %shared_ptr. |
c8bd30dd | 347 | * @post get() == __r.get() && use_count() == __r.use_count() |
459f9f82 | 348 | */ |
a2e0054e JW |
349 | template<typename _Yp, |
350 | typename = _Constructible<const shared_ptr<_Yp>&>> | |
351 | shared_ptr(const shared_ptr<_Yp>& __r) noexcept | |
5aadb69b | 352 | : __shared_ptr<_Tp>(__r) { } |
459f9f82 | 353 | |
8e32aa11 BK |
354 | /** |
355 | * @brief Move-constructs a %shared_ptr instance from @a __r. | |
356 | * @param __r A %shared_ptr rvalue. | |
aaf0ca6f JW |
357 | * @post *this contains the old value of @a __r, @a __r is empty. |
358 | */ | |
cf70f97b | 359 | shared_ptr(shared_ptr&& __r) noexcept |
8e32aa11 | 360 | : __shared_ptr<_Tp>(std::move(__r)) { } |
aaf0ca6f | 361 | |
8e32aa11 BK |
362 | /** |
363 | * @brief Move-constructs a %shared_ptr instance from @a __r. | |
364 | * @param __r A %shared_ptr rvalue. | |
aaf0ca6f JW |
365 | * @post *this contains the old value of @a __r, @a __r is empty. |
366 | */ | |
a2e0054e JW |
367 | template<typename _Yp, typename = _Constructible<shared_ptr<_Yp>>> |
368 | shared_ptr(shared_ptr<_Yp>&& __r) noexcept | |
8e32aa11 BK |
369 | : __shared_ptr<_Tp>(std::move(__r)) { } |
370 | ||
371 | /** | |
372 | * @brief Constructs a %shared_ptr that shares ownership with @a __r | |
c8bd30dd PC |
373 | * and stores a copy of the pointer stored in @a __r. |
374 | * @param __r A weak_ptr. | |
375 | * @post use_count() == __r.use_count() | |
376 | * @throw bad_weak_ptr when __r.expired(), | |
459f9f82 PC |
377 | * in which case the constructor has no effect. |
378 | */ | |
a2e0054e JW |
379 | template<typename _Yp, typename = _Constructible<const weak_ptr<_Yp>&>> |
380 | explicit shared_ptr(const weak_ptr<_Yp>& __r) | |
459f9f82 | 381 | : __shared_ptr<_Tp>(__r) { } |
c8bd30dd | 382 | |
e1bcd685 | 383 | #if _GLIBCXX_USE_DEPRECATED |
a2c0a194 JW |
384 | #pragma GCC diagnostic push |
385 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" | |
a2e0054e JW |
386 | template<typename _Yp, typename = _Constructible<auto_ptr<_Yp>>> |
387 | shared_ptr(auto_ptr<_Yp>&& __r); | |
a2c0a194 | 388 | #pragma GCC diagnostic pop |
40abbf1f | 389 | #endif |
459f9f82 | 390 | |
d8af84e6 JW |
391 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
392 | // 2399. shared_ptr's constructor from unique_ptr should be constrained | |
a2e0054e JW |
393 | template<typename _Yp, typename _Del, |
394 | typename = _Constructible<unique_ptr<_Yp, _Del>>> | |
395 | shared_ptr(unique_ptr<_Yp, _Del>&& __r) | |
640cbe76 | 396 | : __shared_ptr<_Tp>(std::move(__r)) { } |
459f9f82 | 397 | |
b2343559 JW |
398 | #if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED |
399 | // This non-standard constructor exists to support conversions that | |
400 | // were possible in C++11 and C++14 but are ill-formed in C++17. | |
401 | // If an exception is thrown this constructor has no effect. | |
402 | template<typename _Yp, typename _Del, | |
403 | _Constructible<unique_ptr<_Yp, _Del>, __sp_array_delete>* = 0> | |
404 | shared_ptr(unique_ptr<_Yp, _Del>&& __r) | |
405 | : __shared_ptr<_Tp>(std::move(__r), __sp_array_delete()) { } | |
406 | #endif | |
407 | ||
6d00745b JW |
408 | /** |
409 | * @brief Construct an empty %shared_ptr. | |
6d00745b JW |
410 | * @post use_count() == 0 && get() == nullptr |
411 | */ | |
33fbb358 | 412 | constexpr shared_ptr(nullptr_t) noexcept : shared_ptr() { } |
6d00745b | 413 | |
a2e70335 | 414 | shared_ptr& operator=(const shared_ptr&) noexcept = default; |
7897a1c0 | 415 | |
a2e0054e JW |
416 | template<typename _Yp> |
417 | _Assignable<const shared_ptr<_Yp>&> | |
418 | operator=(const shared_ptr<_Yp>& __r) noexcept | |
8e32aa11 | 419 | { |
459f9f82 PC |
420 | this->__shared_ptr<_Tp>::operator=(__r); |
421 | return *this; | |
422 | } | |
a25ce4db | 423 | |
e1bcd685 | 424 | #if _GLIBCXX_USE_DEPRECATED |
a2c0a194 JW |
425 | #pragma GCC diagnostic push |
426 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" | |
a2e0054e JW |
427 | template<typename _Yp> |
428 | _Assignable<auto_ptr<_Yp>> | |
429 | operator=(auto_ptr<_Yp>&& __r) | |
8e32aa11 | 430 | { |
640cbe76 | 431 | this->__shared_ptr<_Tp>::operator=(std::move(__r)); |
459f9f82 PC |
432 | return *this; |
433 | } | |
a2c0a194 | 434 | #pragma GCC diagnostic pop |
40abbf1f | 435 | #endif |
aaf0ca6f | 436 | |
aaf0ca6f | 437 | shared_ptr& |
cf70f97b | 438 | operator=(shared_ptr&& __r) noexcept |
aaf0ca6f | 439 | { |
8e32aa11 BK |
440 | this->__shared_ptr<_Tp>::operator=(std::move(__r)); |
441 | return *this; | |
aaf0ca6f | 442 | } |
8e32aa11 | 443 | |
a2e0054e JW |
444 | template<class _Yp> |
445 | _Assignable<shared_ptr<_Yp>> | |
446 | operator=(shared_ptr<_Yp>&& __r) noexcept | |
8e32aa11 BK |
447 | { |
448 | this->__shared_ptr<_Tp>::operator=(std::move(__r)); | |
449 | return *this; | |
450 | } | |
aaf0ca6f | 451 | |
a2e0054e JW |
452 | template<typename _Yp, typename _Del> |
453 | _Assignable<unique_ptr<_Yp, _Del>> | |
454 | operator=(unique_ptr<_Yp, _Del>&& __r) | |
8e32aa11 | 455 | { |
640cbe76 JW |
456 | this->__shared_ptr<_Tp>::operator=(std::move(__r)); |
457 | return *this; | |
458 | } | |
459 | ||
aaf0ca6f JW |
460 | private: |
461 | // This constructor is non-standard, it is used by allocate_shared. | |
462 | template<typename _Alloc, typename... _Args> | |
fb54aa59 JW |
463 | shared_ptr(_Sp_alloc_shared_tag<_Alloc> __tag, _Args&&... __args) |
464 | : __shared_ptr<_Tp>(__tag, std::forward<_Args>(__args)...) | |
8e32aa11 | 465 | { } |
aaf0ca6f | 466 | |
a2e0054e | 467 | template<typename _Yp, typename _Alloc, typename... _Args> |
9a0b518a JW |
468 | friend shared_ptr<_NonArray<_Yp>> |
469 | allocate_shared(const _Alloc&, _Args&&...); | |
470 | ||
471 | template<typename _Yp, typename... _Args> | |
472 | friend shared_ptr<_NonArray<_Yp>> | |
473 | make_shared(_Args&&...); | |
474 | ||
475 | #if __cpp_lib_shared_ptr_arrays >= 201707L | |
476 | // This constructor is non-standard, it is used by allocate_shared<T[]>. | |
477 | template<typename _Alloc, typename _Init = const remove_extent_t<_Tp>*> | |
478 | shared_ptr(const _Sp_counted_array_base<_Alloc>& __a, | |
479 | _Init __init = nullptr) | |
480 | : __shared_ptr<_Tp>(__a, __init) | |
481 | { } | |
482 | ||
483 | template<typename _Yp, typename _Alloc> | |
484 | friend shared_ptr<_UnboundedArray<_Yp>> | |
485 | allocate_shared(const _Alloc&, size_t); | |
486 | ||
487 | template<typename _Yp> | |
488 | friend shared_ptr<_UnboundedArray<_Yp>> | |
489 | make_shared(size_t); | |
490 | ||
491 | template<typename _Yp, typename _Alloc> | |
492 | friend shared_ptr<_UnboundedArray<_Yp>> | |
493 | allocate_shared(const _Alloc&, size_t, const remove_extent_t<_Yp>&); | |
494 | ||
495 | template<typename _Yp> | |
496 | friend shared_ptr<_UnboundedArray<_Yp>> | |
497 | make_shared(size_t, const remove_extent_t<_Yp>&); | |
498 | ||
499 | template<typename _Yp, typename _Alloc> | |
500 | friend shared_ptr<_BoundedArray<_Yp>> | |
501 | allocate_shared(const _Alloc&); | |
502 | ||
503 | template<typename _Yp> | |
504 | friend shared_ptr<_BoundedArray<_Yp>> | |
505 | make_shared(); | |
506 | ||
507 | template<typename _Yp, typename _Alloc> | |
508 | friend shared_ptr<_BoundedArray<_Yp>> | |
509 | allocate_shared(const _Alloc&, const remove_extent_t<_Yp>&); | |
510 | ||
511 | template<typename _Yp> | |
512 | friend shared_ptr<_BoundedArray<_Yp>> | |
513 | make_shared(const remove_extent_t<_Yp>&); | |
514 | ||
515 | #if __cpp_lib_smart_ptr_for_overwrite | |
516 | template<typename _Yp, typename _Alloc> | |
517 | friend shared_ptr<_NotUnboundedArray<_Yp>> | |
518 | allocate_shared_for_overwrite(const _Alloc&); | |
519 | ||
520 | template<typename _Yp> | |
521 | friend shared_ptr<_NotUnboundedArray<_Yp>> | |
522 | make_shared_for_overwrite(); | |
523 | ||
524 | template<typename _Yp, typename _Alloc> | |
525 | friend shared_ptr<_UnboundedArray<_Yp>> | |
526 | allocate_shared_for_overwrite(const _Alloc&, size_t); | |
527 | ||
528 | template<typename _Yp> | |
529 | friend shared_ptr<_UnboundedArray<_Yp>> | |
530 | make_shared_for_overwrite(size_t); | |
531 | #endif | |
532 | #endif | |
156b60e0 JW |
533 | |
534 | // This constructor is non-standard, it is used by weak_ptr::lock(). | |
945151b7 | 535 | shared_ptr(const weak_ptr<_Tp>& __r, std::nothrow_t) noexcept |
156b60e0 JW |
536 | : __shared_ptr<_Tp>(__r, std::nothrow) { } |
537 | ||
538 | friend class weak_ptr<_Tp>; | |
459f9f82 | 539 | }; |
b758b22a | 540 | |
af181c91 JW |
541 | #if __cpp_deduction_guides >= 201606 |
542 | template<typename _Tp> | |
543 | shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>; | |
544 | template<typename _Tp, typename _Del> | |
545 | shared_ptr(unique_ptr<_Tp, _Del>) -> shared_ptr<_Tp>; | |
546 | #endif | |
547 | ||
9ab48d6e | 548 | // 20.7.2.2.7 shared_ptr comparisons |
302b6996 JW |
549 | |
550 | /// @relates shared_ptr @{ | |
551 | ||
552 | /// Equality operator for shared_ptr objects, compares the stored pointers | |
a2e0054e | 553 | template<typename _Tp, typename _Up> |
ec0d5371 | 554 | _GLIBCXX_NODISCARD inline bool |
a2e0054e | 555 | operator==(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept |
8dd5e93a JW |
556 | { return __a.get() == __b.get(); } |
557 | ||
302b6996 | 558 | /// shared_ptr comparison with nullptr |
3abeaf8f | 559 | template<typename _Tp> |
ec0d5371 | 560 | _GLIBCXX_NODISCARD inline bool |
355e71b9 PC |
561 | operator==(const shared_ptr<_Tp>& __a, nullptr_t) noexcept |
562 | { return !__a; } | |
3abeaf8f | 563 | |
f5fa62ed JW |
564 | #ifdef __cpp_lib_three_way_comparison |
565 | template<typename _Tp, typename _Up> | |
566 | inline strong_ordering | |
567 | operator<=>(const shared_ptr<_Tp>& __a, | |
568 | const shared_ptr<_Up>& __b) noexcept | |
569 | { return compare_three_way()(__a.get(), __b.get()); } | |
570 | ||
571 | template<typename _Tp> | |
572 | inline strong_ordering | |
573 | operator<=>(const shared_ptr<_Tp>& __a, nullptr_t) noexcept | |
574 | { | |
575 | using pointer = typename shared_ptr<_Tp>::element_type*; | |
576 | return compare_three_way()(__a.get(), static_cast<pointer>(nullptr)); | |
577 | } | |
578 | #else | |
302b6996 | 579 | /// shared_ptr comparison with nullptr |
3abeaf8f | 580 | template<typename _Tp> |
ec0d5371 | 581 | _GLIBCXX_NODISCARD inline bool |
355e71b9 PC |
582 | operator==(nullptr_t, const shared_ptr<_Tp>& __a) noexcept |
583 | { return !__a; } | |
3abeaf8f | 584 | |
302b6996 | 585 | /// Inequality operator for shared_ptr objects, compares the stored pointers |
a2e0054e | 586 | template<typename _Tp, typename _Up> |
ec0d5371 | 587 | _GLIBCXX_NODISCARD inline bool |
a2e0054e | 588 | operator!=(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept |
8dd5e93a JW |
589 | { return __a.get() != __b.get(); } |
590 | ||
302b6996 | 591 | /// shared_ptr comparison with nullptr |
3abeaf8f | 592 | template<typename _Tp> |
ec0d5371 | 593 | _GLIBCXX_NODISCARD inline bool |
355e71b9 PC |
594 | operator!=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept |
595 | { return (bool)__a; } | |
3abeaf8f | 596 | |
302b6996 | 597 | /// shared_ptr comparison with nullptr |
3abeaf8f | 598 | template<typename _Tp> |
ec0d5371 | 599 | _GLIBCXX_NODISCARD inline bool |
355e71b9 PC |
600 | operator!=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept |
601 | { return (bool)__a; } | |
3abeaf8f | 602 | |
302b6996 | 603 | /// Relational operator for shared_ptr objects, compares the stored pointers |
a2e0054e | 604 | template<typename _Tp, typename _Up> |
ec0d5371 | 605 | _GLIBCXX_NODISCARD inline bool |
a2e0054e | 606 | operator<(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept |
355e71b9 | 607 | { |
a2e0054e JW |
608 | using _Tp_elt = typename shared_ptr<_Tp>::element_type; |
609 | using _Up_elt = typename shared_ptr<_Up>::element_type; | |
610 | using _Vp = typename common_type<_Tp_elt*, _Up_elt*>::type; | |
611 | return less<_Vp>()(__a.get(), __b.get()); | |
355e71b9 PC |
612 | } |
613 | ||
302b6996 | 614 | /// shared_ptr comparison with nullptr |
355e71b9 | 615 | template<typename _Tp> |
ec0d5371 | 616 | _GLIBCXX_NODISCARD inline bool |
355e71b9 | 617 | operator<(const shared_ptr<_Tp>& __a, nullptr_t) noexcept |
a2e0054e JW |
618 | { |
619 | using _Tp_elt = typename shared_ptr<_Tp>::element_type; | |
620 | return less<_Tp_elt*>()(__a.get(), nullptr); | |
621 | } | |
355e71b9 | 622 | |
302b6996 | 623 | /// shared_ptr comparison with nullptr |
355e71b9 | 624 | template<typename _Tp> |
ec0d5371 | 625 | _GLIBCXX_NODISCARD inline bool |
355e71b9 | 626 | operator<(nullptr_t, const shared_ptr<_Tp>& __a) noexcept |
a2e0054e JW |
627 | { |
628 | using _Tp_elt = typename shared_ptr<_Tp>::element_type; | |
629 | return less<_Tp_elt*>()(nullptr, __a.get()); | |
630 | } | |
355e71b9 | 631 | |
302b6996 | 632 | /// Relational operator for shared_ptr objects, compares the stored pointers |
a2e0054e | 633 | template<typename _Tp, typename _Up> |
ec0d5371 | 634 | _GLIBCXX_NODISCARD inline bool |
a2e0054e | 635 | operator<=(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept |
355e71b9 PC |
636 | { return !(__b < __a); } |
637 | ||
302b6996 | 638 | /// shared_ptr comparison with nullptr |
355e71b9 | 639 | template<typename _Tp> |
ec0d5371 | 640 | _GLIBCXX_NODISCARD inline bool |
355e71b9 PC |
641 | operator<=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept |
642 | { return !(nullptr < __a); } | |
643 | ||
302b6996 | 644 | /// shared_ptr comparison with nullptr |
355e71b9 | 645 | template<typename _Tp> |
ec0d5371 | 646 | _GLIBCXX_NODISCARD inline bool |
355e71b9 PC |
647 | operator<=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept |
648 | { return !(__a < nullptr); } | |
649 | ||
302b6996 | 650 | /// Relational operator for shared_ptr objects, compares the stored pointers |
a2e0054e | 651 | template<typename _Tp, typename _Up> |
ec0d5371 | 652 | _GLIBCXX_NODISCARD inline bool |
a2e0054e | 653 | operator>(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept |
355e71b9 PC |
654 | { return (__b < __a); } |
655 | ||
302b6996 | 656 | /// shared_ptr comparison with nullptr |
355e71b9 | 657 | template<typename _Tp> |
ec0d5371 | 658 | _GLIBCXX_NODISCARD inline bool |
355e71b9 | 659 | operator>(const shared_ptr<_Tp>& __a, nullptr_t) noexcept |
a2e0054e | 660 | { return nullptr < __a; } |
355e71b9 | 661 | |
302b6996 | 662 | /// shared_ptr comparison with nullptr |
355e71b9 | 663 | template<typename _Tp> |
ec0d5371 | 664 | _GLIBCXX_NODISCARD inline bool |
355e71b9 | 665 | operator>(nullptr_t, const shared_ptr<_Tp>& __a) noexcept |
a2e0054e | 666 | { return __a < nullptr; } |
355e71b9 | 667 | |
302b6996 | 668 | /// Relational operator for shared_ptr objects, compares the stored pointers |
a2e0054e | 669 | template<typename _Tp, typename _Up> |
ec0d5371 | 670 | _GLIBCXX_NODISCARD inline bool |
a2e0054e | 671 | operator>=(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept |
355e71b9 PC |
672 | { return !(__a < __b); } |
673 | ||
302b6996 | 674 | /// shared_ptr comparison with nullptr |
355e71b9 | 675 | template<typename _Tp> |
ec0d5371 | 676 | _GLIBCXX_NODISCARD inline bool |
355e71b9 PC |
677 | operator>=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept |
678 | { return !(__a < nullptr); } | |
679 | ||
302b6996 | 680 | /// shared_ptr comparison with nullptr |
355e71b9 | 681 | template<typename _Tp> |
ec0d5371 | 682 | _GLIBCXX_NODISCARD inline bool |
355e71b9 PC |
683 | operator>=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept |
684 | { return !(nullptr < __a); } | |
f5fa62ed | 685 | #endif |
8dd5e93a | 686 | |
9ab48d6e | 687 | // 20.7.2.2.8 shared_ptr specialized algorithms. |
302b6996 | 688 | |
efa9d8ee | 689 | /// Swap overload for shared_ptr |
640cbe76 JW |
690 | template<typename _Tp> |
691 | inline void | |
cf70f97b | 692 | swap(shared_ptr<_Tp>& __a, shared_ptr<_Tp>& __b) noexcept |
640cbe76 JW |
693 | { __a.swap(__b); } |
694 | ||
9ab48d6e | 695 | // 20.7.2.2.9 shared_ptr casts. |
302b6996 | 696 | |
efa9d8ee | 697 | /// Convert type of `shared_ptr`, via `static_cast` |
a2e0054e | 698 | template<typename _Tp, typename _Up> |
aaf0ca6f | 699 | inline shared_ptr<_Tp> |
a2e0054e JW |
700 | static_pointer_cast(const shared_ptr<_Up>& __r) noexcept |
701 | { | |
702 | using _Sp = shared_ptr<_Tp>; | |
703 | return _Sp(__r, static_cast<typename _Sp::element_type*>(__r.get())); | |
704 | } | |
c8bd30dd | 705 | |
efa9d8ee | 706 | /// Convert type of `shared_ptr`, via `const_cast` |
a2e0054e | 707 | template<typename _Tp, typename _Up> |
aaf0ca6f | 708 | inline shared_ptr<_Tp> |
a2e0054e JW |
709 | const_pointer_cast(const shared_ptr<_Up>& __r) noexcept |
710 | { | |
711 | using _Sp = shared_ptr<_Tp>; | |
712 | return _Sp(__r, const_cast<typename _Sp::element_type*>(__r.get())); | |
713 | } | |
c8bd30dd | 714 | |
efa9d8ee | 715 | /// Convert type of `shared_ptr`, via `dynamic_cast` |
a2e0054e | 716 | template<typename _Tp, typename _Up> |
aaf0ca6f | 717 | inline shared_ptr<_Tp> |
a2e0054e | 718 | dynamic_pointer_cast(const shared_ptr<_Up>& __r) noexcept |
aaf0ca6f | 719 | { |
a2e0054e JW |
720 | using _Sp = shared_ptr<_Tp>; |
721 | if (auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get())) | |
722 | return _Sp(__r, __p); | |
723 | return _Sp(); | |
aaf0ca6f | 724 | } |
c8bd30dd | 725 | |
efa9d8ee JW |
726 | #if __cplusplus >= 201703L |
727 | /// Convert type of `shared_ptr`, via `reinterpret_cast` | |
30b300de | 728 | /// @since C++17 |
a2e0054e JW |
729 | template<typename _Tp, typename _Up> |
730 | inline shared_ptr<_Tp> | |
731 | reinterpret_pointer_cast(const shared_ptr<_Up>& __r) noexcept | |
732 | { | |
733 | using _Sp = shared_ptr<_Tp>; | |
734 | return _Sp(__r, reinterpret_cast<typename _Sp::element_type*>(__r.get())); | |
735 | } | |
fb3fc4bd JW |
736 | |
737 | #if __cplusplus > 201703L | |
738 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
739 | // 2996. Missing rvalue overloads for shared_ptr operations | |
740 | ||
741 | /// Convert type of `shared_ptr` rvalue, via `static_cast` | |
30b300de | 742 | /// @since C++20 |
fb3fc4bd JW |
743 | template<typename _Tp, typename _Up> |
744 | inline shared_ptr<_Tp> | |
745 | static_pointer_cast(shared_ptr<_Up>&& __r) noexcept | |
746 | { | |
747 | using _Sp = shared_ptr<_Tp>; | |
748 | return _Sp(std::move(__r), | |
749 | static_cast<typename _Sp::element_type*>(__r.get())); | |
750 | } | |
751 | ||
752 | /// Convert type of `shared_ptr` rvalue, via `const_cast` | |
30b300de | 753 | /// @since C++20 |
fb3fc4bd JW |
754 | template<typename _Tp, typename _Up> |
755 | inline shared_ptr<_Tp> | |
756 | const_pointer_cast(shared_ptr<_Up>&& __r) noexcept | |
757 | { | |
758 | using _Sp = shared_ptr<_Tp>; | |
759 | return _Sp(std::move(__r), | |
760 | const_cast<typename _Sp::element_type*>(__r.get())); | |
761 | } | |
762 | ||
763 | /// Convert type of `shared_ptr` rvalue, via `dynamic_cast` | |
30b300de | 764 | /// @since C++20 |
fb3fc4bd JW |
765 | template<typename _Tp, typename _Up> |
766 | inline shared_ptr<_Tp> | |
767 | dynamic_pointer_cast(shared_ptr<_Up>&& __r) noexcept | |
768 | { | |
769 | using _Sp = shared_ptr<_Tp>; | |
770 | if (auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get())) | |
771 | return _Sp(std::move(__r), __p); | |
772 | return _Sp(); | |
773 | } | |
774 | ||
775 | /// Convert type of `shared_ptr` rvalue, via `reinterpret_cast` | |
30b300de | 776 | /// @since C++20 |
fb3fc4bd JW |
777 | template<typename _Tp, typename _Up> |
778 | inline shared_ptr<_Tp> | |
779 | reinterpret_pointer_cast(shared_ptr<_Up>&& __r) noexcept | |
780 | { | |
781 | using _Sp = shared_ptr<_Tp>; | |
782 | return _Sp(std::move(__r), | |
783 | reinterpret_cast<typename _Sp::element_type*>(__r.get())); | |
784 | } | |
785 | #endif // C++20 | |
786 | #endif // C++17 | |
c8bd30dd | 787 | |
f0b88346 | 788 | /// @} |
efa9d8ee | 789 | |
8e32aa11 | 790 | /** |
302b6996 | 791 | * @brief A non-owning observer for a pointer owned by a shared_ptr |
30b300de JW |
792 | * @headerfile memory |
793 | * @since C++11 | |
302b6996 JW |
794 | * |
795 | * A weak_ptr provides a safe alternative to a raw pointer when you want | |
796 | * a non-owning reference to an object that is managed by a shared_ptr. | |
8e32aa11 | 797 | * |
302b6996 JW |
798 | * Unlike a raw pointer, a weak_ptr can be converted to a new shared_ptr |
799 | * that shares ownership with every other shared_ptr that already owns | |
800 | * the pointer. In other words you can upgrade from a non-owning "weak" | |
801 | * reference to an owning shared_ptr, without having access to any of | |
802 | * the existing shared_ptr objects. | |
803 | * | |
804 | * Also unlike a raw pointer, a weak_ptr does not become "dangling" after | |
805 | * the object it points to has been destroyed. Instead, a weak_ptr | |
806 | * becomes _expired_ and can no longer be converted to a shared_ptr that | |
807 | * owns the freed pointer, so you cannot accidentally access the pointed-to | |
808 | * object after it has been destroyed. | |
5b9daa7e | 809 | */ |
c8bd30dd | 810 | template<typename _Tp> |
8e32aa11 | 811 | class weak_ptr : public __weak_ptr<_Tp> |
c8bd30dd | 812 | { |
a2e0054e JW |
813 | template<typename _Arg> |
814 | using _Constructible = typename enable_if< | |
815 | is_constructible<__weak_ptr<_Tp>, _Arg>::value | |
816 | >::type; | |
817 | ||
818 | template<typename _Arg> | |
819 | using _Assignable = typename enable_if< | |
820 | is_assignable<__weak_ptr<_Tp>&, _Arg>::value, weak_ptr& | |
821 | >::type; | |
d8af84e6 | 822 | |
c8bd30dd | 823 | public: |
f871d7f9 | 824 | constexpr weak_ptr() noexcept = default; |
8e32aa11 | 825 | |
a2e0054e JW |
826 | template<typename _Yp, |
827 | typename = _Constructible<const shared_ptr<_Yp>&>> | |
828 | weak_ptr(const shared_ptr<_Yp>& __r) noexcept | |
c8bd30dd PC |
829 | : __weak_ptr<_Tp>(__r) { } |
830 | ||
f871d7f9 JW |
831 | weak_ptr(const weak_ptr&) noexcept = default; |
832 | ||
a2e0054e JW |
833 | template<typename _Yp, typename = _Constructible<const weak_ptr<_Yp>&>> |
834 | weak_ptr(const weak_ptr<_Yp>& __r) noexcept | |
c8bd30dd PC |
835 | : __weak_ptr<_Tp>(__r) { } |
836 | ||
f871d7f9 JW |
837 | weak_ptr(weak_ptr&&) noexcept = default; |
838 | ||
a2e0054e JW |
839 | template<typename _Yp, typename = _Constructible<weak_ptr<_Yp>>> |
840 | weak_ptr(weak_ptr<_Yp>&& __r) noexcept | |
f871d7f9 JW |
841 | : __weak_ptr<_Tp>(std::move(__r)) { } |
842 | ||
843 | weak_ptr& | |
844 | operator=(const weak_ptr& __r) noexcept = default; | |
845 | ||
a2e0054e JW |
846 | template<typename _Yp> |
847 | _Assignable<const weak_ptr<_Yp>&> | |
848 | operator=(const weak_ptr<_Yp>& __r) noexcept | |
8e32aa11 | 849 | { |
c8bd30dd PC |
850 | this->__weak_ptr<_Tp>::operator=(__r); |
851 | return *this; | |
852 | } | |
853 | ||
a2e0054e JW |
854 | template<typename _Yp> |
855 | _Assignable<const shared_ptr<_Yp>&> | |
856 | operator=(const shared_ptr<_Yp>& __r) noexcept | |
8e32aa11 | 857 | { |
c8bd30dd PC |
858 | this->__weak_ptr<_Tp>::operator=(__r); |
859 | return *this; | |
860 | } | |
861 | ||
f871d7f9 JW |
862 | weak_ptr& |
863 | operator=(weak_ptr&& __r) noexcept = default; | |
864 | ||
a2e0054e JW |
865 | template<typename _Yp> |
866 | _Assignable<weak_ptr<_Yp>> | |
867 | operator=(weak_ptr<_Yp>&& __r) noexcept | |
f871d7f9 JW |
868 | { |
869 | this->__weak_ptr<_Tp>::operator=(std::move(__r)); | |
870 | return *this; | |
871 | } | |
872 | ||
c8bd30dd | 873 | shared_ptr<_Tp> |
cf70f97b | 874 | lock() const noexcept |
156b60e0 | 875 | { return shared_ptr<_Tp>(*this, std::nothrow); } |
c8bd30dd PC |
876 | }; |
877 | ||
af181c91 JW |
878 | #if __cpp_deduction_guides >= 201606 |
879 | template<typename _Tp> | |
880 | weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>; | |
881 | #endif | |
882 | ||
9ab48d6e | 883 | // 20.7.2.3.6 weak_ptr specialized algorithms. |
efa9d8ee JW |
884 | /// Swap overload for weak_ptr |
885 | /// @relates weak_ptr | |
8dd5e93a JW |
886 | template<typename _Tp> |
887 | inline void | |
cf70f97b | 888 | swap(weak_ptr<_Tp>& __a, weak_ptr<_Tp>& __b) noexcept |
8dd5e93a JW |
889 | { __a.swap(__b); } |
890 | ||
8e32aa11 BK |
891 | |
892 | /// Primary template owner_less | |
b7dabce5 | 893 | template<typename _Tp = void> |
8e32aa11 BK |
894 | struct owner_less; |
895 | ||
302b6996 | 896 | /// Void specialization of owner_less compares either shared_ptr or weak_ptr |
b7dabce5 JW |
897 | template<> |
898 | struct owner_less<void> : _Sp_owner_less<void, void> | |
899 | { }; | |
900 | ||
8e32aa11 | 901 | /// Partial specialization of owner_less for shared_ptr. |
8dd5e93a JW |
902 | template<typename _Tp> |
903 | struct owner_less<shared_ptr<_Tp>> | |
904 | : public _Sp_owner_less<shared_ptr<_Tp>, weak_ptr<_Tp>> | |
905 | { }; | |
906 | ||
8e32aa11 | 907 | /// Partial specialization of owner_less for weak_ptr. |
8dd5e93a JW |
908 | template<typename _Tp> |
909 | struct owner_less<weak_ptr<_Tp>> | |
910 | : public _Sp_owner_less<weak_ptr<_Tp>, shared_ptr<_Tp>> | |
911 | { }; | |
912 | ||
8e32aa11 | 913 | /** |
30b300de JW |
914 | * @brief Base class allowing use of the member function `shared_from_this`. |
915 | * @headerfile memory | |
916 | * @since C++11 | |
5b9daa7e | 917 | */ |
459f9f82 | 918 | template<typename _Tp> |
c8bd30dd | 919 | class enable_shared_from_this |
459f9f82 PC |
920 | { |
921 | protected: | |
cf70f97b | 922 | constexpr enable_shared_from_this() noexcept { } |
8e32aa11 | 923 | |
cf70f97b | 924 | enable_shared_from_this(const enable_shared_from_this&) noexcept { } |
c8bd30dd PC |
925 | |
926 | enable_shared_from_this& | |
cf70f97b | 927 | operator=(const enable_shared_from_this&) noexcept |
c8bd30dd PC |
928 | { return *this; } |
929 | ||
930 | ~enable_shared_from_this() { } | |
931 | ||
932 | public: | |
933 | shared_ptr<_Tp> | |
934 | shared_from_this() | |
935 | { return shared_ptr<_Tp>(this->_M_weak_this); } | |
936 | ||
937 | shared_ptr<const _Tp> | |
938 | shared_from_this() const | |
939 | { return shared_ptr<const _Tp>(this->_M_weak_this); } | |
940 | ||
9a8e528c | 941 | #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 |
b8806796 | 942 | #define __cpp_lib_enable_shared_from_this 201603L |
30b300de JW |
943 | /** @{ |
944 | * Get a `weak_ptr` referring to the object that has `*this` as its base. | |
945 | * @since C++17 | |
946 | */ | |
9a8e528c | 947 | weak_ptr<_Tp> |
208b8b69 | 948 | weak_from_this() noexcept |
9a8e528c JW |
949 | { return this->_M_weak_this; } |
950 | ||
951 | weak_ptr<const _Tp> | |
208b8b69 | 952 | weak_from_this() const noexcept |
9a8e528c | 953 | { return this->_M_weak_this; } |
30b300de | 954 | /// @} |
9a8e528c JW |
955 | #endif |
956 | ||
c8bd30dd PC |
957 | private: |
958 | template<typename _Tp1> | |
8e32aa11 | 959 | void |
cf70f97b | 960 | _M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const noexcept |
8e32aa11 | 961 | { _M_weak_this._M_assign(__p, __n); } |
c8bd30dd | 962 | |
f3070dab JW |
963 | // Found by ADL when this is an associated class. |
964 | friend const enable_shared_from_this* | |
965 | __enable_shared_from_this_base(const __shared_count<>&, | |
966 | const enable_shared_from_this* __p) | |
967 | { return __p; } | |
968 | ||
969 | template<typename, _Lock_policy> | |
970 | friend class __shared_ptr; | |
c8bd30dd PC |
971 | |
972 | mutable weak_ptr<_Tp> _M_weak_this; | |
459f9f82 | 973 | }; |
c8bd30dd | 974 | |
302b6996 | 975 | /// @relates shared_ptr @{ |
efa9d8ee | 976 | |
8e32aa11 BK |
977 | /** |
978 | * @brief Create an object that is owned by a shared_ptr. | |
aaf0ca6f JW |
979 | * @param __a An allocator. |
980 | * @param __args Arguments for the @a _Tp object's constructor. | |
981 | * @return A shared_ptr that owns the newly created object. | |
982 | * @throw An exception thrown from @a _Alloc::allocate or from the | |
983 | * constructor of @a _Tp. | |
984 | * | |
985 | * A copy of @a __a will be used to allocate memory for the shared_ptr | |
986 | * and the new object. | |
987 | */ | |
988 | template<typename _Tp, typename _Alloc, typename... _Args> | |
9a0b518a | 989 | inline shared_ptr<_NonArray<_Tp>> |
403b89a8 | 990 | allocate_shared(const _Alloc& __a, _Args&&... __args) |
aaf0ca6f | 991 | { |
fb54aa59 | 992 | return shared_ptr<_Tp>(_Sp_alloc_shared_tag<_Alloc>{__a}, |
8e32aa11 | 993 | std::forward<_Args>(__args)...); |
aaf0ca6f JW |
994 | } |
995 | ||
8e32aa11 BK |
996 | /** |
997 | * @brief Create an object that is owned by a shared_ptr. | |
aaf0ca6f JW |
998 | * @param __args Arguments for the @a _Tp object's constructor. |
999 | * @return A shared_ptr that owns the newly created object. | |
1000 | * @throw std::bad_alloc, or an exception thrown from the | |
1001 | * constructor of @a _Tp. | |
1002 | */ | |
1003 | template<typename _Tp, typename... _Args> | |
9a0b518a | 1004 | inline shared_ptr<_NonArray<_Tp>> |
aaf0ca6f JW |
1005 | make_shared(_Args&&... __args) |
1006 | { | |
9a0b518a JW |
1007 | using _Alloc = allocator<void>; |
1008 | _Alloc __a; | |
1009 | return shared_ptr<_Tp>(_Sp_alloc_shared_tag<_Alloc>{__a}, | |
1010 | std::forward<_Args>(__args)...); | |
1011 | } | |
1012 | ||
1013 | #if __cpp_lib_shared_ptr_arrays >= 201707L | |
1014 | /// @cond undocumented | |
1015 | template<typename _Tp, typename _Alloc = allocator<void>> | |
1016 | auto | |
1017 | __make_shared_arr_tag(size_t __n, const _Alloc& __a = _Alloc()) noexcept | |
1018 | { | |
1019 | using _Up = remove_all_extents_t<_Tp>; | |
1020 | using _UpAlloc = __alloc_rebind<_Alloc, _Up>; | |
1021 | size_t __s = sizeof(remove_extent_t<_Tp>) / sizeof(_Up); | |
1022 | if (__builtin_mul_overflow(__s, __n, &__n)) | |
1023 | std::__throw_bad_array_new_length(); | |
1024 | return _Sp_counted_array_base<_UpAlloc>{_UpAlloc(__a), __n}; | |
1025 | } | |
1026 | /// @endcond | |
1027 | ||
1028 | template<typename _Tp, typename _Alloc> | |
1029 | inline shared_ptr<_UnboundedArray<_Tp>> | |
1030 | allocate_shared(const _Alloc& __a, size_t __n) | |
1031 | { | |
1032 | return shared_ptr<_Tp>(std::__make_shared_arr_tag<_Tp>(__n, __a)); | |
1033 | } | |
1034 | ||
1035 | template<typename _Tp> | |
1036 | inline shared_ptr<_UnboundedArray<_Tp>> | |
1037 | make_shared(size_t __n) | |
1038 | { | |
1039 | return shared_ptr<_Tp>(std::__make_shared_arr_tag<_Tp>(__n)); | |
1040 | } | |
1041 | ||
1042 | template<typename _Tp, typename _Alloc> | |
1043 | inline shared_ptr<_UnboundedArray<_Tp>> | |
1044 | allocate_shared(const _Alloc& __a, size_t __n, | |
1045 | const remove_extent_t<_Tp>& __u) | |
1046 | { | |
1047 | return shared_ptr<_Tp>(std::__make_shared_arr_tag<_Tp>(__n, __a), | |
1048 | std::__addressof(__u)); | |
1049 | } | |
1050 | ||
1051 | template<typename _Tp> | |
1052 | inline shared_ptr<_UnboundedArray<_Tp>> | |
1053 | make_shared(size_t __n, const remove_extent_t<_Tp>& __u) | |
1054 | { | |
1055 | return shared_ptr<_Tp>(std::__make_shared_arr_tag<_Tp>(__n), | |
1056 | std::__addressof(__u)); | |
1057 | } | |
1058 | ||
1059 | /// @cond undocumented | |
1060 | template<typename _Tp, typename _Alloc = allocator<void>> | |
1061 | auto | |
1062 | __make_shared_arrN_tag(const _Alloc& __a = _Alloc()) noexcept | |
1063 | { | |
1064 | using _Up = remove_all_extents_t<_Tp>; | |
1065 | using _UpAlloc = __alloc_rebind<_Alloc, _Up>; | |
1066 | size_t __n = sizeof(_Tp) / sizeof(_Up); | |
1067 | return _Sp_counted_array_base<_UpAlloc>{_UpAlloc(__a), __n}; | |
1068 | } | |
1069 | /// @endcond | |
1070 | ||
1071 | template<typename _Tp, typename _Alloc> | |
1072 | inline shared_ptr<_BoundedArray<_Tp>> | |
1073 | allocate_shared(const _Alloc& __a) | |
1074 | { | |
1075 | return shared_ptr<_Tp>(std::__make_shared_arrN_tag<_Tp>(__a)); | |
1076 | } | |
1077 | ||
1078 | template<typename _Tp> | |
1079 | inline shared_ptr<_BoundedArray<_Tp>> | |
1080 | make_shared() | |
1081 | { | |
1082 | return shared_ptr<_Tp>(std::__make_shared_arrN_tag<_Tp>()); | |
1083 | } | |
1084 | ||
1085 | template<typename _Tp, typename _Alloc> | |
1086 | inline shared_ptr<_BoundedArray<_Tp>> | |
1087 | allocate_shared(const _Alloc& __a, const remove_extent_t<_Tp>& __u) | |
1088 | { | |
1089 | return shared_ptr<_Tp>(std::__make_shared_arrN_tag<_Tp>(__a), | |
1090 | std::__addressof(__u)); | |
1091 | } | |
1092 | ||
1093 | template<typename _Tp> | |
1094 | inline shared_ptr<_BoundedArray<_Tp>> | |
1095 | make_shared(const remove_extent_t<_Tp>& __u) | |
1096 | { | |
1097 | return shared_ptr<_Tp>(std::__make_shared_arrN_tag<_Tp>(), | |
1098 | std::__addressof(__u)); | |
1099 | } | |
1100 | ||
1101 | #if __cpp_lib_smart_ptr_for_overwrite | |
1102 | template<typename _Tp, typename _Alloc> | |
1103 | inline shared_ptr<_NotUnboundedArray<_Tp>> | |
1104 | allocate_shared_for_overwrite(const _Alloc& __a) | |
1105 | { | |
1106 | if constexpr (is_array_v<_Tp>) | |
1107 | return shared_ptr<_Tp>(std::__make_shared_arrN_tag<_Tp>(__a), | |
1108 | _Sp_overwrite_tag{}); | |
1109 | else | |
1110 | { | |
1111 | // Rebind the allocator to _Sp_overwrite_tag, so that the | |
1112 | // relevant _Sp_counted_ptr_inplace specialization is used. | |
1113 | using _Alloc2 = __alloc_rebind<_Alloc, _Sp_overwrite_tag>; | |
1114 | _Alloc2 __a2 = __a; | |
1115 | return shared_ptr<_Tp>(_Sp_alloc_shared_tag<_Alloc2>{__a2}); | |
1116 | } | |
1117 | } | |
1118 | ||
1119 | template<typename _Tp> | |
1120 | inline shared_ptr<_NotUnboundedArray<_Tp>> | |
1121 | make_shared_for_overwrite() | |
1122 | { | |
1123 | if constexpr (is_array_v<_Tp>) | |
1124 | return shared_ptr<_Tp>(std::__make_shared_arrN_tag<_Tp>(), | |
1125 | _Sp_overwrite_tag{}); | |
1126 | else | |
1127 | { | |
1128 | using _Alloc = allocator<_Sp_overwrite_tag>; | |
1129 | return shared_ptr<_Tp>(_Sp_alloc_shared_tag<_Alloc>{{}}); | |
1130 | } | |
1131 | } | |
1132 | ||
1133 | template<typename _Tp, typename _Alloc> | |
1134 | inline shared_ptr<_UnboundedArray<_Tp>> | |
1135 | allocate_shared_for_overwrite(const _Alloc& __a, size_t __n) | |
1136 | { | |
1137 | return shared_ptr<_Tp>(std::__make_shared_arr_tag<_Tp>(__n, __a), | |
1138 | _Sp_overwrite_tag{}); | |
1139 | } | |
1140 | ||
1141 | template<typename _Tp> | |
1142 | inline shared_ptr<_UnboundedArray<_Tp>> | |
1143 | make_shared_for_overwrite(size_t __n) | |
1144 | { | |
1145 | return shared_ptr<_Tp>(std::__make_shared_arr_tag<_Tp>(__n), | |
1146 | _Sp_overwrite_tag{}); | |
aaf0ca6f | 1147 | } |
9a0b518a JW |
1148 | #endif // smart_ptr_for_overwrite |
1149 | #endif // shared_ptr_arrays | |
aaf0ca6f | 1150 | |
b0e788cc PC |
1151 | /// std::hash specialization for shared_ptr. |
1152 | template<typename _Tp> | |
1153 | struct hash<shared_ptr<_Tp>> | |
9992d564 | 1154 | : public __hash_base<size_t, shared_ptr<_Tp>> |
b0e788cc PC |
1155 | { |
1156 | size_t | |
72f1c34b | 1157 | operator()(const shared_ptr<_Tp>& __s) const noexcept |
0f88f1f2 JW |
1158 | { |
1159 | return std::hash<typename shared_ptr<_Tp>::element_type*>()(__s.get()); | |
1160 | } | |
b0e788cc PC |
1161 | }; |
1162 | ||
f0b88346 JW |
1163 | /// @} relates shared_ptr |
1164 | /// @} group pointer_abstractions | |
5b9daa7e | 1165 | |
10f26de9 JW |
1166 | #if __cplusplus >= 201703L |
1167 | namespace __detail::__variant | |
1168 | { | |
1169 | template<typename> struct _Never_valueless_alt; // see <variant> | |
1170 | ||
1171 | // Provide the strong exception-safety guarantee when emplacing a | |
1172 | // shared_ptr into a variant. | |
1173 | template<typename _Tp> | |
1174 | struct _Never_valueless_alt<std::shared_ptr<_Tp>> | |
1175 | : std::true_type | |
1176 | { }; | |
1177 | ||
1178 | // Provide the strong exception-safety guarantee when emplacing a | |
1179 | // weak_ptr into a variant. | |
1180 | template<typename _Tp> | |
1181 | struct _Never_valueless_alt<std::weak_ptr<_Tp>> | |
1182 | : std::true_type | |
1183 | { }; | |
1184 | } // namespace __detail::__variant | |
1185 | #endif // C++17 | |
1186 | ||
12ffa228 BK |
1187 | _GLIBCXX_END_NAMESPACE_VERSION |
1188 | } // namespace | |
32fdf2f4 JW |
1189 | |
1190 | #endif // _SHARED_PTR_H |