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"
76 template<
typename>
class auto_ptr;
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
151 { __gnu_cxx::__atomic_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
203 { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); }
207 _M_weak_release() noexcept
210 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
211 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1)
213 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
214 if (_Mutex_base<_Lp>::_S_need_barriers)
218 __atomic_thread_fence (__ATOMIC_ACQ_REL);
225 _M_get_use_count() const noexcept
229 return __atomic_load_n(&_M_use_count, __ATOMIC_RELAXED);
233 _Sp_counted_base(_Sp_counted_base
const&) =
delete;
234 _Sp_counted_base& operator=(_Sp_counted_base
const&) =
delete;
236 _Atomic_word _M_use_count;
237 _Atomic_word _M_weak_count;
242 _Sp_counted_base<_S_single>::
243 _M_add_ref_lock_nothrow() noexcept
245 if (_M_use_count == 0)
253 _Sp_counted_base<_S_mutex>::
254 _M_add_ref_lock_nothrow() noexcept
257 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
267 _Sp_counted_base<_S_atomic>::
268 _M_add_ref_lock_nothrow() noexcept
271 _Atomic_word __count = _M_get_use_count();
279 while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1,
280 true, __ATOMIC_ACQ_REL,
287 _Sp_counted_base<_S_single>::_M_add_ref_copy()
292 _Sp_counted_base<_S_single>::_M_release() noexcept
294 if (--_M_use_count == 0)
297 if (--_M_weak_count == 0)
304 _Sp_counted_base<_S_mutex>::_M_release() noexcept
307 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count);
308 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
310 _M_release_last_use();
316 _Sp_counted_base<_S_atomic>::_M_release() noexcept
318 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count);
320 constexpr bool __lock_free
321 = __atomic_always_lock_free(
sizeof(
long long), 0)
322 && __atomic_always_lock_free(
sizeof(_Atomic_word), 0);
323 constexpr bool __double_word
324 =
sizeof(
long long) == 2 *
sizeof(_Atomic_word);
327 constexpr bool __aligned = __alignof(
long long) <=
alignof(
void*);
328 if _GLIBCXX17_CONSTEXPR (__lock_free && __double_word && __aligned)
330 constexpr int __wordbits = __CHAR_BIT__ *
sizeof(_Atomic_word);
331 constexpr int __shiftbits = __double_word ? __wordbits : 0;
332 constexpr long long __unique_ref = 1LL + (1LL << __shiftbits);
333 auto __both_counts =
reinterpret_cast<long long*
>(&_M_use_count);
335 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
336 if (__atomic_load_n(__both_counts, __ATOMIC_ACQUIRE) == __unique_ref)
342 _M_weak_count = _M_use_count = 0;
343 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count);
344 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
349 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
352 _M_release_last_use_cold();
358 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
360 _M_release_last_use();
366 _Sp_counted_base<_S_single>::_M_weak_add_ref() noexcept
371 _Sp_counted_base<_S_single>::_M_weak_release() noexcept
373 if (--_M_weak_count == 0)
379 _Sp_counted_base<_S_single>::_M_get_use_count() const noexcept
380 {
return _M_use_count; }
384 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
387 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
390 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
391 class __enable_shared_from_this;
393 template<
typename _Tp>
396 template<
typename _Tp>
399 template<
typename _Tp>
402 template<
typename _Tp>
403 class enable_shared_from_this;
405 template<_Lock_policy _Lp = __default_lock_policy>
408 template<_Lock_policy _Lp = __default_lock_policy>
409 class __shared_count;
411#ifdef __glibcxx_atomic_shared_ptr
417 template<
typename _Ptr, _Lock_policy _Lp>
418 class _Sp_counted_ptr final :
public _Sp_counted_base<_Lp>
422 _Sp_counted_ptr(_Ptr __p) noexcept
426 _M_dispose() noexcept
430 _M_destroy() noexcept
437 _Sp_counted_ptr(
const _Sp_counted_ptr&) =
delete;
438 _Sp_counted_ptr& operator=(
const _Sp_counted_ptr&) =
delete;
446 _Sp_counted_ptr<nullptr_t, _S_single>::_M_dispose() noexcept { }
450 _Sp_counted_ptr<nullptr_t, _S_mutex>::_M_dispose() noexcept { }
454 _Sp_counted_ptr<nullptr_t, _S_atomic>::_M_dispose() noexcept { }
461 template<
int _Nm,
typename _Tp,
462 bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
463 struct _Sp_ebo_helper;
466 template<
int _Nm,
typename _Tp>
467 struct _Sp_ebo_helper<_Nm, _Tp, true> :
private _Tp
469 explicit _Sp_ebo_helper(
const _Tp& __tp) : _Tp(__tp) { }
470 explicit _Sp_ebo_helper(_Tp&& __tp) : _Tp(
std::move(__tp)) { }
473 _S_get(_Sp_ebo_helper& __eboh) {
return static_cast<_Tp&
>(__eboh); }
477 template<
int _Nm,
typename _Tp>
478 struct _Sp_ebo_helper<_Nm, _Tp, false>
480 explicit _Sp_ebo_helper(
const _Tp& __tp) : _M_tp(__tp) { }
481 explicit _Sp_ebo_helper(_Tp&& __tp) : _M_tp(
std::move(__tp)) { }
484 _S_get(_Sp_ebo_helper& __eboh)
485 {
return __eboh._M_tp; }
492 template<
typename _Ptr,
typename _Deleter,
typename _Alloc, _Lock_policy _Lp>
493 class _Sp_counted_deleter final :
public _Sp_counted_base<_Lp>
495 class _Impl : _Sp_ebo_helper<0, _Deleter>, _Sp_ebo_helper<1, _Alloc>
497 typedef _Sp_ebo_helper<0, _Deleter> _Del_base;
498 typedef _Sp_ebo_helper<1, _Alloc> _Alloc_base;
501 _Impl(_Ptr __p, _Deleter __d,
const _Alloc& __a) noexcept
502 : _Del_base(
std::move(__d)), _Alloc_base(__a), _M_ptr(__p)
505 _Deleter& _M_del() noexcept {
return _Del_base::_S_get(*
this); }
506 _Alloc& _M_alloc() noexcept {
return _Alloc_base::_S_get(*
this); }
512 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_deleter>;
515 _Sp_counted_deleter(_Ptr __p, _Deleter __d) noexcept
516 : _M_impl(__p,
std::move(__d), _Alloc()) { }
519 _Sp_counted_deleter(_Ptr __p, _Deleter __d,
const _Alloc& __a) noexcept
522 ~_Sp_counted_deleter() noexcept { }
525 _M_dispose() noexcept
526 { _M_impl._M_del()(_M_impl._M_ptr); }
529 _M_destroy() noexcept
531 __allocator_type __a(_M_impl._M_alloc());
532 __allocated_ptr<__allocator_type> __guard_ptr{ __a,
this };
533 this->~_Sp_counted_deleter();
537 _M_get_deleter(
const type_info& __ti [[__gnu__::__unused__]])
noexcept
542 return __ti ==
typeid(_Deleter)
551#ifdef __glibcxx_out_ptr
552 template<
typename,
typename,
typename...>
friend class out_ptr_t;
559 struct _Sp_make_shared_tag
562 template<
typename _Tp,
typename _Alloc, _Lock_policy _Lp>
563 friend class _Sp_counted_ptr_inplace;
565 static const type_info&
566 _S_ti() noexcept _GLIBCXX_VISIBILITY(default)
568 alignas(type_info)
static constexpr char __tag[
sizeof(type_info)] = { };
569 return reinterpret_cast<const type_info&
>(__tag);
572 static bool _S_eq(
const type_info&)
noexcept;
575 template<
typename _Alloc>
576 struct _Sp_alloc_shared_tag
581 template<
typename _Tp,
typename _Alloc, _Lock_policy _Lp>
582 class _Sp_counted_ptr_inplace final :
public _Sp_counted_base<_Lp>
584 class _Impl : _Sp_ebo_helper<0, _Alloc>
586 typedef _Sp_ebo_helper<0, _Alloc> _A_base;
589 explicit _Impl(_Alloc __a) noexcept : _A_base(__a) { }
591 _Alloc& _M_alloc() noexcept {
return _A_base::_S_get(*
this); }
593 __gnu_cxx::__aligned_buffer<__remove_cv_t<_Tp>> _M_storage;
597 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>;
600 template<
typename... _Args>
601 _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
607 std::forward<_Args>(__args)...);
610 ~_Sp_counted_ptr_inplace() noexcept { }
613 _M_dispose() noexcept
620 _M_destroy() noexcept
622 __allocator_type __a(_M_impl._M_alloc());
623 __allocated_ptr<__allocator_type> __guard_ptr{ __a,
this };
624 this->~_Sp_counted_ptr_inplace();
628 friend class __shared_count<_Lp>;
639 if (&__ti == &_Sp_make_shared_tag::_S_ti()
642 __ti ==
typeid(_Sp_make_shared_tag)
644 _Sp_make_shared_tag::_S_eq(__ti)
652 _M_ptr() noexcept {
return _M_impl._M_storage._M_ptr(); }
657#ifdef __glibcxx_smart_ptr_for_overwrite
658 struct _Sp_overwrite_tag { };
664 template<
typename _Tp,
typename _Alloc, _Lock_policy _Lp>
665 requires is_same_v<typename _Alloc::value_type, _Sp_overwrite_tag>
666 class _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> final
668 template<typename _Tp, template<typename> class _Alloc, _Lock_policy _Lp>
669 class _Sp_counted_ptr_inplace<_Tp, _Alloc<_Sp_overwrite_tag>, _Lp> final
671 :
public _Sp_counted_base<_Lp>
673 [[no_unique_address]] _Alloc _M_alloc;
676 remove_cv_t<_Tp> _M_obj;
680 friend class __shared_count<_Lp>;
685 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>;
687 _Sp_counted_ptr_inplace(
const _Alloc& __a)
690 ::new((
void*)_M_ptr()) _Tp;
693 ~_Sp_counted_ptr_inplace() noexcept { }
696 _M_dispose() noexcept
703 _M_destroy() noexcept
705 using pointer =
typename allocator_traits<__allocator_type>::pointer;
706 __allocator_type __a(_M_alloc);
707 auto __p = pointer_traits<pointer>::pointer_to(*
this);
708 __allocated_ptr<__allocator_type> __guard_ptr{ __a, __p };
709 this->~_Sp_counted_ptr_inplace();
718#if __glibcxx_shared_ptr_arrays >= 201707L
719 struct _Sp_overwrite_tag;
722 template<
typename _Alloc>
723 struct _Sp_counted_array_base
725 [[no_unique_address]] _Alloc _M_alloc{};
727 bool _M_overwrite =
false;
729 typename allocator_traits<_Alloc>::pointer
730 _M_alloc_array(
size_t __tail)
732 return allocator_traits<_Alloc>::allocate(_M_alloc, _M_n + __tail);
736 _M_dealloc_array(
typename allocator_traits<_Alloc>::pointer __p,
739 allocator_traits<_Alloc>::deallocate(_M_alloc, __p, _M_n + __tail);
743 template<
typename _Init>
745 _M_init(
typename allocator_traits<_Alloc>::value_type* __p,
748 using _Tp = remove_pointer_t<_Init>;
749 using _Up =
typename allocator_traits<_Alloc>::value_type;
751 if constexpr (is_same_v<_Init, _Sp_overwrite_tag>)
756 else if (__init ==
nullptr)
757 std::__uninitialized_default_n_a(__p, _M_n, _M_alloc);
758 else if constexpr (!is_array_v<_Tp>)
759 std::__uninitialized_fill_n_a(__p, _M_n, *__init, _M_alloc);
762#pragma GCC diagnostic push
763#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
766 using value_type = _Up;
767 using difference_type = ptrdiff_t;
768 using pointer =
const _Up*;
769 using reference =
const _Up&;
770 using iterator_category = forward_iterator_tag;
776 _Iter& operator++() { ++_M_pos;
return *
this; }
777 _Iter operator++(
int) {
auto __i(*
this); ++_M_pos;
return __i; }
779 reference
operator*()
const {
return _M_p[_M_pos % _M_len]; }
780 pointer operator->()
const {
return _M_p + (_M_pos % _M_len); }
782 bool operator==(
const _Iter& __i)
const
783 {
return _M_pos == __i._M_pos; }
785#pragma GCC diagnostic pop
787 _Iter __first{_S_first_elem(__init),
sizeof(_Tp) /
sizeof(_Up)};
788 _Iter __last = __first;
789 __last._M_pos = _M_n;
790 std::__uninitialized_copy_a(__first, __last, __p, _M_alloc);
797 _M_dispose_array(
typename allocator_traits<_Alloc>::value_type* __p)
800 std::destroy_n(__p, _M_n);
805 allocator_traits<_Alloc>::destroy(_M_alloc, __p + __n);
810 template<
typename _Tp>
812 _S_first_elem(_Tp* __p) {
return __p; }
814 template<
typename _Tp,
size_t _Nm>
816 _S_first_elem(_Tp (*__p)[_Nm]) {
return _S_first_elem(*__p); }
821 template<
typename _Alloc, _Lock_policy _Lp>
822 class _Sp_counted_array final
823 :
public _Sp_counted_base<_Lp>, _Sp_counted_array_base<_Alloc>
825 using pointer =
typename allocator_traits<_Alloc>::pointer;
827 pointer _M_alloc_ptr;
831 friend class __shared_count<_Lp>;
834 _Sp_counted_array(
const _Sp_counted_array_base<_Alloc>& __a,
835 pointer __p) noexcept
836 : _Sp_counted_array_base<_Alloc>(__a), _M_alloc_ptr(__p)
839 ~_Sp_counted_array() =
default;
842 _M_dispose() noexcept
845 this->_M_dispose_array(_M_ptr());
850 _M_destroy() noexcept
852 _Sp_counted_array_base<_Alloc> __a = *
this;
853 pointer __p = _M_alloc_ptr;
854 this->~_Sp_counted_array();
855 __a._M_dealloc_array(__p, _S_tail());
860 static constexpr size_t
864 using _Tp =
typename allocator_traits<_Alloc>::value_type;
867 size_t __bytes =
sizeof(_Sp_counted_array);
870 if constexpr (
alignof(_Tp) <
alignof(_Sp_counted_array))
871 __bytes +=
alignof(_Sp_counted_array) -
alignof(_Tp);
873 return (__bytes +
sizeof(_Tp) - 1) /
sizeof(_Tp);
883 struct __sp_array_delete
885 template<
typename _Yp>
886 void operator()(_Yp* __p)
const {
delete[] __p; }
889 template<_Lock_policy _Lp>
893 template<
typename _Tp>
894 struct __not_alloc_shared_tag {
using type = void; };
896 template<
typename _Tp>
897 struct __not_alloc_shared_tag<_Sp_alloc_shared_tag<_Tp>> { };
899#if __glibcxx_shared_ptr_arrays >= 201707L
900 template<
typename _Alloc>
901 struct __not_alloc_shared_tag<_Sp_counted_array_base<_Alloc>> { };
905 constexpr __shared_count() noexcept : _M_pi(0)
908 template<
typename _Ptr>
910 __shared_count(_Ptr __p) : _M_pi(0)
914 _M_pi =
new _Sp_counted_ptr<_Ptr, _Lp>(__p);
919 __throw_exception_again;
923 template<
typename _Ptr>
924 __shared_count(_Ptr __p, false_type)
925 : __shared_count(__p)
928 template<
typename _Ptr>
929 __shared_count(_Ptr __p, true_type)
930 : __shared_count(__p, __sp_array_delete{}, allocator<void>())
933 template<
typename _Ptr,
typename _Deleter,
934 typename =
typename __not_alloc_shared_tag<_Deleter>::type>
935 __shared_count(_Ptr __p, _Deleter __d)
936 : __shared_count(__p,
std::
move(__d), allocator<void>())
939 template<
typename _Ptr,
typename _Deleter,
typename _Alloc,
940 typename =
typename __not_alloc_shared_tag<_Deleter>::type>
941 __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
943 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
946 typename _Sp_cd_type::__allocator_type __a2(__a);
947 auto __guard = std::__allocate_guarded(__a2);
948 _Sp_cd_type* __mem = __guard.get();
956 __throw_exception_again;
960 template<
typename _Tp,
typename _Alloc,
typename... _Args>
961 __shared_count(_Tp*& __p, _Sp_alloc_shared_tag<_Alloc> __a,
964 using _Tp2 = __remove_cv_t<_Tp>;
965 using _Sp_cp_type = _Sp_counted_ptr_inplace<_Tp2, _Alloc, _Lp>;
966 typename _Sp_cp_type::__allocator_type __a2(__a._M_a);
967 auto __guard = std::__allocate_guarded(__a2);
968 _Sp_cp_type* __mem = __guard.get();
969 auto __pi = ::new (__mem)
970 _Sp_cp_type(__a._M_a, std::forward<_Args>(__args)...);
973 __p = __pi->_M_ptr();
976#if __glibcxx_shared_ptr_arrays >= 201707L
977 template<
typename _Tp,
typename _Alloc,
typename _Init>
978 __shared_count(_Tp*& __p,
const _Sp_counted_array_base<_Alloc>& __a,
981 using _Up = remove_all_extents_t<_Tp>;
982 static_assert(is_same_v<_Up, typename _Alloc::value_type>);
984 using _Sp_ca_type = _Sp_counted_array<_Alloc, _Lp>;
985 const size_t __tail = _Sp_ca_type::_S_tail();
987 struct _Guarded_ptr : _Sp_counted_array_base<_Alloc>
989 typename allocator_traits<_Alloc>::pointer _M_ptr;
991 _Guarded_ptr(_Sp_counted_array_base<_Alloc> __a)
992 : _Sp_counted_array_base<_Alloc>(__a),
993 _M_ptr(this->_M_alloc_array(_Sp_ca_type::_S_tail()))
999 this->_M_dealloc_array(_M_ptr, _Sp_ca_type::_S_tail());
1003 _Guarded_ptr __guard{__a};
1005 __guard._M_init(__raw, __init);
1007 void* __c = __raw + __a._M_n;
1008 if constexpr (
alignof(_Up) <
alignof(_Sp_ca_type))
1010 size_t __space =
sizeof(_Up) * __tail;
1011 __c =
std::align(
alignof(_Sp_ca_type),
sizeof(_Sp_ca_type),
1014 auto __pi = ::new(__c) _Sp_ca_type(__guard, __guard._M_ptr);
1015 __guard._M_ptr =
nullptr;
1017 __p =
reinterpret_cast<_Tp*
>(__raw);
1021#if _GLIBCXX_USE_DEPRECATED
1022#pragma GCC diagnostic push
1023#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1025 template<
typename _Tp>
1028#pragma GCC diagnostic pop
1032 template<
typename _Tp,
typename _Del>
1038 if (__r.get() ==
nullptr)
1041 using _Ptr =
typename unique_ptr<_Tp, _Del>::pointer;
1042 using _Del2 = __conditional_t<is_reference<_Del>::value,
1043 reference_wrapper<typename remove_reference<_Del>::type>,
1046 = _Sp_counted_deleter<_Ptr, _Del2, allocator<void>, _Lp>;
1047 using _Alloc = allocator<_Sp_cd_type>;
1048 using _Alloc_traits = allocator_traits<_Alloc>;
1050 _Sp_cd_type* __mem = _Alloc_traits::allocate(__a, 1);
1054 _Alloc_traits::construct(__a, __mem, __r.release(),
1055 std::forward<_Del>(__r.get_deleter()));
1060 explicit __shared_count(
const __weak_count<_Lp>& __r);
1064 __shared_count(
const __weak_count<_Lp>& __r, std::nothrow_t)
noexcept;
1066 ~__shared_count() noexcept
1068 if (_M_pi !=
nullptr)
1069 _M_pi->_M_release();
1072 __shared_count(
const __shared_count& __r) noexcept
1075 if (_M_pi !=
nullptr)
1076 _M_pi->_M_add_ref_copy();
1080 operator=(
const __shared_count& __r)
noexcept
1082 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1085 if (__tmp !=
nullptr)
1086 __tmp->_M_add_ref_copy();
1087 if (_M_pi !=
nullptr)
1088 _M_pi->_M_release();
1095 _M_swap(__shared_count& __r)
noexcept
1097 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1103 _M_get_use_count() const noexcept
1104 {
return _M_pi ? _M_pi->_M_get_use_count() : 0; }
1107 _M_unique() const noexcept
1108 {
return this->_M_get_use_count() == 1; }
1112 {
return _M_pi ? _M_pi->_M_get_deleter(__ti) :
nullptr; }
1115 _M_less(
const __shared_count& __rhs)
const noexcept
1119 _M_less(
const __weak_count<_Lp>& __rhs)
const noexcept
1124 operator==(
const __shared_count& __a,
const __shared_count& __b)
noexcept
1125 {
return __a._M_pi == __b._M_pi; }
1128 friend class __weak_count<_Lp>;
1129#ifdef __glibcxx_atomic_shared_ptr
1130 template<
typename>
friend class _Sp_atomic;
1132#ifdef __glibcxx_out_ptr
1133 template<
typename,
typename,
typename...>
friend class out_ptr_t;
1136 _Sp_counted_base<_Lp>* _M_pi;
1140 template<_Lock_policy _Lp>
1144 constexpr __weak_count() noexcept : _M_pi(
nullptr)
1147 __weak_count(
const __shared_count<_Lp>& __r) noexcept
1150 if (_M_pi !=
nullptr)
1151 _M_pi->_M_weak_add_ref();
1154 __weak_count(
const __weak_count& __r) noexcept
1157 if (_M_pi !=
nullptr)
1158 _M_pi->_M_weak_add_ref();
1161 __weak_count(__weak_count&& __r) noexcept
1163 { __r._M_pi =
nullptr; }
1165 ~__weak_count() noexcept
1167 if (_M_pi !=
nullptr)
1168 _M_pi->_M_weak_release();
1172 operator=(
const __shared_count<_Lp>& __r)
noexcept
1174 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1175 if (__tmp !=
nullptr)
1176 __tmp->_M_weak_add_ref();
1177 if (_M_pi !=
nullptr)
1178 _M_pi->_M_weak_release();
1184 operator=(
const __weak_count& __r)
noexcept
1186 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1187 if (__tmp !=
nullptr)
1188 __tmp->_M_weak_add_ref();
1189 if (_M_pi !=
nullptr)
1190 _M_pi->_M_weak_release();
1196 operator=(__weak_count&& __r)
noexcept
1198 if (_M_pi !=
nullptr)
1199 _M_pi->_M_weak_release();
1201 __r._M_pi =
nullptr;
1206 _M_swap(__weak_count& __r)
noexcept
1208 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1214 _M_get_use_count() const noexcept
1215 {
return _M_pi !=
nullptr ? _M_pi->_M_get_use_count() : 0; }
1218 _M_less(
const __weak_count& __rhs)
const noexcept
1222 _M_less(
const __shared_count<_Lp>& __rhs)
const noexcept
1227 operator==(
const __weak_count& __a,
const __weak_count& __b)
noexcept
1228 {
return __a._M_pi == __b._M_pi; }
1231 friend class __shared_count<_Lp>;
1232#ifdef __glibcxx_atomic_shared_ptr
1233 template<
typename>
friend class _Sp_atomic;
1236 _Sp_counted_base<_Lp>* _M_pi;
1240 template<_Lock_policy _Lp>
1242 __shared_count<_Lp>::__shared_count(
const __weak_count<_Lp>& __r)
1245 if (_M_pi ==
nullptr || !_M_pi->_M_add_ref_lock_nothrow())
1246 __throw_bad_weak_ptr();
1250 template<_Lock_policy _Lp>
1252 __shared_count<_Lp>::
1253 __shared_count(
const __weak_count<_Lp>& __r, std::nothrow_t) noexcept
1256 if (_M_pi && !_M_pi->_M_add_ref_lock_nothrow())
1264 template<
typename _Yp_ptr,
typename _Tp_ptr>
1265 struct __sp_compatible_with
1269 template<
typename _Yp,
typename _Tp>
1270 struct __sp_compatible_with<_Yp*, _Tp*>
1271 : is_convertible<_Yp*, _Tp*>::type
1274 template<
typename _Up,
size_t _Nm>
1275 struct __sp_compatible_with<_Up(*)[_Nm], _Up(*)[]>
1279 template<
typename _Up,
size_t _Nm>
1280 struct __sp_compatible_with<_Up(*)[_Nm],
const _Up(*)[]>
1284 template<
typename _Up,
size_t _Nm>
1285 struct __sp_compatible_with<_Up(*)[_Nm],
volatile _Up(*)[]>
1289 template<
typename _Up,
size_t _Nm>
1290 struct __sp_compatible_with<_Up(*)[_Nm],
const volatile _Up(*)[]>
1295 template<
typename _Up,
size_t _Nm,
typename _Yp,
typename =
void>
1296 struct __sp_is_constructible_arrN
1300 template<
typename _Up,
size_t _Nm,
typename _Yp>
1301 struct __sp_is_constructible_arrN<_Up, _Nm, _Yp, __void_t<_Yp[_Nm]>>
1302 : is_convertible<_Yp(*)[_Nm], _Up(*)[_Nm]>::type
1306 template<
typename _Up,
typename _Yp,
typename =
void>
1307 struct __sp_is_constructible_arr
1311 template<
typename _Up,
typename _Yp>
1312 struct __sp_is_constructible_arr<_Up, _Yp, __void_t<_Yp[]>>
1313 : is_convertible<_Yp(*)[], _Up(*)[]>::type
1317 template<
typename _Tp,
typename _Yp>
1318 struct __sp_is_constructible;
1321 template<
typename _Up,
size_t _Nm,
typename _Yp>
1322 struct __sp_is_constructible<_Up[_Nm], _Yp>
1323 : __sp_is_constructible_arrN<_Up, _Nm, _Yp>::type
1327 template<
typename _Up,
typename _Yp>
1328 struct __sp_is_constructible<_Up[], _Yp>
1329 : __sp_is_constructible_arr<_Up, _Yp>::type
1333 template<
typename _Tp,
typename _Yp>
1334 struct __sp_is_constructible
1335 : is_convertible<_Yp*, _Tp*>::type
1339 template<
typename _Tp>
1340 [[__gnu__::__always_inline__]]
1342 __shared_ptr_deref(_Tp* __p)
1344 __glibcxx_assert(__p !=
nullptr);
1349 template<
typename _Tp, _Lock_policy _Lp,
1350 bool = is_array<_Tp>::value,
bool = is_void<_Tp>::value>
1351 class __shared_ptr_access
1354 using element_type = _Tp;
1358 {
return *std::__shared_ptr_deref(_M_get()); }
1361 operator->() const noexcept
1363 _GLIBCXX_DEBUG_PEDASSERT(_M_get() !=
nullptr);
1369 _M_get() const noexcept
1370 {
return static_cast<const __shared_ptr<_Tp, _Lp>*
>(
this)->get(); }
1374 template<
typename _Tp, _Lock_policy _Lp>
1375 class __shared_ptr_access<_Tp, _Lp, false, true>
1378 using element_type = _Tp;
1381 operator->() const noexcept
1383 auto __ptr =
static_cast<const __shared_ptr<_Tp, _Lp>*
>(
this)->get();
1384 _GLIBCXX_DEBUG_PEDASSERT(__ptr !=
nullptr);
1390 template<
typename _Tp, _Lock_policy _Lp>
1391 class __shared_ptr_access<_Tp, _Lp, true, false>
1394 using element_type =
typename remove_extent<_Tp>::type;
1396#if __cplusplus <= 201402L
1397 [[__deprecated__(
"shared_ptr<T[]>::operator* is absent from C++17")]]
1400 {
return *std::__shared_ptr_deref(_M_get()); }
1402 [[__deprecated__(
"shared_ptr<T[]>::operator-> is absent from C++17")]]
1404 operator->() const noexcept
1406 _GLIBCXX_DEBUG_PEDASSERT(_M_get() !=
nullptr);
1411#pragma GCC diagnostic push
1412#pragma GCC diagnostic ignored "-Wc++17-extensions"
1414 operator[](ptrdiff_t __i)
const noexcept
1416 if constexpr (extent<_Tp>::value)
1417 __glibcxx_assert(__i < extent<_Tp>::value);
1418 return std::__shared_ptr_deref(_M_get())[__i];
1420#pragma GCC diagnostic pop
1424 _M_get() const noexcept
1425 {
return static_cast<const __shared_ptr<_Tp, _Lp>*
>(
this)->get(); }
1428 template<
typename _Tp, _Lock_policy _Lp>
1430 :
public __shared_ptr_access<_Tp, _Lp>
1433 using element_type =
typename remove_extent<_Tp>::type;
1437 template<
typename _Yp>
1439 =
typename enable_if<__sp_is_constructible<_Tp, _Yp>::value>::type;
1442 template<
typename _Yp,
typename _Res =
void>
1443 using _Compatible =
typename
1444 enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type;
1447 template<
typename _Yp>
1448 using _Assignable = _Compatible<_Yp, __shared_ptr&>;
1451 template<
typename _Yp,
typename _Del,
typename _Res = void,
1452 typename _Ptr =
typename unique_ptr<_Yp, _Del>::pointer>
1453 using _UniqCompatible = __enable_if_t<__and_<
1454 __sp_compatible_with<_Yp*, _Tp*>,
1455 is_convertible<_Ptr, element_type*>,
1456 is_move_constructible<_Del>
1460 template<
typename _Yp,
typename _Del>
1461 using _UniqAssignable = _UniqCompatible<_Yp, _Del, __shared_ptr&>;
1465#if __cplusplus > 201402L
1466 using weak_type = __weak_ptr<_Tp, _Lp>;
1469 constexpr __shared_ptr() noexcept
1470 : _M_ptr(0), _M_refcount()
1473 template<
typename _Yp,
typename = _SafeConv<_Yp>>
1475 __shared_ptr(_Yp* __p)
1476 : _M_ptr(__p), _M_refcount(__p, typename is_array<_Tp>::type())
1478 static_assert( !is_void<_Yp>::value,
"incomplete type" );
1479 static_assert(
sizeof(_Yp) > 0,
"incomplete type" );
1480 _M_enable_shared_from_this_with(__p);
1483 template<
typename _Yp,
typename _Deleter,
typename = _SafeConv<_Yp>>
1484 __shared_ptr(_Yp* __p, _Deleter __d)
1485 : _M_ptr(__p), _M_refcount(__p,
std::
move(__d))
1487 static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
1488 "deleter expression d(p) is well-formed");
1489 _M_enable_shared_from_this_with(__p);
1492 template<
typename _Yp,
typename _Deleter,
typename _Alloc,
1493 typename = _SafeConv<_Yp>>
1494 __shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a)
1497 static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
1498 "deleter expression d(p) is well-formed");
1499 _M_enable_shared_from_this_with(__p);
1502 template<
typename _Deleter>
1503 __shared_ptr(nullptr_t __p, _Deleter __d)
1504 : _M_ptr(0), _M_refcount(__p,
std::
move(__d))
1507 template<
typename _Deleter,
typename _Alloc>
1508 __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
1513 template<
typename _Yp>
1514 __shared_ptr(
const __shared_ptr<_Yp, _Lp>& __r,
1515 element_type* __p) noexcept
1516 : _M_ptr(__p), _M_refcount(__r._M_refcount)
1520 template<
typename _Yp>
1521 __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r,
1522 element_type* __p) noexcept
1523 : _M_ptr(__p), _M_refcount()
1525 _M_refcount._M_swap(__r._M_refcount);
1526 __r._M_ptr =
nullptr;
1529 __shared_ptr(
const __shared_ptr&)
noexcept =
default;
1530 __shared_ptr& operator=(
const __shared_ptr&)
noexcept =
default;
1531 ~__shared_ptr() =
default;
1533 template<
typename _Yp,
typename = _Compatible<_Yp>>
1534 __shared_ptr(
const __shared_ptr<_Yp, _Lp>& __r) noexcept
1535 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1538 __shared_ptr(__shared_ptr&& __r) noexcept
1539 : _M_ptr(__r._M_ptr), _M_refcount()
1541 _M_refcount._M_swap(__r._M_refcount);
1542 __r._M_ptr =
nullptr;
1545 template<
typename _Yp,
typename = _Compatible<_Yp>>
1546 __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r) noexcept
1547 : _M_ptr(__r._M_ptr), _M_refcount()
1549 _M_refcount._M_swap(__r._M_refcount);
1550 __r._M_ptr =
nullptr;
1553 template<
typename _Yp,
typename = _Compatible<_Yp>>
1554 explicit __shared_ptr(
const __weak_ptr<_Yp, _Lp>& __r)
1555 : _M_refcount(__r._M_refcount)
1559 _M_ptr = __r._M_ptr;
1563 template<
typename _Yp,
typename _Del,
1564 typename = _UniqCompatible<_Yp, _Del>>
1565 __shared_ptr(unique_ptr<_Yp, _Del>&& __r)
1566 : _M_ptr(__r.get()), _M_refcount()
1568 auto __raw = std::__to_address(__r.get());
1569 _M_refcount = __shared_count<_Lp>(
std::move(__r));
1570 _M_enable_shared_from_this_with(__raw);
1573#if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED
1576 template<
typename _Tp1,
typename _Del,
1577 typename enable_if<__and_<
1578 __not_<is_array<_Tp>>, is_array<_Tp1>,
1579 is_convertible<typename unique_ptr<_Tp1, _Del>::pointer, _Tp*>
1580 >::value,
bool>::type =
true>
1581 __shared_ptr(unique_ptr<_Tp1, _Del>&& __r, __sp_array_delete)
1582 : _M_ptr(__r.get()), _M_refcount()
1584 auto __raw = std::__to_address(__r.get());
1585 _M_refcount = __shared_count<_Lp>(
std::move(__r));
1586 _M_enable_shared_from_this_with(__raw);
1591#if _GLIBCXX_USE_DEPRECATED
1592#pragma GCC diagnostic push
1593#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1595 template<
typename _Yp,
typename = _Compatible<_Yp>>
1596 __shared_ptr(auto_ptr<_Yp>&& __r);
1597#pragma GCC diagnostic pop
1600 constexpr __shared_ptr(nullptr_t) noexcept : __shared_ptr() { }
1602 template<
typename _Yp>
1604 operator=(
const __shared_ptr<_Yp, _Lp>& __r)
noexcept
1606 _M_ptr = __r._M_ptr;
1607 _M_refcount = __r._M_refcount;
1611#if _GLIBCXX_USE_DEPRECATED
1612#pragma GCC diagnostic push
1613#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1614 template<
typename _Yp>
1616 operator=(auto_ptr<_Yp>&& __r)
1618 __shared_ptr(
std::move(__r)).swap(*
this);
1621#pragma GCC diagnostic pop
1625 operator=(__shared_ptr&& __r)
noexcept
1627 __shared_ptr(
std::move(__r)).swap(*
this);
1633 operator=(__shared_ptr<_Yp, _Lp>&& __r)
noexcept
1635 __shared_ptr(
std::move(__r)).swap(*
this);
1639 template<
typename _Yp,
typename _Del>
1640 _UniqAssignable<_Yp, _Del>
1641 operator=(unique_ptr<_Yp, _Del>&& __r)
1643 __shared_ptr(
std::move(__r)).swap(*
this);
1649 { __shared_ptr().swap(*
this); }
1651 template<
typename _Yp>
1656 __glibcxx_assert(__p ==
nullptr || __p != _M_ptr);
1657 __shared_ptr(__p).swap(*
this);
1660 template<
typename _Yp,
typename _Deleter>
1662 reset(_Yp* __p, _Deleter __d)
1663 { __shared_ptr(__p,
std::move(__d)).swap(*
this); }
1665 template<
typename _Yp,
typename _Deleter,
typename _Alloc>
1667 reset(_Yp* __p, _Deleter __d, _Alloc __a)
1672 get() const noexcept
1676 explicit operator bool() const noexcept
1677 {
return _M_ptr !=
nullptr; }
1681 unique() const noexcept
1682 {
return _M_refcount._M_unique(); }
1686 use_count() const noexcept
1687 {
return _M_refcount._M_get_use_count(); }
1691 swap(__shared_ptr<_Tp, _Lp>& __other)
noexcept
1693 std::swap(_M_ptr, __other._M_ptr);
1694 _M_refcount._M_swap(__other._M_refcount);
1704 template<
typename _Tp1>
1706 owner_before(__shared_ptr<_Tp1, _Lp>
const& __rhs)
const noexcept
1707 {
return _M_refcount._M_less(__rhs._M_refcount); }
1709 template<
typename _Tp1>
1711 owner_before(__weak_ptr<_Tp1, _Lp>
const& __rhs)
const noexcept
1712 {
return _M_refcount._M_less(__rhs._M_refcount); }
1717 template<
typename _Alloc,
typename... _Args>
1718 __shared_ptr(_Sp_alloc_shared_tag<_Alloc> __tag, _Args&&... __args)
1719 : _M_ptr(), _M_refcount(_M_ptr, __tag,
std::
forward<_Args>(__args)...)
1720 { _M_enable_shared_from_this_with(_M_ptr); }
1722 template<
typename _Tp1, _Lock_policy _Lp1,
typename _Alloc,
1724 friend __shared_ptr<_Tp1, _Lp1>
1725 __allocate_shared(
const _Alloc& __a, _Args&&... __args);
1727#if __glibcxx_shared_ptr_arrays >= 201707L
1729 template<
typename _Alloc,
typename _Init = const remove_extent_t<_Tp>*>
1730 __shared_ptr(
const _Sp_counted_array_base<_Alloc>& __a,
1731 _Init __init =
nullptr)
1732 : _M_ptr(), _M_refcount(_M_ptr, __a, __init)
1738 __shared_ptr(
const __weak_ptr<_Tp, _Lp>& __r, std::nothrow_t) noexcept
1739 : _M_refcount(__r._M_refcount, std::nothrow)
1741 _M_ptr = _M_refcount._M_get_use_count() ? __r._M_ptr :
nullptr;
1744 friend class __weak_ptr<_Tp, _Lp>;
1748 template<
typename _Yp>
1749 using __esft_base_t =
decltype(__enable_shared_from_this_base(
1751 std::declval<_Yp*>()));
1754 template<
typename _Yp,
typename =
void>
1755 struct __has_esft_base
1758 template<
typename _Yp>
1759 struct __has_esft_base<_Yp, __void_t<__esft_base_t<_Yp>>>
1760 : __not_<is_array<_Tp>> { };
1762 template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
1763 typename enable_if<__has_esft_base<_Yp2>::value>::type
1764 _M_enable_shared_from_this_with(_Yp* __p)
noexcept
1766 if (
auto __base = __enable_shared_from_this_base(_M_refcount, __p))
1767 __base->_M_weak_assign(
const_cast<_Yp2*
>(__p), _M_refcount);
1770 template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
1771 typename enable_if<!__has_esft_base<_Yp2>::value>::type
1772 _M_enable_shared_from_this_with(_Yp*)
noexcept
1777 {
return _M_refcount._M_get_deleter(__ti); }
1779 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
1780 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
1782 template<
typename _Del,
typename _Tp1, _Lock_policy _Lp1>
1783 friend _Del* get_deleter(
const __shared_ptr<_Tp1, _Lp1>&)
noexcept;
1785 template<
typename _Del,
typename _Tp1>
1786 friend _Del* get_deleter(
const shared_ptr<_Tp1>&)
noexcept;
1788#ifdef __glibcxx_atomic_shared_ptr
1789 friend _Sp_atomic<shared_ptr<_Tp>>;
1791#ifdef __glibcxx_out_ptr
1792 template<
typename,
typename,
typename...>
friend class out_ptr_t;
1795 element_type* _M_ptr;
1796 __shared_count<_Lp> _M_refcount;
1801 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1803 operator==(
const __shared_ptr<_Tp1, _Lp>& __a,
1804 const __shared_ptr<_Tp2, _Lp>& __b)
noexcept
1805 {
return __a.get() == __b.get(); }
1807 template<
typename _Tp, _Lock_policy _Lp>
1809 operator==(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1812#ifdef __cpp_lib_three_way_comparison
1813 template<
typename _Tp,
typename _Up, _Lock_policy _Lp>
1814 inline strong_ordering
1815 operator<=>(
const __shared_ptr<_Tp, _Lp>& __a,
1816 const __shared_ptr<_Up, _Lp>& __b)
noexcept
1817 {
return compare_three_way()(__a.get(), __b.get()); }
1819 template<
typename _Tp, _Lock_policy _Lp>
1820 inline strong_ordering
1821 operator<=>(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1823 using pointer =
typename __shared_ptr<_Tp, _Lp>::element_type*;
1824 return compare_three_way()(__a.get(),
static_cast<pointer
>(
nullptr));
1827 template<
typename _Tp, _Lock_policy _Lp>
1829 operator==(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a)
noexcept
1832 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1834 operator!=(
const __shared_ptr<_Tp1, _Lp>& __a,
1835 const __shared_ptr<_Tp2, _Lp>& __b)
noexcept
1836 {
return __a.get() != __b.get(); }
1838 template<
typename _Tp, _Lock_policy _Lp>
1840 operator!=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1841 {
return (
bool)__a; }
1843 template<
typename _Tp, _Lock_policy _Lp>
1845 operator!=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a)
noexcept
1846 {
return (
bool)__a; }
1848 template<
typename _Tp,
typename _Up, _Lock_policy _Lp>
1850 operator<(
const __shared_ptr<_Tp, _Lp>& __a,
1851 const __shared_ptr<_Up, _Lp>& __b)
noexcept
1853 using _Tp_elt =
typename __shared_ptr<_Tp, _Lp>::element_type;
1854 using _Up_elt =
typename __shared_ptr<_Up, _Lp>::element_type;
1855 using _Vp =
typename common_type<_Tp_elt*, _Up_elt*>::type;
1856 return less<_Vp>()(__a.get(), __b.get());
1859 template<
typename _Tp, _Lock_policy _Lp>
1861 operator<(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1863 using _Tp_elt =
typename __shared_ptr<_Tp, _Lp>::element_type;
1864 return less<_Tp_elt*>()(__a.get(),
nullptr);
1867 template<
typename _Tp, _Lock_policy _Lp>
1869 operator<(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a)
noexcept
1871 using _Tp_elt =
typename __shared_ptr<_Tp, _Lp>::element_type;
1872 return less<_Tp_elt*>()(
nullptr, __a.get());
1875 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1877 operator<=(
const __shared_ptr<_Tp1, _Lp>& __a,
1878 const __shared_ptr<_Tp2, _Lp>& __b)
noexcept
1879 {
return !(__b < __a); }
1881 template<
typename _Tp, _Lock_policy _Lp>
1883 operator<=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1884 {
return !(
nullptr < __a); }
1886 template<
typename _Tp, _Lock_policy _Lp>
1888 operator<=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a)
noexcept
1889 {
return !(__a <
nullptr); }
1891 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1893 operator>(
const __shared_ptr<_Tp1, _Lp>& __a,
1894 const __shared_ptr<_Tp2, _Lp>& __b)
noexcept
1895 {
return (__b < __a); }
1897 template<
typename _Tp, _Lock_policy _Lp>
1899 operator>(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1900 {
return nullptr < __a; }
1902 template<
typename _Tp, _Lock_policy _Lp>
1904 operator>(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a)
noexcept
1905 {
return __a <
nullptr; }
1907 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1909 operator>=(
const __shared_ptr<_Tp1, _Lp>& __a,
1910 const __shared_ptr<_Tp2, _Lp>& __b)
noexcept
1911 {
return !(__a < __b); }
1913 template<
typename _Tp, _Lock_policy _Lp>
1915 operator>=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1916 {
return !(__a <
nullptr); }
1918 template<
typename _Tp, _Lock_policy _Lp>
1920 operator>=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a)
noexcept
1921 {
return !(
nullptr < __a); }
1925 template<
typename _Tp, _Lock_policy _Lp>
1927 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b)
noexcept
1937 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1938 inline __shared_ptr<_Tp, _Lp>
1941 using _Sp = __shared_ptr<_Tp, _Lp>;
1942 return _Sp(__r,
static_cast<typename _Sp::element_type*
>(__r.get()));
1950 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1951 inline __shared_ptr<_Tp, _Lp>
1954 using _Sp = __shared_ptr<_Tp, _Lp>;
1955 return _Sp(__r,
const_cast<typename _Sp::element_type*
>(__r.get()));
1963 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1964 inline __shared_ptr<_Tp, _Lp>
1967 using _Sp = __shared_ptr<_Tp, _Lp>;
1968 if (
auto* __p =
dynamic_cast<typename _Sp::element_type*
>(__r.get()))
1969 return _Sp(__r, __p);
1973#if __cplusplus > 201402L
1974 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1975 inline __shared_ptr<_Tp, _Lp>
1976 reinterpret_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r)
noexcept
1978 using _Sp = __shared_ptr<_Tp, _Lp>;
1979 return _Sp(__r,
reinterpret_cast<typename _Sp::element_type*
>(__r.get()));
1983 template<
typename _Tp, _Lock_policy _Lp>
1986 template<
typename _Yp,
typename _Res =
void>
1987 using _Compatible =
typename
1988 enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type;
1991 template<
typename _Yp>
1992 using _Assignable = _Compatible<_Yp, __weak_ptr&>;
1995 using element_type =
typename remove_extent<_Tp>::type;
1997 constexpr __weak_ptr() noexcept
1998 : _M_ptr(
nullptr), _M_refcount()
2001 __weak_ptr(
const __weak_ptr&)
noexcept =
default;
2003 ~__weak_ptr() =
default;
2019 template<
typename _Yp,
typename = _Compatible<_Yp>>
2020 __weak_ptr(
const __weak_ptr<_Yp, _Lp>& __r) noexcept
2021 : _M_refcount(__r._M_refcount)
2022 { _M_ptr = __r.lock().get(); }
2024 template<
typename _Yp,
typename = _Compatible<_Yp>>
2025 __weak_ptr(
const __shared_ptr<_Yp, _Lp>& __r) noexcept
2026 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
2029 __weak_ptr(__weak_ptr&& __r) noexcept
2030 : _M_ptr(__r._M_ptr), _M_refcount(
std::move(__r._M_refcount))
2031 { __r._M_ptr =
nullptr; }
2033 template<
typename _Yp,
typename = _Compatible<_Yp>>
2034 __weak_ptr(__weak_ptr<_Yp, _Lp>&& __r) noexcept
2035 : _M_ptr(__r.lock().get()), _M_refcount(
std::move(__r._M_refcount))
2036 { __r._M_ptr =
nullptr; }
2039 operator=(
const __weak_ptr& __r)
noexcept =
default;
2041 template<
typename _Yp>
2043 operator=(
const __weak_ptr<_Yp, _Lp>& __r)
noexcept
2045 _M_ptr = __r.lock().get();
2046 _M_refcount = __r._M_refcount;
2050 template<
typename _Yp>
2052 operator=(
const __shared_ptr<_Yp, _Lp>& __r)
noexcept
2054 _M_ptr = __r._M_ptr;
2055 _M_refcount = __r._M_refcount;
2060 operator=(__weak_ptr&& __r)
noexcept
2066 template<
typename _Yp>
2068 operator=(__weak_ptr<_Yp, _Lp>&& __r)
noexcept
2070 _M_ptr = __r.lock().get();
2071 _M_refcount =
std::move(__r._M_refcount);
2072 __r._M_ptr =
nullptr;
2076 __shared_ptr<_Tp, _Lp>
2077 lock() const noexcept
2078 {
return __shared_ptr<element_type, _Lp>(*
this, std::nothrow); }
2081 use_count() const noexcept
2082 {
return _M_refcount._M_get_use_count(); }
2085 expired() const noexcept
2086 {
return _M_refcount._M_get_use_count() == 0; }
2088 template<
typename _Tp1>
2090 owner_before(
const __shared_ptr<_Tp1, _Lp>& __rhs)
const noexcept
2091 {
return _M_refcount._M_less(__rhs._M_refcount); }
2093 template<
typename _Tp1>
2095 owner_before(
const __weak_ptr<_Tp1, _Lp>& __rhs)
const noexcept
2096 {
return _M_refcount._M_less(__rhs._M_refcount); }
2100 { __weak_ptr().swap(*
this); }
2103 swap(__weak_ptr& __s)
noexcept
2105 std::swap(_M_ptr, __s._M_ptr);
2106 _M_refcount._M_swap(__s._M_refcount);
2112 _M_assign(_Tp* __ptr,
const __shared_count<_Lp>& __refcount)
noexcept
2114 if (use_count() == 0)
2117 _M_refcount = __refcount;
2121 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
2122 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
2123 friend class __enable_shared_from_this<_Tp, _Lp>;
2124 friend class enable_shared_from_this<_Tp>;
2125#ifdef __glibcxx_atomic_shared_ptr
2126 friend _Sp_atomic<weak_ptr<_Tp>>;
2129 element_type* _M_ptr;
2130 __weak_count<_Lp> _M_refcount;
2134 template<
typename _Tp, _Lock_policy _Lp>
2136 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b)
noexcept
2139#pragma GCC diagnostic push
2140#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
2141 template<
typename _Tp,
typename _Tp1>
2142 struct _Sp_owner_less :
public binary_function<_Tp, _Tp, bool>
2145 operator()(
const _Tp& __lhs,
const _Tp& __rhs)
const noexcept
2146 {
return __lhs.owner_before(__rhs); }
2149 operator()(
const _Tp& __lhs,
const _Tp1& __rhs)
const noexcept
2150 {
return __lhs.owner_before(__rhs); }
2153 operator()(
const _Tp1& __lhs,
const _Tp& __rhs)
const noexcept
2154 {
return __lhs.owner_before(__rhs); }
2156#pragma GCC diagnostic pop
2159 struct _Sp_owner_less<void, void>
2161 template<
typename _Tp,
typename _Up>
2163 operator()(
const _Tp& __lhs,
const _Up& __rhs)
const noexcept
2164 ->
decltype(__lhs.owner_before(__rhs))
2165 {
return __lhs.owner_before(__rhs); }
2167 using is_transparent = void;
2170 template<
typename _Tp, _Lock_policy _Lp>
2171 struct owner_less<__shared_ptr<_Tp, _Lp>>
2172 :
public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
2175 template<
typename _Tp, _Lock_policy _Lp>
2176 struct owner_less<__weak_ptr<_Tp, _Lp>>
2177 :
public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
2181 template<
typename _Tp, _Lock_policy _Lp>
2182 class __enable_shared_from_this
2185 constexpr __enable_shared_from_this() noexcept { }
2187 __enable_shared_from_this(
const __enable_shared_from_this&)
noexcept { }
2189 __enable_shared_from_this&
2190 operator=(
const __enable_shared_from_this&)
noexcept
2193 ~__enable_shared_from_this() { }
2196 __shared_ptr<_Tp, _Lp>
2198 {
return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
2200 __shared_ptr<const _Tp, _Lp>
2201 shared_from_this()
const
2202 {
return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
2204#if __cplusplus > 201402L || !defined(__STRICT_ANSI__)
2205 __weak_ptr<_Tp, _Lp>
2206 weak_from_this() noexcept
2207 {
return this->_M_weak_this; }
2209 __weak_ptr<const _Tp, _Lp>
2210 weak_from_this() const noexcept
2211 {
return this->_M_weak_this; }
2215 template<
typename _Tp1>
2217 _M_weak_assign(_Tp1* __p,
const __shared_count<_Lp>& __n)
const noexcept
2218 { _M_weak_this._M_assign(__p, __n); }
2220 friend const __enable_shared_from_this*
2221 __enable_shared_from_this_base(
const __shared_count<_Lp>&,
2222 const __enable_shared_from_this* __p)
2225 template<
typename, _Lock_policy>
2226 friend class __shared_ptr;
2228 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
2231 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy,
2232 typename _Alloc,
typename... _Args>
2233 inline __shared_ptr<_Tp, _Lp>
2234 __allocate_shared(
const _Alloc& __a, _Args&&... __args)
2236 static_assert(!is_array<_Tp>::value,
"make_shared<T[]> not supported");
2238 return __shared_ptr<_Tp, _Lp>(_Sp_alloc_shared_tag<_Alloc>{__a},
2239 std::forward<_Args>(__args)...);
2242 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy,
2244 inline __shared_ptr<_Tp, _Lp>
2245 __make_shared(_Args&&... __args)
2247 typedef typename std::remove_const<_Tp>::type _Tp_nc;
2249 std::forward<_Args>(__args)...);
2253 template<
typename _Tp, _Lock_policy _Lp>
2254 struct hash<__shared_ptr<_Tp, _Lp>>
2255 :
public __hash_base<size_t, __shared_ptr<_Tp, _Lp>>
2258 operator()(
const __shared_ptr<_Tp, _Lp>& __s)
const noexcept
2265_GLIBCXX_END_NAMESPACE_VERSION
constexpr bool operator<=(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
constexpr bool operator>=(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
constexpr bool operator<(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
constexpr bool operator>(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
constexpr complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __count)
Default-initializes objects in the range [first,first+count).
void * align(size_t __align, size_t __size, void *&__ptr, size_t &__space) noexcept
Fit aligned storage in buffer.
constexpr _Tp * to_address(_Tp *__ptr) noexcept
Obtain address referenced by a pointer to an object.
__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.
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.
The standard allocator, as per C++03 [20.4.1].
Base class for all library exceptions.
A simple smart pointer providing strict ownership semantics.
Exception possibly thrown by shared_ptr.
virtual char const * what() const noexcept
One of the comparison functors.
A move-only smart pointer that manages unique ownership of a resource.