49 #ifndef _SHARED_PTR_BASE_H 50 #define _SHARED_PTR_BASE_H 1 56 #include <bits/refwrap.h> 60 namespace std _GLIBCXX_VISIBILITY(default)
62 _GLIBCXX_BEGIN_NAMESPACE_VERSION
64 #if _GLIBCXX_USE_DEPRECATED 65 template<
typename>
class auto_ptr;
75 virtual char const*
what()
const noexcept;
82 __throw_bad_weak_ptr()
85 using __gnu_cxx::_Lock_policy;
86 using __gnu_cxx::__default_lock_policy;
87 using __gnu_cxx::_S_single;
88 using __gnu_cxx::_S_mutex;
89 using __gnu_cxx::_S_atomic;
92 template<_Lock_policy _Lp>
97 enum { _S_need_barriers = 0 };
101 class _Mutex_base<_S_mutex>
102 :
public __gnu_cxx::__mutex
108 enum { _S_need_barriers = 1 };
111 template<_Lock_policy _Lp = __default_lock_policy>
112 class _Sp_counted_base
113 :
public _Mutex_base<_Lp>
116 _Sp_counted_base() noexcept
117 : _M_use_count(1), _M_weak_count(1) { }
120 ~_Sp_counted_base() noexcept
126 _M_dispose() noexcept = 0;
130 _M_destroy() noexcept
138 { __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); }
144 _M_add_ref_lock_nothrow();
147 _M_release() noexcept
150 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count);
151 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
153 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count);
159 if (_Mutex_base<_Lp>::_S_need_barriers)
161 __atomic_thread_fence (__ATOMIC_ACQ_REL);
165 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
166 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count,
169 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
176 _M_weak_add_ref() noexcept
177 { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); }
180 _M_weak_release() noexcept
183 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
184 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1)
186 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
187 if (_Mutex_base<_Lp>::_S_need_barriers)
191 __atomic_thread_fence (__ATOMIC_ACQ_REL);
198 _M_get_use_count()
const noexcept
202 return __atomic_load_n(&_M_use_count, __ATOMIC_RELAXED);
206 _Sp_counted_base(_Sp_counted_base
const&) =
delete;
207 _Sp_counted_base& operator=(_Sp_counted_base
const&) =
delete;
209 _Atomic_word _M_use_count;
210 _Atomic_word _M_weak_count;
215 _Sp_counted_base<_S_single>::
218 if (_M_use_count == 0)
219 __throw_bad_weak_ptr();
225 _Sp_counted_base<_S_mutex>::
229 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
232 __throw_bad_weak_ptr();
238 _Sp_counted_base<_S_atomic>::
242 _Atomic_word __count = _M_get_use_count();
246 __throw_bad_weak_ptr();
250 while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1,
251 true, __ATOMIC_ACQ_REL,
257 _Sp_counted_base<_S_single>::
258 _M_add_ref_lock_nothrow()
260 if (_M_use_count == 0)
268 _Sp_counted_base<_S_mutex>::
269 _M_add_ref_lock_nothrow()
272 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
282 _Sp_counted_base<_S_atomic>::
283 _M_add_ref_lock_nothrow()
286 _Atomic_word __count = _M_get_use_count();
294 while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1,
295 true, __ATOMIC_ACQ_REL,
302 _Sp_counted_base<_S_single>::_M_add_ref_copy()
307 _Sp_counted_base<_S_single>::_M_release() noexcept
309 if (--_M_use_count == 0)
312 if (--_M_weak_count == 0)
319 _Sp_counted_base<_S_single>::_M_weak_add_ref() noexcept
324 _Sp_counted_base<_S_single>::_M_weak_release() noexcept
326 if (--_M_weak_count == 0)
332 _Sp_counted_base<_S_single>::_M_get_use_count()
const noexcept
333 {
return _M_use_count; }
337 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
340 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
343 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
344 class __enable_shared_from_this;
346 template<
typename _Tp>
349 template<
typename _Tp>
352 template<
typename _Tp>
355 template<
typename _Tp>
358 template<_Lock_policy _Lp = __default_lock_policy>
361 template<_Lock_policy _Lp = __default_lock_policy>
362 class __shared_count;
366 template<
typename _Ptr, _Lock_policy _Lp>
367 class _Sp_counted_ptr final :
public _Sp_counted_base<_Lp>
371 _Sp_counted_ptr(_Ptr __p) noexcept
375 _M_dispose() noexcept
379 _M_destroy() noexcept
386 _Sp_counted_ptr(
const _Sp_counted_ptr&) =
delete;
387 _Sp_counted_ptr& operator=(
const _Sp_counted_ptr&) =
delete;
395 _Sp_counted_ptr<nullptr_t, _S_single>::_M_dispose() noexcept { }
399 _Sp_counted_ptr<nullptr_t, _S_mutex>::_M_dispose() noexcept { }
403 _Sp_counted_ptr<nullptr_t, _S_atomic>::_M_dispose() noexcept { }
405 template<
int _Nm,
typename _Tp,
406 bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
407 struct _Sp_ebo_helper;
410 template<
int _Nm,
typename _Tp>
411 struct _Sp_ebo_helper<_Nm, _Tp, true> :
private _Tp
413 explicit _Sp_ebo_helper(
const _Tp& __tp) : _Tp(__tp) { }
416 _S_get(_Sp_ebo_helper& __eboh) {
return static_cast<_Tp&
>(__eboh); }
420 template<
int _Nm,
typename _Tp>
421 struct _Sp_ebo_helper<_Nm, _Tp, false>
423 explicit _Sp_ebo_helper(
const _Tp& __tp) : _M_tp(__tp) { }
426 _S_get(_Sp_ebo_helper& __eboh)
427 {
return __eboh._M_tp; }
434 template<
typename _Ptr,
typename _Deleter,
typename _Alloc, _Lock_policy _Lp>
435 class _Sp_counted_deleter final :
public _Sp_counted_base<_Lp>
437 class _Impl : _Sp_ebo_helper<0, _Deleter>, _Sp_ebo_helper<1, _Alloc>
439 typedef _Sp_ebo_helper<0, _Deleter> _Del_base;
440 typedef _Sp_ebo_helper<1, _Alloc> _Alloc_base;
443 _Impl(_Ptr __p, _Deleter __d,
const _Alloc& __a) noexcept
444 : _M_ptr(__p), _Del_base(__d), _Alloc_base(__a)
447 _Deleter& _M_del() noexcept {
return _Del_base::_S_get(*
this); }
448 _Alloc& _M_alloc() noexcept {
return _Alloc_base::_S_get(*
this); }
454 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_deleter>;
457 _Sp_counted_deleter(_Ptr __p, _Deleter __d) noexcept
458 : _M_impl(__p, __d, _Alloc()) { }
461 _Sp_counted_deleter(_Ptr __p, _Deleter __d,
const _Alloc& __a) noexcept
462 : _M_impl(__p, __d, __a) { }
464 ~_Sp_counted_deleter() noexcept { }
467 _M_dispose() noexcept
468 { _M_impl._M_del()(_M_impl._M_ptr); }
471 _M_destroy() noexcept
473 __allocator_type __a(_M_impl._M_alloc());
475 this->~_Sp_counted_deleter();
484 return __ti ==
typeid(_Deleter)
498 struct _Sp_make_shared_tag { };
500 template<
typename _Tp,
typename _Alloc, _Lock_policy _Lp>
501 class _Sp_counted_ptr_inplace final :
public _Sp_counted_base<_Lp>
503 class _Impl : _Sp_ebo_helper<0, _Alloc>
505 typedef _Sp_ebo_helper<0, _Alloc> _A_base;
508 explicit _Impl(_Alloc __a) noexcept : _A_base(__a) { }
510 _Alloc& _M_alloc() noexcept {
return _A_base::_S_get(*
this); }
512 __gnu_cxx::__aligned_buffer<_Tp> _M_storage;
516 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>;
518 template<
typename... _Args>
519 _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
525 std::forward<_Args>(__args)...);
528 ~_Sp_counted_ptr_inplace() noexcept { }
531 _M_dispose() noexcept
538 _M_destroy() noexcept
540 __allocator_type __a(_M_impl._M_alloc());
542 this->~_Sp_counted_ptr_inplace();
550 if (__ti ==
typeid(_Sp_make_shared_tag))
551 return const_cast<typename remove_cv<_Tp>::type*
>(_M_ptr());
557 _Tp* _M_ptr() noexcept {
return _M_impl._M_storage._M_ptr(); }
563 struct __sp_array_delete
565 template<
typename _Yp>
566 void operator()(_Yp* __p)
const {
delete[] __p; }
569 template<_Lock_policy _Lp>
573 constexpr __shared_count() noexcept : _M_pi(0)
576 template<
typename _Ptr>
578 __shared_count(_Ptr __p) : _M_pi(0)
582 _M_pi =
new _Sp_counted_ptr<_Ptr, _Lp>(__p);
587 __throw_exception_again;
591 template<
typename _Ptr>
593 : __shared_count(__p)
596 template<
typename _Ptr>
601 template<
typename _Ptr,
typename _Deleter>
602 __shared_count(_Ptr __p, _Deleter __d)
606 template<
typename _Ptr,
typename _Deleter,
typename _Alloc>
607 __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
609 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
612 typename _Sp_cd_type::__allocator_type __a2(__a);
614 _Sp_cd_type* __mem = __guard.get();
615 ::new (__mem) _Sp_cd_type(__p, std::move(__d), std::move(__a));
622 __throw_exception_again;
626 template<
typename _Tp,
typename _Alloc,
typename... _Args>
627 __shared_count(_Sp_make_shared_tag, _Tp*,
const _Alloc& __a,
631 typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type;
632 typename _Sp_cp_type::__allocator_type __a2(__a);
634 _Sp_cp_type* __mem = __guard.get();
635 ::new (__mem) _Sp_cp_type(std::move(__a),
636 std::forward<_Args>(__args)...);
641 #if _GLIBCXX_USE_DEPRECATED 643 template<
typename _Tp>
649 template<
typename _Tp,
typename _Del>
655 if (__r.get() ==
nullptr)
658 using _Ptr =
typename unique_ptr<_Tp, _Del>::pointer;
659 using _Del2 =
typename conditional<is_reference<_Del>::value,
663 = _Sp_counted_deleter<_Ptr, _Del2, allocator<void>, _Lp>;
667 _Sp_cd_type* __mem = _Alloc_traits::allocate(__a, 1);
668 _Alloc_traits::construct(__a, __mem, __r.release(),
674 explicit __shared_count(
const __weak_count<_Lp>& __r);
677 explicit __shared_count(
const __weak_count<_Lp>& __r, std::nothrow_t);
679 ~__shared_count() noexcept
681 if (_M_pi !=
nullptr)
685 __shared_count(
const __shared_count& __r) noexcept
689 _M_pi->_M_add_ref_copy();
693 operator=(
const __shared_count& __r) noexcept
695 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
699 __tmp->_M_add_ref_copy();
708 _M_swap(__shared_count& __r) noexcept
710 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
716 _M_get_use_count()
const noexcept
717 {
return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
720 _M_unique()
const noexcept
721 {
return this->_M_get_use_count() == 1; }
725 {
return _M_pi ? _M_pi->_M_get_deleter(__ti) :
nullptr; }
728 _M_less(
const __shared_count& __rhs)
const noexcept
732 _M_less(
const __weak_count<_Lp>& __rhs)
const noexcept
737 operator==(
const __shared_count& __a,
const __shared_count& __b) noexcept
738 {
return __a._M_pi == __b._M_pi; }
741 friend class __weak_count<_Lp>;
743 _Sp_counted_base<_Lp>* _M_pi;
747 template<_Lock_policy _Lp>
751 constexpr __weak_count() noexcept : _M_pi(
nullptr)
754 __weak_count(
const __shared_count<_Lp>& __r) noexcept
757 if (_M_pi !=
nullptr)
758 _M_pi->_M_weak_add_ref();
761 __weak_count(
const __weak_count& __r) noexcept
764 if (_M_pi !=
nullptr)
765 _M_pi->_M_weak_add_ref();
768 __weak_count(__weak_count&& __r) noexcept
770 { __r._M_pi =
nullptr; }
772 ~__weak_count() noexcept
774 if (_M_pi !=
nullptr)
775 _M_pi->_M_weak_release();
779 operator=(
const __shared_count<_Lp>& __r) noexcept
781 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
782 if (__tmp !=
nullptr)
783 __tmp->_M_weak_add_ref();
784 if (_M_pi !=
nullptr)
785 _M_pi->_M_weak_release();
791 operator=(
const __weak_count& __r) noexcept
793 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
794 if (__tmp !=
nullptr)
795 __tmp->_M_weak_add_ref();
796 if (_M_pi !=
nullptr)
797 _M_pi->_M_weak_release();
803 operator=(__weak_count&& __r) noexcept
805 if (_M_pi !=
nullptr)
806 _M_pi->_M_weak_release();
813 _M_swap(__weak_count& __r) noexcept
815 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
821 _M_get_use_count()
const noexcept
822 {
return _M_pi !=
nullptr ? _M_pi->_M_get_use_count() : 0; }
825 _M_less(
const __weak_count& __rhs)
const noexcept
829 _M_less(
const __shared_count<_Lp>& __rhs)
const noexcept
834 operator==(
const __weak_count& __a,
const __weak_count& __b) noexcept
835 {
return __a._M_pi == __b._M_pi; }
838 friend class __shared_count<_Lp>;
840 _Sp_counted_base<_Lp>* _M_pi;
844 template<_Lock_policy _Lp>
846 __shared_count<_Lp>::__shared_count(
const __weak_count<_Lp>& __r)
849 if (_M_pi !=
nullptr)
850 _M_pi->_M_add_ref_lock();
852 __throw_bad_weak_ptr();
856 template<_Lock_policy _Lp>
858 __shared_count<_Lp>::
859 __shared_count(
const __weak_count<_Lp>& __r, std::nothrow_t)
862 if (_M_pi !=
nullptr)
863 if (!_M_pi->_M_add_ref_lock_nothrow())
867 #define __cpp_lib_shared_ptr_arrays 201603 873 template<
typename _Yp_ptr,
typename _Tp_ptr>
874 struct __sp_compatible_with
878 template<
typename _Yp,
typename _Tp>
879 struct __sp_compatible_with<_Yp*, _Tp*>
880 : is_convertible<_Yp*, _Tp*>::type
883 template<
typename _Up,
size_t _Nm>
884 struct __sp_compatible_with<_Up(*)[_Nm], _Up(*)[]>
888 template<
typename _Up,
size_t _Nm>
889 struct __sp_compatible_with<_Up(*)[_Nm],
const _Up(*)[]>
893 template<
typename _Up,
size_t _Nm>
894 struct __sp_compatible_with<_Up(*)[_Nm],
volatile _Up(*)[]>
898 template<
typename _Up,
size_t _Nm>
899 struct __sp_compatible_with<_Up(*)[_Nm],
const volatile _Up(*)[]>
904 template<
typename _Up,
size_t _Nm,
typename _Yp,
typename =
void>
905 struct __sp_is_constructible_arrN
909 template<
typename _Up,
size_t _Nm,
typename _Yp>
910 struct __sp_is_constructible_arrN<_Up, _Nm, _Yp, __void_t<_Yp[_Nm]>>
911 : is_convertible<_Yp(*)[_Nm], _Up(*)[_Nm]>::type
915 template<
typename _Up,
typename _Yp,
typename =
void>
916 struct __sp_is_constructible_arr
920 template<
typename _Up,
typename _Yp>
921 struct __sp_is_constructible_arr<_Up, _Yp, __void_t<_Yp[]>>
922 : is_convertible<_Yp(*)[], _Up(*)[]>::type
926 template<
typename _Tp,
typename _Yp>
927 struct __sp_is_constructible;
930 template<
typename _Up,
size_t _Nm,
typename _Yp>
931 struct __sp_is_constructible<_Up[_Nm], _Yp>
932 : __sp_is_constructible_arrN<_Up, _Nm, _Yp>::type
936 template<
typename _Up,
typename _Yp>
937 struct __sp_is_constructible<_Up[], _Yp>
938 : __sp_is_constructible_arr<_Up, _Yp>::type
942 template<
typename _Tp,
typename _Yp>
943 struct __sp_is_constructible
944 : is_convertible<_Yp*, _Tp*>::type
949 template<
typename _Tp, _Lock_policy _Lp,
951 class __shared_ptr_access
954 using element_type = _Tp;
959 __glibcxx_assert(_M_get() !=
nullptr);
964 operator->()
const noexcept
966 _GLIBCXX_DEBUG_PEDASSERT(_M_get() !=
nullptr);
972 _M_get()
const noexcept
973 {
return static_cast<const __shared_ptr<_Tp, _Lp>*
>(
this)->
get(); }
977 template<
typename _Tp, _Lock_policy _Lp>
978 class __shared_ptr_access<_Tp, _Lp, false, true>
981 using element_type = _Tp;
984 operator->()
const noexcept
986 auto __ptr =
static_cast<const __shared_ptr<_Tp, _Lp>*
>(
this)->
get();
987 _GLIBCXX_DEBUG_PEDASSERT(__ptr !=
nullptr);
993 template<
typename _Tp, _Lock_policy _Lp>
994 class __shared_ptr_access<_Tp, _Lp, true, false>
997 using element_type =
typename remove_extent<_Tp>::type;
999 #if __cplusplus <= 201402L 1000 [[__deprecated__(
"shared_ptr<T[]>::operator* is absent from C++17")]]
1004 __glibcxx_assert(_M_get() !=
nullptr);
1008 [[__deprecated__(
"shared_ptr<T[]>::operator-> is absent from C++17")]]
1010 operator->()
const noexcept
1012 _GLIBCXX_DEBUG_PEDASSERT(_M_get() !=
nullptr);
1018 operator[](ptrdiff_t __i)
const 1020 __glibcxx_assert(_M_get() !=
nullptr);
1021 __glibcxx_assert(!extent<_Tp>::value || __i < extent<_Tp>::value);
1022 return _M_get()[__i];
1027 _M_get()
const noexcept
1028 {
return static_cast<const __shared_ptr<_Tp, _Lp>*
>(
this)->
get(); }
1031 template<
typename _Tp, _Lock_policy _Lp>
1033 :
public __shared_ptr_access<_Tp, _Lp>
1036 using element_type =
typename remove_extent<_Tp>::type;
1040 template<
typename _Yp>
1042 =
typename enable_if<__sp_is_constructible<_Tp, _Yp>::value>::type;
1045 template<
typename _Yp,
typename _Res =
void>
1046 using _Compatible =
typename 1047 enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type;
1050 template<
typename _Yp>
1051 using _Assignable = _Compatible<_Yp, __shared_ptr&>;
1054 template<
typename _Yp,
typename _Del,
typename _Res = void,
1055 typename _Ptr =
typename unique_ptr<_Yp, _Del>::pointer>
1056 using _UniqCompatible =
typename enable_if<__and_<
1057 __sp_compatible_with<_Yp*, _Tp*>, is_convertible<_Ptr, element_type*>
1058 >::value, _Res>::type;
1061 template<
typename _Yp,
typename _Del>
1062 using _UniqAssignable = _UniqCompatible<_Yp, _Del, __shared_ptr&>;
1066 #if __cplusplus > 201402L 1067 using weak_type = __weak_ptr<_Tp, _Lp>;
1070 constexpr __shared_ptr() noexcept
1071 : _M_ptr(0), _M_refcount()
1074 template<
typename _Yp,
typename = _SafeConv<_Yp>>
1076 __shared_ptr(_Yp* __p)
1080 static_assert(
sizeof(_Yp) > 0,
"incomplete type" );
1081 _M_enable_shared_from_this_with(__p);
1084 template<
typename _Yp,
typename _Deleter,
typename = _SafeConv<_Yp>>
1085 __shared_ptr(_Yp* __p, _Deleter __d)
1086 : _M_ptr(__p), _M_refcount(__p, __d)
1088 static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
1089 "deleter expression d(p) is well-formed");
1090 _M_enable_shared_from_this_with(__p);
1093 template<
typename _Yp,
typename _Deleter,
typename _Alloc,
1094 typename = _SafeConv<_Yp>>
1095 __shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a)
1096 : _M_ptr(__p), _M_refcount(__p, __d, std::move(__a))
1098 static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
1099 "deleter expression d(p) is well-formed");
1100 _M_enable_shared_from_this_with(__p);
1103 template<
typename _Deleter>
1104 __shared_ptr(nullptr_t __p, _Deleter __d)
1105 : _M_ptr(0), _M_refcount(__p, __d)
1108 template<
typename _Deleter,
typename _Alloc>
1109 __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
1110 : _M_ptr(0), _M_refcount(__p, __d, std::move(__a))
1113 template<
typename _Yp>
1114 __shared_ptr(
const __shared_ptr<_Yp, _Lp>& __r,
1115 element_type* __p) noexcept
1116 : _M_ptr(__p), _M_refcount(__r._M_refcount)
1119 __shared_ptr(
const __shared_ptr&) noexcept =
default;
1120 __shared_ptr& operator=(
const __shared_ptr&) noexcept =
default;
1121 ~__shared_ptr() =
default;
1123 template<
typename _Yp,
typename = _Compatible<_Yp>>
1124 __shared_ptr(
const __shared_ptr<_Yp, _Lp>& __r) noexcept
1125 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1128 __shared_ptr(__shared_ptr&& __r) noexcept
1129 : _M_ptr(__r._M_ptr), _M_refcount()
1131 _M_refcount._M_swap(__r._M_refcount);
1135 template<
typename _Yp,
typename = _Compatible<_Yp>>
1136 __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r) noexcept
1137 : _M_ptr(__r._M_ptr), _M_refcount()
1139 _M_refcount._M_swap(__r._M_refcount);
1143 template<
typename _Yp,
typename = _Compatible<_Yp>>
1144 explicit __shared_ptr(
const __weak_ptr<_Yp, _Lp>& __r)
1145 : _M_refcount(__r._M_refcount)
1149 _M_ptr = __r._M_ptr;
1153 template<
typename _Yp,
typename _Del,
1154 typename = _UniqCompatible<_Yp, _Del>>
1156 : _M_ptr(__r.get()), _M_refcount()
1158 auto __raw = _S_raw_ptr(__r.get());
1159 _M_refcount = __shared_count<_Lp>(std::move(__r));
1160 _M_enable_shared_from_this_with(__raw);
1163 #if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED 1166 template<
typename _Tp1,
typename _Del,
1167 typename enable_if<__and_<
1169 is_convertible<typename unique_ptr<_Tp1, _Del>::pointer, _Tp*>
1170 >::value,
bool>::type =
true>
1172 : _M_ptr(__r.get()), _M_refcount()
1174 auto __raw = _S_raw_ptr(__r.get());
1175 _M_refcount = __shared_count<_Lp>(std::move(__r));
1176 _M_enable_shared_from_this_with(__raw);
1181 #if _GLIBCXX_USE_DEPRECATED 1183 template<
typename _Yp,
typename = _Compatible<_Yp>>
1187 constexpr __shared_ptr(nullptr_t) noexcept : __shared_ptr() { }
1189 template<
typename _Yp>
1191 operator=(
const __shared_ptr<_Yp, _Lp>& __r) noexcept
1193 _M_ptr = __r._M_ptr;
1194 _M_refcount = __r._M_refcount;
1198 #if _GLIBCXX_USE_DEPRECATED 1199 template<
typename _Yp>
1203 __shared_ptr(std::move(__r)).swap(*
this);
1209 operator=(__shared_ptr&& __r) noexcept
1211 __shared_ptr(std::move(__r)).swap(*
this);
1217 operator=(__shared_ptr<_Yp, _Lp>&& __r) noexcept
1219 __shared_ptr(std::move(__r)).swap(*
this);
1223 template<
typename _Yp,
typename _Del>
1224 _UniqAssignable<_Yp, _Del>
1227 __shared_ptr(std::move(__r)).swap(*
this);
1233 { __shared_ptr().swap(*
this); }
1235 template<
typename _Yp>
1240 __glibcxx_assert(__p == 0 || __p != _M_ptr);
1241 __shared_ptr(__p).swap(*
this);
1244 template<
typename _Yp,
typename _Deleter>
1246 reset(_Yp* __p, _Deleter __d)
1247 { __shared_ptr(__p, __d).swap(*
this); }
1249 template<
typename _Yp,
typename _Deleter,
typename _Alloc>
1251 reset(_Yp* __p, _Deleter __d, _Alloc __a)
1252 { __shared_ptr(__p, __d, std::move(__a)).swap(*
this); }
1255 get()
const noexcept
1258 explicit operator bool()
const 1259 {
return _M_ptr == 0 ?
false :
true; }
1262 unique()
const noexcept
1263 {
return _M_refcount._M_unique(); }
1266 use_count()
const noexcept
1267 {
return _M_refcount._M_get_use_count(); }
1270 swap(__shared_ptr<_Tp, _Lp>& __other) noexcept
1272 std::swap(_M_ptr, __other._M_ptr);
1273 _M_refcount._M_swap(__other._M_refcount);
1276 template<
typename _Tp1>
1278 owner_before(__shared_ptr<_Tp1, _Lp>
const& __rhs)
const 1279 {
return _M_refcount._M_less(__rhs._M_refcount); }
1281 template<
typename _Tp1>
1283 owner_before(__weak_ptr<_Tp1, _Lp>
const& __rhs)
const 1284 {
return _M_refcount._M_less(__rhs._M_refcount); }
1289 template<
typename _Alloc,
typename... _Args>
1290 __shared_ptr(_Sp_make_shared_tag __tag,
const _Alloc& __a,
1292 : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a,
1293 std::forward<_Args>(__args)...)
1297 void* __p = _M_refcount._M_get_deleter(
typeid(__tag));
1298 _M_ptr =
static_cast<_Tp*
>(__p);
1299 _M_enable_shared_from_this_with(_M_ptr);
1302 template<
typename _Alloc>
1305 void operator()(
typename _Alloc::value_type* __ptr)
1313 template<
typename _Alloc,
typename... _Args>
1314 __shared_ptr(_Sp_make_shared_tag __tag,
const _Alloc& __a,
1316 : _M_ptr(), _M_refcount()
1319 rebind_traits<typename std::remove_cv<_Tp>::type> __traits;
1320 _Deleter<typename __traits::allocator_type> __del = { __a };
1322 auto __ptr = __guard.get();
1325 __traits::construct(__del._M_alloc, __ptr,
1326 std::forward<_Args>(__args)...);
1328 __shared_count<_Lp> __count(__ptr, __del, __del._M_alloc);
1329 _M_refcount._M_swap(__count);
1331 _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>
1367 typename enable_if<__has_esft_base<_Yp2>::value>::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>
1375 typename enable_if<!__has_esft_base<_Yp2>::value>::type
1376 _M_enable_shared_from_this_with(_Yp*) noexcept
1381 {
return _M_refcount._M_get_deleter(__ti); }
1383 template<
typename _Tp1>
1385 _S_raw_ptr(_Tp1* __ptr)
1388 template<
typename _Tp1>
1393 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
1394 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
1396 template<
typename _Del,
typename _Tp1, _Lock_policy _Lp1>
1397 friend _Del*
get_deleter(
const __shared_ptr<_Tp1, _Lp1>&) noexcept;
1399 element_type* _M_ptr;
1400 __shared_count<_Lp> _M_refcount;
1405 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1407 operator==(
const __shared_ptr<_Tp1, _Lp>& __a,
1408 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1409 {
return __a.get() == __b.get(); }
1411 template<
typename _Tp, _Lock_policy _Lp>
1413 operator==(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1416 template<
typename _Tp, _Lock_policy _Lp>
1418 operator==(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1421 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1423 operator!=(
const __shared_ptr<_Tp1, _Lp>& __a,
1424 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1425 {
return __a.get() != __b.get(); }
1427 template<
typename _Tp, _Lock_policy _Lp>
1429 operator!=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1430 {
return (
bool)__a; }
1432 template<
typename _Tp, _Lock_policy _Lp>
1434 operator!=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1435 {
return (
bool)__a; }
1437 template<
typename _Tp,
typename _Up, _Lock_policy _Lp>
1439 operator<(const __shared_ptr<_Tp, _Lp>& __a,
1440 const __shared_ptr<_Up, _Lp>& __b) noexcept
1442 using _Tp_elt =
typename __shared_ptr<_Tp, _Lp>::element_type;
1443 using _Up_elt =
typename __shared_ptr<_Up, _Lp>::element_type;
1444 using _Vp =
typename common_type<_Tp_elt*, _Up_elt*>::type;
1445 return less<_Vp>()(__a.get(), __b.get());
1448 template<
typename _Tp, _Lock_policy _Lp>
1450 operator<(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1452 using _Tp_elt =
typename __shared_ptr<_Tp, _Lp>::element_type;
1456 template<
typename _Tp, _Lock_policy _Lp>
1458 operator<(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1460 using _Tp_elt =
typename __shared_ptr<_Tp, _Lp>::element_type;
1464 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1466 operator<=(const __shared_ptr<_Tp1, _Lp>& __a,
1467 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1468 {
return !(__b < __a); }
1470 template<
typename _Tp, _Lock_policy _Lp>
1472 operator<=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1473 {
return !(
nullptr < __a); }
1475 template<
typename _Tp, _Lock_policy _Lp>
1477 operator<=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1478 {
return !(__a <
nullptr); }
1480 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1482 operator>(
const __shared_ptr<_Tp1, _Lp>& __a,
1483 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1484 {
return (__b < __a); }
1486 template<
typename _Tp, _Lock_policy _Lp>
1488 operator>(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1489 {
return nullptr < __a; }
1491 template<
typename _Tp, _Lock_policy _Lp>
1493 operator>(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1494 {
return __a <
nullptr; }
1496 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1498 operator>=(
const __shared_ptr<_Tp1, _Lp>& __a,
1499 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1500 {
return !(__a < __b); }
1502 template<
typename _Tp, _Lock_policy _Lp>
1504 operator>=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1505 {
return !(__a <
nullptr); }
1507 template<
typename _Tp, _Lock_policy _Lp>
1509 operator>=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1510 {
return !(
nullptr < __a); }
1512 template<
typename _Sp>
1516 operator()(
const _Sp& __lhs,
const _Sp& __rhs)
const noexcept
1518 typedef typename _Sp::element_type element_type;
1523 template<
typename _Tp, _Lock_policy _Lp>
1524 struct less<__shared_ptr<_Tp, _Lp>>
1525 :
public _Sp_less<__shared_ptr<_Tp, _Lp>>
1529 template<
typename _Tp, _Lock_policy _Lp>
1531 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b) noexcept
1541 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1542 inline __shared_ptr<_Tp, _Lp>
1543 static_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1545 using _Sp = __shared_ptr<_Tp, _Lp>;
1546 return _Sp(__r, static_cast<typename _Sp::element_type*>(__r.get()));
1554 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1555 inline __shared_ptr<_Tp, _Lp>
1556 const_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1558 using _Sp = __shared_ptr<_Tp, _Lp>;
1559 return _Sp(__r, const_cast<typename _Sp::element_type*>(__r.get()));
1567 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1568 inline __shared_ptr<_Tp, _Lp>
1569 dynamic_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1571 using _Sp = __shared_ptr<_Tp, _Lp>;
1572 if (
auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get()))
1573 return _Sp(__r, __p);
1577 #if __cplusplus > 201402L 1578 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1579 inline __shared_ptr<_Tp, _Lp>
1580 reinterpret_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1582 using _Sp = __shared_ptr<_Tp, _Lp>;
1583 return _Sp(__r, reinterpret_cast<typename _Sp::element_type*>(__r.get()));
1587 template<
typename _Tp, _Lock_policy _Lp>
1590 template<
typename _Yp,
typename _Res =
void>
1591 using _Compatible =
typename 1592 enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type;
1595 template<
typename _Yp>
1596 using _Assignable = _Compatible<_Yp, __weak_ptr&>;
1599 using element_type =
typename remove_extent<_Tp>::type;
1601 constexpr __weak_ptr() noexcept
1602 : _M_ptr(
nullptr), _M_refcount()
1605 __weak_ptr(
const __weak_ptr&) noexcept =
default;
1607 ~__weak_ptr() =
default;
1623 template<
typename _Yp,
typename = _Compatible<_Yp>>
1624 __weak_ptr(
const __weak_ptr<_Yp, _Lp>& __r) noexcept
1625 : _M_refcount(__r._M_refcount)
1626 { _M_ptr = __r.lock().get(); }
1628 template<
typename _Yp,
typename = _Compatible<_Yp>>
1629 __weak_ptr(
const __shared_ptr<_Yp, _Lp>& __r) noexcept
1630 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1633 __weak_ptr(__weak_ptr&& __r) noexcept
1634 : _M_ptr(__r._M_ptr), _M_refcount(std::move(__r._M_refcount))
1635 { __r._M_ptr =
nullptr; }
1637 template<
typename _Yp,
typename = _Compatible<_Yp>>
1638 __weak_ptr(__weak_ptr<_Yp, _Lp>&& __r) noexcept
1639 : _M_ptr(__r.lock().get()), _M_refcount(std::move(__r._M_refcount))
1640 { __r._M_ptr =
nullptr; }
1643 operator=(
const __weak_ptr& __r) noexcept =
default;
1645 template<
typename _Yp>
1647 operator=(
const __weak_ptr<_Yp, _Lp>& __r) noexcept
1649 _M_ptr = __r.lock().get();
1650 _M_refcount = __r._M_refcount;
1654 template<
typename _Yp>
1656 operator=(
const __shared_ptr<_Yp, _Lp>& __r) noexcept
1658 _M_ptr = __r._M_ptr;
1659 _M_refcount = __r._M_refcount;
1664 operator=(__weak_ptr&& __r) noexcept
1666 _M_ptr = __r._M_ptr;
1667 _M_refcount = std::move(__r._M_refcount);
1668 __r._M_ptr =
nullptr;
1672 template<
typename _Yp>
1674 operator=(__weak_ptr<_Yp, _Lp>&& __r) noexcept
1676 _M_ptr = __r.lock().get();
1677 _M_refcount = std::move(__r._M_refcount);
1678 __r._M_ptr =
nullptr;
1682 __shared_ptr<_Tp, _Lp>
1683 lock()
const noexcept
1684 {
return __shared_ptr<element_type, _Lp>(*
this, std::nothrow); }
1687 use_count()
const noexcept
1688 {
return _M_refcount._M_get_use_count(); }
1691 expired()
const noexcept
1692 {
return _M_refcount._M_get_use_count() == 0; }
1694 template<
typename _Tp1>
1696 owner_before(
const __shared_ptr<_Tp1, _Lp>& __rhs)
const 1697 {
return _M_refcount._M_less(__rhs._M_refcount); }
1699 template<
typename _Tp1>
1701 owner_before(
const __weak_ptr<_Tp1, _Lp>& __rhs)
const 1702 {
return _M_refcount._M_less(__rhs._M_refcount); }
1706 { __weak_ptr().swap(*
this); }
1709 swap(__weak_ptr& __s) noexcept
1711 std::swap(_M_ptr, __s._M_ptr);
1712 _M_refcount._M_swap(__s._M_refcount);
1718 _M_assign(_Tp* __ptr,
const __shared_count<_Lp>& __refcount) noexcept
1720 if (use_count() == 0)
1723 _M_refcount = __refcount;
1727 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
1728 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
1729 friend class __enable_shared_from_this<_Tp, _Lp>;
1732 element_type* _M_ptr;
1733 __weak_count<_Lp> _M_refcount;
1737 template<
typename _Tp, _Lock_policy _Lp>
1739 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b) noexcept
1742 template<
typename _Tp,
typename _Tp1>
1746 operator()(
const _Tp& __lhs,
const _Tp& __rhs)
const 1747 {
return __lhs.owner_before(__rhs); }
1750 operator()(
const _Tp& __lhs,
const _Tp1& __rhs)
const 1751 {
return __lhs.owner_before(__rhs); }
1754 operator()(
const _Tp1& __lhs,
const _Tp& __rhs)
const 1755 {
return __lhs.owner_before(__rhs); }
1759 struct _Sp_owner_less<void, void>
1761 template<
typename _Tp,
typename _Up>
1763 operator()(
const _Tp& __lhs,
const _Up& __rhs)
const 1764 -> decltype(__lhs.owner_before(__rhs))
1765 {
return __lhs.owner_before(__rhs); }
1767 using is_transparent = void;
1770 template<
typename _Tp, _Lock_policy _Lp>
1772 :
public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
1775 template<
typename _Tp, _Lock_policy _Lp>
1777 :
public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
1781 template<
typename _Tp, _Lock_policy _Lp>
1782 class __enable_shared_from_this
1785 constexpr __enable_shared_from_this() noexcept { }
1787 __enable_shared_from_this(
const __enable_shared_from_this&) noexcept { }
1789 __enable_shared_from_this&
1790 operator=(
const __enable_shared_from_this&) noexcept
1793 ~__enable_shared_from_this() { }
1796 __shared_ptr<_Tp, _Lp>
1798 {
return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
1800 __shared_ptr<const _Tp, _Lp>
1801 shared_from_this()
const 1802 {
return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
1804 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 1805 __weak_ptr<_Tp, _Lp>
1806 weak_from_this() noexcept
1807 {
return this->_M_weak_this; }
1809 __weak_ptr<const _Tp, _Lp>
1810 weak_from_this()
const noexcept
1811 {
return this->_M_weak_this; }
1815 template<
typename _Tp1>
1817 _M_weak_assign(_Tp1* __p,
const __shared_count<_Lp>& __n)
const noexcept
1818 { _M_weak_this._M_assign(__p, __n); }
1820 friend const __enable_shared_from_this*
1821 __enable_shared_from_this_base(
const __shared_count<_Lp>&,
1822 const __enable_shared_from_this* __p)
1825 template<
typename, _Lock_policy>
1826 friend class __shared_ptr;
1828 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
1831 template<
typename _Tp, _Lock_policy _Lp,
typename _Alloc,
typename... _Args>
1832 inline __shared_ptr<_Tp, _Lp>
1833 __allocate_shared(
const _Alloc& __a, _Args&&... __args)
1835 return __shared_ptr<_Tp, _Lp>(_Sp_make_shared_tag(), __a,
1836 std::forward<_Args>(__args)...);
1839 template<
typename _Tp, _Lock_policy _Lp,
typename... _Args>
1840 inline __shared_ptr<_Tp, _Lp>
1841 __make_shared(_Args&&... __args)
1843 typedef typename std::remove_const<_Tp>::type _Tp_nc;
1845 std::forward<_Args>(__args)...);
1849 template<
typename _Tp, _Lock_policy _Lp>
1850 struct hash<__shared_ptr<_Tp, _Lp>>
1851 :
public __hash_base<size_t, __shared_ptr<_Tp, _Lp>>
1854 operator()(
const __shared_ptr<_Tp, _Lp>& __s)
const noexcept
1861 _GLIBCXX_END_NAMESPACE_VERSION
1864 #endif // _SHARED_PTR_BASE_H The standard allocator, as per [20.4].
A simple smart pointer providing strict ownership semantics.
virtual char const * what() const noexcept
complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
Exception possibly thrown by shared_ptr.
Primary class template for reference_wrapper.
Base class allowing use of member function shared_from_this.
__allocated_ptr< _Alloc > __allocate_guarded(_Alloc &__a)
Allocate space for a single object using __a.
ISO C++ entities toplevel namespace is std.
A smart pointer with reference-counted copy semantics.
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
A smart pointer with weak semantics.
20.7.1.2 unique_ptr for single objects.
allocator<void> specialization.
One of the comparison functors.
Base class for all library exceptions.
Uniform interface to all allocator types.
static auto construct(_Alloc &__a, _Tp *__p, _Args &&...__args) -> decltype(_S_construct(__a, __p, std::forward< _Args >(__args)...))
Construct an object of type _Tp.
static void destroy(_Alloc &__a, _Tp *__p)
Destroy an object of type _Tp.
_Del * get_deleter(const __shared_ptr< _Tp, _Lp > &__p) noexcept
20.7.2.2.10 shared_ptr get_deleter
Primary template owner_less.
void lock(_L1 &__l1, _L2 &__l2, _L3 &...__l3)
Generic lock.
Primary class template hash.
Non-standard RAII type for managing pointers obtained from allocators.