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
504 template<
typename _Tp, _Lock_policy _Lp>
505 friend class __shared_ptr;
506 template<
typename _Tp,
typename _Alloc, _Lock_policy _Lp>
507 friend class _Sp_counted_ptr_inplace;
510 _S_ti() noexcept _GLIBCXX_VISIBILITY(
default)
513 return reinterpret_cast<const type_info&
>(__tag);
517 template<
typename _Tp,
typename _Alloc, _Lock_policy _Lp>
518 class _Sp_counted_ptr_inplace final :
public _Sp_counted_base<_Lp>
520 class _Impl : _Sp_ebo_helper<0, _Alloc>
522 typedef _Sp_ebo_helper<0, _Alloc> _A_base;
525 explicit _Impl(_Alloc __a) noexcept : _A_base(__a) { }
527 _Alloc& _M_alloc() noexcept {
return _A_base::_S_get(*
this); }
529 __gnu_cxx::__aligned_buffer<_Tp> _M_storage;
533 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>;
536 template<
typename... _Args>
537 _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
543 std::forward<_Args>(__args)...);
546 ~_Sp_counted_ptr_inplace() noexcept { }
549 _M_dispose() noexcept
556 _M_destroy() noexcept
558 __allocator_type __a(_M_impl._M_alloc());
560 this->~_Sp_counted_ptr_inplace();
569 if (&__ti == &_Sp_make_shared_tag::_S_ti())
570 return const_cast<typename remove_cv<_Tp>::type*
>(_M_ptr());
574 else if (__ti ==
typeid(_Sp_make_shared_tag))
575 return const_cast<typename remove_cv<_Tp>::type*
>(_M_ptr());
581 _Tp* _M_ptr() noexcept {
return _M_impl._M_storage._M_ptr(); }
587 struct __sp_array_delete
589 template<
typename _Yp>
590 void operator()(_Yp* __p)
const {
delete[] __p; }
593 template<_Lock_policy _Lp>
597 constexpr __shared_count() noexcept : _M_pi(0)
600 template<
typename _Ptr>
602 __shared_count(_Ptr __p) : _M_pi(0)
606 _M_pi =
new _Sp_counted_ptr<_Ptr, _Lp>(__p);
611 __throw_exception_again;
615 template<
typename _Ptr>
617 : __shared_count(__p)
620 template<
typename _Ptr>
625 template<
typename _Ptr,
typename _Deleter>
626 __shared_count(_Ptr __p, _Deleter __d)
630 template<
typename _Ptr,
typename _Deleter,
typename _Alloc>
631 __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
633 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
636 typename _Sp_cd_type::__allocator_type __a2(__a);
638 _Sp_cd_type* __mem = __guard.get();
639 ::new (__mem) _Sp_cd_type(__p, std::move(__d), std::move(__a));
646 __throw_exception_again;
650 template<
typename _Tp,
typename _Alloc,
typename... _Args>
651 __shared_count(_Sp_make_shared_tag, _Tp*,
const _Alloc& __a,
655 typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type;
656 typename _Sp_cp_type::__allocator_type __a2(__a);
658 _Sp_cp_type* __mem = __guard.get();
659 ::new (__mem) _Sp_cp_type(__a, std::forward<_Args>(__args)...);
664 #if _GLIBCXX_USE_DEPRECATED 665 #pragma GCC diagnostic push 666 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 668 template<
typename _Tp>
671 #pragma GCC diagnostic pop 675 template<
typename _Tp,
typename _Del>
681 if (__r.get() ==
nullptr)
684 using _Ptr =
typename unique_ptr<_Tp, _Del>::pointer;
689 = _Sp_counted_deleter<_Ptr, _Del2, allocator<void>, _Lp>;
693 _Sp_cd_type* __mem = _Alloc_traits::allocate(__a, 1);
694 _Alloc_traits::construct(__a, __mem, __r.release(),
700 explicit __shared_count(
const __weak_count<_Lp>& __r);
703 explicit __shared_count(
const __weak_count<_Lp>& __r, std::nothrow_t);
705 ~__shared_count() noexcept
707 if (_M_pi !=
nullptr)
711 __shared_count(
const __shared_count& __r) noexcept
715 _M_pi->_M_add_ref_copy();
719 operator=(
const __shared_count& __r) noexcept
721 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
725 __tmp->_M_add_ref_copy();
734 _M_swap(__shared_count& __r) noexcept
736 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
742 _M_get_use_count()
const noexcept
743 {
return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
746 _M_unique()
const noexcept
747 {
return this->_M_get_use_count() == 1; }
751 {
return _M_pi ? _M_pi->_M_get_deleter(__ti) :
nullptr; }
754 _M_less(
const __shared_count& __rhs)
const noexcept
758 _M_less(
const __weak_count<_Lp>& __rhs)
const noexcept
763 operator==(
const __shared_count& __a,
const __shared_count& __b) noexcept
764 {
return __a._M_pi == __b._M_pi; }
767 friend class __weak_count<_Lp>;
769 _Sp_counted_base<_Lp>* _M_pi;
773 template<_Lock_policy _Lp>
777 constexpr __weak_count() noexcept : _M_pi(
nullptr)
780 __weak_count(
const __shared_count<_Lp>& __r) noexcept
783 if (_M_pi !=
nullptr)
784 _M_pi->_M_weak_add_ref();
787 __weak_count(
const __weak_count& __r) noexcept
790 if (_M_pi !=
nullptr)
791 _M_pi->_M_weak_add_ref();
794 __weak_count(__weak_count&& __r) noexcept
796 { __r._M_pi =
nullptr; }
798 ~__weak_count() noexcept
800 if (_M_pi !=
nullptr)
801 _M_pi->_M_weak_release();
805 operator=(
const __shared_count<_Lp>& __r) noexcept
807 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
808 if (__tmp !=
nullptr)
809 __tmp->_M_weak_add_ref();
810 if (_M_pi !=
nullptr)
811 _M_pi->_M_weak_release();
817 operator=(
const __weak_count& __r) noexcept
819 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
820 if (__tmp !=
nullptr)
821 __tmp->_M_weak_add_ref();
822 if (_M_pi !=
nullptr)
823 _M_pi->_M_weak_release();
829 operator=(__weak_count&& __r) noexcept
831 if (_M_pi !=
nullptr)
832 _M_pi->_M_weak_release();
839 _M_swap(__weak_count& __r) noexcept
841 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
847 _M_get_use_count()
const noexcept
848 {
return _M_pi !=
nullptr ? _M_pi->_M_get_use_count() : 0; }
851 _M_less(
const __weak_count& __rhs)
const noexcept
855 _M_less(
const __shared_count<_Lp>& __rhs)
const noexcept
860 operator==(
const __weak_count& __a,
const __weak_count& __b) noexcept
861 {
return __a._M_pi == __b._M_pi; }
864 friend class __shared_count<_Lp>;
866 _Sp_counted_base<_Lp>* _M_pi;
870 template<_Lock_policy _Lp>
872 __shared_count<_Lp>::__shared_count(
const __weak_count<_Lp>& __r)
875 if (_M_pi !=
nullptr)
876 _M_pi->_M_add_ref_lock();
878 __throw_bad_weak_ptr();
882 template<_Lock_policy _Lp>
884 __shared_count<_Lp>::
885 __shared_count(
const __weak_count<_Lp>& __r, std::nothrow_t)
888 if (_M_pi !=
nullptr)
889 if (!_M_pi->_M_add_ref_lock_nothrow())
893 #define __cpp_lib_shared_ptr_arrays 201603 899 template<
typename _Yp_ptr,
typename _Tp_ptr>
900 struct __sp_compatible_with
904 template<
typename _Yp,
typename _Tp>
905 struct __sp_compatible_with<_Yp*, _Tp*>
909 template<
typename _Up,
size_t _Nm>
910 struct __sp_compatible_with<_Up(*)[_Nm], _Up(*)[]>
914 template<
typename _Up,
size_t _Nm>
915 struct __sp_compatible_with<_Up(*)[_Nm],
const _Up(*)[]>
919 template<
typename _Up,
size_t _Nm>
920 struct __sp_compatible_with<_Up(*)[_Nm],
volatile _Up(*)[]>
924 template<
typename _Up,
size_t _Nm>
925 struct __sp_compatible_with<_Up(*)[_Nm],
const volatile _Up(*)[]>
930 template<
typename _Up,
size_t _Nm,
typename _Yp,
typename =
void>
931 struct __sp_is_constructible_arrN
935 template<
typename _Up,
size_t _Nm,
typename _Yp>
936 struct __sp_is_constructible_arrN<_Up, _Nm, _Yp, __void_t<_Yp[_Nm]>>
941 template<
typename _Up,
typename _Yp,
typename =
void>
942 struct __sp_is_constructible_arr
946 template<
typename _Up,
typename _Yp>
947 struct __sp_is_constructible_arr<_Up, _Yp, __void_t<_Yp[]>>
952 template<
typename _Tp,
typename _Yp>
953 struct __sp_is_constructible;
956 template<
typename _Up,
size_t _Nm,
typename _Yp>
957 struct __sp_is_constructible<_Up[_Nm], _Yp>
958 : __sp_is_constructible_arrN<_Up, _Nm, _Yp>::type
962 template<
typename _Up,
typename _Yp>
963 struct __sp_is_constructible<_Up[], _Yp>
964 : __sp_is_constructible_arr<_Up, _Yp>::type
968 template<
typename _Tp,
typename _Yp>
969 struct __sp_is_constructible
975 template<
typename _Tp, _Lock_policy _Lp,
977 class __shared_ptr_access
980 using element_type = _Tp;
985 __glibcxx_assert(_M_get() !=
nullptr);
990 operator->()
const noexcept
992 _GLIBCXX_DEBUG_PEDASSERT(_M_get() !=
nullptr);
998 _M_get()
const noexcept
999 {
return static_cast<const __shared_ptr<_Tp, _Lp>*
>(
this)->
get(); }
1003 template<
typename _Tp, _Lock_policy _Lp>
1004 class __shared_ptr_access<_Tp, _Lp, false, true>
1007 using element_type = _Tp;
1010 operator->()
const noexcept
1012 auto __ptr =
static_cast<const __shared_ptr<_Tp, _Lp>*
>(
this)->
get();
1013 _GLIBCXX_DEBUG_PEDASSERT(__ptr !=
nullptr);
1019 template<
typename _Tp, _Lock_policy _Lp>
1020 class __shared_ptr_access<_Tp, _Lp, true, false>
1023 using element_type =
typename remove_extent<_Tp>::type;
1025 #if __cplusplus <= 201402L 1026 [[__deprecated__(
"shared_ptr<T[]>::operator* is absent from C++17")]]
1030 __glibcxx_assert(_M_get() !=
nullptr);
1034 [[__deprecated__(
"shared_ptr<T[]>::operator-> is absent from C++17")]]
1036 operator->()
const noexcept
1038 _GLIBCXX_DEBUG_PEDASSERT(_M_get() !=
nullptr);
1044 operator[](ptrdiff_t __i)
const 1046 __glibcxx_assert(_M_get() !=
nullptr);
1048 return _M_get()[__i];
1053 _M_get()
const noexcept
1054 {
return static_cast<const __shared_ptr<_Tp, _Lp>*
>(
this)->
get(); }
1057 template<
typename _Tp, _Lock_policy _Lp>
1059 :
public __shared_ptr_access<_Tp, _Lp>
1062 using element_type =
typename remove_extent<_Tp>::type;
1066 template<
typename _Yp>
1071 template<
typename _Yp,
typename _Res =
void>
1072 using _Compatible =
typename 1076 template<
typename _Yp>
1077 using _Assignable = _Compatible<_Yp, __shared_ptr&>;
1080 template<
typename _Yp,
typename _Del,
typename _Res = void,
1081 typename _Ptr =
typename unique_ptr<_Yp, _Del>::pointer>
1082 using _UniqCompatible =
typename enable_if<__and_<
1084 >::value, _Res>::type;
1087 template<
typename _Yp,
typename _Del>
1088 using _UniqAssignable = _UniqCompatible<_Yp, _Del, __shared_ptr&>;
1092 #if __cplusplus > 201402L 1093 using weak_type = __weak_ptr<_Tp, _Lp>;
1096 constexpr __shared_ptr() noexcept
1097 : _M_ptr(0), _M_refcount()
1100 template<
typename _Yp,
typename = _SafeConv<_Yp>>
1102 __shared_ptr(_Yp* __p)
1106 static_assert(
sizeof(_Yp) > 0,
"incomplete type" );
1107 _M_enable_shared_from_this_with(__p);
1110 template<
typename _Yp,
typename _Deleter,
typename = _SafeConv<_Yp>>
1111 __shared_ptr(_Yp* __p, _Deleter __d)
1112 : _M_ptr(__p), _M_refcount(__p, std::move(__d))
1114 static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
1115 "deleter expression d(p) is well-formed");
1116 _M_enable_shared_from_this_with(__p);
1119 template<
typename _Yp,
typename _Deleter,
typename _Alloc,
1120 typename = _SafeConv<_Yp>>
1121 __shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a)
1122 : _M_ptr(__p), _M_refcount(__p, std::move(__d), std::move(__a))
1124 static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
1125 "deleter expression d(p) is well-formed");
1126 _M_enable_shared_from_this_with(__p);
1129 template<
typename _Deleter>
1130 __shared_ptr(nullptr_t __p, _Deleter __d)
1131 : _M_ptr(0), _M_refcount(__p, std::move(__d))
1134 template<
typename _Deleter,
typename _Alloc>
1135 __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
1136 : _M_ptr(0), _M_refcount(__p, std::move(__d), std::move(__a))
1139 template<
typename _Yp>
1140 __shared_ptr(
const __shared_ptr<_Yp, _Lp>& __r,
1141 element_type* __p) noexcept
1142 : _M_ptr(__p), _M_refcount(__r._M_refcount)
1145 __shared_ptr(
const __shared_ptr&) noexcept =
default;
1146 __shared_ptr& operator=(
const __shared_ptr&) noexcept =
default;
1147 ~__shared_ptr() =
default;
1149 template<
typename _Yp,
typename = _Compatible<_Yp>>
1150 __shared_ptr(
const __shared_ptr<_Yp, _Lp>& __r) noexcept
1151 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1154 __shared_ptr(__shared_ptr&& __r) noexcept
1155 : _M_ptr(__r._M_ptr), _M_refcount()
1157 _M_refcount._M_swap(__r._M_refcount);
1161 template<
typename _Yp,
typename = _Compatible<_Yp>>
1162 __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r) noexcept
1163 : _M_ptr(__r._M_ptr), _M_refcount()
1165 _M_refcount._M_swap(__r._M_refcount);
1169 template<
typename _Yp,
typename = _Compatible<_Yp>>
1170 explicit __shared_ptr(
const __weak_ptr<_Yp, _Lp>& __r)
1171 : _M_refcount(__r._M_refcount)
1175 _M_ptr = __r._M_ptr;
1179 template<
typename _Yp,
typename _Del,
1180 typename = _UniqCompatible<_Yp, _Del>>
1182 : _M_ptr(__r.get()), _M_refcount()
1184 auto __raw = __to_address(__r.get());
1185 _M_refcount = __shared_count<_Lp>(std::move(__r));
1186 _M_enable_shared_from_this_with(__raw);
1189 #if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED 1192 template<
typename _Tp1,
typename _Del,
1196 >::value,
bool>::type =
true>
1198 : _M_ptr(__r.get()), _M_refcount()
1200 auto __raw = __to_address(__r.get());
1201 _M_refcount = __shared_count<_Lp>(std::move(__r));
1202 _M_enable_shared_from_this_with(__raw);
1207 #if _GLIBCXX_USE_DEPRECATED 1208 #pragma GCC diagnostic push 1209 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 1211 template<
typename _Yp,
typename = _Compatible<_Yp>>
1213 #pragma GCC diagnostic pop 1216 constexpr __shared_ptr(nullptr_t) noexcept : __shared_ptr() { }
1218 template<
typename _Yp>
1220 operator=(
const __shared_ptr<_Yp, _Lp>& __r) noexcept
1222 _M_ptr = __r._M_ptr;
1223 _M_refcount = __r._M_refcount;
1227 #if _GLIBCXX_USE_DEPRECATED 1228 #pragma GCC diagnostic push 1229 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 1230 template<
typename _Yp>
1234 __shared_ptr(std::move(__r)).swap(*
this);
1237 #pragma GCC diagnostic pop 1241 operator=(__shared_ptr&& __r) noexcept
1243 __shared_ptr(std::move(__r)).swap(*
this);
1249 operator=(__shared_ptr<_Yp, _Lp>&& __r) noexcept
1251 __shared_ptr(std::move(__r)).swap(*
this);
1255 template<
typename _Yp,
typename _Del>
1256 _UniqAssignable<_Yp, _Del>
1259 __shared_ptr(std::move(__r)).swap(*
this);
1265 { __shared_ptr().swap(*
this); }
1267 template<
typename _Yp>
1272 __glibcxx_assert(__p == 0 || __p != _M_ptr);
1273 __shared_ptr(__p).swap(*
this);
1276 template<
typename _Yp,
typename _Deleter>
1278 reset(_Yp* __p, _Deleter __d)
1279 { __shared_ptr(__p, std::move(__d)).swap(*
this); }
1281 template<
typename _Yp,
typename _Deleter,
typename _Alloc>
1283 reset(_Yp* __p, _Deleter __d, _Alloc __a)
1284 { __shared_ptr(__p, std::move(__d), std::move(__a)).swap(*
this); }
1287 get()
const noexcept
1290 explicit operator bool()
const 1291 {
return _M_ptr == 0 ? false :
true; }
1294 unique()
const noexcept
1295 {
return _M_refcount._M_unique(); }
1298 use_count()
const noexcept
1299 {
return _M_refcount._M_get_use_count(); }
1302 swap(__shared_ptr<_Tp, _Lp>& __other) noexcept
1304 std::swap(_M_ptr, __other._M_ptr);
1305 _M_refcount._M_swap(__other._M_refcount);
1308 template<
typename _Tp1>
1310 owner_before(__shared_ptr<_Tp1, _Lp>
const& __rhs)
const noexcept
1311 {
return _M_refcount._M_less(__rhs._M_refcount); }
1313 template<
typename _Tp1>
1315 owner_before(__weak_ptr<_Tp1, _Lp>
const& __rhs)
const noexcept
1316 {
return _M_refcount._M_less(__rhs._M_refcount); }
1320 template<
typename _Alloc,
typename... _Args>
1321 __shared_ptr(_Sp_make_shared_tag __tag,
const _Alloc& __a,
1323 : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a,
1324 std::forward<_Args>(__args)...)
1328 void* __p = _M_refcount._M_get_deleter(_Sp_make_shared_tag::_S_ti());
1329 _M_ptr =
static_cast<_Tp*
>(__p);
1330 _M_enable_shared_from_this_with(_M_ptr);
1333 template<
typename _Tp1, _Lock_policy _Lp1,
typename _Alloc,
1335 friend __shared_ptr<_Tp1, _Lp1>
1336 __allocate_shared(
const _Alloc& __a, _Args&&... __args);
1340 __shared_ptr(
const __weak_ptr<_Tp, _Lp>& __r, std::nothrow_t)
1341 : _M_refcount(__r._M_refcount, std::nothrow)
1343 _M_ptr = _M_refcount._M_get_use_count() ? __r._M_ptr :
nullptr;
1346 friend class __weak_ptr<_Tp, _Lp>;
1350 template<
typename _Yp>
1351 using __esft_base_t = decltype(__enable_shared_from_this_base(
1352 std::declval<
const __shared_count<_Lp>&>(),
1353 std::declval<_Yp*>()));
1356 template<
typename _Yp,
typename =
void>
1357 struct __has_esft_base
1360 template<
typename _Yp>
1361 struct __has_esft_base<_Yp, __void_t<__esft_base_t<_Yp>>>
1362 : __not_<is_array<_Tp>> { };
1364 template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
1366 _M_enable_shared_from_this_with(_Yp* __p) noexcept
1368 if (
auto __base = __enable_shared_from_this_base(_M_refcount, __p))
1369 __base->_M_weak_assign(const_cast<_Yp2*>(__p), _M_refcount);
1372 template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
1374 _M_enable_shared_from_this_with(_Yp*) noexcept
1379 {
return _M_refcount._M_get_deleter(__ti); }
1381 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
1382 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
1384 template<
typename _Del,
typename _Tp1, _Lock_policy _Lp1>
1385 friend _Del* get_deleter(
const __shared_ptr<_Tp1, _Lp1>&) noexcept;
1387 template<
typename _Del,
typename _Tp1>
1390 element_type* _M_ptr;
1391 __shared_count<_Lp> _M_refcount;
1396 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1398 operator==(
const __shared_ptr<_Tp1, _Lp>& __a,
1399 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1400 {
return __a.get() == __b.get(); }
1402 template<
typename _Tp, _Lock_policy _Lp>
1404 operator==(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1407 template<
typename _Tp, _Lock_policy _Lp>
1409 operator==(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1412 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1414 operator!=(
const __shared_ptr<_Tp1, _Lp>& __a,
1415 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1416 {
return __a.get() != __b.get(); }
1418 template<
typename _Tp, _Lock_policy _Lp>
1420 operator!=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1421 {
return (
bool)__a; }
1423 template<
typename _Tp, _Lock_policy _Lp>
1425 operator!=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1426 {
return (
bool)__a; }
1428 template<
typename _Tp,
typename _Up, _Lock_policy _Lp>
1430 operator<(const __shared_ptr<_Tp, _Lp>& __a,
1431 const __shared_ptr<_Up, _Lp>& __b) noexcept
1433 using _Tp_elt =
typename __shared_ptr<_Tp, _Lp>::element_type;
1434 using _Up_elt =
typename __shared_ptr<_Up, _Lp>::element_type;
1436 return less<_Vp>()(__a.get(), __b.get());
1439 template<
typename _Tp, _Lock_policy _Lp>
1441 operator<(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1443 using _Tp_elt =
typename __shared_ptr<_Tp, _Lp>::element_type;
1447 template<
typename _Tp, _Lock_policy _Lp>
1449 operator<(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1451 using _Tp_elt =
typename __shared_ptr<_Tp, _Lp>::element_type;
1455 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1457 operator<=(const __shared_ptr<_Tp1, _Lp>& __a,
1458 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1459 {
return !(__b < __a); }
1461 template<
typename _Tp, _Lock_policy _Lp>
1463 operator<=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1464 {
return !(
nullptr < __a); }
1466 template<
typename _Tp, _Lock_policy _Lp>
1468 operator<=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1469 {
return !(__a <
nullptr); }
1471 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1473 operator>(
const __shared_ptr<_Tp1, _Lp>& __a,
1474 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1475 {
return (__b < __a); }
1477 template<
typename _Tp, _Lock_policy _Lp>
1479 operator>(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1480 {
return nullptr < __a; }
1482 template<
typename _Tp, _Lock_policy _Lp>
1484 operator>(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1485 {
return __a <
nullptr; }
1487 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1489 operator>=(
const __shared_ptr<_Tp1, _Lp>& __a,
1490 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1491 {
return !(__a < __b); }
1493 template<
typename _Tp, _Lock_policy _Lp>
1495 operator>=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1496 {
return !(__a <
nullptr); }
1498 template<
typename _Tp, _Lock_policy _Lp>
1500 operator>=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1501 {
return !(
nullptr < __a); }
1504 template<
typename _Tp, _Lock_policy _Lp>
1506 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b) noexcept
1516 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1517 inline __shared_ptr<_Tp, _Lp>
1520 using _Sp = __shared_ptr<_Tp, _Lp>;
1521 return _Sp(__r, static_cast<typename _Sp::element_type*>(__r.get()));
1529 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1530 inline __shared_ptr<_Tp, _Lp>
1533 using _Sp = __shared_ptr<_Tp, _Lp>;
1534 return _Sp(__r, const_cast<typename _Sp::element_type*>(__r.get()));
1542 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1543 inline __shared_ptr<_Tp, _Lp>
1546 using _Sp = __shared_ptr<_Tp, _Lp>;
1547 if (
auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get()))
1548 return _Sp(__r, __p);
1552 #if __cplusplus > 201402L 1553 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1554 inline __shared_ptr<_Tp, _Lp>
1555 reinterpret_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1557 using _Sp = __shared_ptr<_Tp, _Lp>;
1558 return _Sp(__r, reinterpret_cast<typename _Sp::element_type*>(__r.get()));
1562 template<
typename _Tp, _Lock_policy _Lp>
1565 template<
typename _Yp,
typename _Res =
void>
1566 using _Compatible =
typename 1570 template<
typename _Yp>
1571 using _Assignable = _Compatible<_Yp, __weak_ptr&>;
1574 using element_type =
typename remove_extent<_Tp>::type;
1576 constexpr __weak_ptr() noexcept
1577 : _M_ptr(
nullptr), _M_refcount()
1580 __weak_ptr(
const __weak_ptr&) noexcept =
default;
1582 ~__weak_ptr() =
default;
1598 template<
typename _Yp,
typename = _Compatible<_Yp>>
1599 __weak_ptr(
const __weak_ptr<_Yp, _Lp>& __r) noexcept
1600 : _M_refcount(__r._M_refcount)
1601 { _M_ptr = __r.lock().get(); }
1603 template<
typename _Yp,
typename = _Compatible<_Yp>>
1604 __weak_ptr(
const __shared_ptr<_Yp, _Lp>& __r) noexcept
1605 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1608 __weak_ptr(__weak_ptr&& __r) noexcept
1609 : _M_ptr(__r._M_ptr), _M_refcount(std::move(__r._M_refcount))
1610 { __r._M_ptr =
nullptr; }
1612 template<
typename _Yp,
typename = _Compatible<_Yp>>
1613 __weak_ptr(__weak_ptr<_Yp, _Lp>&& __r) noexcept
1614 : _M_ptr(__r.lock().get()), _M_refcount(std::move(__r._M_refcount))
1615 { __r._M_ptr =
nullptr; }
1618 operator=(
const __weak_ptr& __r) noexcept =
default;
1620 template<
typename _Yp>
1622 operator=(
const __weak_ptr<_Yp, _Lp>& __r) noexcept
1624 _M_ptr = __r.lock().get();
1625 _M_refcount = __r._M_refcount;
1629 template<
typename _Yp>
1631 operator=(
const __shared_ptr<_Yp, _Lp>& __r) noexcept
1633 _M_ptr = __r._M_ptr;
1634 _M_refcount = __r._M_refcount;
1639 operator=(__weak_ptr&& __r) noexcept
1641 _M_ptr = __r._M_ptr;
1642 _M_refcount = std::move(__r._M_refcount);
1643 __r._M_ptr =
nullptr;
1647 template<
typename _Yp>
1649 operator=(__weak_ptr<_Yp, _Lp>&& __r) noexcept
1651 _M_ptr = __r.lock().get();
1652 _M_refcount = std::move(__r._M_refcount);
1653 __r._M_ptr =
nullptr;
1657 __shared_ptr<_Tp, _Lp>
1658 lock()
const noexcept
1659 {
return __shared_ptr<element_type, _Lp>(*
this, std::nothrow); }
1662 use_count()
const noexcept
1663 {
return _M_refcount._M_get_use_count(); }
1666 expired()
const noexcept
1667 {
return _M_refcount._M_get_use_count() == 0; }
1669 template<
typename _Tp1>
1671 owner_before(
const __shared_ptr<_Tp1, _Lp>& __rhs)
const noexcept
1672 {
return _M_refcount._M_less(__rhs._M_refcount); }
1674 template<
typename _Tp1>
1676 owner_before(
const __weak_ptr<_Tp1, _Lp>& __rhs)
const noexcept
1677 {
return _M_refcount._M_less(__rhs._M_refcount); }
1681 { __weak_ptr().swap(*
this); }
1684 swap(__weak_ptr& __s) noexcept
1686 std::swap(_M_ptr, __s._M_ptr);
1687 _M_refcount._M_swap(__s._M_refcount);
1693 _M_assign(_Tp* __ptr,
const __shared_count<_Lp>& __refcount) noexcept
1695 if (use_count() == 0)
1698 _M_refcount = __refcount;
1702 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
1703 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
1704 friend class __enable_shared_from_this<_Tp, _Lp>;
1707 element_type* _M_ptr;
1708 __weak_count<_Lp> _M_refcount;
1712 template<
typename _Tp, _Lock_policy _Lp>
1714 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b) noexcept
1717 template<
typename _Tp,
typename _Tp1>
1721 operator()(
const _Tp& __lhs,
const _Tp& __rhs)
const noexcept
1722 {
return __lhs.owner_before(__rhs); }
1725 operator()(
const _Tp& __lhs,
const _Tp1& __rhs)
const noexcept
1726 {
return __lhs.owner_before(__rhs); }
1729 operator()(
const _Tp1& __lhs,
const _Tp& __rhs)
const noexcept
1730 {
return __lhs.owner_before(__rhs); }
1734 struct _Sp_owner_less<void, void>
1736 template<
typename _Tp,
typename _Up>
1738 operator()(
const _Tp& __lhs,
const _Up& __rhs)
const noexcept
1739 -> decltype(__lhs.owner_before(__rhs))
1740 {
return __lhs.owner_before(__rhs); }
1742 using is_transparent = void;
1745 template<
typename _Tp, _Lock_policy _Lp>
1747 :
public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
1750 template<
typename _Tp, _Lock_policy _Lp>
1752 :
public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
1756 template<
typename _Tp, _Lock_policy _Lp>
1757 class __enable_shared_from_this
1760 constexpr __enable_shared_from_this() noexcept { }
1762 __enable_shared_from_this(
const __enable_shared_from_this&) noexcept { }
1764 __enable_shared_from_this&
1765 operator=(
const __enable_shared_from_this&) noexcept
1768 ~__enable_shared_from_this() { }
1771 __shared_ptr<_Tp, _Lp>
1773 {
return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
1775 __shared_ptr<const _Tp, _Lp>
1776 shared_from_this()
const 1777 {
return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
1779 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 1780 __weak_ptr<_Tp, _Lp>
1781 weak_from_this() noexcept
1782 {
return this->_M_weak_this; }
1784 __weak_ptr<const _Tp, _Lp>
1785 weak_from_this()
const noexcept
1786 {
return this->_M_weak_this; }
1790 template<
typename _Tp1>
1792 _M_weak_assign(_Tp1* __p,
const __shared_count<_Lp>& __n)
const noexcept
1793 { _M_weak_this._M_assign(__p, __n); }
1795 friend const __enable_shared_from_this*
1796 __enable_shared_from_this_base(
const __shared_count<_Lp>&,
1797 const __enable_shared_from_this* __p)
1800 template<
typename, _Lock_policy>
1801 friend class __shared_ptr;
1803 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
1806 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy,
1807 typename _Alloc,
typename... _Args>
1808 inline __shared_ptr<_Tp, _Lp>
1809 __allocate_shared(
const _Alloc& __a, _Args&&... __args)
1811 return __shared_ptr<_Tp, _Lp>(_Sp_make_shared_tag(), __a,
1812 std::forward<_Args>(__args)...);
1815 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy,
1817 inline __shared_ptr<_Tp, _Lp>
1818 __make_shared(_Args&&... __args)
1820 typedef typename std::remove_const<_Tp>::type _Tp_nc;
1822 std::forward<_Args>(__args)...);
1826 template<
typename _Tp, _Lock_policy _Lp>
1827 struct hash<__shared_ptr<_Tp, _Lp>>
1828 :
public __hash_base<size_t, __shared_ptr<_Tp, _Lp>>
1831 operator()(
const __shared_ptr<_Tp, _Lp>& __s)
const noexcept
1838 _GLIBCXX_END_NAMESPACE_VERSION
1841 #endif // _SHARED_PTR_BASE_H allocator<void> specialization.
Base class for all library exceptions.
Define a member typedef type only if a boolean constant is true.
Uniform interface to all allocator types.
Primary template owner_less.
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Primary class template hash.
__shared_ptr< _Tp, _Lp > dynamic_pointer_cast(const __shared_ptr< _Tp1, _Lp > &__r) noexcept
dynamic_pointer_cast
ISO C++ entities toplevel namespace is std.
One of the comparison functors.
The standard allocator, as per [20.4].
20.7.1.2 unique_ptr for single objects.
Non-standard RAII type for managing pointers obtained from allocators.
A smart pointer with weak semantics.
Define a member typedef type to one of two argument types.
Primary class template for reference_wrapper.
virtual char const * what() const noexcept
_GLIBCXX20_CONSTEXPR complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
void lock(_L1 &__l1, _L2 &__l2, _L3 &... __l3)
Generic lock.
__shared_ptr< _Tp, _Lp > const_pointer_cast(const __shared_ptr< _Tp1, _Lp > &__r) noexcept
const_pointer_cast
__allocated_ptr< _Alloc > __allocate_guarded(_Alloc &__a)
Allocate space for a single object using __a.
static auto construct(_Alloc &__a, _Tp *__p, _Args &&... __args) noexcept(noexcept(_S_construct(__a, __p, std::forward< _Args >(__args)...))) -> decltype(_S_construct(__a, __p, std::forward< _Args >(__args)...))
Construct an object of type _Tp.
_Iterator __base(_Iterator __it)
A simple smart pointer providing strict ownership semantics.
static void destroy(_Alloc &__a, _Tp *__p) noexcept(noexcept(_S_destroy(__a, __p, 0)))
Destroy an object of type _Tp.
__shared_ptr< _Tp, _Lp > static_pointer_cast(const __shared_ptr< _Tp1, _Lp > &__r) noexcept
static_pointer_cast
A smart pointer with reference-counted copy semantics.
Exception possibly thrown by shared_ptr.
Base class allowing use of member function shared_from_this.