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 { __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
256 __gnu_cxx::__scoped_lock sentry(*
this);
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#pragma GCC diagnostic push
319#pragma GCC diagnostic ignored "-Wc++17-extensions"
320 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count);
322 constexpr bool __lock_free
323 = __atomic_always_lock_free(
sizeof(
long long), 0)
324 && __atomic_always_lock_free(
sizeof(_Atomic_word), 0);
325 constexpr bool __double_word
326 =
sizeof(
long long) == 2 *
sizeof(_Atomic_word);
329 constexpr bool __aligned = __alignof(
long long) <=
alignof(
void*);
330 if constexpr (__lock_free && __double_word && __aligned)
332 constexpr int __wordbits = __CHAR_BIT__ *
sizeof(_Atomic_word);
333 constexpr int __shiftbits = __double_word ? __wordbits : 0;
334 constexpr long long __unique_ref = 1LL + (1LL << __shiftbits);
335 auto __both_counts =
reinterpret_cast<long long*
>(&_M_use_count);
337 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
338 if (__atomic_load_n(__both_counts, __ATOMIC_ACQUIRE) == __unique_ref)
344 _M_weak_count = _M_use_count = 0;
345 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count);
346 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
351 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
354 _M_release_last_use_cold();
360 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
362 _M_release_last_use();
364#pragma GCC diagnostic pop
369 _Sp_counted_base<_S_single>::_M_weak_add_ref() noexcept
374 _Sp_counted_base<_S_single>::_M_weak_release() noexcept
376 if (--_M_weak_count == 0)
382 _Sp_counted_base<_S_single>::_M_get_use_count() const noexcept
383 {
return _M_use_count; }
387 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
390 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
393 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
394 class __enable_shared_from_this;
396 template<
typename _Tp>
399 template<
typename _Tp>
402 template<
typename _Tp>
405 template<
typename _Tp>
406 class enable_shared_from_this;
408 template<_Lock_policy _Lp = __default_lock_policy>
411 template<_Lock_policy _Lp = __default_lock_policy>
412 class __shared_count;
414#ifdef __glibcxx_atomic_shared_ptr
420 template<
typename _Ptr, _Lock_policy _Lp>
421 class _Sp_counted_ptr final :
public _Sp_counted_base<_Lp>
425 _Sp_counted_ptr(_Ptr __p) noexcept
429 _M_dispose() noexcept
433 _M_destroy() noexcept
437 _M_get_deleter(
const std::type_info&)
noexcept
440 _Sp_counted_ptr(
const _Sp_counted_ptr&) =
delete;
441 _Sp_counted_ptr& operator=(
const _Sp_counted_ptr&) =
delete;
449 _Sp_counted_ptr<nullptr_t, _S_single>::_M_dispose() noexcept { }
453 _Sp_counted_ptr<nullptr_t, _S_mutex>::_M_dispose() noexcept { }
457 _Sp_counted_ptr<nullptr_t, _S_atomic>::_M_dispose() noexcept { }
464 template<
int _Nm,
typename _Tp,
465 bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
466 struct _Sp_ebo_helper;
469 template<
int _Nm,
typename _Tp>
470 struct _Sp_ebo_helper<_Nm, _Tp, true> :
private _Tp
472 explicit _Sp_ebo_helper(
const _Tp& __tp) : _Tp(__tp) { }
473 explicit _Sp_ebo_helper(_Tp&& __tp) : _Tp(
std::move(__tp)) { }
476 _S_get(_Sp_ebo_helper& __eboh) {
return static_cast<_Tp&
>(__eboh); }
480 template<
int _Nm,
typename _Tp>
481 struct _Sp_ebo_helper<_Nm, _Tp, false>
483 explicit _Sp_ebo_helper(
const _Tp& __tp) : _M_tp(__tp) { }
484 explicit _Sp_ebo_helper(_Tp&& __tp) : _M_tp(
std::move(__tp)) { }
487 _S_get(_Sp_ebo_helper& __eboh)
488 {
return __eboh._M_tp; }
495 template<
typename _Ptr,
typename _Deleter,
typename _Alloc, _Lock_policy _Lp>
496 class _Sp_counted_deleter final :
public _Sp_counted_base<_Lp>
498 class _Impl : _Sp_ebo_helper<0, _Deleter>, _Sp_ebo_helper<1, _Alloc>
500 typedef _Sp_ebo_helper<0, _Deleter> _Del_base;
501 typedef _Sp_ebo_helper<1, _Alloc> _Alloc_base;
504 _Impl(_Ptr __p, _Deleter __d,
const _Alloc& __a) noexcept
505 : _Del_base(
std::move(__d)), _Alloc_base(__a), _M_ptr(__p)
508 _Deleter& _M_del() noexcept {
return _Del_base::_S_get(*
this); }
509 _Alloc& _M_alloc() noexcept {
return _Alloc_base::_S_get(*
this); }
515 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_deleter>;
518 _Sp_counted_deleter(_Ptr __p, _Deleter __d) noexcept
519 : _M_impl(__p,
std::move(__d), _Alloc()) { }
522 _Sp_counted_deleter(_Ptr __p, _Deleter __d,
const _Alloc& __a) noexcept
525 ~_Sp_counted_deleter() noexcept { }
528 _M_dispose() noexcept
529 { _M_impl._M_del()(_M_impl._M_ptr); }
532 _M_destroy() noexcept
534 __allocator_type __a(_M_impl._M_alloc());
535 __allocated_ptr<__allocator_type> __guard_ptr{ __a,
this };
536 this->~_Sp_counted_deleter();
540 _M_get_deleter(
const type_info& __ti [[__gnu__::__unused__]])
noexcept
545 return __ti ==
typeid(_Deleter)
554#ifdef __glibcxx_out_ptr
555 template<
typename,
typename,
typename...>
friend class out_ptr_t;
562 struct _Sp_make_shared_tag
565 template<
typename _Tp,
typename _Alloc, _Lock_policy _Lp>
566 friend class _Sp_counted_ptr_inplace;
568 static const type_info&
569 _S_ti() noexcept _GLIBCXX_VISIBILITY(default)
571 alignas(type_info)
static constexpr char __tag[
sizeof(type_info)] = { };
572 return reinterpret_cast<const type_info&
>(__tag);
575 static bool _S_eq(
const type_info&)
noexcept;
578 template<
typename _Alloc>
579 struct _Sp_alloc_shared_tag
584 template<
typename _Tp,
typename _Alloc, _Lock_policy _Lp>
585 class _Sp_counted_ptr_inplace final :
public _Sp_counted_base<_Lp>
587 class _Impl : _Sp_ebo_helper<0, _Alloc>
589 typedef _Sp_ebo_helper<0, _Alloc> _A_base;
592 explicit _Impl(_Alloc __a) noexcept : _A_base(__a) { }
594 _Alloc& _M_alloc() noexcept {
return _A_base::_S_get(*
this); }
596 __gnu_cxx::__aligned_buffer<__remove_cv_t<_Tp>> _M_storage;
600 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>;
603 template<
typename... _Args>
604 _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
613 ~_Sp_counted_ptr_inplace() noexcept { }
616 _M_dispose() noexcept
623 _M_destroy() noexcept
625 __allocator_type __a(_M_impl._M_alloc());
626 __allocated_ptr<__allocator_type> __guard_ptr{ __a,
this };
627 this->~_Sp_counted_ptr_inplace();
631 friend class __shared_count<_Lp>;
636 _M_get_deleter(
const std::type_info& __ti)
noexcept override
642 if (&__ti == &_Sp_make_shared_tag::_S_ti()
645 __ti ==
typeid(_Sp_make_shared_tag)
647 _Sp_make_shared_tag::_S_eq(__ti)
655 _M_ptr() noexcept {
return _M_impl._M_storage._M_ptr(); }
660#ifdef __glibcxx_smart_ptr_for_overwrite
661 struct _Sp_overwrite_tag { };
667 template<
typename _Tp,
typename _Alloc, _Lock_policy _Lp>
668 requires is_same_v<typename _Alloc::value_type, _Sp_overwrite_tag>
669 class _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> final
671 template<typename _Tp, template<typename> class _Alloc, _Lock_policy _Lp>
672 class _Sp_counted_ptr_inplace<_Tp, _Alloc<_Sp_overwrite_tag>, _Lp> final
674 :
public _Sp_counted_base<_Lp>
676 [[no_unique_address]] _Alloc _M_alloc;
679 remove_cv_t<_Tp> _M_obj;
683 friend class __shared_count<_Lp>;
688 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>;
690 _Sp_counted_ptr_inplace(
const _Alloc& __a)
693 ::new((
void*)_M_ptr()) _Tp;
696 ~_Sp_counted_ptr_inplace() noexcept { }
699 _M_dispose() noexcept
706 _M_destroy() noexcept
708 using pointer =
typename allocator_traits<__allocator_type>::pointer;
709 __allocator_type __a(_M_alloc);
710 auto __p = pointer_traits<pointer>::pointer_to(*
this);
711 __allocated_ptr<__allocator_type> __guard_ptr{ __a, __p };
712 this->~_Sp_counted_ptr_inplace();
716 _M_get_deleter(
const std::type_info&)
noexcept override
721#if __glibcxx_shared_ptr_arrays >= 201707L
722 struct _Sp_overwrite_tag;
725 template<
typename _Alloc>
726 struct _Sp_counted_array_base
728 [[no_unique_address]] _Alloc _M_alloc{};
730 bool _M_overwrite =
false;
732 typename allocator_traits<_Alloc>::pointer
733 _M_alloc_array(
size_t __tail)
735 return allocator_traits<_Alloc>::allocate(_M_alloc, _M_n + __tail);
739 _M_dealloc_array(
typename allocator_traits<_Alloc>::pointer __p,
742 allocator_traits<_Alloc>::deallocate(_M_alloc, __p, _M_n + __tail);
746 template<
typename _Init>
748 _M_init(
typename allocator_traits<_Alloc>::value_type* __p,
751 using _Tp = remove_pointer_t<_Init>;
752 using _Up =
typename allocator_traits<_Alloc>::value_type;
754 if constexpr (is_same_v<_Init, _Sp_overwrite_tag>)
759 else if (__init ==
nullptr)
760 std::__uninitialized_default_n_a(__p, _M_n, _M_alloc);
761 else if constexpr (!is_array_v<_Tp>)
762 std::__uninitialized_fill_n_a(__p, _M_n, *__init, _M_alloc);
765#pragma GCC diagnostic push
766#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
769 using value_type = _Up;
770 using difference_type = ptrdiff_t;
771 using pointer =
const _Up*;
772 using reference =
const _Up&;
773 using iterator_category = forward_iterator_tag;
779 _Iter& operator++() { ++_M_pos;
return *
this; }
780 _Iter operator++(
int) {
auto __i(*
this); ++_M_pos;
return __i; }
782 reference
operator*()
const {
return _M_p[_M_pos % _M_len]; }
783 pointer operator->()
const {
return _M_p + (_M_pos % _M_len); }
785 bool operator==(
const _Iter& __i)
const
786 {
return _M_pos == __i._M_pos; }
788#pragma GCC diagnostic pop
790 _Iter __first{_S_first_elem(__init),
sizeof(_Tp) /
sizeof(_Up)};
791 _Iter __last = __first;
792 __last._M_pos = _M_n;
793 std::__uninitialized_copy_a(__first, __last, __p, _M_alloc);
800 _M_dispose_array(
typename allocator_traits<_Alloc>::value_type* __p)
803 std::destroy_n(__p, _M_n);
808 allocator_traits<_Alloc>::destroy(_M_alloc, __p + __n);
813 template<
typename _Tp>
815 _S_first_elem(_Tp* __p) {
return __p; }
817 template<
typename _Tp,
size_t _Nm>
819 _S_first_elem(_Tp (*__p)[_Nm]) {
return _S_first_elem(*__p); }
824 template<
typename _Alloc, _Lock_policy _Lp>
825 class _Sp_counted_array final
826 :
public _Sp_counted_base<_Lp>, _Sp_counted_array_base<_Alloc>
828 using pointer =
typename allocator_traits<_Alloc>::pointer;
830 pointer _M_alloc_ptr;
834 friend class __shared_count<_Lp>;
837 _Sp_counted_array(
const _Sp_counted_array_base<_Alloc>& __a,
838 pointer __p) noexcept
839 : _Sp_counted_array_base<_Alloc>(__a), _M_alloc_ptr(__p)
842 ~_Sp_counted_array() =
default;
845 _M_dispose() noexcept
848 this->_M_dispose_array(_M_ptr());
853 _M_destroy() noexcept
855 _Sp_counted_array_base<_Alloc> __a = *
this;
856 pointer __p = _M_alloc_ptr;
857 this->~_Sp_counted_array();
858 __a._M_dealloc_array(__p, _S_tail());
863 static constexpr size_t
867 using _Tp =
typename allocator_traits<_Alloc>::value_type;
870 size_t __bytes =
sizeof(_Sp_counted_array);
873 if constexpr (
alignof(_Tp) <
alignof(_Sp_counted_array))
874 __bytes +=
alignof(_Sp_counted_array) -
alignof(_Tp);
876 return (__bytes +
sizeof(_Tp) - 1) /
sizeof(_Tp);
880 _M_get_deleter(
const std::type_info&)
noexcept override
886 struct __sp_array_delete
888 template<
typename _Yp>
889 void operator()(_Yp* __p)
const {
delete[] __p; }
892 template<_Lock_policy _Lp>
896 template<
typename _Tp>
897 struct __not_alloc_shared_tag {
using type = void; };
899 template<
typename _Tp>
900 struct __not_alloc_shared_tag<_Sp_alloc_shared_tag<_Tp>> { };
902#if __glibcxx_shared_ptr_arrays >= 201707L
903 template<
typename _Alloc>
904 struct __not_alloc_shared_tag<_Sp_counted_array_base<_Alloc>> { };
908 constexpr __shared_count() noexcept : _M_pi(0)
911 template<
typename _Ptr>
913 __shared_count(_Ptr __p) : _M_pi(0)
917 _M_pi =
new _Sp_counted_ptr<_Ptr, _Lp>(__p);
922 __throw_exception_again;
926 template<
typename _Ptr>
927 __shared_count(_Ptr __p, false_type)
928 : __shared_count(__p)
931 template<
typename _Ptr>
932 __shared_count(_Ptr __p, true_type)
933 : __shared_count(__p, __sp_array_delete{}, allocator<void>())
936 template<
typename _Ptr,
typename _Deleter,
937 typename =
typename __not_alloc_shared_tag<_Deleter>::type>
938 __shared_count(_Ptr __p, _Deleter __d)
939 : __shared_count(__p, std::
move(__d), allocator<void>())
942 template<
typename _Ptr,
typename _Deleter,
typename _Alloc,
943 typename =
typename __not_alloc_shared_tag<_Deleter>::type>
944 __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
946 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
949 typename _Sp_cd_type::__allocator_type __a2(__a);
950 auto __guard = std::__allocate_guarded(__a2);
951 _Sp_cd_type* __mem = __guard.get();
959 __throw_exception_again;
963 template<
typename _Tp,
typename _Alloc,
typename... _Args>
964 __shared_count(_Tp*& __p, _Sp_alloc_shared_tag<_Alloc> __a,
967 using _Tp2 = __remove_cv_t<_Tp>;
968 using _Sp_cp_type = _Sp_counted_ptr_inplace<_Tp2, _Alloc, _Lp>;
969 typename _Sp_cp_type::__allocator_type __a2(__a._M_a);
970 auto __guard = std::__allocate_guarded(__a2);
971 _Sp_cp_type* __mem = __guard.get();
972 auto __pi = ::new (__mem)
976 __p = __pi->_M_ptr();
979#if __glibcxx_shared_ptr_arrays >= 201707L
980 template<
typename _Tp,
typename _Alloc,
typename _Init>
981 __shared_count(_Tp*& __p,
const _Sp_counted_array_base<_Alloc>& __a,
984 using _Up = remove_all_extents_t<_Tp>;
985 static_assert(is_same_v<_Up, typename _Alloc::value_type>);
987 using _Sp_ca_type = _Sp_counted_array<_Alloc, _Lp>;
988 const size_t __tail = _Sp_ca_type::_S_tail();
990 struct _Guarded_ptr : _Sp_counted_array_base<_Alloc>
992 typename allocator_traits<_Alloc>::pointer _M_ptr;
994 _Guarded_ptr(_Sp_counted_array_base<_Alloc> __a)
995 : _Sp_counted_array_base<_Alloc>(__a),
996 _M_ptr(this->_M_alloc_array(_Sp_ca_type::_S_tail()))
1002 this->_M_dealloc_array(_M_ptr, _Sp_ca_type::_S_tail());
1006 _Guarded_ptr __guard{__a};
1008 __guard._M_init(__raw, __init);
1010 void* __c = __raw + __a._M_n;
1011 if constexpr (
alignof(_Up) <
alignof(_Sp_ca_type))
1013 size_t __space =
sizeof(_Up) * __tail;
1014 __c =
std::align(
alignof(_Sp_ca_type),
sizeof(_Sp_ca_type),
1017 auto __pi = ::new(__c) _Sp_ca_type(__guard, __guard._M_ptr);
1018 __guard._M_ptr =
nullptr;
1020 __p =
reinterpret_cast<_Tp*
>(__raw);
1024#if _GLIBCXX_USE_DEPRECATED
1025#pragma GCC diagnostic push
1026#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1028 template<
typename _Tp>
1030 __shared_count(std::auto_ptr<_Tp>&& __r);
1031#pragma GCC diagnostic pop
1035 template<
typename _Tp,
typename _Del>
1037 __shared_count(std::unique_ptr<_Tp, _Del>&& __r) : _M_pi(0)
1041 if (__r.get() ==
nullptr)
1044 using _Ptr =
typename unique_ptr<_Tp, _Del>::pointer;
1045 using _Del2 = __conditional_t<is_reference<_Del>::value,
1046 reference_wrapper<
typename remove_reference<_Del>::type>,
1049 = _Sp_counted_deleter<_Ptr, _Del2, allocator<void>, _Lp>;
1050 using _Alloc = allocator<_Sp_cd_type>;
1051 using _Alloc_traits = allocator_traits<_Alloc>;
1053 _Sp_cd_type* __mem = _Alloc_traits::allocate(__a, 1);
1057 _Alloc_traits::construct(__a, __mem, __r.release(),
1063 explicit __shared_count(
const __weak_count<_Lp>& __r);
1067 __shared_count(
const __weak_count<_Lp>& __r, std::nothrow_t)
noexcept;
1069 ~__shared_count() noexcept
1071 if (_M_pi !=
nullptr)
1072 _M_pi->_M_release();
1075 __shared_count(
const __shared_count& __r) noexcept
1078 if (_M_pi !=
nullptr)
1079 _M_pi->_M_add_ref_copy();
1083 operator=(
const __shared_count& __r)
noexcept
1085 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1088 if (__tmp !=
nullptr)
1089 __tmp->_M_add_ref_copy();
1090 if (_M_pi !=
nullptr)
1091 _M_pi->_M_release();
1098 _M_swap(__shared_count& __r)
noexcept
1100 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1106 _M_get_use_count() const noexcept
1107 {
return _M_pi ? _M_pi->_M_get_use_count() : 0; }
1110 _M_unique() const noexcept
1111 {
return this->_M_get_use_count() == 1; }
1114 _M_get_deleter(
const std::type_info& __ti)
const noexcept
1115 {
return _M_pi ? _M_pi->_M_get_deleter(__ti) :
nullptr; }
1118 _M_less(
const __shared_count& __rhs)
const noexcept
1119 {
return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
1122 _M_less(
const __weak_count<_Lp>& __rhs)
const noexcept
1123 {
return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
1125#ifdef __glibcxx_smart_ptr_owner_equality
1127 _M_owner_hash() const noexcept
1128 {
return std::hash<_Sp_counted_base<_Lp>*>()(this->_M_pi); }
1133 operator==(
const __shared_count& __a,
const __shared_count& __b)
noexcept
1134 {
return __a._M_pi == __b._M_pi; }
1137 friend class __weak_count<_Lp>;
1138#ifdef __glibcxx_atomic_shared_ptr
1139 template<
typename>
friend class _Sp_atomic;
1141#ifdef __glibcxx_out_ptr
1142 template<
typename,
typename,
typename...>
friend class out_ptr_t;
1145 _Sp_counted_base<_Lp>* _M_pi;
1149 template<_Lock_policy _Lp>
1153 constexpr __weak_count() noexcept : _M_pi(
nullptr)
1156 __weak_count(
const __shared_count<_Lp>& __r) noexcept
1159 if (_M_pi !=
nullptr)
1160 _M_pi->_M_weak_add_ref();
1163 __weak_count(
const __weak_count& __r) noexcept
1166 if (_M_pi !=
nullptr)
1167 _M_pi->_M_weak_add_ref();
1170 __weak_count(__weak_count&& __r) noexcept
1172 { __r._M_pi =
nullptr; }
1174 ~__weak_count() noexcept
1176 if (_M_pi !=
nullptr)
1177 _M_pi->_M_weak_release();
1181 operator=(
const __shared_count<_Lp>& __r)
noexcept
1183 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1184 if (__tmp !=
nullptr)
1185 __tmp->_M_weak_add_ref();
1186 if (_M_pi !=
nullptr)
1187 _M_pi->_M_weak_release();
1193 operator=(
const __weak_count& __r)
noexcept
1195 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1196 if (__tmp !=
nullptr)
1197 __tmp->_M_weak_add_ref();
1198 if (_M_pi !=
nullptr)
1199 _M_pi->_M_weak_release();
1205 operator=(__weak_count&& __r)
noexcept
1207 if (_M_pi !=
nullptr)
1208 _M_pi->_M_weak_release();
1210 __r._M_pi =
nullptr;
1215 _M_swap(__weak_count& __r)
noexcept
1217 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
1223 _M_get_use_count() const noexcept
1224 {
return _M_pi !=
nullptr ? _M_pi->_M_get_use_count() : 0; }
1227 _M_less(
const __weak_count& __rhs)
const noexcept
1228 {
return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
1231 _M_less(
const __shared_count<_Lp>& __rhs)
const noexcept
1232 {
return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
1234#ifdef __glibcxx_smart_ptr_owner_equality
1236 _M_owner_hash() const noexcept
1237 {
return std::hash<_Sp_counted_base<_Lp>*>()(this->_M_pi); }
1242 operator==(
const __weak_count& __a,
const __weak_count& __b)
noexcept
1243 {
return __a._M_pi == __b._M_pi; }
1246 friend class __shared_count<_Lp>;
1247#ifdef __glibcxx_atomic_shared_ptr
1248 template<
typename>
friend class _Sp_atomic;
1251 _Sp_counted_base<_Lp>* _M_pi;
1255 template<_Lock_policy _Lp>
1257 __shared_count<_Lp>::__shared_count(
const __weak_count<_Lp>& __r)
1260 if (_M_pi ==
nullptr || !_M_pi->_M_add_ref_lock_nothrow())
1261 __throw_bad_weak_ptr();
1265 template<_Lock_policy _Lp>
1267 __shared_count<_Lp>::
1268 __shared_count(
const __weak_count<_Lp>& __r, std::nothrow_t) noexcept
1271 if (_M_pi && !_M_pi->_M_add_ref_lock_nothrow())
1279 template<
typename _Yp_ptr,
typename _Tp_ptr>
1280 struct __sp_compatible_with
1284 template<
typename _Yp,
typename _Tp>
1285 struct __sp_compatible_with<_Yp*, _Tp*>
1286 : is_convertible<_Yp*, _Tp*>::type
1289 template<
typename _Up,
size_t _Nm>
1290 struct __sp_compatible_with<_Up(*)[_Nm], _Up(*)[]>
1294 template<
typename _Up,
size_t _Nm>
1295 struct __sp_compatible_with<_Up(*)[_Nm],
const _Up(*)[]>
1299 template<
typename _Up,
size_t _Nm>
1300 struct __sp_compatible_with<_Up(*)[_Nm],
volatile _Up(*)[]>
1304 template<
typename _Up,
size_t _Nm>
1305 struct __sp_compatible_with<_Up(*)[_Nm],
const volatile _Up(*)[]>
1310 template<
typename _Up,
size_t _Nm,
typename _Yp,
typename =
void>
1311 struct __sp_is_constructible_arrN
1315 template<
typename _Up,
size_t _Nm,
typename _Yp>
1316 struct __sp_is_constructible_arrN<_Up, _Nm, _Yp, __void_t<_Yp[_Nm]>>
1317 : is_convertible<_Yp(*)[_Nm], _Up(*)[_Nm]>::type
1321 template<
typename _Up,
typename _Yp,
typename =
void>
1322 struct __sp_is_constructible_arr
1326 template<
typename _Up,
typename _Yp>
1327 struct __sp_is_constructible_arr<_Up, _Yp, __void_t<_Yp[]>>
1328 : is_convertible<_Yp(*)[], _Up(*)[]>::type
1332 template<
typename _Tp,
typename _Yp>
1333 struct __sp_is_constructible;
1336 template<
typename _Up,
size_t _Nm,
typename _Yp>
1337 struct __sp_is_constructible<_Up[_Nm], _Yp>
1338 : __sp_is_constructible_arrN<_Up, _Nm, _Yp>::type
1342 template<
typename _Up,
typename _Yp>
1343 struct __sp_is_constructible<_Up[], _Yp>
1344 : __sp_is_constructible_arr<_Up, _Yp>::type
1348 template<
typename _Tp,
typename _Yp>
1349 struct __sp_is_constructible
1350 : is_convertible<_Yp*, _Tp*>::type
1354 template<
typename _Tp>
1355 [[__gnu__::__always_inline__]]
1357 __shared_ptr_deref(_Tp* __p)
1359 __glibcxx_assert(__p !=
nullptr);
1364 template<
typename _Tp, _Lock_policy _Lp,
1366 class __shared_ptr_access
1369 using element_type = _Tp;
1372 operator*() const noexcept
1373 {
return *std::__shared_ptr_deref(_M_get()); }
1376 operator->() const noexcept
1378 _GLIBCXX_DEBUG_PEDASSERT(_M_get() !=
nullptr);
1384 _M_get() const noexcept
1385 {
return static_cast<const __shared_ptr<_Tp, _Lp>*
>(
this)->get(); }
1389 template<
typename _Tp, _Lock_policy _Lp>
1390 class __shared_ptr_access<_Tp, _Lp, false, true>
1393 using element_type = _Tp;
1396 operator->() const noexcept
1398 auto __ptr =
static_cast<const __shared_ptr<_Tp, _Lp>*
>(
this)->get();
1399 _GLIBCXX_DEBUG_PEDASSERT(__ptr !=
nullptr);
1405 template<
typename _Tp, _Lock_policy _Lp>
1406 class __shared_ptr_access<_Tp, _Lp, true, false>
1409 using element_type =
typename remove_extent<_Tp>::type;
1411#if __cplusplus <= 201402L
1412 [[__deprecated__(
"shared_ptr<T[]>::operator* is absent from C++17")]]
1414 operator*() const noexcept
1415 {
return *std::__shared_ptr_deref(_M_get()); }
1417 [[__deprecated__(
"shared_ptr<T[]>::operator-> is absent from C++17")]]
1419 operator->() const noexcept
1421 _GLIBCXX_DEBUG_PEDASSERT(_M_get() !=
nullptr);
1426#pragma GCC diagnostic push
1427#pragma GCC diagnostic ignored "-Wc++17-extensions"
1429 operator[](ptrdiff_t __i)
const noexcept
1431 if constexpr (extent<_Tp>::value)
1432 __glibcxx_assert(__i < extent<_Tp>::value);
1433 return std::__shared_ptr_deref(_M_get())[__i];
1435#pragma GCC diagnostic pop
1439 _M_get() const noexcept
1440 {
return static_cast<const __shared_ptr<_Tp, _Lp>*
>(
this)->get(); }
1443 template<
typename _Tp, _Lock_policy _Lp>
1445 :
public __shared_ptr_access<_Tp, _Lp>
1448 using element_type =
typename remove_extent<_Tp>::type;
1452 template<
typename _Yp>
1454 =
typename enable_if<__sp_is_constructible<_Tp, _Yp>::value>::type;
1457 template<
typename _Yp,
typename _Res =
void>
1458 using _Compatible =
typename
1459 enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type;
1462 template<
typename _Yp>
1463 using _Assignable = _Compatible<_Yp, __shared_ptr&>;
1466 template<
typename _Yp,
typename _Del,
typename _Res = void,
1467 typename _Ptr =
typename unique_ptr<_Yp, _Del>::pointer>
1468 using _UniqCompatible = __enable_if_t<__and_<
1469 __sp_compatible_with<_Yp*, _Tp*>,
1470 is_convertible<_Ptr, element_type*>,
1471 is_move_constructible<_Del>
1475 template<
typename _Yp,
typename _Del>
1476 using _UniqAssignable = _UniqCompatible<_Yp, _Del, __shared_ptr&>;
1480#if __cplusplus > 201402L
1481 using weak_type = __weak_ptr<_Tp, _Lp>;
1484 constexpr __shared_ptr() noexcept
1485 : _M_ptr(0), _M_refcount()
1488 template<
typename _Yp,
typename = _SafeConv<_Yp>>
1490 __shared_ptr(_Yp* __p)
1491 : _M_ptr(__p), _M_refcount(__p, typename is_array<_Tp>::type())
1493 static_assert( !is_void<_Yp>::value,
"incomplete type" );
1494 static_assert(
sizeof(_Yp) > 0,
"incomplete type" );
1495 _M_enable_shared_from_this_with(__p);
1498 template<
typename _Yp,
typename _Deleter,
typename = _SafeConv<_Yp>>
1499 __shared_ptr(_Yp* __p, _Deleter __d)
1500 : _M_ptr(__p), _M_refcount(__p, std::
move(__d))
1502 static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
1503 "deleter expression d(p) is well-formed");
1504 _M_enable_shared_from_this_with(__p);
1507 template<
typename _Yp,
typename _Deleter,
typename _Alloc,
1508 typename = _SafeConv<_Yp>>
1509 __shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a)
1510 : _M_ptr(__p), _M_refcount(__p, std::
move(__d), std::
move(__a))
1512 static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
1513 "deleter expression d(p) is well-formed");
1514 _M_enable_shared_from_this_with(__p);
1517 template<
typename _Deleter>
1518 __shared_ptr(nullptr_t __p, _Deleter __d)
1519 : _M_ptr(0), _M_refcount(__p, std::
move(__d))
1522 template<
typename _Deleter,
typename _Alloc>
1523 __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
1524 : _M_ptr(0), _M_refcount(__p, std::
move(__d), std::
move(__a))
1528 template<
typename _Yp>
1529 __shared_ptr(
const __shared_ptr<_Yp, _Lp>& __r,
1530 element_type* __p) noexcept
1531 : _M_ptr(__p), _M_refcount(__r._M_refcount)
1535 template<
typename _Yp>
1536 __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r,
1537 element_type* __p) noexcept
1538 : _M_ptr(__p), _M_refcount()
1540 _M_refcount._M_swap(__r._M_refcount);
1541 __r._M_ptr =
nullptr;
1544 __shared_ptr(
const __shared_ptr&)
noexcept =
default;
1545 __shared_ptr& operator=(
const __shared_ptr&)
noexcept =
default;
1546 ~__shared_ptr() =
default;
1548 template<
typename _Yp,
typename = _Compatible<_Yp>>
1549 __shared_ptr(
const __shared_ptr<_Yp, _Lp>& __r) noexcept
1550 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1553 __shared_ptr(__shared_ptr&& __r) noexcept
1554 : _M_ptr(__r._M_ptr), _M_refcount()
1556 _M_refcount._M_swap(__r._M_refcount);
1557 __r._M_ptr =
nullptr;
1560 template<
typename _Yp,
typename = _Compatible<_Yp>>
1561 __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r) noexcept
1562 : _M_ptr(__r._M_ptr), _M_refcount()
1564 _M_refcount._M_swap(__r._M_refcount);
1565 __r._M_ptr =
nullptr;
1568 template<
typename _Yp,
typename = _Compatible<_Yp>>
1569 explicit __shared_ptr(
const __weak_ptr<_Yp, _Lp>& __r)
1570 : _M_refcount(__r._M_refcount)
1574 _M_ptr = __r._M_ptr;
1578 template<
typename _Yp,
typename _Del,
1579 typename = _UniqCompatible<_Yp, _Del>>
1580 __shared_ptr(unique_ptr<_Yp, _Del>&& __r)
1581 : _M_ptr(__r.get()), _M_refcount()
1583 auto __raw = std::__to_address(__r.get());
1584 _M_refcount = __shared_count<_Lp>(
std::move(__r));
1585 _M_enable_shared_from_this_with(__raw);
1588#if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED
1591 template<
typename _Tp1,
typename _Del,
1592 typename enable_if<__and_<
1593 __not_<is_array<_Tp>>, is_array<_Tp1>,
1594 is_convertible<typename unique_ptr<_Tp1, _Del>::pointer, _Tp*>
1595 >::value,
bool>::type =
true>
1596 __shared_ptr(unique_ptr<_Tp1, _Del>&& __r, __sp_array_delete)
1597 : _M_ptr(__r.get()), _M_refcount()
1599 auto __raw = std::__to_address(__r.get());
1600 _M_refcount = __shared_count<_Lp>(
std::move(__r));
1601 _M_enable_shared_from_this_with(__raw);
1606#if _GLIBCXX_USE_DEPRECATED
1607#pragma GCC diagnostic push
1608#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1610 template<
typename _Yp,
typename = _Compatible<_Yp>>
1611 __shared_ptr(auto_ptr<_Yp>&& __r);
1612#pragma GCC diagnostic pop
1615 constexpr __shared_ptr(nullptr_t) noexcept : __shared_ptr() { }
1617 template<
typename _Yp>
1619 operator=(
const __shared_ptr<_Yp, _Lp>& __r)
noexcept
1621 _M_ptr = __r._M_ptr;
1622 _M_refcount = __r._M_refcount;
1626#if _GLIBCXX_USE_DEPRECATED
1627#pragma GCC diagnostic push
1628#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1629 template<
typename _Yp>
1631 operator=(auto_ptr<_Yp>&& __r)
1633 __shared_ptr(
std::move(__r)).swap(*
this);
1636#pragma GCC diagnostic pop
1640 operator=(__shared_ptr&& __r)
noexcept
1642 __shared_ptr(
std::move(__r)).swap(*
this);
1648 operator=(__shared_ptr<_Yp, _Lp>&& __r)
noexcept
1650 __shared_ptr(
std::move(__r)).swap(*
this);
1654 template<
typename _Yp,
typename _Del>
1655 _UniqAssignable<_Yp, _Del>
1656 operator=(unique_ptr<_Yp, _Del>&& __r)
1658 __shared_ptr(
std::move(__r)).swap(*
this);
1664 { __shared_ptr().swap(*
this); }
1666 template<
typename _Yp>
1671 __glibcxx_assert(__p ==
nullptr || __p != _M_ptr);
1672 __shared_ptr(__p).swap(*
this);
1675 template<
typename _Yp,
typename _Deleter>
1677 reset(_Yp* __p, _Deleter __d)
1678 { __shared_ptr(__p,
std::move(__d)).swap(*
this); }
1680 template<
typename _Yp,
typename _Deleter,
typename _Alloc>
1682 reset(_Yp* __p, _Deleter __d, _Alloc __a)
1687 get() const noexcept
1691 explicit operator bool() const noexcept
1692 {
return _M_ptr !=
nullptr; }
1696 unique() const noexcept
1697 {
return _M_refcount._M_unique(); }
1701 use_count() const noexcept
1702 {
return _M_refcount._M_get_use_count(); }
1706 swap(__shared_ptr<_Tp, _Lp>& __other)
noexcept
1708 std::swap(_M_ptr, __other._M_ptr);
1709 _M_refcount._M_swap(__other._M_refcount);
1719 template<
typename _Tp1>
1721 owner_before(__shared_ptr<_Tp1, _Lp>
const& __rhs)
const noexcept
1722 {
return _M_refcount._M_less(__rhs._M_refcount); }
1724 template<
typename _Tp1>
1726 owner_before(__weak_ptr<_Tp1, _Lp>
const& __rhs)
const noexcept
1727 {
return _M_refcount._M_less(__rhs._M_refcount); }
1730#ifdef __glibcxx_smart_ptr_owner_equality
1731 size_t owner_hash() const noexcept {
return _M_refcount._M_owner_hash(); }
1733 template<
typename _Tp1>
1735 owner_equal(__shared_ptr<_Tp1, _Lp>
const& __rhs)
const noexcept
1736 {
return _M_refcount == __rhs._M_refcount; }
1738 template<
typename _Tp1>
1740 owner_equal(__weak_ptr<_Tp1, _Lp>
const& __rhs)
const noexcept
1741 {
return _M_refcount == __rhs._M_refcount; }
1746 template<
typename _Alloc,
typename... _Args>
1747 __shared_ptr(_Sp_alloc_shared_tag<_Alloc> __tag, _Args&&... __args)
1748 : _M_ptr(), _M_refcount(_M_ptr, __tag, std::
forward<_Args>(__args)...)
1749 { _M_enable_shared_from_this_with(_M_ptr); }
1751 template<
typename _Tp1, _Lock_policy _Lp1,
typename _Alloc,
1753 friend __shared_ptr<_Tp1, _Lp1>
1754 __allocate_shared(
const _Alloc& __a, _Args&&... __args);
1756#if __glibcxx_shared_ptr_arrays >= 201707L
1758 template<
typename _Alloc,
typename _Init = const remove_extent_t<_Tp>*>
1759 __shared_ptr(
const _Sp_counted_array_base<_Alloc>& __a,
1760 _Init __init =
nullptr)
1761 : _M_ptr(), _M_refcount(_M_ptr, __a, __init)
1767 __shared_ptr(
const __weak_ptr<_Tp, _Lp>& __r, std::nothrow_t) noexcept
1768 : _M_refcount(__r._M_refcount, std::nothrow)
1770 _M_ptr = _M_refcount._M_get_use_count() ? __r._M_ptr :
nullptr;
1773 friend class __weak_ptr<_Tp, _Lp>;
1777 template<
typename _Yp>
1778 using __esft_base_t =
decltype(__enable_shared_from_this_base(
1783 template<
typename _Yp,
typename =
void>
1784 struct __has_esft_base
1787 template<
typename _Yp>
1788 struct __has_esft_base<_Yp, __void_t<__esft_base_t<_Yp>>>
1789 : __not_<is_array<_Tp>> { };
1791 template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
1792 typename enable_if<__has_esft_base<_Yp2>::value>::type
1793 _M_enable_shared_from_this_with(_Yp* __p)
noexcept
1795 if (
auto __base = __enable_shared_from_this_base(_M_refcount, __p))
1796 __base->_M_weak_assign(
const_cast<_Yp2*
>(__p), _M_refcount);
1799 template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
1800 typename enable_if<!__has_esft_base<_Yp2>::value>::type
1801 _M_enable_shared_from_this_with(_Yp*)
noexcept
1805 _M_get_deleter(
const std::type_info& __ti)
const noexcept
1806 {
return _M_refcount._M_get_deleter(__ti); }
1808 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
1809 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
1811 template<
typename _Del,
typename _Tp1, _Lock_policy _Lp1>
1812 friend _Del* get_deleter(
const __shared_ptr<_Tp1, _Lp1>&)
noexcept;
1814 template<
typename _Del,
typename _Tp1>
1815 friend _Del* get_deleter(
const shared_ptr<_Tp1>&)
noexcept;
1817#ifdef __glibcxx_atomic_shared_ptr
1818 friend _Sp_atomic<shared_ptr<_Tp>>;
1820#ifdef __glibcxx_out_ptr
1821 template<
typename,
typename,
typename...>
friend class out_ptr_t;
1824 element_type* _M_ptr;
1825 __shared_count<_Lp> _M_refcount;
1830 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1832 operator==(
const __shared_ptr<_Tp1, _Lp>& __a,
1833 const __shared_ptr<_Tp2, _Lp>& __b)
noexcept
1834 {
return __a.get() == __b.get(); }
1836 template<
typename _Tp, _Lock_policy _Lp>
1838 operator==(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1841#ifdef __cpp_lib_three_way_comparison
1842 template<
typename _Tp,
typename _Up, _Lock_policy _Lp>
1843 inline strong_ordering
1844 operator<=>(
const __shared_ptr<_Tp, _Lp>& __a,
1845 const __shared_ptr<_Up, _Lp>& __b)
noexcept
1846 {
return compare_three_way()(__a.get(), __b.get()); }
1848 template<
typename _Tp, _Lock_policy _Lp>
1849 inline strong_ordering
1850 operator<=>(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1852 using pointer =
typename __shared_ptr<_Tp, _Lp>::element_type*;
1853 return compare_three_way()(__a.get(),
static_cast<pointer
>(
nullptr));
1856 template<
typename _Tp, _Lock_policy _Lp>
1858 operator==(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a)
noexcept
1861 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1863 operator!=(
const __shared_ptr<_Tp1, _Lp>& __a,
1864 const __shared_ptr<_Tp2, _Lp>& __b)
noexcept
1865 {
return __a.get() != __b.get(); }
1867 template<
typename _Tp, _Lock_policy _Lp>
1869 operator!=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1870 {
return (
bool)__a; }
1872 template<
typename _Tp, _Lock_policy _Lp>
1874 operator!=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a)
noexcept
1875 {
return (
bool)__a; }
1877 template<
typename _Tp,
typename _Up, _Lock_policy _Lp>
1879 operator<(
const __shared_ptr<_Tp, _Lp>& __a,
1880 const __shared_ptr<_Up, _Lp>& __b)
noexcept
1882 using _Tp_elt =
typename __shared_ptr<_Tp, _Lp>::element_type;
1883 using _Up_elt =
typename __shared_ptr<_Up, _Lp>::element_type;
1885 return less<_Vp>()(__a.get(), __b.get());
1888 template<
typename _Tp, _Lock_policy _Lp>
1890 operator<(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1892 using _Tp_elt =
typename __shared_ptr<_Tp, _Lp>::element_type;
1896 template<
typename _Tp, _Lock_policy _Lp>
1898 operator<(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a)
noexcept
1900 using _Tp_elt =
typename __shared_ptr<_Tp, _Lp>::element_type;
1904 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1906 operator<=(
const __shared_ptr<_Tp1, _Lp>& __a,
1907 const __shared_ptr<_Tp2, _Lp>& __b)
noexcept
1908 {
return !(__b < __a); }
1910 template<
typename _Tp, _Lock_policy _Lp>
1912 operator<=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1913 {
return !(
nullptr < __a); }
1915 template<
typename _Tp, _Lock_policy _Lp>
1917 operator<=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a)
noexcept
1918 {
return !(__a <
nullptr); }
1920 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1922 operator>(
const __shared_ptr<_Tp1, _Lp>& __a,
1923 const __shared_ptr<_Tp2, _Lp>& __b)
noexcept
1924 {
return (__b < __a); }
1926 template<
typename _Tp, _Lock_policy _Lp>
1928 operator>(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1929 {
return nullptr < __a; }
1931 template<
typename _Tp, _Lock_policy _Lp>
1933 operator>(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a)
noexcept
1934 {
return __a <
nullptr; }
1936 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1938 operator>=(
const __shared_ptr<_Tp1, _Lp>& __a,
1939 const __shared_ptr<_Tp2, _Lp>& __b)
noexcept
1940 {
return !(__a < __b); }
1942 template<
typename _Tp, _Lock_policy _Lp>
1944 operator>=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t)
noexcept
1945 {
return !(__a <
nullptr); }
1947 template<
typename _Tp, _Lock_policy _Lp>
1949 operator>=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a)
noexcept
1950 {
return !(
nullptr < __a); }
1954 template<
typename _Tp, _Lock_policy _Lp>
1956 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b)
noexcept
1966 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1967 inline __shared_ptr<_Tp, _Lp>
1970 using _Sp = __shared_ptr<_Tp, _Lp>;
1971 return _Sp(__r,
static_cast<typename _Sp::element_type*
>(__r.get()));
1979 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1980 inline __shared_ptr<_Tp, _Lp>
1983 using _Sp = __shared_ptr<_Tp, _Lp>;
1984 return _Sp(__r,
const_cast<typename _Sp::element_type*
>(__r.get()));
1992 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1993 inline __shared_ptr<_Tp, _Lp>
1996 using _Sp = __shared_ptr<_Tp, _Lp>;
1997 if (
auto* __p =
dynamic_cast<typename _Sp::element_type*
>(__r.get()))
1998 return _Sp(__r, __p);
2002#if __cplusplus > 201402L
2003 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
2004 inline __shared_ptr<_Tp, _Lp>
2005 reinterpret_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r)
noexcept
2007 using _Sp = __shared_ptr<_Tp, _Lp>;
2008 return _Sp(__r,
reinterpret_cast<typename _Sp::element_type*
>(__r.get()));
2012 template<
typename _Tp, _Lock_policy _Lp>
2015 template<
typename _Yp,
typename _Res =
void>
2016 using _Compatible =
typename
2020 template<
typename _Yp>
2021 using _Assignable = _Compatible<_Yp, __weak_ptr&>;
2024 using element_type =
typename remove_extent<_Tp>::type;
2026 constexpr __weak_ptr() noexcept
2027 : _M_ptr(
nullptr), _M_refcount()
2030 __weak_ptr(
const __weak_ptr&)
noexcept =
default;
2032 ~__weak_ptr() =
default;
2048 template<
typename _Yp,
typename = _Compatible<_Yp>>
2049 __weak_ptr(
const __weak_ptr<_Yp, _Lp>& __r) noexcept
2050 : _M_refcount(__r._M_refcount)
2051 { _M_ptr = __r.lock().get(); }
2053 template<
typename _Yp,
typename = _Compatible<_Yp>>
2054 __weak_ptr(
const __shared_ptr<_Yp, _Lp>& __r) noexcept
2055 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
2058 __weak_ptr(__weak_ptr&& __r) noexcept
2059 : _M_ptr(__r._M_ptr), _M_refcount(
std::move(__r._M_refcount))
2060 { __r._M_ptr =
nullptr; }
2062 template<
typename _Yp,
typename = _Compatible<_Yp>>
2063 __weak_ptr(__weak_ptr<_Yp, _Lp>&& __r) noexcept
2064 : _M_ptr(__r.lock().get()), _M_refcount(
std::move(__r._M_refcount))
2065 { __r._M_ptr =
nullptr; }
2068 operator=(
const __weak_ptr& __r)
noexcept =
default;
2070 template<
typename _Yp>
2072 operator=(
const __weak_ptr<_Yp, _Lp>& __r)
noexcept
2074 _M_ptr = __r.lock().get();
2075 _M_refcount = __r._M_refcount;
2079 template<
typename _Yp>
2081 operator=(
const __shared_ptr<_Yp, _Lp>& __r)
noexcept
2083 _M_ptr = __r._M_ptr;
2084 _M_refcount = __r._M_refcount;
2089 operator=(__weak_ptr&& __r)
noexcept
2095 template<
typename _Yp>
2097 operator=(__weak_ptr<_Yp, _Lp>&& __r)
noexcept
2099 _M_ptr = __r.lock().get();
2100 _M_refcount =
std::move(__r._M_refcount);
2101 __r._M_ptr =
nullptr;
2105 __shared_ptr<_Tp, _Lp>
2106 lock() const noexcept
2107 {
return __shared_ptr<_Tp, _Lp>(*
this, std::nothrow); }
2110 use_count() const noexcept
2111 {
return _M_refcount._M_get_use_count(); }
2114 expired() const noexcept
2115 {
return _M_refcount._M_get_use_count() == 0; }
2117 template<
typename _Tp1>
2119 owner_before(
const __shared_ptr<_Tp1, _Lp>& __rhs)
const noexcept
2120 {
return _M_refcount._M_less(__rhs._M_refcount); }
2122 template<
typename _Tp1>
2124 owner_before(
const __weak_ptr<_Tp1, _Lp>& __rhs)
const noexcept
2125 {
return _M_refcount._M_less(__rhs._M_refcount); }
2127#ifdef __glibcxx_smart_ptr_owner_equality
2128 size_t owner_hash() const noexcept {
return _M_refcount._M_owner_hash(); }
2130 template<
typename _Tp1>
2132 owner_equal(
const __shared_ptr<_Tp1, _Lp> & __rhs)
const noexcept
2133 {
return _M_refcount == __rhs._M_refcount; }
2135 template<
typename _Tp1>
2137 owner_equal(
const __weak_ptr<_Tp1, _Lp> & __rhs)
const noexcept
2138 {
return _M_refcount == __rhs._M_refcount; }
2143 { __weak_ptr().swap(*
this); }
2146 swap(__weak_ptr& __s)
noexcept
2148 std::swap(_M_ptr, __s._M_ptr);
2149 _M_refcount._M_swap(__s._M_refcount);
2155 _M_assign(_Tp* __ptr,
const __shared_count<_Lp>& __refcount)
noexcept
2157 if (use_count() == 0)
2160 _M_refcount = __refcount;
2164 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
2165 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
2166 friend class __enable_shared_from_this<_Tp, _Lp>;
2168#ifdef __glibcxx_atomic_shared_ptr
2169 friend _Sp_atomic<weak_ptr<_Tp>>;
2172 element_type* _M_ptr;
2173 __weak_count<_Lp> _M_refcount;
2177 template<
typename _Tp, _Lock_policy _Lp>
2179 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b)
noexcept
2182#pragma GCC diagnostic push
2183#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
2184 template<
typename _Tp,
typename _Tp1>
2188 operator()(
const _Tp& __lhs,
const _Tp& __rhs)
const noexcept
2189 {
return __lhs.owner_before(__rhs); }
2192 operator()(
const _Tp& __lhs,
const _Tp1& __rhs)
const noexcept
2193 {
return __lhs.owner_before(__rhs); }
2196 operator()(
const _Tp1& __lhs,
const _Tp& __rhs)
const noexcept
2197 {
return __lhs.owner_before(__rhs); }
2199#pragma GCC diagnostic pop
2202 struct _Sp_owner_less<void, void>
2204 template<
typename _Tp,
typename _Up>
2206 operator()(
const _Tp& __lhs,
const _Up& __rhs)
const noexcept
2207 ->
decltype(__lhs.owner_before(__rhs))
2208 {
return __lhs.owner_before(__rhs); }
2210 using is_transparent = void;
2213 template<
typename _Tp, _Lock_policy _Lp>
2215 :
public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
2218 template<
typename _Tp, _Lock_policy _Lp>
2220 :
public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
2224 template<
typename _Tp, _Lock_policy _Lp>
2225 class __enable_shared_from_this
2228 constexpr __enable_shared_from_this() noexcept { }
2230 __enable_shared_from_this(
const __enable_shared_from_this&)
noexcept { }
2232 __enable_shared_from_this&
2233 operator=(
const __enable_shared_from_this&)
noexcept
2236 ~__enable_shared_from_this() { }
2239 __shared_ptr<_Tp, _Lp>
2241 {
return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
2243 __shared_ptr<const _Tp, _Lp>
2244 shared_from_this()
const
2245 {
return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
2247#if __cplusplus > 201402L || !defined(__STRICT_ANSI__)
2248 __weak_ptr<_Tp, _Lp>
2249 weak_from_this() noexcept
2250 {
return this->_M_weak_this; }
2252 __weak_ptr<const _Tp, _Lp>
2253 weak_from_this() const noexcept
2254 {
return this->_M_weak_this; }
2258 template<
typename _Tp1>
2260 _M_weak_assign(_Tp1* __p,
const __shared_count<_Lp>& __n)
const noexcept
2261 { _M_weak_this._M_assign(__p, __n); }
2263 friend const __enable_shared_from_this*
2264 __enable_shared_from_this_base(
const __shared_count<_Lp>&,
2265 const __enable_shared_from_this* __p)
2268 template<
typename, _Lock_policy>
2269 friend class __shared_ptr;
2271 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
2274 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy,
2275 typename _Alloc,
typename... _Args>
2276 inline __shared_ptr<_Tp, _Lp>
2277 __allocate_shared(
const _Alloc& __a, _Args&&... __args)
2281 return __shared_ptr<_Tp, _Lp>(_Sp_alloc_shared_tag<_Alloc>{__a},
2285 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy,
2287 inline __shared_ptr<_Tp, _Lp>
2288 __make_shared(_Args&&... __args)
2290 typedef typename std::remove_const<_Tp>::type _Tp_nc;
2291 return std::__allocate_shared<_Tp, _Lp>(std::allocator<_Tp_nc>(),
2296 template<
typename _Tp, _Lock_policy _Lp>
2297 struct hash<__shared_ptr<_Tp, _Lp>>
2298 :
public __hash_base<size_t, __shared_ptr<_Tp, _Lp>>
2301 operator()(
const __shared_ptr<_Tp, _Lp>& __s)
const noexcept
2308_GLIBCXX_END_NAMESPACE_VERSION
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.
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.