49#ifndef _SHARED_PTR_BASE_H
50#define _SHARED_PTR_BASE_H 1
63#if __cplusplus >= 202002L
69namespace std _GLIBCXX_VISIBILITY(default)
71_GLIBCXX_BEGIN_NAMESPACE_VERSION
73#if _GLIBCXX_USE_DEPRECATED
74#pragma GCC diagnostic push
75#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
77#pragma GCC diagnostic pop
87 virtual char const*
what() const noexcept;
94 __throw_bad_weak_ptr()
97 using __gnu_cxx::_Lock_policy;
98 using __gnu_cxx::__default_lock_policy;
99 using __gnu_cxx::_S_single;
100 using __gnu_cxx::_S_mutex;
101 using __gnu_cxx::_S_atomic;
104 template<_Lock_policy _Lp>
109 enum { _S_need_barriers = 0 };
113 class _Mutex_base<_S_mutex>
114 :
public __gnu_cxx::__mutex
120 enum { _S_need_barriers = 1 };
123 template<_Lock_policy _Lp = __default_lock_policy>
124 class _Sp_counted_base
125 :
public _Mutex_base<_Lp>
128 _Sp_counted_base() noexcept
129 : _M_use_count(1), _M_weak_count(1) { }
132 ~_Sp_counted_base() noexcept
138 _M_dispose() noexcept = 0;
142 _M_destroy() noexcept
146 _M_get_deleter(
const std::type_info&)
noexcept = 0;
151 { _S_chk(__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1)); }
157 if (!_M_add_ref_lock_nothrow())
158 __throw_bad_weak_ptr();
163 _M_add_ref_lock_nothrow() noexcept;
167 _M_release() noexcept;
171 _M_release_last_use() noexcept
173 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count);
179 if (_Mutex_base<_Lp>::_S_need_barriers)
181 __atomic_thread_fence (__ATOMIC_ACQ_REL);
185 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
186 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count,
189 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
195 __attribute__((__noinline__))
197 _M_release_last_use_cold() noexcept
198 { _M_release_last_use(); }
202 _M_weak_add_ref() noexcept
206 constexpr _Atomic_word __max = -1;
207 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, 1) == __max)
208 [[__unlikely__]] __builtin_trap();
213 _M_weak_release() noexcept
216 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
217 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1)
219 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
220 if (_Mutex_base<_Lp>::_S_need_barriers)
224 __atomic_thread_fence (__ATOMIC_ACQ_REL);
231 _M_get_use_count() const noexcept
235 auto __count = __atomic_load_n(&_M_use_count, __ATOMIC_RELAXED);
240 return static_cast<_Unsigned_count_type
>(__count);
244 _Sp_counted_base(_Sp_counted_base
const&) =
delete;
245 _Sp_counted_base& operator=(_Sp_counted_base
const&) =
delete;
247#pragma GCC diagnostic push
248#pragma GCC diagnostic ignored "-Wignored-attributes"
250 using _Unsigned_count_type = make_unsigned<_Atomic_word>::type;
251#pragma GCC diagnostic pop
256 _S_chk(_Atomic_word __count)
258 constexpr _Atomic_word __max_atomic_word = _Unsigned_count_type(-1)/2;
276 constexpr _Atomic_word __max
277 =
sizeof(long) >
sizeof(_Atomic_word) ? -1 : __max_atomic_word;
279 if (__count == __max) [[__unlikely__]]
283 _Atomic_word _M_use_count;
284 _Atomic_word _M_weak_count;
292 _Sp_counted_base<_S_single>::_M_add_ref_copy()
294 _S_chk(_M_use_count);
295 __gnu_cxx::__atomic_add_single(&_M_use_count, 1);
300 _Sp_counted_base<_S_single>::_M_weak_release() noexcept
302 if (__gnu_cxx::__exchange_and_add_single(&_M_weak_count, -1) == 1)
308 _Sp_counted_base<_S_single>::_M_get_use_count() const noexcept
310 return static_cast<_Unsigned_count_type
>(_M_use_count);
316 _Sp_counted_base<_S_single>::
317 _M_add_ref_lock_nothrow() noexcept
319 if (_M_use_count == 0)
327 _Sp_counted_base<_S_mutex>::
328 _M_add_ref_lock_nothrow() noexcept
330 __gnu_cxx::__scoped_lock sentry(*
this);
331 if (
auto __c = __gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1))
348 _Sp_counted_base<_S_atomic>::
349 _M_add_ref_lock_nothrow() noexcept
352 _Atomic_word __count = _M_get_use_count();
360 while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1,
361 true, __ATOMIC_ACQ_REL,
369 _Sp_counted_base<_S_single>::_M_release() noexcept
371 if (__gnu_cxx::__exchange_and_add_single(&_M_use_count, -1) == 1)
380 _Sp_counted_base<_S_mutex>::_M_release() noexcept
383 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count);
384 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
386 _M_release_last_use();
392 _Sp_counted_base<_S_atomic>::_M_release() noexcept
394#pragma GCC diagnostic push
395#pragma GCC diagnostic ignored "-Wc++17-extensions"
396 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count);
398 constexpr bool __lock_free
399 = __atomic_always_lock_free(
sizeof(
long long), 0)
400 && __atomic_always_lock_free(
sizeof(_Atomic_word), 0);
401 constexpr bool __double_word
402 =
sizeof(
long long) == 2 *
sizeof(_Atomic_word);
405 constexpr bool __aligned = __alignof(
long long) <=
alignof(
void*);
406 if constexpr (__lock_free && __double_word && __aligned)
408 constexpr int __wordbits = __CHAR_BIT__ *
sizeof(_Atomic_word);
409 constexpr int __shiftbits = __double_word ? __wordbits : 0;
410 constexpr long long __unique_ref = 1LL + (1LL << __shiftbits);
411 auto __both_counts =
reinterpret_cast<long long*
>(&_M_use_count);
413 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
414 if (__atomic_load_n(__both_counts, __ATOMIC_ACQUIRE) == __unique_ref)
420 _M_weak_count = _M_use_count = 0;
421 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count);
422 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
427 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
430 _M_release_last_use_cold();
436 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
438 _M_release_last_use();
440#pragma GCC diagnostic pop
444 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
447 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
450 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
451 class __enable_shared_from_this;
453 template<
typename _Tp>
456 template<
typename _Tp>
459 template<
typename _Tp>
462 template<
typename _Tp>
463 class enable_shared_from_this;
465 template<_Lock_policy _Lp = __default_lock_policy>
468 template<_Lock_policy _Lp = __default_lock_policy>
469 class __shared_count;
471#ifdef __glibcxx_atomic_shared_ptr
477 template<
typename _Ptr, _Lock_policy _Lp>
478 class _Sp_counted_ptr final :
public _Sp_counted_base<_Lp>
482 _Sp_counted_ptr(_Ptr __p) noexcept
486 _M_dispose() noexcept
490 _M_destroy() noexcept
494 _M_get_deleter(
const std::type_info&)
noexcept
497 _Sp_counted_ptr(
const _Sp_counted_ptr&) =
delete;
498 _Sp_counted_ptr& operator=(
const _Sp_counted_ptr&) =
delete;
506 _Sp_counted_ptr<nullptr_t, _S_single>::_M_dispose() noexcept { }
510 _Sp_counted_ptr<nullptr_t, _S_mutex>::_M_dispose() noexcept { }
514 _Sp_counted_ptr<nullptr_t, _S_atomic>::_M_dispose() noexcept { }
516#if ! __has_cpp_attribute(__no_unique_address__)
517#error "support for [[__no_unique_address__]] attribute is required"
520#if ! _GLIBCXX_INLINE_VERSION
523 template<
typename _Tp,
bool = !__is_final(_Tp) && __is_empty(_Tp)>
524 struct _Sp_ebo_helper;
526 template<
typename _Tp,
bool = true>
527 struct _Sp_ebo_helper;
531 template<
typename _Tp>
532 struct _Sp_ebo_helper<_Tp, true>
534 [[__no_unique_address__]] _Tp _M_obj;
537#if ! _GLIBCXX_INLINE_VERSION
539 template<
typename _Tp>
540 struct _Sp_ebo_helper<_Tp, false>
547 template<
typename _Ptr,
typename _Deleter,
typename _Alloc, _Lock_policy _Lp>
548 class _Sp_counted_deleter final :
public _Sp_counted_base<_Lp>
551 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_deleter>;
554 _Sp_counted_deleter(_Ptr __p, _Deleter __d) noexcept
555 : _M_del{
std::move(__d)}, _M_alloc{}, _M_ptr(__p) { }
558 _Sp_counted_deleter(_Ptr __p, _Deleter __d,
const _Alloc& __a) noexcept
559 : _M_del{
std::move(__d)}, _M_alloc{__a}, _M_ptr(__p) { }
561#pragma GCC diagnostic push
562#pragma GCC diagnostic ignored "-Wfree-nonheap-object"
563 template<
typename>
class auto_ptr;
564 ~_Sp_counted_deleter() noexcept { }
565#pragma GCC diagnostic pop
568 _M_dispose() noexcept
569 { _M_del._M_obj(_M_ptr); }
572 _M_destroy() noexcept
574 __allocator_type __a(_M_alloc._M_obj);
575 __allocated_ptr<__allocator_type> __guard_ptr{ __a,
this };
576 this->~_Sp_counted_deleter();
580 _M_get_deleter(
const type_info& __ti [[__gnu__::__unused__]])
noexcept
585 if (__ti ==
typeid(_Deleter))
592#ifdef __glibcxx_out_ptr
593 template<
typename,
typename,
typename...>
friend class out_ptr_t;
596 [[__no_unique_address__]] _Sp_ebo_helper<_Deleter> _M_del;
597 [[__no_unique_address__]] _Sp_ebo_helper<_Alloc> _M_alloc;
603 struct _Sp_make_shared_tag
606 template<
typename _Tp,
typename _Alloc, _Lock_policy _Lp>
607 friend class _Sp_counted_ptr_inplace;
609 static const type_info&
610 _S_ti() noexcept _GLIBCXX_VISIBILITY(default)
612 alignas(type_info)
static constexpr char __tag[
sizeof(type_info)] = { };
613 return reinterpret_cast<const type_info&
>(__tag);
616 static bool _S_eq(
const type_info&)
noexcept;
619 template<
typename _Alloc>
620 struct _Sp_alloc_shared_tag
625 template<
typename _Tp,
typename _Alloc, _Lock_policy _Lp>
626 class _Sp_counted_ptr_inplace final :
public _Sp_counted_base<_Lp>
629 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>;
632 template<
typename... _Args>
633 _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
642#pragma GCC diagnostic push
643#pragma GCC diagnostic ignored "-Warray-bounds"
644 ~_Sp_counted_ptr_inplace() noexcept { }
645#pragma GCC diagnostic pop
648 _M_dispose() noexcept
655 _M_destroy() noexcept
657 __allocator_type __a(_M_alloc._M_obj);
658 __allocated_ptr<__allocator_type> __guard_ptr{ __a,
this };
659 this->~_Sp_counted_ptr_inplace();
663 friend class __shared_count<_Lp>;
668 _M_get_deleter(
const std::type_info& __ti)
noexcept override
674 if (&__ti == &_Sp_make_shared_tag::_S_ti()
677 __ti ==
typeid(_Sp_make_shared_tag)
679 _Sp_make_shared_tag::_S_eq(__ti)
687 _M_ptr() noexcept {
return _M_storage._M_ptr(); }
689 [[__no_unique_address__]] _Sp_ebo_helper<_Alloc> _M_alloc;
690 __gnu_cxx::__aligned_buffer<__remove_cv_t<_Tp>> _M_storage;
693#ifdef __glibcxx_smart_ptr_for_overwrite
694 struct _Sp_overwrite_tag { };
699 template<
typename _Tp,
typename _Alloc, _Lock_policy _Lp>
700 requires is_same_v<typename _Alloc::value_type, _Sp_overwrite_tag>
701 class _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> final
702 :
public _Sp_counted_base<_Lp>
704 [[no_unique_address]] _Alloc _M_alloc;
707 remove_cv_t<_Tp> _M_obj;
711 friend class __shared_count<_Lp>;
716 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>;
718 _Sp_counted_ptr_inplace(
const _Alloc& __a)
721 ::new((
void*)_M_ptr()) _Tp;
724 ~_Sp_counted_ptr_inplace() noexcept { }
727 _M_dispose() noexcept
734 _M_destroy() noexcept
736 using pointer =
typename allocator_traits<__allocator_type>::pointer;
737 __allocator_type __a(_M_alloc);
738 auto __p = pointer_traits<pointer>::pointer_to(*
this);
739 __allocated_ptr<__allocator_type> __guard_ptr{ __a, __p };
740 this->~_Sp_counted_ptr_inplace();
744 _M_get_deleter(
const std::type_info&)
noexcept override
749#if __glibcxx_shared_ptr_arrays >= 201707L
750 struct _Sp_overwrite_tag;
753 template<
typename _Alloc>
754 struct _Sp_counted_array_base
756 [[no_unique_address]] _Alloc _M_alloc{};
758 bool _M_overwrite =
false;
760 typename allocator_traits<_Alloc>::pointer
761 _M_alloc_array(
size_t __tail)
763 return allocator_traits<_Alloc>::allocate(_M_alloc, _M_n + __tail);
767 _M_dealloc_array(
typename allocator_traits<_Alloc>::pointer __p,
770 allocator_traits<_Alloc>::deallocate(_M_alloc, __p, _M_n + __tail);
774 template<
typename _Init>
776 _M_init(
typename allocator_traits<_Alloc>::value_type* __p,
779 using _Tp = remove_pointer_t<_Init>;
780 using _Up =
typename allocator_traits<_Alloc>::value_type;
782 if constexpr (is_same_v<_Init, _Sp_overwrite_tag>)
787 else if (__init ==
nullptr)
788 std::__uninitialized_default_n_a(__p, _M_n, _M_alloc);
789 else if constexpr (!is_array_v<_Tp>)
790 std::__uninitialized_fill_n_a(__p, _M_n, *__init, _M_alloc);
793#pragma GCC diagnostic push
794#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
797 using value_type = _Up;
798 using difference_type = ptrdiff_t;
799 using pointer =
const _Up*;
800 using reference =
const _Up&;
801 using iterator_category = forward_iterator_tag;
807 _Iter& operator++() { ++_M_pos;
return *
this; }
808 _Iter operator++(
int) {
auto __i(*
this); ++_M_pos;
return __i; }
810 reference
operator*()
const {
return _M_p[_M_pos % _M_len]; }
811 pointer operator->()
const {
return _M_p + (_M_pos % _M_len); }
813 bool operator==(
const _Iter& __i)
const
814 {
return _M_pos == __i._M_pos; }
816#pragma GCC diagnostic pop
818 _Iter __first{_S_first_elem(__init),
sizeof(_Tp) /
sizeof(_Up)};
819 _Iter __last = __first;
820 __last._M_pos = _M_n;
821 std::__uninitialized_copy_a(__first, __last, __p, _M_alloc);
828 _M_dispose_array(
typename allocator_traits<_Alloc>::value_type* __p)
831 std::destroy_n(__p, _M_n);
836 allocator_traits<_Alloc>::destroy(_M_alloc, __p + __n);
841 template<
typename _Tp>
843 _S_first_elem(_Tp* __p) {
return __p; }
845 template<
typename _Tp,
size_t _Nm>
847 _S_first_elem(_Tp (*__p)[_Nm]) {
return _S_first_elem(*__p); }
852 template<
typename _Alloc, _Lock_policy _Lp>
853 class _Sp_counted_array final
854 :
public _Sp_counted_base<_Lp>, _Sp_counted_array_base<_Alloc>
856 using pointer =
typename allocator_traits<_Alloc>::pointer;
858 pointer _M_alloc_ptr;
862 friend class __shared_count<_Lp>;
865 _Sp_counted_array(
const _Sp_counted_array_base<_Alloc>& __a,
866 pointer __p) noexcept
867 : _Sp_counted_array_base<_Alloc>(__a), _M_alloc_ptr(__p)
870 ~_Sp_counted_array() =
default;
873 _M_dispose() noexcept
876 this->_M_dispose_array(_M_ptr());
881 _M_destroy() noexcept
883 _Sp_counted_array_base<_Alloc> __a = *
this;
884 pointer __p = _M_alloc_ptr;
885 this->~_Sp_counted_array();
886 __a._M_dealloc_array(__p, _S_tail());
891 static constexpr size_t
895 using _Tp =
typename allocator_traits<_Alloc>::value_type;
898 size_t __bytes =
sizeof(_Sp_counted_array);
901 if constexpr (
alignof(_Tp) <
alignof(_Sp_counted_array))
902 __bytes +=
alignof(_Sp_counted_array) -
alignof(_Tp);
904 return (__bytes +
sizeof(_Tp) - 1) /
sizeof(_Tp);
908 _M_get_deleter(
const std::type_info&)
noexcept override
914 struct __sp_array_delete
916 template<
typename _Yp>
917 void operator()(_Yp* __p)
const {
delete[] __p; }
920 template<_Lock_policy _Lp>
924 template<
typename _Tp>
925 struct __not_alloc_shared_tag {
using type = void; };
927 template<
typename _Tp>
928 struct __not_alloc_shared_tag<_Sp_alloc_shared_tag<_Tp>> { };
930#if __glibcxx_shared_ptr_arrays >= 201707L
931 template<
typename _Alloc>
932 struct __not_alloc_shared_tag<_Sp_counted_array_base<_Alloc>> { };
936 constexpr __shared_count() noexcept : _M_pi(0)
939 template<
typename _Ptr>
941 __shared_count(_Ptr __p) : _M_pi(0)
945 _M_pi =
new _Sp_counted_ptr<_Ptr, _Lp>(__p);
950 __throw_exception_again;
954 template<
typename _Ptr>
955 __shared_count(_Ptr __p, false_type)
956 : __shared_count(__p)
959 template<
typename _Ptr>
960 __shared_count(_Ptr __p, true_type)
961 : __shared_count(__p, __sp_array_delete{}, allocator<void>())
964 template<
typename _Ptr,
typename _Deleter,
965 typename =
typename __not_alloc_shared_tag<_Deleter>::type>
966 __shared_count(_Ptr __p, _Deleter __d)
967 : __shared_count(__p, std::move(__d), allocator<void>())
970 template<
typename _Ptr,
typename _Deleter,
typename _Alloc,
971 typename =
typename __not_alloc_shared_tag<_Deleter>::type>
972 __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
974 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
977 typename _Sp_cd_type::__allocator_type __a2(__a);
978 auto __guard = std::__allocate_guarded(__a2);
979 _Sp_cd_type* __mem = __guard.get();
987 __throw_exception_again;
991 template<
typename _Tp,
typename _Alloc,
typename... _Args>
992 __shared_count(_Tp*& __p, _Sp_alloc_shared_tag<_Alloc> __a,
995 using _Tp2 = __remove_cv_t<_Tp>;
996 using _Sp_cp_type = _Sp_counted_ptr_inplace<_Tp2, _Alloc, _Lp>;
997 typename _Sp_cp_type::__allocator_type __a2(__a._M_a);
998 auto __guard = std::__allocate_guarded(__a2);
999 _Sp_cp_type* __mem = __guard.get();
1000 auto __pi = ::new (__mem)
1004 __p = __pi->_M_ptr();
1007#if __glibcxx_shared_ptr_arrays >= 201707L
1008 template<
typename _Tp,
typename _Alloc,
typename _Init>
1009 __shared_count(_Tp*& __p,
const _Sp_counted_array_base<_Alloc>& __a,
1012 using _Up = remove_all_extents_t<_Tp>;
1013 static_assert(is_same_v<_Up, typename _Alloc::value_type>);
1015 using _Sp_ca_type = _Sp_counted_array<_Alloc, _Lp>;
1016 const size_t __tail = _Sp_ca_type::_S_tail();
1018 struct _Guarded_ptr : _Sp_counted_array_base<_Alloc>
1020 typename allocator_traits<_Alloc>::pointer _M_ptr;
1022 _Guarded_ptr(_Sp_counted_array_base<_Alloc> __a)
1023 : _Sp_counted_array_base<_Alloc>(__a),
1024 _M_ptr(this->_M_alloc_array(_Sp_ca_type::_S_tail()))
1030 this->_M_dealloc_array(_M_ptr, _Sp_ca_type::_S_tail());
1034 _Guarded_ptr __guard{__a};
1036 __guard._M_init(__raw, __init);
1038 void* __c = __raw + __a._M_n;
1039 if constexpr (
alignof(_Up) <
alignof(_Sp_ca_type))
1041 size_t __space =
sizeof(_Up) * __tail;
1042 __c =
std::align(
alignof(_Sp_ca_type),
sizeof(_Sp_ca_type),
1045 auto __pi = ::new(__c) _Sp_ca_type(__guard, __guard._M_ptr);
1046 __guard._M_ptr =
nullptr;
1048 __p =
reinterpret_cast<_Tp*
>(__raw);
1052#if _GLIBCXX_USE_DEPRECATED
1053#pragma GCC diagnostic push
1054#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1056 template<
typename _Tp>
1058 __shared_count(std::auto_ptr<_Tp>&& __r);
1059#pragma GCC diagnostic pop
1063 template<
typename _Tp,
typename _Del>
1065 __shared_count(std::unique_ptr<_Tp, _Del>&& __r) : _M_pi(0)
1069 if (__r.get() ==
nullptr)
1072 using _Ptr =
typename unique_ptr<_Tp, _Del>::pointer;
1073 using _Del2 = __conditional_t<is_reference<_Del>::value,
1074 reference_wrapper<
typename remove_reference<_Del>::type>,
1077 = _Sp_counted_deleter<_Ptr, _Del2, allocator<void>, _Lp>;
1078 using _Alloc = allocator<_Sp_cd_type>;
1079 using _Alloc_traits = allocator_traits<_Alloc>;
1081 _Sp_cd_type* __mem = _Alloc_traits::allocate(__a, 1);
1085 _Alloc_traits::construct(__a, __mem, __r.release(),
1091 explicit __shared_count(
const __weak_count<_Lp>& __r);
1095 __shared_count(
const __weak_count<_Lp>& __r, std::nothrow_t)
noexcept;
1097 ~__shared_count() noexcept
1099 if (_M_pi !=
nullptr)
1100 _M_pi->_M_release();
1103 __shared_count(
const __shared_count& __r) noexcept
1106 if (_M_pi !=
nullptr)
1107 _M_pi->_M_add_ref_copy();
1111 operator=(
const __shared_count& __r)
noexcept
1113 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1116 if (__tmp !=
nullptr)
1117 __tmp->_M_add_ref_copy();
1118 if (_M_pi !=
nullptr)
1119 _M_pi->_M_release();
1126 _M_swap(__shared_count& __r)
noexcept
1128 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1134 _M_get_use_count() const noexcept
1135 {
return _M_pi ? _M_pi->_M_get_use_count() : 0; }
1138 _M_unique() const noexcept
1139 {
return this->_M_get_use_count() == 1; }
1142 _M_get_deleter(
const std::type_info& __ti)
const noexcept
1143 {
return _M_pi ? _M_pi->_M_get_deleter(__ti) :
nullptr; }
1146 _M_less(
const __shared_count& __rhs)
const noexcept
1147 {
return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
1150 _M_less(
const __weak_count<_Lp>& __rhs)
const noexcept
1151 {
return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
1153#ifdef __glibcxx_smart_ptr_owner_equality
1155 _M_owner_hash() const noexcept
1156 {
return std::hash<_Sp_counted_base<_Lp>*>()(this->_M_pi); }
1161 operator==(
const __shared_count& __a,
const __shared_count& __b)
noexcept
1162 {
return __a._M_pi == __b._M_pi; }
1165 friend class __weak_count<_Lp>;
1166#ifdef __glibcxx_atomic_shared_ptr
1167 template<
typename>
friend class _Sp_atomic;
1169#ifdef __glibcxx_out_ptr
1170 template<
typename,
typename,
typename...>
friend class out_ptr_t;
1173 _Sp_counted_base<_Lp>* _M_pi;
1177 template<_Lock_policy _Lp>
1181 constexpr __weak_count() noexcept : _M_pi(
nullptr)
1184 __weak_count(
const __shared_count<_Lp>& __r) noexcept
1187 if (_M_pi !=
nullptr)
1188 _M_pi->_M_weak_add_ref();
1191 __weak_count(
const __weak_count& __r) noexcept
1194 if (_M_pi !=
nullptr)
1195 _M_pi->_M_weak_add_ref();
1198 __weak_count(__weak_count&& __r) noexcept
1200 { __r._M_pi =
nullptr; }
1202 ~__weak_count() noexcept
1204 if (_M_pi !=
nullptr)
1205 _M_pi->_M_weak_release();
1209 operator=(
const __shared_count<_Lp>& __r)
noexcept
1211 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1212 if (__tmp !=
nullptr)
1213 __tmp->_M_weak_add_ref();
1214 if (_M_pi !=
nullptr)
1215 _M_pi->_M_weak_release();
1221 operator=(
const __weak_count& __r)
noexcept
1223 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1224 if (__tmp !=
nullptr)
1225 __tmp->_M_weak_add_ref();
1226 if (_M_pi !=
nullptr)
1227 _M_pi->_M_weak_release();
1233 operator=(__weak_count&& __r)
noexcept
1235 if (_M_pi !=
nullptr)
1236 _M_pi->_M_weak_release();
1238 __r._M_pi =
nullptr;
1243 _M_swap(__weak_count& __r)
noexcept
1245 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1251 _M_get_use_count() const noexcept
1252 {
return _M_pi !=
nullptr ? _M_pi->_M_get_use_count() : 0; }
1255 _M_less(
const __weak_count& __rhs)
const noexcept
1256 {
return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
1259 _M_less(
const __shared_count<_Lp>& __rhs)
const noexcept
1260 {
return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
1262#ifdef __glibcxx_smart_ptr_owner_equality
1264 _M_owner_hash() const noexcept
1265 {
return std::hash<_Sp_counted_base<_Lp>*>()(this->_M_pi); }
1270 operator==(
const __weak_count& __a,
const __weak_count& __b)
noexcept
1271 {
return __a._M_pi == __b._M_pi; }
1274 friend class __shared_count<_Lp>;
1275#ifdef __glibcxx_atomic_shared_ptr
1276 template<
typename>
friend class _Sp_atomic;
1279 _Sp_counted_base<_Lp>* _M_pi;
1283 template<_Lock_policy _Lp>
1285 __shared_count<_Lp>::__shared_count(
const __weak_count<_Lp>& __r)
1288 if (_M_pi ==
nullptr || !_M_pi->_M_add_ref_lock_nothrow())
1289 __throw_bad_weak_ptr();
1293 template<_Lock_policy _Lp>
1295 __shared_count<_Lp>::
1296 __shared_count(
const __weak_count<_Lp>& __r, std::nothrow_t) noexcept
1299 if (_M_pi && !_M_pi->_M_add_ref_lock_nothrow())
1307 template<
typename _Yp_ptr,
typename _Tp_ptr>
1308 struct __sp_compatible_with
1312 template<
typename _Yp,
typename _Tp>
1313 struct __sp_compatible_with<_Yp*, _Tp*>
1314 : is_convertible<_Yp*, _Tp*>::type
1317 template<
typename _Up,
size_t _Nm>
1318 struct __sp_compatible_with<_Up(*)[_Nm], _Up(*)[]>
1322 template<
typename _Up,
size_t _Nm>
1323 struct __sp_compatible_with<_Up(*)[_Nm],
const _Up(*)[]>
1327 template<
typename _Up,
size_t _Nm>
1328 struct __sp_compatible_with<_Up(*)[_Nm],
volatile _Up(*)[]>
1332 template<
typename _Up,
size_t _Nm>
1333 struct __sp_compatible_with<_Up(*)[_Nm],
const volatile _Up(*)[]>
1338 template<
typename _Up,
size_t _Nm,
typename _Yp,
typename =
void>
1339 struct __sp_is_constructible_arrN
1343 template<
typename _Up,
size_t _Nm,
typename _Yp>
1344 struct __sp_is_constructible_arrN<_Up, _Nm, _Yp, __void_t<_Yp[_Nm]>>
1345 : is_convertible<_Yp(*)[_Nm], _Up(*)[_Nm]>::type
1349 template<
typename _Up,
typename _Yp,
typename =
void>
1350 struct __sp_is_constructible_arr
1354 template<
typename _Up,
typename _Yp>
1355 struct __sp_is_constructible_arr<_Up, _Yp, __void_t<_Yp[]>>
1356 : is_convertible<_Yp(*)[], _Up(*)[]>::type
1360 template<
typename _Tp,
typename _Yp>
1361 struct __sp_is_constructible;
1364 template<
typename _Up,
size_t _Nm,
typename _Yp>
1365 struct __sp_is_constructible<_Up[_Nm], _Yp>
1366 : __sp_is_constructible_arrN<_Up, _Nm, _Yp>::type
1370 template<
typename _Up,
typename _Yp>
1371 struct __sp_is_constructible<_Up[], _Yp>
1372 : __sp_is_constructible_arr<_Up, _Yp>::type
1376 template<
typename _Tp,
typename _Yp>
1377 struct __sp_is_constructible
1378 : is_convertible<_Yp*, _Tp*>::type
1382 template<
typename _Tp>
1383 [[__gnu__::__always_inline__]]
1385 __shared_ptr_deref(_Tp* __p)
1387 __glibcxx_assert(__p !=
nullptr);
1392 template<
typename _Tp, _Lock_policy _Lp,
1394 class __shared_ptr_access
1397 using element_type = _Tp;
1400 operator*() const noexcept
1401 {
return *std::__shared_ptr_deref(_M_get()); }
1404 operator->() const noexcept
1406 _GLIBCXX_DEBUG_PEDASSERT(_M_get() !=
nullptr);
1412 _M_get() const noexcept
1413 {
return static_cast<const __shared_ptr<_Tp, _Lp>*
>(
this)->get(); }
1417 template<
typename _Tp, _Lock_policy _Lp>
1418 class __shared_ptr_access<_Tp, _Lp, false, true>
1421 using element_type = _Tp;
1424 operator->() const noexcept
1426 auto __ptr =
static_cast<const __shared_ptr<_Tp, _Lp>*
>(
this)->get();
1427 _GLIBCXX_DEBUG_PEDASSERT(__ptr !=
nullptr);
1433 template<
typename _Tp, _Lock_policy _Lp>
1434 class __shared_ptr_access<_Tp, _Lp, true, false>
1437 using element_type =
typename remove_extent<_Tp>::type;
1439#if __cplusplus <= 201402L
1440 [[__deprecated__(
"shared_ptr<T[]>::operator* is absent from C++17")]]
1442 operator*() const noexcept
1443 {
return *std::__shared_ptr_deref(_M_get()); }
1445 [[__deprecated__(
"shared_ptr<T[]>::operator-> is absent from C++17")]]
1447 operator->() const noexcept
1449 _GLIBCXX_DEBUG_PEDASSERT(_M_get() !=
nullptr);
1454#pragma GCC diagnostic push
1455#pragma GCC diagnostic ignored "-Wc++17-extensions"
1457 operator[](ptrdiff_t __i)
const noexcept
1459 if constexpr (extent<_Tp>::value)
1460 __glibcxx_assert(__i < extent<_Tp>::value);
1461 return std::__shared_ptr_deref(_M_get())[__i];
1463#pragma GCC diagnostic pop
1467 _M_get() const noexcept
1468 {
return static_cast<const __shared_ptr<_Tp, _Lp>*
>(
this)->get(); }
1471 template<
typename _Tp, _Lock_policy _Lp>
1473 :
public __shared_ptr_access<_Tp, _Lp>
1476 using element_type =
typename remove_extent<_Tp>::type;
1480 template<
typename _Yp>
1482 =
typename enable_if<__sp_is_constructible<_Tp, _Yp>::value>::type;
1485 template<
typename _Yp,
typename _Res =
void>
1486 using _Compatible =
typename
1487 enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type;
1490 template<
typename _Yp>
1491 using _Assignable = _Compatible<_Yp, __shared_ptr&>;
1494 template<
typename _Yp,
typename _Del,
typename _Res = void,
1495 typename _Ptr =
typename unique_ptr<_Yp, _Del>::pointer>
1496 using _UniqCompatible = __enable_if_t<__and_<
1497 __sp_compatible_with<_Yp*, _Tp*>,
1498 is_convertible<_Ptr, element_type*>,
1499 is_move_constructible<_Del>
1503 template<
typename _Yp,
typename _Del>
1504 using _UniqAssignable = _UniqCompatible<_Yp, _Del, __shared_ptr&>;
1508#if __cplusplus > 201402L
1509 using weak_type = __weak_ptr<_Tp, _Lp>;
1512 constexpr __shared_ptr() noexcept
1513 : _M_ptr(0), _M_refcount()
1516 template<
typename _Yp,
typename = _SafeConv<_Yp>>
1518 __shared_ptr(_Yp* __p)
1519 : _M_ptr(__p), _M_refcount(__p, typename is_array<_Tp>::type())
1521 static_assert( !is_void<_Yp>::value,
"incomplete type" );
1522 static_assert(
sizeof(_Yp) > 0,
"incomplete type" );
1523 _M_enable_shared_from_this_with(__p);
1526 template<
typename _Yp,
typename _Deleter,
typename = _SafeConv<_Yp>>
1527 __shared_ptr(_Yp* __p, _Deleter __d)
1528 : _M_ptr(__p), _M_refcount(__p, std::
move(__d))
1530 static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
1531 "deleter expression d(p) is well-formed");
1532 _M_enable_shared_from_this_with(__p);
1535 template<
typename _Yp,
typename _Deleter,
typename _Alloc,
1536 typename = _SafeConv<_Yp>>
1537 __shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a)
1538 : _M_ptr(__p), _M_refcount(__p, std::
move(__d), std::
move(__a))
1540 static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
1541 "deleter expression d(p) is well-formed");
1542 _M_enable_shared_from_this_with(__p);
1545 template<
typename _Deleter>
1546 __shared_ptr(nullptr_t __p, _Deleter __d)
1547 : _M_ptr(0), _M_refcount(__p, std::
move(__d))
1550 template<
typename _Deleter,
typename _Alloc>
1551 __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
1552 : _M_ptr(0), _M_refcount(__p, std::
move(__d), std::
move(__a))
1556 template<
typename _Yp>
1557 __shared_ptr(
const __shared_ptr<_Yp, _Lp>& __r,
1558 element_type* __p) noexcept
1559 : _M_ptr(__p), _M_refcount(__r._M_refcount)
1563 template<
typename _Yp>
1564 __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r,
1565 element_type* __p) noexcept
1566 : _M_ptr(__p), _M_refcount()
1568 _M_refcount._M_swap(__r._M_refcount);
1569 __r._M_ptr =
nullptr;
1572 __shared_ptr(
const __shared_ptr&)
noexcept =
default;
1573 __shared_ptr& operator=(
const __shared_ptr&)
noexcept =
default;
1574 ~__shared_ptr() =
default;
1576 template<
typename _Yp,
typename = _Compatible<_Yp>>
1577 __shared_ptr(
const __shared_ptr<_Yp, _Lp>& __r) noexcept
1578 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1581 __shared_ptr(__shared_ptr&& __r) noexcept
1582 : _M_ptr(__r._M_ptr), _M_refcount()
1584 _M_refcount._M_swap(__r._M_refcount);
1585 __r._M_ptr =
nullptr;
1588 template<
typename _Yp,
typename = _Compatible<_Yp>>
1589 __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r) noexcept
1590 : _M_ptr(__r._M_ptr), _M_refcount()
1592 _M_refcount._M_swap(__r._M_refcount);
1593 __r._M_ptr =
nullptr;
1596 template<
typename _Yp,
typename = _Compatible<_Yp>>
1597 explicit __shared_ptr(
const __weak_ptr<_Yp, _Lp>& __r)
1598 : _M_refcount(__r._M_refcount)
1602 _M_ptr = __r._M_ptr;
1606 template<
typename _Yp,
typename _Del,
1607 typename = _UniqCompatible<_Yp, _Del>>
1608 __shared_ptr(unique_ptr<_Yp, _Del>&& __r)
1609 : _M_ptr(__r.get()), _M_refcount()
1611 auto __raw = std::__to_address(__r.get());
1612 _M_refcount = __shared_count<_Lp>(
std::move(__r));
1613 _M_enable_shared_from_this_with(__raw);
1616#if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED
1619 template<
typename _Tp1,
typename _Del,
1620 typename enable_if<__and_<
1621 __not_<is_array<_Tp>>, is_array<_Tp1>,
1622 is_convertible<typename unique_ptr<_Tp1, _Del>::pointer, _Tp*>
1623 >::value,
bool>::type =
true>
1624 __shared_ptr(unique_ptr<_Tp1, _Del>&& __r, __sp_array_delete)
1625 : _M_ptr(__r.get()), _M_refcount()
1627 auto __raw = std::__to_address(__r.get());
1628 _M_refcount = __shared_count<_Lp>(
std::move(__r));
1629 _M_enable_shared_from_this_with(__raw);
1634#if _GLIBCXX_USE_DEPRECATED
1635#pragma GCC diagnostic push
1636#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1638 template<
typename _Yp,
typename = _Compatible<_Yp>>
1639 __shared_ptr(auto_ptr<_Yp>&& __r);
1640#pragma GCC diagnostic pop
1643 constexpr __shared_ptr(nullptr_t) noexcept : __shared_ptr() { }
1645 template<
typename _Yp>
1647 operator=(
const __shared_ptr<_Yp, _Lp>& __r)
noexcept
1649 _M_ptr = __r._M_ptr;
1650 _M_refcount = __r._M_refcount;
1654#if _GLIBCXX_USE_DEPRECATED
1655#pragma GCC diagnostic push
1656#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1657 template<
typename _Yp>
1659 operator=(auto_ptr<_Yp>&& __r)
1661 __shared_ptr(
std::move(__r)).swap(*
this);
1664#pragma GCC diagnostic pop
1668 operator=(__shared_ptr&& __r)
noexcept
1670 __shared_ptr(
std::move(__r)).swap(*
this);
1676 operator=(__shared_ptr<_Yp, _Lp>&& __r)
noexcept
1678 __shared_ptr(
std::move(__r)).swap(*
this);
1682 template<
typename _Yp,
typename _Del>
1683 _UniqAssignable<_Yp, _Del>
1684 operator=(unique_ptr<_Yp, _Del>&& __r)
1686 __shared_ptr(
std::move(__r)).swap(*
this);
1692 { __shared_ptr().swap(*
this); }
1694 template<
typename _Yp>
1699 __glibcxx_assert(__p ==
nullptr || __p != _M_ptr);
1700 __shared_ptr(__p).swap(*
this);
1703 template<
typename _Yp,
typename _Deleter>
1705 reset(_Yp* __p, _Deleter __d)
1706 { __shared_ptr(__p,
std::move(__d)).swap(*
this); }
1708 template<
typename _Yp,
typename _Deleter,
typename _Alloc>
1710 reset(_Yp* __p, _Deleter __d, _Alloc __a)
1715 get() const noexcept
1719 explicit operator bool() const noexcept
1720 {
return _M_ptr !=
nullptr; }
1724 unique() const noexcept
1725 {
return _M_refcount._M_unique(); }
1729 use_count() const noexcept
1730 {
return _M_refcount._M_get_use_count(); }
1734 swap(__shared_ptr<_Tp, _Lp>& __other)
noexcept
1736 std::swap(_M_ptr, __other._M_ptr);
1737 _M_refcount._M_swap(__other._M_refcount);
1747 template<
typename _Tp1>
1749 owner_before(__shared_ptr<_Tp1, _Lp>
const& __rhs)
const noexcept
1750 {
return _M_refcount._M_less(__rhs._M_refcount); }
1752 template<
typename _Tp1>
1754 owner_before(__weak_ptr<_Tp1, _Lp>
const& __rhs)
const noexcept
1755 {
return _M_refcount._M_less(__rhs._M_refcount); }
1758#ifdef __glibcxx_smart_ptr_owner_equality
1759 size_t owner_hash() const noexcept {
return _M_refcount._M_owner_hash(); }
1761 template<
typename _Tp1>
1763 owner_equal(__shared_ptr<_Tp1, _Lp>
const& __rhs)
const noexcept
1764 {
return _M_refcount == __rhs._M_refcount; }
1766 template<
typename _Tp1>
1768 owner_equal(__weak_ptr<_Tp1, _Lp>
const& __rhs)
const noexcept
1769 {
return _M_refcount == __rhs._M_refcount; }
1774 template<
typename _Alloc,
typename... _Args>
1775 __shared_ptr(_Sp_alloc_shared_tag<_Alloc> __tag, _Args&&... __args)
1776 : _M_ptr(), _M_refcount(_M_ptr, __tag, std::
forward<_Args>(__args)...)
1777 { _M_enable_shared_from_this_with(_M_ptr); }
1779 template<
typename _Tp1, _Lock_policy _Lp1,
typename _Alloc,
1781 friend __shared_ptr<_Tp1, _Lp1>
1782 __allocate_shared(
const _Alloc& __a, _Args&&... __args);
1784#if __glibcxx_shared_ptr_arrays >= 201707L
1786 template<
typename _Alloc,
typename _Init = const remove_extent_t<_Tp>*>
1787 __shared_ptr(
const _Sp_counted_array_base<_Alloc>& __a,
1788 _Init __init =
nullptr)
1789 : _M_ptr(), _M_refcount(_M_ptr, __a, __init)
1795 __shared_ptr(
const __weak_ptr<_Tp, _Lp>& __r, std::nothrow_t) noexcept
1796 : _M_refcount(__r._M_refcount, std::nothrow)
1798 _M_ptr = _M_refcount._M_get_use_count() ? __r._M_ptr :
nullptr;
1801 friend class __weak_ptr<_Tp, _Lp>;
1805 template<
typename _Yp>
1806 using __esft_base_t =
decltype(__enable_shared_from_this_base(
1811 template<
typename _Yp,
typename =
void>
1812 struct __has_esft_base
1815 template<
typename _Yp>
1816 struct __has_esft_base<_Yp, __void_t<__esft_base_t<_Yp>>>
1817 : __not_<is_array<_Tp>> { };
1819 template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
1820 typename enable_if<__has_esft_base<_Yp2>::value>::type
1821 _M_enable_shared_from_this_with(_Yp* __p)
noexcept
1823 if (
auto __base = __enable_shared_from_this_base(_M_refcount, __p))
1824 __base->_M_weak_assign(
const_cast<_Yp2*
>(__p), _M_refcount);
1827 template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
1828 typename enable_if<!__has_esft_base<_Yp2>::value>::type
1829 _M_enable_shared_from_this_with(_Yp*)
noexcept
1833 _M_get_deleter(
const std::type_info& __ti)
const noexcept
1834 {
return _M_refcount._M_get_deleter(__ti); }
1836 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
1837 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
1839 template<
typename _Del,
typename _Tp1, _Lock_policy _Lp1>
1840 friend _Del* get_deleter(
const __shared_ptr<_Tp1, _Lp1>&)
noexcept;
1842 template<
typename _Del,
typename _Tp1>
1843 friend _Del* get_deleter(
const shared_ptr<_Tp1>&)
noexcept;
1845#ifdef __glibcxx_atomic_shared_ptr
1846 friend _Sp_atomic<shared_ptr<_Tp>>;
1848#ifdef __glibcxx_out_ptr
1849 template<
typename,
typename,
typename...>
friend class out_ptr_t;
1852 element_type* _M_ptr;
1853 __shared_count<_Lp> _M_refcount;
1858 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1860 operator==(
const __shared_ptr<_Tp1, _Lp>& __a,
1861 const __shared_ptr<_Tp2, _Lp>& __b)
noexcept
1862 {
return __a.get() == __b.get(); }
1864 template<
typename _Tp, _Lock_policy _Lp>
1866 operator==(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1869#ifdef __cpp_lib_three_way_comparison
1870 template<
typename _Tp,
typename _Up, _Lock_policy _Lp>
1871 inline strong_ordering
1872 operator<=>(
const __shared_ptr<_Tp, _Lp>& __a,
1873 const __shared_ptr<_Up, _Lp>& __b)
noexcept
1874 {
return compare_three_way()(__a.get(), __b.get()); }
1876 template<
typename _Tp, _Lock_policy _Lp>
1877 inline strong_ordering
1878 operator<=>(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1880 using pointer =
typename __shared_ptr<_Tp, _Lp>::element_type*;
1881 return compare_three_way()(__a.get(),
static_cast<pointer
>(
nullptr));
1884 template<
typename _Tp, _Lock_policy _Lp>
1886 operator==(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a)
noexcept
1889 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1891 operator!=(
const __shared_ptr<_Tp1, _Lp>& __a,
1892 const __shared_ptr<_Tp2, _Lp>& __b)
noexcept
1893 {
return __a.get() != __b.get(); }
1895 template<
typename _Tp, _Lock_policy _Lp>
1897 operator!=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1898 {
return (
bool)__a; }
1900 template<
typename _Tp, _Lock_policy _Lp>
1902 operator!=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a)
noexcept
1903 {
return (
bool)__a; }
1905 template<
typename _Tp,
typename _Up, _Lock_policy _Lp>
1907 operator<(
const __shared_ptr<_Tp, _Lp>& __a,
1908 const __shared_ptr<_Up, _Lp>& __b)
noexcept
1910 using _Tp_elt =
typename __shared_ptr<_Tp, _Lp>::element_type;
1911 using _Up_elt =
typename __shared_ptr<_Up, _Lp>::element_type;
1913 return less<_Vp>()(__a.get(), __b.get());
1916 template<
typename _Tp, _Lock_policy _Lp>
1918 operator<(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1920 using _Tp_elt =
typename __shared_ptr<_Tp, _Lp>::element_type;
1924 template<
typename _Tp, _Lock_policy _Lp>
1926 operator<(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a)
noexcept
1928 using _Tp_elt =
typename __shared_ptr<_Tp, _Lp>::element_type;
1932 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1934 operator<=(
const __shared_ptr<_Tp1, _Lp>& __a,
1935 const __shared_ptr<_Tp2, _Lp>& __b)
noexcept
1936 {
return !(__b < __a); }
1938 template<
typename _Tp, _Lock_policy _Lp>
1940 operator<=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1941 {
return !(
nullptr < __a); }
1943 template<
typename _Tp, _Lock_policy _Lp>
1945 operator<=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a)
noexcept
1946 {
return !(__a <
nullptr); }
1948 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1950 operator>(
const __shared_ptr<_Tp1, _Lp>& __a,
1951 const __shared_ptr<_Tp2, _Lp>& __b)
noexcept
1952 {
return (__b < __a); }
1954 template<
typename _Tp, _Lock_policy _Lp>
1956 operator>(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1957 {
return nullptr < __a; }
1959 template<
typename _Tp, _Lock_policy _Lp>
1961 operator>(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a)
noexcept
1962 {
return __a <
nullptr; }
1964 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1966 operator>=(
const __shared_ptr<_Tp1, _Lp>& __a,
1967 const __shared_ptr<_Tp2, _Lp>& __b)
noexcept
1968 {
return !(__a < __b); }
1970 template<
typename _Tp, _Lock_policy _Lp>
1972 operator>=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1973 {
return !(__a <
nullptr); }
1975 template<
typename _Tp, _Lock_policy _Lp>
1977 operator>=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a)
noexcept
1978 {
return !(
nullptr < __a); }
1982 template<
typename _Tp, _Lock_policy _Lp>
1984 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b)
noexcept
1994 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1995 inline __shared_ptr<_Tp, _Lp>
1998 using _Sp = __shared_ptr<_Tp, _Lp>;
1999 return _Sp(__r,
static_cast<typename _Sp::element_type*
>(__r.get()));
2007 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
2008 inline __shared_ptr<_Tp, _Lp>
2011 using _Sp = __shared_ptr<_Tp, _Lp>;
2012 return _Sp(__r,
const_cast<typename _Sp::element_type*
>(__r.get()));
2020 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
2021 inline __shared_ptr<_Tp, _Lp>
2024 using _Sp = __shared_ptr<_Tp, _Lp>;
2025 if (
auto* __p =
dynamic_cast<typename _Sp::element_type*
>(__r.get()))
2026 return _Sp(__r, __p);
2030#if __cplusplus > 201402L
2031 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
2032 inline __shared_ptr<_Tp, _Lp>
2033 reinterpret_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r)
noexcept
2035 using _Sp = __shared_ptr<_Tp, _Lp>;
2036 return _Sp(__r,
reinterpret_cast<typename _Sp::element_type*
>(__r.get()));
2040 template<
typename _Tp, _Lock_policy _Lp>
2044 using element_type =
typename remove_extent<_Tp>::type;
2047 template<
typename _Yp,
typename _Res =
void>
2048 using _Compatible =
typename
2052 template<
typename _Yp>
2053 using _Assignable = _Compatible<_Yp, __weak_ptr&>;
2055#pragma GCC diagnostic push
2056#pragma GCC diagnostic ignored "-Wc++17-extensions"
2058 template<
typename _Yp>
2059 static element_type*
2060 _S_safe_upcast(
const __weak_ptr<_Yp, _Lp>& __r)
2075 else if constexpr (__and_<is_scalar<_At>,
is_scalar<_Bt>>::value)
2077#if _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_is_virtual_base_of)
2081 else if constexpr (!__builtin_is_virtual_base_of(_Tp, _Yp))
2088 return __r.lock().get();
2090#pragma GCC diagnostic pop
2093 constexpr __weak_ptr() noexcept
2094 : _M_ptr(
nullptr), _M_refcount()
2097 __weak_ptr(
const __weak_ptr&)
noexcept =
default;
2099 ~__weak_ptr() =
default;
2115 template<
typename _Yp,
typename = _Compatible<_Yp>>
2116 __weak_ptr(
const __weak_ptr<_Yp, _Lp>& __r) noexcept
2117 : _M_ptr(_S_safe_upcast(__r)), _M_refcount(__r._M_refcount)
2120 template<
typename _Yp,
typename = _Compatible<_Yp>>
2121 __weak_ptr(
const __shared_ptr<_Yp, _Lp>& __r) noexcept
2122 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
2125 __weak_ptr(__weak_ptr&& __r) noexcept
2126 : _M_ptr(__r._M_ptr), _M_refcount(
std::move(__r._M_refcount))
2127 { __r._M_ptr =
nullptr; }
2129 template<
typename _Yp,
typename = _Compatible<_Yp>>
2130 __weak_ptr(__weak_ptr<_Yp, _Lp>&& __r) noexcept
2131 : _M_ptr(_S_safe_upcast(__r)), _M_refcount(
std::move(__r._M_refcount))
2132 { __r._M_ptr =
nullptr; }
2135 operator=(
const __weak_ptr& __r)
noexcept =
default;
2137 template<
typename _Yp>
2139 operator=(
const __weak_ptr<_Yp, _Lp>& __r)
noexcept
2141 _M_ptr = _S_safe_upcast(__r);
2142 _M_refcount = __r._M_refcount;
2146 template<
typename _Yp>
2148 operator=(
const __shared_ptr<_Yp, _Lp>& __r)
noexcept
2150 _M_ptr = __r._M_ptr;
2151 _M_refcount = __r._M_refcount;
2156 operator=(__weak_ptr&& __r)
noexcept
2162 template<
typename _Yp>
2164 operator=(__weak_ptr<_Yp, _Lp>&& __r)
noexcept
2166 _M_ptr = _S_safe_upcast(__r);
2167 _M_refcount =
std::move(__r._M_refcount);
2168 __r._M_ptr =
nullptr;
2172 __shared_ptr<_Tp, _Lp>
2173 lock() const noexcept
2174 {
return __shared_ptr<_Tp, _Lp>(*
this, std::nothrow); }
2177 use_count() const noexcept
2178 {
return _M_refcount._M_get_use_count(); }
2181 expired() const noexcept
2182 {
return _M_refcount._M_get_use_count() == 0; }
2184 template<
typename _Tp1>
2186 owner_before(
const __shared_ptr<_Tp1, _Lp>& __rhs)
const noexcept
2187 {
return _M_refcount._M_less(__rhs._M_refcount); }
2189 template<
typename _Tp1>
2191 owner_before(
const __weak_ptr<_Tp1, _Lp>& __rhs)
const noexcept
2192 {
return _M_refcount._M_less(__rhs._M_refcount); }
2194#ifdef __glibcxx_smart_ptr_owner_equality
2195 size_t owner_hash() const noexcept {
return _M_refcount._M_owner_hash(); }
2197 template<
typename _Tp1>
2199 owner_equal(
const __shared_ptr<_Tp1, _Lp> & __rhs)
const noexcept
2200 {
return _M_refcount == __rhs._M_refcount; }
2202 template<
typename _Tp1>
2204 owner_equal(
const __weak_ptr<_Tp1, _Lp> & __rhs)
const noexcept
2205 {
return _M_refcount == __rhs._M_refcount; }
2210 { __weak_ptr().swap(*
this); }
2213 swap(__weak_ptr& __s)
noexcept
2215 std::swap(_M_ptr, __s._M_ptr);
2216 _M_refcount._M_swap(__s._M_refcount);
2222 _M_assign(_Tp* __ptr,
const __shared_count<_Lp>& __refcount)
noexcept
2224 if (use_count() == 0)
2227 _M_refcount = __refcount;
2231 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
2232 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
2233 friend class __enable_shared_from_this<_Tp, _Lp>;
2235#ifdef __glibcxx_atomic_shared_ptr
2236 friend _Sp_atomic<weak_ptr<_Tp>>;
2239 element_type* _M_ptr;
2240 __weak_count<_Lp> _M_refcount;
2244 template<
typename _Tp, _Lock_policy _Lp>
2246 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b)
noexcept
2249#pragma GCC diagnostic push
2250#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
2251 template<
typename _Tp,
typename _Tp1>
2255 operator()(
const _Tp& __lhs,
const _Tp& __rhs)
const noexcept
2256 {
return __lhs.owner_before(__rhs); }
2259 operator()(
const _Tp& __lhs,
const _Tp1& __rhs)
const noexcept
2260 {
return __lhs.owner_before(__rhs); }
2263 operator()(
const _Tp1& __lhs,
const _Tp& __rhs)
const noexcept
2264 {
return __lhs.owner_before(__rhs); }
2266#pragma GCC diagnostic pop
2269 struct _Sp_owner_less<void, void>
2271 template<
typename _Tp,
typename _Up>
2273 operator()(
const _Tp& __lhs,
const _Up& __rhs)
const noexcept
2274 ->
decltype(__lhs.owner_before(__rhs))
2275 {
return __lhs.owner_before(__rhs); }
2277 using is_transparent = void;
2280 template<
typename _Tp, _Lock_policy _Lp>
2282 :
public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
2285 template<
typename _Tp, _Lock_policy _Lp>
2287 :
public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
2291 template<
typename _Tp, _Lock_policy _Lp>
2292 class __enable_shared_from_this
2295 constexpr __enable_shared_from_this() noexcept { }
2297 __enable_shared_from_this(
const __enable_shared_from_this&)
noexcept { }
2299 __enable_shared_from_this&
2300 operator=(
const __enable_shared_from_this&)
noexcept
2303 ~__enable_shared_from_this() { }
2306 __shared_ptr<_Tp, _Lp>
2308 {
return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
2310 __shared_ptr<const _Tp, _Lp>
2311 shared_from_this()
const
2312 {
return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
2314#if __cplusplus > 201402L || !defined(__STRICT_ANSI__)
2315 __weak_ptr<_Tp, _Lp>
2316 weak_from_this() noexcept
2317 {
return this->_M_weak_this; }
2319 __weak_ptr<const _Tp, _Lp>
2320 weak_from_this() const noexcept
2321 {
return this->_M_weak_this; }
2325 template<
typename _Tp1>
2327 _M_weak_assign(_Tp1* __p,
const __shared_count<_Lp>& __n)
const noexcept
2328 { _M_weak_this._M_assign(__p, __n); }
2330 friend const __enable_shared_from_this*
2331 __enable_shared_from_this_base(
const __shared_count<_Lp>&,
2332 const __enable_shared_from_this* __p)
2335 template<
typename, _Lock_policy>
2336 friend class __shared_ptr;
2338 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
2341 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy,
2342 typename _Alloc,
typename... _Args>
2343 inline __shared_ptr<_Tp, _Lp>
2344 __allocate_shared(
const _Alloc& __a, _Args&&... __args)
2348 return __shared_ptr<_Tp, _Lp>(_Sp_alloc_shared_tag<_Alloc>{__a},
2352 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy,
2354 inline __shared_ptr<_Tp, _Lp>
2355 __make_shared(_Args&&... __args)
2357 typedef typename std::remove_const<_Tp>::type _Tp_nc;
2358 return std::__allocate_shared<_Tp, _Lp>(std::allocator<_Tp_nc>(),
2363 template<
typename _Tp, _Lock_policy _Lp>
2364 struct hash<__shared_ptr<_Tp, _Lp>>
2365 :
public __hash_base<size_t, __shared_ptr<_Tp, _Lp>>
2368 operator()(
const __shared_ptr<_Tp, _Lp>& __s)
const noexcept
2375_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.