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#pragma GCC diagnostic ignored "-Warray-bounds"
585 template<
typename>
class auto_ptr;
586 ~_Sp_counted_deleter() noexcept { }
589 _M_dispose() noexcept
590 { _M_impl._M_del()(_M_impl._M_ptr); }
591#pragma GCC diagnostic pop
594 _M_destroy() noexcept
596 __allocator_type __a(_M_impl._M_alloc());
597 __allocated_ptr<__allocator_type> __guard_ptr{ __a,
this };
598 this->~_Sp_counted_deleter();
602 _M_get_deleter(
const type_info& __ti [[__gnu__::__unused__]])
noexcept
607 return __ti ==
typeid(_Deleter)
616#ifdef __glibcxx_out_ptr
617 template<
typename,
typename,
typename...>
friend class out_ptr_t;
624 struct _Sp_make_shared_tag
627 template<
typename _Tp,
typename _Alloc, _Lock_policy _Lp>
628 friend class _Sp_counted_ptr_inplace;
630 static const type_info&
631 _S_ti() noexcept _GLIBCXX_VISIBILITY(default)
633 alignas(type_info)
static constexpr char __tag[
sizeof(type_info)] = { };
634 return reinterpret_cast<const type_info&
>(__tag);
637 static bool _S_eq(
const type_info&)
noexcept;
640 template<
typename _Alloc>
641 struct _Sp_alloc_shared_tag
646 template<
typename _Tp,
typename _Alloc, _Lock_policy _Lp>
647 class _Sp_counted_ptr_inplace final :
public _Sp_counted_base<_Lp>
649 class _Impl : _Sp_ebo_helper<0, _Alloc>
651 typedef _Sp_ebo_helper<0, _Alloc> _A_base;
654 explicit _Impl(_Alloc __a) noexcept : _A_base(__a) { }
656 _Alloc& _M_alloc() noexcept {
return _A_base::_S_get(*
this); }
658 __gnu_cxx::__aligned_buffer<__remove_cv_t<_Tp>> _M_storage;
662 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>;
665 template<
typename... _Args>
666 _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
675#pragma GCC diagnostic push
676#pragma GCC diagnostic ignored "-Warray-bounds"
677 ~_Sp_counted_ptr_inplace() noexcept { }
680 _M_dispose() noexcept
684#pragma GCC diagnostic pop
688 _M_destroy() noexcept
690 __allocator_type __a(_M_impl._M_alloc());
691 __allocated_ptr<__allocator_type> __guard_ptr{ __a,
this };
692 this->~_Sp_counted_ptr_inplace();
696 friend class __shared_count<_Lp>;
701 _M_get_deleter(
const std::type_info& __ti)
noexcept override
707 if (&__ti == &_Sp_make_shared_tag::_S_ti()
710 __ti ==
typeid(_Sp_make_shared_tag)
712 _Sp_make_shared_tag::_S_eq(__ti)
720 _M_ptr() noexcept {
return _M_impl._M_storage._M_ptr(); }
725#ifdef __glibcxx_smart_ptr_for_overwrite
726 struct _Sp_overwrite_tag { };
732 template<
typename _Tp,
typename _Alloc, _Lock_policy _Lp>
733 requires is_same_v<typename _Alloc::value_type, _Sp_overwrite_tag>
734 class _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> final
736 template<typename _Tp, template<typename> class _Alloc, _Lock_policy _Lp>
737 class _Sp_counted_ptr_inplace<_Tp, _Alloc<_Sp_overwrite_tag>, _Lp> final
739 :
public _Sp_counted_base<_Lp>
741 [[no_unique_address]] _Alloc _M_alloc;
744 remove_cv_t<_Tp> _M_obj;
748 friend class __shared_count<_Lp>;
753 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>;
755 _Sp_counted_ptr_inplace(
const _Alloc& __a)
758 ::new((
void*)_M_ptr()) _Tp;
761 ~_Sp_counted_ptr_inplace() noexcept { }
764 _M_dispose() noexcept
771 _M_destroy() noexcept
773 using pointer =
typename allocator_traits<__allocator_type>::pointer;
774 __allocator_type __a(_M_alloc);
775 auto __p = pointer_traits<pointer>::pointer_to(*
this);
776 __allocated_ptr<__allocator_type> __guard_ptr{ __a, __p };
777 this->~_Sp_counted_ptr_inplace();
781 _M_get_deleter(
const std::type_info&)
noexcept override
786#if __glibcxx_shared_ptr_arrays >= 201707L
787 struct _Sp_overwrite_tag;
790 template<
typename _Alloc>
791 struct _Sp_counted_array_base
793 [[no_unique_address]] _Alloc _M_alloc{};
795 bool _M_overwrite =
false;
797 typename allocator_traits<_Alloc>::pointer
798 _M_alloc_array(
size_t __tail)
800 return allocator_traits<_Alloc>::allocate(_M_alloc, _M_n + __tail);
804 _M_dealloc_array(
typename allocator_traits<_Alloc>::pointer __p,
807 allocator_traits<_Alloc>::deallocate(_M_alloc, __p, _M_n + __tail);
811 template<
typename _Init>
813 _M_init(
typename allocator_traits<_Alloc>::value_type* __p,
816 using _Tp = remove_pointer_t<_Init>;
817 using _Up =
typename allocator_traits<_Alloc>::value_type;
819 if constexpr (is_same_v<_Init, _Sp_overwrite_tag>)
824 else if (__init ==
nullptr)
825 std::__uninitialized_default_n_a(__p, _M_n, _M_alloc);
826 else if constexpr (!is_array_v<_Tp>)
827 std::__uninitialized_fill_n_a(__p, _M_n, *__init, _M_alloc);
830#pragma GCC diagnostic push
831#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
834 using value_type = _Up;
835 using difference_type = ptrdiff_t;
836 using pointer =
const _Up*;
837 using reference =
const _Up&;
838 using iterator_category = forward_iterator_tag;
844 _Iter& operator++() { ++_M_pos;
return *
this; }
845 _Iter operator++(
int) {
auto __i(*
this); ++_M_pos;
return __i; }
847 reference
operator*()
const {
return _M_p[_M_pos % _M_len]; }
848 pointer operator->()
const {
return _M_p + (_M_pos % _M_len); }
850 bool operator==(
const _Iter& __i)
const
851 {
return _M_pos == __i._M_pos; }
853#pragma GCC diagnostic pop
855 _Iter __first{_S_first_elem(__init),
sizeof(_Tp) /
sizeof(_Up)};
856 _Iter __last = __first;
857 __last._M_pos = _M_n;
858 std::__uninitialized_copy_a(__first, __last, __p, _M_alloc);
865 _M_dispose_array(
typename allocator_traits<_Alloc>::value_type* __p)
868 std::destroy_n(__p, _M_n);
873 allocator_traits<_Alloc>::destroy(_M_alloc, __p + __n);
878 template<
typename _Tp>
880 _S_first_elem(_Tp* __p) {
return __p; }
882 template<
typename _Tp,
size_t _Nm>
884 _S_first_elem(_Tp (*__p)[_Nm]) {
return _S_first_elem(*__p); }
889 template<
typename _Alloc, _Lock_policy _Lp>
890 class _Sp_counted_array final
891 :
public _Sp_counted_base<_Lp>, _Sp_counted_array_base<_Alloc>
893 using pointer =
typename allocator_traits<_Alloc>::pointer;
895 pointer _M_alloc_ptr;
899 friend class __shared_count<_Lp>;
902 _Sp_counted_array(
const _Sp_counted_array_base<_Alloc>& __a,
903 pointer __p) noexcept
904 : _Sp_counted_array_base<_Alloc>(__a), _M_alloc_ptr(__p)
907 ~_Sp_counted_array() =
default;
910 _M_dispose() noexcept
913 this->_M_dispose_array(_M_ptr());
918 _M_destroy() noexcept
920 _Sp_counted_array_base<_Alloc> __a = *
this;
921 pointer __p = _M_alloc_ptr;
922 this->~_Sp_counted_array();
923 __a._M_dealloc_array(__p, _S_tail());
928 static constexpr size_t
932 using _Tp =
typename allocator_traits<_Alloc>::value_type;
935 size_t __bytes =
sizeof(_Sp_counted_array);
938 if constexpr (
alignof(_Tp) <
alignof(_Sp_counted_array))
939 __bytes +=
alignof(_Sp_counted_array) -
alignof(_Tp);
941 return (__bytes +
sizeof(_Tp) - 1) /
sizeof(_Tp);
945 _M_get_deleter(
const std::type_info&)
noexcept override
951 struct __sp_array_delete
953 template<
typename _Yp>
954 void operator()(_Yp* __p)
const {
delete[] __p; }
957 template<_Lock_policy _Lp>
961 template<
typename _Tp>
962 struct __not_alloc_shared_tag {
using type = void; };
964 template<
typename _Tp>
965 struct __not_alloc_shared_tag<_Sp_alloc_shared_tag<_Tp>> { };
967#if __glibcxx_shared_ptr_arrays >= 201707L
968 template<
typename _Alloc>
969 struct __not_alloc_shared_tag<_Sp_counted_array_base<_Alloc>> { };
973 constexpr __shared_count() noexcept : _M_pi(0)
976 template<
typename _Ptr>
978 __shared_count(_Ptr __p) : _M_pi(0)
982 _M_pi =
new _Sp_counted_ptr<_Ptr, _Lp>(__p);
987 __throw_exception_again;
991 template<
typename _Ptr>
992 __shared_count(_Ptr __p, false_type)
993 : __shared_count(__p)
996 template<
typename _Ptr>
997 __shared_count(_Ptr __p, true_type)
998 : __shared_count(__p, __sp_array_delete{}, allocator<void>())
1001 template<
typename _Ptr,
typename _Deleter,
1002 typename =
typename __not_alloc_shared_tag<_Deleter>::type>
1003 __shared_count(_Ptr __p, _Deleter __d)
1004 : __shared_count(__p, std::move(__d), allocator<void>())
1007 template<
typename _Ptr,
typename _Deleter,
typename _Alloc,
1008 typename =
typename __not_alloc_shared_tag<_Deleter>::type>
1009 __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
1011 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
1014 typename _Sp_cd_type::__allocator_type __a2(__a);
1015 auto __guard = std::__allocate_guarded(__a2);
1016 _Sp_cd_type* __mem = __guard.get();
1024 __throw_exception_again;
1028 template<
typename _Tp,
typename _Alloc,
typename... _Args>
1029 __shared_count(_Tp*& __p, _Sp_alloc_shared_tag<_Alloc> __a,
1032 using _Tp2 = __remove_cv_t<_Tp>;
1033 using _Sp_cp_type = _Sp_counted_ptr_inplace<_Tp2, _Alloc, _Lp>;
1034 typename _Sp_cp_type::__allocator_type __a2(__a._M_a);
1035 auto __guard = std::__allocate_guarded(__a2);
1036 _Sp_cp_type* __mem = __guard.get();
1037 auto __pi = ::new (__mem)
1041 __p = __pi->_M_ptr();
1044#if __glibcxx_shared_ptr_arrays >= 201707L
1045 template<
typename _Tp,
typename _Alloc,
typename _Init>
1046 __shared_count(_Tp*& __p,
const _Sp_counted_array_base<_Alloc>& __a,
1049 using _Up = remove_all_extents_t<_Tp>;
1050 static_assert(is_same_v<_Up, typename _Alloc::value_type>);
1052 using _Sp_ca_type = _Sp_counted_array<_Alloc, _Lp>;
1053 const size_t __tail = _Sp_ca_type::_S_tail();
1055 struct _Guarded_ptr : _Sp_counted_array_base<_Alloc>
1057 typename allocator_traits<_Alloc>::pointer _M_ptr;
1059 _Guarded_ptr(_Sp_counted_array_base<_Alloc> __a)
1060 : _Sp_counted_array_base<_Alloc>(__a),
1061 _M_ptr(this->_M_alloc_array(_Sp_ca_type::_S_tail()))
1067 this->_M_dealloc_array(_M_ptr, _Sp_ca_type::_S_tail());
1071 _Guarded_ptr __guard{__a};
1073 __guard._M_init(__raw, __init);
1075 void* __c = __raw + __a._M_n;
1076 if constexpr (
alignof(_Up) <
alignof(_Sp_ca_type))
1078 size_t __space =
sizeof(_Up) * __tail;
1079 __c =
std::align(
alignof(_Sp_ca_type),
sizeof(_Sp_ca_type),
1082 auto __pi = ::new(__c) _Sp_ca_type(__guard, __guard._M_ptr);
1083 __guard._M_ptr =
nullptr;
1085 __p =
reinterpret_cast<_Tp*
>(__raw);
1089#if _GLIBCXX_USE_DEPRECATED
1090#pragma GCC diagnostic push
1091#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1093 template<
typename _Tp>
1095 __shared_count(std::auto_ptr<_Tp>&& __r);
1096#pragma GCC diagnostic pop
1100 template<
typename _Tp,
typename _Del>
1102 __shared_count(std::unique_ptr<_Tp, _Del>&& __r) : _M_pi(0)
1106 if (__r.get() ==
nullptr)
1109 using _Ptr =
typename unique_ptr<_Tp, _Del>::pointer;
1110 using _Del2 = __conditional_t<is_reference<_Del>::value,
1111 reference_wrapper<
typename remove_reference<_Del>::type>,
1114 = _Sp_counted_deleter<_Ptr, _Del2, allocator<void>, _Lp>;
1115 using _Alloc = allocator<_Sp_cd_type>;
1116 using _Alloc_traits = allocator_traits<_Alloc>;
1118 _Sp_cd_type* __mem = _Alloc_traits::allocate(__a, 1);
1122 _Alloc_traits::construct(__a, __mem, __r.release(),
1128 explicit __shared_count(
const __weak_count<_Lp>& __r);
1132 __shared_count(
const __weak_count<_Lp>& __r, std::nothrow_t)
noexcept;
1134 ~__shared_count() noexcept
1136 if (_M_pi !=
nullptr)
1137 _M_pi->_M_release();
1140 __shared_count(
const __shared_count& __r) noexcept
1143 if (_M_pi !=
nullptr)
1144 _M_pi->_M_add_ref_copy();
1148 operator=(
const __shared_count& __r)
noexcept
1150 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1153 if (__tmp !=
nullptr)
1154 __tmp->_M_add_ref_copy();
1155 if (_M_pi !=
nullptr)
1156 _M_pi->_M_release();
1163 _M_swap(__shared_count& __r)
noexcept
1165 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1171 _M_get_use_count() const noexcept
1172 {
return _M_pi ? _M_pi->_M_get_use_count() : 0; }
1175 _M_unique() const noexcept
1176 {
return this->_M_get_use_count() == 1; }
1179 _M_get_deleter(
const std::type_info& __ti)
const noexcept
1180 {
return _M_pi ? _M_pi->_M_get_deleter(__ti) :
nullptr; }
1183 _M_less(
const __shared_count& __rhs)
const noexcept
1184 {
return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
1187 _M_less(
const __weak_count<_Lp>& __rhs)
const noexcept
1188 {
return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
1190#ifdef __glibcxx_smart_ptr_owner_equality
1192 _M_owner_hash() const noexcept
1193 {
return std::hash<_Sp_counted_base<_Lp>*>()(this->_M_pi); }
1198 operator==(
const __shared_count& __a,
const __shared_count& __b)
noexcept
1199 {
return __a._M_pi == __b._M_pi; }
1202 friend class __weak_count<_Lp>;
1203#ifdef __glibcxx_atomic_shared_ptr
1204 template<
typename>
friend class _Sp_atomic;
1206#ifdef __glibcxx_out_ptr
1207 template<
typename,
typename,
typename...>
friend class out_ptr_t;
1210 _Sp_counted_base<_Lp>* _M_pi;
1214 template<_Lock_policy _Lp>
1218 constexpr __weak_count() noexcept : _M_pi(
nullptr)
1221 __weak_count(
const __shared_count<_Lp>& __r) noexcept
1224 if (_M_pi !=
nullptr)
1225 _M_pi->_M_weak_add_ref();
1228 __weak_count(
const __weak_count& __r) noexcept
1231 if (_M_pi !=
nullptr)
1232 _M_pi->_M_weak_add_ref();
1235 __weak_count(__weak_count&& __r) noexcept
1237 { __r._M_pi =
nullptr; }
1239 ~__weak_count() noexcept
1241 if (_M_pi !=
nullptr)
1242 _M_pi->_M_weak_release();
1246 operator=(
const __shared_count<_Lp>& __r)
noexcept
1248 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1249 if (__tmp !=
nullptr)
1250 __tmp->_M_weak_add_ref();
1251 if (_M_pi !=
nullptr)
1252 _M_pi->_M_weak_release();
1258 operator=(
const __weak_count& __r)
noexcept
1260 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1261 if (__tmp !=
nullptr)
1262 __tmp->_M_weak_add_ref();
1263 if (_M_pi !=
nullptr)
1264 _M_pi->_M_weak_release();
1270 operator=(__weak_count&& __r)
noexcept
1272 if (_M_pi !=
nullptr)
1273 _M_pi->_M_weak_release();
1275 __r._M_pi =
nullptr;
1280 _M_swap(__weak_count& __r)
noexcept
1282 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1288 _M_get_use_count() const noexcept
1289 {
return _M_pi !=
nullptr ? _M_pi->_M_get_use_count() : 0; }
1292 _M_less(
const __weak_count& __rhs)
const noexcept
1293 {
return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
1296 _M_less(
const __shared_count<_Lp>& __rhs)
const noexcept
1297 {
return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
1299#ifdef __glibcxx_smart_ptr_owner_equality
1301 _M_owner_hash() const noexcept
1302 {
return std::hash<_Sp_counted_base<_Lp>*>()(this->_M_pi); }
1307 operator==(
const __weak_count& __a,
const __weak_count& __b)
noexcept
1308 {
return __a._M_pi == __b._M_pi; }
1311 friend class __shared_count<_Lp>;
1312#ifdef __glibcxx_atomic_shared_ptr
1313 template<
typename>
friend class _Sp_atomic;
1316 _Sp_counted_base<_Lp>* _M_pi;
1320 template<_Lock_policy _Lp>
1322 __shared_count<_Lp>::__shared_count(
const __weak_count<_Lp>& __r)
1325 if (_M_pi ==
nullptr || !_M_pi->_M_add_ref_lock_nothrow())
1326 __throw_bad_weak_ptr();
1330 template<_Lock_policy _Lp>
1332 __shared_count<_Lp>::
1333 __shared_count(
const __weak_count<_Lp>& __r, std::nothrow_t) noexcept
1336 if (_M_pi && !_M_pi->_M_add_ref_lock_nothrow())
1344 template<
typename _Yp_ptr,
typename _Tp_ptr>
1345 struct __sp_compatible_with
1349 template<
typename _Yp,
typename _Tp>
1350 struct __sp_compatible_with<_Yp*, _Tp*>
1351 : is_convertible<_Yp*, _Tp*>::type
1354 template<
typename _Up,
size_t _Nm>
1355 struct __sp_compatible_with<_Up(*)[_Nm], _Up(*)[]>
1359 template<
typename _Up,
size_t _Nm>
1360 struct __sp_compatible_with<_Up(*)[_Nm],
const _Up(*)[]>
1364 template<
typename _Up,
size_t _Nm>
1365 struct __sp_compatible_with<_Up(*)[_Nm],
volatile _Up(*)[]>
1369 template<
typename _Up,
size_t _Nm>
1370 struct __sp_compatible_with<_Up(*)[_Nm],
const volatile _Up(*)[]>
1375 template<
typename _Up,
size_t _Nm,
typename _Yp,
typename =
void>
1376 struct __sp_is_constructible_arrN
1380 template<
typename _Up,
size_t _Nm,
typename _Yp>
1381 struct __sp_is_constructible_arrN<_Up, _Nm, _Yp, __void_t<_Yp[_Nm]>>
1382 : is_convertible<_Yp(*)[_Nm], _Up(*)[_Nm]>::type
1386 template<
typename _Up,
typename _Yp,
typename =
void>
1387 struct __sp_is_constructible_arr
1391 template<
typename _Up,
typename _Yp>
1392 struct __sp_is_constructible_arr<_Up, _Yp, __void_t<_Yp[]>>
1393 : is_convertible<_Yp(*)[], _Up(*)[]>::type
1397 template<
typename _Tp,
typename _Yp>
1398 struct __sp_is_constructible;
1401 template<
typename _Up,
size_t _Nm,
typename _Yp>
1402 struct __sp_is_constructible<_Up[_Nm], _Yp>
1403 : __sp_is_constructible_arrN<_Up, _Nm, _Yp>::type
1407 template<
typename _Up,
typename _Yp>
1408 struct __sp_is_constructible<_Up[], _Yp>
1409 : __sp_is_constructible_arr<_Up, _Yp>::type
1413 template<
typename _Tp,
typename _Yp>
1414 struct __sp_is_constructible
1415 : is_convertible<_Yp*, _Tp*>::type
1419 template<
typename _Tp>
1420 [[__gnu__::__always_inline__]]
1422 __shared_ptr_deref(_Tp* __p)
1424 __glibcxx_assert(__p !=
nullptr);
1429 template<
typename _Tp, _Lock_policy _Lp,
1431 class __shared_ptr_access
1434 using element_type = _Tp;
1437 operator*() const noexcept
1438 {
return *std::__shared_ptr_deref(_M_get()); }
1441 operator->() const noexcept
1443 _GLIBCXX_DEBUG_PEDASSERT(_M_get() !=
nullptr);
1449 _M_get() const noexcept
1450 {
return static_cast<const __shared_ptr<_Tp, _Lp>*
>(
this)->get(); }
1454 template<
typename _Tp, _Lock_policy _Lp>
1455 class __shared_ptr_access<_Tp, _Lp, false, true>
1458 using element_type = _Tp;
1461 operator->() const noexcept
1463 auto __ptr =
static_cast<const __shared_ptr<_Tp, _Lp>*
>(
this)->get();
1464 _GLIBCXX_DEBUG_PEDASSERT(__ptr !=
nullptr);
1470 template<
typename _Tp, _Lock_policy _Lp>
1471 class __shared_ptr_access<_Tp, _Lp, true, false>
1474 using element_type =
typename remove_extent<_Tp>::type;
1476#if __cplusplus <= 201402L
1477 [[__deprecated__(
"shared_ptr<T[]>::operator* is absent from C++17")]]
1479 operator*() const noexcept
1480 {
return *std::__shared_ptr_deref(_M_get()); }
1482 [[__deprecated__(
"shared_ptr<T[]>::operator-> is absent from C++17")]]
1484 operator->() const noexcept
1486 _GLIBCXX_DEBUG_PEDASSERT(_M_get() !=
nullptr);
1491#pragma GCC diagnostic push
1492#pragma GCC diagnostic ignored "-Wc++17-extensions"
1494 operator[](ptrdiff_t __i)
const noexcept
1496 if constexpr (extent<_Tp>::value)
1497 __glibcxx_assert(__i < extent<_Tp>::value);
1498 return std::__shared_ptr_deref(_M_get())[__i];
1500#pragma GCC diagnostic pop
1504 _M_get() const noexcept
1505 {
return static_cast<const __shared_ptr<_Tp, _Lp>*
>(
this)->get(); }
1508 template<
typename _Tp, _Lock_policy _Lp>
1510 :
public __shared_ptr_access<_Tp, _Lp>
1513 using element_type =
typename remove_extent<_Tp>::type;
1517 template<
typename _Yp>
1519 =
typename enable_if<__sp_is_constructible<_Tp, _Yp>::value>::type;
1522 template<
typename _Yp,
typename _Res =
void>
1523 using _Compatible =
typename
1524 enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type;
1527 template<
typename _Yp>
1528 using _Assignable = _Compatible<_Yp, __shared_ptr&>;
1531 template<
typename _Yp,
typename _Del,
typename _Res = void,
1532 typename _Ptr =
typename unique_ptr<_Yp, _Del>::pointer>
1533 using _UniqCompatible = __enable_if_t<__and_<
1534 __sp_compatible_with<_Yp*, _Tp*>,
1535 is_convertible<_Ptr, element_type*>,
1536 is_move_constructible<_Del>
1540 template<
typename _Yp,
typename _Del>
1541 using _UniqAssignable = _UniqCompatible<_Yp, _Del, __shared_ptr&>;
1545#if __cplusplus > 201402L
1546 using weak_type = __weak_ptr<_Tp, _Lp>;
1549 constexpr __shared_ptr() noexcept
1550 : _M_ptr(0), _M_refcount()
1553 template<
typename _Yp,
typename = _SafeConv<_Yp>>
1555 __shared_ptr(_Yp* __p)
1556 : _M_ptr(__p), _M_refcount(__p, typename is_array<_Tp>::type())
1558 static_assert( !is_void<_Yp>::value,
"incomplete type" );
1559 static_assert(
sizeof(_Yp) > 0,
"incomplete type" );
1560 _M_enable_shared_from_this_with(__p);
1563 template<
typename _Yp,
typename _Deleter,
typename = _SafeConv<_Yp>>
1564 __shared_ptr(_Yp* __p, _Deleter __d)
1565 : _M_ptr(__p), _M_refcount(__p, std::
move(__d))
1567 static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
1568 "deleter expression d(p) is well-formed");
1569 _M_enable_shared_from_this_with(__p);
1572 template<
typename _Yp,
typename _Deleter,
typename _Alloc,
1573 typename = _SafeConv<_Yp>>
1574 __shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a)
1575 : _M_ptr(__p), _M_refcount(__p, std::
move(__d), std::
move(__a))
1577 static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
1578 "deleter expression d(p) is well-formed");
1579 _M_enable_shared_from_this_with(__p);
1582 template<
typename _Deleter>
1583 __shared_ptr(nullptr_t __p, _Deleter __d)
1584 : _M_ptr(0), _M_refcount(__p, std::
move(__d))
1587 template<
typename _Deleter,
typename _Alloc>
1588 __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
1589 : _M_ptr(0), _M_refcount(__p, std::
move(__d), std::
move(__a))
1593 template<
typename _Yp>
1594 __shared_ptr(
const __shared_ptr<_Yp, _Lp>& __r,
1595 element_type* __p) noexcept
1596 : _M_ptr(__p), _M_refcount(__r._M_refcount)
1600 template<
typename _Yp>
1601 __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r,
1602 element_type* __p) noexcept
1603 : _M_ptr(__p), _M_refcount()
1605 _M_refcount._M_swap(__r._M_refcount);
1606 __r._M_ptr =
nullptr;
1609 __shared_ptr(
const __shared_ptr&)
noexcept =
default;
1610 __shared_ptr& operator=(
const __shared_ptr&)
noexcept =
default;
1611 ~__shared_ptr() =
default;
1613 template<
typename _Yp,
typename = _Compatible<_Yp>>
1614 __shared_ptr(
const __shared_ptr<_Yp, _Lp>& __r) noexcept
1615 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1618 __shared_ptr(__shared_ptr&& __r) noexcept
1619 : _M_ptr(__r._M_ptr), _M_refcount()
1621 _M_refcount._M_swap(__r._M_refcount);
1622 __r._M_ptr =
nullptr;
1625 template<
typename _Yp,
typename = _Compatible<_Yp>>
1626 __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r) noexcept
1627 : _M_ptr(__r._M_ptr), _M_refcount()
1629 _M_refcount._M_swap(__r._M_refcount);
1630 __r._M_ptr =
nullptr;
1633 template<
typename _Yp,
typename = _Compatible<_Yp>>
1634 explicit __shared_ptr(
const __weak_ptr<_Yp, _Lp>& __r)
1635 : _M_refcount(__r._M_refcount)
1639 _M_ptr = __r._M_ptr;
1643 template<
typename _Yp,
typename _Del,
1644 typename = _UniqCompatible<_Yp, _Del>>
1645 __shared_ptr(unique_ptr<_Yp, _Del>&& __r)
1646 : _M_ptr(__r.get()), _M_refcount()
1648 auto __raw = std::__to_address(__r.get());
1649 _M_refcount = __shared_count<_Lp>(
std::move(__r));
1650 _M_enable_shared_from_this_with(__raw);
1653#if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED
1656 template<
typename _Tp1,
typename _Del,
1657 typename enable_if<__and_<
1658 __not_<is_array<_Tp>>, is_array<_Tp1>,
1659 is_convertible<typename unique_ptr<_Tp1, _Del>::pointer, _Tp*>
1660 >::value,
bool>::type =
true>
1661 __shared_ptr(unique_ptr<_Tp1, _Del>&& __r, __sp_array_delete)
1662 : _M_ptr(__r.get()), _M_refcount()
1664 auto __raw = std::__to_address(__r.get());
1665 _M_refcount = __shared_count<_Lp>(
std::move(__r));
1666 _M_enable_shared_from_this_with(__raw);
1671#if _GLIBCXX_USE_DEPRECATED
1672#pragma GCC diagnostic push
1673#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1675 template<
typename _Yp,
typename = _Compatible<_Yp>>
1676 __shared_ptr(auto_ptr<_Yp>&& __r);
1677#pragma GCC diagnostic pop
1680 constexpr __shared_ptr(nullptr_t) noexcept : __shared_ptr() { }
1682 template<
typename _Yp>
1684 operator=(
const __shared_ptr<_Yp, _Lp>& __r)
noexcept
1686 _M_ptr = __r._M_ptr;
1687 _M_refcount = __r._M_refcount;
1691#if _GLIBCXX_USE_DEPRECATED
1692#pragma GCC diagnostic push
1693#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1694 template<
typename _Yp>
1696 operator=(auto_ptr<_Yp>&& __r)
1698 __shared_ptr(
std::move(__r)).swap(*
this);
1701#pragma GCC diagnostic pop
1705 operator=(__shared_ptr&& __r)
noexcept
1707 __shared_ptr(
std::move(__r)).swap(*
this);
1713 operator=(__shared_ptr<_Yp, _Lp>&& __r)
noexcept
1715 __shared_ptr(
std::move(__r)).swap(*
this);
1719 template<
typename _Yp,
typename _Del>
1720 _UniqAssignable<_Yp, _Del>
1721 operator=(unique_ptr<_Yp, _Del>&& __r)
1723 __shared_ptr(
std::move(__r)).swap(*
this);
1729 { __shared_ptr().swap(*
this); }
1731 template<
typename _Yp>
1736 __glibcxx_assert(__p ==
nullptr || __p != _M_ptr);
1737 __shared_ptr(__p).swap(*
this);
1740 template<
typename _Yp,
typename _Deleter>
1742 reset(_Yp* __p, _Deleter __d)
1743 { __shared_ptr(__p,
std::move(__d)).swap(*
this); }
1745 template<
typename _Yp,
typename _Deleter,
typename _Alloc>
1747 reset(_Yp* __p, _Deleter __d, _Alloc __a)
1752 get() const noexcept
1756 explicit operator bool() const noexcept
1757 {
return _M_ptr !=
nullptr; }
1761 unique() const noexcept
1762 {
return _M_refcount._M_unique(); }
1766 use_count() const noexcept
1767 {
return _M_refcount._M_get_use_count(); }
1771 swap(__shared_ptr<_Tp, _Lp>& __other)
noexcept
1773 std::swap(_M_ptr, __other._M_ptr);
1774 _M_refcount._M_swap(__other._M_refcount);
1784 template<
typename _Tp1>
1786 owner_before(__shared_ptr<_Tp1, _Lp>
const& __rhs)
const noexcept
1787 {
return _M_refcount._M_less(__rhs._M_refcount); }
1789 template<
typename _Tp1>
1791 owner_before(__weak_ptr<_Tp1, _Lp>
const& __rhs)
const noexcept
1792 {
return _M_refcount._M_less(__rhs._M_refcount); }
1795#ifdef __glibcxx_smart_ptr_owner_equality
1796 size_t owner_hash() const noexcept {
return _M_refcount._M_owner_hash(); }
1798 template<
typename _Tp1>
1800 owner_equal(__shared_ptr<_Tp1, _Lp>
const& __rhs)
const noexcept
1801 {
return _M_refcount == __rhs._M_refcount; }
1803 template<
typename _Tp1>
1805 owner_equal(__weak_ptr<_Tp1, _Lp>
const& __rhs)
const noexcept
1806 {
return _M_refcount == __rhs._M_refcount; }
1811 template<
typename _Alloc,
typename... _Args>
1812 __shared_ptr(_Sp_alloc_shared_tag<_Alloc> __tag, _Args&&... __args)
1813 : _M_ptr(), _M_refcount(_M_ptr, __tag, std::
forward<_Args>(__args)...)
1814 { _M_enable_shared_from_this_with(_M_ptr); }
1816 template<
typename _Tp1, _Lock_policy _Lp1,
typename _Alloc,
1818 friend __shared_ptr<_Tp1, _Lp1>
1819 __allocate_shared(
const _Alloc& __a, _Args&&... __args);
1821#if __glibcxx_shared_ptr_arrays >= 201707L
1823 template<
typename _Alloc,
typename _Init = const remove_extent_t<_Tp>*>
1824 __shared_ptr(
const _Sp_counted_array_base<_Alloc>& __a,
1825 _Init __init =
nullptr)
1826 : _M_ptr(), _M_refcount(_M_ptr, __a, __init)
1832 __shared_ptr(
const __weak_ptr<_Tp, _Lp>& __r, std::nothrow_t) noexcept
1833 : _M_refcount(__r._M_refcount, std::nothrow)
1835 _M_ptr = _M_refcount._M_get_use_count() ? __r._M_ptr :
nullptr;
1838 friend class __weak_ptr<_Tp, _Lp>;
1842 template<
typename _Yp>
1843 using __esft_base_t =
decltype(__enable_shared_from_this_base(
1848 template<
typename _Yp,
typename =
void>
1849 struct __has_esft_base
1852 template<
typename _Yp>
1853 struct __has_esft_base<_Yp, __void_t<__esft_base_t<_Yp>>>
1854 : __not_<is_array<_Tp>> { };
1856 template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
1857 typename enable_if<__has_esft_base<_Yp2>::value>::type
1858 _M_enable_shared_from_this_with(_Yp* __p)
noexcept
1860 if (
auto __base = __enable_shared_from_this_base(_M_refcount, __p))
1861 __base->_M_weak_assign(
const_cast<_Yp2*
>(__p), _M_refcount);
1864 template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
1865 typename enable_if<!__has_esft_base<_Yp2>::value>::type
1866 _M_enable_shared_from_this_with(_Yp*)
noexcept
1870 _M_get_deleter(
const std::type_info& __ti)
const noexcept
1871 {
return _M_refcount._M_get_deleter(__ti); }
1873 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
1874 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
1876 template<
typename _Del,
typename _Tp1, _Lock_policy _Lp1>
1877 friend _Del* get_deleter(
const __shared_ptr<_Tp1, _Lp1>&)
noexcept;
1879 template<
typename _Del,
typename _Tp1>
1880 friend _Del* get_deleter(
const shared_ptr<_Tp1>&)
noexcept;
1882#ifdef __glibcxx_atomic_shared_ptr
1883 friend _Sp_atomic<shared_ptr<_Tp>>;
1885#ifdef __glibcxx_out_ptr
1886 template<
typename,
typename,
typename...>
friend class out_ptr_t;
1889 element_type* _M_ptr;
1890 __shared_count<_Lp> _M_refcount;
1895 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1897 operator==(
const __shared_ptr<_Tp1, _Lp>& __a,
1898 const __shared_ptr<_Tp2, _Lp>& __b)
noexcept
1899 {
return __a.get() == __b.get(); }
1901 template<
typename _Tp, _Lock_policy _Lp>
1903 operator==(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1906#ifdef __cpp_lib_three_way_comparison
1907 template<
typename _Tp,
typename _Up, _Lock_policy _Lp>
1908 inline strong_ordering
1909 operator<=>(
const __shared_ptr<_Tp, _Lp>& __a,
1910 const __shared_ptr<_Up, _Lp>& __b)
noexcept
1911 {
return compare_three_way()(__a.get(), __b.get()); }
1913 template<
typename _Tp, _Lock_policy _Lp>
1914 inline strong_ordering
1915 operator<=>(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1917 using pointer =
typename __shared_ptr<_Tp, _Lp>::element_type*;
1918 return compare_three_way()(__a.get(),
static_cast<pointer
>(
nullptr));
1921 template<
typename _Tp, _Lock_policy _Lp>
1923 operator==(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a)
noexcept
1926 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1928 operator!=(
const __shared_ptr<_Tp1, _Lp>& __a,
1929 const __shared_ptr<_Tp2, _Lp>& __b)
noexcept
1930 {
return __a.get() != __b.get(); }
1932 template<
typename _Tp, _Lock_policy _Lp>
1934 operator!=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1935 {
return (
bool)__a; }
1937 template<
typename _Tp, _Lock_policy _Lp>
1939 operator!=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a)
noexcept
1940 {
return (
bool)__a; }
1942 template<
typename _Tp,
typename _Up, _Lock_policy _Lp>
1944 operator<(
const __shared_ptr<_Tp, _Lp>& __a,
1945 const __shared_ptr<_Up, _Lp>& __b)
noexcept
1947 using _Tp_elt =
typename __shared_ptr<_Tp, _Lp>::element_type;
1948 using _Up_elt =
typename __shared_ptr<_Up, _Lp>::element_type;
1950 return less<_Vp>()(__a.get(), __b.get());
1953 template<
typename _Tp, _Lock_policy _Lp>
1955 operator<(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1957 using _Tp_elt =
typename __shared_ptr<_Tp, _Lp>::element_type;
1961 template<
typename _Tp, _Lock_policy _Lp>
1963 operator<(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a)
noexcept
1965 using _Tp_elt =
typename __shared_ptr<_Tp, _Lp>::element_type;
1969 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1971 operator<=(
const __shared_ptr<_Tp1, _Lp>& __a,
1972 const __shared_ptr<_Tp2, _Lp>& __b)
noexcept
1973 {
return !(__b < __a); }
1975 template<
typename _Tp, _Lock_policy _Lp>
1977 operator<=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1978 {
return !(
nullptr < __a); }
1980 template<
typename _Tp, _Lock_policy _Lp>
1982 operator<=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a)
noexcept
1983 {
return !(__a <
nullptr); }
1985 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1987 operator>(
const __shared_ptr<_Tp1, _Lp>& __a,
1988 const __shared_ptr<_Tp2, _Lp>& __b)
noexcept
1989 {
return (__b < __a); }
1991 template<
typename _Tp, _Lock_policy _Lp>
1993 operator>(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1994 {
return nullptr < __a; }
1996 template<
typename _Tp, _Lock_policy _Lp>
1998 operator>(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a)
noexcept
1999 {
return __a <
nullptr; }
2001 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
2003 operator>=(
const __shared_ptr<_Tp1, _Lp>& __a,
2004 const __shared_ptr<_Tp2, _Lp>& __b)
noexcept
2005 {
return !(__a < __b); }
2007 template<
typename _Tp, _Lock_policy _Lp>
2009 operator>=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
2010 {
return !(__a <
nullptr); }
2012 template<
typename _Tp, _Lock_policy _Lp>
2014 operator>=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a)
noexcept
2015 {
return !(
nullptr < __a); }
2019 template<
typename _Tp, _Lock_policy _Lp>
2021 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b)
noexcept
2031 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
2032 inline __shared_ptr<_Tp, _Lp>
2035 using _Sp = __shared_ptr<_Tp, _Lp>;
2036 return _Sp(__r,
static_cast<typename _Sp::element_type*
>(__r.get()));
2044 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
2045 inline __shared_ptr<_Tp, _Lp>
2048 using _Sp = __shared_ptr<_Tp, _Lp>;
2049 return _Sp(__r,
const_cast<typename _Sp::element_type*
>(__r.get()));
2057 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
2058 inline __shared_ptr<_Tp, _Lp>
2061 using _Sp = __shared_ptr<_Tp, _Lp>;
2062 if (
auto* __p =
dynamic_cast<typename _Sp::element_type*
>(__r.get()))
2063 return _Sp(__r, __p);
2067#if __cplusplus > 201402L
2068 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
2069 inline __shared_ptr<_Tp, _Lp>
2070 reinterpret_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r)
noexcept
2072 using _Sp = __shared_ptr<_Tp, _Lp>;
2073 return _Sp(__r,
reinterpret_cast<typename _Sp::element_type*
>(__r.get()));
2077 template<
typename _Tp, _Lock_policy _Lp>
2081 using element_type =
typename remove_extent<_Tp>::type;
2084 template<
typename _Yp,
typename _Res =
void>
2085 using _Compatible =
typename
2089 template<
typename _Yp>
2090 using _Assignable = _Compatible<_Yp, __weak_ptr&>;
2092#pragma GCC diagnostic push
2093#pragma GCC diagnostic ignored "-Wc++17-extensions"
2095 template<
typename _Yp>
2096 static element_type*
2097 _S_safe_upcast(
const __weak_ptr<_Yp, _Lp>& __r)
2112 else if constexpr (__and_<is_scalar<_At>,
is_scalar<_Bt>>::value)
2114#if _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_is_virtual_base_of)
2118 else if constexpr (!__builtin_is_virtual_base_of(_Tp, _Yp))
2125 return __r.lock().get();
2127#pragma GCC diagnostic pop
2130 constexpr __weak_ptr() noexcept
2131 : _M_ptr(
nullptr), _M_refcount()
2134 __weak_ptr(
const __weak_ptr&)
noexcept =
default;
2136 ~__weak_ptr() =
default;
2152 template<
typename _Yp,
typename = _Compatible<_Yp>>
2153 __weak_ptr(
const __weak_ptr<_Yp, _Lp>& __r) noexcept
2154 : _M_ptr(_S_safe_upcast(__r)), _M_refcount(__r._M_refcount)
2157 template<
typename _Yp,
typename = _Compatible<_Yp>>
2158 __weak_ptr(
const __shared_ptr<_Yp, _Lp>& __r) noexcept
2159 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
2162 __weak_ptr(__weak_ptr&& __r) noexcept
2163 : _M_ptr(__r._M_ptr), _M_refcount(
std::move(__r._M_refcount))
2164 { __r._M_ptr =
nullptr; }
2166 template<
typename _Yp,
typename = _Compatible<_Yp>>
2167 __weak_ptr(__weak_ptr<_Yp, _Lp>&& __r) noexcept
2168 : _M_ptr(_S_safe_upcast(__r)), _M_refcount(
std::move(__r._M_refcount))
2169 { __r._M_ptr =
nullptr; }
2172 operator=(
const __weak_ptr& __r)
noexcept =
default;
2174 template<
typename _Yp>
2176 operator=(
const __weak_ptr<_Yp, _Lp>& __r)
noexcept
2178 _M_ptr = _S_safe_upcast(__r);
2179 _M_refcount = __r._M_refcount;
2183 template<
typename _Yp>
2185 operator=(
const __shared_ptr<_Yp, _Lp>& __r)
noexcept
2187 _M_ptr = __r._M_ptr;
2188 _M_refcount = __r._M_refcount;
2193 operator=(__weak_ptr&& __r)
noexcept
2199 template<
typename _Yp>
2201 operator=(__weak_ptr<_Yp, _Lp>&& __r)
noexcept
2203 _M_ptr = _S_safe_upcast(__r);
2204 _M_refcount =
std::move(__r._M_refcount);
2205 __r._M_ptr =
nullptr;
2209 __shared_ptr<_Tp, _Lp>
2210 lock() const noexcept
2211 {
return __shared_ptr<_Tp, _Lp>(*
this, std::nothrow); }
2214 use_count() const noexcept
2215 {
return _M_refcount._M_get_use_count(); }
2218 expired() const noexcept
2219 {
return _M_refcount._M_get_use_count() == 0; }
2221 template<
typename _Tp1>
2223 owner_before(
const __shared_ptr<_Tp1, _Lp>& __rhs)
const noexcept
2224 {
return _M_refcount._M_less(__rhs._M_refcount); }
2226 template<
typename _Tp1>
2228 owner_before(
const __weak_ptr<_Tp1, _Lp>& __rhs)
const noexcept
2229 {
return _M_refcount._M_less(__rhs._M_refcount); }
2231#ifdef __glibcxx_smart_ptr_owner_equality
2232 size_t owner_hash() const noexcept {
return _M_refcount._M_owner_hash(); }
2234 template<
typename _Tp1>
2236 owner_equal(
const __shared_ptr<_Tp1, _Lp> & __rhs)
const noexcept
2237 {
return _M_refcount == __rhs._M_refcount; }
2239 template<
typename _Tp1>
2241 owner_equal(
const __weak_ptr<_Tp1, _Lp> & __rhs)
const noexcept
2242 {
return _M_refcount == __rhs._M_refcount; }
2247 { __weak_ptr().swap(*
this); }
2250 swap(__weak_ptr& __s)
noexcept
2252 std::swap(_M_ptr, __s._M_ptr);
2253 _M_refcount._M_swap(__s._M_refcount);
2259 _M_assign(_Tp* __ptr,
const __shared_count<_Lp>& __refcount)
noexcept
2261 if (use_count() == 0)
2264 _M_refcount = __refcount;
2268 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
2269 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
2270 friend class __enable_shared_from_this<_Tp, _Lp>;
2272#ifdef __glibcxx_atomic_shared_ptr
2273 friend _Sp_atomic<weak_ptr<_Tp>>;
2276 element_type* _M_ptr;
2277 __weak_count<_Lp> _M_refcount;
2281 template<
typename _Tp, _Lock_policy _Lp>
2283 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b)
noexcept
2286#pragma GCC diagnostic push
2287#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
2288 template<
typename _Tp,
typename _Tp1>
2292 operator()(
const _Tp& __lhs,
const _Tp& __rhs)
const noexcept
2293 {
return __lhs.owner_before(__rhs); }
2296 operator()(
const _Tp& __lhs,
const _Tp1& __rhs)
const noexcept
2297 {
return __lhs.owner_before(__rhs); }
2300 operator()(
const _Tp1& __lhs,
const _Tp& __rhs)
const noexcept
2301 {
return __lhs.owner_before(__rhs); }
2303#pragma GCC diagnostic pop
2306 struct _Sp_owner_less<void, void>
2308 template<
typename _Tp,
typename _Up>
2310 operator()(
const _Tp& __lhs,
const _Up& __rhs)
const noexcept
2311 ->
decltype(__lhs.owner_before(__rhs))
2312 {
return __lhs.owner_before(__rhs); }
2314 using is_transparent = void;
2317 template<
typename _Tp, _Lock_policy _Lp>
2319 :
public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
2322 template<
typename _Tp, _Lock_policy _Lp>
2324 :
public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
2328 template<
typename _Tp, _Lock_policy _Lp>
2329 class __enable_shared_from_this
2332 constexpr __enable_shared_from_this() noexcept { }
2334 __enable_shared_from_this(
const __enable_shared_from_this&)
noexcept { }
2336 __enable_shared_from_this&
2337 operator=(
const __enable_shared_from_this&)
noexcept
2340 ~__enable_shared_from_this() { }
2343 __shared_ptr<_Tp, _Lp>
2345 {
return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
2347 __shared_ptr<const _Tp, _Lp>
2348 shared_from_this()
const
2349 {
return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
2351#if __cplusplus > 201402L || !defined(__STRICT_ANSI__)
2352 __weak_ptr<_Tp, _Lp>
2353 weak_from_this() noexcept
2354 {
return this->_M_weak_this; }
2356 __weak_ptr<const _Tp, _Lp>
2357 weak_from_this() const noexcept
2358 {
return this->_M_weak_this; }
2362 template<
typename _Tp1>
2364 _M_weak_assign(_Tp1* __p,
const __shared_count<_Lp>& __n)
const noexcept
2365 { _M_weak_this._M_assign(__p, __n); }
2367 friend const __enable_shared_from_this*
2368 __enable_shared_from_this_base(
const __shared_count<_Lp>&,
2369 const __enable_shared_from_this* __p)
2372 template<
typename, _Lock_policy>
2373 friend class __shared_ptr;
2375 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
2378 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy,
2379 typename _Alloc,
typename... _Args>
2380 inline __shared_ptr<_Tp, _Lp>
2381 __allocate_shared(
const _Alloc& __a, _Args&&... __args)
2385 return __shared_ptr<_Tp, _Lp>(_Sp_alloc_shared_tag<_Alloc>{__a},
2389 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy,
2391 inline __shared_ptr<_Tp, _Lp>
2392 __make_shared(_Args&&... __args)
2394 typedef typename std::remove_const<_Tp>::type _Tp_nc;
2395 return std::__allocate_shared<_Tp, _Lp>(std::allocator<_Tp_nc>(),
2400 template<
typename _Tp, _Lock_policy _Lp>
2401 struct hash<__shared_ptr<_Tp, _Lp>>
2402 :
public __hash_base<size_t, __shared_ptr<_Tp, _Lp>>
2405 operator()(
const __shared_ptr<_Tp, _Lp>& __s)
const noexcept
2412_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.