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 { }
521 template<
int _Nm,
typename _Tp,
522 bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
523 struct _Sp_ebo_helper;
526 template<
int _Nm,
typename _Tp>
527 struct _Sp_ebo_helper<_Nm, _Tp, true> :
private _Tp
529 explicit _Sp_ebo_helper(
const _Tp& __tp) : _Tp(__tp) { }
530 explicit _Sp_ebo_helper(_Tp&& __tp) : _Tp(
std::move(__tp)) { }
533 _S_get(_Sp_ebo_helper& __eboh) {
return static_cast<_Tp&
>(__eboh); }
537 template<
int _Nm,
typename _Tp>
538 struct _Sp_ebo_helper<_Nm, _Tp, false>
540 explicit _Sp_ebo_helper(
const _Tp& __tp) : _M_tp(__tp) { }
541 explicit _Sp_ebo_helper(_Tp&& __tp) : _M_tp(
std::move(__tp)) { }
544 _S_get(_Sp_ebo_helper& __eboh)
545 {
return __eboh._M_tp; }
552 template<
typename _Ptr,
typename _Deleter,
typename _Alloc, _Lock_policy _Lp>
553 class _Sp_counted_deleter final :
public _Sp_counted_base<_Lp>
555 class _Impl : _Sp_ebo_helper<0, _Deleter>, _Sp_ebo_helper<1, _Alloc>
557 typedef _Sp_ebo_helper<0, _Deleter> _Del_base;
558 typedef _Sp_ebo_helper<1, _Alloc> _Alloc_base;
561 _Impl(_Ptr __p, _Deleter __d,
const _Alloc& __a) noexcept
562 : _Del_base(
std::move(__d)), _Alloc_base(__a), _M_ptr(__p)
565 _Deleter& _M_del() noexcept {
return _Del_base::_S_get(*
this); }
566 _Alloc& _M_alloc() noexcept {
return _Alloc_base::_S_get(*
this); }
572 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_deleter>;
575 _Sp_counted_deleter(_Ptr __p, _Deleter __d) noexcept
576 : _M_impl(__p,
std::move(__d), _Alloc()) { }
579 _Sp_counted_deleter(_Ptr __p, _Deleter __d,
const _Alloc& __a) noexcept
582#pragma GCC diagnostic push
583#pragma GCC diagnostic ignored "-Wfree-nonheap-object"
584 template<
typename>
class auto_ptr;
585 ~_Sp_counted_deleter() noexcept { }
586#pragma GCC diagnostic pop
589 _M_dispose() noexcept
590 { _M_impl._M_del()(_M_impl._M_ptr); }
593 _M_destroy() noexcept
595 __allocator_type __a(_M_impl._M_alloc());
596 __allocated_ptr<__allocator_type> __guard_ptr{ __a,
this };
597 this->~_Sp_counted_deleter();
601 _M_get_deleter(
const type_info& __ti [[__gnu__::__unused__]])
noexcept
606 return __ti ==
typeid(_Deleter)
615#ifdef __glibcxx_out_ptr
616 template<
typename,
typename,
typename...>
friend class out_ptr_t;
623 struct _Sp_make_shared_tag
626 template<
typename _Tp,
typename _Alloc, _Lock_policy _Lp>
627 friend class _Sp_counted_ptr_inplace;
629 static const type_info&
630 _S_ti() noexcept _GLIBCXX_VISIBILITY(default)
632 alignas(type_info)
static constexpr char __tag[
sizeof(type_info)] = { };
633 return reinterpret_cast<const type_info&
>(__tag);
636 static bool _S_eq(
const type_info&)
noexcept;
639 template<
typename _Alloc>
640 struct _Sp_alloc_shared_tag
645 template<
typename _Tp,
typename _Alloc, _Lock_policy _Lp>
646 class _Sp_counted_ptr_inplace final :
public _Sp_counted_base<_Lp>
648 class _Impl : _Sp_ebo_helper<0, _Alloc>
650 typedef _Sp_ebo_helper<0, _Alloc> _A_base;
653 explicit _Impl(_Alloc __a) noexcept : _A_base(__a) { }
655 _Alloc& _M_alloc() noexcept {
return _A_base::_S_get(*
this); }
657 __gnu_cxx::__aligned_buffer<__remove_cv_t<_Tp>> _M_storage;
661 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>;
664 template<
typename... _Args>
665 _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
674#pragma GCC diagnostic push
675#pragma GCC diagnostic ignored "-Warray-bounds"
676 ~_Sp_counted_ptr_inplace() noexcept { }
677#pragma GCC diagnostic pop
680 _M_dispose() noexcept
687 _M_destroy() noexcept
689 __allocator_type __a(_M_impl._M_alloc());
690 __allocated_ptr<__allocator_type> __guard_ptr{ __a,
this };
691 this->~_Sp_counted_ptr_inplace();
695 friend class __shared_count<_Lp>;
700 _M_get_deleter(
const std::type_info& __ti)
noexcept override
706 if (&__ti == &_Sp_make_shared_tag::_S_ti()
709 __ti ==
typeid(_Sp_make_shared_tag)
711 _Sp_make_shared_tag::_S_eq(__ti)
719 _M_ptr() noexcept {
return _M_impl._M_storage._M_ptr(); }
724#ifdef __glibcxx_smart_ptr_for_overwrite
725 struct _Sp_overwrite_tag { };
730 template<
typename _Tp,
typename _Alloc, _Lock_policy _Lp>
731 requires is_same_v<typename _Alloc::value_type, _Sp_overwrite_tag>
732 class _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> final
733 :
public _Sp_counted_base<_Lp>
735 [[no_unique_address]] _Alloc _M_alloc;
738 remove_cv_t<_Tp> _M_obj;
742 friend class __shared_count<_Lp>;
747 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>;
749 _Sp_counted_ptr_inplace(
const _Alloc& __a)
752 ::new((
void*)_M_ptr()) _Tp;
755 ~_Sp_counted_ptr_inplace() noexcept { }
758 _M_dispose() noexcept
765 _M_destroy() noexcept
767 using pointer =
typename allocator_traits<__allocator_type>::pointer;
768 __allocator_type __a(_M_alloc);
769 auto __p = pointer_traits<pointer>::pointer_to(*
this);
770 __allocated_ptr<__allocator_type> __guard_ptr{ __a, __p };
771 this->~_Sp_counted_ptr_inplace();
775 _M_get_deleter(
const std::type_info&)
noexcept override
780#if __glibcxx_shared_ptr_arrays >= 201707L
781 struct _Sp_overwrite_tag;
784 template<
typename _Alloc>
785 struct _Sp_counted_array_base
787 [[no_unique_address]] _Alloc _M_alloc{};
789 bool _M_overwrite =
false;
791 typename allocator_traits<_Alloc>::pointer
792 _M_alloc_array(
size_t __tail)
794 return allocator_traits<_Alloc>::allocate(_M_alloc, _M_n + __tail);
798 _M_dealloc_array(
typename allocator_traits<_Alloc>::pointer __p,
801 allocator_traits<_Alloc>::deallocate(_M_alloc, __p, _M_n + __tail);
805 template<
typename _Init>
807 _M_init(
typename allocator_traits<_Alloc>::value_type* __p,
810 using _Tp = remove_pointer_t<_Init>;
811 using _Up =
typename allocator_traits<_Alloc>::value_type;
813 if constexpr (is_same_v<_Init, _Sp_overwrite_tag>)
818 else if (__init ==
nullptr)
819 std::__uninitialized_default_n_a(__p, _M_n, _M_alloc);
820 else if constexpr (!is_array_v<_Tp>)
821 std::__uninitialized_fill_n_a(__p, _M_n, *__init, _M_alloc);
824#pragma GCC diagnostic push
825#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
828 using value_type = _Up;
829 using difference_type = ptrdiff_t;
830 using pointer =
const _Up*;
831 using reference =
const _Up&;
832 using iterator_category = forward_iterator_tag;
838 _Iter& operator++() { ++_M_pos;
return *
this; }
839 _Iter operator++(
int) {
auto __i(*
this); ++_M_pos;
return __i; }
841 reference
operator*()
const {
return _M_p[_M_pos % _M_len]; }
842 pointer operator->()
const {
return _M_p + (_M_pos % _M_len); }
844 bool operator==(
const _Iter& __i)
const
845 {
return _M_pos == __i._M_pos; }
847#pragma GCC diagnostic pop
849 _Iter __first{_S_first_elem(__init),
sizeof(_Tp) /
sizeof(_Up)};
850 _Iter __last = __first;
851 __last._M_pos = _M_n;
852 std::__uninitialized_copy_a(__first, __last, __p, _M_alloc);
859 _M_dispose_array(
typename allocator_traits<_Alloc>::value_type* __p)
862 std::destroy_n(__p, _M_n);
867 allocator_traits<_Alloc>::destroy(_M_alloc, __p + __n);
872 template<
typename _Tp>
874 _S_first_elem(_Tp* __p) {
return __p; }
876 template<
typename _Tp,
size_t _Nm>
878 _S_first_elem(_Tp (*__p)[_Nm]) {
return _S_first_elem(*__p); }
883 template<
typename _Alloc, _Lock_policy _Lp>
884 class _Sp_counted_array final
885 :
public _Sp_counted_base<_Lp>, _Sp_counted_array_base<_Alloc>
887 using pointer =
typename allocator_traits<_Alloc>::pointer;
889 pointer _M_alloc_ptr;
893 friend class __shared_count<_Lp>;
896 _Sp_counted_array(
const _Sp_counted_array_base<_Alloc>& __a,
897 pointer __p) noexcept
898 : _Sp_counted_array_base<_Alloc>(__a), _M_alloc_ptr(__p)
901 ~_Sp_counted_array() =
default;
904 _M_dispose() noexcept
907 this->_M_dispose_array(_M_ptr());
912 _M_destroy() noexcept
914 _Sp_counted_array_base<_Alloc> __a = *
this;
915 pointer __p = _M_alloc_ptr;
916 this->~_Sp_counted_array();
917 __a._M_dealloc_array(__p, _S_tail());
922 static constexpr size_t
926 using _Tp =
typename allocator_traits<_Alloc>::value_type;
929 size_t __bytes =
sizeof(_Sp_counted_array);
932 if constexpr (
alignof(_Tp) <
alignof(_Sp_counted_array))
933 __bytes +=
alignof(_Sp_counted_array) -
alignof(_Tp);
935 return (__bytes +
sizeof(_Tp) - 1) /
sizeof(_Tp);
939 _M_get_deleter(
const std::type_info&)
noexcept override
945 struct __sp_array_delete
947 template<
typename _Yp>
948 void operator()(_Yp* __p)
const {
delete[] __p; }
951 template<_Lock_policy _Lp>
955 template<
typename _Tp>
956 struct __not_alloc_shared_tag {
using type = void; };
958 template<
typename _Tp>
959 struct __not_alloc_shared_tag<_Sp_alloc_shared_tag<_Tp>> { };
961#if __glibcxx_shared_ptr_arrays >= 201707L
962 template<
typename _Alloc>
963 struct __not_alloc_shared_tag<_Sp_counted_array_base<_Alloc>> { };
967 constexpr __shared_count() noexcept : _M_pi(0)
970 template<
typename _Ptr>
972 __shared_count(_Ptr __p) : _M_pi(0)
976 _M_pi =
new _Sp_counted_ptr<_Ptr, _Lp>(__p);
981 __throw_exception_again;
985 template<
typename _Ptr>
986 __shared_count(_Ptr __p, false_type)
987 : __shared_count(__p)
990 template<
typename _Ptr>
991 __shared_count(_Ptr __p, true_type)
992 : __shared_count(__p, __sp_array_delete{}, allocator<void>())
995 template<
typename _Ptr,
typename _Deleter,
996 typename =
typename __not_alloc_shared_tag<_Deleter>::type>
997 __shared_count(_Ptr __p, _Deleter __d)
998 : __shared_count(__p, std::move(__d), allocator<void>())
1001 template<
typename _Ptr,
typename _Deleter,
typename _Alloc,
1002 typename =
typename __not_alloc_shared_tag<_Deleter>::type>
1003 __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
1005 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
1008 typename _Sp_cd_type::__allocator_type __a2(__a);
1009 auto __guard = std::__allocate_guarded(__a2);
1010 _Sp_cd_type* __mem = __guard.get();
1018 __throw_exception_again;
1022 template<
typename _Tp,
typename _Alloc,
typename... _Args>
1023 __shared_count(_Tp*& __p, _Sp_alloc_shared_tag<_Alloc> __a,
1026 using _Tp2 = __remove_cv_t<_Tp>;
1027 using _Sp_cp_type = _Sp_counted_ptr_inplace<_Tp2, _Alloc, _Lp>;
1028 typename _Sp_cp_type::__allocator_type __a2(__a._M_a);
1029 auto __guard = std::__allocate_guarded(__a2);
1030 _Sp_cp_type* __mem = __guard.get();
1031 auto __pi = ::new (__mem)
1035 __p = __pi->_M_ptr();
1038#if __glibcxx_shared_ptr_arrays >= 201707L
1039 template<
typename _Tp,
typename _Alloc,
typename _Init>
1040 __shared_count(_Tp*& __p,
const _Sp_counted_array_base<_Alloc>& __a,
1043 using _Up = remove_all_extents_t<_Tp>;
1044 static_assert(is_same_v<_Up, typename _Alloc::value_type>);
1046 using _Sp_ca_type = _Sp_counted_array<_Alloc, _Lp>;
1047 const size_t __tail = _Sp_ca_type::_S_tail();
1049 struct _Guarded_ptr : _Sp_counted_array_base<_Alloc>
1051 typename allocator_traits<_Alloc>::pointer _M_ptr;
1053 _Guarded_ptr(_Sp_counted_array_base<_Alloc> __a)
1054 : _Sp_counted_array_base<_Alloc>(__a),
1055 _M_ptr(this->_M_alloc_array(_Sp_ca_type::_S_tail()))
1061 this->_M_dealloc_array(_M_ptr, _Sp_ca_type::_S_tail());
1065 _Guarded_ptr __guard{__a};
1067 __guard._M_init(__raw, __init);
1069 void* __c = __raw + __a._M_n;
1070 if constexpr (
alignof(_Up) <
alignof(_Sp_ca_type))
1072 size_t __space =
sizeof(_Up) * __tail;
1073 __c =
std::align(
alignof(_Sp_ca_type),
sizeof(_Sp_ca_type),
1076 auto __pi = ::new(__c) _Sp_ca_type(__guard, __guard._M_ptr);
1077 __guard._M_ptr =
nullptr;
1079 __p =
reinterpret_cast<_Tp*
>(__raw);
1083#if _GLIBCXX_USE_DEPRECATED
1084#pragma GCC diagnostic push
1085#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1087 template<
typename _Tp>
1089 __shared_count(std::auto_ptr<_Tp>&& __r);
1090#pragma GCC diagnostic pop
1094 template<
typename _Tp,
typename _Del>
1096 __shared_count(std::unique_ptr<_Tp, _Del>&& __r) : _M_pi(0)
1100 if (__r.get() ==
nullptr)
1103 using _Ptr =
typename unique_ptr<_Tp, _Del>::pointer;
1104 using _Del2 = __conditional_t<is_reference<_Del>::value,
1105 reference_wrapper<
typename remove_reference<_Del>::type>,
1108 = _Sp_counted_deleter<_Ptr, _Del2, allocator<void>, _Lp>;
1109 using _Alloc = allocator<_Sp_cd_type>;
1110 using _Alloc_traits = allocator_traits<_Alloc>;
1112 _Sp_cd_type* __mem = _Alloc_traits::allocate(__a, 1);
1116 _Alloc_traits::construct(__a, __mem, __r.release(),
1122 explicit __shared_count(
const __weak_count<_Lp>& __r);
1126 __shared_count(
const __weak_count<_Lp>& __r, std::nothrow_t)
noexcept;
1128 ~__shared_count() noexcept
1130 if (_M_pi !=
nullptr)
1131 _M_pi->_M_release();
1134 __shared_count(
const __shared_count& __r) noexcept
1137 if (_M_pi !=
nullptr)
1138 _M_pi->_M_add_ref_copy();
1142 operator=(
const __shared_count& __r)
noexcept
1144 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1147 if (__tmp !=
nullptr)
1148 __tmp->_M_add_ref_copy();
1149 if (_M_pi !=
nullptr)
1150 _M_pi->_M_release();
1157 _M_swap(__shared_count& __r)
noexcept
1159 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1165 _M_get_use_count() const noexcept
1166 {
return _M_pi ? _M_pi->_M_get_use_count() : 0; }
1169 _M_unique() const noexcept
1170 {
return this->_M_get_use_count() == 1; }
1173 _M_get_deleter(
const std::type_info& __ti)
const noexcept
1174 {
return _M_pi ? _M_pi->_M_get_deleter(__ti) :
nullptr; }
1177 _M_less(
const __shared_count& __rhs)
const noexcept
1178 {
return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
1181 _M_less(
const __weak_count<_Lp>& __rhs)
const noexcept
1182 {
return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
1184#ifdef __glibcxx_smart_ptr_owner_equality
1186 _M_owner_hash() const noexcept
1187 {
return std::hash<_Sp_counted_base<_Lp>*>()(this->_M_pi); }
1192 operator==(
const __shared_count& __a,
const __shared_count& __b)
noexcept
1193 {
return __a._M_pi == __b._M_pi; }
1196 friend class __weak_count<_Lp>;
1197#ifdef __glibcxx_atomic_shared_ptr
1198 template<
typename>
friend class _Sp_atomic;
1200#ifdef __glibcxx_out_ptr
1201 template<
typename,
typename,
typename...>
friend class out_ptr_t;
1204 _Sp_counted_base<_Lp>* _M_pi;
1208 template<_Lock_policy _Lp>
1212 constexpr __weak_count() noexcept : _M_pi(
nullptr)
1215 __weak_count(
const __shared_count<_Lp>& __r) noexcept
1218 if (_M_pi !=
nullptr)
1219 _M_pi->_M_weak_add_ref();
1222 __weak_count(
const __weak_count& __r) noexcept
1225 if (_M_pi !=
nullptr)
1226 _M_pi->_M_weak_add_ref();
1229 __weak_count(__weak_count&& __r) noexcept
1231 { __r._M_pi =
nullptr; }
1233 ~__weak_count() noexcept
1235 if (_M_pi !=
nullptr)
1236 _M_pi->_M_weak_release();
1240 operator=(
const __shared_count<_Lp>& __r)
noexcept
1242 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1243 if (__tmp !=
nullptr)
1244 __tmp->_M_weak_add_ref();
1245 if (_M_pi !=
nullptr)
1246 _M_pi->_M_weak_release();
1252 operator=(
const __weak_count& __r)
noexcept
1254 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1255 if (__tmp !=
nullptr)
1256 __tmp->_M_weak_add_ref();
1257 if (_M_pi !=
nullptr)
1258 _M_pi->_M_weak_release();
1264 operator=(__weak_count&& __r)
noexcept
1266 if (_M_pi !=
nullptr)
1267 _M_pi->_M_weak_release();
1269 __r._M_pi =
nullptr;
1274 _M_swap(__weak_count& __r)
noexcept
1276 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1282 _M_get_use_count() const noexcept
1283 {
return _M_pi !=
nullptr ? _M_pi->_M_get_use_count() : 0; }
1286 _M_less(
const __weak_count& __rhs)
const noexcept
1287 {
return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
1290 _M_less(
const __shared_count<_Lp>& __rhs)
const noexcept
1291 {
return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
1293#ifdef __glibcxx_smart_ptr_owner_equality
1295 _M_owner_hash() const noexcept
1296 {
return std::hash<_Sp_counted_base<_Lp>*>()(this->_M_pi); }
1301 operator==(
const __weak_count& __a,
const __weak_count& __b)
noexcept
1302 {
return __a._M_pi == __b._M_pi; }
1305 friend class __shared_count<_Lp>;
1306#ifdef __glibcxx_atomic_shared_ptr
1307 template<
typename>
friend class _Sp_atomic;
1310 _Sp_counted_base<_Lp>* _M_pi;
1314 template<_Lock_policy _Lp>
1316 __shared_count<_Lp>::__shared_count(
const __weak_count<_Lp>& __r)
1319 if (_M_pi ==
nullptr || !_M_pi->_M_add_ref_lock_nothrow())
1320 __throw_bad_weak_ptr();
1324 template<_Lock_policy _Lp>
1326 __shared_count<_Lp>::
1327 __shared_count(
const __weak_count<_Lp>& __r, std::nothrow_t) noexcept
1330 if (_M_pi && !_M_pi->_M_add_ref_lock_nothrow())
1338 template<
typename _Yp_ptr,
typename _Tp_ptr>
1339 struct __sp_compatible_with
1343 template<
typename _Yp,
typename _Tp>
1344 struct __sp_compatible_with<_Yp*, _Tp*>
1345 : is_convertible<_Yp*, _Tp*>::type
1348 template<
typename _Up,
size_t _Nm>
1349 struct __sp_compatible_with<_Up(*)[_Nm], _Up(*)[]>
1353 template<
typename _Up,
size_t _Nm>
1354 struct __sp_compatible_with<_Up(*)[_Nm],
const _Up(*)[]>
1358 template<
typename _Up,
size_t _Nm>
1359 struct __sp_compatible_with<_Up(*)[_Nm],
volatile _Up(*)[]>
1363 template<
typename _Up,
size_t _Nm>
1364 struct __sp_compatible_with<_Up(*)[_Nm],
const volatile _Up(*)[]>
1369 template<
typename _Up,
size_t _Nm,
typename _Yp,
typename =
void>
1370 struct __sp_is_constructible_arrN
1374 template<
typename _Up,
size_t _Nm,
typename _Yp>
1375 struct __sp_is_constructible_arrN<_Up, _Nm, _Yp, __void_t<_Yp[_Nm]>>
1376 : is_convertible<_Yp(*)[_Nm], _Up(*)[_Nm]>::type
1380 template<
typename _Up,
typename _Yp,
typename =
void>
1381 struct __sp_is_constructible_arr
1385 template<
typename _Up,
typename _Yp>
1386 struct __sp_is_constructible_arr<_Up, _Yp, __void_t<_Yp[]>>
1387 : is_convertible<_Yp(*)[], _Up(*)[]>::type
1391 template<
typename _Tp,
typename _Yp>
1392 struct __sp_is_constructible;
1395 template<
typename _Up,
size_t _Nm,
typename _Yp>
1396 struct __sp_is_constructible<_Up[_Nm], _Yp>
1397 : __sp_is_constructible_arrN<_Up, _Nm, _Yp>::type
1401 template<
typename _Up,
typename _Yp>
1402 struct __sp_is_constructible<_Up[], _Yp>
1403 : __sp_is_constructible_arr<_Up, _Yp>::type
1407 template<
typename _Tp,
typename _Yp>
1408 struct __sp_is_constructible
1409 : is_convertible<_Yp*, _Tp*>::type
1413 template<
typename _Tp>
1414 [[__gnu__::__always_inline__]]
1416 __shared_ptr_deref(_Tp* __p)
1418 __glibcxx_assert(__p !=
nullptr);
1423 template<
typename _Tp, _Lock_policy _Lp,
1425 class __shared_ptr_access
1428 using element_type = _Tp;
1431 operator*() const noexcept
1432 {
return *std::__shared_ptr_deref(_M_get()); }
1435 operator->() const noexcept
1437 _GLIBCXX_DEBUG_PEDASSERT(_M_get() !=
nullptr);
1443 _M_get() const noexcept
1444 {
return static_cast<const __shared_ptr<_Tp, _Lp>*
>(
this)->get(); }
1448 template<
typename _Tp, _Lock_policy _Lp>
1449 class __shared_ptr_access<_Tp, _Lp, false, true>
1452 using element_type = _Tp;
1455 operator->() const noexcept
1457 auto __ptr =
static_cast<const __shared_ptr<_Tp, _Lp>*
>(
this)->get();
1458 _GLIBCXX_DEBUG_PEDASSERT(__ptr !=
nullptr);
1464 template<
typename _Tp, _Lock_policy _Lp>
1465 class __shared_ptr_access<_Tp, _Lp, true, false>
1468 using element_type =
typename remove_extent<_Tp>::type;
1470#if __cplusplus <= 201402L
1471 [[__deprecated__(
"shared_ptr<T[]>::operator* is absent from C++17")]]
1473 operator*() const noexcept
1474 {
return *std::__shared_ptr_deref(_M_get()); }
1476 [[__deprecated__(
"shared_ptr<T[]>::operator-> is absent from C++17")]]
1478 operator->() const noexcept
1480 _GLIBCXX_DEBUG_PEDASSERT(_M_get() !=
nullptr);
1485#pragma GCC diagnostic push
1486#pragma GCC diagnostic ignored "-Wc++17-extensions"
1488 operator[](ptrdiff_t __i)
const noexcept
1490 if constexpr (extent<_Tp>::value)
1491 __glibcxx_assert(__i < extent<_Tp>::value);
1492 return std::__shared_ptr_deref(_M_get())[__i];
1494#pragma GCC diagnostic pop
1498 _M_get() const noexcept
1499 {
return static_cast<const __shared_ptr<_Tp, _Lp>*
>(
this)->get(); }
1502 template<
typename _Tp, _Lock_policy _Lp>
1504 :
public __shared_ptr_access<_Tp, _Lp>
1507 using element_type =
typename remove_extent<_Tp>::type;
1511 template<
typename _Yp>
1513 =
typename enable_if<__sp_is_constructible<_Tp, _Yp>::value>::type;
1516 template<
typename _Yp,
typename _Res =
void>
1517 using _Compatible =
typename
1518 enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type;
1521 template<
typename _Yp>
1522 using _Assignable = _Compatible<_Yp, __shared_ptr&>;
1525 template<
typename _Yp,
typename _Del,
typename _Res = void,
1526 typename _Ptr =
typename unique_ptr<_Yp, _Del>::pointer>
1527 using _UniqCompatible = __enable_if_t<__and_<
1528 __sp_compatible_with<_Yp*, _Tp*>,
1529 is_convertible<_Ptr, element_type*>,
1530 is_move_constructible<_Del>
1534 template<
typename _Yp,
typename _Del>
1535 using _UniqAssignable = _UniqCompatible<_Yp, _Del, __shared_ptr&>;
1539#if __cplusplus > 201402L
1540 using weak_type = __weak_ptr<_Tp, _Lp>;
1543 constexpr __shared_ptr() noexcept
1544 : _M_ptr(0), _M_refcount()
1547 template<
typename _Yp,
typename = _SafeConv<_Yp>>
1549 __shared_ptr(_Yp* __p)
1550 : _M_ptr(__p), _M_refcount(__p, typename is_array<_Tp>::type())
1552 static_assert( !is_void<_Yp>::value,
"incomplete type" );
1553 static_assert(
sizeof(_Yp) > 0,
"incomplete type" );
1554 _M_enable_shared_from_this_with(__p);
1557 template<
typename _Yp,
typename _Deleter,
typename = _SafeConv<_Yp>>
1558 __shared_ptr(_Yp* __p, _Deleter __d)
1559 : _M_ptr(__p), _M_refcount(__p, std::
move(__d))
1561 static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
1562 "deleter expression d(p) is well-formed");
1563 _M_enable_shared_from_this_with(__p);
1566 template<
typename _Yp,
typename _Deleter,
typename _Alloc,
1567 typename = _SafeConv<_Yp>>
1568 __shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a)
1569 : _M_ptr(__p), _M_refcount(__p, std::
move(__d), std::
move(__a))
1571 static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
1572 "deleter expression d(p) is well-formed");
1573 _M_enable_shared_from_this_with(__p);
1576 template<
typename _Deleter>
1577 __shared_ptr(nullptr_t __p, _Deleter __d)
1578 : _M_ptr(0), _M_refcount(__p, std::
move(__d))
1581 template<
typename _Deleter,
typename _Alloc>
1582 __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
1583 : _M_ptr(0), _M_refcount(__p, std::
move(__d), std::
move(__a))
1587 template<
typename _Yp>
1588 __shared_ptr(
const __shared_ptr<_Yp, _Lp>& __r,
1589 element_type* __p) noexcept
1590 : _M_ptr(__p), _M_refcount(__r._M_refcount)
1594 template<
typename _Yp>
1595 __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r,
1596 element_type* __p) noexcept
1597 : _M_ptr(__p), _M_refcount()
1599 _M_refcount._M_swap(__r._M_refcount);
1600 __r._M_ptr =
nullptr;
1603 __shared_ptr(
const __shared_ptr&)
noexcept =
default;
1604 __shared_ptr& operator=(
const __shared_ptr&)
noexcept =
default;
1605 ~__shared_ptr() =
default;
1607 template<
typename _Yp,
typename = _Compatible<_Yp>>
1608 __shared_ptr(
const __shared_ptr<_Yp, _Lp>& __r) noexcept
1609 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1612 __shared_ptr(__shared_ptr&& __r) noexcept
1613 : _M_ptr(__r._M_ptr), _M_refcount()
1615 _M_refcount._M_swap(__r._M_refcount);
1616 __r._M_ptr =
nullptr;
1619 template<
typename _Yp,
typename = _Compatible<_Yp>>
1620 __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r) noexcept
1621 : _M_ptr(__r._M_ptr), _M_refcount()
1623 _M_refcount._M_swap(__r._M_refcount);
1624 __r._M_ptr =
nullptr;
1627 template<
typename _Yp,
typename = _Compatible<_Yp>>
1628 explicit __shared_ptr(
const __weak_ptr<_Yp, _Lp>& __r)
1629 : _M_refcount(__r._M_refcount)
1633 _M_ptr = __r._M_ptr;
1637 template<
typename _Yp,
typename _Del,
1638 typename = _UniqCompatible<_Yp, _Del>>
1639 __shared_ptr(unique_ptr<_Yp, _Del>&& __r)
1640 : _M_ptr(__r.get()), _M_refcount()
1642 auto __raw = std::__to_address(__r.get());
1643 _M_refcount = __shared_count<_Lp>(
std::move(__r));
1644 _M_enable_shared_from_this_with(__raw);
1647#if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED
1650 template<
typename _Tp1,
typename _Del,
1651 typename enable_if<__and_<
1652 __not_<is_array<_Tp>>, is_array<_Tp1>,
1653 is_convertible<typename unique_ptr<_Tp1, _Del>::pointer, _Tp*>
1654 >::value,
bool>::type =
true>
1655 __shared_ptr(unique_ptr<_Tp1, _Del>&& __r, __sp_array_delete)
1656 : _M_ptr(__r.get()), _M_refcount()
1658 auto __raw = std::__to_address(__r.get());
1659 _M_refcount = __shared_count<_Lp>(
std::move(__r));
1660 _M_enable_shared_from_this_with(__raw);
1665#if _GLIBCXX_USE_DEPRECATED
1666#pragma GCC diagnostic push
1667#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1669 template<
typename _Yp,
typename = _Compatible<_Yp>>
1670 __shared_ptr(auto_ptr<_Yp>&& __r);
1671#pragma GCC diagnostic pop
1674 constexpr __shared_ptr(nullptr_t) noexcept : __shared_ptr() { }
1676 template<
typename _Yp>
1678 operator=(
const __shared_ptr<_Yp, _Lp>& __r)
noexcept
1680 _M_ptr = __r._M_ptr;
1681 _M_refcount = __r._M_refcount;
1685#if _GLIBCXX_USE_DEPRECATED
1686#pragma GCC diagnostic push
1687#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1688 template<
typename _Yp>
1690 operator=(auto_ptr<_Yp>&& __r)
1692 __shared_ptr(
std::move(__r)).swap(*
this);
1695#pragma GCC diagnostic pop
1699 operator=(__shared_ptr&& __r)
noexcept
1701 __shared_ptr(
std::move(__r)).swap(*
this);
1707 operator=(__shared_ptr<_Yp, _Lp>&& __r)
noexcept
1709 __shared_ptr(
std::move(__r)).swap(*
this);
1713 template<
typename _Yp,
typename _Del>
1714 _UniqAssignable<_Yp, _Del>
1715 operator=(unique_ptr<_Yp, _Del>&& __r)
1717 __shared_ptr(
std::move(__r)).swap(*
this);
1723 { __shared_ptr().swap(*
this); }
1725 template<
typename _Yp>
1730 __glibcxx_assert(__p ==
nullptr || __p != _M_ptr);
1731 __shared_ptr(__p).swap(*
this);
1734 template<
typename _Yp,
typename _Deleter>
1736 reset(_Yp* __p, _Deleter __d)
1737 { __shared_ptr(__p,
std::move(__d)).swap(*
this); }
1739 template<
typename _Yp,
typename _Deleter,
typename _Alloc>
1741 reset(_Yp* __p, _Deleter __d, _Alloc __a)
1746 get() const noexcept
1750 explicit operator bool() const noexcept
1751 {
return _M_ptr !=
nullptr; }
1755 unique() const noexcept
1756 {
return _M_refcount._M_unique(); }
1760 use_count() const noexcept
1761 {
return _M_refcount._M_get_use_count(); }
1765 swap(__shared_ptr<_Tp, _Lp>& __other)
noexcept
1767 std::swap(_M_ptr, __other._M_ptr);
1768 _M_refcount._M_swap(__other._M_refcount);
1778 template<
typename _Tp1>
1780 owner_before(__shared_ptr<_Tp1, _Lp>
const& __rhs)
const noexcept
1781 {
return _M_refcount._M_less(__rhs._M_refcount); }
1783 template<
typename _Tp1>
1785 owner_before(__weak_ptr<_Tp1, _Lp>
const& __rhs)
const noexcept
1786 {
return _M_refcount._M_less(__rhs._M_refcount); }
1789#ifdef __glibcxx_smart_ptr_owner_equality
1790 size_t owner_hash() const noexcept {
return _M_refcount._M_owner_hash(); }
1792 template<
typename _Tp1>
1794 owner_equal(__shared_ptr<_Tp1, _Lp>
const& __rhs)
const noexcept
1795 {
return _M_refcount == __rhs._M_refcount; }
1797 template<
typename _Tp1>
1799 owner_equal(__weak_ptr<_Tp1, _Lp>
const& __rhs)
const noexcept
1800 {
return _M_refcount == __rhs._M_refcount; }
1805 template<
typename _Alloc,
typename... _Args>
1806 __shared_ptr(_Sp_alloc_shared_tag<_Alloc> __tag, _Args&&... __args)
1807 : _M_ptr(), _M_refcount(_M_ptr, __tag, std::
forward<_Args>(__args)...)
1808 { _M_enable_shared_from_this_with(_M_ptr); }
1810 template<
typename _Tp1, _Lock_policy _Lp1,
typename _Alloc,
1812 friend __shared_ptr<_Tp1, _Lp1>
1813 __allocate_shared(
const _Alloc& __a, _Args&&... __args);
1815#if __glibcxx_shared_ptr_arrays >= 201707L
1817 template<
typename _Alloc,
typename _Init = const remove_extent_t<_Tp>*>
1818 __shared_ptr(
const _Sp_counted_array_base<_Alloc>& __a,
1819 _Init __init =
nullptr)
1820 : _M_ptr(), _M_refcount(_M_ptr, __a, __init)
1826 __shared_ptr(
const __weak_ptr<_Tp, _Lp>& __r, std::nothrow_t) noexcept
1827 : _M_refcount(__r._M_refcount, std::nothrow)
1829 _M_ptr = _M_refcount._M_get_use_count() ? __r._M_ptr :
nullptr;
1832 friend class __weak_ptr<_Tp, _Lp>;
1836 template<
typename _Yp>
1837 using __esft_base_t =
decltype(__enable_shared_from_this_base(
1842 template<
typename _Yp,
typename =
void>
1843 struct __has_esft_base
1846 template<
typename _Yp>
1847 struct __has_esft_base<_Yp, __void_t<__esft_base_t<_Yp>>>
1848 : __not_<is_array<_Tp>> { };
1850 template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
1851 typename enable_if<__has_esft_base<_Yp2>::value>::type
1852 _M_enable_shared_from_this_with(_Yp* __p)
noexcept
1854 if (
auto __base = __enable_shared_from_this_base(_M_refcount, __p))
1855 __base->_M_weak_assign(
const_cast<_Yp2*
>(__p), _M_refcount);
1858 template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
1859 typename enable_if<!__has_esft_base<_Yp2>::value>::type
1860 _M_enable_shared_from_this_with(_Yp*)
noexcept
1864 _M_get_deleter(
const std::type_info& __ti)
const noexcept
1865 {
return _M_refcount._M_get_deleter(__ti); }
1867 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
1868 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
1870 template<
typename _Del,
typename _Tp1, _Lock_policy _Lp1>
1871 friend _Del* get_deleter(
const __shared_ptr<_Tp1, _Lp1>&)
noexcept;
1873 template<
typename _Del,
typename _Tp1>
1874 friend _Del* get_deleter(
const shared_ptr<_Tp1>&)
noexcept;
1876#ifdef __glibcxx_atomic_shared_ptr
1877 friend _Sp_atomic<shared_ptr<_Tp>>;
1879#ifdef __glibcxx_out_ptr
1880 template<
typename,
typename,
typename...>
friend class out_ptr_t;
1883 element_type* _M_ptr;
1884 __shared_count<_Lp> _M_refcount;
1889 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1891 operator==(
const __shared_ptr<_Tp1, _Lp>& __a,
1892 const __shared_ptr<_Tp2, _Lp>& __b)
noexcept
1893 {
return __a.get() == __b.get(); }
1895 template<
typename _Tp, _Lock_policy _Lp>
1897 operator==(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1900#ifdef __cpp_lib_three_way_comparison
1901 template<
typename _Tp,
typename _Up, _Lock_policy _Lp>
1902 inline strong_ordering
1903 operator<=>(
const __shared_ptr<_Tp, _Lp>& __a,
1904 const __shared_ptr<_Up, _Lp>& __b)
noexcept
1905 {
return compare_three_way()(__a.get(), __b.get()); }
1907 template<
typename _Tp, _Lock_policy _Lp>
1908 inline strong_ordering
1909 operator<=>(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1911 using pointer =
typename __shared_ptr<_Tp, _Lp>::element_type*;
1912 return compare_three_way()(__a.get(),
static_cast<pointer
>(
nullptr));
1915 template<
typename _Tp, _Lock_policy _Lp>
1917 operator==(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a)
noexcept
1920 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1922 operator!=(
const __shared_ptr<_Tp1, _Lp>& __a,
1923 const __shared_ptr<_Tp2, _Lp>& __b)
noexcept
1924 {
return __a.get() != __b.get(); }
1926 template<
typename _Tp, _Lock_policy _Lp>
1928 operator!=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1929 {
return (
bool)__a; }
1931 template<
typename _Tp, _Lock_policy _Lp>
1933 operator!=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a)
noexcept
1934 {
return (
bool)__a; }
1936 template<
typename _Tp,
typename _Up, _Lock_policy _Lp>
1938 operator<(
const __shared_ptr<_Tp, _Lp>& __a,
1939 const __shared_ptr<_Up, _Lp>& __b)
noexcept
1941 using _Tp_elt =
typename __shared_ptr<_Tp, _Lp>::element_type;
1942 using _Up_elt =
typename __shared_ptr<_Up, _Lp>::element_type;
1944 return less<_Vp>()(__a.get(), __b.get());
1947 template<
typename _Tp, _Lock_policy _Lp>
1949 operator<(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1951 using _Tp_elt =
typename __shared_ptr<_Tp, _Lp>::element_type;
1955 template<
typename _Tp, _Lock_policy _Lp>
1957 operator<(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a)
noexcept
1959 using _Tp_elt =
typename __shared_ptr<_Tp, _Lp>::element_type;
1963 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1965 operator<=(
const __shared_ptr<_Tp1, _Lp>& __a,
1966 const __shared_ptr<_Tp2, _Lp>& __b)
noexcept
1967 {
return !(__b < __a); }
1969 template<
typename _Tp, _Lock_policy _Lp>
1971 operator<=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1972 {
return !(
nullptr < __a); }
1974 template<
typename _Tp, _Lock_policy _Lp>
1976 operator<=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a)
noexcept
1977 {
return !(__a <
nullptr); }
1979 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1981 operator>(
const __shared_ptr<_Tp1, _Lp>& __a,
1982 const __shared_ptr<_Tp2, _Lp>& __b)
noexcept
1983 {
return (__b < __a); }
1985 template<
typename _Tp, _Lock_policy _Lp>
1987 operator>(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1988 {
return nullptr < __a; }
1990 template<
typename _Tp, _Lock_policy _Lp>
1992 operator>(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a)
noexcept
1993 {
return __a <
nullptr; }
1995 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1997 operator>=(
const __shared_ptr<_Tp1, _Lp>& __a,
1998 const __shared_ptr<_Tp2, _Lp>& __b)
noexcept
1999 {
return !(__a < __b); }
2001 template<
typename _Tp, _Lock_policy _Lp>
2003 operator>=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
2004 {
return !(__a <
nullptr); }
2006 template<
typename _Tp, _Lock_policy _Lp>
2008 operator>=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a)
noexcept
2009 {
return !(
nullptr < __a); }
2013 template<
typename _Tp, _Lock_policy _Lp>
2015 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b)
noexcept
2025 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
2026 inline __shared_ptr<_Tp, _Lp>
2029 using _Sp = __shared_ptr<_Tp, _Lp>;
2030 return _Sp(__r,
static_cast<typename _Sp::element_type*
>(__r.get()));
2038 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
2039 inline __shared_ptr<_Tp, _Lp>
2042 using _Sp = __shared_ptr<_Tp, _Lp>;
2043 return _Sp(__r,
const_cast<typename _Sp::element_type*
>(__r.get()));
2051 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
2052 inline __shared_ptr<_Tp, _Lp>
2055 using _Sp = __shared_ptr<_Tp, _Lp>;
2056 if (
auto* __p =
dynamic_cast<typename _Sp::element_type*
>(__r.get()))
2057 return _Sp(__r, __p);
2061#if __cplusplus > 201402L
2062 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
2063 inline __shared_ptr<_Tp, _Lp>
2064 reinterpret_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r)
noexcept
2066 using _Sp = __shared_ptr<_Tp, _Lp>;
2067 return _Sp(__r,
reinterpret_cast<typename _Sp::element_type*
>(__r.get()));
2071 template<
typename _Tp, _Lock_policy _Lp>
2075 using element_type =
typename remove_extent<_Tp>::type;
2078 template<
typename _Yp,
typename _Res =
void>
2079 using _Compatible =
typename
2083 template<
typename _Yp>
2084 using _Assignable = _Compatible<_Yp, __weak_ptr&>;
2086#pragma GCC diagnostic push
2087#pragma GCC diagnostic ignored "-Wc++17-extensions"
2089 template<
typename _Yp>
2090 static element_type*
2091 _S_safe_upcast(
const __weak_ptr<_Yp, _Lp>& __r)
2106 else if constexpr (__and_<is_scalar<_At>,
is_scalar<_Bt>>::value)
2108#if _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_is_virtual_base_of)
2112 else if constexpr (!__builtin_is_virtual_base_of(_Tp, _Yp))
2119 return __r.lock().get();
2121#pragma GCC diagnostic pop
2124 constexpr __weak_ptr() noexcept
2125 : _M_ptr(
nullptr), _M_refcount()
2128 __weak_ptr(
const __weak_ptr&)
noexcept =
default;
2130 ~__weak_ptr() =
default;
2146 template<
typename _Yp,
typename = _Compatible<_Yp>>
2147 __weak_ptr(
const __weak_ptr<_Yp, _Lp>& __r) noexcept
2148 : _M_ptr(_S_safe_upcast(__r)), _M_refcount(__r._M_refcount)
2151 template<
typename _Yp,
typename = _Compatible<_Yp>>
2152 __weak_ptr(
const __shared_ptr<_Yp, _Lp>& __r) noexcept
2153 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
2156 __weak_ptr(__weak_ptr&& __r) noexcept
2157 : _M_ptr(__r._M_ptr), _M_refcount(
std::move(__r._M_refcount))
2158 { __r._M_ptr =
nullptr; }
2160 template<
typename _Yp,
typename = _Compatible<_Yp>>
2161 __weak_ptr(__weak_ptr<_Yp, _Lp>&& __r) noexcept
2162 : _M_ptr(_S_safe_upcast(__r)), _M_refcount(
std::move(__r._M_refcount))
2163 { __r._M_ptr =
nullptr; }
2166 operator=(
const __weak_ptr& __r)
noexcept =
default;
2168 template<
typename _Yp>
2170 operator=(
const __weak_ptr<_Yp, _Lp>& __r)
noexcept
2172 _M_ptr = _S_safe_upcast(__r);
2173 _M_refcount = __r._M_refcount;
2177 template<
typename _Yp>
2179 operator=(
const __shared_ptr<_Yp, _Lp>& __r)
noexcept
2181 _M_ptr = __r._M_ptr;
2182 _M_refcount = __r._M_refcount;
2187 operator=(__weak_ptr&& __r)
noexcept
2193 template<
typename _Yp>
2195 operator=(__weak_ptr<_Yp, _Lp>&& __r)
noexcept
2197 _M_ptr = _S_safe_upcast(__r);
2198 _M_refcount =
std::move(__r._M_refcount);
2199 __r._M_ptr =
nullptr;
2203 __shared_ptr<_Tp, _Lp>
2204 lock() const noexcept
2205 {
return __shared_ptr<_Tp, _Lp>(*
this, std::nothrow); }
2208 use_count() const noexcept
2209 {
return _M_refcount._M_get_use_count(); }
2212 expired() const noexcept
2213 {
return _M_refcount._M_get_use_count() == 0; }
2215 template<
typename _Tp1>
2217 owner_before(
const __shared_ptr<_Tp1, _Lp>& __rhs)
const noexcept
2218 {
return _M_refcount._M_less(__rhs._M_refcount); }
2220 template<
typename _Tp1>
2222 owner_before(
const __weak_ptr<_Tp1, _Lp>& __rhs)
const noexcept
2223 {
return _M_refcount._M_less(__rhs._M_refcount); }
2225#ifdef __glibcxx_smart_ptr_owner_equality
2226 size_t owner_hash() const noexcept {
return _M_refcount._M_owner_hash(); }
2228 template<
typename _Tp1>
2230 owner_equal(
const __shared_ptr<_Tp1, _Lp> & __rhs)
const noexcept
2231 {
return _M_refcount == __rhs._M_refcount; }
2233 template<
typename _Tp1>
2235 owner_equal(
const __weak_ptr<_Tp1, _Lp> & __rhs)
const noexcept
2236 {
return _M_refcount == __rhs._M_refcount; }
2241 { __weak_ptr().swap(*
this); }
2244 swap(__weak_ptr& __s)
noexcept
2246 std::swap(_M_ptr, __s._M_ptr);
2247 _M_refcount._M_swap(__s._M_refcount);
2253 _M_assign(_Tp* __ptr,
const __shared_count<_Lp>& __refcount)
noexcept
2255 if (use_count() == 0)
2258 _M_refcount = __refcount;
2262 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
2263 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
2264 friend class __enable_shared_from_this<_Tp, _Lp>;
2266#ifdef __glibcxx_atomic_shared_ptr
2267 friend _Sp_atomic<weak_ptr<_Tp>>;
2270 element_type* _M_ptr;
2271 __weak_count<_Lp> _M_refcount;
2275 template<
typename _Tp, _Lock_policy _Lp>
2277 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b)
noexcept
2280#pragma GCC diagnostic push
2281#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
2282 template<
typename _Tp,
typename _Tp1>
2286 operator()(
const _Tp& __lhs,
const _Tp& __rhs)
const noexcept
2287 {
return __lhs.owner_before(__rhs); }
2290 operator()(
const _Tp& __lhs,
const _Tp1& __rhs)
const noexcept
2291 {
return __lhs.owner_before(__rhs); }
2294 operator()(
const _Tp1& __lhs,
const _Tp& __rhs)
const noexcept
2295 {
return __lhs.owner_before(__rhs); }
2297#pragma GCC diagnostic pop
2300 struct _Sp_owner_less<void, void>
2302 template<
typename _Tp,
typename _Up>
2304 operator()(
const _Tp& __lhs,
const _Up& __rhs)
const noexcept
2305 ->
decltype(__lhs.owner_before(__rhs))
2306 {
return __lhs.owner_before(__rhs); }
2308 using is_transparent = void;
2311 template<
typename _Tp, _Lock_policy _Lp>
2313 :
public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
2316 template<
typename _Tp, _Lock_policy _Lp>
2318 :
public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
2322 template<
typename _Tp, _Lock_policy _Lp>
2323 class __enable_shared_from_this
2326 constexpr __enable_shared_from_this() noexcept { }
2328 __enable_shared_from_this(
const __enable_shared_from_this&)
noexcept { }
2330 __enable_shared_from_this&
2331 operator=(
const __enable_shared_from_this&)
noexcept
2334 ~__enable_shared_from_this() { }
2337 __shared_ptr<_Tp, _Lp>
2339 {
return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
2341 __shared_ptr<const _Tp, _Lp>
2342 shared_from_this()
const
2343 {
return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
2345#if __cplusplus > 201402L || !defined(__STRICT_ANSI__)
2346 __weak_ptr<_Tp, _Lp>
2347 weak_from_this() noexcept
2348 {
return this->_M_weak_this; }
2350 __weak_ptr<const _Tp, _Lp>
2351 weak_from_this() const noexcept
2352 {
return this->_M_weak_this; }
2356 template<
typename _Tp1>
2358 _M_weak_assign(_Tp1* __p,
const __shared_count<_Lp>& __n)
const noexcept
2359 { _M_weak_this._M_assign(__p, __n); }
2361 friend const __enable_shared_from_this*
2362 __enable_shared_from_this_base(
const __shared_count<_Lp>&,
2363 const __enable_shared_from_this* __p)
2366 template<
typename, _Lock_policy>
2367 friend class __shared_ptr;
2369 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
2372 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy,
2373 typename _Alloc,
typename... _Args>
2374 inline __shared_ptr<_Tp, _Lp>
2375 __allocate_shared(
const _Alloc& __a, _Args&&... __args)
2379 return __shared_ptr<_Tp, _Lp>(_Sp_alloc_shared_tag<_Alloc>{__a},
2383 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy,
2385 inline __shared_ptr<_Tp, _Lp>
2386 __make_shared(_Args&&... __args)
2388 typedef typename std::remove_const<_Tp>::type _Tp_nc;
2389 return std::__allocate_shared<_Tp, _Lp>(std::allocator<_Tp_nc>(),
2394 template<
typename _Tp, _Lock_policy _Lp>
2395 struct hash<__shared_ptr<_Tp, _Lp>>
2396 :
public __hash_base<size_t, __shared_ptr<_Tp, _Lp>>
2399 operator()(
const __shared_ptr<_Tp, _Lp>& __s)
const noexcept
2406_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.