49 #ifndef _SHARED_PTR_BASE_H
50 #define _SHARED_PTR_BASE_H 1
63 #if __cplusplus >= 202002L
70 namespace std _GLIBCXX_VISIBILITY(default)
72 _GLIBCXX_BEGIN_NAMESPACE_VERSION
74 #if _GLIBCXX_USE_DEPRECATED
75 #pragma GCC diagnostic push
76 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
77 template<
typename>
class auto_ptr;
78 #pragma GCC diagnostic pop
88 virtual char const*
what() const noexcept;
95 __throw_bad_weak_ptr()
98 using __gnu_cxx::_Lock_policy;
99 using __gnu_cxx::__default_lock_policy;
100 using __gnu_cxx::_S_single;
101 using __gnu_cxx::_S_mutex;
102 using __gnu_cxx::_S_atomic;
105 template<_Lock_policy _Lp>
110 enum { _S_need_barriers = 0 };
114 class _Mutex_base<_S_mutex>
115 :
public __gnu_cxx::__mutex
121 enum { _S_need_barriers = 1 };
124 template<_Lock_policy _Lp = __default_lock_policy>
125 class _Sp_counted_base
126 :
public _Mutex_base<_Lp>
129 _Sp_counted_base() noexcept
130 : _M_use_count(1), _M_weak_count(1) { }
133 ~_Sp_counted_base() noexcept
139 _M_dispose() noexcept = 0;
143 _M_destroy() noexcept
152 { __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); }
158 if (!_M_add_ref_lock_nothrow())
159 __throw_bad_weak_ptr();
164 _M_add_ref_lock_nothrow() noexcept;
168 _M_release() noexcept;
172 _M_release_last_use() noexcept
174 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count);
180 if (_Mutex_base<_Lp>::_S_need_barriers)
182 __atomic_thread_fence (__ATOMIC_ACQ_REL);
186 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
187 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count,
190 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
196 __attribute__((__noinline__))
198 _M_release_last_use_cold() noexcept
199 { _M_release_last_use(); }
203 _M_weak_add_ref() noexcept
204 { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); }
208 _M_weak_release() noexcept
211 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
212 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1)
214 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
215 if (_Mutex_base<_Lp>::_S_need_barriers)
219 __atomic_thread_fence (__ATOMIC_ACQ_REL);
226 _M_get_use_count() const noexcept
230 return __atomic_load_n(&_M_use_count, __ATOMIC_RELAXED);
234 _Sp_counted_base(_Sp_counted_base
const&) =
delete;
235 _Sp_counted_base& operator=(_Sp_counted_base
const&) =
delete;
237 _Atomic_word _M_use_count;
238 _Atomic_word _M_weak_count;
243 _Sp_counted_base<_S_single>::
244 _M_add_ref_lock_nothrow() noexcept
246 if (_M_use_count == 0)
254 _Sp_counted_base<_S_mutex>::
255 _M_add_ref_lock_nothrow() noexcept
258 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
268 _Sp_counted_base<_S_atomic>::
269 _M_add_ref_lock_nothrow() noexcept
272 _Atomic_word __count = _M_get_use_count();
280 while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1,
281 true, __ATOMIC_ACQ_REL,
288 _Sp_counted_base<_S_single>::_M_add_ref_copy()
293 _Sp_counted_base<_S_single>::_M_release() noexcept
295 if (--_M_use_count == 0)
298 if (--_M_weak_count == 0)
305 _Sp_counted_base<_S_mutex>::_M_release() noexcept
308 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count);
309 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
311 _M_release_last_use();
317 _Sp_counted_base<_S_atomic>::_M_release() noexcept
319 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count);
321 constexpr
bool __lock_free
322 = __atomic_always_lock_free(
sizeof(
long long), 0)
323 && __atomic_always_lock_free(
sizeof(_Atomic_word), 0);
324 constexpr
bool __double_word
325 =
sizeof(
long long) == 2 *
sizeof(_Atomic_word);
328 constexpr
bool __aligned = __alignof(
long long) <=
alignof(
void*);
329 if _GLIBCXX17_CONSTEXPR (__lock_free && __double_word && __aligned)
331 constexpr
int __wordbits = __CHAR_BIT__ *
sizeof(_Atomic_word);
332 constexpr
int __shiftbits = __double_word ? __wordbits : 0;
333 constexpr
long long __unique_ref = 1LL + (1LL << __shiftbits);
334 auto __both_counts =
reinterpret_cast<long long*
>(&_M_use_count);
336 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
337 if (__atomic_load_n(__both_counts, __ATOMIC_ACQUIRE) == __unique_ref)
343 *(
long long*)(&_M_use_count) = 0;
344 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count);
345 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
350 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
353 _M_release_last_use_cold();
359 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
361 _M_release_last_use();
367 _Sp_counted_base<_S_single>::_M_weak_add_ref() noexcept
372 _Sp_counted_base<_S_single>::_M_weak_release() noexcept
374 if (--_M_weak_count == 0)
380 _Sp_counted_base<_S_single>::_M_get_use_count() const noexcept
381 {
return _M_use_count; }
385 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
388 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
391 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
392 class __enable_shared_from_this;
394 template<
typename _Tp>
397 template<
typename _Tp>
400 template<
typename _Tp>
403 template<
typename _Tp>
404 class enable_shared_from_this;
406 template<_Lock_policy _Lp = __default_lock_policy>
409 template<_Lock_policy _Lp = __default_lock_policy>
410 class __shared_count;
412 #if __cplusplus >= 202002L
418 template<
typename _Ptr, _Lock_policy _Lp>
419 class _Sp_counted_ptr final :
public _Sp_counted_base<_Lp>
423 _Sp_counted_ptr(_Ptr __p) noexcept
427 _M_dispose() noexcept
431 _M_destroy() noexcept
438 _Sp_counted_ptr(
const _Sp_counted_ptr&) =
delete;
439 _Sp_counted_ptr& operator=(
const _Sp_counted_ptr&) =
delete;
447 _Sp_counted_ptr<nullptr_t, _S_single>::_M_dispose() noexcept { }
451 _Sp_counted_ptr<nullptr_t, _S_mutex>::_M_dispose() noexcept { }
455 _Sp_counted_ptr<nullptr_t, _S_atomic>::_M_dispose() noexcept { }
462 template<
int _Nm,
typename _Tp,
463 bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
464 struct _Sp_ebo_helper;
467 template<
int _Nm,
typename _Tp>
468 struct _Sp_ebo_helper<_Nm, _Tp, true> :
private _Tp
470 explicit _Sp_ebo_helper(
const _Tp& __tp) : _Tp(__tp) { }
471 explicit _Sp_ebo_helper(_Tp&& __tp) : _Tp(
std::move(__tp)) { }
474 _S_get(_Sp_ebo_helper& __eboh) {
return static_cast<_Tp&
>(__eboh); }
478 template<
int _Nm,
typename _Tp>
479 struct _Sp_ebo_helper<_Nm, _Tp, false>
481 explicit _Sp_ebo_helper(
const _Tp& __tp) : _M_tp(__tp) { }
482 explicit _Sp_ebo_helper(_Tp&& __tp) : _M_tp(
std::move(__tp)) { }
485 _S_get(_Sp_ebo_helper& __eboh)
486 {
return __eboh._M_tp; }
493 template<
typename _Ptr,
typename _Deleter,
typename _Alloc, _Lock_policy _Lp>
494 class _Sp_counted_deleter final :
public _Sp_counted_base<_Lp>
496 class _Impl : _Sp_ebo_helper<0, _Deleter>, _Sp_ebo_helper<1, _Alloc>
498 typedef _Sp_ebo_helper<0, _Deleter> _Del_base;
499 typedef _Sp_ebo_helper<1, _Alloc> _Alloc_base;
502 _Impl(_Ptr __p, _Deleter __d,
const _Alloc& __a) noexcept
503 : _Del_base(
std::move(__d)), _Alloc_base(__a), _M_ptr(__p)
506 _Deleter& _M_del() noexcept {
return _Del_base::_S_get(*
this); }
507 _Alloc& _M_alloc() noexcept {
return _Alloc_base::_S_get(*
this); }
513 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_deleter>;
516 _Sp_counted_deleter(_Ptr __p, _Deleter __d) noexcept
517 : _M_impl(__p,
std::move(__d), _Alloc()) { }
520 _Sp_counted_deleter(_Ptr __p, _Deleter __d,
const _Alloc& __a) noexcept
523 ~_Sp_counted_deleter() noexcept { }
526 _M_dispose() noexcept
527 { _M_impl._M_del()(_M_impl._M_ptr); }
530 _M_destroy() noexcept
532 __allocator_type __a(_M_impl._M_alloc());
533 __allocated_ptr<__allocator_type> __guard_ptr{ __a,
this };
534 this->~_Sp_counted_deleter();
538 _M_get_deleter(
const type_info& __ti [[__gnu__::__unused__]]) noexcept
543 return __ti ==
typeid(_Deleter)
557 struct _Sp_make_shared_tag
560 template<
typename _Tp,
typename _Alloc, _Lock_policy _Lp>
561 friend class _Sp_counted_ptr_inplace;
563 static const type_info&
564 _S_ti() noexcept _GLIBCXX_VISIBILITY(default)
566 alignas(type_info)
static constexpr
char __tag[
sizeof(type_info)] = { };
567 return reinterpret_cast<const type_info&
>(__tag);
570 static bool _S_eq(
const type_info&) noexcept;
573 template<
typename _Alloc>
574 struct _Sp_alloc_shared_tag
579 template<
typename _Tp,
typename _Alloc, _Lock_policy _Lp>
580 class _Sp_counted_ptr_inplace final :
public _Sp_counted_base<_Lp>
582 class _Impl : _Sp_ebo_helper<0, _Alloc>
584 typedef _Sp_ebo_helper<0, _Alloc> _A_base;
587 explicit _Impl(_Alloc __a) noexcept : _A_base(__a) { }
589 _Alloc& _M_alloc() noexcept {
return _A_base::_S_get(*
this); }
591 __gnu_cxx::__aligned_buffer<_Tp> _M_storage;
595 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>;
598 template<
typename... _Args>
599 _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
605 std::forward<_Args>(__args)...);
608 ~_Sp_counted_ptr_inplace() noexcept { }
611 _M_dispose() noexcept
618 _M_destroy() noexcept
620 __allocator_type __a(_M_impl._M_alloc());
621 __allocated_ptr<__allocator_type> __guard_ptr{ __a,
this };
622 this->~_Sp_counted_ptr_inplace();
626 friend class __shared_count<_Lp>;
633 auto __ptr =
const_cast<typename remove_cv<_Tp>::type*
>(_M_ptr());
638 if (&__ti == &_Sp_make_shared_tag::_S_ti()
641 __ti ==
typeid(_Sp_make_shared_tag)
643 _Sp_make_shared_tag::_S_eq(__ti)
650 _Tp* _M_ptr() noexcept {
return _M_impl._M_storage._M_ptr(); }
655 #if __cplusplus >= 202002L
656 # define __cpp_lib_smart_ptr_for_overwrite 202002L
657 struct _Sp_overwrite_tag { };
663 template<
typename _Tp,
typename _Alloc, _Lock_policy _Lp>
664 requires is_same_v<typename _Alloc::value_type, _Sp_overwrite_tag>
665 class _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> final
667 template<typename _Tp, template<typename> class _Alloc, _Lock_policy _Lp>
668 class _Sp_counted_ptr_inplace<_Tp, _Alloc<_Sp_overwrite_tag>, _Lp> final
670 :
public _Sp_counted_base<_Lp>
672 [[no_unique_address]] _Alloc _M_alloc;
679 friend class __shared_count<_Lp>;
684 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>;
686 _Sp_counted_ptr_inplace(
const _Alloc& __a)
689 ::new((
void*)_M_ptr()) _Tp;
692 ~_Sp_counted_ptr_inplace() noexcept { }
695 _M_dispose() noexcept
702 _M_destroy() noexcept
705 __allocator_type __a(_M_alloc);
706 auto __p = pointer_traits<pointer>::pointer_to(*
this);
707 __allocated_ptr<__allocator_type> __guard_ptr{ __a, __p };
708 this->~_Sp_counted_ptr_inplace();
717 #if __cplusplus <= 201703L
718 # define __cpp_lib_shared_ptr_arrays 201611L
720 # define __cpp_lib_shared_ptr_arrays 201707L
722 struct _Sp_overwrite_tag;
725 template<
typename _Alloc>
726 struct _Sp_counted_array_base
728 [[no_unique_address]] _Alloc _M_alloc{};
730 bool _M_overwrite =
false;
733 _M_alloc_array(
size_t __tail)
746 template<
typename _Init>
751 using _Tp = remove_pointer_t<_Init>;
754 if constexpr (is_same_v<_Init, _Sp_overwrite_tag>)
759 else if (__init ==
nullptr)
760 std::__uninitialized_default_n_a(__p, _M_n, _M_alloc);
761 else if constexpr (!is_array_v<_Tp>)
762 std::__uninitialized_fill_n_a(__p, _M_n, *__init, _M_alloc);
767 using value_type = _Up;
768 using difference_type = ptrdiff_t;
769 using pointer =
const _Up*;
770 using reference =
const _Up&;
771 using iterator_category = forward_iterator_tag;
777 _Iter& operator++() { ++_M_pos;
return *
this; }
778 _Iter operator++(
int) {
auto __i(*
this); ++_M_pos;
return __i; }
780 reference
operator*()
const {
return _M_p[_M_pos % _M_len]; }
781 pointer operator->()
const {
return _M_p + (_M_pos % _M_len); }
783 bool operator==(
const _Iter& __i)
const
784 {
return _M_pos == __i._M_pos; }
787 _Iter __first{_S_first_elem(__init),
sizeof(_Tp) /
sizeof(_Up)};
788 _Iter __last = __first;
789 __last._M_pos = _M_n;
790 std::__uninitialized_copy_a(__first, __last, __p, _M_alloc);
800 std::destroy_n(__p, _M_n);
810 template<
typename _Tp>
812 _S_first_elem(_Tp* __p) {
return __p; }
814 template<
typename _Tp,
size_t _Nm>
816 _S_first_elem(_Tp (*__p)[_Nm]) {
return _S_first_elem(*__p); }
821 template<
typename _Alloc, _Lock_policy _Lp>
822 class _Sp_counted_array final
823 :
public _Sp_counted_base<_Lp>, _Sp_counted_array_base<_Alloc>
827 pointer _M_alloc_ptr;
831 friend class __shared_count<_Lp>;
834 _Sp_counted_array(
const _Sp_counted_array_base<_Alloc>& __a,
835 pointer __p) noexcept
836 : _Sp_counted_array_base<_Alloc>(__a), _M_alloc_ptr(__p)
839 ~_Sp_counted_array() =
default;
842 _M_dispose() noexcept
845 this->_M_dispose_array(_M_ptr());
850 _M_destroy() noexcept
852 _Sp_counted_array_base<_Alloc> __a = *
this;
853 pointer __p = _M_alloc_ptr;
854 this->~_Sp_counted_array();
855 __a._M_dealloc_array(__p, _S_tail());
860 static constexpr
size_t
867 size_t __bytes =
sizeof(_Sp_counted_array);
870 if constexpr (
alignof(_Tp) <
alignof(_Sp_counted_array))
871 __bytes +=
alignof(_Sp_counted_array) -
alignof(_Tp);
873 return (__bytes +
sizeof(_Tp) - 1) /
sizeof(_Tp);
883 struct __sp_array_delete
885 template<
typename _Yp>
886 void operator()(_Yp* __p)
const {
delete[] __p; }
889 template<_Lock_policy _Lp>
893 template<
typename _Tp>
894 struct __not_alloc_shared_tag {
using type = void; };
896 template<
typename _Tp>
897 struct __not_alloc_shared_tag<_Sp_alloc_shared_tag<_Tp>> { };
899 #if __cpp_lib_shared_ptr_arrays >= 201707L
900 template<
typename _Alloc>
901 struct __not_alloc_shared_tag<_Sp_counted_array_base<_Alloc>> { };
905 constexpr __shared_count() noexcept : _M_pi(0)
908 template<
typename _Ptr>
910 __shared_count(_Ptr __p) : _M_pi(0)
914 _M_pi =
new _Sp_counted_ptr<_Ptr, _Lp>(__p);
919 __throw_exception_again;
923 template<
typename _Ptr>
925 : __shared_count(__p)
928 template<
typename _Ptr>
930 : __shared_count(__p, __sp_array_delete{}, allocator<void>())
933 template<
typename _Ptr,
typename _Deleter,
934 typename =
typename __not_alloc_shared_tag<_Deleter>::type>
935 __shared_count(_Ptr __p, _Deleter __d)
936 : __shared_count(__p,
std::
move(__d), allocator<void>())
939 template<
typename _Ptr,
typename _Deleter,
typename _Alloc,
940 typename =
typename __not_alloc_shared_tag<_Deleter>::type>
941 __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
943 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
946 typename _Sp_cd_type::__allocator_type __a2(__a);
947 auto __guard = std::__allocate_guarded(__a2);
948 _Sp_cd_type* __mem = __guard.get();
956 __throw_exception_again;
960 template<
typename _Tp,
typename _Alloc,
typename... _Args>
961 __shared_count(_Tp*& __p, _Sp_alloc_shared_tag<_Alloc> __a,
964 typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type;
965 typename _Sp_cp_type::__allocator_type __a2(__a._M_a);
966 auto __guard = std::__allocate_guarded(__a2);
967 _Sp_cp_type* __mem = __guard.get();
968 auto __pi = ::new (__mem)
969 _Sp_cp_type(__a._M_a, std::forward<_Args>(__args)...);
972 __p = __pi->_M_ptr();
975 #if __cpp_lib_shared_ptr_arrays >= 201707L
976 template<
typename _Tp,
typename _Alloc,
typename _Init>
977 __shared_count(_Tp*& __p,
const _Sp_counted_array_base<_Alloc>& __a,
980 using _Up = remove_all_extents_t<_Tp>;
981 static_assert(is_same_v<_Up, typename _Alloc::value_type>);
983 using _Sp_ca_type = _Sp_counted_array<_Alloc, _Lp>;
984 const size_t __tail = _Sp_ca_type::_S_tail();
986 struct _Guarded_ptr : _Sp_counted_array_base<_Alloc>
990 _Guarded_ptr(_Sp_counted_array_base<_Alloc> __a)
991 : _Sp_counted_array_base<_Alloc>(__a),
992 _M_ptr(this->_M_alloc_array(_Sp_ca_type::_S_tail()))
998 this->_M_dealloc_array(_M_ptr, _Sp_ca_type::_S_tail());
1002 _Guarded_ptr __guard{__a};
1004 __guard._M_init(__raw, __init);
1006 void* __c = __raw + __a._M_n;
1007 if constexpr (
alignof(_Up) <
alignof(_Sp_ca_type))
1009 size_t __space =
sizeof(_Up) * __tail;
1010 __c =
std::align(
alignof(_Sp_ca_type),
sizeof(_Sp_ca_type),
1013 auto __pi = ::new(__c) _Sp_ca_type(__guard, __guard._M_ptr);
1014 __guard._M_ptr =
nullptr;
1016 __p =
reinterpret_cast<_Tp*
>(__raw);
1020 #if _GLIBCXX_USE_DEPRECATED
1021 #pragma GCC diagnostic push
1022 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1024 template<
typename _Tp>
1027 #pragma GCC diagnostic pop
1031 template<
typename _Tp,
typename _Del>
1037 if (__r.get() ==
nullptr)
1040 using _Ptr =
typename unique_ptr<_Tp, _Del>::pointer;
1041 using _Del2 = __conditional_t<is_reference<_Del>::value,
1042 reference_wrapper<typename remove_reference<_Del>::type>,
1045 = _Sp_counted_deleter<_Ptr, _Del2, allocator<void>, _Lp>;
1046 using _Alloc = allocator<_Sp_cd_type>;
1047 using _Alloc_traits = allocator_traits<_Alloc>;
1049 _Sp_cd_type* __mem = _Alloc_traits::allocate(__a, 1);
1053 _Alloc_traits::construct(__a, __mem, __r.release(),
1054 std::forward<_Del>(__r.get_deleter()));
1059 explicit __shared_count(
const __weak_count<_Lp>& __r);
1063 __shared_count(
const __weak_count<_Lp>& __r, std::nothrow_t) noexcept;
1065 ~__shared_count() noexcept
1067 if (_M_pi !=
nullptr)
1068 _M_pi->_M_release();
1071 __shared_count(
const __shared_count& __r) noexcept
1074 if (_M_pi !=
nullptr)
1075 _M_pi->_M_add_ref_copy();
1079 operator=(
const __shared_count& __r) noexcept
1081 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1084 if (__tmp !=
nullptr)
1085 __tmp->_M_add_ref_copy();
1086 if (_M_pi !=
nullptr)
1087 _M_pi->_M_release();
1094 _M_swap(__shared_count& __r) noexcept
1096 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1102 _M_get_use_count() const noexcept
1103 {
return _M_pi ? _M_pi->_M_get_use_count() : 0; }
1106 _M_unique() const noexcept
1107 {
return this->_M_get_use_count() == 1; }
1111 {
return _M_pi ? _M_pi->_M_get_deleter(__ti) :
nullptr; }
1114 _M_less(
const __shared_count& __rhs)
const noexcept
1118 _M_less(
const __weak_count<_Lp>& __rhs)
const noexcept
1123 operator==(
const __shared_count& __a,
const __shared_count& __b) noexcept
1124 {
return __a._M_pi == __b._M_pi; }
1127 friend class __weak_count<_Lp>;
1128 #if __cplusplus >= 202002L
1129 template<
typename>
friend class _Sp_atomic;
1132 _Sp_counted_base<_Lp>* _M_pi;
1136 template<_Lock_policy _Lp>
1140 constexpr __weak_count() noexcept : _M_pi(
nullptr)
1143 __weak_count(
const __shared_count<_Lp>& __r) noexcept
1146 if (_M_pi !=
nullptr)
1147 _M_pi->_M_weak_add_ref();
1150 __weak_count(
const __weak_count& __r) noexcept
1153 if (_M_pi !=
nullptr)
1154 _M_pi->_M_weak_add_ref();
1157 __weak_count(__weak_count&& __r) noexcept
1159 { __r._M_pi =
nullptr; }
1161 ~__weak_count() noexcept
1163 if (_M_pi !=
nullptr)
1164 _M_pi->_M_weak_release();
1168 operator=(
const __shared_count<_Lp>& __r) noexcept
1170 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1171 if (__tmp !=
nullptr)
1172 __tmp->_M_weak_add_ref();
1173 if (_M_pi !=
nullptr)
1174 _M_pi->_M_weak_release();
1180 operator=(
const __weak_count& __r) noexcept
1182 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1183 if (__tmp !=
nullptr)
1184 __tmp->_M_weak_add_ref();
1185 if (_M_pi !=
nullptr)
1186 _M_pi->_M_weak_release();
1192 operator=(__weak_count&& __r) noexcept
1194 if (_M_pi !=
nullptr)
1195 _M_pi->_M_weak_release();
1197 __r._M_pi =
nullptr;
1202 _M_swap(__weak_count& __r) noexcept
1204 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1210 _M_get_use_count() const noexcept
1211 {
return _M_pi !=
nullptr ? _M_pi->_M_get_use_count() : 0; }
1214 _M_less(
const __weak_count& __rhs)
const noexcept
1218 _M_less(
const __shared_count<_Lp>& __rhs)
const noexcept
1223 operator==(
const __weak_count& __a,
const __weak_count& __b) noexcept
1224 {
return __a._M_pi == __b._M_pi; }
1227 friend class __shared_count<_Lp>;
1228 #if __cplusplus >= 202002L
1229 template<
typename>
friend class _Sp_atomic;
1232 _Sp_counted_base<_Lp>* _M_pi;
1236 template<_Lock_policy _Lp>
1238 __shared_count<_Lp>::__shared_count(
const __weak_count<_Lp>& __r)
1241 if (_M_pi ==
nullptr || !_M_pi->_M_add_ref_lock_nothrow())
1242 __throw_bad_weak_ptr();
1246 template<_Lock_policy _Lp>
1248 __shared_count<_Lp>::
1249 __shared_count(
const __weak_count<_Lp>& __r, std::nothrow_t) noexcept
1252 if (_M_pi && !_M_pi->_M_add_ref_lock_nothrow())
1260 template<
typename _Yp_ptr,
typename _Tp_ptr>
1261 struct __sp_compatible_with
1265 template<
typename _Yp,
typename _Tp>
1266 struct __sp_compatible_with<_Yp*, _Tp*>
1267 : is_convertible<_Yp*, _Tp*>::type
1270 template<
typename _Up,
size_t _Nm>
1271 struct __sp_compatible_with<_Up(*)[_Nm], _Up(*)[]>
1275 template<
typename _Up,
size_t _Nm>
1276 struct __sp_compatible_with<_Up(*)[_Nm],
const _Up(*)[]>
1280 template<
typename _Up,
size_t _Nm>
1281 struct __sp_compatible_with<_Up(*)[_Nm],
volatile _Up(*)[]>
1285 template<
typename _Up,
size_t _Nm>
1286 struct __sp_compatible_with<_Up(*)[_Nm],
const volatile _Up(*)[]>
1291 template<
typename _Up,
size_t _Nm,
typename _Yp,
typename =
void>
1292 struct __sp_is_constructible_arrN
1296 template<
typename _Up,
size_t _Nm,
typename _Yp>
1297 struct __sp_is_constructible_arrN<_Up, _Nm, _Yp, __void_t<_Yp[_Nm]>>
1298 : is_convertible<_Yp(*)[_Nm], _Up(*)[_Nm]>::type
1302 template<
typename _Up,
typename _Yp,
typename =
void>
1303 struct __sp_is_constructible_arr
1307 template<
typename _Up,
typename _Yp>
1308 struct __sp_is_constructible_arr<_Up, _Yp, __void_t<_Yp[]>>
1309 : is_convertible<_Yp(*)[], _Up(*)[]>::type
1313 template<
typename _Tp,
typename _Yp>
1314 struct __sp_is_constructible;
1317 template<
typename _Up,
size_t _Nm,
typename _Yp>
1318 struct __sp_is_constructible<_Up[_Nm], _Yp>
1319 : __sp_is_constructible_arrN<_Up, _Nm, _Yp>::type
1323 template<
typename _Up,
typename _Yp>
1324 struct __sp_is_constructible<_Up[], _Yp>
1325 : __sp_is_constructible_arr<_Up, _Yp>::type
1329 template<
typename _Tp,
typename _Yp>
1330 struct __sp_is_constructible
1331 : is_convertible<_Yp*, _Tp*>::type
1336 template<
typename _Tp, _Lock_policy _Lp,
1337 bool = is_array<_Tp>::value,
bool = is_void<_Tp>::value>
1338 class __shared_ptr_access
1341 using element_type = _Tp;
1346 __glibcxx_assert(_M_get() !=
nullptr);
1351 operator->() const noexcept
1353 _GLIBCXX_DEBUG_PEDASSERT(_M_get() !=
nullptr);
1359 _M_get() const noexcept
1360 {
return static_cast<const __shared_ptr<_Tp, _Lp>*
>(
this)->get(); }
1364 template<
typename _Tp, _Lock_policy _Lp>
1365 class __shared_ptr_access<_Tp, _Lp, false, true>
1368 using element_type = _Tp;
1371 operator->() const noexcept
1373 auto __ptr =
static_cast<const __shared_ptr<_Tp, _Lp>*
>(
this)->get();
1374 _GLIBCXX_DEBUG_PEDASSERT(__ptr !=
nullptr);
1380 template<
typename _Tp, _Lock_policy _Lp>
1381 class __shared_ptr_access<_Tp, _Lp, true, false>
1384 using element_type =
typename remove_extent<_Tp>::type;
1386 #if __cplusplus <= 201402L
1387 [[__deprecated__(
"shared_ptr<T[]>::operator* is absent from C++17")]]
1391 __glibcxx_assert(_M_get() !=
nullptr);
1395 [[__deprecated__(
"shared_ptr<T[]>::operator-> is absent from C++17")]]
1397 operator->() const noexcept
1399 _GLIBCXX_DEBUG_PEDASSERT(_M_get() !=
nullptr);
1405 operator[](ptrdiff_t __i)
const noexcept
1407 __glibcxx_assert(_M_get() !=
nullptr);
1408 __glibcxx_assert(!extent<_Tp>::value || __i < extent<_Tp>::value);
1409 return _M_get()[__i];
1414 _M_get() const noexcept
1415 {
return static_cast<const __shared_ptr<_Tp, _Lp>*
>(
this)->get(); }
1418 template<
typename _Tp, _Lock_policy _Lp>
1420 :
public __shared_ptr_access<_Tp, _Lp>
1423 using element_type =
typename remove_extent<_Tp>::type;
1427 template<
typename _Yp>
1429 =
typename enable_if<__sp_is_constructible<_Tp, _Yp>::value>::type;
1432 template<
typename _Yp,
typename _Res =
void>
1433 using _Compatible =
typename
1434 enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type;
1437 template<
typename _Yp>
1438 using _Assignable = _Compatible<_Yp, __shared_ptr&>;
1441 template<
typename _Yp,
typename _Del,
typename _Res = void,
1442 typename _Ptr =
typename unique_ptr<_Yp, _Del>::pointer>
1443 using _UniqCompatible = __enable_if_t<__and_<
1444 __sp_compatible_with<_Yp*, _Tp*>,
1445 is_convertible<_Ptr, element_type*>,
1446 is_move_constructible<_Del>
1450 template<
typename _Yp,
typename _Del>
1451 using _UniqAssignable = _UniqCompatible<_Yp, _Del, __shared_ptr&>;
1455 #if __cplusplus > 201402L
1456 using weak_type = __weak_ptr<_Tp, _Lp>;
1459 constexpr __shared_ptr() noexcept
1460 : _M_ptr(0), _M_refcount()
1463 template<
typename _Yp,
typename = _SafeConv<_Yp>>
1465 __shared_ptr(_Yp* __p)
1466 : _M_ptr(__p), _M_refcount(__p, typename is_array<_Tp>::type())
1468 static_assert( !is_void<_Yp>::value,
"incomplete type" );
1469 static_assert(
sizeof(_Yp) > 0,
"incomplete type" );
1470 _M_enable_shared_from_this_with(__p);
1473 template<
typename _Yp,
typename _Deleter,
typename = _SafeConv<_Yp>>
1474 __shared_ptr(_Yp* __p, _Deleter __d)
1475 : _M_ptr(__p), _M_refcount(__p,
std::
move(__d))
1477 static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
1478 "deleter expression d(p) is well-formed");
1479 _M_enable_shared_from_this_with(__p);
1482 template<
typename _Yp,
typename _Deleter,
typename _Alloc,
1483 typename = _SafeConv<_Yp>>
1484 __shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a)
1487 static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
1488 "deleter expression d(p) is well-formed");
1489 _M_enable_shared_from_this_with(__p);
1492 template<
typename _Deleter>
1493 __shared_ptr(nullptr_t __p, _Deleter __d)
1494 : _M_ptr(0), _M_refcount(__p,
std::
move(__d))
1497 template<
typename _Deleter,
typename _Alloc>
1498 __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
1503 template<
typename _Yp>
1504 __shared_ptr(
const __shared_ptr<_Yp, _Lp>& __r,
1505 element_type* __p) noexcept
1506 : _M_ptr(__p), _M_refcount(__r._M_refcount)
1510 template<
typename _Yp>
1511 __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r,
1512 element_type* __p) noexcept
1513 : _M_ptr(__p), _M_refcount()
1515 _M_refcount._M_swap(__r._M_refcount);
1516 __r._M_ptr =
nullptr;
1519 __shared_ptr(
const __shared_ptr&) noexcept =
default;
1520 __shared_ptr& operator=(
const __shared_ptr&) noexcept =
default;
1521 ~__shared_ptr() =
default;
1523 template<
typename _Yp,
typename = _Compatible<_Yp>>
1524 __shared_ptr(
const __shared_ptr<_Yp, _Lp>& __r) noexcept
1525 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1528 __shared_ptr(__shared_ptr&& __r) noexcept
1529 : _M_ptr(__r._M_ptr), _M_refcount()
1531 _M_refcount._M_swap(__r._M_refcount);
1532 __r._M_ptr =
nullptr;
1535 template<
typename _Yp,
typename = _Compatible<_Yp>>
1536 __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r) noexcept
1537 : _M_ptr(__r._M_ptr), _M_refcount()
1539 _M_refcount._M_swap(__r._M_refcount);
1540 __r._M_ptr =
nullptr;
1543 template<
typename _Yp,
typename = _Compatible<_Yp>>
1544 explicit __shared_ptr(
const __weak_ptr<_Yp, _Lp>& __r)
1545 : _M_refcount(__r._M_refcount)
1549 _M_ptr = __r._M_ptr;
1553 template<
typename _Yp,
typename _Del,
1554 typename = _UniqCompatible<_Yp, _Del>>
1555 __shared_ptr(unique_ptr<_Yp, _Del>&& __r)
1556 : _M_ptr(__r.get()), _M_refcount()
1558 auto __raw = __to_address(__r.get());
1559 _M_refcount = __shared_count<_Lp>(
std::move(__r));
1560 _M_enable_shared_from_this_with(__raw);
1563 #if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED
1566 template<
typename _Tp1,
typename _Del,
1567 typename enable_if<__and_<
1568 __not_<is_array<_Tp>>, is_array<_Tp1>,
1569 is_convertible<typename unique_ptr<_Tp1, _Del>::pointer, _Tp*>
1570 >::value,
bool>::type =
true>
1571 __shared_ptr(unique_ptr<_Tp1, _Del>&& __r, __sp_array_delete)
1572 : _M_ptr(__r.get()), _M_refcount()
1574 auto __raw = __to_address(__r.get());
1575 _M_refcount = __shared_count<_Lp>(
std::move(__r));
1576 _M_enable_shared_from_this_with(__raw);
1581 #if _GLIBCXX_USE_DEPRECATED
1582 #pragma GCC diagnostic push
1583 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1585 template<
typename _Yp,
typename = _Compatible<_Yp>>
1586 __shared_ptr(auto_ptr<_Yp>&& __r);
1587 #pragma GCC diagnostic pop
1590 constexpr __shared_ptr(nullptr_t) noexcept : __shared_ptr() { }
1592 template<
typename _Yp>
1594 operator=(
const __shared_ptr<_Yp, _Lp>& __r) noexcept
1596 _M_ptr = __r._M_ptr;
1597 _M_refcount = __r._M_refcount;
1601 #if _GLIBCXX_USE_DEPRECATED
1602 #pragma GCC diagnostic push
1603 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1604 template<
typename _Yp>
1606 operator=(auto_ptr<_Yp>&& __r)
1608 __shared_ptr(
std::move(__r)).swap(*
this);
1611 #pragma GCC diagnostic pop
1615 operator=(__shared_ptr&& __r) noexcept
1617 __shared_ptr(
std::move(__r)).swap(*
this);
1623 operator=(__shared_ptr<_Yp, _Lp>&& __r) noexcept
1625 __shared_ptr(
std::move(__r)).swap(*
this);
1629 template<
typename _Yp,
typename _Del>
1630 _UniqAssignable<_Yp, _Del>
1631 operator=(unique_ptr<_Yp, _Del>&& __r)
1633 __shared_ptr(
std::move(__r)).swap(*
this);
1639 { __shared_ptr().swap(*
this); }
1641 template<
typename _Yp>
1646 __glibcxx_assert(__p ==
nullptr || __p != _M_ptr);
1647 __shared_ptr(__p).swap(*
this);
1650 template<
typename _Yp,
typename _Deleter>
1652 reset(_Yp* __p, _Deleter __d)
1653 { __shared_ptr(__p,
std::move(__d)).swap(*
this); }
1655 template<
typename _Yp,
typename _Deleter,
typename _Alloc>
1657 reset(_Yp* __p, _Deleter __d, _Alloc __a)
1662 get() const noexcept
1666 explicit operator bool() const noexcept
1667 {
return _M_ptr !=
nullptr; }
1671 unique() const noexcept
1672 {
return _M_refcount._M_unique(); }
1676 use_count() const noexcept
1677 {
return _M_refcount._M_get_use_count(); }
1681 swap(__shared_ptr<_Tp, _Lp>& __other) noexcept
1684 _M_refcount._M_swap(__other._M_refcount);
1694 template<
typename _Tp1>
1696 owner_before(__shared_ptr<_Tp1, _Lp>
const& __rhs)
const noexcept
1697 {
return _M_refcount._M_less(__rhs._M_refcount); }
1699 template<
typename _Tp1>
1701 owner_before(__weak_ptr<_Tp1, _Lp>
const& __rhs)
const noexcept
1702 {
return _M_refcount._M_less(__rhs._M_refcount); }
1707 template<
typename _Alloc,
typename... _Args>
1708 __shared_ptr(_Sp_alloc_shared_tag<_Alloc> __tag, _Args&&... __args)
1709 : _M_ptr(), _M_refcount(_M_ptr, __tag,
std::
forward<_Args>(__args)...)
1710 { _M_enable_shared_from_this_with(_M_ptr); }
1712 template<
typename _Tp1, _Lock_policy _Lp1,
typename _Alloc,
1714 friend __shared_ptr<_Tp1, _Lp1>
1715 __allocate_shared(
const _Alloc& __a, _Args&&... __args);
1717 #if __cpp_lib_shared_ptr_arrays >= 201707L
1719 template<
typename _Alloc,
typename _Init = const remove_extent_t<_Tp>*>
1720 __shared_ptr(
const _Sp_counted_array_base<_Alloc>& __a,
1721 _Init __init =
nullptr)
1722 : _M_ptr(), _M_refcount(_M_ptr, __a, __init)
1728 __shared_ptr(
const __weak_ptr<_Tp, _Lp>& __r, std::nothrow_t) noexcept
1729 : _M_refcount(__r._M_refcount, std::nothrow)
1731 _M_ptr = _M_refcount._M_get_use_count() ? __r._M_ptr :
nullptr;
1734 friend class __weak_ptr<_Tp, _Lp>;
1738 template<
typename _Yp>
1739 using __esft_base_t = decltype(__enable_shared_from_this_base(
1741 std::declval<_Yp*>()));
1744 template<
typename _Yp,
typename =
void>
1745 struct __has_esft_base
1748 template<
typename _Yp>
1749 struct __has_esft_base<_Yp, __void_t<__esft_base_t<_Yp>>>
1750 : __not_<is_array<_Tp>> { };
1752 template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
1753 typename enable_if<__has_esft_base<_Yp2>::value>::type
1754 _M_enable_shared_from_this_with(_Yp* __p) noexcept
1756 if (
auto __base = __enable_shared_from_this_base(_M_refcount, __p))
1757 __base->_M_weak_assign(
const_cast<_Yp2*
>(__p), _M_refcount);
1760 template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
1761 typename enable_if<!__has_esft_base<_Yp2>::value>::type
1762 _M_enable_shared_from_this_with(_Yp*) noexcept
1767 {
return _M_refcount._M_get_deleter(__ti); }
1769 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
1770 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
1772 template<
typename _Del,
typename _Tp1, _Lock_policy _Lp1>
1773 friend _Del* get_deleter(
const __shared_ptr<_Tp1, _Lp1>&) noexcept;
1775 template<
typename _Del,
typename _Tp1>
1776 friend _Del* get_deleter(
const shared_ptr<_Tp1>&) noexcept;
1778 #if __cplusplus >= 202002L
1779 friend _Sp_atomic<shared_ptr<_Tp>>;
1782 element_type* _M_ptr;
1783 __shared_count<_Lp> _M_refcount;
1788 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1790 operator==(
const __shared_ptr<_Tp1, _Lp>& __a,
1791 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1792 {
return __a.get() == __b.get(); }
1794 template<
typename _Tp, _Lock_policy _Lp>
1796 operator==(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1799 #ifdef __cpp_lib_three_way_comparison
1800 template<
typename _Tp,
typename _Up, _Lock_policy _Lp>
1801 inline strong_ordering
1802 operator<=>(
const __shared_ptr<_Tp, _Lp>& __a,
1803 const __shared_ptr<_Up, _Lp>& __b) noexcept
1804 {
return compare_three_way()(__a.get(), __b.get()); }
1806 template<
typename _Tp, _Lock_policy _Lp>
1807 inline strong_ordering
1808 operator<=>(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1810 using pointer =
typename __shared_ptr<_Tp, _Lp>::element_type*;
1811 return compare_three_way()(__a.get(),
static_cast<pointer
>(
nullptr));
1814 template<
typename _Tp, _Lock_policy _Lp>
1816 operator==(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1819 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1821 operator!=(
const __shared_ptr<_Tp1, _Lp>& __a,
1822 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1823 {
return __a.get() != __b.get(); }
1825 template<
typename _Tp, _Lock_policy _Lp>
1827 operator!=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1828 {
return (
bool)__a; }
1830 template<
typename _Tp, _Lock_policy _Lp>
1832 operator!=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1833 {
return (
bool)__a; }
1835 template<
typename _Tp,
typename _Up, _Lock_policy _Lp>
1837 operator<(
const __shared_ptr<_Tp, _Lp>& __a,
1838 const __shared_ptr<_Up, _Lp>& __b) noexcept
1840 using _Tp_elt =
typename __shared_ptr<_Tp, _Lp>::element_type;
1841 using _Up_elt =
typename __shared_ptr<_Up, _Lp>::element_type;
1842 using _Vp =
typename common_type<_Tp_elt*, _Up_elt*>::type;
1843 return less<_Vp>()(__a.get(), __b.get());
1846 template<
typename _Tp, _Lock_policy _Lp>
1848 operator<(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1850 using _Tp_elt =
typename __shared_ptr<_Tp, _Lp>::element_type;
1851 return less<_Tp_elt*>()(__a.get(),
nullptr);
1854 template<
typename _Tp, _Lock_policy _Lp>
1856 operator<(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1858 using _Tp_elt =
typename __shared_ptr<_Tp, _Lp>::element_type;
1859 return less<_Tp_elt*>()(
nullptr, __a.get());
1862 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1864 operator<=(
const __shared_ptr<_Tp1, _Lp>& __a,
1865 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1866 {
return !(__b < __a); }
1868 template<
typename _Tp, _Lock_policy _Lp>
1870 operator<=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1871 {
return !(
nullptr < __a); }
1873 template<
typename _Tp, _Lock_policy _Lp>
1875 operator<=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1876 {
return !(__a <
nullptr); }
1878 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1880 operator>(
const __shared_ptr<_Tp1, _Lp>& __a,
1881 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1882 {
return (__b < __a); }
1884 template<
typename _Tp, _Lock_policy _Lp>
1886 operator>(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1887 {
return nullptr < __a; }
1889 template<
typename _Tp, _Lock_policy _Lp>
1891 operator>(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1892 {
return __a <
nullptr; }
1894 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1896 operator>=(
const __shared_ptr<_Tp1, _Lp>& __a,
1897 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1898 {
return !(__a < __b); }
1900 template<
typename _Tp, _Lock_policy _Lp>
1902 operator>=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1903 {
return !(__a <
nullptr); }
1905 template<
typename _Tp, _Lock_policy _Lp>
1907 operator>=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1908 {
return !(
nullptr < __a); }
1912 template<
typename _Tp, _Lock_policy _Lp>
1914 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b) noexcept
1924 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1925 inline __shared_ptr<_Tp, _Lp>
1928 using _Sp = __shared_ptr<_Tp, _Lp>;
1929 return _Sp(__r,
static_cast<typename _Sp::element_type*
>(__r.get()));
1937 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1938 inline __shared_ptr<_Tp, _Lp>
1941 using _Sp = __shared_ptr<_Tp, _Lp>;
1942 return _Sp(__r,
const_cast<typename _Sp::element_type*
>(__r.get()));
1950 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1951 inline __shared_ptr<_Tp, _Lp>
1954 using _Sp = __shared_ptr<_Tp, _Lp>;
1955 if (
auto* __p =
dynamic_cast<typename _Sp::element_type*
>(__r.get()))
1956 return _Sp(__r, __p);
1960 #if __cplusplus > 201402L
1961 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1962 inline __shared_ptr<_Tp, _Lp>
1963 reinterpret_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1965 using _Sp = __shared_ptr<_Tp, _Lp>;
1966 return _Sp(__r,
reinterpret_cast<typename _Sp::element_type*
>(__r.get()));
1970 template<
typename _Tp, _Lock_policy _Lp>
1973 template<
typename _Yp,
typename _Res =
void>
1974 using _Compatible =
typename
1975 enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type;
1978 template<
typename _Yp>
1979 using _Assignable = _Compatible<_Yp, __weak_ptr&>;
1982 using element_type =
typename remove_extent<_Tp>::type;
1984 constexpr __weak_ptr() noexcept
1985 : _M_ptr(
nullptr), _M_refcount()
1988 __weak_ptr(
const __weak_ptr&) noexcept =
default;
1990 ~__weak_ptr() =
default;
2006 template<
typename _Yp,
typename = _Compatible<_Yp>>
2007 __weak_ptr(
const __weak_ptr<_Yp, _Lp>& __r) noexcept
2008 : _M_refcount(__r._M_refcount)
2009 { _M_ptr = __r.lock().get(); }
2011 template<
typename _Yp,
typename = _Compatible<_Yp>>
2012 __weak_ptr(
const __shared_ptr<_Yp, _Lp>& __r) noexcept
2013 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
2016 __weak_ptr(__weak_ptr&& __r) noexcept
2017 : _M_ptr(__r._M_ptr), _M_refcount(
std::move(__r._M_refcount))
2018 { __r._M_ptr =
nullptr; }
2020 template<
typename _Yp,
typename = _Compatible<_Yp>>
2021 __weak_ptr(__weak_ptr<_Yp, _Lp>&& __r) noexcept
2022 : _M_ptr(__r.lock().get()), _M_refcount(
std::move(__r._M_refcount))
2023 { __r._M_ptr =
nullptr; }
2026 operator=(
const __weak_ptr& __r) noexcept =
default;
2028 template<
typename _Yp>
2030 operator=(
const __weak_ptr<_Yp, _Lp>& __r) noexcept
2032 _M_ptr = __r.lock().get();
2033 _M_refcount = __r._M_refcount;
2037 template<
typename _Yp>
2039 operator=(
const __shared_ptr<_Yp, _Lp>& __r) noexcept
2041 _M_ptr = __r._M_ptr;
2042 _M_refcount = __r._M_refcount;
2047 operator=(__weak_ptr&& __r) noexcept
2049 _M_ptr = __r._M_ptr;
2050 _M_refcount =
std::move(__r._M_refcount);
2051 __r._M_ptr =
nullptr;
2055 template<
typename _Yp>
2057 operator=(__weak_ptr<_Yp, _Lp>&& __r) noexcept
2059 _M_ptr = __r.lock().get();
2060 _M_refcount =
std::move(__r._M_refcount);
2061 __r._M_ptr =
nullptr;
2065 __shared_ptr<_Tp, _Lp>
2066 lock() const noexcept
2067 {
return __shared_ptr<element_type, _Lp>(*
this, std::nothrow); }
2070 use_count() const noexcept
2071 {
return _M_refcount._M_get_use_count(); }
2074 expired() const noexcept
2075 {
return _M_refcount._M_get_use_count() == 0; }
2077 template<
typename _Tp1>
2079 owner_before(
const __shared_ptr<_Tp1, _Lp>& __rhs)
const noexcept
2080 {
return _M_refcount._M_less(__rhs._M_refcount); }
2082 template<
typename _Tp1>
2084 owner_before(
const __weak_ptr<_Tp1, _Lp>& __rhs)
const noexcept
2085 {
return _M_refcount._M_less(__rhs._M_refcount); }
2089 { __weak_ptr().swap(*
this); }
2092 swap(__weak_ptr& __s) noexcept
2095 _M_refcount._M_swap(__s._M_refcount);
2101 _M_assign(_Tp* __ptr,
const __shared_count<_Lp>& __refcount) noexcept
2103 if (use_count() == 0)
2106 _M_refcount = __refcount;
2110 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
2111 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
2112 friend class __enable_shared_from_this<_Tp, _Lp>;
2113 friend class enable_shared_from_this<_Tp>;
2114 #if __cplusplus >= 202002L
2115 friend _Sp_atomic<weak_ptr<_Tp>>;
2118 element_type* _M_ptr;
2119 __weak_count<_Lp> _M_refcount;
2123 template<
typename _Tp, _Lock_policy _Lp>
2125 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b) noexcept
2128 #pragma GCC diagnostic push
2129 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
2130 template<
typename _Tp,
typename _Tp1>
2131 struct _Sp_owner_less :
public binary_function<_Tp, _Tp, bool>
2134 operator()(
const _Tp& __lhs,
const _Tp& __rhs)
const noexcept
2135 {
return __lhs.owner_before(__rhs); }
2138 operator()(
const _Tp& __lhs,
const _Tp1& __rhs)
const noexcept
2139 {
return __lhs.owner_before(__rhs); }
2142 operator()(
const _Tp1& __lhs,
const _Tp& __rhs)
const noexcept
2143 {
return __lhs.owner_before(__rhs); }
2145 #pragma GCC diagnostic pop
2148 struct _Sp_owner_less<void, void>
2150 template<
typename _Tp,
typename _Up>
2152 operator()(
const _Tp& __lhs,
const _Up& __rhs)
const noexcept
2153 -> decltype(__lhs.owner_before(__rhs))
2154 {
return __lhs.owner_before(__rhs); }
2156 using is_transparent = void;
2159 template<
typename _Tp, _Lock_policy _Lp>
2160 struct owner_less<__shared_ptr<_Tp, _Lp>>
2161 :
public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
2164 template<
typename _Tp, _Lock_policy _Lp>
2165 struct owner_less<__weak_ptr<_Tp, _Lp>>
2166 :
public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
2170 template<
typename _Tp, _Lock_policy _Lp>
2171 class __enable_shared_from_this
2174 constexpr __enable_shared_from_this() noexcept { }
2176 __enable_shared_from_this(
const __enable_shared_from_this&) noexcept { }
2178 __enable_shared_from_this&
2179 operator=(
const __enable_shared_from_this&) noexcept
2182 ~__enable_shared_from_this() { }
2185 __shared_ptr<_Tp, _Lp>
2187 {
return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
2189 __shared_ptr<const _Tp, _Lp>
2190 shared_from_this()
const
2191 {
return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
2193 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__)
2194 __weak_ptr<_Tp, _Lp>
2195 weak_from_this() noexcept
2196 {
return this->_M_weak_this; }
2198 __weak_ptr<const _Tp, _Lp>
2199 weak_from_this() const noexcept
2200 {
return this->_M_weak_this; }
2204 template<
typename _Tp1>
2206 _M_weak_assign(_Tp1* __p,
const __shared_count<_Lp>& __n)
const noexcept
2207 { _M_weak_this._M_assign(__p, __n); }
2209 friend const __enable_shared_from_this*
2210 __enable_shared_from_this_base(
const __shared_count<_Lp>&,
2211 const __enable_shared_from_this* __p)
2214 template<
typename, _Lock_policy>
2215 friend class __shared_ptr;
2217 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
2220 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy,
2221 typename _Alloc,
typename... _Args>
2222 inline __shared_ptr<_Tp, _Lp>
2223 __allocate_shared(
const _Alloc& __a, _Args&&... __args)
2225 static_assert(!is_array<_Tp>::value,
"make_shared<T[]> not supported");
2227 return __shared_ptr<_Tp, _Lp>(_Sp_alloc_shared_tag<_Alloc>{__a},
2228 std::forward<_Args>(__args)...);
2231 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy,
2233 inline __shared_ptr<_Tp, _Lp>
2234 __make_shared(_Args&&... __args)
2236 typedef typename std::remove_const<_Tp>::type _Tp_nc;
2238 std::forward<_Args>(__args)...);
2242 template<
typename _Tp, _Lock_policy _Lp>
2243 struct hash<__shared_ptr<_Tp, _Lp>>
2244 :
public __hash_base<size_t, __shared_ptr<_Tp, _Lp>>
2247 operator()(
const __shared_ptr<_Tp, _Lp>& __s)
const noexcept
2254 _GLIBCXX_END_NAMESPACE_VERSION
constexpr complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __count)
Default-initializes objects in the range [first,first+count).
void * align(size_t __align, size_t __size, void *&__ptr, size_t &__space) noexcept
Fit aligned storage in buffer.
constexpr _Tp * to_address(_Tp *__ptr) noexcept
Obtain address referenced by a pointer to an object.
integral_constant< bool, true > true_type
The type used as a compile-time boolean with true value.
integral_constant< bool, false > false_type
The type used as a compile-time boolean with false value.
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
auto declval() noexcept -> decltype(__declval< _Tp >(0))
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
void swap(any &__x, any &__y) noexcept
Exchange the states of two any objects.
void lock(_L1 &__l1, _L2 &__l2, _L3 &... __l3)
Generic lock.
ISO C++ entities toplevel namespace is std.
__shared_ptr< _Tp, _Lp > static_pointer_cast(const __shared_ptr< _Tp1, _Lp > &__r) noexcept
static_pointer_cast
__shared_ptr< _Tp, _Lp > const_pointer_cast(const __shared_ptr< _Tp1, _Lp > &__r) noexcept
const_pointer_cast
__shared_ptr< _Tp, _Lp > dynamic_pointer_cast(const __shared_ptr< _Tp1, _Lp > &__r) noexcept
dynamic_pointer_cast
constexpr _Iterator __base(_Iterator __it)
Primary class template hash.
static constexpr auto construct(_Alloc &__a, _Tp *__p, _Args &&... __args) noexcept(noexcept(_S_construct(__a, __p, std::forward< _Args >(__args)...))) -> decltype(_S_construct(__a, __p, std::forward< _Args >(__args)...))
Construct an object of type _Tp
__detected_or_t< value_type *, __pointer, _Alloc > pointer
The allocator's pointer type.
static constexpr pointer allocate(_Alloc &__a, size_type __n)
Allocate memory.
_Alloc::value_type value_type
The allocated type.
static constexpr void deallocate(_Alloc &__a, pointer __p, size_type __n)
Deallocate memory.
static constexpr void destroy(_Alloc &__a, _Tp *__p) noexcept(noexcept(_S_destroy(__a, __p, 0)))
Destroy an object of type _Tp.
The standard allocator, as per C++03 [20.4.1].
Base class for all library exceptions.
A simple smart pointer providing strict ownership semantics.
Exception possibly thrown by shared_ptr.
virtual char const * what() const noexcept
One of the comparison functors.
A move-only smart pointer that manages unique ownership of a resource.