1 // shared_ptr and weak_ptr implementation details -*- C++ -*-
3 // Copyright (C) 2007-2019 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 // GCC Note: Based on files from version 1.32.0 of the Boost library.
28 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
31 // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
32 // Copyright (C) 2001, 2002, 2003 Peter Dimov
35 // Copyright (C) 2001, 2002, 2003 Peter Dimov
37 // enable_shared_from_this.hpp
38 // Copyright (C) 2002 Peter Dimov
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)
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}
49 #ifndef _SHARED_PTR_BASE_H
50 #define _SHARED_PTR_BASE_H 1
53 #include <bits/allocated_ptr.h>
54 #include <bits/refwrap.h>
55 #include <bits/stl_function.h>
56 #include <ext/aligned_buffer.h>
58 namespace std
_GLIBCXX_VISIBILITY(default)
60 _GLIBCXX_BEGIN_NAMESPACE_VERSION
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
70 * @brief Exception possibly thrown by @c shared_ptr.
73 class bad_weak_ptr
: public std::exception
76 virtual char const* what() const noexcept
;
78 virtual ~bad_weak_ptr() noexcept
;
81 // Substitute for bad_weak_ptr object in the case of -fno-exceptions.
83 __throw_bad_weak_ptr()
84 { _GLIBCXX_THROW_OR_ABORT(bad_weak_ptr()); }
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
;
92 // Empty helper class except when the template argument is _S_mutex.
93 template<_Lock_policy _Lp
>
97 // The atomic policy uses fully-fenced builtins, single doesn't care.
98 enum { _S_need_barriers
= 0 };
102 class _Mutex_base
<_S_mutex
>
103 : public __gnu_cxx::__mutex
106 // This policy is used when atomic builtins are not available.
107 // The replacement atomic operations might not have the necessary
109 enum { _S_need_barriers
= 1 };
112 template<_Lock_policy _Lp
= __default_lock_policy
>
113 class _Sp_counted_base
114 : public _Mutex_base
<_Lp
>
117 _Sp_counted_base() noexcept
118 : _M_use_count(1), _M_weak_count(1) { }
121 ~_Sp_counted_base() noexcept
124 // Called when _M_use_count drops to zero, to release the resources
127 _M_dispose() noexcept
= 0;
129 // Called when _M_weak_count drops to zero.
131 _M_destroy() noexcept
135 _M_get_deleter(const std::type_info
&) noexcept
= 0;
139 { __gnu_cxx::__atomic_add_dispatch(&_M_use_count
, 1); }
145 _M_add_ref_lock_nothrow();
148 _M_release() noexcept
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)
154 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count
);
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
)
162 __atomic_thread_fence (__ATOMIC_ACQ_REL
);
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
,
170 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count
);
177 _M_weak_add_ref() noexcept
178 { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count
, 1); }
181 _M_weak_release() noexcept
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)
187 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count
);
188 if (_Mutex_base
<_Lp
>::_S_need_barriers
)
191 // destroy() must observe results of dispose()
192 __atomic_thread_fence (__ATOMIC_ACQ_REL
);
199 _M_get_use_count() const noexcept
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
);
207 _Sp_counted_base(_Sp_counted_base
const&) = delete;
208 _Sp_counted_base
& operator=(_Sp_counted_base
const&) = delete;
210 _Atomic_word _M_use_count
; // #shared
211 _Atomic_word _M_weak_count
; // #weak + (#shared != 0)
216 _Sp_counted_base
<_S_single
>::
219 if (_M_use_count
== 0)
220 __throw_bad_weak_ptr();
226 _Sp_counted_base
<_S_mutex
>::
229 __gnu_cxx::__scoped_lock
sentry(*this);
230 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count
, 1) == 0)
233 __throw_bad_weak_ptr();
239 _Sp_counted_base
<_S_atomic
>::
242 // Perform lock-free add-if-not-zero operation.
243 _Atomic_word __count
= _M_get_use_count();
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.
251 while (!__atomic_compare_exchange_n(&_M_use_count
, &__count
, __count
+ 1,
252 true, __ATOMIC_ACQ_REL
,
258 _Sp_counted_base
<_S_single
>::
259 _M_add_ref_lock_nothrow()
261 if (_M_use_count
== 0)
269 _Sp_counted_base
<_S_mutex
>::
270 _M_add_ref_lock_nothrow()
272 __gnu_cxx::__scoped_lock
sentry(*this);
273 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count
, 1) == 0)
283 _Sp_counted_base
<_S_atomic
>::
284 _M_add_ref_lock_nothrow()
286 // Perform lock-free add-if-not-zero operation.
287 _Atomic_word __count
= _M_get_use_count();
292 // Replace the current counter value with the old value + 1, as
293 // long as it's not changed meanwhile.
295 while (!__atomic_compare_exchange_n(&_M_use_count
, &__count
, __count
+ 1,
296 true, __ATOMIC_ACQ_REL
,
303 _Sp_counted_base
<_S_single
>::_M_add_ref_copy()
308 _Sp_counted_base
<_S_single
>::_M_release() noexcept
310 if (--_M_use_count
== 0)
313 if (--_M_weak_count
== 0)
320 _Sp_counted_base
<_S_single
>::_M_weak_add_ref() noexcept
325 _Sp_counted_base
<_S_single
>::_M_weak_release() noexcept
327 if (--_M_weak_count
== 0)
333 _Sp_counted_base
<_S_single
>::_M_get_use_count() const noexcept
334 { return _M_use_count
; }
337 // Forward declarations.
338 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
341 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
344 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
345 class __enable_shared_from_this
;
347 template<typename _Tp
>
350 template<typename _Tp
>
353 template<typename _Tp
>
356 template<typename _Tp
>
357 class enable_shared_from_this
;
359 template<_Lock_policy _Lp
= __default_lock_policy
>
362 template<_Lock_policy _Lp
= __default_lock_policy
>
363 class __shared_count
;
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
>
372 _Sp_counted_ptr(_Ptr __p
) noexcept
376 _M_dispose() noexcept
380 _M_destroy() noexcept
384 _M_get_deleter(const std::type_info
&) noexcept
387 _Sp_counted_ptr(const _Sp_counted_ptr
&) = delete;
388 _Sp_counted_ptr
& operator=(const _Sp_counted_ptr
&) = delete;
396 _Sp_counted_ptr
<nullptr_t
, _S_single
>::_M_dispose() noexcept
{ }
400 _Sp_counted_ptr
<nullptr_t
, _S_mutex
>::_M_dispose() noexcept
{ }
404 _Sp_counted_ptr
<nullptr_t
, _S_atomic
>::_M_dispose() noexcept
{ }
406 template<int _Nm
, typename _Tp
,
407 bool __use_ebo
= !__is_final(_Tp
) && __is_empty(_Tp
)>
408 struct _Sp_ebo_helper
;
410 /// Specialization using EBO.
411 template<int _Nm
, typename _Tp
>
412 struct _Sp_ebo_helper
<_Nm
, _Tp
, true> : private _Tp
414 explicit _Sp_ebo_helper(const _Tp
& __tp
) : _Tp(__tp
) { }
415 explicit _Sp_ebo_helper(_Tp
&& __tp
) : _Tp(std::move(__tp
)) { }
418 _S_get(_Sp_ebo_helper
& __eboh
) { return static_cast<_Tp
&>(__eboh
); }
421 /// Specialization not using EBO.
422 template<int _Nm
, typename _Tp
>
423 struct _Sp_ebo_helper
<_Nm
, _Tp
, false>
425 explicit _Sp_ebo_helper(const _Tp
& __tp
) : _M_tp(__tp
) { }
426 explicit _Sp_ebo_helper(_Tp
&& __tp
) : _M_tp(std::move(__tp
)) { }
429 _S_get(_Sp_ebo_helper
& __eboh
)
430 { return __eboh
._M_tp
; }
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
>
440 class _Impl
: _Sp_ebo_helper
<0, _Deleter
>, _Sp_ebo_helper
<1, _Alloc
>
442 typedef _Sp_ebo_helper
<0, _Deleter
> _Del_base
;
443 typedef _Sp_ebo_helper
<1, _Alloc
> _Alloc_base
;
446 _Impl(_Ptr __p
, _Deleter __d
, const _Alloc
& __a
) noexcept
447 : _M_ptr(__p
), _Del_base(std::move(__d
)), _Alloc_base(__a
)
450 _Deleter
& _M_del() noexcept
{ return _Del_base::_S_get(*this); }
451 _Alloc
& _M_alloc() noexcept
{ return _Alloc_base::_S_get(*this); }
457 using __allocator_type
= __alloc_rebind
<_Alloc
, _Sp_counted_deleter
>;
459 // __d(__p) must not throw.
460 _Sp_counted_deleter(_Ptr __p
, _Deleter __d
) noexcept
461 : _M_impl(__p
, std::move(__d
), _Alloc()) { }
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
) { }
467 ~_Sp_counted_deleter() noexcept
{ }
470 _M_dispose() noexcept
471 { _M_impl
._M_del()(_M_impl
._M_ptr
); }
474 _M_destroy() noexcept
476 __allocator_type
__a(_M_impl
._M_alloc());
477 __allocated_ptr
<__allocator_type
> __guard_ptr
{ __a
, this };
478 this->~_Sp_counted_deleter();
482 _M_get_deleter(const std::type_info
& __ti
) noexcept
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())
499 // helpers for make_shared / allocate_shared
501 struct _Sp_make_shared_tag
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
;
509 static const type_info
&
510 _S_ti() noexcept
_GLIBCXX_VISIBILITY(default)
512 alignas(type_info
) static constexpr char __tag
[sizeof(type_info
)] = { };
513 return reinterpret_cast<const type_info
&>(__tag
);
517 template<typename _Tp
, typename _Alloc
, _Lock_policy _Lp
>
518 class _Sp_counted_ptr_inplace final
: public _Sp_counted_base
<_Lp
>
520 class _Impl
: _Sp_ebo_helper
<0, _Alloc
>
522 typedef _Sp_ebo_helper
<0, _Alloc
> _A_base
;
525 explicit _Impl(_Alloc __a
) noexcept
: _A_base(__a
) { }
527 _Alloc
& _M_alloc() noexcept
{ return _A_base::_S_get(*this); }
529 __gnu_cxx::__aligned_buffer
<_Tp
> _M_storage
;
533 using __allocator_type
= __alloc_rebind
<_Alloc
, _Sp_counted_ptr_inplace
>;
535 // Alloc parameter is not a reference so doesn't alias anything in __args
536 template<typename
... _Args
>
537 _Sp_counted_ptr_inplace(_Alloc __a
, _Args
&&... __args
)
540 // _GLIBCXX_RESOLVE_LIB_DEFECTS
541 // 2070. allocate_shared should use allocator_traits<A>::construct
542 allocator_traits
<_Alloc
>::construct(__a
, _M_ptr(),
543 std::forward
<_Args
>(__args
)...); // might throw
546 ~_Sp_counted_ptr_inplace() noexcept
{ }
549 _M_dispose() noexcept
551 allocator_traits
<_Alloc
>::destroy(_M_impl
._M_alloc(), _M_ptr());
554 // Override because the allocator needs to know the dynamic type
556 _M_destroy() noexcept
558 __allocator_type
__a(_M_impl
._M_alloc());
559 __allocated_ptr
<__allocator_type
> __guard_ptr
{ __a
, this };
560 this->~_Sp_counted_ptr_inplace();
563 // Sneaky trick so __shared_ptr can get the managed pointer.
565 _M_get_deleter(const std::type_info
& __ti
) noexcept override
567 // Check for the fake type_info first, so we don't try to access it
568 // as a real type_info object.
569 if (&__ti
== &_Sp_make_shared_tag::_S_ti())
570 return const_cast<typename remove_cv
<_Tp
>::type
*>(_M_ptr());
572 // Callers compiled with old libstdc++ headers and RTTI enabled
573 // might pass this instead:
574 else if (__ti
== typeid(_Sp_make_shared_tag
))
575 return const_cast<typename remove_cv
<_Tp
>::type
*>(_M_ptr());
581 _Tp
* _M_ptr() noexcept
{ return _M_impl
._M_storage
._M_ptr(); }
586 // The default deleter for shared_ptr<T[]> and shared_ptr<T[N]>.
587 struct __sp_array_delete
589 template<typename _Yp
>
590 void operator()(_Yp
* __p
) const { delete[] __p
; }
593 template<_Lock_policy _Lp
>
597 constexpr __shared_count() noexcept
: _M_pi(0)
600 template<typename _Ptr
>
602 __shared_count(_Ptr __p
) : _M_pi(0)
606 _M_pi
= new _Sp_counted_ptr
<_Ptr
, _Lp
>(__p
);
611 __throw_exception_again
;
615 template<typename _Ptr
>
616 __shared_count(_Ptr __p
, /* is_array = */ false_type
)
617 : __shared_count(__p
)
620 template<typename _Ptr
>
621 __shared_count(_Ptr __p
, /* is_array = */ true_type
)
622 : __shared_count(__p
, __sp_array_delete
{}, allocator
<void>())
625 template<typename _Ptr
, typename _Deleter
>
626 __shared_count(_Ptr __p
, _Deleter __d
)
627 : __shared_count(__p
, std::move(__d
), allocator
<void>())
630 template<typename _Ptr
, typename _Deleter
, typename _Alloc
>
631 __shared_count(_Ptr __p
, _Deleter __d
, _Alloc __a
) : _M_pi(0)
633 typedef _Sp_counted_deleter
<_Ptr
, _Deleter
, _Alloc
, _Lp
> _Sp_cd_type
;
636 typename
_Sp_cd_type::__allocator_type
__a2(__a
);
637 auto __guard
= std::__allocate_guarded(__a2
);
638 _Sp_cd_type
* __mem
= __guard
.get();
639 ::new (__mem
) _Sp_cd_type(__p
, std::move(__d
), std::move(__a
));
645 __d(__p
); // Call _Deleter on __p.
646 __throw_exception_again
;
650 template<typename _Tp
, typename _Alloc
, typename
... _Args
>
651 __shared_count(_Sp_make_shared_tag
, _Tp
*, const _Alloc
& __a
,
655 typedef _Sp_counted_ptr_inplace
<_Tp
, _Alloc
, _Lp
> _Sp_cp_type
;
656 typename
_Sp_cp_type::__allocator_type
__a2(__a
);
657 auto __guard
= std::__allocate_guarded(__a2
);
658 _Sp_cp_type
* __mem
= __guard
.get();
659 ::new (__mem
) _Sp_cp_type(__a
, std::forward
<_Args
>(__args
)...);
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
>
670 __shared_count(std::auto_ptr
<_Tp
>&& __r
);
671 #pragma GCC diagnostic pop
674 // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee.
675 template<typename _Tp
, typename _Del
>
677 __shared_count(std::unique_ptr
<_Tp
, _Del
>&& __r
) : _M_pi(0)
679 // _GLIBCXX_RESOLVE_LIB_DEFECTS
680 // 2415. Inconsistency between unique_ptr and shared_ptr
681 if (__r
.get() == nullptr)
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
>,
689 = _Sp_counted_deleter
<_Ptr
, _Del2
, allocator
<void>, _Lp
>;
690 using _Alloc
= allocator
<_Sp_cd_type
>;
691 using _Alloc_traits
= allocator_traits
<_Alloc
>;
693 _Sp_cd_type
* __mem
= _Alloc_traits::allocate(__a
, 1);
694 _Alloc_traits::construct(__a
, __mem
, __r
.release(),
695 __r
.get_deleter()); // non-throwing
699 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
700 explicit __shared_count(const __weak_count
<_Lp
>& __r
);
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
);
705 ~__shared_count() noexcept
707 if (_M_pi
!= nullptr)
711 __shared_count(const __shared_count
& __r
) noexcept
715 _M_pi
->_M_add_ref_copy();
719 operator=(const __shared_count
& __r
) noexcept
721 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
725 __tmp
->_M_add_ref_copy();
734 _M_swap(__shared_count
& __r
) noexcept
736 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
742 _M_get_use_count() const noexcept
743 { return _M_pi
!= 0 ? _M_pi
->_M_get_use_count() : 0; }
746 _M_unique() const noexcept
747 { return this->_M_get_use_count() == 1; }
750 _M_get_deleter(const std::type_info
& __ti
) const noexcept
751 { return _M_pi
? _M_pi
->_M_get_deleter(__ti
) : nullptr; }
754 _M_less(const __shared_count
& __rhs
) const noexcept
755 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
758 _M_less(const __weak_count
<_Lp
>& __rhs
) const noexcept
759 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
761 // Friend function injected into enclosing namespace and found by ADL
763 operator==(const __shared_count
& __a
, const __shared_count
& __b
) noexcept
764 { return __a
._M_pi
== __b
._M_pi
; }
767 friend class __weak_count
<_Lp
>;
769 _Sp_counted_base
<_Lp
>* _M_pi
;
773 template<_Lock_policy _Lp
>
777 constexpr __weak_count() noexcept
: _M_pi(nullptr)
780 __weak_count(const __shared_count
<_Lp
>& __r
) noexcept
783 if (_M_pi
!= nullptr)
784 _M_pi
->_M_weak_add_ref();
787 __weak_count(const __weak_count
& __r
) noexcept
790 if (_M_pi
!= nullptr)
791 _M_pi
->_M_weak_add_ref();
794 __weak_count(__weak_count
&& __r
) noexcept
796 { __r
._M_pi
= nullptr; }
798 ~__weak_count() noexcept
800 if (_M_pi
!= nullptr)
801 _M_pi
->_M_weak_release();
805 operator=(const __shared_count
<_Lp
>& __r
) noexcept
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();
817 operator=(const __weak_count
& __r
) noexcept
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();
829 operator=(__weak_count
&& __r
) noexcept
831 if (_M_pi
!= nullptr)
832 _M_pi
->_M_weak_release();
839 _M_swap(__weak_count
& __r
) noexcept
841 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
847 _M_get_use_count() const noexcept
848 { return _M_pi
!= nullptr ? _M_pi
->_M_get_use_count() : 0; }
851 _M_less(const __weak_count
& __rhs
) const noexcept
852 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
855 _M_less(const __shared_count
<_Lp
>& __rhs
) const noexcept
856 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
858 // Friend function injected into enclosing namespace and found by ADL
860 operator==(const __weak_count
& __a
, const __weak_count
& __b
) noexcept
861 { return __a
._M_pi
== __b
._M_pi
; }
864 friend class __shared_count
<_Lp
>;
866 _Sp_counted_base
<_Lp
>* _M_pi
;
869 // Now that __weak_count is defined we can define this constructor:
870 template<_Lock_policy _Lp
>
872 __shared_count
<_Lp
>::__shared_count(const __weak_count
<_Lp
>& __r
)
875 if (_M_pi
!= nullptr)
876 _M_pi
->_M_add_ref_lock();
878 __throw_bad_weak_ptr();
881 // Now that __weak_count is defined we can define this constructor:
882 template<_Lock_policy _Lp
>
884 __shared_count
<_Lp
>::
885 __shared_count(const __weak_count
<_Lp
>& __r
, std::nothrow_t
)
888 if (_M_pi
!= nullptr)
889 if (!_M_pi
->_M_add_ref_lock_nothrow())
893 #define __cpp_lib_shared_ptr_arrays 201603
895 // Helper traits for shared_ptr of array:
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
904 template<typename _Yp
, typename _Tp
>
905 struct __sp_compatible_with
<_Yp
*, _Tp
*>
906 : is_convertible
<_Yp
*, _Tp
*>::type
909 template<typename _Up
, size_t _Nm
>
910 struct __sp_compatible_with
<_Up(*)[_Nm
], _Up(*)[]>
914 template<typename _Up
, size_t _Nm
>
915 struct __sp_compatible_with
<_Up(*)[_Nm
], const _Up(*)[]>
919 template<typename _Up
, size_t _Nm
>
920 struct __sp_compatible_with
<_Up(*)[_Nm
], volatile _Up(*)[]>
924 template<typename _Up
, size_t _Nm
>
925 struct __sp_compatible_with
<_Up(*)[_Nm
], const volatile _Up(*)[]>
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
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
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
946 template<typename _Up
, typename _Yp
>
947 struct __sp_is_constructible_arr
<_Up
, _Yp
, __void_t
<_Yp
[]>>
948 : is_convertible
<_Yp(*)[], _Up(*)[]>::type
951 // Trait to check if shared_ptr<T> can be constructed from Y*.
952 template<typename _Tp
, typename _Yp
>
953 struct __sp_is_constructible
;
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
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
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
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
980 using element_type
= _Tp
;
983 operator*() const noexcept
985 __glibcxx_assert(_M_get() != nullptr);
990 operator->() const noexcept
992 _GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr);
998 _M_get() const noexcept
999 { return static_cast<const __shared_ptr
<_Tp
, _Lp
>*>(this)->get(); }
1002 // Define operator-> for shared_ptr<cv void>.
1003 template<typename _Tp
, _Lock_policy _Lp
>
1004 class __shared_ptr_access
<_Tp
, _Lp
, false, true>
1007 using element_type
= _Tp
;
1010 operator->() const noexcept
1012 auto __ptr
= static_cast<const __shared_ptr
<_Tp
, _Lp
>*>(this)->get();
1013 _GLIBCXX_DEBUG_PEDASSERT(__ptr
!= nullptr);
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>
1023 using element_type
= typename remove_extent
<_Tp
>::type
;
1025 #if __cplusplus <= 201402L
1026 [[__deprecated__("shared_ptr<T[]>::operator* is absent from C++17")]]
1028 operator*() const noexcept
1030 __glibcxx_assert(_M_get() != nullptr);
1034 [[__deprecated__("shared_ptr<T[]>::operator-> is absent from C++17")]]
1036 operator->() const noexcept
1038 _GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr);
1044 operator[](ptrdiff_t __i
) const
1046 __glibcxx_assert(_M_get() != nullptr);
1047 __glibcxx_assert(!extent
<_Tp
>::value
|| __i
< extent
<_Tp
>::value
);
1048 return _M_get()[__i
];
1053 _M_get() const noexcept
1054 { return static_cast<const __shared_ptr
<_Tp
, _Lp
>*>(this)->get(); }
1057 template<typename _Tp
, _Lock_policy _Lp
>
1059 : public __shared_ptr_access
<_Tp
, _Lp
>
1062 using element_type
= typename remove_extent
<_Tp
>::type
;
1065 // Constraint for taking ownership of a pointer of type _Yp*:
1066 template<typename _Yp
>
1068 = typename enable_if
<__sp_is_constructible
<_Tp
, _Yp
>::value
>::type
;
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
;
1075 // Constraint for assignment from shared_ptr and weak_ptr:
1076 template<typename _Yp
>
1077 using _Assignable
= _Compatible
<_Yp
, __shared_ptr
&>;
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
;
1086 // Constraint for assignment from unique_ptr:
1087 template<typename _Yp
, typename _Del
>
1088 using _UniqAssignable
= _UniqCompatible
<_Yp
, _Del
, __shared_ptr
&>;
1092 #if __cplusplus > 201402L
1093 using weak_type
= __weak_ptr
<_Tp
, _Lp
>;
1096 constexpr __shared_ptr() noexcept
1097 : _M_ptr(0), _M_refcount()
1100 template<typename _Yp
, typename
= _SafeConv
<_Yp
>>
1102 __shared_ptr(_Yp
* __p
)
1103 : _M_ptr(__p
), _M_refcount(__p
, typename is_array
<_Tp
>::type())
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
);
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
))
1114 static_assert(__is_invocable
<_Deleter
&, _Yp
*&>::value
,
1115 "deleter expression d(p) is well-formed");
1116 _M_enable_shared_from_this_with(__p
);
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
))
1124 static_assert(__is_invocable
<_Deleter
&, _Yp
*&>::value
,
1125 "deleter expression d(p) is well-formed");
1126 _M_enable_shared_from_this_with(__p
);
1129 template<typename _Deleter
>
1130 __shared_ptr(nullptr_t __p
, _Deleter __d
)
1131 : _M_ptr(0), _M_refcount(__p
, std::move(__d
))
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
))
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
1145 __shared_ptr(const __shared_ptr
&) noexcept
= default;
1146 __shared_ptr
& operator=(const __shared_ptr
&) noexcept
= default;
1147 ~__shared_ptr() = default;
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
)
1154 __shared_ptr(__shared_ptr
&& __r
) noexcept
1155 : _M_ptr(__r
._M_ptr
), _M_refcount()
1157 _M_refcount
._M_swap(__r
._M_refcount
);
1161 template<typename _Yp
, typename
= _Compatible
<_Yp
>>
1162 __shared_ptr(__shared_ptr
<_Yp
, _Lp
>&& __r
) noexcept
1163 : _M_ptr(__r
._M_ptr
), _M_refcount()
1165 _M_refcount
._M_swap(__r
._M_refcount
);
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
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
;
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()
1184 auto __raw
= __to_address(__r
.get());
1185 _M_refcount
= __shared_count
<_Lp
>(std::move(__r
));
1186 _M_enable_shared_from_this_with(__raw
);
1189 #if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED
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()
1200 auto __raw
= __to_address(__r
.get());
1201 _M_refcount
= __shared_count
<_Lp
>(std::move(__r
));
1202 _M_enable_shared_from_this_with(__raw
);
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
1216 constexpr __shared_ptr(nullptr_t
) noexcept
: __shared_ptr() { }
1218 template<typename _Yp
>
1220 operator=(const __shared_ptr
<_Yp
, _Lp
>& __r
) noexcept
1222 _M_ptr
= __r
._M_ptr
;
1223 _M_refcount
= __r
._M_refcount
; // __shared_count::op= doesn't throw
1227 #if _GLIBCXX_USE_DEPRECATED
1228 #pragma GCC diagnostic push
1229 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1230 template<typename _Yp
>
1232 operator=(auto_ptr
<_Yp
>&& __r
)
1234 __shared_ptr(std::move(__r
)).swap(*this);
1237 #pragma GCC diagnostic pop
1241 operator=(__shared_ptr
&& __r
) noexcept
1243 __shared_ptr(std::move(__r
)).swap(*this);
1249 operator=(__shared_ptr
<_Yp
, _Lp
>&& __r
) noexcept
1251 __shared_ptr(std::move(__r
)).swap(*this);
1255 template<typename _Yp
, typename _Del
>
1256 _UniqAssignable
<_Yp
, _Del
>
1257 operator=(unique_ptr
<_Yp
, _Del
>&& __r
)
1259 __shared_ptr(std::move(__r
)).swap(*this);
1265 { __shared_ptr().swap(*this); }
1267 template<typename _Yp
>
1269 reset(_Yp
* __p
) // _Yp must be complete.
1271 // Catch self-reset errors.
1272 __glibcxx_assert(__p
== 0 || __p
!= _M_ptr
);
1273 __shared_ptr(__p
).swap(*this);
1276 template<typename _Yp
, typename _Deleter
>
1278 reset(_Yp
* __p
, _Deleter __d
)
1279 { __shared_ptr(__p
, std::move(__d
)).swap(*this); }
1281 template<typename _Yp
, typename _Deleter
, typename _Alloc
>
1283 reset(_Yp
* __p
, _Deleter __d
, _Alloc __a
)
1284 { __shared_ptr(__p
, std::move(__d
), std::move(__a
)).swap(*this); }
1287 get() const noexcept
1290 explicit operator bool() const // never throws
1291 { return _M_ptr
== 0 ? false : true; }
1294 unique() const noexcept
1295 { return _M_refcount
._M_unique(); }
1298 use_count() const noexcept
1299 { return _M_refcount
._M_get_use_count(); }
1302 swap(__shared_ptr
<_Tp
, _Lp
>& __other
) noexcept
1304 std::swap(_M_ptr
, __other
._M_ptr
);
1305 _M_refcount
._M_swap(__other
._M_refcount
);
1308 template<typename _Tp1
>
1310 owner_before(__shared_ptr
<_Tp1
, _Lp
> const& __rhs
) const noexcept
1311 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1313 template<typename _Tp1
>
1315 owner_before(__weak_ptr
<_Tp1
, _Lp
> const& __rhs
) const noexcept
1316 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
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
,
1323 : _M_ptr(), _M_refcount(__tag
, (_Tp
*)0, __a
,
1324 std::forward
<_Args
>(__args
)...)
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
);
1333 template<typename _Tp1
, _Lock_policy _Lp1
, typename _Alloc
,
1335 friend __shared_ptr
<_Tp1
, _Lp1
>
1336 __allocate_shared(const _Alloc
& __a
, _Args
&&... __args
);
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
)
1343 _M_ptr
= _M_refcount
._M_get_use_count() ? __r
._M_ptr
: nullptr;
1346 friend class __weak_ptr
<_Tp
, _Lp
>;
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
*>()));
1355 // Detect an accessible and unambiguous enable_shared_from_this base.
1356 template<typename _Yp
, typename
= void>
1357 struct __has_esft_base
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
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
1368 if (auto __base
= __enable_shared_from_this_base(_M_refcount
, __p
))
1369 __base
->_M_weak_assign(const_cast<_Yp2
*>(__p
), _M_refcount
);
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
1378 _M_get_deleter(const std::type_info
& __ti
) const noexcept
1379 { return _M_refcount
._M_get_deleter(__ti
); }
1381 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __shared_ptr
;
1382 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __weak_ptr
;
1384 template<typename _Del
, typename _Tp1
, _Lock_policy _Lp1
>
1385 friend _Del
* get_deleter(const __shared_ptr
<_Tp1
, _Lp1
>&) noexcept
;
1387 template<typename _Del
, typename _Tp1
>
1388 friend _Del
* get_deleter(const shared_ptr
<_Tp1
>&) noexcept
;
1390 element_type
* _M_ptr
; // Contained pointer.
1391 __shared_count
<_Lp
> _M_refcount
; // Reference counter.
1395 // 20.7.2.2.7 shared_ptr comparisons
1396 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1398 operator==(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1399 const __shared_ptr
<_Tp2
, _Lp
>& __b
) noexcept
1400 { return __a
.get() == __b
.get(); }
1402 template<typename _Tp
, _Lock_policy _Lp
>
1404 operator==(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
) noexcept
1407 template<typename _Tp
, _Lock_policy _Lp
>
1409 operator==(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __a
) noexcept
1412 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1414 operator!=(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1415 const __shared_ptr
<_Tp2
, _Lp
>& __b
) noexcept
1416 { return __a
.get() != __b
.get(); }
1418 template<typename _Tp
, _Lock_policy _Lp
>
1420 operator!=(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
) noexcept
1421 { return (bool)__a
; }
1423 template<typename _Tp
, _Lock_policy _Lp
>
1425 operator!=(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __a
) noexcept
1426 { return (bool)__a
; }
1428 template<typename _Tp
, typename _Up
, _Lock_policy _Lp
>
1430 operator<(const __shared_ptr
<_Tp
, _Lp
>& __a
,
1431 const __shared_ptr
<_Up
, _Lp
>& __b
) noexcept
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());
1439 template<typename _Tp
, _Lock_policy _Lp
>
1441 operator<(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
) noexcept
1443 using _Tp_elt
= typename __shared_ptr
<_Tp
, _Lp
>::element_type
;
1444 return less
<_Tp_elt
*>()(__a
.get(), nullptr);
1447 template<typename _Tp
, _Lock_policy _Lp
>
1449 operator<(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __a
) noexcept
1451 using _Tp_elt
= typename __shared_ptr
<_Tp
, _Lp
>::element_type
;
1452 return less
<_Tp_elt
*>()(nullptr, __a
.get());
1455 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1457 operator<=(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1458 const __shared_ptr
<_Tp2
, _Lp
>& __b
) noexcept
1459 { return !(__b
< __a
); }
1461 template<typename _Tp
, _Lock_policy _Lp
>
1463 operator<=(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
) noexcept
1464 { return !(nullptr < __a
); }
1466 template<typename _Tp
, _Lock_policy _Lp
>
1468 operator<=(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __a
) noexcept
1469 { return !(__a
< nullptr); }
1471 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1473 operator>(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1474 const __shared_ptr
<_Tp2
, _Lp
>& __b
) noexcept
1475 { return (__b
< __a
); }
1477 template<typename _Tp
, _Lock_policy _Lp
>
1479 operator>(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
) noexcept
1480 { return nullptr < __a
; }
1482 template<typename _Tp
, _Lock_policy _Lp
>
1484 operator>(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __a
) noexcept
1485 { return __a
< nullptr; }
1487 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1489 operator>=(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1490 const __shared_ptr
<_Tp2
, _Lp
>& __b
) noexcept
1491 { return !(__a
< __b
); }
1493 template<typename _Tp
, _Lock_policy _Lp
>
1495 operator>=(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
) noexcept
1496 { return !(__a
< nullptr); }
1498 template<typename _Tp
, _Lock_policy _Lp
>
1500 operator>=(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __a
) noexcept
1501 { return !(nullptr < __a
); }
1503 // 20.7.2.2.8 shared_ptr specialized algorithms.
1504 template<typename _Tp
, _Lock_policy _Lp
>
1506 swap(__shared_ptr
<_Tp
, _Lp
>& __a
, __shared_ptr
<_Tp
, _Lp
>& __b
) noexcept
1509 // 20.7.2.2.9 shared_ptr casts
1511 // The seemingly equivalent code:
1512 // shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))
1513 // will eventually result in undefined behaviour, attempting to
1514 // delete the same object twice.
1515 /// static_pointer_cast
1516 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
1517 inline __shared_ptr
<_Tp
, _Lp
>
1518 static_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
) noexcept
1520 using _Sp
= __shared_ptr
<_Tp
, _Lp
>;
1521 return _Sp(__r
, static_cast<typename
_Sp::element_type
*>(__r
.get()));
1524 // The seemingly equivalent code:
1525 // shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))
1526 // will eventually result in undefined behaviour, attempting to
1527 // delete the same object twice.
1528 /// const_pointer_cast
1529 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
1530 inline __shared_ptr
<_Tp
, _Lp
>
1531 const_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
) noexcept
1533 using _Sp
= __shared_ptr
<_Tp
, _Lp
>;
1534 return _Sp(__r
, const_cast<typename
_Sp::element_type
*>(__r
.get()));
1537 // The seemingly equivalent code:
1538 // shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))
1539 // will eventually result in undefined behaviour, attempting to
1540 // delete the same object twice.
1541 /// dynamic_pointer_cast
1542 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
1543 inline __shared_ptr
<_Tp
, _Lp
>
1544 dynamic_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
) noexcept
1546 using _Sp
= __shared_ptr
<_Tp
, _Lp
>;
1547 if (auto* __p
= dynamic_cast<typename
_Sp::element_type
*>(__r
.get()))
1548 return _Sp(__r
, __p
);
1552 #if __cplusplus > 201402L
1553 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
1554 inline __shared_ptr
<_Tp
, _Lp
>
1555 reinterpret_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
) noexcept
1557 using _Sp
= __shared_ptr
<_Tp
, _Lp
>;
1558 return _Sp(__r
, reinterpret_cast<typename
_Sp::element_type
*>(__r
.get()));
1562 template<typename _Tp
, _Lock_policy _Lp
>
1565 template<typename _Yp
, typename _Res
= void>
1566 using _Compatible
= typename
1567 enable_if
<__sp_compatible_with
<_Yp
*, _Tp
*>::value
, _Res
>::type
;
1569 // Constraint for assignment from shared_ptr and weak_ptr:
1570 template<typename _Yp
>
1571 using _Assignable
= _Compatible
<_Yp
, __weak_ptr
&>;
1574 using element_type
= typename remove_extent
<_Tp
>::type
;
1576 constexpr __weak_ptr() noexcept
1577 : _M_ptr(nullptr), _M_refcount()
1580 __weak_ptr(const __weak_ptr
&) noexcept
= default;
1582 ~__weak_ptr() = default;
1584 // The "obvious" converting constructor implementation:
1586 // template<typename _Tp1>
1587 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
1588 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
1591 // has a serious problem.
1593 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
1594 // conversion may require access to *__r._M_ptr (virtual inheritance).
1596 // It is not possible to avoid spurious access violations since
1597 // in multithreaded programs __r._M_ptr may be invalidated at any point.
1598 template<typename _Yp
, typename
= _Compatible
<_Yp
>>
1599 __weak_ptr(const __weak_ptr
<_Yp
, _Lp
>& __r
) noexcept
1600 : _M_refcount(__r
._M_refcount
)
1601 { _M_ptr
= __r
.lock().get(); }
1603 template<typename _Yp
, typename
= _Compatible
<_Yp
>>
1604 __weak_ptr(const __shared_ptr
<_Yp
, _Lp
>& __r
) noexcept
1605 : _M_ptr(__r
._M_ptr
), _M_refcount(__r
._M_refcount
)
1608 __weak_ptr(__weak_ptr
&& __r
) noexcept
1609 : _M_ptr(__r
._M_ptr
), _M_refcount(std::move(__r
._M_refcount
))
1610 { __r
._M_ptr
= nullptr; }
1612 template<typename _Yp
, typename
= _Compatible
<_Yp
>>
1613 __weak_ptr(__weak_ptr
<_Yp
, _Lp
>&& __r
) noexcept
1614 : _M_ptr(__r
.lock().get()), _M_refcount(std::move(__r
._M_refcount
))
1615 { __r
._M_ptr
= nullptr; }
1618 operator=(const __weak_ptr
& __r
) noexcept
= default;
1620 template<typename _Yp
>
1622 operator=(const __weak_ptr
<_Yp
, _Lp
>& __r
) noexcept
1624 _M_ptr
= __r
.lock().get();
1625 _M_refcount
= __r
._M_refcount
;
1629 template<typename _Yp
>
1631 operator=(const __shared_ptr
<_Yp
, _Lp
>& __r
) noexcept
1633 _M_ptr
= __r
._M_ptr
;
1634 _M_refcount
= __r
._M_refcount
;
1639 operator=(__weak_ptr
&& __r
) noexcept
1641 _M_ptr
= __r
._M_ptr
;
1642 _M_refcount
= std::move(__r
._M_refcount
);
1643 __r
._M_ptr
= nullptr;
1647 template<typename _Yp
>
1649 operator=(__weak_ptr
<_Yp
, _Lp
>&& __r
) noexcept
1651 _M_ptr
= __r
.lock().get();
1652 _M_refcount
= std::move(__r
._M_refcount
);
1653 __r
._M_ptr
= nullptr;
1657 __shared_ptr
<_Tp
, _Lp
>
1658 lock() const noexcept
1659 { return __shared_ptr
<element_type
, _Lp
>(*this, std::nothrow
); }
1662 use_count() const noexcept
1663 { return _M_refcount
._M_get_use_count(); }
1666 expired() const noexcept
1667 { return _M_refcount
._M_get_use_count() == 0; }
1669 template<typename _Tp1
>
1671 owner_before(const __shared_ptr
<_Tp1
, _Lp
>& __rhs
) const noexcept
1672 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1674 template<typename _Tp1
>
1676 owner_before(const __weak_ptr
<_Tp1
, _Lp
>& __rhs
) const noexcept
1677 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1681 { __weak_ptr().swap(*this); }
1684 swap(__weak_ptr
& __s
) noexcept
1686 std::swap(_M_ptr
, __s
._M_ptr
);
1687 _M_refcount
._M_swap(__s
._M_refcount
);
1691 // Used by __enable_shared_from_this.
1693 _M_assign(_Tp
* __ptr
, const __shared_count
<_Lp
>& __refcount
) noexcept
1695 if (use_count() == 0)
1698 _M_refcount
= __refcount
;
1702 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __shared_ptr
;
1703 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __weak_ptr
;
1704 friend class __enable_shared_from_this
<_Tp
, _Lp
>;
1705 friend class enable_shared_from_this
<_Tp
>;
1707 element_type
* _M_ptr
; // Contained pointer.
1708 __weak_count
<_Lp
> _M_refcount
; // Reference counter.
1711 // 20.7.2.3.6 weak_ptr specialized algorithms.
1712 template<typename _Tp
, _Lock_policy _Lp
>
1714 swap(__weak_ptr
<_Tp
, _Lp
>& __a
, __weak_ptr
<_Tp
, _Lp
>& __b
) noexcept
1717 template<typename _Tp
, typename _Tp1
>
1718 struct _Sp_owner_less
: public binary_function
<_Tp
, _Tp
, bool>
1721 operator()(const _Tp
& __lhs
, const _Tp
& __rhs
) const noexcept
1722 { return __lhs
.owner_before(__rhs
); }
1725 operator()(const _Tp
& __lhs
, const _Tp1
& __rhs
) const noexcept
1726 { return __lhs
.owner_before(__rhs
); }
1729 operator()(const _Tp1
& __lhs
, const _Tp
& __rhs
) const noexcept
1730 { return __lhs
.owner_before(__rhs
); }
1734 struct _Sp_owner_less
<void, void>
1736 template<typename _Tp
, typename _Up
>
1738 operator()(const _Tp
& __lhs
, const _Up
& __rhs
) const noexcept
1739 -> decltype(__lhs
.owner_before(__rhs
))
1740 { return __lhs
.owner_before(__rhs
); }
1742 using is_transparent
= void;
1745 template<typename _Tp
, _Lock_policy _Lp
>
1746 struct owner_less
<__shared_ptr
<_Tp
, _Lp
>>
1747 : public _Sp_owner_less
<__shared_ptr
<_Tp
, _Lp
>, __weak_ptr
<_Tp
, _Lp
>>
1750 template<typename _Tp
, _Lock_policy _Lp
>
1751 struct owner_less
<__weak_ptr
<_Tp
, _Lp
>>
1752 : public _Sp_owner_less
<__weak_ptr
<_Tp
, _Lp
>, __shared_ptr
<_Tp
, _Lp
>>
1756 template<typename _Tp
, _Lock_policy _Lp
>
1757 class __enable_shared_from_this
1760 constexpr __enable_shared_from_this() noexcept
{ }
1762 __enable_shared_from_this(const __enable_shared_from_this
&) noexcept
{ }
1764 __enable_shared_from_this
&
1765 operator=(const __enable_shared_from_this
&) noexcept
1768 ~__enable_shared_from_this() { }
1771 __shared_ptr
<_Tp
, _Lp
>
1773 { return __shared_ptr
<_Tp
, _Lp
>(this->_M_weak_this
); }
1775 __shared_ptr
<const _Tp
, _Lp
>
1776 shared_from_this() const
1777 { return __shared_ptr
<const _Tp
, _Lp
>(this->_M_weak_this
); }
1779 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
1780 __weak_ptr
<_Tp
, _Lp
>
1781 weak_from_this() noexcept
1782 { return this->_M_weak_this
; }
1784 __weak_ptr
<const _Tp
, _Lp
>
1785 weak_from_this() const noexcept
1786 { return this->_M_weak_this
; }
1790 template<typename _Tp1
>
1792 _M_weak_assign(_Tp1
* __p
, const __shared_count
<_Lp
>& __n
) const noexcept
1793 { _M_weak_this
._M_assign(__p
, __n
); }
1795 friend const __enable_shared_from_this
*
1796 __enable_shared_from_this_base(const __shared_count
<_Lp
>&,
1797 const __enable_shared_from_this
* __p
)
1800 template<typename
, _Lock_policy
>
1801 friend class __shared_ptr
;
1803 mutable __weak_ptr
<_Tp
, _Lp
> _M_weak_this
;
1806 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
,
1807 typename _Alloc
, typename
... _Args
>
1808 inline __shared_ptr
<_Tp
, _Lp
>
1809 __allocate_shared(const _Alloc
& __a
, _Args
&&... __args
)
1811 return __shared_ptr
<_Tp
, _Lp
>(_Sp_make_shared_tag(), __a
,
1812 std::forward
<_Args
>(__args
)...);
1815 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
,
1817 inline __shared_ptr
<_Tp
, _Lp
>
1818 __make_shared(_Args
&&... __args
)
1820 typedef typename
std::remove_const
<_Tp
>::type _Tp_nc
;
1821 return std::__allocate_shared
<_Tp
, _Lp
>(std::allocator
<_Tp_nc
>(),
1822 std::forward
<_Args
>(__args
)...);
1825 /// std::hash specialization for __shared_ptr.
1826 template<typename _Tp
, _Lock_policy _Lp
>
1827 struct hash
<__shared_ptr
<_Tp
, _Lp
>>
1828 : public __hash_base
<size_t, __shared_ptr
<_Tp
, _Lp
>>
1831 operator()(const __shared_ptr
<_Tp
, _Lp
>& __s
) const noexcept
1833 return hash
<typename __shared_ptr
<_Tp
, _Lp
>::element_type
*>()(
1838 _GLIBCXX_END_NAMESPACE_VERSION
1841 #endif // _SHARED_PTR_BASE_H