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>
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
465 : _M_impl(__p, std::move(__d), __a) { }
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());
478 this->~_Sp_counted_deleter();
487 return __ti ==
typeid(_Deleter)
501 struct _Sp_make_shared_tag
505 template<
typename _Tp, _Lock_policy _Lp>
506 friend class __shared_ptr;
507 template<
typename _Tp,
typename _Alloc, _Lock_policy _Lp>
508 friend class _Sp_counted_ptr_inplace;
511 _S_ti() noexcept _GLIBCXX_VISIBILITY(
default)
513 alignas(
type_info)
static constexpr _Sp_make_shared_tag __tag;
514 return reinterpret_cast<const type_info&
>(__tag);
519 template<
typename _Tp,
typename _Alloc, _Lock_policy _Lp>
520 class _Sp_counted_ptr_inplace final :
public _Sp_counted_base<_Lp>
522 class _Impl : _Sp_ebo_helper<0, _Alloc>
524 typedef _Sp_ebo_helper<0, _Alloc> _A_base;
527 explicit _Impl(_Alloc __a) noexcept : _A_base(__a) { }
529 _Alloc& _M_alloc() noexcept {
return _A_base::_S_get(*
this); }
531 __gnu_cxx::__aligned_buffer<_Tp> _M_storage;
535 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>;
537 template<
typename... _Args>
538 _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
544 std::forward<_Args>(__args)...);
547 ~_Sp_counted_ptr_inplace() noexcept { }
550 _M_dispose() noexcept
557 _M_destroy() noexcept
559 __allocator_type __a(_M_impl._M_alloc());
561 this->~_Sp_counted_ptr_inplace();
569 if (__ti ==
typeid(_Sp_make_shared_tag))
571 if (&__ti == &_Sp_make_shared_tag::_S_ti())
573 return const_cast<typename remove_cv<_Tp>::type*
>(_M_ptr());
578 _Tp* _M_ptr() noexcept {
return _M_impl._M_storage._M_ptr(); }
584 struct __sp_array_delete
586 template<
typename _Yp>
587 void operator()(_Yp* __p)
const {
delete[] __p; }
590 template<_Lock_policy _Lp>
594 constexpr __shared_count() noexcept : _M_pi(0)
597 template<
typename _Ptr>
599 __shared_count(_Ptr __p) : _M_pi(0)
603 _M_pi =
new _Sp_counted_ptr<_Ptr, _Lp>(__p);
608 __throw_exception_again;
612 template<
typename _Ptr>
614 : __shared_count(__p)
617 template<
typename _Ptr>
622 template<
typename _Ptr,
typename _Deleter>
623 __shared_count(_Ptr __p, _Deleter __d)
627 template<
typename _Ptr,
typename _Deleter,
typename _Alloc>
628 __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
630 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
633 typename _Sp_cd_type::__allocator_type __a2(__a);
635 _Sp_cd_type* __mem = __guard.get();
636 ::new (__mem) _Sp_cd_type(__p, std::move(__d), std::move(__a));
643 __throw_exception_again;
647 template<
typename _Tp,
typename _Alloc,
typename... _Args>
648 __shared_count(_Sp_make_shared_tag, _Tp*,
const _Alloc& __a,
652 typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type;
653 typename _Sp_cp_type::__allocator_type __a2(__a);
655 _Sp_cp_type* __mem = __guard.get();
656 ::new (__mem) _Sp_cp_type(std::move(__a),
657 std::forward<_Args>(__args)...);
662 #if _GLIBCXX_USE_DEPRECATED 663 #pragma GCC diagnostic push 664 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 666 template<
typename _Tp>
669 #pragma GCC diagnostic pop 673 template<
typename _Tp,
typename _Del>
679 if (__r.get() ==
nullptr)
682 using _Ptr =
typename unique_ptr<_Tp, _Del>::pointer;
687 = _Sp_counted_deleter<_Ptr, _Del2, allocator<void>, _Lp>;
691 _Sp_cd_type* __mem = _Alloc_traits::allocate(__a, 1);
692 _Alloc_traits::construct(__a, __mem, __r.release(),
698 explicit __shared_count(
const __weak_count<_Lp>& __r);
701 explicit __shared_count(
const __weak_count<_Lp>& __r, std::nothrow_t);
703 ~__shared_count() noexcept
705 if (_M_pi !=
nullptr)
709 __shared_count(
const __shared_count& __r) noexcept
713 _M_pi->_M_add_ref_copy();
717 operator=(
const __shared_count& __r) noexcept
719 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
723 __tmp->_M_add_ref_copy();
732 _M_swap(__shared_count& __r) noexcept
734 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
740 _M_get_use_count()
const noexcept
741 {
return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
744 _M_unique()
const noexcept
745 {
return this->_M_get_use_count() == 1; }
749 {
return _M_pi ? _M_pi->_M_get_deleter(__ti) :
nullptr; }
752 _M_less(
const __shared_count& __rhs)
const noexcept
756 _M_less(
const __weak_count<_Lp>& __rhs)
const noexcept
761 operator==(
const __shared_count& __a,
const __shared_count& __b) noexcept
762 {
return __a._M_pi == __b._M_pi; }
765 friend class __weak_count<_Lp>;
767 _Sp_counted_base<_Lp>* _M_pi;
771 template<_Lock_policy _Lp>
775 constexpr __weak_count() noexcept : _M_pi(
nullptr)
778 __weak_count(
const __shared_count<_Lp>& __r) noexcept
781 if (_M_pi !=
nullptr)
782 _M_pi->_M_weak_add_ref();
785 __weak_count(
const __weak_count& __r) noexcept
788 if (_M_pi !=
nullptr)
789 _M_pi->_M_weak_add_ref();
792 __weak_count(__weak_count&& __r) noexcept
794 { __r._M_pi =
nullptr; }
796 ~__weak_count() noexcept
798 if (_M_pi !=
nullptr)
799 _M_pi->_M_weak_release();
803 operator=(
const __shared_count<_Lp>& __r) noexcept
805 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
806 if (__tmp !=
nullptr)
807 __tmp->_M_weak_add_ref();
808 if (_M_pi !=
nullptr)
809 _M_pi->_M_weak_release();
815 operator=(
const __weak_count& __r) noexcept
817 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
818 if (__tmp !=
nullptr)
819 __tmp->_M_weak_add_ref();
820 if (_M_pi !=
nullptr)
821 _M_pi->_M_weak_release();
827 operator=(__weak_count&& __r) noexcept
829 if (_M_pi !=
nullptr)
830 _M_pi->_M_weak_release();
837 _M_swap(__weak_count& __r) noexcept
839 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
845 _M_get_use_count()
const noexcept
846 {
return _M_pi !=
nullptr ? _M_pi->_M_get_use_count() : 0; }
849 _M_less(
const __weak_count& __rhs)
const noexcept
853 _M_less(
const __shared_count<_Lp>& __rhs)
const noexcept
858 operator==(
const __weak_count& __a,
const __weak_count& __b) noexcept
859 {
return __a._M_pi == __b._M_pi; }
862 friend class __shared_count<_Lp>;
864 _Sp_counted_base<_Lp>* _M_pi;
868 template<_Lock_policy _Lp>
870 __shared_count<_Lp>::__shared_count(
const __weak_count<_Lp>& __r)
873 if (_M_pi !=
nullptr)
874 _M_pi->_M_add_ref_lock();
876 __throw_bad_weak_ptr();
880 template<_Lock_policy _Lp>
882 __shared_count<_Lp>::
883 __shared_count(
const __weak_count<_Lp>& __r, std::nothrow_t)
886 if (_M_pi !=
nullptr)
887 if (!_M_pi->_M_add_ref_lock_nothrow())
891 #define __cpp_lib_shared_ptr_arrays 201603 897 template<
typename _Yp_ptr,
typename _Tp_ptr>
898 struct __sp_compatible_with
902 template<
typename _Yp,
typename _Tp>
903 struct __sp_compatible_with<_Yp*, _Tp*>
907 template<
typename _Up,
size_t _Nm>
908 struct __sp_compatible_with<_Up(*)[_Nm], _Up(*)[]>
912 template<
typename _Up,
size_t _Nm>
913 struct __sp_compatible_with<_Up(*)[_Nm],
const _Up(*)[]>
917 template<
typename _Up,
size_t _Nm>
918 struct __sp_compatible_with<_Up(*)[_Nm],
volatile _Up(*)[]>
922 template<
typename _Up,
size_t _Nm>
923 struct __sp_compatible_with<_Up(*)[_Nm],
const volatile _Up(*)[]>
928 template<
typename _Up,
size_t _Nm,
typename _Yp,
typename =
void>
929 struct __sp_is_constructible_arrN
933 template<
typename _Up,
size_t _Nm,
typename _Yp>
934 struct __sp_is_constructible_arrN<_Up, _Nm, _Yp, __void_t<_Yp[_Nm]>>
939 template<
typename _Up,
typename _Yp,
typename =
void>
940 struct __sp_is_constructible_arr
944 template<
typename _Up,
typename _Yp>
945 struct __sp_is_constructible_arr<_Up, _Yp, __void_t<_Yp[]>>
950 template<
typename _Tp,
typename _Yp>
951 struct __sp_is_constructible;
954 template<
typename _Up,
size_t _Nm,
typename _Yp>
955 struct __sp_is_constructible<_Up[_Nm], _Yp>
956 : __sp_is_constructible_arrN<_Up, _Nm, _Yp>::type
960 template<
typename _Up,
typename _Yp>
961 struct __sp_is_constructible<_Up[], _Yp>
962 : __sp_is_constructible_arr<_Up, _Yp>::type
966 template<
typename _Tp,
typename _Yp>
967 struct __sp_is_constructible
973 template<
typename _Tp, _Lock_policy _Lp,
975 class __shared_ptr_access
978 using element_type = _Tp;
983 __glibcxx_assert(_M_get() !=
nullptr);
988 operator->()
const noexcept
990 _GLIBCXX_DEBUG_PEDASSERT(_M_get() !=
nullptr);
996 _M_get()
const noexcept
997 {
return static_cast<const __shared_ptr<_Tp, _Lp>*
>(
this)->
get(); }
1001 template<
typename _Tp, _Lock_policy _Lp>
1002 class __shared_ptr_access<_Tp, _Lp, false, true>
1005 using element_type = _Tp;
1008 operator->()
const noexcept
1010 auto __ptr =
static_cast<const __shared_ptr<_Tp, _Lp>*
>(
this)->
get();
1011 _GLIBCXX_DEBUG_PEDASSERT(__ptr !=
nullptr);
1017 template<
typename _Tp, _Lock_policy _Lp>
1018 class __shared_ptr_access<_Tp, _Lp, true, false>
1021 using element_type =
typename remove_extent<_Tp>::type;
1023 #if __cplusplus <= 201402L 1024 [[__deprecated__(
"shared_ptr<T[]>::operator* is absent from C++17")]]
1028 __glibcxx_assert(_M_get() !=
nullptr);
1032 [[__deprecated__(
"shared_ptr<T[]>::operator-> is absent from C++17")]]
1034 operator->()
const noexcept
1036 _GLIBCXX_DEBUG_PEDASSERT(_M_get() !=
nullptr);
1042 operator[](ptrdiff_t __i)
const 1044 __glibcxx_assert(_M_get() !=
nullptr);
1046 return _M_get()[__i];
1051 _M_get()
const noexcept
1052 {
return static_cast<const __shared_ptr<_Tp, _Lp>*
>(
this)->
get(); }
1055 template<
typename _Tp, _Lock_policy _Lp>
1057 :
public __shared_ptr_access<_Tp, _Lp>
1060 using element_type =
typename remove_extent<_Tp>::type;
1064 template<
typename _Yp>
1069 template<
typename _Yp,
typename _Res =
void>
1070 using _Compatible =
typename 1074 template<
typename _Yp>
1075 using _Assignable = _Compatible<_Yp, __shared_ptr&>;
1078 template<
typename _Yp,
typename _Del,
typename _Res = void,
1079 typename _Ptr =
typename unique_ptr<_Yp, _Del>::pointer>
1080 using _UniqCompatible =
typename enable_if<__and_<
1082 >::value, _Res>::type;
1085 template<
typename _Yp,
typename _Del>
1086 using _UniqAssignable = _UniqCompatible<_Yp, _Del, __shared_ptr&>;
1090 #if __cplusplus > 201402L 1091 using weak_type = __weak_ptr<_Tp, _Lp>;
1094 constexpr __shared_ptr() noexcept
1095 : _M_ptr(0), _M_refcount()
1098 template<
typename _Yp,
typename = _SafeConv<_Yp>>
1100 __shared_ptr(_Yp* __p)
1104 static_assert(
sizeof(_Yp) > 0,
"incomplete type" );
1105 _M_enable_shared_from_this_with(__p);
1108 template<
typename _Yp,
typename _Deleter,
typename = _SafeConv<_Yp>>
1109 __shared_ptr(_Yp* __p, _Deleter __d)
1110 : _M_ptr(__p), _M_refcount(__p, std::move(__d))
1112 static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
1113 "deleter expression d(p) is well-formed");
1114 _M_enable_shared_from_this_with(__p);
1117 template<
typename _Yp,
typename _Deleter,
typename _Alloc,
1118 typename = _SafeConv<_Yp>>
1119 __shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a)
1120 : _M_ptr(__p), _M_refcount(__p, std::move(__d), std::move(__a))
1122 static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
1123 "deleter expression d(p) is well-formed");
1124 _M_enable_shared_from_this_with(__p);
1127 template<
typename _Deleter>
1128 __shared_ptr(nullptr_t __p, _Deleter __d)
1129 : _M_ptr(0), _M_refcount(__p, std::move(__d))
1132 template<
typename _Deleter,
typename _Alloc>
1133 __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
1134 : _M_ptr(0), _M_refcount(__p, std::move(__d), std::move(__a))
1137 template<
typename _Yp>
1138 __shared_ptr(
const __shared_ptr<_Yp, _Lp>& __r,
1139 element_type* __p) noexcept
1140 : _M_ptr(__p), _M_refcount(__r._M_refcount)
1143 __shared_ptr(
const __shared_ptr&) noexcept =
default;
1144 __shared_ptr& operator=(
const __shared_ptr&) noexcept =
default;
1145 ~__shared_ptr() =
default;
1147 template<
typename _Yp,
typename = _Compatible<_Yp>>
1148 __shared_ptr(
const __shared_ptr<_Yp, _Lp>& __r) noexcept
1149 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1152 __shared_ptr(__shared_ptr&& __r) noexcept
1153 : _M_ptr(__r._M_ptr), _M_refcount()
1155 _M_refcount._M_swap(__r._M_refcount);
1159 template<
typename _Yp,
typename = _Compatible<_Yp>>
1160 __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r) noexcept
1161 : _M_ptr(__r._M_ptr), _M_refcount()
1163 _M_refcount._M_swap(__r._M_refcount);
1167 template<
typename _Yp,
typename = _Compatible<_Yp>>
1168 explicit __shared_ptr(
const __weak_ptr<_Yp, _Lp>& __r)
1169 : _M_refcount(__r._M_refcount)
1173 _M_ptr = __r._M_ptr;
1177 template<
typename _Yp,
typename _Del,
1178 typename = _UniqCompatible<_Yp, _Del>>
1180 : _M_ptr(__r.get()), _M_refcount()
1182 auto __raw = __to_address(__r.get());
1183 _M_refcount = __shared_count<_Lp>(std::move(__r));
1184 _M_enable_shared_from_this_with(__raw);
1187 #if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED 1190 template<
typename _Tp1,
typename _Del,
1194 >::value,
bool>::type =
true>
1196 : _M_ptr(__r.get()), _M_refcount()
1198 auto __raw = __to_address(__r.get());
1199 _M_refcount = __shared_count<_Lp>(std::move(__r));
1200 _M_enable_shared_from_this_with(__raw);
1205 #if _GLIBCXX_USE_DEPRECATED 1206 #pragma GCC diagnostic push 1207 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 1209 template<
typename _Yp,
typename = _Compatible<_Yp>>
1211 #pragma GCC diagnostic pop 1214 constexpr __shared_ptr(nullptr_t) noexcept : __shared_ptr() { }
1216 template<
typename _Yp>
1218 operator=(
const __shared_ptr<_Yp, _Lp>& __r) noexcept
1220 _M_ptr = __r._M_ptr;
1221 _M_refcount = __r._M_refcount;
1225 #if _GLIBCXX_USE_DEPRECATED 1226 #pragma GCC diagnostic push 1227 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 1228 template<
typename _Yp>
1232 __shared_ptr(std::move(__r)).swap(*
this);
1235 #pragma GCC diagnostic pop 1239 operator=(__shared_ptr&& __r) noexcept
1241 __shared_ptr(std::move(__r)).swap(*
this);
1247 operator=(__shared_ptr<_Yp, _Lp>&& __r) noexcept
1249 __shared_ptr(std::move(__r)).swap(*
this);
1253 template<
typename _Yp,
typename _Del>
1254 _UniqAssignable<_Yp, _Del>
1257 __shared_ptr(std::move(__r)).swap(*
this);
1263 { __shared_ptr().swap(*
this); }
1265 template<
typename _Yp>
1270 __glibcxx_assert(__p == 0 || __p != _M_ptr);
1271 __shared_ptr(__p).swap(*
this);
1274 template<
typename _Yp,
typename _Deleter>
1276 reset(_Yp* __p, _Deleter __d)
1277 { __shared_ptr(__p, std::move(__d)).swap(*
this); }
1279 template<
typename _Yp,
typename _Deleter,
typename _Alloc>
1281 reset(_Yp* __p, _Deleter __d, _Alloc __a)
1282 { __shared_ptr(__p, std::move(__d), std::move(__a)).swap(*
this); }
1285 get()
const noexcept
1288 explicit operator bool()
const 1289 {
return _M_ptr == 0 ? false :
true; }
1292 unique()
const noexcept
1293 {
return _M_refcount._M_unique(); }
1296 use_count()
const noexcept
1297 {
return _M_refcount._M_get_use_count(); }
1300 swap(__shared_ptr<_Tp, _Lp>& __other) noexcept
1302 std::swap(_M_ptr, __other._M_ptr);
1303 _M_refcount._M_swap(__other._M_refcount);
1306 template<
typename _Tp1>
1308 owner_before(__shared_ptr<_Tp1, _Lp>
const& __rhs)
const noexcept
1309 {
return _M_refcount._M_less(__rhs._M_refcount); }
1311 template<
typename _Tp1>
1313 owner_before(__weak_ptr<_Tp1, _Lp>
const& __rhs)
const noexcept
1314 {
return _M_refcount._M_less(__rhs._M_refcount); }
1318 template<
typename _Alloc,
typename... _Args>
1319 __shared_ptr(_Sp_make_shared_tag __tag,
const _Alloc& __a,
1321 : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a,
1322 std::forward<_Args>(__args)...)
1327 void* __p = _M_refcount._M_get_deleter(
typeid(__tag));
1329 void* __p = _M_refcount._M_get_deleter(_Sp_make_shared_tag::_S_ti());
1331 _M_ptr =
static_cast<_Tp*
>(__p);
1332 _M_enable_shared_from_this_with(_M_ptr);
1335 template<
typename _Tp1, _Lock_policy _Lp1,
typename _Alloc,
1337 friend __shared_ptr<_Tp1, _Lp1>
1338 __allocate_shared(
const _Alloc& __a, _Args&&... __args);
1342 __shared_ptr(
const __weak_ptr<_Tp, _Lp>& __r, std::nothrow_t)
1343 : _M_refcount(__r._M_refcount, std::nothrow)
1345 _M_ptr = _M_refcount._M_get_use_count() ? __r._M_ptr :
nullptr;
1348 friend class __weak_ptr<_Tp, _Lp>;
1352 template<
typename _Yp>
1353 using __esft_base_t = decltype(__enable_shared_from_this_base(
1354 std::declval<
const __shared_count<_Lp>&>(),
1355 std::declval<_Yp*>()));
1358 template<
typename _Yp,
typename =
void>
1359 struct __has_esft_base
1362 template<
typename _Yp>
1363 struct __has_esft_base<_Yp, __void_t<__esft_base_t<_Yp>>>
1364 : __not_<is_array<_Tp>> { };
1366 template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
1368 _M_enable_shared_from_this_with(_Yp* __p) noexcept
1370 if (
auto __base = __enable_shared_from_this_base(_M_refcount, __p))
1371 __base->_M_weak_assign(const_cast<_Yp2*>(__p), _M_refcount);
1374 template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
1376 _M_enable_shared_from_this_with(_Yp*) noexcept
1381 {
return _M_refcount._M_get_deleter(__ti); }
1383 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
1384 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
1386 template<
typename _Del,
typename _Tp1, _Lock_policy _Lp1>
1387 friend _Del* get_deleter(
const __shared_ptr<_Tp1, _Lp1>&) noexcept;
1389 template<
typename _Del,
typename _Tp1>
1392 element_type* _M_ptr;
1393 __shared_count<_Lp> _M_refcount;
1398 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1400 operator==(
const __shared_ptr<_Tp1, _Lp>& __a,
1401 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1402 {
return __a.get() == __b.get(); }
1404 template<
typename _Tp, _Lock_policy _Lp>
1406 operator==(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1409 template<
typename _Tp, _Lock_policy _Lp>
1411 operator==(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1414 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1416 operator!=(
const __shared_ptr<_Tp1, _Lp>& __a,
1417 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1418 {
return __a.get() != __b.get(); }
1420 template<
typename _Tp, _Lock_policy _Lp>
1422 operator!=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1423 {
return (
bool)__a; }
1425 template<
typename _Tp, _Lock_policy _Lp>
1427 operator!=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1428 {
return (
bool)__a; }
1430 template<
typename _Tp,
typename _Up, _Lock_policy _Lp>
1432 operator<(const __shared_ptr<_Tp, _Lp>& __a,
1433 const __shared_ptr<_Up, _Lp>& __b) noexcept
1435 using _Tp_elt =
typename __shared_ptr<_Tp, _Lp>::element_type;
1436 using _Up_elt =
typename __shared_ptr<_Up, _Lp>::element_type;
1438 return less<_Vp>()(__a.get(), __b.get());
1441 template<
typename _Tp, _Lock_policy _Lp>
1443 operator<(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1445 using _Tp_elt =
typename __shared_ptr<_Tp, _Lp>::element_type;
1449 template<
typename _Tp, _Lock_policy _Lp>
1451 operator<(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1453 using _Tp_elt =
typename __shared_ptr<_Tp, _Lp>::element_type;
1457 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1459 operator<=(const __shared_ptr<_Tp1, _Lp>& __a,
1460 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1461 {
return !(__b < __a); }
1463 template<
typename _Tp, _Lock_policy _Lp>
1465 operator<=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1466 {
return !(
nullptr < __a); }
1468 template<
typename _Tp, _Lock_policy _Lp>
1470 operator<=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1471 {
return !(__a <
nullptr); }
1473 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1475 operator>(
const __shared_ptr<_Tp1, _Lp>& __a,
1476 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1477 {
return (__b < __a); }
1479 template<
typename _Tp, _Lock_policy _Lp>
1481 operator>(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1482 {
return nullptr < __a; }
1484 template<
typename _Tp, _Lock_policy _Lp>
1486 operator>(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1487 {
return __a <
nullptr; }
1489 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1491 operator>=(
const __shared_ptr<_Tp1, _Lp>& __a,
1492 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1493 {
return !(__a < __b); }
1495 template<
typename _Tp, _Lock_policy _Lp>
1497 operator>=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1498 {
return !(__a <
nullptr); }
1500 template<
typename _Tp, _Lock_policy _Lp>
1502 operator>=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1503 {
return !(
nullptr < __a); }
1505 template<
typename _Sp>
1509 operator()(
const _Sp& __lhs,
const _Sp& __rhs)
const noexcept
1511 typedef typename _Sp::element_type element_type;
1516 template<
typename _Tp, _Lock_policy _Lp>
1517 struct less<__shared_ptr<_Tp, _Lp>>
1518 :
public _Sp_less<__shared_ptr<_Tp, _Lp>>
1522 template<
typename _Tp, _Lock_policy _Lp>
1524 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b) noexcept
1534 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1535 inline __shared_ptr<_Tp, _Lp>
1536 static_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1538 using _Sp = __shared_ptr<_Tp, _Lp>;
1539 return _Sp(__r, static_cast<typename _Sp::element_type*>(__r.get()));
1547 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1548 inline __shared_ptr<_Tp, _Lp>
1549 const_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1551 using _Sp = __shared_ptr<_Tp, _Lp>;
1552 return _Sp(__r, const_cast<typename _Sp::element_type*>(__r.get()));
1560 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1561 inline __shared_ptr<_Tp, _Lp>
1562 dynamic_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1564 using _Sp = __shared_ptr<_Tp, _Lp>;
1565 if (
auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get()))
1566 return _Sp(__r, __p);
1570 #if __cplusplus > 201402L 1571 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1572 inline __shared_ptr<_Tp, _Lp>
1573 reinterpret_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1575 using _Sp = __shared_ptr<_Tp, _Lp>;
1576 return _Sp(__r, reinterpret_cast<typename _Sp::element_type*>(__r.get()));
1580 template<
typename _Tp, _Lock_policy _Lp>
1583 template<
typename _Yp,
typename _Res =
void>
1584 using _Compatible =
typename 1588 template<
typename _Yp>
1589 using _Assignable = _Compatible<_Yp, __weak_ptr&>;
1592 using element_type =
typename remove_extent<_Tp>::type;
1594 constexpr __weak_ptr() noexcept
1595 : _M_ptr(
nullptr), _M_refcount()
1598 __weak_ptr(
const __weak_ptr&) noexcept =
default;
1600 ~__weak_ptr() =
default;
1616 template<
typename _Yp,
typename = _Compatible<_Yp>>
1617 __weak_ptr(
const __weak_ptr<_Yp, _Lp>& __r) noexcept
1618 : _M_refcount(__r._M_refcount)
1619 { _M_ptr = __r.lock().get(); }
1621 template<
typename _Yp,
typename = _Compatible<_Yp>>
1622 __weak_ptr(
const __shared_ptr<_Yp, _Lp>& __r) noexcept
1623 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1626 __weak_ptr(__weak_ptr&& __r) noexcept
1627 : _M_ptr(__r._M_ptr), _M_refcount(std::move(__r._M_refcount))
1628 { __r._M_ptr =
nullptr; }
1630 template<
typename _Yp,
typename = _Compatible<_Yp>>
1631 __weak_ptr(__weak_ptr<_Yp, _Lp>&& __r) noexcept
1632 : _M_ptr(__r.lock().get()), _M_refcount(std::move(__r._M_refcount))
1633 { __r._M_ptr =
nullptr; }
1636 operator=(
const __weak_ptr& __r) noexcept =
default;
1638 template<
typename _Yp>
1640 operator=(
const __weak_ptr<_Yp, _Lp>& __r) noexcept
1642 _M_ptr = __r.lock().get();
1643 _M_refcount = __r._M_refcount;
1647 template<
typename _Yp>
1649 operator=(
const __shared_ptr<_Yp, _Lp>& __r) noexcept
1651 _M_ptr = __r._M_ptr;
1652 _M_refcount = __r._M_refcount;
1657 operator=(__weak_ptr&& __r) noexcept
1659 _M_ptr = __r._M_ptr;
1660 _M_refcount = std::move(__r._M_refcount);
1661 __r._M_ptr =
nullptr;
1665 template<
typename _Yp>
1667 operator=(__weak_ptr<_Yp, _Lp>&& __r) noexcept
1669 _M_ptr = __r.lock().get();
1670 _M_refcount = std::move(__r._M_refcount);
1671 __r._M_ptr =
nullptr;
1675 __shared_ptr<_Tp, _Lp>
1676 lock()
const noexcept
1677 {
return __shared_ptr<element_type, _Lp>(*
this, std::nothrow); }
1680 use_count()
const noexcept
1681 {
return _M_refcount._M_get_use_count(); }
1684 expired()
const noexcept
1685 {
return _M_refcount._M_get_use_count() == 0; }
1687 template<
typename _Tp1>
1689 owner_before(
const __shared_ptr<_Tp1, _Lp>& __rhs)
const noexcept
1690 {
return _M_refcount._M_less(__rhs._M_refcount); }
1692 template<
typename _Tp1>
1694 owner_before(
const __weak_ptr<_Tp1, _Lp>& __rhs)
const noexcept
1695 {
return _M_refcount._M_less(__rhs._M_refcount); }
1699 { __weak_ptr().swap(*
this); }
1702 swap(__weak_ptr& __s) noexcept
1704 std::swap(_M_ptr, __s._M_ptr);
1705 _M_refcount._M_swap(__s._M_refcount);
1711 _M_assign(_Tp* __ptr,
const __shared_count<_Lp>& __refcount) noexcept
1713 if (use_count() == 0)
1716 _M_refcount = __refcount;
1720 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
1721 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
1722 friend class __enable_shared_from_this<_Tp, _Lp>;
1725 element_type* _M_ptr;
1726 __weak_count<_Lp> _M_refcount;
1730 template<
typename _Tp, _Lock_policy _Lp>
1732 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b) noexcept
1735 template<
typename _Tp,
typename _Tp1>
1739 operator()(
const _Tp& __lhs,
const _Tp& __rhs)
const noexcept
1740 {
return __lhs.owner_before(__rhs); }
1743 operator()(
const _Tp& __lhs,
const _Tp1& __rhs)
const noexcept
1744 {
return __lhs.owner_before(__rhs); }
1747 operator()(
const _Tp1& __lhs,
const _Tp& __rhs)
const noexcept
1748 {
return __lhs.owner_before(__rhs); }
1752 struct _Sp_owner_less<void, void>
1754 template<
typename _Tp,
typename _Up>
1756 operator()(
const _Tp& __lhs,
const _Up& __rhs)
const noexcept
1757 -> decltype(__lhs.owner_before(__rhs))
1758 {
return __lhs.owner_before(__rhs); }
1760 using is_transparent = void;
1763 template<
typename _Tp, _Lock_policy _Lp>
1765 :
public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
1768 template<
typename _Tp, _Lock_policy _Lp>
1770 :
public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
1774 template<
typename _Tp, _Lock_policy _Lp>
1775 class __enable_shared_from_this
1778 constexpr __enable_shared_from_this() noexcept { }
1780 __enable_shared_from_this(
const __enable_shared_from_this&) noexcept { }
1782 __enable_shared_from_this&
1783 operator=(
const __enable_shared_from_this&) noexcept
1786 ~__enable_shared_from_this() { }
1789 __shared_ptr<_Tp, _Lp>
1791 {
return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
1793 __shared_ptr<const _Tp, _Lp>
1794 shared_from_this()
const 1795 {
return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
1797 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 1798 __weak_ptr<_Tp, _Lp>
1799 weak_from_this() noexcept
1800 {
return this->_M_weak_this; }
1802 __weak_ptr<const _Tp, _Lp>
1803 weak_from_this()
const noexcept
1804 {
return this->_M_weak_this; }
1808 template<
typename _Tp1>
1810 _M_weak_assign(_Tp1* __p,
const __shared_count<_Lp>& __n)
const noexcept
1811 { _M_weak_this._M_assign(__p, __n); }
1813 friend const __enable_shared_from_this*
1814 __enable_shared_from_this_base(
const __shared_count<_Lp>&,
1815 const __enable_shared_from_this* __p)
1818 template<
typename, _Lock_policy>
1819 friend class __shared_ptr;
1821 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
1824 template<
typename _Tp, _Lock_policy _Lp,
typename _Alloc,
typename... _Args>
1825 inline __shared_ptr<_Tp, _Lp>
1826 __allocate_shared(
const _Alloc& __a, _Args&&... __args)
1828 return __shared_ptr<_Tp, _Lp>(_Sp_make_shared_tag(), __a,
1829 std::forward<_Args>(__args)...);
1832 template<
typename _Tp, _Lock_policy _Lp,
typename... _Args>
1833 inline __shared_ptr<_Tp, _Lp>
1834 __make_shared(_Args&&... __args)
1836 typedef typename std::remove_const<_Tp>::type _Tp_nc;
1838 std::forward<_Args>(__args)...);
1842 template<
typename _Tp, _Lock_policy _Lp>
1843 struct hash<__shared_ptr<_Tp, _Lp>>
1844 :
public __hash_base<size_t, __shared_ptr<_Tp, _Lp>>
1847 operator()(
const __shared_ptr<_Tp, _Lp>& __s)
const noexcept
1854 _GLIBCXX_END_NAMESPACE_VERSION
1857 #endif // _SHARED_PTR_BASE_H allocator<void> specialization.
A smart pointer with weak semantics.
void lock(_L1 &__l1, _L2 &__l2, _L3 &... __l3)
Generic lock.
One of the comparison functors.
The standard allocator, as per [20.4].
Base class allowing use of member function shared_from_this.
Primary class template hash.
Define a member typedef type to one of two argument types.
Uniform interface to all allocator types.
static void destroy(_Alloc &__a, _Tp *__p)
Destroy an object of type _Tp.
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Primary class template for reference_wrapper.
__allocated_ptr< _Alloc > __allocate_guarded(_Alloc &__a)
Allocate space for a single object using __a.
20.7.1.2 unique_ptr for single objects.
ISO C++ entities toplevel namespace is std.
A simple smart pointer providing strict ownership semantics.
Define a member typedef type only if a boolean constant is true.
Non-standard RAII type for managing pointers obtained from allocators.
Exception possibly thrown by shared_ptr.
static auto construct(_Alloc &__a, _Tp *__p, _Args &&... __args) -> decltype(_S_construct(__a, __p, std::forward< _Args >(__args)...))
Construct an object of type _Tp.
Primary template owner_less.
A smart pointer with reference-counted copy semantics.
Base class for all library exceptions.
virtual char const * what() const noexcept
complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.