1 | // shared_ptr and weak_ptr implementation details -*- C++ -*- |
2 | |
3 | // Copyright (C) 2007-2018 Free Software Foundation, Inc. |
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 |
8 | // Free Software Foundation; either version 3, or (at your option) |
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 | |
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/>. |
24 | |
25 | // GCC Note: Based on files from version 1.32.0 of the Boost library. |
26 | |
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 | |
44 | /** @file bits/shared_ptr_base.h |
45 | * This is an internal header file, included by other library headers. |
46 | * Do not attempt to use it directly. @headername{memory} |
47 | */ |
48 | |
49 | #ifndef _SHARED_PTR_BASE_H |
50 | #define _SHARED_PTR_BASE_H 1 |
51 | |
52 | #include <typeinfo> |
53 | #include <bits/allocated_ptr.h> |
54 | #include <bits/refwrap.h> |
55 | #include <bits/stl_function.h> |
56 | #include <ext/aligned_buffer.h> |
57 | |
58 | namespace std _GLIBCXX_VISIBILITY(default) |
59 | { |
60 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
61 | |
62 | #if _GLIBCXX_USE_DEPRECATED |
63 | #pragma GCC diagnostic push |
64 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
65 | template<typename> class auto_ptr; |
66 | #pragma GCC diagnostic pop |
67 | #endif |
68 | |
69 | /** |
70 | * @brief Exception possibly thrown by @c shared_ptr. |
71 | * @ingroup exceptions |
72 | */ |
73 | class bad_weak_ptr : public std::exception |
74 | { |
75 | public: |
76 | virtual char const* what() const noexcept; |
77 | |
78 | virtual ~bad_weak_ptr() noexcept; |
79 | }; |
80 | |
81 | // Substitute for bad_weak_ptr object in the case of -fno-exceptions. |
82 | inline void |
83 | __throw_bad_weak_ptr() |
84 | { _GLIBCXX_THROW_OR_ABORT(bad_weak_ptr()); } |
85 | |
86 | using __gnu_cxx::_Lock_policy; |
87 | using __gnu_cxx::__default_lock_policy; |
88 | using __gnu_cxx::_S_single; |
89 | using __gnu_cxx::_S_mutex; |
90 | using __gnu_cxx::_S_atomic; |
91 | |
92 | // Empty helper class except when the template argument is _S_mutex. |
93 | template<_Lock_policy _Lp> |
94 | class _Mutex_base |
95 | { |
96 | protected: |
97 | // The atomic policy uses fully-fenced builtins, single doesn't care. |
98 | enum { _S_need_barriers = 0 }; |
99 | }; |
100 | |
101 | template<> |
102 | class _Mutex_base<_S_mutex> |
103 | : public __gnu_cxx::__mutex |
104 | { |
105 | protected: |
106 | // This policy is used when atomic builtins are not available. |
107 | // The replacement atomic operations might not have the necessary |
108 | // memory barriers. |
109 | enum { _S_need_barriers = 1 }; |
110 | }; |
111 | |
112 | template<_Lock_policy _Lp = __default_lock_policy> |
113 | class _Sp_counted_base |
114 | : public _Mutex_base<_Lp> |
115 | { |
116 | public: |
117 | _Sp_counted_base() noexcept |
118 | : _M_use_count(1), _M_weak_count(1) { } |
119 | |
120 | virtual |
121 | ~_Sp_counted_base() noexcept |
122 | { } |
123 | |
124 | // Called when _M_use_count drops to zero, to release the resources |
125 | // managed by *this. |
126 | virtual void |
127 | _M_dispose() noexcept = 0; |
128 | |
129 | // Called when _M_weak_count drops to zero. |
130 | virtual void |
131 | _M_destroy() noexcept |
132 | { delete this; } |
133 | |
134 | virtual void* |
135 | _M_get_deleter(const std::type_info&) noexcept = 0; |
136 | |
137 | void |
138 | _M_add_ref_copy() |
139 | { __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); } |
140 | |
141 | void |
142 | _M_add_ref_lock(); |
143 | |
144 | bool |
145 | _M_add_ref_lock_nothrow(); |
146 | |
147 | void |
148 | _M_release() noexcept |
149 | { |
150 | // Be race-detector-friendly. For more info see bits/c++config. |
151 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count); |
152 | if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1) |
153 | { |
154 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count); |
155 | _M_dispose(); |
156 | // There must be a memory barrier between dispose() and destroy() |
157 | // to ensure that the effects of dispose() are observed in the |
158 | // thread that runs destroy(). |
159 | // See http://gcc.gnu.org/ml/libstdc++/2005-11/msg00136.html |
160 | if (_Mutex_base<_Lp>::_S_need_barriers) |
161 | { |
162 | __atomic_thread_fence (__ATOMIC_ACQ_REL); |
163 | } |
164 | |
165 | // Be race-detector-friendly. For more info see bits/c++config. |
166 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count); |
167 | if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, |
168 | -1) == 1) |
169 | { |
170 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count); |
171 | _M_destroy(); |
172 | } |
173 | } |
174 | } |
175 | |
176 | void |
177 | _M_weak_add_ref() noexcept |
178 | { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); } |
179 | |
180 | void |
181 | _M_weak_release() noexcept |
182 | { |
183 | // Be race-detector-friendly. For more info see bits/c++config. |
184 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count); |
185 | if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1) |
186 | { |
187 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count); |
188 | if (_Mutex_base<_Lp>::_S_need_barriers) |
189 | { |
190 | // See _M_release(), |
191 | // destroy() must observe results of dispose() |
192 | __atomic_thread_fence (__ATOMIC_ACQ_REL); |
193 | } |
194 | _M_destroy(); |
195 | } |
196 | } |
197 | |
198 | long |
199 | _M_get_use_count() const noexcept |
200 | { |
201 | // No memory barrier is used here so there is no synchronization |
202 | // with other threads. |
203 | return __atomic_load_n(&_M_use_count, __ATOMIC_RELAXED); |
204 | } |
205 | |
206 | private: |
207 | _Sp_counted_base(_Sp_counted_base const&) = delete; |
208 | _Sp_counted_base& operator=(_Sp_counted_base const&) = delete; |
209 | |
210 | _Atomic_word _M_use_count; // #shared |
211 | _Atomic_word _M_weak_count; // #weak + (#shared != 0) |
212 | }; |
213 | |
214 | template<> |
215 | inline void |
216 | _Sp_counted_base<_S_single>:: |
217 | _M_add_ref_lock() |
218 | { |
219 | if (_M_use_count == 0) |
220 | __throw_bad_weak_ptr(); |
221 | ++_M_use_count; |
222 | } |
223 | |
224 | template<> |
225 | inline void |
226 | _Sp_counted_base<_S_mutex>:: |
227 | _M_add_ref_lock() |
228 | { |
229 | __gnu_cxx::__scoped_lock sentry(*this); |
230 | if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0) |
231 | { |
232 | _M_use_count = 0; |
233 | __throw_bad_weak_ptr(); |
234 | } |
235 | } |
236 | |
237 | template<> |
238 | inline void |
239 | _Sp_counted_base<_S_atomic>:: |
240 | _M_add_ref_lock() |
241 | { |
242 | // Perform lock-free add-if-not-zero operation. |
243 | _Atomic_word __count = _M_get_use_count(); |
244 | do |
245 | { |
246 | if (__count == 0) |
247 | __throw_bad_weak_ptr(); |
248 | // Replace the current counter value with the old value + 1, as |
249 | // long as it's not changed meanwhile. |
250 | } |
251 | while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1, |
252 | true, __ATOMIC_ACQ_REL, |
253 | __ATOMIC_RELAXED)); |
254 | } |
255 | |
256 | template<> |
257 | inline bool |
258 | _Sp_counted_base<_S_single>:: |
259 | _M_add_ref_lock_nothrow() |
260 | { |
261 | if (_M_use_count == 0) |
262 | return false; |
263 | ++_M_use_count; |
264 | return true; |
265 | } |
266 | |
267 | template<> |
268 | inline bool |
269 | _Sp_counted_base<_S_mutex>:: |
270 | _M_add_ref_lock_nothrow() |
271 | { |
272 | __gnu_cxx::__scoped_lock sentry(*this); |
273 | if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0) |
274 | { |
275 | _M_use_count = 0; |
276 | return false; |
277 | } |
278 | return true; |
279 | } |
280 | |
281 | template<> |
282 | inline bool |
283 | _Sp_counted_base<_S_atomic>:: |
284 | _M_add_ref_lock_nothrow() |
285 | { |
286 | // Perform lock-free add-if-not-zero operation. |
287 | _Atomic_word __count = _M_get_use_count(); |
288 | do |
289 | { |
290 | if (__count == 0) |
291 | return false; |
292 | // Replace the current counter value with the old value + 1, as |
293 | // long as it's not changed meanwhile. |
294 | } |
295 | while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1, |
296 | true, __ATOMIC_ACQ_REL, |
297 | __ATOMIC_RELAXED)); |
298 | return true; |
299 | } |
300 | |
301 | template<> |
302 | inline void |
303 | _Sp_counted_base<_S_single>::_M_add_ref_copy() |
304 | { ++_M_use_count; } |
305 | |
306 | template<> |
307 | inline void |
308 | _Sp_counted_base<_S_single>::_M_release() noexcept |
309 | { |
310 | if (--_M_use_count == 0) |
311 | { |
312 | _M_dispose(); |
313 | if (--_M_weak_count == 0) |
314 | _M_destroy(); |
315 | } |
316 | } |
317 | |
318 | template<> |
319 | inline void |
320 | _Sp_counted_base<_S_single>::_M_weak_add_ref() noexcept |
321 | { ++_M_weak_count; } |
322 | |
323 | template<> |
324 | inline void |
325 | _Sp_counted_base<_S_single>::_M_weak_release() noexcept |
326 | { |
327 | if (--_M_weak_count == 0) |
328 | _M_destroy(); |
329 | } |
330 | |
331 | template<> |
332 | inline long |
333 | _Sp_counted_base<_S_single>::_M_get_use_count() const noexcept |
334 | { return _M_use_count; } |
335 | |
336 | |
337 | // Forward declarations. |
338 | template<typename _Tp, _Lock_policy _Lp = __default_lock_policy> |
339 | class __shared_ptr; |
340 | |
341 | template<typename _Tp, _Lock_policy _Lp = __default_lock_policy> |
342 | class __weak_ptr; |
343 | |
344 | template<typename _Tp, _Lock_policy _Lp = __default_lock_policy> |
345 | class __enable_shared_from_this; |
346 | |
347 | template<typename _Tp> |
348 | class shared_ptr; |
349 | |
350 | template<typename _Tp> |
351 | class weak_ptr; |
352 | |
353 | template<typename _Tp> |
354 | struct owner_less; |
355 | |
356 | template<typename _Tp> |
357 | class enable_shared_from_this; |
358 | |
359 | template<_Lock_policy _Lp = __default_lock_policy> |
360 | class __weak_count; |
361 | |
362 | template<_Lock_policy _Lp = __default_lock_policy> |
363 | class __shared_count; |
364 | |
365 | |
366 | // Counted ptr with no deleter or allocator support |
367 | template<typename _Ptr, _Lock_policy _Lp> |
368 | class _Sp_counted_ptr final : public _Sp_counted_base<_Lp> |
369 | { |
370 | public: |
371 | explicit |
372 | _Sp_counted_ptr(_Ptr __p) noexcept |
373 | : _M_ptr(__p) { } |
374 | |
375 | virtual void |
376 | _M_dispose() noexcept |
377 | { delete _M_ptr; } |
378 | |
379 | virtual void |
380 | _M_destroy() noexcept |
381 | { delete this; } |
382 | |
383 | virtual void* |
384 | _M_get_deleter(const std::type_info&) noexcept |
385 | { return nullptr; } |
386 | |
387 | _Sp_counted_ptr(const _Sp_counted_ptr&) = delete; |
388 | _Sp_counted_ptr& operator=(const _Sp_counted_ptr&) = delete; |
389 | |
390 | private: |
391 | _Ptr _M_ptr; |
392 | }; |
393 | |
394 | template<> |
395 | inline void |
396 | _Sp_counted_ptr<nullptr_t, _S_single>::_M_dispose() noexcept { } |
397 | |
398 | template<> |
399 | inline void |
400 | _Sp_counted_ptr<nullptr_t, _S_mutex>::_M_dispose() noexcept { } |
401 | |
402 | template<> |
403 | inline void |
404 | _Sp_counted_ptr<nullptr_t, _S_atomic>::_M_dispose() noexcept { } |
405 | |
406 | template<int _Nm, typename _Tp, |
407 | bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)> |
408 | struct _Sp_ebo_helper; |
409 | |
410 | /// Specialization using EBO. |
411 | template<int _Nm, typename _Tp> |
412 | struct _Sp_ebo_helper<_Nm, _Tp, true> : private _Tp |
413 | { |
414 | explicit _Sp_ebo_helper(const _Tp& __tp) : _Tp(__tp) { } |
415 | explicit _Sp_ebo_helper(_Tp&& __tp) : _Tp(std::move(__tp)) { } |
416 | |
417 | static _Tp& |
418 | _S_get(_Sp_ebo_helper& __eboh) { return static_cast<_Tp&>(__eboh); } |
419 | }; |
420 | |
421 | /// Specialization not using EBO. |
422 | template<int _Nm, typename _Tp> |
423 | struct _Sp_ebo_helper<_Nm, _Tp, false> |
424 | { |
425 | explicit _Sp_ebo_helper(const _Tp& __tp) : _M_tp(__tp) { } |
426 | explicit _Sp_ebo_helper(_Tp&& __tp) : _M_tp(std::move(__tp)) { } |
427 | |
428 | static _Tp& |
429 | _S_get(_Sp_ebo_helper& __eboh) |
430 | { return __eboh._M_tp; } |
431 | |
432 | private: |
433 | _Tp _M_tp; |
434 | }; |
435 | |
436 | // Support for custom deleter and/or allocator |
437 | template<typename _Ptr, typename _Deleter, typename _Alloc, _Lock_policy _Lp> |
438 | class _Sp_counted_deleter final : public _Sp_counted_base<_Lp> |
439 | { |
440 | class _Impl : _Sp_ebo_helper<0, _Deleter>, _Sp_ebo_helper<1, _Alloc> |
441 | { |
442 | typedef _Sp_ebo_helper<0, _Deleter> _Del_base; |
443 | typedef _Sp_ebo_helper<1, _Alloc> _Alloc_base; |
444 | |
445 | public: |
446 | _Impl(_Ptr __p, _Deleter __d, const _Alloc& __a) noexcept |
447 | : _M_ptr(__p), _Del_base(std::move(__d)), _Alloc_base(__a) |
448 | { } |
449 | |
450 | _Deleter& _M_del() noexcept { return _Del_base::_S_get(*this); } |
451 | _Alloc& _M_alloc() noexcept { return _Alloc_base::_S_get(*this); } |
452 | |
453 | _Ptr _M_ptr; |
454 | }; |
455 | |
456 | public: |
457 | using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_deleter>; |
458 | |
459 | // __d(__p) must not throw. |
460 | _Sp_counted_deleter(_Ptr __p, _Deleter __d) noexcept |
461 | : _M_impl(__p, std::move(__d), _Alloc()) { } |
462 | |
463 | // __d(__p) must not throw. |
464 | _Sp_counted_deleter(_Ptr __p, _Deleter __d, const _Alloc& __a) noexcept |
465 | : _M_impl(__p, std::move(__d), __a) { } |
466 | |
467 | ~_Sp_counted_deleter() noexcept { } |
468 | |
469 | virtual void |
470 | _M_dispose() noexcept |
471 | { _M_impl._M_del()(_M_impl._M_ptr); } |
472 | |
473 | virtual void |
474 | _M_destroy() noexcept |
475 | { |
476 | __allocator_type __a(_M_impl._M_alloc()); |
477 | __allocated_ptr<__allocator_type> __guard_ptr{ __a, this }; |
478 | this->~_Sp_counted_deleter(); |
479 | } |
480 | |
481 | virtual void* |
482 | _M_get_deleter(const std::type_info& __ti) noexcept |
483 | { |
484 | #if __cpp_rtti |
485 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
486 | // 2400. shared_ptr's get_deleter() should use addressof() |
487 | return __ti == typeid(_Deleter) |
488 | ? std::__addressof(_M_impl._M_del()) |
489 | : nullptr; |
490 | #else |
491 | return nullptr; |
492 | #endif |
493 | } |
494 | |
495 | private: |
496 | _Impl _M_impl; |
497 | }; |
498 | |
499 | // helpers for make_shared / allocate_shared |
500 | |
501 | struct _Sp_make_shared_tag |
502 | { |
503 | private: |
504 | template<typename _Tp, _Lock_policy _Lp> |
505 | friend class __shared_ptr; |
506 | template<typename _Tp, typename _Alloc, _Lock_policy _Lp> |
507 | friend class _Sp_counted_ptr_inplace; |
508 | |
509 | static const type_info& |
510 | _S_ti() noexcept _GLIBCXX_VISIBILITY(default) |
511 | { |
512 | alignas(type_info) static constexpr char __tag[sizeof(type_info)] = { }; |
513 | return reinterpret_cast<const type_info&>(__tag); |
514 | } |
515 | }; |
516 | |
517 | template<typename _Tp, typename _Alloc, _Lock_policy _Lp> |
518 | class _Sp_counted_ptr_inplace final : public _Sp_counted_base<_Lp> |
519 | { |
520 | class _Impl : _Sp_ebo_helper<0, _Alloc> |
521 | { |
522 | typedef _Sp_ebo_helper<0, _Alloc> _A_base; |
523 | |
524 | public: |
525 | explicit _Impl(_Alloc __a) noexcept : _A_base(__a) { } |
526 | |
527 | _Alloc& _M_alloc() noexcept { return _A_base::_S_get(*this); } |
528 | |
529 | __gnu_cxx::__aligned_buffer<_Tp> _M_storage; |
530 | }; |
531 | |
532 | public: |
533 | using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>; |
534 | |
535 | template<typename... _Args> |
536 | _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args) |
537 | : _M_impl(__a) |
538 | { |
539 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
540 | // 2070. allocate_shared should use allocator_traits<A>::construct |
541 | allocator_traits<_Alloc>::construct(__a, _M_ptr(), |
542 | std::forward<_Args>(__args)...); // might throw |
543 | } |
544 | |
545 | ~_Sp_counted_ptr_inplace() noexcept { } |
546 | |
547 | virtual void |
548 | _M_dispose() noexcept |
549 | { |
550 | allocator_traits<_Alloc>::destroy(_M_impl._M_alloc(), _M_ptr()); |
551 | } |
552 | |
553 | // Override because the allocator needs to know the dynamic type |
554 | virtual void |
555 | _M_destroy() noexcept |
556 | { |
557 | __allocator_type __a(_M_impl._M_alloc()); |
558 | __allocated_ptr<__allocator_type> __guard_ptr{ __a, this }; |
559 | this->~_Sp_counted_ptr_inplace(); |
560 | } |
561 | |
562 | // Sneaky trick so __shared_ptr can get the managed pointer. |
563 | virtual void* |
564 | _M_get_deleter(const std::type_info& __ti) noexcept override |
565 | { |
566 | // Check for the fake type_info first, so we don't try to access it |
567 | // as a real type_info object. |
568 | if (&__ti == &_Sp_make_shared_tag::_S_ti()) |
569 | return const_cast<typename remove_cv<_Tp>::type*>(_M_ptr()); |
570 | #if __cpp_rtti |
571 | // Callers compiled with old libstdc++ headers and RTTI enabled |
572 | // might pass this instead: |
573 | else if (__ti == typeid(_Sp_make_shared_tag)) |
574 | return const_cast<typename remove_cv<_Tp>::type*>(_M_ptr()); |
575 | #endif |
576 | return nullptr; |
577 | } |
578 | |
579 | private: |
580 | _Tp* _M_ptr() noexcept { return _M_impl._M_storage._M_ptr(); } |
581 | |
582 | _Impl _M_impl; |
583 | }; |
584 | |
585 | // The default deleter for shared_ptr<T[]> and shared_ptr<T[N]>. |
586 | struct __sp_array_delete |
587 | { |
588 | template<typename _Yp> |
589 | void operator()(_Yp* __p) const { delete[] __p; } |
590 | }; |
591 | |
592 | template<_Lock_policy _Lp> |
593 | class __shared_count |
594 | { |
595 | public: |
596 | constexpr __shared_count() noexcept : _M_pi(0) |
597 | { } |
598 | |
599 | template<typename _Ptr> |
600 | explicit |
601 | __shared_count(_Ptr __p) : _M_pi(0) |
602 | { |
603 | __try |
604 | { |
605 | _M_pi = new _Sp_counted_ptr<_Ptr, _Lp>(__p); |
606 | } |
607 | __catch(...) |
608 | { |
609 | delete __p; |
610 | __throw_exception_again; |
611 | } |
612 | } |
613 | |
614 | template<typename _Ptr> |
615 | __shared_count(_Ptr __p, /* is_array = */ false_type) |
616 | : __shared_count(__p) |
617 | { } |
618 | |
619 | template<typename _Ptr> |
620 | __shared_count(_Ptr __p, /* is_array = */ true_type) |
621 | : __shared_count(__p, __sp_array_delete{}, allocator<void>()) |
622 | { } |
623 | |
624 | template<typename _Ptr, typename _Deleter> |
625 | __shared_count(_Ptr __p, _Deleter __d) |
626 | : __shared_count(__p, std::move(__d), allocator<void>()) |
627 | { } |
628 | |
629 | template<typename _Ptr, typename _Deleter, typename _Alloc> |
630 | __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0) |
631 | { |
632 | typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type; |
633 | __try |
634 | { |
635 | typename _Sp_cd_type::__allocator_type __a2(__a); |
636 | auto __guard = std::__allocate_guarded(__a2); |
637 | _Sp_cd_type* __mem = __guard.get(); |
638 | ::new (__mem) _Sp_cd_type(__p, std::move(__d), std::move(__a)); |
639 | _M_pi = __mem; |
640 | __guard = nullptr; |
641 | } |
642 | __catch(...) |
643 | { |
644 | __d(__p); // Call _Deleter on __p. |
645 | __throw_exception_again; |
646 | } |
647 | } |
648 | |
649 | template<typename _Tp, typename _Alloc, typename... _Args> |
650 | __shared_count(_Sp_make_shared_tag, _Tp*, const _Alloc& __a, |
651 | _Args&&... __args) |
652 | : _M_pi(0) |
653 | { |
654 | typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type; |
655 | typename _Sp_cp_type::__allocator_type __a2(__a); |
656 | auto __guard = std::__allocate_guarded(__a2); |
657 | _Sp_cp_type* __mem = __guard.get(); |
658 | ::new (__mem) _Sp_cp_type(std::move(__a), |
659 | std::forward<_Args>(__args)...); |
660 | _M_pi = __mem; |
661 | __guard = nullptr; |
662 | } |
663 | |
664 | #if _GLIBCXX_USE_DEPRECATED |
665 | #pragma GCC diagnostic push |
666 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
667 | // Special case for auto_ptr<_Tp> to provide the strong guarantee. |
668 | template<typename _Tp> |
669 | explicit |
670 | __shared_count(std::auto_ptr<_Tp>&& __r); |
671 | #pragma GCC diagnostic pop |
672 | #endif |
673 | |
674 | // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee. |
675 | template<typename _Tp, typename _Del> |
676 | explicit |
677 | __shared_count(std::unique_ptr<_Tp, _Del>&& __r) : _M_pi(0) |
678 | { |
679 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
680 | // 2415. Inconsistency between unique_ptr and shared_ptr |
681 | if (__r.get() == nullptr) |
682 | return; |
683 | |
684 | using _Ptr = typename unique_ptr<_Tp, _Del>::pointer; |
685 | using _Del2 = typename conditional<is_reference<_Del>::value, |
686 | reference_wrapper<typename remove_reference<_Del>::type>, |
687 | _Del>::type; |
688 | using _Sp_cd_type |
689 | = _Sp_counted_deleter<_Ptr, _Del2, allocator<void>, _Lp>; |
690 | using _Alloc = allocator<_Sp_cd_type>; |
691 | using _Alloc_traits = allocator_traits<_Alloc>; |
692 | _Alloc __a; |
693 | _Sp_cd_type* __mem = _Alloc_traits::allocate(__a, 1); |
694 | _Alloc_traits::construct(__a, __mem, __r.release(), |
695 | __r.get_deleter()); // non-throwing |
696 | _M_pi = __mem; |
697 | } |
698 | |
699 | // Throw bad_weak_ptr when __r._M_get_use_count() == 0. |
700 | explicit __shared_count(const __weak_count<_Lp>& __r); |
701 | |
702 | // Does not throw if __r._M_get_use_count() == 0, caller must check. |
703 | explicit __shared_count(const __weak_count<_Lp>& __r, std::nothrow_t); |
704 | |
705 | ~__shared_count() noexcept |
706 | { |
707 | if (_M_pi != nullptr) |
708 | _M_pi->_M_release(); |
709 | } |
710 | |
711 | __shared_count(const __shared_count& __r) noexcept |
712 | : _M_pi(__r._M_pi) |
713 | { |
714 | if (_M_pi != 0) |
715 | _M_pi->_M_add_ref_copy(); |
716 | } |
717 | |
718 | __shared_count& |
719 | operator=(const __shared_count& __r) noexcept |
720 | { |
721 | _Sp_counted_base<_Lp>* __tmp = __r._M_pi; |
722 | if (__tmp != _M_pi) |
723 | { |
724 | if (__tmp != 0) |
725 | __tmp->_M_add_ref_copy(); |
726 | if (_M_pi != 0) |
727 | _M_pi->_M_release(); |
728 | _M_pi = __tmp; |
729 | } |
730 | return *this; |
731 | } |
732 | |
733 | void |
734 | _M_swap(__shared_count& __r) noexcept |
735 | { |
736 | _Sp_counted_base<_Lp>* __tmp = __r._M_pi; |
737 | __r._M_pi = _M_pi; |
738 | _M_pi = __tmp; |
739 | } |
740 | |
741 | long |
742 | _M_get_use_count() const noexcept |
743 | { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; } |
744 | |
745 | bool |
746 | _M_unique() const noexcept |
747 | { return this->_M_get_use_count() == 1; } |
748 | |
749 | void* |
750 | _M_get_deleter(const std::type_info& __ti) const noexcept |
751 | { return _M_pi ? _M_pi->_M_get_deleter(__ti) : nullptr; } |
752 | |
753 | bool |
754 | _M_less(const __shared_count& __rhs) const noexcept |
755 | { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); } |
756 | |
757 | bool |
758 | _M_less(const __weak_count<_Lp>& __rhs) const noexcept |
759 | { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); } |
760 | |
761 | // Friend function injected into enclosing namespace and found by ADL |
762 | friend inline bool |
763 | operator==(const __shared_count& __a, const __shared_count& __b) noexcept |
764 | { return __a._M_pi == __b._M_pi; } |
765 | |
766 | private: |
767 | friend class __weak_count<_Lp>; |
768 | |
769 | _Sp_counted_base<_Lp>* _M_pi; |
770 | }; |
771 | |
772 | |
773 | template<_Lock_policy _Lp> |
774 | class __weak_count |
775 | { |
776 | public: |
777 | constexpr __weak_count() noexcept : _M_pi(nullptr) |
778 | { } |
779 | |
780 | __weak_count(const __shared_count<_Lp>& __r) noexcept |
781 | : _M_pi(__r._M_pi) |
782 | { |
783 | if (_M_pi != nullptr) |
784 | _M_pi->_M_weak_add_ref(); |
785 | } |
786 | |
787 | __weak_count(const __weak_count& __r) noexcept |
788 | : _M_pi(__r._M_pi) |
789 | { |
790 | if (_M_pi != nullptr) |
791 | _M_pi->_M_weak_add_ref(); |
792 | } |
793 | |
794 | __weak_count(__weak_count&& __r) noexcept |
795 | : _M_pi(__r._M_pi) |
796 | { __r._M_pi = nullptr; } |
797 | |
798 | ~__weak_count() noexcept |
799 | { |
800 | if (_M_pi != nullptr) |
801 | _M_pi->_M_weak_release(); |
802 | } |
803 | |
804 | __weak_count& |
805 | operator=(const __shared_count<_Lp>& __r) noexcept |
806 | { |
807 | _Sp_counted_base<_Lp>* __tmp = __r._M_pi; |
808 | if (__tmp != nullptr) |
809 | __tmp->_M_weak_add_ref(); |
810 | if (_M_pi != nullptr) |
811 | _M_pi->_M_weak_release(); |
812 | _M_pi = __tmp; |
813 | return *this; |
814 | } |
815 | |
816 | __weak_count& |
817 | operator=(const __weak_count& __r) noexcept |
818 | { |
819 | _Sp_counted_base<_Lp>* __tmp = __r._M_pi; |
820 | if (__tmp != nullptr) |
821 | __tmp->_M_weak_add_ref(); |
822 | if (_M_pi != nullptr) |
823 | _M_pi->_M_weak_release(); |
824 | _M_pi = __tmp; |
825 | return *this; |
826 | } |
827 | |
828 | __weak_count& |
829 | operator=(__weak_count&& __r) noexcept |
830 | { |
831 | if (_M_pi != nullptr) |
832 | _M_pi->_M_weak_release(); |
833 | _M_pi = __r._M_pi; |
834 | __r._M_pi = nullptr; |
835 | return *this; |
836 | } |
837 | |
838 | void |
839 | _M_swap(__weak_count& __r) noexcept |
840 | { |
841 | _Sp_counted_base<_Lp>* __tmp = __r._M_pi; |
842 | __r._M_pi = _M_pi; |
843 | _M_pi = __tmp; |
844 | } |
845 | |
846 | long |
847 | _M_get_use_count() const noexcept |
848 | { return _M_pi != nullptr ? _M_pi->_M_get_use_count() : 0; } |
849 | |
850 | bool |
851 | _M_less(const __weak_count& __rhs) const noexcept |
852 | { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); } |
853 | |
854 | bool |
855 | _M_less(const __shared_count<_Lp>& __rhs) const noexcept |
856 | { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); } |
857 | |
858 | // Friend function injected into enclosing namespace and found by ADL |
859 | friend inline bool |
860 | operator==(const __weak_count& __a, const __weak_count& __b) noexcept |
861 | { return __a._M_pi == __b._M_pi; } |
862 | |
863 | private: |
864 | friend class __shared_count<_Lp>; |
865 | |
866 | _Sp_counted_base<_Lp>* _M_pi; |
867 | }; |
868 | |
869 | // Now that __weak_count is defined we can define this constructor: |
870 | template<_Lock_policy _Lp> |
871 | inline |
872 | __shared_count<_Lp>::__shared_count(const __weak_count<_Lp>& __r) |
873 | : _M_pi(__r._M_pi) |
874 | { |
875 | if (_M_pi != nullptr) |
876 | _M_pi->_M_add_ref_lock(); |
877 | else |
878 | __throw_bad_weak_ptr(); |
879 | } |
880 | |
881 | // Now that __weak_count is defined we can define this constructor: |
882 | template<_Lock_policy _Lp> |
883 | inline |
884 | __shared_count<_Lp>:: |
885 | __shared_count(const __weak_count<_Lp>& __r, std::nothrow_t) |
886 | : _M_pi(__r._M_pi) |
887 | { |
888 | if (_M_pi != nullptr) |
889 | if (!_M_pi->_M_add_ref_lock_nothrow()) |
890 | _M_pi = nullptr; |
891 | } |
892 | |
893 | #define __cpp_lib_shared_ptr_arrays 201603 |
894 | |
895 | // Helper traits for shared_ptr of array: |
896 | |
897 | // A pointer type Y* is said to be compatible with a pointer type T* when |
898 | // either Y* is convertible to T* or Y is U[N] and T is U cv []. |
899 | template<typename _Yp_ptr, typename _Tp_ptr> |
900 | struct __sp_compatible_with |
901 | : false_type |
902 | { }; |
903 | |
904 | template<typename _Yp, typename _Tp> |
905 | struct __sp_compatible_with<_Yp*, _Tp*> |
906 | : is_convertible<_Yp*, _Tp*>::type |
907 | { }; |
908 | |
909 | template<typename _Up, size_t _Nm> |
910 | struct __sp_compatible_with<_Up(*)[_Nm], _Up(*)[]> |
911 | : true_type |
912 | { }; |
913 | |
914 | template<typename _Up, size_t _Nm> |
915 | struct __sp_compatible_with<_Up(*)[_Nm], const _Up(*)[]> |
916 | : true_type |
917 | { }; |
918 | |
919 | template<typename _Up, size_t _Nm> |
920 | struct __sp_compatible_with<_Up(*)[_Nm], volatile _Up(*)[]> |
921 | : true_type |
922 | { }; |
923 | |
924 | template<typename _Up, size_t _Nm> |
925 | struct __sp_compatible_with<_Up(*)[_Nm], const volatile _Up(*)[]> |
926 | : true_type |
927 | { }; |
928 | |
929 | // Test conversion from Y(*)[N] to U(*)[N] without forming invalid type Y[N]. |
930 | template<typename _Up, size_t _Nm, typename _Yp, typename = void> |
931 | struct __sp_is_constructible_arrN |
932 | : false_type |
933 | { }; |
934 | |
935 | template<typename _Up, size_t _Nm, typename _Yp> |
936 | struct __sp_is_constructible_arrN<_Up, _Nm, _Yp, __void_t<_Yp[_Nm]>> |
937 | : is_convertible<_Yp(*)[_Nm], _Up(*)[_Nm]>::type |
938 | { }; |
939 | |
940 | // Test conversion from Y(*)[] to U(*)[] without forming invalid type Y[]. |
941 | template<typename _Up, typename _Yp, typename = void> |
942 | struct __sp_is_constructible_arr |
943 | : false_type |
944 | { }; |
945 | |
946 | template<typename _Up, typename _Yp> |
947 | struct __sp_is_constructible_arr<_Up, _Yp, __void_t<_Yp[]>> |
948 | : is_convertible<_Yp(*)[], _Up(*)[]>::type |
949 | { }; |
950 | |
951 | // Trait to check if shared_ptr<T> can be constructed from Y*. |
952 | template<typename _Tp, typename _Yp> |
953 | struct __sp_is_constructible; |
954 | |
955 | // When T is U[N], Y(*)[N] shall be convertible to T*; |
956 | template<typename _Up, size_t _Nm, typename _Yp> |
957 | struct __sp_is_constructible<_Up[_Nm], _Yp> |
958 | : __sp_is_constructible_arrN<_Up, _Nm, _Yp>::type |
959 | { }; |
960 | |
961 | // when T is U[], Y(*)[] shall be convertible to T*; |
962 | template<typename _Up, typename _Yp> |
963 | struct __sp_is_constructible<_Up[], _Yp> |
964 | : __sp_is_constructible_arr<_Up, _Yp>::type |
965 | { }; |
966 | |
967 | // otherwise, Y* shall be convertible to T*. |
968 | template<typename _Tp, typename _Yp> |
969 | struct __sp_is_constructible |
970 | : is_convertible<_Yp*, _Tp*>::type |
971 | { }; |
972 | |
973 | |
974 | // Define operator* and operator-> for shared_ptr<T>. |
975 | template<typename _Tp, _Lock_policy _Lp, |
976 | bool = is_array<_Tp>::value, bool = is_void<_Tp>::value> |
977 | class __shared_ptr_access |
978 | { |
979 | public: |
980 | using element_type = _Tp; |
981 | |
982 | element_type& |
983 | operator*() const noexcept |
984 | { |
985 | __glibcxx_assert(_M_get() != nullptr); |
986 | return *_M_get(); |
987 | } |
988 | |
989 | element_type* |
990 | operator->() const noexcept |
991 | { |
992 | _GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr); |
993 | return _M_get(); |
994 | } |
995 | |
996 | private: |
997 | element_type* |
998 | _M_get() const noexcept |
999 | { return static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get(); } |
1000 | }; |
1001 | |
1002 | // Define operator-> for shared_ptr<cv void>. |
1003 | template<typename _Tp, _Lock_policy _Lp> |
1004 | class __shared_ptr_access<_Tp, _Lp, false, true> |
1005 | { |
1006 | public: |
1007 | using element_type = _Tp; |
1008 | |
1009 | element_type* |
1010 | operator->() const noexcept |
1011 | { |
1012 | auto __ptr = static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get(); |
1013 | _GLIBCXX_DEBUG_PEDASSERT(__ptr != nullptr); |
1014 | return __ptr; |
1015 | } |
1016 | }; |
1017 | |
1018 | // Define operator[] for shared_ptr<T[]> and shared_ptr<T[N]>. |
1019 | template<typename _Tp, _Lock_policy _Lp> |
1020 | class __shared_ptr_access<_Tp, _Lp, true, false> |
1021 | { |
1022 | public: |
1023 | using element_type = typename remove_extent<_Tp>::type; |
1024 | |
1025 | #if __cplusplus <= 201402L |
1026 | [[__deprecated__("shared_ptr<T[]>::operator* is absent from C++17" )]] |
1027 | element_type& |
1028 | operator*() const noexcept |
1029 | { |
1030 | __glibcxx_assert(_M_get() != nullptr); |
1031 | return *_M_get(); |
1032 | } |
1033 | |
1034 | [[__deprecated__("shared_ptr<T[]>::operator-> is absent from C++17" )]] |
1035 | element_type* |
1036 | operator->() const noexcept |
1037 | { |
1038 | _GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr); |
1039 | return _M_get(); |
1040 | } |
1041 | #endif |
1042 | |
1043 | element_type& |
1044 | operator[](ptrdiff_t __i) const |
1045 | { |
1046 | __glibcxx_assert(_M_get() != nullptr); |
1047 | __glibcxx_assert(!extent<_Tp>::value || __i < extent<_Tp>::value); |
1048 | return _M_get()[__i]; |
1049 | } |
1050 | |
1051 | private: |
1052 | element_type* |
1053 | _M_get() const noexcept |
1054 | { return static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get(); } |
1055 | }; |
1056 | |
1057 | template<typename _Tp, _Lock_policy _Lp> |
1058 | class __shared_ptr |
1059 | : public __shared_ptr_access<_Tp, _Lp> |
1060 | { |
1061 | public: |
1062 | using element_type = typename remove_extent<_Tp>::type; |
1063 | |
1064 | private: |
1065 | // Constraint for taking ownership of a pointer of type _Yp*: |
1066 | template<typename _Yp> |
1067 | using _SafeConv |
1068 | = typename enable_if<__sp_is_constructible<_Tp, _Yp>::value>::type; |
1069 | |
1070 | // Constraint for construction from shared_ptr and weak_ptr: |
1071 | template<typename _Yp, typename _Res = void> |
1072 | using _Compatible = typename |
1073 | enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type; |
1074 | |
1075 | // Constraint for assignment from shared_ptr and weak_ptr: |
1076 | template<typename _Yp> |
1077 | using _Assignable = _Compatible<_Yp, __shared_ptr&>; |
1078 | |
1079 | // Constraint for construction from unique_ptr: |
1080 | template<typename _Yp, typename _Del, typename _Res = void, |
1081 | typename _Ptr = typename unique_ptr<_Yp, _Del>::pointer> |
1082 | using _UniqCompatible = typename enable_if<__and_< |
1083 | __sp_compatible_with<_Yp*, _Tp*>, is_convertible<_Ptr, element_type*> |
1084 | >::value, _Res>::type; |
1085 | |
1086 | // Constraint for assignment from unique_ptr: |
1087 | template<typename _Yp, typename _Del> |
1088 | using _UniqAssignable = _UniqCompatible<_Yp, _Del, __shared_ptr&>; |
1089 | |
1090 | public: |
1091 | |
1092 | #if __cplusplus > 201402L |
1093 | using weak_type = __weak_ptr<_Tp, _Lp>; |
1094 | #endif |
1095 | |
1096 | constexpr __shared_ptr() noexcept |
1097 | : _M_ptr(0), _M_refcount() |
1098 | { } |
1099 | |
1100 | template<typename _Yp, typename = _SafeConv<_Yp>> |
1101 | explicit |
1102 | __shared_ptr(_Yp* __p) |
1103 | : _M_ptr(__p), _M_refcount(__p, typename is_array<_Tp>::type()) |
1104 | { |
1105 | static_assert( !is_void<_Yp>::value, "incomplete type" ); |
1106 | static_assert( sizeof(_Yp) > 0, "incomplete type" ); |
1107 | _M_enable_shared_from_this_with(__p); |
1108 | } |
1109 | |
1110 | template<typename _Yp, typename _Deleter, typename = _SafeConv<_Yp>> |
1111 | __shared_ptr(_Yp* __p, _Deleter __d) |
1112 | : _M_ptr(__p), _M_refcount(__p, std::move(__d)) |
1113 | { |
1114 | static_assert(__is_invocable<_Deleter&, _Yp*&>::value, |
1115 | "deleter expression d(p) is well-formed" ); |
1116 | _M_enable_shared_from_this_with(__p); |
1117 | } |
1118 | |
1119 | template<typename _Yp, typename _Deleter, typename _Alloc, |
1120 | typename = _SafeConv<_Yp>> |
1121 | __shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a) |
1122 | : _M_ptr(__p), _M_refcount(__p, std::move(__d), std::move(__a)) |
1123 | { |
1124 | static_assert(__is_invocable<_Deleter&, _Yp*&>::value, |
1125 | "deleter expression d(p) is well-formed" ); |
1126 | _M_enable_shared_from_this_with(__p); |
1127 | } |
1128 | |
1129 | template<typename _Deleter> |
1130 | __shared_ptr(nullptr_t __p, _Deleter __d) |
1131 | : _M_ptr(0), _M_refcount(__p, std::move(__d)) |
1132 | { } |
1133 | |
1134 | template<typename _Deleter, typename _Alloc> |
1135 | __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a) |
1136 | : _M_ptr(0), _M_refcount(__p, std::move(__d), std::move(__a)) |
1137 | { } |
1138 | |
1139 | template<typename _Yp> |
1140 | __shared_ptr(const __shared_ptr<_Yp, _Lp>& __r, |
1141 | element_type* __p) noexcept |
1142 | : _M_ptr(__p), _M_refcount(__r._M_refcount) // never throws |
1143 | { } |
1144 | |
1145 | __shared_ptr(const __shared_ptr&) noexcept = default; |
1146 | __shared_ptr& operator=(const __shared_ptr&) noexcept = default; |
1147 | ~__shared_ptr() = default; |
1148 | |
1149 | template<typename _Yp, typename = _Compatible<_Yp>> |
1150 | __shared_ptr(const __shared_ptr<_Yp, _Lp>& __r) noexcept |
1151 | : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) |
1152 | { } |
1153 | |
1154 | __shared_ptr(__shared_ptr&& __r) noexcept |
1155 | : _M_ptr(__r._M_ptr), _M_refcount() |
1156 | { |
1157 | _M_refcount._M_swap(__r._M_refcount); |
1158 | __r._M_ptr = 0; |
1159 | } |
1160 | |
1161 | template<typename _Yp, typename = _Compatible<_Yp>> |
1162 | __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r) noexcept |
1163 | : _M_ptr(__r._M_ptr), _M_refcount() |
1164 | { |
1165 | _M_refcount._M_swap(__r._M_refcount); |
1166 | __r._M_ptr = 0; |
1167 | } |
1168 | |
1169 | template<typename _Yp, typename = _Compatible<_Yp>> |
1170 | explicit __shared_ptr(const __weak_ptr<_Yp, _Lp>& __r) |
1171 | : _M_refcount(__r._M_refcount) // may throw |
1172 | { |
1173 | // It is now safe to copy __r._M_ptr, as |
1174 | // _M_refcount(__r._M_refcount) did not throw. |
1175 | _M_ptr = __r._M_ptr; |
1176 | } |
1177 | |
1178 | // If an exception is thrown this constructor has no effect. |
1179 | template<typename _Yp, typename _Del, |
1180 | typename = _UniqCompatible<_Yp, _Del>> |
1181 | __shared_ptr(unique_ptr<_Yp, _Del>&& __r) |
1182 | : _M_ptr(__r.get()), _M_refcount() |
1183 | { |
1184 | auto __raw = __to_address(__r.get()); |
1185 | _M_refcount = __shared_count<_Lp>(std::move(__r)); |
1186 | _M_enable_shared_from_this_with(__raw); |
1187 | } |
1188 | |
1189 | #if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED |
1190 | protected: |
1191 | // If an exception is thrown this constructor has no effect. |
1192 | template<typename _Tp1, typename _Del, |
1193 | typename enable_if<__and_< |
1194 | __not_<is_array<_Tp>>, is_array<_Tp1>, |
1195 | is_convertible<typename unique_ptr<_Tp1, _Del>::pointer, _Tp*> |
1196 | >::value, bool>::type = true> |
1197 | __shared_ptr(unique_ptr<_Tp1, _Del>&& __r, __sp_array_delete) |
1198 | : _M_ptr(__r.get()), _M_refcount() |
1199 | { |
1200 | auto __raw = __to_address(__r.get()); |
1201 | _M_refcount = __shared_count<_Lp>(std::move(__r)); |
1202 | _M_enable_shared_from_this_with(__raw); |
1203 | } |
1204 | public: |
1205 | #endif |
1206 | |
1207 | #if _GLIBCXX_USE_DEPRECATED |
1208 | #pragma GCC diagnostic push |
1209 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
1210 | // Postcondition: use_count() == 1 and __r.get() == 0 |
1211 | template<typename _Yp, typename = _Compatible<_Yp>> |
1212 | __shared_ptr(auto_ptr<_Yp>&& __r); |
1213 | #pragma GCC diagnostic pop |
1214 | #endif |
1215 | |
1216 | constexpr __shared_ptr(nullptr_t) noexcept : __shared_ptr() { } |
1217 | |
1218 | template<typename _Yp> |
1219 | _Assignable<_Yp> |
1220 | operator=(const __shared_ptr<_Yp, _Lp>& __r) noexcept |
1221 | { |
1222 | _M_ptr = __r._M_ptr; |
1223 | _M_refcount = __r._M_refcount; // __shared_count::op= doesn't throw |
1224 | return *this; |
1225 | } |
1226 | |
1227 | #if _GLIBCXX_USE_DEPRECATED |
1228 | #pragma GCC diagnostic push |
1229 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
1230 | template<typename _Yp> |
1231 | _Assignable<_Yp> |
1232 | operator=(auto_ptr<_Yp>&& __r) |
1233 | { |
1234 | __shared_ptr(std::move(__r)).swap(*this); |
1235 | return *this; |
1236 | } |
1237 | #pragma GCC diagnostic pop |
1238 | #endif |
1239 | |
1240 | __shared_ptr& |
1241 | operator=(__shared_ptr&& __r) noexcept |
1242 | { |
1243 | __shared_ptr(std::move(__r)).swap(*this); |
1244 | return *this; |
1245 | } |
1246 | |
1247 | template<class _Yp> |
1248 | _Assignable<_Yp> |
1249 | operator=(__shared_ptr<_Yp, _Lp>&& __r) noexcept |
1250 | { |
1251 | __shared_ptr(std::move(__r)).swap(*this); |
1252 | return *this; |
1253 | } |
1254 | |
1255 | template<typename _Yp, typename _Del> |
1256 | _UniqAssignable<_Yp, _Del> |
1257 | operator=(unique_ptr<_Yp, _Del>&& __r) |
1258 | { |
1259 | __shared_ptr(std::move(__r)).swap(*this); |
1260 | return *this; |
1261 | } |
1262 | |
1263 | void |
1264 | reset() noexcept |
1265 | { __shared_ptr().swap(*this); } |
1266 | |
1267 | template<typename _Yp> |
1268 | _SafeConv<_Yp> |
1269 | reset(_Yp* __p) // _Yp must be complete. |
1270 | { |
1271 | // Catch self-reset errors. |
1272 | __glibcxx_assert(__p == 0 || __p != _M_ptr); |
1273 | __shared_ptr(__p).swap(*this); |
1274 | } |
1275 | |
1276 | template<typename _Yp, typename _Deleter> |
1277 | _SafeConv<_Yp> |
1278 | reset(_Yp* __p, _Deleter __d) |
1279 | { __shared_ptr(__p, std::move(__d)).swap(*this); } |
1280 | |
1281 | template<typename _Yp, typename _Deleter, typename _Alloc> |
1282 | _SafeConv<_Yp> |
1283 | reset(_Yp* __p, _Deleter __d, _Alloc __a) |
1284 | { __shared_ptr(__p, std::move(__d), std::move(__a)).swap(*this); } |
1285 | |
1286 | element_type* |
1287 | get() const noexcept |
1288 | { return _M_ptr; } |
1289 | |
1290 | explicit operator bool() const // never throws |
1291 | { return _M_ptr == 0 ? false : true; } |
1292 | |
1293 | bool |
1294 | unique() const noexcept |
1295 | { return _M_refcount._M_unique(); } |
1296 | |
1297 | long |
1298 | use_count() const noexcept |
1299 | { return _M_refcount._M_get_use_count(); } |
1300 | |
1301 | void |
1302 | swap(__shared_ptr<_Tp, _Lp>& __other) noexcept |
1303 | { |
1304 | std::swap(_M_ptr, __other._M_ptr); |
1305 | _M_refcount._M_swap(__other._M_refcount); |
1306 | } |
1307 | |
1308 | template<typename _Tp1> |
1309 | bool |
1310 | owner_before(__shared_ptr<_Tp1, _Lp> const& __rhs) const noexcept |
1311 | { return _M_refcount._M_less(__rhs._M_refcount); } |
1312 | |
1313 | template<typename _Tp1> |
1314 | bool |
1315 | owner_before(__weak_ptr<_Tp1, _Lp> const& __rhs) const noexcept |
1316 | { return _M_refcount._M_less(__rhs._M_refcount); } |
1317 | |
1318 | protected: |
1319 | // This constructor is non-standard, it is used by allocate_shared. |
1320 | template<typename _Alloc, typename... _Args> |
1321 | __shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a, |
1322 | _Args&&... __args) |
1323 | : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a, |
1324 | std::forward<_Args>(__args)...) |
1325 | { |
1326 | // _M_ptr needs to point to the newly constructed object. |
1327 | // This relies on _Sp_counted_ptr_inplace::_M_get_deleter. |
1328 | void* __p = _M_refcount._M_get_deleter(_Sp_make_shared_tag::_S_ti()); |
1329 | _M_ptr = static_cast<_Tp*>(__p); |
1330 | _M_enable_shared_from_this_with(_M_ptr); |
1331 | } |
1332 | |
1333 | template<typename _Tp1, _Lock_policy _Lp1, typename _Alloc, |
1334 | typename... _Args> |
1335 | friend __shared_ptr<_Tp1, _Lp1> |
1336 | __allocate_shared(const _Alloc& __a, _Args&&... __args); |
1337 | |
1338 | // This constructor is used by __weak_ptr::lock() and |
1339 | // shared_ptr::shared_ptr(const weak_ptr&, std::nothrow_t). |
1340 | __shared_ptr(const __weak_ptr<_Tp, _Lp>& __r, std::nothrow_t) |
1341 | : _M_refcount(__r._M_refcount, std::nothrow) |
1342 | { |
1343 | _M_ptr = _M_refcount._M_get_use_count() ? __r._M_ptr : nullptr; |
1344 | } |
1345 | |
1346 | friend class __weak_ptr<_Tp, _Lp>; |
1347 | |
1348 | private: |
1349 | |
1350 | template<typename _Yp> |
1351 | using __esft_base_t = decltype(__enable_shared_from_this_base( |
1352 | std::declval<const __shared_count<_Lp>&>(), |
1353 | std::declval<_Yp*>())); |
1354 | |
1355 | // Detect an accessible and unambiguous enable_shared_from_this base. |
1356 | template<typename _Yp, typename = void> |
1357 | struct __has_esft_base |
1358 | : false_type { }; |
1359 | |
1360 | template<typename _Yp> |
1361 | struct __has_esft_base<_Yp, __void_t<__esft_base_t<_Yp>>> |
1362 | : __not_<is_array<_Tp>> { }; // No enable shared_from_this for arrays |
1363 | |
1364 | template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type> |
1365 | typename enable_if<__has_esft_base<_Yp2>::value>::type |
1366 | _M_enable_shared_from_this_with(_Yp* __p) noexcept |
1367 | { |
1368 | if (auto __base = __enable_shared_from_this_base(_M_refcount, __p)) |
1369 | __base->_M_weak_assign(const_cast<_Yp2*>(__p), _M_refcount); |
1370 | } |
1371 | |
1372 | template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type> |
1373 | typename enable_if<!__has_esft_base<_Yp2>::value>::type |
1374 | _M_enable_shared_from_this_with(_Yp*) noexcept |
1375 | { } |
1376 | |
1377 | void* |
1378 | _M_get_deleter(const std::type_info& __ti) const noexcept |
1379 | { return _M_refcount._M_get_deleter(__ti); } |
1380 | |
1381 | template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr; |
1382 | template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr; |
1383 | |
1384 | template<typename _Del, typename _Tp1, _Lock_policy _Lp1> |
1385 | friend _Del* get_deleter(const __shared_ptr<_Tp1, _Lp1>&) noexcept; |
1386 | |
1387 | template<typename _Del, typename _Tp1> |
1388 | friend _Del* get_deleter(const shared_ptr<_Tp1>&) noexcept; |
1389 | |
1390 | element_type* _M_ptr; // Contained pointer. |
1391 | __shared_count<_Lp> _M_refcount; // Reference counter. |
1392 | }; |
1393 | |
1394 | |
1395 | // 20.7.2.2.7 shared_ptr comparisons |
1396 | template<typename _Tp1, typename _Tp2, _Lock_policy _Lp> |
1397 | inline bool |
1398 | operator==(const __shared_ptr<_Tp1, _Lp>& __a, |
1399 | const __shared_ptr<_Tp2, _Lp>& __b) noexcept |
1400 | { return __a.get() == __b.get(); } |
1401 | |
1402 | template<typename _Tp, _Lock_policy _Lp> |
1403 | inline bool |
1404 | operator==(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept |
1405 | { return !__a; } |
1406 | |
1407 | template<typename _Tp, _Lock_policy _Lp> |
1408 | inline bool |
1409 | operator==(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept |
1410 | { return !__a; } |
1411 | |
1412 | template<typename _Tp1, typename _Tp2, _Lock_policy _Lp> |
1413 | inline bool |
1414 | operator!=(const __shared_ptr<_Tp1, _Lp>& __a, |
1415 | const __shared_ptr<_Tp2, _Lp>& __b) noexcept |
1416 | { return __a.get() != __b.get(); } |
1417 | |
1418 | template<typename _Tp, _Lock_policy _Lp> |
1419 | inline bool |
1420 | operator!=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept |
1421 | { return (bool)__a; } |
1422 | |
1423 | template<typename _Tp, _Lock_policy _Lp> |
1424 | inline bool |
1425 | operator!=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept |
1426 | { return (bool)__a; } |
1427 | |
1428 | template<typename _Tp, typename _Up, _Lock_policy _Lp> |
1429 | inline bool |
1430 | operator<(const __shared_ptr<_Tp, _Lp>& __a, |
1431 | const __shared_ptr<_Up, _Lp>& __b) noexcept |
1432 | { |
1433 | using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type; |
1434 | using _Up_elt = typename __shared_ptr<_Up, _Lp>::element_type; |
1435 | using _Vp = typename common_type<_Tp_elt*, _Up_elt*>::type; |
1436 | return less<_Vp>()(__a.get(), __b.get()); |
1437 | } |
1438 | |
1439 | template<typename _Tp, _Lock_policy _Lp> |
1440 | inline bool |
1441 | operator<(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept |
1442 | { |
1443 | using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type; |
1444 | return less<_Tp_elt*>()(__a.get(), nullptr); |
1445 | } |
1446 | |
1447 | template<typename _Tp, _Lock_policy _Lp> |
1448 | inline bool |
1449 | operator<(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept |
1450 | { |
1451 | using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type; |
1452 | return less<_Tp_elt*>()(nullptr, __a.get()); |
1453 | } |
1454 | |
1455 | template<typename _Tp1, typename _Tp2, _Lock_policy _Lp> |
1456 | inline bool |
1457 | operator<=(const __shared_ptr<_Tp1, _Lp>& __a, |
1458 | const __shared_ptr<_Tp2, _Lp>& __b) noexcept |
1459 | { return !(__b < __a); } |
1460 | |
1461 | template<typename _Tp, _Lock_policy _Lp> |
1462 | inline bool |
1463 | operator<=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept |
1464 | { return !(nullptr < __a); } |
1465 | |
1466 | template<typename _Tp, _Lock_policy _Lp> |
1467 | inline bool |
1468 | operator<=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept |
1469 | { return !(__a < nullptr); } |
1470 | |
1471 | template<typename _Tp1, typename _Tp2, _Lock_policy _Lp> |
1472 | inline bool |
1473 | operator>(const __shared_ptr<_Tp1, _Lp>& __a, |
1474 | const __shared_ptr<_Tp2, _Lp>& __b) noexcept |
1475 | { return (__b < __a); } |
1476 | |
1477 | template<typename _Tp, _Lock_policy _Lp> |
1478 | inline bool |
1479 | operator>(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept |
1480 | { return nullptr < __a; } |
1481 | |
1482 | template<typename _Tp, _Lock_policy _Lp> |
1483 | inline bool |
1484 | operator>(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept |
1485 | { return __a < nullptr; } |
1486 | |
1487 | template<typename _Tp1, typename _Tp2, _Lock_policy _Lp> |
1488 | inline bool |
1489 | operator>=(const __shared_ptr<_Tp1, _Lp>& __a, |
1490 | const __shared_ptr<_Tp2, _Lp>& __b) noexcept |
1491 | { return !(__a < __b); } |
1492 | |
1493 | template<typename _Tp, _Lock_policy _Lp> |
1494 | inline bool |
1495 | operator>=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept |
1496 | { return !(__a < nullptr); } |
1497 | |
1498 | template<typename _Tp, _Lock_policy _Lp> |
1499 | inline bool |
1500 | operator>=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept |
1501 | { return !(nullptr < __a); } |
1502 | |
1503 | template<typename _Sp> |
1504 | struct _Sp_less : public binary_function<_Sp, _Sp, bool> |
1505 | { |
1506 | bool |
1507 | operator()(const _Sp& __lhs, const _Sp& __rhs) const noexcept |
1508 | { |
1509 | typedef typename _Sp::element_type element_type; |
1510 | return std::less<element_type*>()(__lhs.get(), __rhs.get()); |
1511 | } |
1512 | }; |
1513 | |
1514 | template<typename _Tp, _Lock_policy _Lp> |
1515 | struct less<__shared_ptr<_Tp, _Lp>> |
1516 | : public _Sp_less<__shared_ptr<_Tp, _Lp>> |
1517 | { }; |
1518 | |
1519 | // 20.7.2.2.8 shared_ptr specialized algorithms. |
1520 | template<typename _Tp, _Lock_policy _Lp> |
1521 | inline void |
1522 | swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b) noexcept |
1523 | { __a.swap(__b); } |
1524 | |
1525 | // 20.7.2.2.9 shared_ptr casts |
1526 | |
1527 | // The seemingly equivalent code: |
1528 | // shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get())) |
1529 | // will eventually result in undefined behaviour, attempting to |
1530 | // delete the same object twice. |
1531 | /// static_pointer_cast |
1532 | template<typename _Tp, typename _Tp1, _Lock_policy _Lp> |
1533 | inline __shared_ptr<_Tp, _Lp> |
1534 | static_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept |
1535 | { |
1536 | using _Sp = __shared_ptr<_Tp, _Lp>; |
1537 | return _Sp(__r, static_cast<typename _Sp::element_type*>(__r.get())); |
1538 | } |
1539 | |
1540 | // The seemingly equivalent code: |
1541 | // shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get())) |
1542 | // will eventually result in undefined behaviour, attempting to |
1543 | // delete the same object twice. |
1544 | /// const_pointer_cast |
1545 | template<typename _Tp, typename _Tp1, _Lock_policy _Lp> |
1546 | inline __shared_ptr<_Tp, _Lp> |
1547 | const_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept |
1548 | { |
1549 | using _Sp = __shared_ptr<_Tp, _Lp>; |
1550 | return _Sp(__r, const_cast<typename _Sp::element_type*>(__r.get())); |
1551 | } |
1552 | |
1553 | // The seemingly equivalent code: |
1554 | // shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get())) |
1555 | // will eventually result in undefined behaviour, attempting to |
1556 | // delete the same object twice. |
1557 | /// dynamic_pointer_cast |
1558 | template<typename _Tp, typename _Tp1, _Lock_policy _Lp> |
1559 | inline __shared_ptr<_Tp, _Lp> |
1560 | dynamic_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept |
1561 | { |
1562 | using _Sp = __shared_ptr<_Tp, _Lp>; |
1563 | if (auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get())) |
1564 | return _Sp(__r, __p); |
1565 | return _Sp(); |
1566 | } |
1567 | |
1568 | #if __cplusplus > 201402L |
1569 | template<typename _Tp, typename _Tp1, _Lock_policy _Lp> |
1570 | inline __shared_ptr<_Tp, _Lp> |
1571 | reinterpret_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept |
1572 | { |
1573 | using _Sp = __shared_ptr<_Tp, _Lp>; |
1574 | return _Sp(__r, reinterpret_cast<typename _Sp::element_type*>(__r.get())); |
1575 | } |
1576 | #endif |
1577 | |
1578 | template<typename _Tp, _Lock_policy _Lp> |
1579 | class __weak_ptr |
1580 | { |
1581 | template<typename _Yp, typename _Res = void> |
1582 | using _Compatible = typename |
1583 | enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type; |
1584 | |
1585 | // Constraint for assignment from shared_ptr and weak_ptr: |
1586 | template<typename _Yp> |
1587 | using _Assignable = _Compatible<_Yp, __weak_ptr&>; |
1588 | |
1589 | public: |
1590 | using element_type = typename remove_extent<_Tp>::type; |
1591 | |
1592 | constexpr __weak_ptr() noexcept |
1593 | : _M_ptr(nullptr), _M_refcount() |
1594 | { } |
1595 | |
1596 | __weak_ptr(const __weak_ptr&) noexcept = default; |
1597 | |
1598 | ~__weak_ptr() = default; |
1599 | |
1600 | // The "obvious" converting constructor implementation: |
1601 | // |
1602 | // template<typename _Tp1> |
1603 | // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r) |
1604 | // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws |
1605 | // { } |
1606 | // |
1607 | // has a serious problem. |
1608 | // |
1609 | // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr) |
1610 | // conversion may require access to *__r._M_ptr (virtual inheritance). |
1611 | // |
1612 | // It is not possible to avoid spurious access violations since |
1613 | // in multithreaded programs __r._M_ptr may be invalidated at any point. |
1614 | template<typename _Yp, typename = _Compatible<_Yp>> |
1615 | __weak_ptr(const __weak_ptr<_Yp, _Lp>& __r) noexcept |
1616 | : _M_refcount(__r._M_refcount) |
1617 | { _M_ptr = __r.lock().get(); } |
1618 | |
1619 | template<typename _Yp, typename = _Compatible<_Yp>> |
1620 | __weak_ptr(const __shared_ptr<_Yp, _Lp>& __r) noexcept |
1621 | : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) |
1622 | { } |
1623 | |
1624 | __weak_ptr(__weak_ptr&& __r) noexcept |
1625 | : _M_ptr(__r._M_ptr), _M_refcount(std::move(__r._M_refcount)) |
1626 | { __r._M_ptr = nullptr; } |
1627 | |
1628 | template<typename _Yp, typename = _Compatible<_Yp>> |
1629 | __weak_ptr(__weak_ptr<_Yp, _Lp>&& __r) noexcept |
1630 | : _M_ptr(__r.lock().get()), _M_refcount(std::move(__r._M_refcount)) |
1631 | { __r._M_ptr = nullptr; } |
1632 | |
1633 | __weak_ptr& |
1634 | operator=(const __weak_ptr& __r) noexcept = default; |
1635 | |
1636 | template<typename _Yp> |
1637 | _Assignable<_Yp> |
1638 | operator=(const __weak_ptr<_Yp, _Lp>& __r) noexcept |
1639 | { |
1640 | _M_ptr = __r.lock().get(); |
1641 | _M_refcount = __r._M_refcount; |
1642 | return *this; |
1643 | } |
1644 | |
1645 | template<typename _Yp> |
1646 | _Assignable<_Yp> |
1647 | operator=(const __shared_ptr<_Yp, _Lp>& __r) noexcept |
1648 | { |
1649 | _M_ptr = __r._M_ptr; |
1650 | _M_refcount = __r._M_refcount; |
1651 | return *this; |
1652 | } |
1653 | |
1654 | __weak_ptr& |
1655 | operator=(__weak_ptr&& __r) noexcept |
1656 | { |
1657 | _M_ptr = __r._M_ptr; |
1658 | _M_refcount = std::move(__r._M_refcount); |
1659 | __r._M_ptr = nullptr; |
1660 | return *this; |
1661 | } |
1662 | |
1663 | template<typename _Yp> |
1664 | _Assignable<_Yp> |
1665 | operator=(__weak_ptr<_Yp, _Lp>&& __r) noexcept |
1666 | { |
1667 | _M_ptr = __r.lock().get(); |
1668 | _M_refcount = std::move(__r._M_refcount); |
1669 | __r._M_ptr = nullptr; |
1670 | return *this; |
1671 | } |
1672 | |
1673 | __shared_ptr<_Tp, _Lp> |
1674 | lock() const noexcept |
1675 | { return __shared_ptr<element_type, _Lp>(*this, std::nothrow); } |
1676 | |
1677 | long |
1678 | use_count() const noexcept |
1679 | { return _M_refcount._M_get_use_count(); } |
1680 | |
1681 | bool |
1682 | expired() const noexcept |
1683 | { return _M_refcount._M_get_use_count() == 0; } |
1684 | |
1685 | template<typename _Tp1> |
1686 | bool |
1687 | owner_before(const __shared_ptr<_Tp1, _Lp>& __rhs) const noexcept |
1688 | { return _M_refcount._M_less(__rhs._M_refcount); } |
1689 | |
1690 | template<typename _Tp1> |
1691 | bool |
1692 | owner_before(const __weak_ptr<_Tp1, _Lp>& __rhs) const noexcept |
1693 | { return _M_refcount._M_less(__rhs._M_refcount); } |
1694 | |
1695 | void |
1696 | reset() noexcept |
1697 | { __weak_ptr().swap(*this); } |
1698 | |
1699 | void |
1700 | swap(__weak_ptr& __s) noexcept |
1701 | { |
1702 | std::swap(_M_ptr, __s._M_ptr); |
1703 | _M_refcount._M_swap(__s._M_refcount); |
1704 | } |
1705 | |
1706 | private: |
1707 | // Used by __enable_shared_from_this. |
1708 | void |
1709 | _M_assign(_Tp* __ptr, const __shared_count<_Lp>& __refcount) noexcept |
1710 | { |
1711 | if (use_count() == 0) |
1712 | { |
1713 | _M_ptr = __ptr; |
1714 | _M_refcount = __refcount; |
1715 | } |
1716 | } |
1717 | |
1718 | template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr; |
1719 | template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr; |
1720 | friend class __enable_shared_from_this<_Tp, _Lp>; |
1721 | friend class enable_shared_from_this<_Tp>; |
1722 | |
1723 | element_type* _M_ptr; // Contained pointer. |
1724 | __weak_count<_Lp> _M_refcount; // Reference counter. |
1725 | }; |
1726 | |
1727 | // 20.7.2.3.6 weak_ptr specialized algorithms. |
1728 | template<typename _Tp, _Lock_policy _Lp> |
1729 | inline void |
1730 | swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b) noexcept |
1731 | { __a.swap(__b); } |
1732 | |
1733 | template<typename _Tp, typename _Tp1> |
1734 | struct _Sp_owner_less : public binary_function<_Tp, _Tp, bool> |
1735 | { |
1736 | bool |
1737 | operator()(const _Tp& __lhs, const _Tp& __rhs) const noexcept |
1738 | { return __lhs.owner_before(__rhs); } |
1739 | |
1740 | bool |
1741 | operator()(const _Tp& __lhs, const _Tp1& __rhs) const noexcept |
1742 | { return __lhs.owner_before(__rhs); } |
1743 | |
1744 | bool |
1745 | operator()(const _Tp1& __lhs, const _Tp& __rhs) const noexcept |
1746 | { return __lhs.owner_before(__rhs); } |
1747 | }; |
1748 | |
1749 | template<> |
1750 | struct _Sp_owner_less<void, void> |
1751 | { |
1752 | template<typename _Tp, typename _Up> |
1753 | auto |
1754 | operator()(const _Tp& __lhs, const _Up& __rhs) const noexcept |
1755 | -> decltype(__lhs.owner_before(__rhs)) |
1756 | { return __lhs.owner_before(__rhs); } |
1757 | |
1758 | using is_transparent = void; |
1759 | }; |
1760 | |
1761 | template<typename _Tp, _Lock_policy _Lp> |
1762 | struct owner_less<__shared_ptr<_Tp, _Lp>> |
1763 | : public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>> |
1764 | { }; |
1765 | |
1766 | template<typename _Tp, _Lock_policy _Lp> |
1767 | struct owner_less<__weak_ptr<_Tp, _Lp>> |
1768 | : public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>> |
1769 | { }; |
1770 | |
1771 | |
1772 | template<typename _Tp, _Lock_policy _Lp> |
1773 | class __enable_shared_from_this |
1774 | { |
1775 | protected: |
1776 | constexpr __enable_shared_from_this() noexcept { } |
1777 | |
1778 | __enable_shared_from_this(const __enable_shared_from_this&) noexcept { } |
1779 | |
1780 | __enable_shared_from_this& |
1781 | operator=(const __enable_shared_from_this&) noexcept |
1782 | { return *this; } |
1783 | |
1784 | ~__enable_shared_from_this() { } |
1785 | |
1786 | public: |
1787 | __shared_ptr<_Tp, _Lp> |
1788 | shared_from_this() |
1789 | { return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); } |
1790 | |
1791 | __shared_ptr<const _Tp, _Lp> |
1792 | shared_from_this() const |
1793 | { return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); } |
1794 | |
1795 | #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 |
1796 | __weak_ptr<_Tp, _Lp> |
1797 | weak_from_this() noexcept |
1798 | { return this->_M_weak_this; } |
1799 | |
1800 | __weak_ptr<const _Tp, _Lp> |
1801 | weak_from_this() const noexcept |
1802 | { return this->_M_weak_this; } |
1803 | #endif |
1804 | |
1805 | private: |
1806 | template<typename _Tp1> |
1807 | void |
1808 | _M_weak_assign(_Tp1* __p, const __shared_count<_Lp>& __n) const noexcept |
1809 | { _M_weak_this._M_assign(__p, __n); } |
1810 | |
1811 | friend const __enable_shared_from_this* |
1812 | __enable_shared_from_this_base(const __shared_count<_Lp>&, |
1813 | const __enable_shared_from_this* __p) |
1814 | { return __p; } |
1815 | |
1816 | template<typename, _Lock_policy> |
1817 | friend class __shared_ptr; |
1818 | |
1819 | mutable __weak_ptr<_Tp, _Lp> _M_weak_this; |
1820 | }; |
1821 | |
1822 | template<typename _Tp, _Lock_policy _Lp, typename _Alloc, typename... _Args> |
1823 | inline __shared_ptr<_Tp, _Lp> |
1824 | __allocate_shared(const _Alloc& __a, _Args&&... __args) |
1825 | { |
1826 | return __shared_ptr<_Tp, _Lp>(_Sp_make_shared_tag(), __a, |
1827 | std::forward<_Args>(__args)...); |
1828 | } |
1829 | |
1830 | template<typename _Tp, _Lock_policy _Lp, typename... _Args> |
1831 | inline __shared_ptr<_Tp, _Lp> |
1832 | __make_shared(_Args&&... __args) |
1833 | { |
1834 | typedef typename std::remove_const<_Tp>::type _Tp_nc; |
1835 | return std::__allocate_shared<_Tp, _Lp>(std::allocator<_Tp_nc>(), |
1836 | std::forward<_Args>(__args)...); |
1837 | } |
1838 | |
1839 | /// std::hash specialization for __shared_ptr. |
1840 | template<typename _Tp, _Lock_policy _Lp> |
1841 | struct hash<__shared_ptr<_Tp, _Lp>> |
1842 | : public __hash_base<size_t, __shared_ptr<_Tp, _Lp>> |
1843 | { |
1844 | size_t |
1845 | operator()(const __shared_ptr<_Tp, _Lp>& __s) const noexcept |
1846 | { |
1847 | return hash<typename __shared_ptr<_Tp, _Lp>::element_type*>()( |
1848 | __s.get()); |
1849 | } |
1850 | }; |
1851 | |
1852 | _GLIBCXX_END_NAMESPACE_VERSION |
1853 | } // namespace |
1854 | |
1855 | #endif // _SHARED_PTR_BASE_H |
1856 | |