]>
Commit | Line | Data |
---|---|---|
5b9daa7e | 1 | // shared_ptr and weak_ptr implementation -*- C++ -*- |
b758b22a | 2 | |
a5544970 | 3 | // Copyright (C) 2007-2019 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 | ||
8e32aa11 | 52 | #include <bits/shared_ptr_base.h> |
aaf0ca6f | 53 | |
12ffa228 BK |
54 | namespace std _GLIBCXX_VISIBILITY(default) |
55 | { | |
56 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
5b9daa7e BK |
57 | |
58 | /** | |
59 | * @addtogroup pointer_abstractions | |
60 | * @{ | |
61 | */ | |
62 | ||
05a4261b | 63 | /// 20.7.2.2.11 shared_ptr I/O |
8e32aa11 | 64 | template<typename _Ch, typename _Tr, typename _Tp, _Lock_policy _Lp> |
d779a591 | 65 | inline std::basic_ostream<_Ch, _Tr>& |
8e32aa11 BK |
66 | operator<<(std::basic_ostream<_Ch, _Tr>& __os, |
67 | const __shared_ptr<_Tp, _Lp>& __p) | |
a25ce4db | 68 | { |
8e32aa11 BK |
69 | __os << __p.get(); |
70 | return __os; | |
a25ce4db | 71 | } |
459f9f82 | 72 | |
8e32aa11 BK |
73 | template<typename _Del, typename _Tp, _Lock_policy _Lp> |
74 | inline _Del* | |
cf70f97b | 75 | get_deleter(const __shared_ptr<_Tp, _Lp>& __p) noexcept |
70826946 | 76 | { |
0f3d27f0 | 77 | #if __cpp_rtti |
70826946 JW |
78 | return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del))); |
79 | #else | |
80 | return 0; | |
81 | #endif | |
82 | } | |
b758b22a | 83 | |
78a8b676 JW |
84 | /// 20.7.2.2.10 shared_ptr get_deleter |
85 | template<typename _Del, typename _Tp> | |
86 | inline _Del* | |
87 | get_deleter(const shared_ptr<_Tp>& __p) noexcept | |
88 | { | |
89 | #if __cpp_rtti | |
90 | return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del))); | |
91 | #else | |
92 | return 0; | |
93 | #endif | |
94 | } | |
a15024e6 | 95 | |
8e32aa11 BK |
96 | /** |
97 | * @brief A smart pointer with reference-counted copy semantics. | |
98 | * | |
99 | * The object pointed to is deleted when the last shared_ptr pointing to | |
100 | * it is destroyed or reset. | |
101 | */ | |
c8bd30dd | 102 | template<typename _Tp> |
8e32aa11 | 103 | class shared_ptr : public __shared_ptr<_Tp> |
459f9f82 | 104 | { |
a2e0054e JW |
105 | template<typename... _Args> |
106 | using _Constructible = typename enable_if< | |
107 | is_constructible<__shared_ptr<_Tp>, _Args...>::value | |
108 | >::type; | |
7663cae2 | 109 | |
a2e0054e JW |
110 | template<typename _Arg> |
111 | using _Assignable = typename enable_if< | |
112 | is_assignable<__shared_ptr<_Tp>&, _Arg>::value, shared_ptr& | |
113 | >::type; | |
d8af84e6 | 114 | |
459f9f82 | 115 | public: |
f21f4463 | 116 | |
a2e0054e JW |
117 | using element_type = typename __shared_ptr<_Tp>::element_type; |
118 | ||
f21f4463 JW |
119 | #if __cplusplus > 201402L |
120 | # define __cpp_lib_shared_ptr_weak_type 201606 | |
121 | using weak_type = weak_ptr<_Tp>; | |
122 | #endif | |
8e32aa11 BK |
123 | /** |
124 | * @brief Construct an empty %shared_ptr. | |
459f9f82 PC |
125 | * @post use_count()==0 && get()==0 |
126 | */ | |
a2e0054e | 127 | constexpr shared_ptr() noexcept : __shared_ptr<_Tp>() { } |
b758b22a | 128 | |
7897a1c0 BK |
129 | shared_ptr(const shared_ptr&) noexcept = default; |
130 | ||
8e32aa11 BK |
131 | /** |
132 | * @brief Construct a %shared_ptr that owns the pointer @a __p. | |
c8bd30dd PC |
133 | * @param __p A pointer that is convertible to element_type*. |
134 | * @post use_count() == 1 && get() == __p | |
135 | * @throw std::bad_alloc, in which case @c delete @a __p is called. | |
459f9f82 | 136 | */ |
a2e0054e JW |
137 | template<typename _Yp, typename = _Constructible<_Yp*>> |
138 | explicit | |
139 | shared_ptr(_Yp* __p) : __shared_ptr<_Tp>(__p) { } | |
b758b22a | 140 | |
8e32aa11 BK |
141 | /** |
142 | * @brief Construct a %shared_ptr that owns the pointer @a __p | |
c8bd30dd PC |
143 | * and the deleter @a __d. |
144 | * @param __p A pointer. | |
145 | * @param __d A deleter. | |
146 | * @post use_count() == 1 && get() == __p | |
147 | * @throw std::bad_alloc, in which case @a __d(__p) is called. | |
8e32aa11 BK |
148 | * |
149 | * Requirements: _Deleter's copy constructor and destructor must | |
150 | * not throw | |
151 | * | |
152 | * __shared_ptr will release __p by calling __d(__p) | |
459f9f82 | 153 | */ |
a2e0054e JW |
154 | template<typename _Yp, typename _Deleter, |
155 | typename = _Constructible<_Yp*, _Deleter>> | |
156 | shared_ptr(_Yp* __p, _Deleter __d) | |
53d01fd9 | 157 | : __shared_ptr<_Tp>(__p, std::move(__d)) { } |
8e32aa11 | 158 | |
6d00745b JW |
159 | /** |
160 | * @brief Construct a %shared_ptr that owns a null pointer | |
161 | * and the deleter @a __d. | |
162 | * @param __p A null pointer constant. | |
163 | * @param __d A deleter. | |
164 | * @post use_count() == 1 && get() == __p | |
165 | * @throw std::bad_alloc, in which case @a __d(__p) is called. | |
166 | * | |
167 | * Requirements: _Deleter's copy constructor and destructor must | |
168 | * not throw | |
169 | * | |
170 | * The last owner will call __d(__p) | |
171 | */ | |
172 | template<typename _Deleter> | |
173 | shared_ptr(nullptr_t __p, _Deleter __d) | |
53d01fd9 | 174 | : __shared_ptr<_Tp>(__p, std::move(__d)) { } |
6d00745b | 175 | |
8e32aa11 BK |
176 | /** |
177 | * @brief Construct a %shared_ptr that owns the pointer @a __p | |
aaf0ca6f JW |
178 | * and the deleter @a __d. |
179 | * @param __p A pointer. | |
180 | * @param __d A deleter. | |
181 | * @param __a An allocator. | |
182 | * @post use_count() == 1 && get() == __p | |
183 | * @throw std::bad_alloc, in which case @a __d(__p) is called. | |
8e32aa11 BK |
184 | * |
185 | * Requirements: _Deleter's copy constructor and destructor must | |
186 | * not throw _Alloc's copy constructor and destructor must not | |
187 | * throw. | |
188 | * | |
189 | * __shared_ptr will release __p by calling __d(__p) | |
aaf0ca6f | 190 | */ |
a2e0054e JW |
191 | template<typename _Yp, typename _Deleter, typename _Alloc, |
192 | typename = _Constructible<_Yp*, _Deleter, _Alloc>> | |
193 | shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a) | |
53d01fd9 | 194 | : __shared_ptr<_Tp>(__p, std::move(__d), std::move(__a)) { } |
8e32aa11 | 195 | |
6d00745b JW |
196 | /** |
197 | * @brief Construct a %shared_ptr that owns a null pointer | |
198 | * and the deleter @a __d. | |
199 | * @param __p A null pointer constant. | |
200 | * @param __d A deleter. | |
201 | * @param __a An allocator. | |
202 | * @post use_count() == 1 && get() == __p | |
203 | * @throw std::bad_alloc, in which case @a __d(__p) is called. | |
204 | * | |
205 | * Requirements: _Deleter's copy constructor and destructor must | |
206 | * not throw _Alloc's copy constructor and destructor must not | |
207 | * throw. | |
208 | * | |
209 | * The last owner will call __d(__p) | |
210 | */ | |
211 | template<typename _Deleter, typename _Alloc> | |
403b89a8 | 212 | shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a) |
53d01fd9 | 213 | : __shared_ptr<_Tp>(__p, std::move(__d), std::move(__a)) { } |
6d00745b | 214 | |
8e32aa11 | 215 | // Aliasing constructor |
aaf0ca6f | 216 | |
8e32aa11 BK |
217 | /** |
218 | * @brief Constructs a %shared_ptr instance that stores @a __p | |
aaf0ca6f | 219 | * and shares ownership with @a __r. |
8e32aa11 | 220 | * @param __r A %shared_ptr. |
aaf0ca6f JW |
221 | * @param __p A pointer that will remain valid while @a *__r is valid. |
222 | * @post get() == __p && use_count() == __r.use_count() | |
223 | * | |
224 | * This can be used to construct a @c shared_ptr to a sub-object | |
225 | * of an object managed by an existing @c shared_ptr. | |
226 | * | |
227 | * @code | |
228 | * shared_ptr< pair<int,int> > pii(new pair<int,int>()); | |
229 | * shared_ptr<int> pi(pii, &pii->first); | |
230 | * assert(pii.use_count() == 2); | |
231 | * @endcode | |
232 | */ | |
a2e0054e JW |
233 | template<typename _Yp> |
234 | shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) noexcept | |
8e32aa11 BK |
235 | : __shared_ptr<_Tp>(__r, __p) { } |
236 | ||
237 | /** | |
238 | * @brief If @a __r is empty, constructs an empty %shared_ptr; | |
239 | * otherwise construct a %shared_ptr that shares ownership | |
c8bd30dd | 240 | * with @a __r. |
8e32aa11 | 241 | * @param __r A %shared_ptr. |
c8bd30dd | 242 | * @post get() == __r.get() && use_count() == __r.use_count() |
459f9f82 | 243 | */ |
a2e0054e JW |
244 | template<typename _Yp, |
245 | typename = _Constructible<const shared_ptr<_Yp>&>> | |
246 | shared_ptr(const shared_ptr<_Yp>& __r) noexcept | |
5aadb69b | 247 | : __shared_ptr<_Tp>(__r) { } |
459f9f82 | 248 | |
8e32aa11 BK |
249 | /** |
250 | * @brief Move-constructs a %shared_ptr instance from @a __r. | |
251 | * @param __r A %shared_ptr rvalue. | |
aaf0ca6f JW |
252 | * @post *this contains the old value of @a __r, @a __r is empty. |
253 | */ | |
cf70f97b | 254 | shared_ptr(shared_ptr&& __r) noexcept |
8e32aa11 | 255 | : __shared_ptr<_Tp>(std::move(__r)) { } |
aaf0ca6f | 256 | |
8e32aa11 BK |
257 | /** |
258 | * @brief Move-constructs a %shared_ptr instance from @a __r. | |
259 | * @param __r A %shared_ptr rvalue. | |
aaf0ca6f JW |
260 | * @post *this contains the old value of @a __r, @a __r is empty. |
261 | */ | |
a2e0054e JW |
262 | template<typename _Yp, typename = _Constructible<shared_ptr<_Yp>>> |
263 | shared_ptr(shared_ptr<_Yp>&& __r) noexcept | |
8e32aa11 BK |
264 | : __shared_ptr<_Tp>(std::move(__r)) { } |
265 | ||
266 | /** | |
267 | * @brief Constructs a %shared_ptr that shares ownership with @a __r | |
c8bd30dd PC |
268 | * and stores a copy of the pointer stored in @a __r. |
269 | * @param __r A weak_ptr. | |
270 | * @post use_count() == __r.use_count() | |
271 | * @throw bad_weak_ptr when __r.expired(), | |
459f9f82 PC |
272 | * in which case the constructor has no effect. |
273 | */ | |
a2e0054e JW |
274 | template<typename _Yp, typename = _Constructible<const weak_ptr<_Yp>&>> |
275 | explicit shared_ptr(const weak_ptr<_Yp>& __r) | |
459f9f82 | 276 | : __shared_ptr<_Tp>(__r) { } |
c8bd30dd | 277 | |
e1bcd685 | 278 | #if _GLIBCXX_USE_DEPRECATED |
a2c0a194 JW |
279 | #pragma GCC diagnostic push |
280 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" | |
a2e0054e JW |
281 | template<typename _Yp, typename = _Constructible<auto_ptr<_Yp>>> |
282 | shared_ptr(auto_ptr<_Yp>&& __r); | |
a2c0a194 | 283 | #pragma GCC diagnostic pop |
40abbf1f | 284 | #endif |
459f9f82 | 285 | |
d8af84e6 JW |
286 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
287 | // 2399. shared_ptr's constructor from unique_ptr should be constrained | |
a2e0054e JW |
288 | template<typename _Yp, typename _Del, |
289 | typename = _Constructible<unique_ptr<_Yp, _Del>>> | |
290 | shared_ptr(unique_ptr<_Yp, _Del>&& __r) | |
640cbe76 | 291 | : __shared_ptr<_Tp>(std::move(__r)) { } |
459f9f82 | 292 | |
b2343559 JW |
293 | #if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED |
294 | // This non-standard constructor exists to support conversions that | |
295 | // were possible in C++11 and C++14 but are ill-formed in C++17. | |
296 | // If an exception is thrown this constructor has no effect. | |
297 | template<typename _Yp, typename _Del, | |
298 | _Constructible<unique_ptr<_Yp, _Del>, __sp_array_delete>* = 0> | |
299 | shared_ptr(unique_ptr<_Yp, _Del>&& __r) | |
300 | : __shared_ptr<_Tp>(std::move(__r), __sp_array_delete()) { } | |
301 | #endif | |
302 | ||
6d00745b JW |
303 | /** |
304 | * @brief Construct an empty %shared_ptr. | |
6d00745b JW |
305 | * @post use_count() == 0 && get() == nullptr |
306 | */ | |
33fbb358 | 307 | constexpr shared_ptr(nullptr_t) noexcept : shared_ptr() { } |
6d00745b | 308 | |
a2e70335 | 309 | shared_ptr& operator=(const shared_ptr&) noexcept = default; |
7897a1c0 | 310 | |
a2e0054e JW |
311 | template<typename _Yp> |
312 | _Assignable<const shared_ptr<_Yp>&> | |
313 | operator=(const shared_ptr<_Yp>& __r) noexcept | |
8e32aa11 | 314 | { |
459f9f82 PC |
315 | this->__shared_ptr<_Tp>::operator=(__r); |
316 | return *this; | |
317 | } | |
a25ce4db | 318 | |
e1bcd685 | 319 | #if _GLIBCXX_USE_DEPRECATED |
a2c0a194 JW |
320 | #pragma GCC diagnostic push |
321 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" | |
a2e0054e JW |
322 | template<typename _Yp> |
323 | _Assignable<auto_ptr<_Yp>> | |
324 | operator=(auto_ptr<_Yp>&& __r) | |
8e32aa11 | 325 | { |
640cbe76 | 326 | this->__shared_ptr<_Tp>::operator=(std::move(__r)); |
459f9f82 PC |
327 | return *this; |
328 | } | |
a2c0a194 | 329 | #pragma GCC diagnostic pop |
40abbf1f | 330 | #endif |
aaf0ca6f | 331 | |
aaf0ca6f | 332 | shared_ptr& |
cf70f97b | 333 | operator=(shared_ptr&& __r) noexcept |
aaf0ca6f | 334 | { |
8e32aa11 BK |
335 | this->__shared_ptr<_Tp>::operator=(std::move(__r)); |
336 | return *this; | |
aaf0ca6f | 337 | } |
8e32aa11 | 338 | |
a2e0054e JW |
339 | template<class _Yp> |
340 | _Assignable<shared_ptr<_Yp>> | |
341 | operator=(shared_ptr<_Yp>&& __r) noexcept | |
8e32aa11 BK |
342 | { |
343 | this->__shared_ptr<_Tp>::operator=(std::move(__r)); | |
344 | return *this; | |
345 | } | |
aaf0ca6f | 346 | |
a2e0054e JW |
347 | template<typename _Yp, typename _Del> |
348 | _Assignable<unique_ptr<_Yp, _Del>> | |
349 | operator=(unique_ptr<_Yp, _Del>&& __r) | |
8e32aa11 | 350 | { |
640cbe76 JW |
351 | this->__shared_ptr<_Tp>::operator=(std::move(__r)); |
352 | return *this; | |
353 | } | |
354 | ||
aaf0ca6f JW |
355 | private: |
356 | // This constructor is non-standard, it is used by allocate_shared. | |
357 | template<typename _Alloc, typename... _Args> | |
403b89a8 PC |
358 | shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a, |
359 | _Args&&... __args) | |
8e32aa11 BK |
360 | : __shared_ptr<_Tp>(__tag, __a, std::forward<_Args>(__args)...) |
361 | { } | |
aaf0ca6f | 362 | |
a2e0054e JW |
363 | template<typename _Yp, typename _Alloc, typename... _Args> |
364 | friend shared_ptr<_Yp> | |
403b89a8 | 365 | allocate_shared(const _Alloc& __a, _Args&&... __args); |
156b60e0 JW |
366 | |
367 | // This constructor is non-standard, it is used by weak_ptr::lock(). | |
368 | shared_ptr(const weak_ptr<_Tp>& __r, std::nothrow_t) | |
369 | : __shared_ptr<_Tp>(__r, std::nothrow) { } | |
370 | ||
371 | friend class weak_ptr<_Tp>; | |
459f9f82 | 372 | }; |
b758b22a | 373 | |
af181c91 JW |
374 | #if __cpp_deduction_guides >= 201606 |
375 | template<typename _Tp> | |
376 | shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>; | |
377 | template<typename _Tp, typename _Del> | |
378 | shared_ptr(unique_ptr<_Tp, _Del>) -> shared_ptr<_Tp>; | |
379 | #endif | |
380 | ||
9ab48d6e | 381 | // 20.7.2.2.7 shared_ptr comparisons |
a2e0054e | 382 | template<typename _Tp, typename _Up> |
8dd5e93a | 383 | inline bool |
a2e0054e | 384 | operator==(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept |
8dd5e93a JW |
385 | { return __a.get() == __b.get(); } |
386 | ||
3abeaf8f PC |
387 | template<typename _Tp> |
388 | inline bool | |
355e71b9 PC |
389 | operator==(const shared_ptr<_Tp>& __a, nullptr_t) noexcept |
390 | { return !__a; } | |
3abeaf8f PC |
391 | |
392 | template<typename _Tp> | |
393 | inline bool | |
355e71b9 PC |
394 | operator==(nullptr_t, const shared_ptr<_Tp>& __a) noexcept |
395 | { return !__a; } | |
3abeaf8f | 396 | |
a2e0054e | 397 | template<typename _Tp, typename _Up> |
8dd5e93a | 398 | inline bool |
a2e0054e | 399 | operator!=(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept |
8dd5e93a JW |
400 | { return __a.get() != __b.get(); } |
401 | ||
3abeaf8f PC |
402 | template<typename _Tp> |
403 | inline bool | |
355e71b9 PC |
404 | operator!=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept |
405 | { return (bool)__a; } | |
3abeaf8f PC |
406 | |
407 | template<typename _Tp> | |
408 | inline bool | |
355e71b9 PC |
409 | operator!=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept |
410 | { return (bool)__a; } | |
3abeaf8f | 411 | |
a2e0054e | 412 | template<typename _Tp, typename _Up> |
8dd5e93a | 413 | inline bool |
a2e0054e | 414 | operator<(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept |
355e71b9 | 415 | { |
a2e0054e JW |
416 | using _Tp_elt = typename shared_ptr<_Tp>::element_type; |
417 | using _Up_elt = typename shared_ptr<_Up>::element_type; | |
418 | using _Vp = typename common_type<_Tp_elt*, _Up_elt*>::type; | |
419 | return less<_Vp>()(__a.get(), __b.get()); | |
355e71b9 PC |
420 | } |
421 | ||
422 | template<typename _Tp> | |
423 | inline bool | |
424 | operator<(const shared_ptr<_Tp>& __a, nullptr_t) noexcept | |
a2e0054e JW |
425 | { |
426 | using _Tp_elt = typename shared_ptr<_Tp>::element_type; | |
427 | return less<_Tp_elt*>()(__a.get(), nullptr); | |
428 | } | |
355e71b9 PC |
429 | |
430 | template<typename _Tp> | |
431 | inline bool | |
432 | operator<(nullptr_t, const shared_ptr<_Tp>& __a) noexcept | |
a2e0054e JW |
433 | { |
434 | using _Tp_elt = typename shared_ptr<_Tp>::element_type; | |
435 | return less<_Tp_elt*>()(nullptr, __a.get()); | |
436 | } | |
355e71b9 | 437 | |
a2e0054e | 438 | template<typename _Tp, typename _Up> |
355e71b9 | 439 | inline bool |
a2e0054e | 440 | operator<=(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept |
355e71b9 PC |
441 | { return !(__b < __a); } |
442 | ||
443 | template<typename _Tp> | |
444 | inline bool | |
445 | operator<=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept | |
446 | { return !(nullptr < __a); } | |
447 | ||
448 | template<typename _Tp> | |
449 | inline bool | |
450 | operator<=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept | |
451 | { return !(__a < nullptr); } | |
452 | ||
a2e0054e | 453 | template<typename _Tp, typename _Up> |
355e71b9 | 454 | inline bool |
a2e0054e | 455 | operator>(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept |
355e71b9 PC |
456 | { return (__b < __a); } |
457 | ||
458 | template<typename _Tp> | |
459 | inline bool | |
460 | operator>(const shared_ptr<_Tp>& __a, nullptr_t) noexcept | |
a2e0054e | 461 | { return nullptr < __a; } |
355e71b9 PC |
462 | |
463 | template<typename _Tp> | |
464 | inline bool | |
465 | operator>(nullptr_t, const shared_ptr<_Tp>& __a) noexcept | |
a2e0054e | 466 | { return __a < nullptr; } |
355e71b9 | 467 | |
a2e0054e | 468 | template<typename _Tp, typename _Up> |
355e71b9 | 469 | inline bool |
a2e0054e | 470 | operator>=(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept |
355e71b9 PC |
471 | { return !(__a < __b); } |
472 | ||
473 | template<typename _Tp> | |
474 | inline bool | |
475 | operator>=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept | |
476 | { return !(__a < nullptr); } | |
477 | ||
478 | template<typename _Tp> | |
479 | inline bool | |
480 | operator>=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept | |
481 | { return !(nullptr < __a); } | |
8dd5e93a | 482 | |
9ab48d6e | 483 | // 20.7.2.2.8 shared_ptr specialized algorithms. |
640cbe76 JW |
484 | template<typename _Tp> |
485 | inline void | |
cf70f97b | 486 | swap(shared_ptr<_Tp>& __a, shared_ptr<_Tp>& __b) noexcept |
640cbe76 JW |
487 | { __a.swap(__b); } |
488 | ||
9ab48d6e | 489 | // 20.7.2.2.9 shared_ptr casts. |
a2e0054e | 490 | template<typename _Tp, typename _Up> |
aaf0ca6f | 491 | inline shared_ptr<_Tp> |
a2e0054e JW |
492 | static_pointer_cast(const shared_ptr<_Up>& __r) noexcept |
493 | { | |
494 | using _Sp = shared_ptr<_Tp>; | |
495 | return _Sp(__r, static_cast<typename _Sp::element_type*>(__r.get())); | |
496 | } | |
c8bd30dd | 497 | |
a2e0054e | 498 | template<typename _Tp, typename _Up> |
aaf0ca6f | 499 | inline shared_ptr<_Tp> |
a2e0054e JW |
500 | const_pointer_cast(const shared_ptr<_Up>& __r) noexcept |
501 | { | |
502 | using _Sp = shared_ptr<_Tp>; | |
503 | return _Sp(__r, const_cast<typename _Sp::element_type*>(__r.get())); | |
504 | } | |
c8bd30dd | 505 | |
a2e0054e | 506 | template<typename _Tp, typename _Up> |
aaf0ca6f | 507 | inline shared_ptr<_Tp> |
a2e0054e | 508 | dynamic_pointer_cast(const shared_ptr<_Up>& __r) noexcept |
aaf0ca6f | 509 | { |
a2e0054e JW |
510 | using _Sp = shared_ptr<_Tp>; |
511 | if (auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get())) | |
512 | return _Sp(__r, __p); | |
513 | return _Sp(); | |
aaf0ca6f | 514 | } |
c8bd30dd | 515 | |
a2e0054e JW |
516 | #if __cplusplus > 201402L |
517 | template<typename _Tp, typename _Up> | |
518 | inline shared_ptr<_Tp> | |
519 | reinterpret_pointer_cast(const shared_ptr<_Up>& __r) noexcept | |
520 | { | |
521 | using _Sp = shared_ptr<_Tp>; | |
522 | return _Sp(__r, reinterpret_cast<typename _Sp::element_type*>(__r.get())); | |
523 | } | |
524 | #endif | |
c8bd30dd | 525 | |
8e32aa11 BK |
526 | /** |
527 | * @brief A smart pointer with weak semantics. | |
528 | * | |
5b9daa7e BK |
529 | * With forwarding constructors and assignment operators. |
530 | */ | |
c8bd30dd | 531 | template<typename _Tp> |
8e32aa11 | 532 | class weak_ptr : public __weak_ptr<_Tp> |
c8bd30dd | 533 | { |
a2e0054e JW |
534 | template<typename _Arg> |
535 | using _Constructible = typename enable_if< | |
536 | is_constructible<__weak_ptr<_Tp>, _Arg>::value | |
537 | >::type; | |
538 | ||
539 | template<typename _Arg> | |
540 | using _Assignable = typename enable_if< | |
541 | is_assignable<__weak_ptr<_Tp>&, _Arg>::value, weak_ptr& | |
542 | >::type; | |
d8af84e6 | 543 | |
c8bd30dd | 544 | public: |
f871d7f9 | 545 | constexpr weak_ptr() noexcept = default; |
8e32aa11 | 546 | |
a2e0054e JW |
547 | template<typename _Yp, |
548 | typename = _Constructible<const shared_ptr<_Yp>&>> | |
549 | weak_ptr(const shared_ptr<_Yp>& __r) noexcept | |
c8bd30dd PC |
550 | : __weak_ptr<_Tp>(__r) { } |
551 | ||
f871d7f9 JW |
552 | weak_ptr(const weak_ptr&) noexcept = default; |
553 | ||
a2e0054e JW |
554 | template<typename _Yp, typename = _Constructible<const weak_ptr<_Yp>&>> |
555 | weak_ptr(const weak_ptr<_Yp>& __r) noexcept | |
c8bd30dd PC |
556 | : __weak_ptr<_Tp>(__r) { } |
557 | ||
f871d7f9 JW |
558 | weak_ptr(weak_ptr&&) noexcept = default; |
559 | ||
a2e0054e JW |
560 | template<typename _Yp, typename = _Constructible<weak_ptr<_Yp>>> |
561 | weak_ptr(weak_ptr<_Yp>&& __r) noexcept | |
f871d7f9 JW |
562 | : __weak_ptr<_Tp>(std::move(__r)) { } |
563 | ||
564 | weak_ptr& | |
565 | operator=(const weak_ptr& __r) noexcept = default; | |
566 | ||
a2e0054e JW |
567 | template<typename _Yp> |
568 | _Assignable<const weak_ptr<_Yp>&> | |
569 | operator=(const weak_ptr<_Yp>& __r) noexcept | |
8e32aa11 | 570 | { |
c8bd30dd PC |
571 | this->__weak_ptr<_Tp>::operator=(__r); |
572 | return *this; | |
573 | } | |
574 | ||
a2e0054e JW |
575 | template<typename _Yp> |
576 | _Assignable<const shared_ptr<_Yp>&> | |
577 | operator=(const shared_ptr<_Yp>& __r) noexcept | |
8e32aa11 | 578 | { |
c8bd30dd PC |
579 | this->__weak_ptr<_Tp>::operator=(__r); |
580 | return *this; | |
581 | } | |
582 | ||
f871d7f9 JW |
583 | weak_ptr& |
584 | operator=(weak_ptr&& __r) noexcept = default; | |
585 | ||
a2e0054e JW |
586 | template<typename _Yp> |
587 | _Assignable<weak_ptr<_Yp>> | |
588 | operator=(weak_ptr<_Yp>&& __r) noexcept | |
f871d7f9 JW |
589 | { |
590 | this->__weak_ptr<_Tp>::operator=(std::move(__r)); | |
591 | return *this; | |
592 | } | |
593 | ||
c8bd30dd | 594 | shared_ptr<_Tp> |
cf70f97b | 595 | lock() const noexcept |
156b60e0 | 596 | { return shared_ptr<_Tp>(*this, std::nothrow); } |
c8bd30dd PC |
597 | }; |
598 | ||
af181c91 JW |
599 | #if __cpp_deduction_guides >= 201606 |
600 | template<typename _Tp> | |
601 | weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>; | |
602 | #endif | |
603 | ||
9ab48d6e | 604 | // 20.7.2.3.6 weak_ptr specialized algorithms. |
8dd5e93a JW |
605 | template<typename _Tp> |
606 | inline void | |
cf70f97b | 607 | swap(weak_ptr<_Tp>& __a, weak_ptr<_Tp>& __b) noexcept |
8dd5e93a JW |
608 | { __a.swap(__b); } |
609 | ||
8e32aa11 BK |
610 | |
611 | /// Primary template owner_less | |
b7dabce5 | 612 | template<typename _Tp = void> |
8e32aa11 BK |
613 | struct owner_less; |
614 | ||
b7dabce5 JW |
615 | /// Void specialization of owner_less |
616 | template<> | |
617 | struct owner_less<void> : _Sp_owner_less<void, void> | |
618 | { }; | |
619 | ||
8e32aa11 | 620 | /// Partial specialization of owner_less for shared_ptr. |
8dd5e93a JW |
621 | template<typename _Tp> |
622 | struct owner_less<shared_ptr<_Tp>> | |
623 | : public _Sp_owner_less<shared_ptr<_Tp>, weak_ptr<_Tp>> | |
624 | { }; | |
625 | ||
8e32aa11 | 626 | /// Partial specialization of owner_less for weak_ptr. |
8dd5e93a JW |
627 | template<typename _Tp> |
628 | struct owner_less<weak_ptr<_Tp>> | |
629 | : public _Sp_owner_less<weak_ptr<_Tp>, shared_ptr<_Tp>> | |
630 | { }; | |
631 | ||
8e32aa11 | 632 | /** |
5b9daa7e BK |
633 | * @brief Base class allowing use of member function shared_from_this. |
634 | */ | |
459f9f82 | 635 | template<typename _Tp> |
c8bd30dd | 636 | class enable_shared_from_this |
459f9f82 PC |
637 | { |
638 | protected: | |
cf70f97b | 639 | constexpr enable_shared_from_this() noexcept { } |
8e32aa11 | 640 | |
cf70f97b | 641 | enable_shared_from_this(const enable_shared_from_this&) noexcept { } |
c8bd30dd PC |
642 | |
643 | enable_shared_from_this& | |
cf70f97b | 644 | operator=(const enable_shared_from_this&) noexcept |
c8bd30dd PC |
645 | { return *this; } |
646 | ||
647 | ~enable_shared_from_this() { } | |
648 | ||
649 | public: | |
650 | shared_ptr<_Tp> | |
651 | shared_from_this() | |
652 | { return shared_ptr<_Tp>(this->_M_weak_this); } | |
653 | ||
654 | shared_ptr<const _Tp> | |
655 | shared_from_this() const | |
656 | { return shared_ptr<const _Tp>(this->_M_weak_this); } | |
657 | ||
9a8e528c JW |
658 | #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 |
659 | #define __cpp_lib_enable_shared_from_this 201603 | |
660 | weak_ptr<_Tp> | |
208b8b69 | 661 | weak_from_this() noexcept |
9a8e528c JW |
662 | { return this->_M_weak_this; } |
663 | ||
664 | weak_ptr<const _Tp> | |
208b8b69 | 665 | weak_from_this() const noexcept |
9a8e528c JW |
666 | { return this->_M_weak_this; } |
667 | #endif | |
668 | ||
c8bd30dd PC |
669 | private: |
670 | template<typename _Tp1> | |
8e32aa11 | 671 | void |
cf70f97b | 672 | _M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const noexcept |
8e32aa11 | 673 | { _M_weak_this._M_assign(__p, __n); } |
c8bd30dd | 674 | |
f3070dab JW |
675 | // Found by ADL when this is an associated class. |
676 | friend const enable_shared_from_this* | |
677 | __enable_shared_from_this_base(const __shared_count<>&, | |
678 | const enable_shared_from_this* __p) | |
679 | { return __p; } | |
680 | ||
681 | template<typename, _Lock_policy> | |
682 | friend class __shared_ptr; | |
c8bd30dd PC |
683 | |
684 | mutable weak_ptr<_Tp> _M_weak_this; | |
459f9f82 | 685 | }; |
c8bd30dd | 686 | |
8e32aa11 BK |
687 | /** |
688 | * @brief Create an object that is owned by a shared_ptr. | |
aaf0ca6f JW |
689 | * @param __a An allocator. |
690 | * @param __args Arguments for the @a _Tp object's constructor. | |
691 | * @return A shared_ptr that owns the newly created object. | |
692 | * @throw An exception thrown from @a _Alloc::allocate or from the | |
693 | * constructor of @a _Tp. | |
694 | * | |
695 | * A copy of @a __a will be used to allocate memory for the shared_ptr | |
696 | * and the new object. | |
697 | */ | |
698 | template<typename _Tp, typename _Alloc, typename... _Args> | |
699 | inline shared_ptr<_Tp> | |
403b89a8 | 700 | allocate_shared(const _Alloc& __a, _Args&&... __args) |
aaf0ca6f | 701 | { |
403b89a8 | 702 | return shared_ptr<_Tp>(_Sp_make_shared_tag(), __a, |
8e32aa11 | 703 | std::forward<_Args>(__args)...); |
aaf0ca6f JW |
704 | } |
705 | ||
8e32aa11 BK |
706 | /** |
707 | * @brief Create an object that is owned by a shared_ptr. | |
aaf0ca6f JW |
708 | * @param __args Arguments for the @a _Tp object's constructor. |
709 | * @return A shared_ptr that owns the newly created object. | |
710 | * @throw std::bad_alloc, or an exception thrown from the | |
711 | * constructor of @a _Tp. | |
712 | */ | |
713 | template<typename _Tp, typename... _Args> | |
714 | inline shared_ptr<_Tp> | |
715 | make_shared(_Args&&... __args) | |
716 | { | |
9e2d426c | 717 | typedef typename std::remove_cv<_Tp>::type _Tp_nc; |
3096f51b PC |
718 | return std::allocate_shared<_Tp>(std::allocator<_Tp_nc>(), |
719 | std::forward<_Args>(__args)...); | |
aaf0ca6f | 720 | } |
aaf0ca6f | 721 | |
b0e788cc PC |
722 | /// std::hash specialization for shared_ptr. |
723 | template<typename _Tp> | |
724 | struct hash<shared_ptr<_Tp>> | |
9992d564 | 725 | : public __hash_base<size_t, shared_ptr<_Tp>> |
b0e788cc PC |
726 | { |
727 | size_t | |
72f1c34b | 728 | operator()(const shared_ptr<_Tp>& __s) const noexcept |
0f88f1f2 JW |
729 | { |
730 | return std::hash<typename shared_ptr<_Tp>::element_type*>()(__s.get()); | |
731 | } | |
b0e788cc PC |
732 | }; |
733 | ||
5b9daa7e BK |
734 | // @} group pointer_abstractions |
735 | ||
12ffa228 BK |
736 | _GLIBCXX_END_NAMESPACE_VERSION |
737 | } // namespace | |
32fdf2f4 JW |
738 | |
739 | #endif // _SHARED_PTR_H |