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 _GLIBCXX_DEBUG_PEDASSERT(_M_get() !=
nullptr);
987 return static_cast<const __shared_ptr<_Tp, _Lp>*
>(
this)->
get();
992 template<
typename _Tp, _Lock_policy _Lp>
993 class __shared_ptr_access<_Tp, _Lp, true, false>
996 using element_type =
typename remove_extent<_Tp>::type;
998 #if __cplusplus <= 201402L 999 [[__deprecated__(
"shared_ptr<T[]>::operator* is absent from C++17")]]
1003 __glibcxx_assert(_M_get() !=
nullptr);
1007 [[__deprecated__(
"shared_ptr<T[]>::operator-> is absent from C++17")]]
1009 operator->()
const noexcept
1011 _GLIBCXX_DEBUG_PEDASSERT(_M_get() !=
nullptr);
1017 operator[](ptrdiff_t __i)
const 1019 __glibcxx_assert(_M_get() !=
nullptr);
1020 __glibcxx_assert(!extent<_Tp>::value || __i < extent<_Tp>::value);
1021 return _M_get()[__i];
1026 _M_get()
const noexcept
1027 {
return static_cast<const __shared_ptr<_Tp, _Lp>*
>(
this)->
get(); }
1030 template<
typename _Tp, _Lock_policy _Lp>
1032 :
public __shared_ptr_access<_Tp, _Lp>
1035 using element_type =
typename remove_extent<_Tp>::type;
1039 template<
typename _Yp>
1041 =
typename enable_if<__sp_is_constructible<_Tp, _Yp>::value>::type;
1044 template<
typename _Yp,
typename _Res =
void>
1045 using _Compatible =
typename 1046 enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type;
1049 template<
typename _Yp>
1050 using _Assignable = _Compatible<_Yp, __shared_ptr&>;
1053 template<
typename _Yp,
typename _Del,
typename _Res = void,
1054 typename _Ptr =
typename unique_ptr<_Yp, _Del>::pointer>
1055 using _UniqCompatible =
typename enable_if<__and_<
1056 __sp_compatible_with<_Yp*, _Tp*>, is_convertible<_Ptr, element_type*>
1057 >::value, _Res>::type;
1060 template<
typename _Yp,
typename _Del>
1061 using _UniqAssignable = _UniqCompatible<_Yp, _Del, __shared_ptr&>;
1065 #if __cplusplus > 201402L 1066 using weak_type = __weak_ptr<_Tp, _Lp>;
1069 constexpr __shared_ptr() noexcept
1070 : _M_ptr(0), _M_refcount()
1073 template<
typename _Yp,
typename = _SafeConv<_Yp>>
1075 __shared_ptr(_Yp* __p)
1079 static_assert(
sizeof(_Yp) > 0,
"incomplete type" );
1080 _M_enable_shared_from_this_with(__p);
1083 template<
typename _Yp,
typename _Deleter,
typename = _SafeConv<_Yp>>
1084 __shared_ptr(_Yp* __p, _Deleter __d)
1085 : _M_ptr(__p), _M_refcount(__p, __d)
1087 static_assert(__is_callable<_Deleter(_Yp*)>::value,
1088 "deleter expression d(p) is well-formed");
1089 _M_enable_shared_from_this_with(__p);
1092 template<
typename _Yp,
typename _Deleter,
typename _Alloc,
1093 typename = _SafeConv<_Yp>>
1094 __shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a)
1095 : _M_ptr(__p), _M_refcount(__p, __d, std::move(__a))
1097 static_assert(__is_callable<_Deleter(_Yp*)>::value,
1098 "deleter expression d(p) is well-formed");
1099 _M_enable_shared_from_this_with(__p);
1102 template<
typename _Deleter>
1103 __shared_ptr(nullptr_t __p, _Deleter __d)
1104 : _M_ptr(0), _M_refcount(__p, __d)
1107 template<
typename _Deleter,
typename _Alloc>
1108 __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
1109 : _M_ptr(0), _M_refcount(__p, __d, std::move(__a))
1112 template<
typename _Yp>
1113 __shared_ptr(
const __shared_ptr<_Yp, _Lp>& __r,
1114 element_type* __p) noexcept
1115 : _M_ptr(__p), _M_refcount(__r._M_refcount)
1118 __shared_ptr(
const __shared_ptr&) noexcept =
default;
1119 __shared_ptr& operator=(
const __shared_ptr&) noexcept =
default;
1120 ~__shared_ptr() =
default;
1122 template<
typename _Yp,
typename = _Compatible<_Yp>>
1123 __shared_ptr(
const __shared_ptr<_Yp, _Lp>& __r) noexcept
1124 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1127 __shared_ptr(__shared_ptr&& __r) noexcept
1128 : _M_ptr(__r._M_ptr), _M_refcount()
1130 _M_refcount._M_swap(__r._M_refcount);
1134 template<
typename _Yp,
typename = _Compatible<_Yp>>
1135 __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r) noexcept
1136 : _M_ptr(__r._M_ptr), _M_refcount()
1138 _M_refcount._M_swap(__r._M_refcount);
1142 template<
typename _Yp,
typename = _Compatible<_Yp>>
1143 explicit __shared_ptr(
const __weak_ptr<_Yp, _Lp>& __r)
1144 : _M_refcount(__r._M_refcount)
1148 _M_ptr = __r._M_ptr;
1152 template<
typename _Yp,
typename _Del,
1153 typename = _UniqCompatible<_Yp, _Del>>
1155 : _M_ptr(__r.get()), _M_refcount()
1157 auto __raw = _S_raw_ptr(__r.get());
1158 _M_refcount = __shared_count<_Lp>(std::move(__r));
1159 _M_enable_shared_from_this_with(__raw);
1162 #if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED 1165 template<
typename _Tp1,
typename _Del,
1166 typename enable_if<__and_<
1168 is_convertible<typename unique_ptr<_Tp1, _Del>::pointer, _Tp*>
1169 >::value,
bool>::type =
true>
1171 : _M_ptr(__r.get()), _M_refcount()
1173 auto __raw = _S_raw_ptr(__r.get());
1174 _M_refcount = __shared_count<_Lp>(std::move(__r));
1175 _M_enable_shared_from_this_with(__raw);
1180 #if _GLIBCXX_USE_DEPRECATED 1182 template<
typename _Yp,
typename = _Compatible<_Yp>>
1186 constexpr __shared_ptr(nullptr_t) noexcept : __shared_ptr() { }
1188 template<
typename _Yp>
1190 operator=(
const __shared_ptr<_Yp, _Lp>& __r) noexcept
1192 _M_ptr = __r._M_ptr;
1193 _M_refcount = __r._M_refcount;
1197 #if _GLIBCXX_USE_DEPRECATED 1198 template<
typename _Yp>
1202 __shared_ptr(std::move(__r)).swap(*
this);
1208 operator=(__shared_ptr&& __r) noexcept
1210 __shared_ptr(std::move(__r)).swap(*
this);
1216 operator=(__shared_ptr<_Yp, _Lp>&& __r) noexcept
1218 __shared_ptr(std::move(__r)).swap(*
this);
1222 template<
typename _Yp,
typename _Del>
1223 _UniqAssignable<_Yp, _Del>
1226 __shared_ptr(std::move(__r)).swap(*
this);
1232 { __shared_ptr().swap(*
this); }
1234 template<
typename _Yp>
1239 __glibcxx_assert(__p == 0 || __p != _M_ptr);
1240 __shared_ptr(__p).swap(*
this);
1243 template<
typename _Yp,
typename _Deleter>
1245 reset(_Yp* __p, _Deleter __d)
1246 { __shared_ptr(__p, __d).swap(*
this); }
1248 template<
typename _Yp,
typename _Deleter,
typename _Alloc>
1250 reset(_Yp* __p, _Deleter __d, _Alloc __a)
1251 { __shared_ptr(__p, __d, std::move(__a)).swap(*
this); }
1254 get()
const noexcept
1257 explicit operator bool()
const 1258 {
return _M_ptr == 0 ?
false :
true; }
1261 unique()
const noexcept
1262 {
return _M_refcount._M_unique(); }
1265 use_count()
const noexcept
1266 {
return _M_refcount._M_get_use_count(); }
1269 swap(__shared_ptr<_Tp, _Lp>& __other) noexcept
1271 std::swap(_M_ptr, __other._M_ptr);
1272 _M_refcount._M_swap(__other._M_refcount);
1275 template<
typename _Tp1>
1277 owner_before(__shared_ptr<_Tp1, _Lp>
const& __rhs)
const 1278 {
return _M_refcount._M_less(__rhs._M_refcount); }
1280 template<
typename _Tp1>
1282 owner_before(__weak_ptr<_Tp1, _Lp>
const& __rhs)
const 1283 {
return _M_refcount._M_less(__rhs._M_refcount); }
1288 template<
typename _Alloc,
typename... _Args>
1289 __shared_ptr(_Sp_make_shared_tag __tag,
const _Alloc& __a,
1291 : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a,
1292 std::forward<_Args>(__args)...)
1296 void* __p = _M_refcount._M_get_deleter(
typeid(__tag));
1297 _M_ptr =
static_cast<_Tp*
>(__p);
1298 _M_enable_shared_from_this_with(_M_ptr);
1301 template<
typename _Alloc>
1304 void operator()(
typename _Alloc::value_type* __ptr)
1312 template<
typename _Alloc,
typename... _Args>
1313 __shared_ptr(_Sp_make_shared_tag __tag,
const _Alloc& __a,
1315 : _M_ptr(), _M_refcount()
1318 rebind_traits<typename std::remove_cv<_Tp>::type> __traits;
1319 _Deleter<typename __traits::allocator_type> __del = { __a };
1321 auto __ptr = __guard.get();
1324 __traits::construct(__del._M_alloc, __ptr,
1325 std::forward<_Args>(__args)...);
1327 __shared_count<_Lp> __count(__ptr, __del, __del._M_alloc);
1328 _M_refcount._M_swap(__count);
1330 _M_enable_shared_from_this_with(_M_ptr);
1334 template<
typename _Tp1, _Lock_policy _Lp1,
typename _Alloc,
1336 friend __shared_ptr<_Tp1, _Lp1>
1337 __allocate_shared(
const _Alloc& __a, _Args&&... __args);
1341 __shared_ptr(
const __weak_ptr<_Tp, _Lp>& __r, std::nothrow_t)
1342 : _M_refcount(__r._M_refcount, std::nothrow)
1344 _M_ptr = _M_refcount._M_get_use_count() ? __r._M_ptr :
nullptr;
1347 friend class __weak_ptr<_Tp, _Lp>;
1351 template<
typename _Yp>
1352 using __esft_base_t = decltype(__enable_shared_from_this_base(
1353 std::declval<
const __shared_count<_Lp>&>(),
1354 std::declval<_Yp*>()));
1357 template<
typename _Yp,
typename =
void>
1358 struct __has_esft_base
1361 template<
typename _Yp>
1362 struct __has_esft_base<_Yp, __void_t<__esft_base_t<_Yp>>>
1363 : __not_<is_array<_Tp>> { };
1365 template<
typename _Yp>
1366 typename enable_if<__has_esft_base<_Yp>::value>::type
1367 _M_enable_shared_from_this_with(
const _Yp* __p) noexcept
1369 if (
auto __base = __enable_shared_from_this_base(_M_refcount, __p))
1370 __base->_M_weak_assign(const_cast<_Yp*>(__p), _M_refcount);
1373 template<
typename _Yp>
1374 typename enable_if<!__has_esft_base<_Yp>::value>::type
1375 _M_enable_shared_from_this_with(
const _Yp*) noexcept
1380 {
return _M_refcount._M_get_deleter(__ti); }
1382 template<
typename _Tp1>
1384 _S_raw_ptr(_Tp1* __ptr)
1387 template<
typename _Tp1>
1392 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
1393 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
1395 template<
typename _Del,
typename _Tp1, _Lock_policy _Lp1>
1396 friend _Del*
get_deleter(
const __shared_ptr<_Tp1, _Lp1>&) noexcept;
1398 element_type* _M_ptr;
1399 __shared_count<_Lp> _M_refcount;
1404 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1406 operator==(
const __shared_ptr<_Tp1, _Lp>& __a,
1407 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1408 {
return __a.get() == __b.get(); }
1410 template<
typename _Tp, _Lock_policy _Lp>
1412 operator==(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1415 template<
typename _Tp, _Lock_policy _Lp>
1417 operator==(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1420 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1422 operator!=(
const __shared_ptr<_Tp1, _Lp>& __a,
1423 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1424 {
return __a.get() != __b.get(); }
1426 template<
typename _Tp, _Lock_policy _Lp>
1428 operator!=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1429 {
return (
bool)__a; }
1431 template<
typename _Tp, _Lock_policy _Lp>
1433 operator!=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1434 {
return (
bool)__a; }
1436 template<
typename _Tp,
typename _Up, _Lock_policy _Lp>
1438 operator<(const __shared_ptr<_Tp, _Lp>& __a,
1439 const __shared_ptr<_Up, _Lp>& __b) noexcept
1441 using _Tp_elt =
typename __shared_ptr<_Tp, _Lp>::element_type;
1442 using _Up_elt =
typename __shared_ptr<_Up, _Lp>::element_type;
1443 using _Vp =
typename common_type<_Tp_elt*, _Up_elt*>::type;
1444 return less<_Vp>()(__a.get(), __b.get());
1447 template<
typename _Tp, _Lock_policy _Lp>
1449 operator<(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1451 using _Tp_elt =
typename __shared_ptr<_Tp, _Lp>::element_type;
1455 template<
typename _Tp, _Lock_policy _Lp>
1457 operator<(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1459 using _Tp_elt =
typename __shared_ptr<_Tp, _Lp>::element_type;
1463 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1465 operator<=(const __shared_ptr<_Tp1, _Lp>& __a,
1466 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1467 {
return !(__b < __a); }
1469 template<
typename _Tp, _Lock_policy _Lp>
1471 operator<=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1472 {
return !(
nullptr < __a); }
1474 template<
typename _Tp, _Lock_policy _Lp>
1476 operator<=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1477 {
return !(__a <
nullptr); }
1479 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1481 operator>(
const __shared_ptr<_Tp1, _Lp>& __a,
1482 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1483 {
return (__b < __a); }
1485 template<
typename _Tp, _Lock_policy _Lp>
1487 operator>(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1488 {
return nullptr < __a; }
1490 template<
typename _Tp, _Lock_policy _Lp>
1492 operator>(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1493 {
return __a <
nullptr; }
1495 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1497 operator>=(
const __shared_ptr<_Tp1, _Lp>& __a,
1498 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1499 {
return !(__a < __b); }
1501 template<
typename _Tp, _Lock_policy _Lp>
1503 operator>=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1504 {
return !(__a <
nullptr); }
1506 template<
typename _Tp, _Lock_policy _Lp>
1508 operator>=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1509 {
return !(
nullptr < __a); }
1511 template<
typename _Sp>
1515 operator()(
const _Sp& __lhs,
const _Sp& __rhs)
const noexcept
1517 typedef typename _Sp::element_type element_type;
1522 template<
typename _Tp, _Lock_policy _Lp>
1523 struct less<__shared_ptr<_Tp, _Lp>>
1524 :
public _Sp_less<__shared_ptr<_Tp, _Lp>>
1528 template<
typename _Tp, _Lock_policy _Lp>
1530 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b) noexcept
1540 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1541 inline __shared_ptr<_Tp, _Lp>
1542 static_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1544 using _Sp = __shared_ptr<_Tp, _Lp>;
1545 return _Sp(__r, static_cast<typename _Sp::element_type*>(__r.get()));
1553 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1554 inline __shared_ptr<_Tp, _Lp>
1555 const_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1557 using _Sp = __shared_ptr<_Tp, _Lp>;
1558 return _Sp(__r, const_cast<typename _Sp::element_type*>(__r.get()));
1566 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1567 inline __shared_ptr<_Tp, _Lp>
1568 dynamic_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1570 using _Sp = __shared_ptr<_Tp, _Lp>;
1571 if (
auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get()))
1572 return _Sp(__r, __p);
1576 #if __cplusplus > 201402L 1577 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1578 inline __shared_ptr<_Tp, _Lp>
1579 reinterpret_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1581 using _Sp = __shared_ptr<_Tp, _Lp>;
1582 return _Sp(__r, reinterpret_cast<typename _Sp::element_type*>(__r.get()));
1586 template<
typename _Tp, _Lock_policy _Lp>
1589 template<
typename _Yp,
typename _Res =
void>
1590 using _Compatible =
typename 1591 enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type;
1594 template<
typename _Yp>
1595 using _Assignable = _Compatible<_Yp, __weak_ptr&>;
1598 using element_type =
typename remove_extent<_Tp>::type;
1600 constexpr __weak_ptr() noexcept
1601 : _M_ptr(
nullptr), _M_refcount()
1604 __weak_ptr(
const __weak_ptr&) noexcept =
default;
1606 ~__weak_ptr() =
default;
1622 template<
typename _Yp,
typename = _Compatible<_Yp>>
1623 __weak_ptr(
const __weak_ptr<_Yp, _Lp>& __r) noexcept
1624 : _M_refcount(__r._M_refcount)
1625 { _M_ptr = __r.lock().get(); }
1627 template<
typename _Yp,
typename = _Compatible<_Yp>>
1628 __weak_ptr(
const __shared_ptr<_Yp, _Lp>& __r) noexcept
1629 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1632 __weak_ptr(__weak_ptr&& __r) noexcept
1633 : _M_ptr(__r._M_ptr), _M_refcount(std::move(__r._M_refcount))
1634 { __r._M_ptr =
nullptr; }
1636 template<
typename _Yp,
typename = _Compatible<_Yp>>
1637 __weak_ptr(__weak_ptr<_Yp, _Lp>&& __r) noexcept
1638 : _M_ptr(__r.lock().get()), _M_refcount(std::move(__r._M_refcount))
1639 { __r._M_ptr =
nullptr; }
1642 operator=(
const __weak_ptr& __r) noexcept =
default;
1644 template<
typename _Yp>
1646 operator=(
const __weak_ptr<_Yp, _Lp>& __r) noexcept
1648 _M_ptr = __r.lock().get();
1649 _M_refcount = __r._M_refcount;
1653 template<
typename _Yp>
1655 operator=(
const __shared_ptr<_Yp, _Lp>& __r) noexcept
1657 _M_ptr = __r._M_ptr;
1658 _M_refcount = __r._M_refcount;
1663 operator=(__weak_ptr&& __r) noexcept
1665 _M_ptr = __r._M_ptr;
1666 _M_refcount = std::move(__r._M_refcount);
1667 __r._M_ptr =
nullptr;
1671 template<
typename _Yp>
1673 operator=(__weak_ptr<_Yp, _Lp>&& __r) noexcept
1675 _M_ptr = __r.lock().get();
1676 _M_refcount = std::move(__r._M_refcount);
1677 __r._M_ptr =
nullptr;
1681 __shared_ptr<_Tp, _Lp>
1682 lock()
const noexcept
1683 {
return __shared_ptr<element_type, _Lp>(*
this, std::nothrow); }
1686 use_count()
const noexcept
1687 {
return _M_refcount._M_get_use_count(); }
1690 expired()
const noexcept
1691 {
return _M_refcount._M_get_use_count() == 0; }
1693 template<
typename _Tp1>
1695 owner_before(
const __shared_ptr<_Tp1, _Lp>& __rhs)
const 1696 {
return _M_refcount._M_less(__rhs._M_refcount); }
1698 template<
typename _Tp1>
1700 owner_before(
const __weak_ptr<_Tp1, _Lp>& __rhs)
const 1701 {
return _M_refcount._M_less(__rhs._M_refcount); }
1705 { __weak_ptr().swap(*
this); }
1708 swap(__weak_ptr& __s) noexcept
1710 std::swap(_M_ptr, __s._M_ptr);
1711 _M_refcount._M_swap(__s._M_refcount);
1717 _M_assign(_Tp* __ptr,
const __shared_count<_Lp>& __refcount) noexcept
1719 if (use_count() == 0)
1722 _M_refcount = __refcount;
1726 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
1727 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
1728 friend class __enable_shared_from_this<_Tp, _Lp>;
1731 element_type* _M_ptr;
1732 __weak_count<_Lp> _M_refcount;
1736 template<
typename _Tp, _Lock_policy _Lp>
1738 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b) noexcept
1741 template<
typename _Tp,
typename _Tp1>
1745 operator()(
const _Tp& __lhs,
const _Tp& __rhs)
const 1746 {
return __lhs.owner_before(__rhs); }
1749 operator()(
const _Tp& __lhs,
const _Tp1& __rhs)
const 1750 {
return __lhs.owner_before(__rhs); }
1753 operator()(
const _Tp1& __lhs,
const _Tp& __rhs)
const 1754 {
return __lhs.owner_before(__rhs); }
1758 struct _Sp_owner_less<void, void>
1760 template<
typename _Tp,
typename _Up>
1762 operator()(
const _Tp& __lhs,
const _Up& __rhs)
const 1763 -> decltype(__lhs.owner_before(__rhs))
1764 {
return __lhs.owner_before(__rhs); }
1766 using is_transparent = void;
1769 template<
typename _Tp, _Lock_policy _Lp>
1771 :
public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
1774 template<
typename _Tp, _Lock_policy _Lp>
1776 :
public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
1780 template<
typename _Tp, _Lock_policy _Lp>
1781 class __enable_shared_from_this
1784 constexpr __enable_shared_from_this() noexcept { }
1786 __enable_shared_from_this(
const __enable_shared_from_this&) noexcept { }
1788 __enable_shared_from_this&
1789 operator=(
const __enable_shared_from_this&) noexcept
1792 ~__enable_shared_from_this() { }
1795 __shared_ptr<_Tp, _Lp>
1797 {
return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
1799 __shared_ptr<const _Tp, _Lp>
1800 shared_from_this()
const 1801 {
return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
1803 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 1804 __weak_ptr<_Tp, _Lp>
1805 weak_from_this() noexcept
1806 {
return this->_M_weak_this; }
1808 __weak_ptr<const _Tp, _Lp>
1809 weak_from_this()
const noexcept
1810 {
return this->_M_weak_this; }
1814 template<
typename _Tp1>
1816 _M_weak_assign(_Tp1* __p,
const __shared_count<_Lp>& __n)
const noexcept
1817 { _M_weak_this._M_assign(__p, __n); }
1820 __enable_shared_from_this_base(
const __shared_count<_Lp>&,
1821 const __enable_shared_from_this* __p)
1824 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
1827 template<
typename _Tp, _Lock_policy _Lp,
typename _Alloc,
typename... _Args>
1828 inline __shared_ptr<_Tp, _Lp>
1829 __allocate_shared(
const _Alloc& __a, _Args&&... __args)
1831 return __shared_ptr<_Tp, _Lp>(_Sp_make_shared_tag(), __a,
1832 std::forward<_Args>(__args)...);
1835 template<
typename _Tp, _Lock_policy _Lp,
typename... _Args>
1836 inline __shared_ptr<_Tp, _Lp>
1837 __make_shared(_Args&&... __args)
1839 typedef typename std::remove_const<_Tp>::type _Tp_nc;
1841 std::forward<_Args>(__args)...);
1845 template<
typename _Tp, _Lock_policy _Lp>
1846 struct hash<__shared_ptr<_Tp, _Lp>>
1847 :
public __hash_base<size_t, __shared_ptr<_Tp, _Lp>>
1850 operator()(
const __shared_ptr<_Tp, _Lp>& __s)
const noexcept
1857 _GLIBCXX_END_NAMESPACE_VERSION
1860 #endif // _SHARED_PTR_BASE_H
Primary class template hash.
Uniform interface to all allocator types.
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
ISO C++ entities toplevel namespace is std.
static void destroy(_Alloc &__a, _Tp *__p)
Destroy an object of type _Tp.
allocator<void> specialization.
20.7.1.2 unique_ptr for single objects.
complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
Primary class template for reference_wrapper.
Exception possibly thrown by shared_ptr.
virtual char const * what() const noexcept
void lock(_L1 &__l1, _L2 &__l2, _L3 &... __l3)
Generic lock.
Base class for all library exceptions.
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.
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.
One of the comparison functors.
A simple smart pointer providing strict ownership semantics.
Non-standard RAII type for managing pointers obtained from allocators.
_Del * get_deleter(const __shared_ptr< _Tp, _Lp > &__p) noexcept
20.7.2.2.10 shared_ptr get_deleter
A smart pointer with weak semantics.
The standard allocator, as per [20.4].