49 #ifndef _SHARED_PTR_BASE_H
50 #define _SHARED_PTR_BASE_H 1
58 namespace std _GLIBCXX_VISIBILITY(default)
60 _GLIBCXX_BEGIN_NAMESPACE_VERSION
62 #if _GLIBCXX_USE_DEPRECATED
63 #pragma GCC diagnostic push
64 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
65 template<
typename>
class auto_ptr;
66 #pragma GCC diagnostic pop
76 virtual char const*
what()
const noexcept;
83 __throw_bad_weak_ptr()
86 using __gnu_cxx::_Lock_policy;
87 using __gnu_cxx::__default_lock_policy;
88 using __gnu_cxx::_S_single;
89 using __gnu_cxx::_S_mutex;
90 using __gnu_cxx::_S_atomic;
93 template<_Lock_policy _Lp>
98 enum { _S_need_barriers = 0 };
102 class _Mutex_base<_S_mutex>
103 :
public __gnu_cxx::__mutex
109 enum { _S_need_barriers = 1 };
112 template<_Lock_policy _Lp = __default_lock_policy>
113 class _Sp_counted_base
114 :
public _Mutex_base<_Lp>
117 _Sp_counted_base() noexcept
118 : _M_use_count(1), _M_weak_count(1) { }
121 ~_Sp_counted_base() noexcept
127 _M_dispose() noexcept = 0;
131 _M_destroy() noexcept
139 { __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); }
145 _M_add_ref_lock_nothrow();
148 _M_release() noexcept
151 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count);
152 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
154 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count);
160 if (_Mutex_base<_Lp>::_S_need_barriers)
162 __atomic_thread_fence (__ATOMIC_ACQ_REL);
166 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
167 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count,
170 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
177 _M_weak_add_ref() noexcept
178 { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); }
181 _M_weak_release() noexcept
184 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
185 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1)
187 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
188 if (_Mutex_base<_Lp>::_S_need_barriers)
192 __atomic_thread_fence (__ATOMIC_ACQ_REL);
199 _M_get_use_count() const noexcept
203 return __atomic_load_n(&_M_use_count, __ATOMIC_RELAXED);
207 _Sp_counted_base(_Sp_counted_base
const&) =
delete;
208 _Sp_counted_base& operator=(_Sp_counted_base
const&) =
delete;
210 _Atomic_word _M_use_count;
211 _Atomic_word _M_weak_count;
216 _Sp_counted_base<_S_single>::
219 if (_M_use_count == 0)
220 __throw_bad_weak_ptr();
226 _Sp_counted_base<_S_mutex>::
230 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
233 __throw_bad_weak_ptr();
239 _Sp_counted_base<_S_atomic>::
243 _Atomic_word __count = _M_get_use_count();
247 __throw_bad_weak_ptr();
251 while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1,
252 true, __ATOMIC_ACQ_REL,
258 _Sp_counted_base<_S_single>::
259 _M_add_ref_lock_nothrow()
261 if (_M_use_count == 0)
269 _Sp_counted_base<_S_mutex>::
270 _M_add_ref_lock_nothrow()
273 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
283 _Sp_counted_base<_S_atomic>::
284 _M_add_ref_lock_nothrow()
287 _Atomic_word __count = _M_get_use_count();
295 while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1,
296 true, __ATOMIC_ACQ_REL,
303 _Sp_counted_base<_S_single>::_M_add_ref_copy()
308 _Sp_counted_base<_S_single>::_M_release() noexcept
310 if (--_M_use_count == 0)
313 if (--_M_weak_count == 0)
320 _Sp_counted_base<_S_single>::_M_weak_add_ref() noexcept
325 _Sp_counted_base<_S_single>::_M_weak_release() noexcept
327 if (--_M_weak_count == 0)
333 _Sp_counted_base<_S_single>::_M_get_use_count() const noexcept
334 {
return _M_use_count; }
338 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
341 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
344 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
345 class __enable_shared_from_this;
347 template<
typename _Tp>
350 template<
typename _Tp>
353 template<
typename _Tp>
356 template<
typename _Tp>
357 class enable_shared_from_this;
359 template<_Lock_policy _Lp = __default_lock_policy>
362 template<_Lock_policy _Lp = __default_lock_policy>
363 class __shared_count;
367 template<
typename _Ptr, _Lock_policy _Lp>
368 class _Sp_counted_ptr final :
public _Sp_counted_base<_Lp>
372 _Sp_counted_ptr(_Ptr __p) noexcept
376 _M_dispose() noexcept
380 _M_destroy() noexcept
387 _Sp_counted_ptr(
const _Sp_counted_ptr&) =
delete;
388 _Sp_counted_ptr& operator=(
const _Sp_counted_ptr&) =
delete;
396 _Sp_counted_ptr<nullptr_t, _S_single>::_M_dispose() noexcept { }
400 _Sp_counted_ptr<nullptr_t, _S_mutex>::_M_dispose() noexcept { }
404 _Sp_counted_ptr<nullptr_t, _S_atomic>::_M_dispose() noexcept { }
406 template<
int _Nm,
typename _Tp,
407 bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
408 struct _Sp_ebo_helper;
411 template<
int _Nm,
typename _Tp>
412 struct _Sp_ebo_helper<_Nm, _Tp, true> :
private _Tp
414 explicit _Sp_ebo_helper(
const _Tp& __tp) : _Tp(__tp) { }
415 explicit _Sp_ebo_helper(_Tp&& __tp) : _Tp(
std::move(__tp)) { }
418 _S_get(_Sp_ebo_helper& __eboh) {
return static_cast<_Tp&
>(__eboh); }
422 template<
int _Nm,
typename _Tp>
423 struct _Sp_ebo_helper<_Nm, _Tp, false>
425 explicit _Sp_ebo_helper(
const _Tp& __tp) : _M_tp(__tp) { }
426 explicit _Sp_ebo_helper(_Tp&& __tp) : _M_tp(
std::move(__tp)) { }
429 _S_get(_Sp_ebo_helper& __eboh)
430 {
return __eboh._M_tp; }
437 template<
typename _Ptr,
typename _Deleter,
typename _Alloc, _Lock_policy _Lp>
438 class _Sp_counted_deleter final :
public _Sp_counted_base<_Lp>
440 class _Impl : _Sp_ebo_helper<0, _Deleter>, _Sp_ebo_helper<1, _Alloc>
442 typedef _Sp_ebo_helper<0, _Deleter> _Del_base;
443 typedef _Sp_ebo_helper<1, _Alloc> _Alloc_base;
446 _Impl(_Ptr __p, _Deleter __d,
const _Alloc& __a) noexcept
447 : _M_ptr(__p), _Del_base(
std::move(__d)), _Alloc_base(__a)
450 _Deleter& _M_del() noexcept {
return _Del_base::_S_get(*
this); }
451 _Alloc& _M_alloc() noexcept {
return _Alloc_base::_S_get(*
this); }
457 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_deleter>;
460 _Sp_counted_deleter(_Ptr __p, _Deleter __d) noexcept
461 : _M_impl(__p,
std::move(__d), _Alloc()) { }
464 _Sp_counted_deleter(_Ptr __p, _Deleter __d,
const _Alloc& __a) noexcept
467 ~_Sp_counted_deleter() noexcept { }
470 _M_dispose() noexcept
471 { _M_impl._M_del()(_M_impl._M_ptr); }
474 _M_destroy() noexcept
476 __allocator_type __a(_M_impl._M_alloc());
477 __allocated_ptr<__allocator_type> __guard_ptr{ __a,
this };
478 this->~_Sp_counted_deleter();
487 return __ti ==
typeid(_Deleter)
501 struct _Sp_make_shared_tag
504 template<
typename _Tp,
typename _Alloc, _Lock_policy _Lp>
505 friend class _Sp_counted_ptr_inplace;
507 static const type_info&
508 _S_ti() noexcept _GLIBCXX_VISIBILITY(default)
510 alignas(type_info)
static constexpr
char __tag[
sizeof(type_info)] = { };
511 return reinterpret_cast<const type_info&
>(__tag);
514 static bool _S_eq(
const type_info&) noexcept;
517 template<
typename _Alloc>
518 struct _Sp_alloc_shared_tag
523 template<
typename _Tp,
typename _Alloc, _Lock_policy _Lp>
524 class _Sp_counted_ptr_inplace final :
public _Sp_counted_base<_Lp>
526 class _Impl : _Sp_ebo_helper<0, _Alloc>
528 typedef _Sp_ebo_helper<0, _Alloc> _A_base;
531 explicit _Impl(_Alloc __a) noexcept : _A_base(__a) { }
533 _Alloc& _M_alloc() noexcept {
return _A_base::_S_get(*
this); }
535 __gnu_cxx::__aligned_buffer<_Tp> _M_storage;
539 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>;
542 template<
typename... _Args>
543 _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
549 std::forward<_Args>(__args)...);
552 ~_Sp_counted_ptr_inplace() noexcept { }
555 _M_dispose() noexcept
562 _M_destroy() noexcept
564 __allocator_type __a(_M_impl._M_alloc());
565 __allocated_ptr<__allocator_type> __guard_ptr{ __a,
this };
566 this->~_Sp_counted_ptr_inplace();
570 friend class __shared_count<_Lp>;
577 auto __ptr =
const_cast<typename remove_cv<_Tp>::type*
>(_M_ptr());
582 if (&__ti == &_Sp_make_shared_tag::_S_ti()
585 __ti ==
typeid(_Sp_make_shared_tag)
587 _Sp_make_shared_tag::_S_eq(__ti)
594 _Tp* _M_ptr() noexcept {
return _M_impl._M_storage._M_ptr(); }
600 struct __sp_array_delete
602 template<
typename _Yp>
603 void operator()(_Yp* __p)
const {
delete[] __p; }
606 template<_Lock_policy _Lp>
609 template<
typename _Tp>
610 struct __not_alloc_shared_tag {
using type = void; };
612 template<
typename _Tp>
613 struct __not_alloc_shared_tag<_Sp_alloc_shared_tag<_Tp>> { };
616 constexpr __shared_count() noexcept : _M_pi(0)
619 template<
typename _Ptr>
621 __shared_count(_Ptr __p) : _M_pi(0)
625 _M_pi =
new _Sp_counted_ptr<_Ptr, _Lp>(__p);
630 __throw_exception_again;
634 template<
typename _Ptr>
636 : __shared_count(__p)
639 template<
typename _Ptr>
641 : __shared_count(__p, __sp_array_delete{}, allocator<void>())
644 template<
typename _Ptr,
typename _Deleter,
645 typename =
typename __not_alloc_shared_tag<_Deleter>::type>
646 __shared_count(_Ptr __p, _Deleter __d)
647 : __shared_count(__p,
std::
move(__d), allocator<void>())
650 template<
typename _Ptr,
typename _Deleter,
typename _Alloc,
651 typename =
typename __not_alloc_shared_tag<_Deleter>::type>
652 __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
654 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
657 typename _Sp_cd_type::__allocator_type __a2(__a);
659 _Sp_cd_type* __mem = __guard.get();
667 __throw_exception_again;
671 template<
typename _Tp,
typename _Alloc,
typename... _Args>
672 __shared_count(_Tp*& __p, _Sp_alloc_shared_tag<_Alloc> __a,
675 typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type;
676 typename _Sp_cp_type::__allocator_type __a2(__a._M_a);
678 _Sp_cp_type* __mem = __guard.get();
679 auto __pi = ::new (__mem)
680 _Sp_cp_type(__a._M_a, std::forward<_Args>(__args)...);
683 __p = __pi->_M_ptr();
686 #if _GLIBCXX_USE_DEPRECATED
687 #pragma GCC diagnostic push
688 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
690 template<
typename _Tp>
693 #pragma GCC diagnostic pop
697 template<
typename _Tp,
typename _Del>
703 if (__r.get() ==
nullptr)
706 using _Ptr =
typename unique_ptr<_Tp, _Del>::pointer;
707 using _Del2 =
typename conditional<is_reference<_Del>::value,
708 reference_wrapper<typename remove_reference<_Del>::type>,
711 = _Sp_counted_deleter<_Ptr, _Del2, allocator<void>, _Lp>;
712 using _Alloc = allocator<_Sp_cd_type>;
713 using _Alloc_traits = allocator_traits<_Alloc>;
715 _Sp_cd_type* __mem = _Alloc_traits::allocate(__a, 1);
716 _Alloc_traits::construct(__a, __mem, __r.release(),
722 explicit __shared_count(
const __weak_count<_Lp>& __r);
725 explicit __shared_count(
const __weak_count<_Lp>& __r, std::nothrow_t);
727 ~__shared_count() noexcept
729 if (_M_pi !=
nullptr)
733 __shared_count(
const __shared_count& __r) noexcept
737 _M_pi->_M_add_ref_copy();
741 operator=(
const __shared_count& __r) noexcept
743 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
747 __tmp->_M_add_ref_copy();
756 _M_swap(__shared_count& __r) noexcept
758 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
764 _M_get_use_count() const noexcept
765 {
return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
768 _M_unique() const noexcept
769 {
return this->_M_get_use_count() == 1; }
773 {
return _M_pi ? _M_pi->_M_get_deleter(__ti) :
nullptr; }
776 _M_less(
const __shared_count& __rhs)
const noexcept
780 _M_less(
const __weak_count<_Lp>& __rhs)
const noexcept
785 operator==(
const __shared_count& __a,
const __shared_count& __b) noexcept
786 {
return __a._M_pi == __b._M_pi; }
789 friend class __weak_count<_Lp>;
791 _Sp_counted_base<_Lp>* _M_pi;
795 template<_Lock_policy _Lp>
799 constexpr __weak_count() noexcept : _M_pi(
nullptr)
802 __weak_count(
const __shared_count<_Lp>& __r) noexcept
805 if (_M_pi !=
nullptr)
806 _M_pi->_M_weak_add_ref();
809 __weak_count(
const __weak_count& __r) noexcept
812 if (_M_pi !=
nullptr)
813 _M_pi->_M_weak_add_ref();
816 __weak_count(__weak_count&& __r) noexcept
818 { __r._M_pi =
nullptr; }
820 ~__weak_count() noexcept
822 if (_M_pi !=
nullptr)
823 _M_pi->_M_weak_release();
827 operator=(
const __shared_count<_Lp>& __r) noexcept
829 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
830 if (__tmp !=
nullptr)
831 __tmp->_M_weak_add_ref();
832 if (_M_pi !=
nullptr)
833 _M_pi->_M_weak_release();
839 operator=(
const __weak_count& __r) noexcept
841 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
842 if (__tmp !=
nullptr)
843 __tmp->_M_weak_add_ref();
844 if (_M_pi !=
nullptr)
845 _M_pi->_M_weak_release();
851 operator=(__weak_count&& __r) noexcept
853 if (_M_pi !=
nullptr)
854 _M_pi->_M_weak_release();
861 _M_swap(__weak_count& __r) noexcept
863 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
869 _M_get_use_count() const noexcept
870 {
return _M_pi !=
nullptr ? _M_pi->_M_get_use_count() : 0; }
873 _M_less(
const __weak_count& __rhs)
const noexcept
877 _M_less(
const __shared_count<_Lp>& __rhs)
const noexcept
882 operator==(
const __weak_count& __a,
const __weak_count& __b) noexcept
883 {
return __a._M_pi == __b._M_pi; }
886 friend class __shared_count<_Lp>;
888 _Sp_counted_base<_Lp>* _M_pi;
892 template<_Lock_policy _Lp>
894 __shared_count<_Lp>::__shared_count(
const __weak_count<_Lp>& __r)
897 if (_M_pi !=
nullptr)
898 _M_pi->_M_add_ref_lock();
900 __throw_bad_weak_ptr();
904 template<_Lock_policy _Lp>
906 __shared_count<_Lp>::
907 __shared_count(
const __weak_count<_Lp>& __r, std::nothrow_t)
910 if (_M_pi !=
nullptr)
911 if (!_M_pi->_M_add_ref_lock_nothrow())
915 #define __cpp_lib_shared_ptr_arrays 201611L
921 template<
typename _Yp_ptr,
typename _Tp_ptr>
922 struct __sp_compatible_with
926 template<
typename _Yp,
typename _Tp>
927 struct __sp_compatible_with<_Yp*, _Tp*>
928 : is_convertible<_Yp*, _Tp*>::type
931 template<
typename _Up,
size_t _Nm>
932 struct __sp_compatible_with<_Up(*)[_Nm], _Up(*)[]>
936 template<
typename _Up,
size_t _Nm>
937 struct __sp_compatible_with<_Up(*)[_Nm],
const _Up(*)[]>
941 template<
typename _Up,
size_t _Nm>
942 struct __sp_compatible_with<_Up(*)[_Nm],
volatile _Up(*)[]>
946 template<
typename _Up,
size_t _Nm>
947 struct __sp_compatible_with<_Up(*)[_Nm],
const volatile _Up(*)[]>
952 template<
typename _Up,
size_t _Nm,
typename _Yp,
typename =
void>
953 struct __sp_is_constructible_arrN
957 template<
typename _Up,
size_t _Nm,
typename _Yp>
958 struct __sp_is_constructible_arrN<_Up, _Nm, _Yp, __void_t<_Yp[_Nm]>>
959 : is_convertible<_Yp(*)[_Nm], _Up(*)[_Nm]>::type
963 template<
typename _Up,
typename _Yp,
typename =
void>
964 struct __sp_is_constructible_arr
968 template<
typename _Up,
typename _Yp>
969 struct __sp_is_constructible_arr<_Up, _Yp, __void_t<_Yp[]>>
970 : is_convertible<_Yp(*)[], _Up(*)[]>::type
974 template<
typename _Tp,
typename _Yp>
975 struct __sp_is_constructible;
978 template<
typename _Up,
size_t _Nm,
typename _Yp>
979 struct __sp_is_constructible<_Up[_Nm], _Yp>
980 : __sp_is_constructible_arrN<_Up, _Nm, _Yp>::type
984 template<
typename _Up,
typename _Yp>
985 struct __sp_is_constructible<_Up[], _Yp>
986 : __sp_is_constructible_arr<_Up, _Yp>::type
990 template<
typename _Tp,
typename _Yp>
991 struct __sp_is_constructible
992 : is_convertible<_Yp*, _Tp*>::type
997 template<
typename _Tp, _Lock_policy _Lp,
998 bool = is_array<_Tp>::value,
bool = is_void<_Tp>::value>
999 class __shared_ptr_access
1002 using element_type = _Tp;
1007 __glibcxx_assert(_M_get() !=
nullptr);
1012 operator->() const noexcept
1014 _GLIBCXX_DEBUG_PEDASSERT(_M_get() !=
nullptr);
1020 _M_get() const noexcept
1021 {
return static_cast<const __shared_ptr<_Tp, _Lp>*
>(
this)->get(); }
1025 template<
typename _Tp, _Lock_policy _Lp>
1026 class __shared_ptr_access<_Tp, _Lp, false, true>
1029 using element_type = _Tp;
1032 operator->() const noexcept
1034 auto __ptr =
static_cast<const __shared_ptr<_Tp, _Lp>*
>(
this)->get();
1035 _GLIBCXX_DEBUG_PEDASSERT(__ptr !=
nullptr);
1041 template<
typename _Tp, _Lock_policy _Lp>
1042 class __shared_ptr_access<_Tp, _Lp, true, false>
1045 using element_type =
typename remove_extent<_Tp>::type;
1047 #if __cplusplus <= 201402L
1048 [[__deprecated__(
"shared_ptr<T[]>::operator* is absent from C++17")]]
1052 __glibcxx_assert(_M_get() !=
nullptr);
1056 [[__deprecated__(
"shared_ptr<T[]>::operator-> is absent from C++17")]]
1058 operator->() const noexcept
1060 _GLIBCXX_DEBUG_PEDASSERT(_M_get() !=
nullptr);
1066 operator[](ptrdiff_t __i)
const
1068 __glibcxx_assert(_M_get() !=
nullptr);
1069 __glibcxx_assert(!extent<_Tp>::value || __i < extent<_Tp>::value);
1070 return _M_get()[__i];
1075 _M_get() const noexcept
1076 {
return static_cast<const __shared_ptr<_Tp, _Lp>*
>(
this)->get(); }
1079 template<
typename _Tp, _Lock_policy _Lp>
1081 :
public __shared_ptr_access<_Tp, _Lp>
1084 using element_type =
typename remove_extent<_Tp>::type;
1088 template<
typename _Yp>
1090 =
typename enable_if<__sp_is_constructible<_Tp, _Yp>::value>::type;
1093 template<
typename _Yp,
typename _Res =
void>
1094 using _Compatible =
typename
1095 enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type;
1098 template<
typename _Yp>
1099 using _Assignable = _Compatible<_Yp, __shared_ptr&>;
1102 template<
typename _Yp,
typename _Del,
typename _Res = void,
1103 typename _Ptr =
typename unique_ptr<_Yp, _Del>::pointer>
1104 using _UniqCompatible =
typename enable_if<__and_<
1105 __sp_compatible_with<_Yp*, _Tp*>, is_convertible<_Ptr, element_type*>
1106 >::value, _Res>::type;
1109 template<
typename _Yp,
typename _Del>
1110 using _UniqAssignable = _UniqCompatible<_Yp, _Del, __shared_ptr&>;
1114 #if __cplusplus > 201402L
1115 using weak_type = __weak_ptr<_Tp, _Lp>;
1118 constexpr __shared_ptr() noexcept
1119 : _M_ptr(0), _M_refcount()
1122 template<
typename _Yp,
typename = _SafeConv<_Yp>>
1124 __shared_ptr(_Yp* __p)
1125 : _M_ptr(__p), _M_refcount(__p, typename is_array<_Tp>::type())
1127 static_assert( !is_void<_Yp>::value,
"incomplete type" );
1128 static_assert(
sizeof(_Yp) > 0,
"incomplete type" );
1129 _M_enable_shared_from_this_with(__p);
1132 template<
typename _Yp,
typename _Deleter,
typename = _SafeConv<_Yp>>
1133 __shared_ptr(_Yp* __p, _Deleter __d)
1134 : _M_ptr(__p), _M_refcount(__p,
std::
move(__d))
1136 static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
1137 "deleter expression d(p) is well-formed");
1138 _M_enable_shared_from_this_with(__p);
1141 template<
typename _Yp,
typename _Deleter,
typename _Alloc,
1142 typename = _SafeConv<_Yp>>
1143 __shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a)
1146 static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
1147 "deleter expression d(p) is well-formed");
1148 _M_enable_shared_from_this_with(__p);
1151 template<
typename _Deleter>
1152 __shared_ptr(nullptr_t __p, _Deleter __d)
1153 : _M_ptr(0), _M_refcount(__p,
std::
move(__d))
1156 template<
typename _Deleter,
typename _Alloc>
1157 __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
1162 template<
typename _Yp>
1163 __shared_ptr(
const __shared_ptr<_Yp, _Lp>& __r,
1164 element_type* __p) noexcept
1165 : _M_ptr(__p), _M_refcount(__r._M_refcount)
1169 template<
typename _Yp>
1170 __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r,
1171 element_type* __p) noexcept
1172 : _M_ptr(__p), _M_refcount()
1174 _M_refcount._M_swap(__r._M_refcount);
1178 __shared_ptr(
const __shared_ptr&) noexcept =
default;
1179 __shared_ptr& operator=(
const __shared_ptr&) noexcept =
default;
1180 ~__shared_ptr() =
default;
1182 template<
typename _Yp,
typename = _Compatible<_Yp>>
1183 __shared_ptr(
const __shared_ptr<_Yp, _Lp>& __r) noexcept
1184 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1187 __shared_ptr(__shared_ptr&& __r) noexcept
1188 : _M_ptr(__r._M_ptr), _M_refcount()
1190 _M_refcount._M_swap(__r._M_refcount);
1194 template<
typename _Yp,
typename = _Compatible<_Yp>>
1195 __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r) noexcept
1196 : _M_ptr(__r._M_ptr), _M_refcount()
1198 _M_refcount._M_swap(__r._M_refcount);
1202 template<
typename _Yp,
typename = _Compatible<_Yp>>
1203 explicit __shared_ptr(
const __weak_ptr<_Yp, _Lp>& __r)
1204 : _M_refcount(__r._M_refcount)
1208 _M_ptr = __r._M_ptr;
1212 template<
typename _Yp,
typename _Del,
1213 typename = _UniqCompatible<_Yp, _Del>>
1214 __shared_ptr(unique_ptr<_Yp, _Del>&& __r)
1215 : _M_ptr(__r.get()), _M_refcount()
1217 auto __raw = __to_address(__r.get());
1218 _M_refcount = __shared_count<_Lp>(
std::move(__r));
1219 _M_enable_shared_from_this_with(__raw);
1222 #if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED
1225 template<
typename _Tp1,
typename _Del,
1226 typename enable_if<__and_<
1227 __not_<is_array<_Tp>>, is_array<_Tp1>,
1228 is_convertible<typename unique_ptr<_Tp1, _Del>::pointer, _Tp*>
1229 >::value,
bool>::type =
true>
1230 __shared_ptr(unique_ptr<_Tp1, _Del>&& __r, __sp_array_delete)
1231 : _M_ptr(__r.get()), _M_refcount()
1233 auto __raw = __to_address(__r.get());
1234 _M_refcount = __shared_count<_Lp>(
std::move(__r));
1235 _M_enable_shared_from_this_with(__raw);
1240 #if _GLIBCXX_USE_DEPRECATED
1241 #pragma GCC diagnostic push
1242 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1244 template<
typename _Yp,
typename = _Compatible<_Yp>>
1245 __shared_ptr(auto_ptr<_Yp>&& __r);
1246 #pragma GCC diagnostic pop
1249 constexpr __shared_ptr(nullptr_t) noexcept : __shared_ptr() { }
1251 template<
typename _Yp>
1253 operator=(
const __shared_ptr<_Yp, _Lp>& __r) noexcept
1255 _M_ptr = __r._M_ptr;
1256 _M_refcount = __r._M_refcount;
1260 #if _GLIBCXX_USE_DEPRECATED
1261 #pragma GCC diagnostic push
1262 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1263 template<
typename _Yp>
1265 operator=(auto_ptr<_Yp>&& __r)
1267 __shared_ptr(
std::move(__r)).swap(*
this);
1270 #pragma GCC diagnostic pop
1274 operator=(__shared_ptr&& __r) noexcept
1276 __shared_ptr(
std::move(__r)).swap(*
this);
1282 operator=(__shared_ptr<_Yp, _Lp>&& __r) noexcept
1284 __shared_ptr(
std::move(__r)).swap(*
this);
1288 template<
typename _Yp,
typename _Del>
1289 _UniqAssignable<_Yp, _Del>
1290 operator=(unique_ptr<_Yp, _Del>&& __r)
1292 __shared_ptr(
std::move(__r)).swap(*
this);
1298 { __shared_ptr().swap(*
this); }
1300 template<
typename _Yp>
1305 __glibcxx_assert(__p == 0 || __p != _M_ptr);
1306 __shared_ptr(__p).swap(*
this);
1309 template<
typename _Yp,
typename _Deleter>
1311 reset(_Yp* __p, _Deleter __d)
1312 { __shared_ptr(__p,
std::move(__d)).swap(*
this); }
1314 template<
typename _Yp,
typename _Deleter,
typename _Alloc>
1316 reset(_Yp* __p, _Deleter __d, _Alloc __a)
1321 get() const noexcept
1325 explicit operator bool() const
1326 {
return _M_ptr == 0 ? false :
true; }
1330 unique() const noexcept
1331 {
return _M_refcount._M_unique(); }
1335 use_count() const noexcept
1336 {
return _M_refcount._M_get_use_count(); }
1340 swap(__shared_ptr<_Tp, _Lp>& __other) noexcept
1342 std::swap(_M_ptr, __other._M_ptr);
1343 _M_refcount._M_swap(__other._M_refcount);
1353 template<
typename _Tp1>
1355 owner_before(__shared_ptr<_Tp1, _Lp>
const& __rhs)
const noexcept
1356 {
return _M_refcount._M_less(__rhs._M_refcount); }
1358 template<
typename _Tp1>
1360 owner_before(__weak_ptr<_Tp1, _Lp>
const& __rhs)
const noexcept
1361 {
return _M_refcount._M_less(__rhs._M_refcount); }
1366 template<
typename _Alloc,
typename... _Args>
1367 __shared_ptr(_Sp_alloc_shared_tag<_Alloc> __tag, _Args&&... __args)
1368 : _M_ptr(), _M_refcount(_M_ptr, __tag,
std::
forward<_Args>(__args)...)
1369 { _M_enable_shared_from_this_with(_M_ptr); }
1371 template<
typename _Tp1, _Lock_policy _Lp1,
typename _Alloc,
1373 friend __shared_ptr<_Tp1, _Lp1>
1374 __allocate_shared(
const _Alloc& __a, _Args&&... __args);
1378 __shared_ptr(
const __weak_ptr<_Tp, _Lp>& __r, std::nothrow_t)
1379 : _M_refcount(__r._M_refcount,
std::nothrow)
1381 _M_ptr = _M_refcount._M_get_use_count() ? __r._M_ptr :
nullptr;
1384 friend class __weak_ptr<_Tp, _Lp>;
1388 template<
typename _Yp>
1389 using __esft_base_t = decltype(__enable_shared_from_this_base(
1390 std::declval<
const __shared_count<_Lp>&>(),
1391 std::declval<_Yp*>()));
1394 template<
typename _Yp,
typename =
void>
1395 struct __has_esft_base
1398 template<
typename _Yp>
1399 struct __has_esft_base<_Yp, __void_t<__esft_base_t<_Yp>>>
1400 : __not_<is_array<_Tp>> { };
1402 template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
1403 typename enable_if<__has_esft_base<_Yp2>::value>::type
1404 _M_enable_shared_from_this_with(_Yp* __p) noexcept
1406 if (
auto __base = __enable_shared_from_this_base(_M_refcount, __p))
1407 __base->_M_weak_assign(
const_cast<_Yp2*
>(__p), _M_refcount);
1410 template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
1411 typename enable_if<!__has_esft_base<_Yp2>::value>::type
1412 _M_enable_shared_from_this_with(_Yp*) noexcept
1417 {
return _M_refcount._M_get_deleter(__ti); }
1419 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
1420 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
1422 template<
typename _Del,
typename _Tp1, _Lock_policy _Lp1>
1423 friend _Del* get_deleter(
const __shared_ptr<_Tp1, _Lp1>&) noexcept;
1425 template<
typename _Del,
typename _Tp1>
1426 friend _Del* get_deleter(
const shared_ptr<_Tp1>&) noexcept;
1428 element_type* _M_ptr;
1429 __shared_count<_Lp> _M_refcount;
1434 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1436 operator==(
const __shared_ptr<_Tp1, _Lp>& __a,
1437 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1438 {
return __a.get() == __b.get(); }
1440 template<
typename _Tp, _Lock_policy _Lp>
1442 operator==(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1445 template<
typename _Tp, _Lock_policy _Lp>
1447 operator==(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1450 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1452 operator!=(
const __shared_ptr<_Tp1, _Lp>& __a,
1453 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1454 {
return __a.get() != __b.get(); }
1456 template<
typename _Tp, _Lock_policy _Lp>
1458 operator!=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1459 {
return (
bool)__a; }
1461 template<
typename _Tp, _Lock_policy _Lp>
1463 operator!=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1464 {
return (
bool)__a; }
1466 template<
typename _Tp,
typename _Up, _Lock_policy _Lp>
1468 operator<(
const __shared_ptr<_Tp, _Lp>& __a,
1469 const __shared_ptr<_Up, _Lp>& __b) noexcept
1471 using _Tp_elt =
typename __shared_ptr<_Tp, _Lp>::element_type;
1472 using _Up_elt =
typename __shared_ptr<_Up, _Lp>::element_type;
1473 using _Vp =
typename common_type<_Tp_elt*, _Up_elt*>::type;
1474 return less<_Vp>()(__a.get(), __b.get());
1477 template<
typename _Tp, _Lock_policy _Lp>
1479 operator<(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1481 using _Tp_elt =
typename __shared_ptr<_Tp, _Lp>::element_type;
1482 return less<_Tp_elt*>()(__a.get(),
nullptr);
1485 template<
typename _Tp, _Lock_policy _Lp>
1487 operator<(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1489 using _Tp_elt =
typename __shared_ptr<_Tp, _Lp>::element_type;
1490 return less<_Tp_elt*>()(
nullptr, __a.get());
1493 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1495 operator<=(
const __shared_ptr<_Tp1, _Lp>& __a,
1496 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1497 {
return !(__b < __a); }
1499 template<
typename _Tp, _Lock_policy _Lp>
1501 operator<=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1502 {
return !(
nullptr < __a); }
1504 template<
typename _Tp, _Lock_policy _Lp>
1506 operator<=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1507 {
return !(__a <
nullptr); }
1509 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1511 operator>(
const __shared_ptr<_Tp1, _Lp>& __a,
1512 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1513 {
return (__b < __a); }
1515 template<
typename _Tp, _Lock_policy _Lp>
1517 operator>(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1518 {
return nullptr < __a; }
1520 template<
typename _Tp, _Lock_policy _Lp>
1522 operator>(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1523 {
return __a <
nullptr; }
1525 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1527 operator>=(
const __shared_ptr<_Tp1, _Lp>& __a,
1528 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1529 {
return !(__a < __b); }
1531 template<
typename _Tp, _Lock_policy _Lp>
1533 operator>=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1534 {
return !(__a <
nullptr); }
1536 template<
typename _Tp, _Lock_policy _Lp>
1538 operator>=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1539 {
return !(
nullptr < __a); }
1542 template<
typename _Tp, _Lock_policy _Lp>
1544 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b) noexcept
1554 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1555 inline __shared_ptr<_Tp, _Lp>
1558 using _Sp = __shared_ptr<_Tp, _Lp>;
1559 return _Sp(__r,
static_cast<typename _Sp::element_type*
>(__r.get()));
1567 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1568 inline __shared_ptr<_Tp, _Lp>
1571 using _Sp = __shared_ptr<_Tp, _Lp>;
1572 return _Sp(__r,
const_cast<typename _Sp::element_type*
>(__r.get()));
1580 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1581 inline __shared_ptr<_Tp, _Lp>
1584 using _Sp = __shared_ptr<_Tp, _Lp>;
1585 if (
auto* __p =
dynamic_cast<typename _Sp::element_type*
>(__r.get()))
1586 return _Sp(__r, __p);
1590 #if __cplusplus > 201402L
1591 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1592 inline __shared_ptr<_Tp, _Lp>
1593 reinterpret_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1595 using _Sp = __shared_ptr<_Tp, _Lp>;
1596 return _Sp(__r,
reinterpret_cast<typename _Sp::element_type*
>(__r.get()));
1600 template<
typename _Tp, _Lock_policy _Lp>
1603 template<
typename _Yp,
typename _Res =
void>
1604 using _Compatible =
typename
1605 enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type;
1608 template<
typename _Yp>
1609 using _Assignable = _Compatible<_Yp, __weak_ptr&>;
1612 using element_type =
typename remove_extent<_Tp>::type;
1614 constexpr __weak_ptr() noexcept
1615 : _M_ptr(
nullptr), _M_refcount()
1618 __weak_ptr(
const __weak_ptr&) noexcept =
default;
1620 ~__weak_ptr() =
default;
1636 template<
typename _Yp,
typename = _Compatible<_Yp>>
1637 __weak_ptr(
const __weak_ptr<_Yp, _Lp>& __r) noexcept
1638 : _M_refcount(__r._M_refcount)
1639 { _M_ptr = __r.lock().get(); }
1641 template<
typename _Yp,
typename = _Compatible<_Yp>>
1642 __weak_ptr(
const __shared_ptr<_Yp, _Lp>& __r) noexcept
1643 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1646 __weak_ptr(__weak_ptr&& __r) noexcept
1647 : _M_ptr(__r._M_ptr), _M_refcount(
std::move(__r._M_refcount))
1648 { __r._M_ptr =
nullptr; }
1650 template<
typename _Yp,
typename = _Compatible<_Yp>>
1651 __weak_ptr(__weak_ptr<_Yp, _Lp>&& __r) noexcept
1652 : _M_ptr(__r.lock().get()), _M_refcount(
std::move(__r._M_refcount))
1653 { __r._M_ptr =
nullptr; }
1656 operator=(
const __weak_ptr& __r) noexcept =
default;
1658 template<
typename _Yp>
1660 operator=(
const __weak_ptr<_Yp, _Lp>& __r) noexcept
1662 _M_ptr = __r.lock().get();
1663 _M_refcount = __r._M_refcount;
1667 template<
typename _Yp>
1669 operator=(
const __shared_ptr<_Yp, _Lp>& __r) noexcept
1671 _M_ptr = __r._M_ptr;
1672 _M_refcount = __r._M_refcount;
1677 operator=(__weak_ptr&& __r) noexcept
1679 _M_ptr = __r._M_ptr;
1680 _M_refcount =
std::move(__r._M_refcount);
1681 __r._M_ptr =
nullptr;
1685 template<
typename _Yp>
1687 operator=(__weak_ptr<_Yp, _Lp>&& __r) noexcept
1689 _M_ptr = __r.lock().get();
1690 _M_refcount =
std::move(__r._M_refcount);
1691 __r._M_ptr =
nullptr;
1695 __shared_ptr<_Tp, _Lp>
1696 lock() const noexcept
1697 {
return __shared_ptr<element_type, _Lp>(*
this, std::nothrow); }
1700 use_count() const noexcept
1701 {
return _M_refcount._M_get_use_count(); }
1704 expired() const noexcept
1705 {
return _M_refcount._M_get_use_count() == 0; }
1707 template<
typename _Tp1>
1709 owner_before(
const __shared_ptr<_Tp1, _Lp>& __rhs)
const noexcept
1710 {
return _M_refcount._M_less(__rhs._M_refcount); }
1712 template<
typename _Tp1>
1714 owner_before(
const __weak_ptr<_Tp1, _Lp>& __rhs)
const noexcept
1715 {
return _M_refcount._M_less(__rhs._M_refcount); }
1719 { __weak_ptr().swap(*
this); }
1722 swap(__weak_ptr& __s) noexcept
1724 std::swap(_M_ptr, __s._M_ptr);
1725 _M_refcount._M_swap(__s._M_refcount);
1731 _M_assign(_Tp* __ptr,
const __shared_count<_Lp>& __refcount) noexcept
1733 if (use_count() == 0)
1736 _M_refcount = __refcount;
1740 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
1741 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
1742 friend class __enable_shared_from_this<_Tp, _Lp>;
1743 friend class enable_shared_from_this<_Tp>;
1745 element_type* _M_ptr;
1746 __weak_count<_Lp> _M_refcount;
1750 template<
typename _Tp, _Lock_policy _Lp>
1752 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b) noexcept
1755 template<
typename _Tp,
typename _Tp1>
1756 struct _Sp_owner_less :
public binary_function<_Tp, _Tp, bool>
1759 operator()(
const _Tp& __lhs,
const _Tp& __rhs)
const noexcept
1760 {
return __lhs.owner_before(__rhs); }
1763 operator()(
const _Tp& __lhs,
const _Tp1& __rhs)
const noexcept
1764 {
return __lhs.owner_before(__rhs); }
1767 operator()(
const _Tp1& __lhs,
const _Tp& __rhs)
const noexcept
1768 {
return __lhs.owner_before(__rhs); }
1772 struct _Sp_owner_less<void, void>
1774 template<
typename _Tp,
typename _Up>
1776 operator()(
const _Tp& __lhs,
const _Up& __rhs)
const noexcept
1777 -> decltype(__lhs.owner_before(__rhs))
1778 {
return __lhs.owner_before(__rhs); }
1780 using is_transparent = void;
1783 template<
typename _Tp, _Lock_policy _Lp>
1784 struct owner_less<__shared_ptr<_Tp, _Lp>>
1785 :
public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
1788 template<
typename _Tp, _Lock_policy _Lp>
1789 struct owner_less<__weak_ptr<_Tp, _Lp>>
1790 :
public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
1794 template<
typename _Tp, _Lock_policy _Lp>
1795 class __enable_shared_from_this
1798 constexpr __enable_shared_from_this() noexcept { }
1800 __enable_shared_from_this(
const __enable_shared_from_this&) noexcept { }
1802 __enable_shared_from_this&
1803 operator=(
const __enable_shared_from_this&) noexcept
1806 ~__enable_shared_from_this() { }
1809 __shared_ptr<_Tp, _Lp>
1811 {
return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
1813 __shared_ptr<const _Tp, _Lp>
1814 shared_from_this()
const
1815 {
return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
1817 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
1818 __weak_ptr<_Tp, _Lp>
1819 weak_from_this() noexcept
1820 {
return this->_M_weak_this; }
1822 __weak_ptr<const _Tp, _Lp>
1823 weak_from_this() const noexcept
1824 {
return this->_M_weak_this; }
1828 template<
typename _Tp1>
1830 _M_weak_assign(_Tp1* __p,
const __shared_count<_Lp>& __n)
const noexcept
1831 { _M_weak_this._M_assign(__p, __n); }
1833 friend const __enable_shared_from_this*
1834 __enable_shared_from_this_base(
const __shared_count<_Lp>&,
1835 const __enable_shared_from_this* __p)
1838 template<
typename, _Lock_policy>
1839 friend class __shared_ptr;
1841 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
1844 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy,
1845 typename _Alloc,
typename... _Args>
1846 inline __shared_ptr<_Tp, _Lp>
1847 __allocate_shared(
const _Alloc& __a, _Args&&... __args)
1849 return __shared_ptr<_Tp, _Lp>(_Sp_alloc_shared_tag<_Alloc>{__a},
1850 std::forward<_Args>(__args)...);
1853 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy,
1855 inline __shared_ptr<_Tp, _Lp>
1856 __make_shared(_Args&&... __args)
1858 typedef typename std::remove_const<_Tp>::type _Tp_nc;
1860 std::forward<_Args>(__args)...);
1864 template<
typename _Tp, _Lock_policy _Lp>
1865 struct hash<__shared_ptr<_Tp, _Lp>>
1866 :
public __hash_base<size_t, __shared_ptr<_Tp, _Lp>>
1869 operator()(
const __shared_ptr<_Tp, _Lp>& __s)
const noexcept
1876 _GLIBCXX_END_NAMESPACE_VERSION
1879 #endif // _SHARED_PTR_BASE_H