49#ifndef _SHARED_PTR_BASE_H
50#define _SHARED_PTR_BASE_H 1
63#if __cplusplus >= 202002L
69namespace std _GLIBCXX_VISIBILITY(default)
71_GLIBCXX_BEGIN_NAMESPACE_VERSION
73#if _GLIBCXX_USE_DEPRECATED
74#pragma GCC diagnostic push
75#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
77#pragma GCC diagnostic pop
87 virtual char const*
what() const noexcept;
94 __throw_bad_weak_ptr()
97 using __gnu_cxx::_Lock_policy;
98 using __gnu_cxx::__default_lock_policy;
99 using __gnu_cxx::_S_single;
100 using __gnu_cxx::_S_mutex;
101 using __gnu_cxx::_S_atomic;
104 template<_Lock_policy _Lp>
109 enum { _S_need_barriers = 0 };
113 class _Mutex_base<_S_mutex>
114 :
public __gnu_cxx::__mutex
120 enum { _S_need_barriers = 1 };
123 template<_Lock_policy _Lp = __default_lock_policy>
124 class _Sp_counted_base
125 :
public _Mutex_base<_Lp>
128 _Sp_counted_base() noexcept
129 : _M_use_count(1), _M_weak_count(1) { }
132 ~_Sp_counted_base() noexcept
138 _M_dispose() noexcept = 0;
142 _M_destroy() noexcept
146 _M_get_deleter(
const std::type_info&)
noexcept = 0;
151 { _S_chk(__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1)); }
157 if (!_M_add_ref_lock_nothrow())
158 __throw_bad_weak_ptr();
163 _M_add_ref_lock_nothrow() noexcept;
167 _M_release() noexcept;
171 _M_release_last_use() noexcept
173 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count);
179 if (_Mutex_base<_Lp>::_S_need_barriers)
181 __atomic_thread_fence (__ATOMIC_ACQ_REL);
185 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
186 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count,
189 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
195 __attribute__((__noinline__))
197 _M_release_last_use_cold() noexcept
198 { _M_release_last_use(); }
202 _M_weak_add_ref() noexcept
206 constexpr _Atomic_word __max = -1;
207 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, 1) == __max)
208 [[__unlikely__]] __builtin_trap();
213 _M_weak_release() noexcept
216 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
217 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1)
219 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
220 if (_Mutex_base<_Lp>::_S_need_barriers)
224 __atomic_thread_fence (__ATOMIC_ACQ_REL);
231 _M_get_use_count() const noexcept
235 auto __count = __atomic_load_n(&_M_use_count, __ATOMIC_RELAXED);
240 return static_cast<_Unsigned_count_type
>(__count);
244 _Sp_counted_base(_Sp_counted_base
const&) =
delete;
245 _Sp_counted_base& operator=(_Sp_counted_base
const&) =
delete;
247#pragma GCC diagnostic push
248#pragma GCC diagnostic ignored "-Wignored-attributes"
250 using _Unsigned_count_type = make_unsigned<_Atomic_word>::type;
251#pragma GCC diagnostic pop
256 _S_chk(_Atomic_word __count)
258 constexpr _Atomic_word __max_atomic_word = _Unsigned_count_type(-1)/2;
276 constexpr _Atomic_word __max
277 =
sizeof(long) >
sizeof(_Atomic_word) ? -1 : __max_atomic_word;
279 if (__count == __max) [[__unlikely__]]
283 _Atomic_word _M_use_count;
284 _Atomic_word _M_weak_count;
292 _Sp_counted_base<_S_single>::_M_add_ref_copy()
294 _S_chk(_M_use_count);
295 __gnu_cxx::__atomic_add_single(&_M_use_count, 1);
300 _Sp_counted_base<_S_single>::_M_weak_release() noexcept
302 if (__gnu_cxx::__exchange_and_add_single(&_M_weak_count, -1) == 1)
308 _Sp_counted_base<_S_single>::_M_get_use_count() const noexcept
310 return static_cast<_Unsigned_count_type
>(_M_use_count);
316 _Sp_counted_base<_S_single>::
317 _M_add_ref_lock_nothrow() noexcept
319 if (_M_use_count == 0)
327 _Sp_counted_base<_S_mutex>::
328 _M_add_ref_lock_nothrow() noexcept
330 __gnu_cxx::__scoped_lock sentry(*
this);
331 if (
auto __c = __gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1))
348 _Sp_counted_base<_S_atomic>::
349 _M_add_ref_lock_nothrow() noexcept
352 _Atomic_word __count = _M_get_use_count();
360 while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1,
361 true, __ATOMIC_ACQ_REL,
369 _Sp_counted_base<_S_single>::_M_release() noexcept
371 if (__gnu_cxx::__exchange_and_add_single(&_M_use_count, -1) == 1)
380 _Sp_counted_base<_S_mutex>::_M_release() noexcept
383 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count);
384 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
386 _M_release_last_use();
392 _Sp_counted_base<_S_atomic>::_M_release() noexcept
394#pragma GCC diagnostic push
395#pragma GCC diagnostic ignored "-Wc++17-extensions"
396 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count);
398 constexpr bool __lock_free
399 = __atomic_always_lock_free(
sizeof(
long long), 0)
400 && __atomic_always_lock_free(
sizeof(_Atomic_word), 0);
401 constexpr bool __double_word
402 =
sizeof(
long long) == 2 *
sizeof(_Atomic_word);
405 constexpr bool __aligned = __alignof(
long long) <=
alignof(
void*);
406 if constexpr (__lock_free && __double_word && __aligned)
408 constexpr int __wordbits = __CHAR_BIT__ *
sizeof(_Atomic_word);
409 constexpr int __shiftbits = __double_word ? __wordbits : 0;
410 constexpr long long __unique_ref = 1LL + (1LL << __shiftbits);
411 auto __both_counts =
reinterpret_cast<long long*
>(&_M_use_count);
413 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
414 if (__atomic_load_n(__both_counts, __ATOMIC_ACQUIRE) == __unique_ref)
420 _M_weak_count = _M_use_count = 0;
421 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count);
422 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
427 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
430 _M_release_last_use_cold();
436 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
438 _M_release_last_use();
440#pragma GCC diagnostic pop
444 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
447 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
450 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
451 class __enable_shared_from_this;
453 template<
typename _Tp>
456 template<
typename _Tp>
459 template<
typename _Tp>
462 template<
typename _Tp>
463 class enable_shared_from_this;
465 template<_Lock_policy _Lp = __default_lock_policy>
468 template<_Lock_policy _Lp = __default_lock_policy>
469 class __shared_count;
471#ifdef __glibcxx_atomic_shared_ptr
477 template<
typename _Ptr, _Lock_policy _Lp>
478 class _Sp_counted_ptr final :
public _Sp_counted_base<_Lp>
482 _Sp_counted_ptr(_Ptr __p) noexcept
486 _M_dispose() noexcept
490 _M_destroy() noexcept
494 _M_get_deleter(
const std::type_info&)
noexcept
497 _Sp_counted_ptr(
const _Sp_counted_ptr&) =
delete;
498 _Sp_counted_ptr& operator=(
const _Sp_counted_ptr&) =
delete;
506 _Sp_counted_ptr<nullptr_t, _S_single>::_M_dispose() noexcept { }
510 _Sp_counted_ptr<nullptr_t, _S_mutex>::_M_dispose() noexcept { }
514 _Sp_counted_ptr<nullptr_t, _S_atomic>::_M_dispose() noexcept { }
516#if ! __has_cpp_attribute(__no_unique_address__)
517#error "support for [[__no_unique_address__]] attribute is required"
520#if ! _GLIBCXX_INLINE_VERSION
523 template<
typename _Tp,
bool = !__is_final(_Tp) && __is_empty(_Tp)>
524 struct _Sp_ebo_helper;
526 template<
typename _Tp,
bool = true>
527 struct _Sp_ebo_helper;
531 template<
typename _Tp>
532 struct _Sp_ebo_helper<_Tp, true>
534 [[__no_unique_address__]] _Tp _M_obj;
537#if ! _GLIBCXX_INLINE_VERSION
539 template<
typename _Tp>
540 struct _Sp_ebo_helper<_Tp, false>
547 template<
typename _Ptr,
typename _Deleter,
typename _Alloc, _Lock_policy _Lp>
548 class _Sp_counted_deleter final :
public _Sp_counted_base<_Lp>
551 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_deleter>;
554 _Sp_counted_deleter(_Ptr __p, _Deleter __d) noexcept
555 : _M_del{
std::move(__d)}, _M_alloc{}, _M_ptr(__p) { }
558 _Sp_counted_deleter(_Ptr __p, _Deleter __d,
const _Alloc& __a) noexcept
559 : _M_del{
std::move(__d)}, _M_alloc{__a}, _M_ptr(__p) { }
561#pragma GCC diagnostic push
562#pragma GCC diagnostic ignored "-Wfree-nonheap-object"
563#pragma GCC diagnostic ignored "-Warray-bounds"
564 template<
typename>
class auto_ptr;
565 ~_Sp_counted_deleter() noexcept { }
568 _M_dispose() noexcept
569 { _M_del._M_obj(_M_ptr); }
570#pragma GCC diagnostic pop
573 _M_destroy() noexcept
575 __allocator_type __a(_M_alloc._M_obj);
576 __allocated_ptr<__allocator_type> __guard_ptr{ __a,
this };
577 this->~_Sp_counted_deleter();
581 _M_get_deleter(
const type_info& __ti [[__gnu__::__unused__]])
noexcept
586 if (__ti ==
typeid(_Deleter))
593#ifdef __glibcxx_out_ptr
594 template<
typename,
typename,
typename...>
friend class out_ptr_t;
597 [[__no_unique_address__]] _Sp_ebo_helper<_Deleter> _M_del;
598 [[__no_unique_address__]] _Sp_ebo_helper<_Alloc> _M_alloc;
604 struct _Sp_make_shared_tag
607 template<
typename _Tp,
typename _Alloc, _Lock_policy _Lp>
608 friend class _Sp_counted_ptr_inplace;
610 static const type_info&
611 _S_ti() noexcept _GLIBCXX_VISIBILITY(default)
613 alignas(type_info)
static constexpr char __tag[
sizeof(type_info)] = { };
614 return reinterpret_cast<const type_info&
>(__tag);
617 static bool _S_eq(
const type_info&)
noexcept;
620 template<
typename _Alloc>
621 struct _Sp_alloc_shared_tag
626 template<
typename _Tp,
typename _Alloc, _Lock_policy _Lp>
627 class _Sp_counted_ptr_inplace final :
public _Sp_counted_base<_Lp>
630 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>;
633 template<
typename... _Args>
634 _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
643#pragma GCC diagnostic push
644#pragma GCC diagnostic ignored "-Warray-bounds"
645 ~_Sp_counted_ptr_inplace() noexcept { }
648 _M_dispose() noexcept
652#pragma GCC diagnostic pop
656 _M_destroy() noexcept
658 __allocator_type __a(_M_alloc._M_obj);
659 __allocated_ptr<__allocator_type> __guard_ptr{ __a,
this };
660 this->~_Sp_counted_ptr_inplace();
664 friend class __shared_count<_Lp>;
669 _M_get_deleter(
const std::type_info& __ti)
noexcept override
675 if (&__ti == &_Sp_make_shared_tag::_S_ti()
678 __ti ==
typeid(_Sp_make_shared_tag)
680 _Sp_make_shared_tag::_S_eq(__ti)
688 _M_ptr() noexcept {
return _M_storage._M_ptr(); }
690 [[__no_unique_address__]] _Sp_ebo_helper<_Alloc> _M_alloc;
691 __gnu_cxx::__aligned_buffer<__remove_cv_t<_Tp>> _M_storage;
694#ifdef __glibcxx_smart_ptr_for_overwrite
695 struct _Sp_overwrite_tag { };
700 template<
typename _Tp,
typename _Alloc, _Lock_policy _Lp>
701 requires is_same_v<typename _Alloc::value_type, _Sp_overwrite_tag>
702 class _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> final
703 :
public _Sp_counted_base<_Lp>
705 [[no_unique_address]] _Alloc _M_alloc;
708 remove_cv_t<_Tp> _M_obj;
712 friend class __shared_count<_Lp>;
717 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>;
719 _Sp_counted_ptr_inplace(
const _Alloc& __a)
722 ::new((
void*)_M_ptr()) _Tp;
725 ~_Sp_counted_ptr_inplace() noexcept { }
728 _M_dispose() noexcept
735 _M_destroy() noexcept
737 using pointer =
typename allocator_traits<__allocator_type>::pointer;
738 __allocator_type __a(_M_alloc);
739 auto __p = pointer_traits<pointer>::pointer_to(*
this);
740 __allocated_ptr<__allocator_type> __guard_ptr{ __a, __p };
741 this->~_Sp_counted_ptr_inplace();
745 _M_get_deleter(
const std::type_info&)
noexcept override
750#if __glibcxx_shared_ptr_arrays >= 201707L
751 struct _Sp_overwrite_tag;
754 template<
typename _Alloc>
755 struct _Sp_counted_array_base
757 [[no_unique_address]] _Alloc _M_alloc{};
759 bool _M_overwrite =
false;
761 typename allocator_traits<_Alloc>::pointer
762 _M_alloc_array(
size_t __tail)
764 return allocator_traits<_Alloc>::allocate(_M_alloc, _M_n + __tail);
768 _M_dealloc_array(
typename allocator_traits<_Alloc>::pointer __p,
771 allocator_traits<_Alloc>::deallocate(_M_alloc, __p, _M_n + __tail);
775 template<
typename _Init>
777 _M_init(
typename allocator_traits<_Alloc>::value_type* __p,
780 using _Tp = remove_pointer_t<_Init>;
781 using _Up =
typename allocator_traits<_Alloc>::value_type;
783 if constexpr (is_same_v<_Init, _Sp_overwrite_tag>)
788 else if (__init ==
nullptr)
789 std::__uninitialized_default_n_a(__p, _M_n, _M_alloc);
790 else if constexpr (!is_array_v<_Tp>)
791 std::__uninitialized_fill_n_a(__p, _M_n, *__init, _M_alloc);
794#pragma GCC diagnostic push
795#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
798 using value_type = _Up;
799 using difference_type = ptrdiff_t;
800 using pointer =
const _Up*;
801 using reference =
const _Up&;
802 using iterator_category = forward_iterator_tag;
808 _Iter& operator++() { ++_M_pos;
return *
this; }
809 _Iter operator++(
int) {
auto __i(*
this); ++_M_pos;
return __i; }
811 reference
operator*()
const {
return _M_p[_M_pos % _M_len]; }
812 pointer operator->()
const {
return _M_p + (_M_pos % _M_len); }
814 bool operator==(
const _Iter& __i)
const
815 {
return _M_pos == __i._M_pos; }
817#pragma GCC diagnostic pop
819 _Iter __first{_S_first_elem(__init),
sizeof(_Tp) /
sizeof(_Up)};
820 _Iter __last = __first;
821 __last._M_pos = _M_n;
822 std::__uninitialized_copy_a(__first, __last, __p, _M_alloc);
829 _M_dispose_array(
typename allocator_traits<_Alloc>::value_type* __p)
832 std::destroy_n(__p, _M_n);
837 allocator_traits<_Alloc>::destroy(_M_alloc, __p + __n);
842 template<
typename _Tp>
844 _S_first_elem(_Tp* __p) {
return __p; }
846 template<
typename _Tp,
size_t _Nm>
848 _S_first_elem(_Tp (*__p)[_Nm]) {
return _S_first_elem(*__p); }
853 template<
typename _Alloc, _Lock_policy _Lp>
854 class _Sp_counted_array final
855 :
public _Sp_counted_base<_Lp>, _Sp_counted_array_base<_Alloc>
857 using pointer =
typename allocator_traits<_Alloc>::pointer;
859 pointer _M_alloc_ptr;
863 friend class __shared_count<_Lp>;
866 _Sp_counted_array(
const _Sp_counted_array_base<_Alloc>& __a,
867 pointer __p) noexcept
868 : _Sp_counted_array_base<_Alloc>(__a), _M_alloc_ptr(__p)
871 ~_Sp_counted_array() =
default;
874 _M_dispose() noexcept
877 this->_M_dispose_array(_M_ptr());
882 _M_destroy() noexcept
884 _Sp_counted_array_base<_Alloc> __a = *
this;
885 pointer __p = _M_alloc_ptr;
886 this->~_Sp_counted_array();
887 __a._M_dealloc_array(__p, _S_tail());
892 static constexpr size_t
896 using _Tp =
typename allocator_traits<_Alloc>::value_type;
899 size_t __bytes =
sizeof(_Sp_counted_array);
902 if constexpr (
alignof(_Tp) <
alignof(_Sp_counted_array))
903 __bytes +=
alignof(_Sp_counted_array) -
alignof(_Tp);
905 return (__bytes +
sizeof(_Tp) - 1) /
sizeof(_Tp);
909 _M_get_deleter(
const std::type_info&)
noexcept override
915 struct __sp_array_delete
917 template<
typename _Yp>
918 void operator()(_Yp* __p)
const {
delete[] __p; }
921 template<_Lock_policy _Lp>
925 template<
typename _Tp>
926 struct __not_alloc_shared_tag {
using type = void; };
928 template<
typename _Tp>
929 struct __not_alloc_shared_tag<_Sp_alloc_shared_tag<_Tp>> { };
931#if __glibcxx_shared_ptr_arrays >= 201707L
932 template<
typename _Alloc>
933 struct __not_alloc_shared_tag<_Sp_counted_array_base<_Alloc>> { };
937 constexpr __shared_count() noexcept : _M_pi(0)
940 template<
typename _Ptr>
942 __shared_count(_Ptr __p) : _M_pi(0)
946 _M_pi =
new _Sp_counted_ptr<_Ptr, _Lp>(__p);
951 __throw_exception_again;
955 template<
typename _Ptr>
956 __shared_count(_Ptr __p, false_type)
957 : __shared_count(__p)
960 template<
typename _Ptr>
961 __shared_count(_Ptr __p, true_type)
962 : __shared_count(__p, __sp_array_delete{}, allocator<void>())
965 template<
typename _Ptr,
typename _Deleter,
966 typename =
typename __not_alloc_shared_tag<_Deleter>::type>
967 __shared_count(_Ptr __p, _Deleter __d)
968 : __shared_count(__p, std::move(__d), allocator<void>())
971 template<
typename _Ptr,
typename _Deleter,
typename _Alloc,
972 typename =
typename __not_alloc_shared_tag<_Deleter>::type>
973 __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
975 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
978 typename _Sp_cd_type::__allocator_type __a2(__a);
979 auto __guard = std::__allocate_guarded(__a2);
980 _Sp_cd_type* __mem = __guard.get();
988 __throw_exception_again;
992 template<
typename _Tp,
typename _Alloc,
typename... _Args>
993 __shared_count(_Tp*& __p, _Sp_alloc_shared_tag<_Alloc> __a,
996 using _Tp2 = __remove_cv_t<_Tp>;
997 using _Sp_cp_type = _Sp_counted_ptr_inplace<_Tp2, _Alloc, _Lp>;
998 typename _Sp_cp_type::__allocator_type __a2(__a._M_a);
999 auto __guard = std::__allocate_guarded(__a2);
1000 _Sp_cp_type* __mem = __guard.get();
1001 auto __pi = ::new (__mem)
1005 __p = __pi->_M_ptr();
1008#if __glibcxx_shared_ptr_arrays >= 201707L
1009 template<
typename _Tp,
typename _Alloc,
typename _Init>
1010 __shared_count(_Tp*& __p,
const _Sp_counted_array_base<_Alloc>& __a,
1013 using _Up = remove_all_extents_t<_Tp>;
1014 static_assert(is_same_v<_Up, typename _Alloc::value_type>);
1016 using _Sp_ca_type = _Sp_counted_array<_Alloc, _Lp>;
1017 const size_t __tail = _Sp_ca_type::_S_tail();
1019 struct _Guarded_ptr : _Sp_counted_array_base<_Alloc>
1021 typename allocator_traits<_Alloc>::pointer _M_ptr;
1023 _Guarded_ptr(_Sp_counted_array_base<_Alloc> __a)
1024 : _Sp_counted_array_base<_Alloc>(__a),
1025 _M_ptr(this->_M_alloc_array(_Sp_ca_type::_S_tail()))
1031 this->_M_dealloc_array(_M_ptr, _Sp_ca_type::_S_tail());
1035 _Guarded_ptr __guard{__a};
1037 __guard._M_init(__raw, __init);
1039 void* __c = __raw + __a._M_n;
1040 if constexpr (
alignof(_Up) <
alignof(_Sp_ca_type))
1042 size_t __space =
sizeof(_Up) * __tail;
1043 __c =
std::align(
alignof(_Sp_ca_type),
sizeof(_Sp_ca_type),
1046 auto __pi = ::new(__c) _Sp_ca_type(__guard, __guard._M_ptr);
1047 __guard._M_ptr =
nullptr;
1049 __p =
reinterpret_cast<_Tp*
>(__raw);
1053#if _GLIBCXX_USE_DEPRECATED
1054#pragma GCC diagnostic push
1055#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1057 template<
typename _Tp>
1059 __shared_count(std::auto_ptr<_Tp>&& __r);
1060#pragma GCC diagnostic pop
1064 template<
typename _Tp,
typename _Del>
1066 __shared_count(std::unique_ptr<_Tp, _Del>&& __r) : _M_pi(0)
1070 if (__r.get() ==
nullptr)
1073 using _Ptr =
typename unique_ptr<_Tp, _Del>::pointer;
1074 using _Del2 = __conditional_t<is_reference<_Del>::value,
1075 reference_wrapper<
typename remove_reference<_Del>::type>,
1078 = _Sp_counted_deleter<_Ptr, _Del2, allocator<void>, _Lp>;
1079 using _Alloc = allocator<_Sp_cd_type>;
1080 using _Alloc_traits = allocator_traits<_Alloc>;
1082 _Sp_cd_type* __mem = _Alloc_traits::allocate(__a, 1);
1086 _Alloc_traits::construct(__a, __mem, __r.release(),
1092 explicit __shared_count(
const __weak_count<_Lp>& __r);
1096 __shared_count(
const __weak_count<_Lp>& __r, std::nothrow_t)
noexcept;
1098 ~__shared_count() noexcept
1100 if (_M_pi !=
nullptr)
1101 _M_pi->_M_release();
1104 __shared_count(
const __shared_count& __r) noexcept
1107 if (_M_pi !=
nullptr)
1108 _M_pi->_M_add_ref_copy();
1112 operator=(
const __shared_count& __r)
noexcept
1114 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1117 if (__tmp !=
nullptr)
1118 __tmp->_M_add_ref_copy();
1119 if (_M_pi !=
nullptr)
1120 _M_pi->_M_release();
1127 _M_swap(__shared_count& __r)
noexcept
1129 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1135 _M_get_use_count() const noexcept
1136 {
return _M_pi ? _M_pi->_M_get_use_count() : 0; }
1139 _M_unique() const noexcept
1140 {
return this->_M_get_use_count() == 1; }
1143 _M_get_deleter(
const std::type_info& __ti)
const noexcept
1144 {
return _M_pi ? _M_pi->_M_get_deleter(__ti) :
nullptr; }
1147 _M_less(
const __shared_count& __rhs)
const noexcept
1148 {
return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
1151 _M_less(
const __weak_count<_Lp>& __rhs)
const noexcept
1152 {
return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
1154#ifdef __glibcxx_smart_ptr_owner_equality
1156 _M_owner_hash() const noexcept
1157 {
return std::hash<_Sp_counted_base<_Lp>*>()(this->_M_pi); }
1162 operator==(
const __shared_count& __a,
const __shared_count& __b)
noexcept
1163 {
return __a._M_pi == __b._M_pi; }
1166 friend class __weak_count<_Lp>;
1167#ifdef __glibcxx_atomic_shared_ptr
1168 template<
typename>
friend class _Sp_atomic;
1170#ifdef __glibcxx_out_ptr
1171 template<
typename,
typename,
typename...>
friend class out_ptr_t;
1174 _Sp_counted_base<_Lp>* _M_pi;
1178 template<_Lock_policy _Lp>
1182 constexpr __weak_count() noexcept : _M_pi(
nullptr)
1185 __weak_count(
const __shared_count<_Lp>& __r) noexcept
1188 if (_M_pi !=
nullptr)
1189 _M_pi->_M_weak_add_ref();
1192 __weak_count(
const __weak_count& __r) noexcept
1195 if (_M_pi !=
nullptr)
1196 _M_pi->_M_weak_add_ref();
1199 __weak_count(__weak_count&& __r) noexcept
1201 { __r._M_pi =
nullptr; }
1203 ~__weak_count() noexcept
1205 if (_M_pi !=
nullptr)
1206 _M_pi->_M_weak_release();
1210 operator=(
const __shared_count<_Lp>& __r)
noexcept
1212 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1213 if (__tmp !=
nullptr)
1214 __tmp->_M_weak_add_ref();
1215 if (_M_pi !=
nullptr)
1216 _M_pi->_M_weak_release();
1222 operator=(
const __weak_count& __r)
noexcept
1224 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1225 if (__tmp !=
nullptr)
1226 __tmp->_M_weak_add_ref();
1227 if (_M_pi !=
nullptr)
1228 _M_pi->_M_weak_release();
1234 operator=(__weak_count&& __r)
noexcept
1236 if (_M_pi !=
nullptr)
1237 _M_pi->_M_weak_release();
1239 __r._M_pi =
nullptr;
1244 _M_swap(__weak_count& __r)
noexcept
1246 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1252 _M_get_use_count() const noexcept
1253 {
return _M_pi !=
nullptr ? _M_pi->_M_get_use_count() : 0; }
1256 _M_less(
const __weak_count& __rhs)
const noexcept
1257 {
return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
1260 _M_less(
const __shared_count<_Lp>& __rhs)
const noexcept
1261 {
return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
1263#ifdef __glibcxx_smart_ptr_owner_equality
1265 _M_owner_hash() const noexcept
1266 {
return std::hash<_Sp_counted_base<_Lp>*>()(this->_M_pi); }
1271 operator==(
const __weak_count& __a,
const __weak_count& __b)
noexcept
1272 {
return __a._M_pi == __b._M_pi; }
1275 friend class __shared_count<_Lp>;
1276#ifdef __glibcxx_atomic_shared_ptr
1277 template<
typename>
friend class _Sp_atomic;
1280 _Sp_counted_base<_Lp>* _M_pi;
1284 template<_Lock_policy _Lp>
1286 __shared_count<_Lp>::__shared_count(
const __weak_count<_Lp>& __r)
1289 if (_M_pi ==
nullptr || !_M_pi->_M_add_ref_lock_nothrow())
1290 __throw_bad_weak_ptr();
1294 template<_Lock_policy _Lp>
1296 __shared_count<_Lp>::
1297 __shared_count(
const __weak_count<_Lp>& __r, std::nothrow_t) noexcept
1300 if (_M_pi && !_M_pi->_M_add_ref_lock_nothrow())
1308 template<
typename _Yp_ptr,
typename _Tp_ptr>
1309 struct __sp_compatible_with
1313 template<
typename _Yp,
typename _Tp>
1314 struct __sp_compatible_with<_Yp*, _Tp*>
1315 : is_convertible<_Yp*, _Tp*>::type
1318 template<
typename _Up,
size_t _Nm>
1319 struct __sp_compatible_with<_Up(*)[_Nm], _Up(*)[]>
1323 template<
typename _Up,
size_t _Nm>
1324 struct __sp_compatible_with<_Up(*)[_Nm],
const _Up(*)[]>
1328 template<
typename _Up,
size_t _Nm>
1329 struct __sp_compatible_with<_Up(*)[_Nm],
volatile _Up(*)[]>
1333 template<
typename _Up,
size_t _Nm>
1334 struct __sp_compatible_with<_Up(*)[_Nm],
const volatile _Up(*)[]>
1339 template<
typename _Up,
size_t _Nm,
typename _Yp,
typename =
void>
1340 struct __sp_is_constructible_arrN
1344 template<
typename _Up,
size_t _Nm,
typename _Yp>
1345 struct __sp_is_constructible_arrN<_Up, _Nm, _Yp, __void_t<_Yp[_Nm]>>
1346 : is_convertible<_Yp(*)[_Nm], _Up(*)[_Nm]>::type
1350 template<
typename _Up,
typename _Yp,
typename =
void>
1351 struct __sp_is_constructible_arr
1355 template<
typename _Up,
typename _Yp>
1356 struct __sp_is_constructible_arr<_Up, _Yp, __void_t<_Yp[]>>
1357 : is_convertible<_Yp(*)[], _Up(*)[]>::type
1361 template<
typename _Tp,
typename _Yp>
1362 struct __sp_is_constructible;
1365 template<
typename _Up,
size_t _Nm,
typename _Yp>
1366 struct __sp_is_constructible<_Up[_Nm], _Yp>
1367 : __sp_is_constructible_arrN<_Up, _Nm, _Yp>::type
1371 template<
typename _Up,
typename _Yp>
1372 struct __sp_is_constructible<_Up[], _Yp>
1373 : __sp_is_constructible_arr<_Up, _Yp>::type
1377 template<
typename _Tp,
typename _Yp>
1378 struct __sp_is_constructible
1379 : is_convertible<_Yp*, _Tp*>::type
1383 template<
typename _Tp>
1384 [[__gnu__::__always_inline__]]
1386 __shared_ptr_deref(_Tp* __p)
1388 __glibcxx_assert(__p !=
nullptr);
1393 template<
typename _Tp, _Lock_policy _Lp,
1395 class __shared_ptr_access
1398 using element_type = _Tp;
1401 operator*() const noexcept
1402 {
return *std::__shared_ptr_deref(_M_get()); }
1405 operator->() const noexcept
1407 _GLIBCXX_DEBUG_PEDASSERT(_M_get() !=
nullptr);
1413 _M_get() const noexcept
1414 {
return static_cast<const __shared_ptr<_Tp, _Lp>*
>(
this)->get(); }
1418 template<
typename _Tp, _Lock_policy _Lp>
1419 class __shared_ptr_access<_Tp, _Lp, false, true>
1422 using element_type = _Tp;
1425 operator->() const noexcept
1427 auto __ptr =
static_cast<const __shared_ptr<_Tp, _Lp>*
>(
this)->get();
1428 _GLIBCXX_DEBUG_PEDASSERT(__ptr !=
nullptr);
1434 template<
typename _Tp, _Lock_policy _Lp>
1435 class __shared_ptr_access<_Tp, _Lp, true, false>
1438 using element_type =
typename remove_extent<_Tp>::type;
1440#if __cplusplus <= 201402L
1441 [[__deprecated__(
"shared_ptr<T[]>::operator* is absent from C++17")]]
1443 operator*() const noexcept
1444 {
return *std::__shared_ptr_deref(_M_get()); }
1446 [[__deprecated__(
"shared_ptr<T[]>::operator-> is absent from C++17")]]
1448 operator->() const noexcept
1450 _GLIBCXX_DEBUG_PEDASSERT(_M_get() !=
nullptr);
1455#pragma GCC diagnostic push
1456#pragma GCC diagnostic ignored "-Wc++17-extensions"
1458 operator[](ptrdiff_t __i)
const noexcept
1460 if constexpr (extent<_Tp>::value)
1461 __glibcxx_assert(__i < extent<_Tp>::value);
1462 return std::__shared_ptr_deref(_M_get())[__i];
1464#pragma GCC diagnostic pop
1468 _M_get() const noexcept
1469 {
return static_cast<const __shared_ptr<_Tp, _Lp>*
>(
this)->get(); }
1472 template<
typename _Tp, _Lock_policy _Lp>
1474 :
public __shared_ptr_access<_Tp, _Lp>
1477 using element_type =
typename remove_extent<_Tp>::type;
1481 template<
typename _Yp>
1483 =
typename enable_if<__sp_is_constructible<_Tp, _Yp>::value>::type;
1486 template<
typename _Yp,
typename _Res =
void>
1487 using _Compatible =
typename
1488 enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type;
1491 template<
typename _Yp>
1492 using _Assignable = _Compatible<_Yp, __shared_ptr&>;
1495 template<
typename _Yp,
typename _Del,
typename _Res = void,
1496 typename _Ptr =
typename unique_ptr<_Yp, _Del>::pointer>
1497 using _UniqCompatible = __enable_if_t<__and_<
1498 __sp_compatible_with<_Yp*, _Tp*>,
1499 is_convertible<_Ptr, element_type*>,
1500 is_move_constructible<_Del>
1504 template<
typename _Yp,
typename _Del>
1505 using _UniqAssignable = _UniqCompatible<_Yp, _Del, __shared_ptr&>;
1509#if __cplusplus > 201402L
1510 using weak_type = __weak_ptr<_Tp, _Lp>;
1513 constexpr __shared_ptr() noexcept
1514 : _M_ptr(0), _M_refcount()
1517 template<
typename _Yp,
typename = _SafeConv<_Yp>>
1519 __shared_ptr(_Yp* __p)
1520 : _M_ptr(__p), _M_refcount(__p, typename is_array<_Tp>::type())
1522 static_assert( !is_void<_Yp>::value,
"incomplete type" );
1523 static_assert(
sizeof(_Yp) > 0,
"incomplete type" );
1524 _M_enable_shared_from_this_with(__p);
1527 template<
typename _Yp,
typename _Deleter,
typename = _SafeConv<_Yp>>
1528 __shared_ptr(_Yp* __p, _Deleter __d)
1529 : _M_ptr(__p), _M_refcount(__p, std::
move(__d))
1531 static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
1532 "deleter expression d(p) is well-formed");
1533 _M_enable_shared_from_this_with(__p);
1536 template<
typename _Yp,
typename _Deleter,
typename _Alloc,
1537 typename = _SafeConv<_Yp>>
1538 __shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a)
1539 : _M_ptr(__p), _M_refcount(__p, std::
move(__d), std::
move(__a))
1541 static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
1542 "deleter expression d(p) is well-formed");
1543 _M_enable_shared_from_this_with(__p);
1546 template<
typename _Deleter>
1547 __shared_ptr(nullptr_t __p, _Deleter __d)
1548 : _M_ptr(0), _M_refcount(__p, std::
move(__d))
1551 template<
typename _Deleter,
typename _Alloc>
1552 __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
1553 : _M_ptr(0), _M_refcount(__p, std::
move(__d), std::
move(__a))
1557 template<
typename _Yp>
1558 __shared_ptr(
const __shared_ptr<_Yp, _Lp>& __r,
1559 element_type* __p) noexcept
1560 : _M_ptr(__p), _M_refcount(__r._M_refcount)
1564 template<
typename _Yp>
1565 __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r,
1566 element_type* __p) noexcept
1567 : _M_ptr(__p), _M_refcount()
1569 _M_refcount._M_swap(__r._M_refcount);
1570 __r._M_ptr =
nullptr;
1573 __shared_ptr(
const __shared_ptr&)
noexcept =
default;
1574 __shared_ptr& operator=(
const __shared_ptr&)
noexcept =
default;
1575 ~__shared_ptr() =
default;
1577 template<
typename _Yp,
typename = _Compatible<_Yp>>
1578 __shared_ptr(
const __shared_ptr<_Yp, _Lp>& __r) noexcept
1579 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1582 __shared_ptr(__shared_ptr&& __r) noexcept
1583 : _M_ptr(__r._M_ptr), _M_refcount()
1585 _M_refcount._M_swap(__r._M_refcount);
1586 __r._M_ptr =
nullptr;
1589 template<
typename _Yp,
typename = _Compatible<_Yp>>
1590 __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r) noexcept
1591 : _M_ptr(__r._M_ptr), _M_refcount()
1593 _M_refcount._M_swap(__r._M_refcount);
1594 __r._M_ptr =
nullptr;
1597 template<
typename _Yp,
typename = _Compatible<_Yp>>
1598 explicit __shared_ptr(
const __weak_ptr<_Yp, _Lp>& __r)
1599 : _M_refcount(__r._M_refcount)
1603 _M_ptr = __r._M_ptr;
1607 template<
typename _Yp,
typename _Del,
1608 typename = _UniqCompatible<_Yp, _Del>>
1609 __shared_ptr(unique_ptr<_Yp, _Del>&& __r)
1610 : _M_ptr(__r.get()), _M_refcount()
1612 auto __raw = std::__to_address(__r.get());
1613 _M_refcount = __shared_count<_Lp>(
std::move(__r));
1614 _M_enable_shared_from_this_with(__raw);
1617#if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED
1620 template<
typename _Tp1,
typename _Del,
1621 typename enable_if<__and_<
1622 __not_<is_array<_Tp>>, is_array<_Tp1>,
1623 is_convertible<typename unique_ptr<_Tp1, _Del>::pointer, _Tp*>
1624 >::value,
bool>::type =
true>
1625 __shared_ptr(unique_ptr<_Tp1, _Del>&& __r, __sp_array_delete)
1626 : _M_ptr(__r.get()), _M_refcount()
1628 auto __raw = std::__to_address(__r.get());
1629 _M_refcount = __shared_count<_Lp>(
std::move(__r));
1630 _M_enable_shared_from_this_with(__raw);
1635#if _GLIBCXX_USE_DEPRECATED
1636#pragma GCC diagnostic push
1637#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1639 template<
typename _Yp,
typename = _Compatible<_Yp>>
1640 __shared_ptr(auto_ptr<_Yp>&& __r);
1641#pragma GCC diagnostic pop
1644 constexpr __shared_ptr(nullptr_t) noexcept : __shared_ptr() { }
1646 template<
typename _Yp>
1648 operator=(
const __shared_ptr<_Yp, _Lp>& __r)
noexcept
1650 _M_ptr = __r._M_ptr;
1651 _M_refcount = __r._M_refcount;
1655#if _GLIBCXX_USE_DEPRECATED
1656#pragma GCC diagnostic push
1657#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1658 template<
typename _Yp>
1660 operator=(auto_ptr<_Yp>&& __r)
1662 __shared_ptr(
std::move(__r)).swap(*
this);
1665#pragma GCC diagnostic pop
1669 operator=(__shared_ptr&& __r)
noexcept
1671 __shared_ptr(
std::move(__r)).swap(*
this);
1677 operator=(__shared_ptr<_Yp, _Lp>&& __r)
noexcept
1679 __shared_ptr(
std::move(__r)).swap(*
this);
1683 template<
typename _Yp,
typename _Del>
1684 _UniqAssignable<_Yp, _Del>
1685 operator=(unique_ptr<_Yp, _Del>&& __r)
1687 __shared_ptr(
std::move(__r)).swap(*
this);
1693 { __shared_ptr().swap(*
this); }
1695 template<
typename _Yp>
1700 __glibcxx_assert(__p ==
nullptr || __p != _M_ptr);
1701 __shared_ptr(__p).swap(*
this);
1704 template<
typename _Yp,
typename _Deleter>
1706 reset(_Yp* __p, _Deleter __d)
1707 { __shared_ptr(__p,
std::move(__d)).swap(*
this); }
1709 template<
typename _Yp,
typename _Deleter,
typename _Alloc>
1711 reset(_Yp* __p, _Deleter __d, _Alloc __a)
1716 get() const noexcept
1720 explicit operator bool() const noexcept
1721 {
return _M_ptr !=
nullptr; }
1725 unique() const noexcept
1726 {
return _M_refcount._M_unique(); }
1730 use_count() const noexcept
1731 {
return _M_refcount._M_get_use_count(); }
1735 swap(__shared_ptr<_Tp, _Lp>& __other)
noexcept
1737 std::swap(_M_ptr, __other._M_ptr);
1738 _M_refcount._M_swap(__other._M_refcount);
1748 template<
typename _Tp1>
1750 owner_before(__shared_ptr<_Tp1, _Lp>
const& __rhs)
const noexcept
1751 {
return _M_refcount._M_less(__rhs._M_refcount); }
1753 template<
typename _Tp1>
1755 owner_before(__weak_ptr<_Tp1, _Lp>
const& __rhs)
const noexcept
1756 {
return _M_refcount._M_less(__rhs._M_refcount); }
1759#ifdef __glibcxx_smart_ptr_owner_equality
1760 size_t owner_hash() const noexcept {
return _M_refcount._M_owner_hash(); }
1762 template<
typename _Tp1>
1764 owner_equal(__shared_ptr<_Tp1, _Lp>
const& __rhs)
const noexcept
1765 {
return _M_refcount == __rhs._M_refcount; }
1767 template<
typename _Tp1>
1769 owner_equal(__weak_ptr<_Tp1, _Lp>
const& __rhs)
const noexcept
1770 {
return _M_refcount == __rhs._M_refcount; }
1775 template<
typename _Alloc,
typename... _Args>
1776 __shared_ptr(_Sp_alloc_shared_tag<_Alloc> __tag, _Args&&... __args)
1777 : _M_ptr(), _M_refcount(_M_ptr, __tag, std::
forward<_Args>(__args)...)
1778 { _M_enable_shared_from_this_with(_M_ptr); }
1780 template<
typename _Tp1, _Lock_policy _Lp1,
typename _Alloc,
1782 friend __shared_ptr<_Tp1, _Lp1>
1783 __allocate_shared(
const _Alloc& __a, _Args&&... __args);
1785#if __glibcxx_shared_ptr_arrays >= 201707L
1787 template<
typename _Alloc,
typename _Init = const remove_extent_t<_Tp>*>
1788 __shared_ptr(
const _Sp_counted_array_base<_Alloc>& __a,
1789 _Init __init =
nullptr)
1790 : _M_ptr(), _M_refcount(_M_ptr, __a, __init)
1796 __shared_ptr(
const __weak_ptr<_Tp, _Lp>& __r, std::nothrow_t) noexcept
1797 : _M_refcount(__r._M_refcount, std::nothrow)
1799 _M_ptr = _M_refcount._M_get_use_count() ? __r._M_ptr :
nullptr;
1802 friend class __weak_ptr<_Tp, _Lp>;
1806 template<
typename _Yp>
1807 using __esft_base_t =
decltype(__enable_shared_from_this_base(
1812 template<
typename _Yp,
typename =
void>
1813 struct __has_esft_base
1816 template<
typename _Yp>
1817 struct __has_esft_base<_Yp, __void_t<__esft_base_t<_Yp>>>
1818 : __not_<is_array<_Tp>> { };
1820 template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
1821 typename enable_if<__has_esft_base<_Yp2>::value>::type
1822 _M_enable_shared_from_this_with(_Yp* __p)
noexcept
1824 if (
auto __base = __enable_shared_from_this_base(_M_refcount, __p))
1825 __base->_M_weak_assign(
const_cast<_Yp2*
>(__p), _M_refcount);
1828 template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
1829 typename enable_if<!__has_esft_base<_Yp2>::value>::type
1830 _M_enable_shared_from_this_with(_Yp*)
noexcept
1834 _M_get_deleter(
const std::type_info& __ti)
const noexcept
1835 {
return _M_refcount._M_get_deleter(__ti); }
1837 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
1838 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
1840 template<
typename _Del,
typename _Tp1, _Lock_policy _Lp1>
1841 friend _Del* get_deleter(
const __shared_ptr<_Tp1, _Lp1>&)
noexcept;
1843 template<
typename _Del,
typename _Tp1>
1844 friend _Del* get_deleter(
const shared_ptr<_Tp1>&)
noexcept;
1846#ifdef __glibcxx_atomic_shared_ptr
1847 friend _Sp_atomic<shared_ptr<_Tp>>;
1849#ifdef __glibcxx_out_ptr
1850 template<
typename,
typename,
typename...>
friend class out_ptr_t;
1853 element_type* _M_ptr;
1854 __shared_count<_Lp> _M_refcount;
1859 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1861 operator==(
const __shared_ptr<_Tp1, _Lp>& __a,
1862 const __shared_ptr<_Tp2, _Lp>& __b)
noexcept
1863 {
return __a.get() == __b.get(); }
1865 template<
typename _Tp, _Lock_policy _Lp>
1867 operator==(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1870#ifdef __cpp_lib_three_way_comparison
1871 template<
typename _Tp,
typename _Up, _Lock_policy _Lp>
1872 inline strong_ordering
1873 operator<=>(
const __shared_ptr<_Tp, _Lp>& __a,
1874 const __shared_ptr<_Up, _Lp>& __b)
noexcept
1875 {
return compare_three_way()(__a.get(), __b.get()); }
1877 template<
typename _Tp, _Lock_policy _Lp>
1878 inline strong_ordering
1879 operator<=>(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1881 using pointer =
typename __shared_ptr<_Tp, _Lp>::element_type*;
1882 return compare_three_way()(__a.get(),
static_cast<pointer
>(
nullptr));
1885 template<
typename _Tp, _Lock_policy _Lp>
1887 operator==(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a)
noexcept
1890 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1892 operator!=(
const __shared_ptr<_Tp1, _Lp>& __a,
1893 const __shared_ptr<_Tp2, _Lp>& __b)
noexcept
1894 {
return __a.get() != __b.get(); }
1896 template<
typename _Tp, _Lock_policy _Lp>
1898 operator!=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1899 {
return (
bool)__a; }
1901 template<
typename _Tp, _Lock_policy _Lp>
1903 operator!=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a)
noexcept
1904 {
return (
bool)__a; }
1906 template<
typename _Tp,
typename _Up, _Lock_policy _Lp>
1908 operator<(
const __shared_ptr<_Tp, _Lp>& __a,
1909 const __shared_ptr<_Up, _Lp>& __b)
noexcept
1911 using _Tp_elt =
typename __shared_ptr<_Tp, _Lp>::element_type;
1912 using _Up_elt =
typename __shared_ptr<_Up, _Lp>::element_type;
1914 return less<_Vp>()(__a.get(), __b.get());
1917 template<
typename _Tp, _Lock_policy _Lp>
1919 operator<(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1921 using _Tp_elt =
typename __shared_ptr<_Tp, _Lp>::element_type;
1925 template<
typename _Tp, _Lock_policy _Lp>
1927 operator<(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a)
noexcept
1929 using _Tp_elt =
typename __shared_ptr<_Tp, _Lp>::element_type;
1933 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1935 operator<=(
const __shared_ptr<_Tp1, _Lp>& __a,
1936 const __shared_ptr<_Tp2, _Lp>& __b)
noexcept
1937 {
return !(__b < __a); }
1939 template<
typename _Tp, _Lock_policy _Lp>
1941 operator<=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1942 {
return !(
nullptr < __a); }
1944 template<
typename _Tp, _Lock_policy _Lp>
1946 operator<=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a)
noexcept
1947 {
return !(__a <
nullptr); }
1949 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1951 operator>(
const __shared_ptr<_Tp1, _Lp>& __a,
1952 const __shared_ptr<_Tp2, _Lp>& __b)
noexcept
1953 {
return (__b < __a); }
1955 template<
typename _Tp, _Lock_policy _Lp>
1957 operator>(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1958 {
return nullptr < __a; }
1960 template<
typename _Tp, _Lock_policy _Lp>
1962 operator>(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a)
noexcept
1963 {
return __a <
nullptr; }
1965 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1967 operator>=(
const __shared_ptr<_Tp1, _Lp>& __a,
1968 const __shared_ptr<_Tp2, _Lp>& __b)
noexcept
1969 {
return !(__a < __b); }
1971 template<
typename _Tp, _Lock_policy _Lp>
1973 operator>=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1974 {
return !(__a <
nullptr); }
1976 template<
typename _Tp, _Lock_policy _Lp>
1978 operator>=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a)
noexcept
1979 {
return !(
nullptr < __a); }
1983 template<
typename _Tp, _Lock_policy _Lp>
1985 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b)
noexcept
1995 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1996 inline __shared_ptr<_Tp, _Lp>
1999 using _Sp = __shared_ptr<_Tp, _Lp>;
2000 return _Sp(__r,
static_cast<typename _Sp::element_type*
>(__r.get()));
2008 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
2009 inline __shared_ptr<_Tp, _Lp>
2012 using _Sp = __shared_ptr<_Tp, _Lp>;
2013 return _Sp(__r,
const_cast<typename _Sp::element_type*
>(__r.get()));
2021 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
2022 inline __shared_ptr<_Tp, _Lp>
2025 using _Sp = __shared_ptr<_Tp, _Lp>;
2026 if (
auto* __p =
dynamic_cast<typename _Sp::element_type*
>(__r.get()))
2027 return _Sp(__r, __p);
2031#if __cplusplus > 201402L
2032 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
2033 inline __shared_ptr<_Tp, _Lp>
2034 reinterpret_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r)
noexcept
2036 using _Sp = __shared_ptr<_Tp, _Lp>;
2037 return _Sp(__r,
reinterpret_cast<typename _Sp::element_type*
>(__r.get()));
2041 template<
typename _Tp, _Lock_policy _Lp>
2045 using element_type =
typename remove_extent<_Tp>::type;
2048 template<
typename _Yp,
typename _Res =
void>
2049 using _Compatible =
typename
2053 template<
typename _Yp>
2054 using _Assignable = _Compatible<_Yp, __weak_ptr&>;
2056#pragma GCC diagnostic push
2057#pragma GCC diagnostic ignored "-Wc++17-extensions"
2059 template<
typename _Yp>
2060 static element_type*
2061 _S_safe_upcast(
const __weak_ptr<_Yp, _Lp>& __r)
2076 else if constexpr (__and_<is_scalar<_At>,
is_scalar<_Bt>>::value)
2078#if _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_is_virtual_base_of)
2082 else if constexpr (!__builtin_is_virtual_base_of(_Tp, _Yp))
2089 return __r.lock().get();
2091#pragma GCC diagnostic pop
2094 constexpr __weak_ptr() noexcept
2095 : _M_ptr(
nullptr), _M_refcount()
2098 __weak_ptr(
const __weak_ptr&)
noexcept =
default;
2100 ~__weak_ptr() =
default;
2116 template<
typename _Yp,
typename = _Compatible<_Yp>>
2117 __weak_ptr(
const __weak_ptr<_Yp, _Lp>& __r) noexcept
2118 : _M_ptr(_S_safe_upcast(__r)), _M_refcount(__r._M_refcount)
2121 template<
typename _Yp,
typename = _Compatible<_Yp>>
2122 __weak_ptr(
const __shared_ptr<_Yp, _Lp>& __r) noexcept
2123 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
2126 __weak_ptr(__weak_ptr&& __r) noexcept
2127 : _M_ptr(__r._M_ptr), _M_refcount(
std::move(__r._M_refcount))
2128 { __r._M_ptr =
nullptr; }
2130 template<
typename _Yp,
typename = _Compatible<_Yp>>
2131 __weak_ptr(__weak_ptr<_Yp, _Lp>&& __r) noexcept
2132 : _M_ptr(_S_safe_upcast(__r)), _M_refcount(
std::move(__r._M_refcount))
2133 { __r._M_ptr =
nullptr; }
2136 operator=(
const __weak_ptr& __r)
noexcept =
default;
2138 template<
typename _Yp>
2140 operator=(
const __weak_ptr<_Yp, _Lp>& __r)
noexcept
2142 _M_ptr = _S_safe_upcast(__r);
2143 _M_refcount = __r._M_refcount;
2147 template<
typename _Yp>
2149 operator=(
const __shared_ptr<_Yp, _Lp>& __r)
noexcept
2151 _M_ptr = __r._M_ptr;
2152 _M_refcount = __r._M_refcount;
2157 operator=(__weak_ptr&& __r)
noexcept
2163 template<
typename _Yp>
2165 operator=(__weak_ptr<_Yp, _Lp>&& __r)
noexcept
2167 _M_ptr = _S_safe_upcast(__r);
2168 _M_refcount =
std::move(__r._M_refcount);
2169 __r._M_ptr =
nullptr;
2173 __shared_ptr<_Tp, _Lp>
2174 lock() const noexcept
2175 {
return __shared_ptr<_Tp, _Lp>(*
this, std::nothrow); }
2178 use_count() const noexcept
2179 {
return _M_refcount._M_get_use_count(); }
2182 expired() const noexcept
2183 {
return _M_refcount._M_get_use_count() == 0; }
2185 template<
typename _Tp1>
2187 owner_before(
const __shared_ptr<_Tp1, _Lp>& __rhs)
const noexcept
2188 {
return _M_refcount._M_less(__rhs._M_refcount); }
2190 template<
typename _Tp1>
2192 owner_before(
const __weak_ptr<_Tp1, _Lp>& __rhs)
const noexcept
2193 {
return _M_refcount._M_less(__rhs._M_refcount); }
2195#ifdef __glibcxx_smart_ptr_owner_equality
2196 size_t owner_hash() const noexcept {
return _M_refcount._M_owner_hash(); }
2198 template<
typename _Tp1>
2200 owner_equal(
const __shared_ptr<_Tp1, _Lp> & __rhs)
const noexcept
2201 {
return _M_refcount == __rhs._M_refcount; }
2203 template<
typename _Tp1>
2205 owner_equal(
const __weak_ptr<_Tp1, _Lp> & __rhs)
const noexcept
2206 {
return _M_refcount == __rhs._M_refcount; }
2211 { __weak_ptr().swap(*
this); }
2214 swap(__weak_ptr& __s)
noexcept
2216 std::swap(_M_ptr, __s._M_ptr);
2217 _M_refcount._M_swap(__s._M_refcount);
2223 _M_assign(_Tp* __ptr,
const __shared_count<_Lp>& __refcount)
noexcept
2225 if (use_count() == 0)
2228 _M_refcount = __refcount;
2232 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
2233 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
2234 friend class __enable_shared_from_this<_Tp, _Lp>;
2236#ifdef __glibcxx_atomic_shared_ptr
2237 friend _Sp_atomic<weak_ptr<_Tp>>;
2240 element_type* _M_ptr;
2241 __weak_count<_Lp> _M_refcount;
2245 template<
typename _Tp, _Lock_policy _Lp>
2247 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b)
noexcept
2250#pragma GCC diagnostic push
2251#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
2252 template<
typename _Tp,
typename _Tp1>
2256 operator()(
const _Tp& __lhs,
const _Tp& __rhs)
const noexcept
2257 {
return __lhs.owner_before(__rhs); }
2260 operator()(
const _Tp& __lhs,
const _Tp1& __rhs)
const noexcept
2261 {
return __lhs.owner_before(__rhs); }
2264 operator()(
const _Tp1& __lhs,
const _Tp& __rhs)
const noexcept
2265 {
return __lhs.owner_before(__rhs); }
2267#pragma GCC diagnostic pop
2270 struct _Sp_owner_less<void, void>
2272 template<
typename _Tp,
typename _Up>
2274 operator()(
const _Tp& __lhs,
const _Up& __rhs)
const noexcept
2275 ->
decltype(__lhs.owner_before(__rhs))
2276 {
return __lhs.owner_before(__rhs); }
2278 using is_transparent = void;
2281 template<
typename _Tp, _Lock_policy _Lp>
2283 :
public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
2286 template<
typename _Tp, _Lock_policy _Lp>
2288 :
public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
2292 template<
typename _Tp, _Lock_policy _Lp>
2293 class __enable_shared_from_this
2296 constexpr __enable_shared_from_this() noexcept { }
2298 __enable_shared_from_this(
const __enable_shared_from_this&)
noexcept { }
2300 __enable_shared_from_this&
2301 operator=(
const __enable_shared_from_this&)
noexcept
2304 ~__enable_shared_from_this() { }
2307 __shared_ptr<_Tp, _Lp>
2309 {
return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
2311 __shared_ptr<const _Tp, _Lp>
2312 shared_from_this()
const
2313 {
return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
2315#if __cplusplus > 201402L || !defined(__STRICT_ANSI__)
2316 __weak_ptr<_Tp, _Lp>
2317 weak_from_this() noexcept
2318 {
return this->_M_weak_this; }
2320 __weak_ptr<const _Tp, _Lp>
2321 weak_from_this() const noexcept
2322 {
return this->_M_weak_this; }
2326 template<
typename _Tp1>
2328 _M_weak_assign(_Tp1* __p,
const __shared_count<_Lp>& __n)
const noexcept
2329 { _M_weak_this._M_assign(__p, __n); }
2331 friend const __enable_shared_from_this*
2332 __enable_shared_from_this_base(
const __shared_count<_Lp>&,
2333 const __enable_shared_from_this* __p)
2336 template<
typename, _Lock_policy>
2337 friend class __shared_ptr;
2339 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
2342 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy,
2343 typename _Alloc,
typename... _Args>
2344 inline __shared_ptr<_Tp, _Lp>
2345 __allocate_shared(
const _Alloc& __a, _Args&&... __args)
2349 return __shared_ptr<_Tp, _Lp>(_Sp_alloc_shared_tag<_Alloc>{__a},
2353 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy,
2355 inline __shared_ptr<_Tp, _Lp>
2356 __make_shared(_Args&&... __args)
2358 typedef typename std::remove_const<_Tp>::type _Tp_nc;
2359 return std::__allocate_shared<_Tp, _Lp>(std::allocator<_Tp_nc>(),
2364 template<
typename _Tp, _Lock_policy _Lp>
2365 struct hash<__shared_ptr<_Tp, _Lp>>
2366 :
public __hash_base<size_t, __shared_ptr<_Tp, _Lp>>
2369 operator()(
const __shared_ptr<_Tp, _Lp>& __s)
const noexcept
2376_GLIBCXX_END_NAMESPACE_VERSION
constexpr complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
constexpr _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.
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
__bool_constant< false > false_type
The type used as a compile-time boolean with false value.
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 * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
void lock(_L1 &__l1, _L2 &__l2, _L3 &... __l3)
Generic lock.
ISO C++ entities toplevel namespace is std.
__shared_ptr< _Tp, _Lp > dynamic_pointer_cast(const __shared_ptr< _Tp1, _Lp > &__r) noexcept
dynamic_pointer_cast
__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
constexpr _Iterator __base(_Iterator __it)
Primary class template hash.
Define a member typedef type only if a boolean constant is true.
static constexpr void construct(_Alloc &__a, _Tp *__p, _Args &&... __args) noexcept(_S_nothrow_construct< _Tp, _Args... >())
Construct an object of type _Tp.
static constexpr void destroy(_Alloc &__a, _Tp *__p) noexcept(_S_nothrow_destroy< _Tp >())
Destroy an object of type _Tp.
Base class for all library exceptions.
Primary template owner_less.
Base class allowing use of the member function shared_from_this.
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.