49 #ifndef _SHARED_PTR_BASE_H 50 #define _SHARED_PTR_BASE_H 1 56 namespace std _GLIBCXX_VISIBILITY(default)
58 _GLIBCXX_BEGIN_NAMESPACE_VERSION
60 #if _GLIBCXX_USE_DEPRECATED 61 template<
typename>
class auto_ptr;
71 virtual char const*
what()
const noexcept;
78 __throw_bad_weak_ptr()
81 using __gnu_cxx::_Lock_policy;
82 using __gnu_cxx::__default_lock_policy;
83 using __gnu_cxx::_S_single;
84 using __gnu_cxx::_S_mutex;
85 using __gnu_cxx::_S_atomic;
88 template<_Lock_policy _Lp>
93 enum { _S_need_barriers = 0 };
97 class _Mutex_base<_S_mutex>
98 :
public __gnu_cxx::__mutex
104 enum { _S_need_barriers = 1 };
107 template<_Lock_policy _Lp = __default_lock_policy>
108 class _Sp_counted_base
109 :
public _Mutex_base<_Lp>
112 _Sp_counted_base() noexcept
113 : _M_use_count(1), _M_weak_count(1) { }
116 ~_Sp_counted_base() noexcept
122 _M_dispose() noexcept = 0;
126 _M_destroy() noexcept
134 { __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); }
140 _M_add_ref_lock_nothrow();
143 _M_release() noexcept
146 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count);
147 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
149 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count);
155 if (_Mutex_base<_Lp>::_S_need_barriers)
157 __atomic_thread_fence (__ATOMIC_ACQ_REL);
161 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
162 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count,
165 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
172 _M_weak_add_ref() noexcept
173 { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); }
176 _M_weak_release() noexcept
179 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
180 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1)
182 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
183 if (_Mutex_base<_Lp>::_S_need_barriers)
187 __atomic_thread_fence (__ATOMIC_ACQ_REL);
194 _M_get_use_count()
const noexcept
198 return __atomic_load_n(&_M_use_count, __ATOMIC_RELAXED);
202 _Sp_counted_base(_Sp_counted_base
const&) =
delete;
203 _Sp_counted_base& operator=(_Sp_counted_base
const&) =
delete;
205 _Atomic_word _M_use_count;
206 _Atomic_word _M_weak_count;
211 _Sp_counted_base<_S_single>::
214 if (_M_use_count == 0)
215 __throw_bad_weak_ptr();
221 _Sp_counted_base<_S_mutex>::
225 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
228 __throw_bad_weak_ptr();
234 _Sp_counted_base<_S_atomic>::
238 _Atomic_word __count = _M_get_use_count();
242 __throw_bad_weak_ptr();
246 while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1,
247 true, __ATOMIC_ACQ_REL,
253 _Sp_counted_base<_S_single>::
254 _M_add_ref_lock_nothrow()
256 if (_M_use_count == 0)
264 _Sp_counted_base<_S_mutex>::
265 _M_add_ref_lock_nothrow()
268 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
278 _Sp_counted_base<_S_atomic>::
279 _M_add_ref_lock_nothrow()
282 _Atomic_word __count = _M_get_use_count();
290 while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1,
291 true, __ATOMIC_ACQ_REL,
298 _Sp_counted_base<_S_single>::_M_add_ref_copy()
303 _Sp_counted_base<_S_single>::_M_release() noexcept
305 if (--_M_use_count == 0)
308 if (--_M_weak_count == 0)
315 _Sp_counted_base<_S_single>::_M_weak_add_ref() noexcept
320 _Sp_counted_base<_S_single>::_M_weak_release() noexcept
322 if (--_M_weak_count == 0)
328 _Sp_counted_base<_S_single>::_M_get_use_count()
const noexcept
329 {
return _M_use_count; }
333 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
336 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
339 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
340 class __enable_shared_from_this;
342 template<
typename _Tp>
345 template<
typename _Tp>
348 template<
typename _Tp>
351 template<
typename _Tp>
354 template<_Lock_policy _Lp = __default_lock_policy>
357 template<_Lock_policy _Lp = __default_lock_policy>
358 class __shared_count;
362 template<
typename _Ptr, _Lock_policy _Lp>
363 class _Sp_counted_ptr final :
public _Sp_counted_base<_Lp>
367 _Sp_counted_ptr(_Ptr __p) noexcept
371 _M_dispose() noexcept
375 _M_destroy() noexcept
382 _Sp_counted_ptr(
const _Sp_counted_ptr&) =
delete;
383 _Sp_counted_ptr& operator=(
const _Sp_counted_ptr&) =
delete;
391 _Sp_counted_ptr<nullptr_t, _S_single>::_M_dispose() noexcept { }
395 _Sp_counted_ptr<nullptr_t, _S_mutex>::_M_dispose() noexcept { }
399 _Sp_counted_ptr<nullptr_t, _S_atomic>::_M_dispose() noexcept { }
401 template<
int _Nm,
typename _Tp,
402 bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
403 struct _Sp_ebo_helper;
406 template<
int _Nm,
typename _Tp>
407 struct _Sp_ebo_helper<_Nm, _Tp, true> :
private _Tp
409 explicit _Sp_ebo_helper(
const _Tp& __tp) : _Tp(__tp) { }
412 _S_get(_Sp_ebo_helper& __eboh) {
return static_cast<_Tp&
>(__eboh); }
416 template<
int _Nm,
typename _Tp>
417 struct _Sp_ebo_helper<_Nm, _Tp, false>
419 explicit _Sp_ebo_helper(
const _Tp& __tp) : _M_tp(__tp) { }
422 _S_get(_Sp_ebo_helper& __eboh)
423 {
return __eboh._M_tp; }
430 template<
typename _Ptr,
typename _Deleter,
typename _Alloc, _Lock_policy _Lp>
431 class _Sp_counted_deleter final :
public _Sp_counted_base<_Lp>
433 class _Impl : _Sp_ebo_helper<0, _Deleter>, _Sp_ebo_helper<1, _Alloc>
435 typedef _Sp_ebo_helper<0, _Deleter> _Del_base;
436 typedef _Sp_ebo_helper<1, _Alloc> _Alloc_base;
439 _Impl(_Ptr __p, _Deleter __d,
const _Alloc& __a) noexcept
440 : _M_ptr(__p), _Del_base(__d), _Alloc_base(__a)
443 _Deleter& _M_del() noexcept {
return _Del_base::_S_get(*
this); }
444 _Alloc& _M_alloc() noexcept {
return _Alloc_base::_S_get(*
this); }
450 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_deleter>;
453 _Sp_counted_deleter(_Ptr __p, _Deleter __d) noexcept
454 : _M_impl(__p, __d, _Alloc()) { }
457 _Sp_counted_deleter(_Ptr __p, _Deleter __d,
const _Alloc& __a) noexcept
458 : _M_impl(__p, __d, __a) { }
460 ~_Sp_counted_deleter() noexcept { }
463 _M_dispose() noexcept
464 { _M_impl._M_del()(_M_impl._M_ptr); }
467 _M_destroy() noexcept
469 __allocator_type __a(_M_impl._M_alloc());
471 this->~_Sp_counted_deleter();
480 return __ti ==
typeid(_Deleter)
494 struct _Sp_make_shared_tag { };
496 template<
typename _Tp,
typename _Alloc, _Lock_policy _Lp>
497 class _Sp_counted_ptr_inplace final :
public _Sp_counted_base<_Lp>
499 class _Impl : _Sp_ebo_helper<0, _Alloc>
501 typedef _Sp_ebo_helper<0, _Alloc> _A_base;
504 explicit _Impl(_Alloc __a) noexcept : _A_base(__a) { }
506 _Alloc& _M_alloc() noexcept {
return _A_base::_S_get(*
this); }
508 __gnu_cxx::__aligned_buffer<_Tp> _M_storage;
512 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>;
514 template<
typename... _Args>
515 _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
521 std::forward<_Args>(__args)...);
524 ~_Sp_counted_ptr_inplace() noexcept { }
527 _M_dispose() noexcept
534 _M_destroy() noexcept
536 __allocator_type __a(_M_impl._M_alloc());
538 this->~_Sp_counted_ptr_inplace();
546 if (__ti ==
typeid(_Sp_make_shared_tag))
547 return const_cast<typename remove_cv<_Tp>::type*
>(_M_ptr());
553 _Tp* _M_ptr() noexcept {
return _M_impl._M_storage._M_ptr(); }
559 template<_Lock_policy _Lp>
563 constexpr __shared_count() noexcept : _M_pi(0)
566 template<
typename _Ptr>
568 __shared_count(_Ptr __p) : _M_pi(0)
572 _M_pi =
new _Sp_counted_ptr<_Ptr, _Lp>(__p);
577 __throw_exception_again;
581 template<
typename _Ptr,
typename _Deleter>
582 __shared_count(_Ptr __p, _Deleter __d)
586 template<
typename _Ptr,
typename _Deleter,
typename _Alloc>
587 __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
589 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
592 typename _Sp_cd_type::__allocator_type __a2(__a);
594 _Sp_cd_type* __mem = __guard.get();
595 ::new (__mem) _Sp_cd_type(__p, std::move(__d), std::move(__a));
602 __throw_exception_again;
606 template<
typename _Tp,
typename _Alloc,
typename... _Args>
607 __shared_count(_Sp_make_shared_tag, _Tp*,
const _Alloc& __a,
611 typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type;
612 typename _Sp_cp_type::__allocator_type __a2(__a);
614 _Sp_cp_type* __mem = __guard.get();
615 ::new (__mem) _Sp_cp_type(std::move(__a),
616 std::forward<_Args>(__args)...);
621 #if _GLIBCXX_USE_DEPRECATED 623 template<
typename _Tp>
629 template<
typename _Tp,
typename _Del>
635 if (__r.get() ==
nullptr)
638 using _Ptr =
typename unique_ptr<_Tp, _Del>::pointer;
639 using _Del2 =
typename conditional<is_reference<_Del>::value,
643 = _Sp_counted_deleter<_Ptr, _Del2, allocator<void>, _Lp>;
647 _Sp_cd_type* __mem = _Alloc_traits::allocate(__a, 1);
648 _Alloc_traits::construct(__a, __mem, __r.release(),
654 explicit __shared_count(
const __weak_count<_Lp>& __r);
657 explicit __shared_count(
const __weak_count<_Lp>& __r, std::nothrow_t);
659 ~__shared_count() noexcept
661 if (_M_pi !=
nullptr)
665 __shared_count(
const __shared_count& __r) noexcept
669 _M_pi->_M_add_ref_copy();
673 operator=(
const __shared_count& __r) noexcept
675 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
679 __tmp->_M_add_ref_copy();
688 _M_swap(__shared_count& __r) noexcept
690 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
696 _M_get_use_count()
const noexcept
697 {
return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
700 _M_unique()
const noexcept
701 {
return this->_M_get_use_count() == 1; }
705 {
return _M_pi ? _M_pi->_M_get_deleter(__ti) :
nullptr; }
708 _M_less(
const __shared_count& __rhs)
const noexcept
712 _M_less(
const __weak_count<_Lp>& __rhs)
const noexcept
717 operator==(
const __shared_count& __a,
const __shared_count& __b) noexcept
718 {
return __a._M_pi == __b._M_pi; }
721 friend class __weak_count<_Lp>;
723 _Sp_counted_base<_Lp>* _M_pi;
727 template<_Lock_policy _Lp>
731 constexpr __weak_count() noexcept : _M_pi(
nullptr)
734 __weak_count(
const __shared_count<_Lp>& __r) noexcept
737 if (_M_pi !=
nullptr)
738 _M_pi->_M_weak_add_ref();
741 __weak_count(
const __weak_count& __r) noexcept
744 if (_M_pi !=
nullptr)
745 _M_pi->_M_weak_add_ref();
748 __weak_count(__weak_count&& __r) noexcept
750 { __r._M_pi =
nullptr; }
752 ~__weak_count() noexcept
754 if (_M_pi !=
nullptr)
755 _M_pi->_M_weak_release();
759 operator=(
const __shared_count<_Lp>& __r) noexcept
761 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
762 if (__tmp !=
nullptr)
763 __tmp->_M_weak_add_ref();
764 if (_M_pi !=
nullptr)
765 _M_pi->_M_weak_release();
771 operator=(
const __weak_count& __r) noexcept
773 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
774 if (__tmp !=
nullptr)
775 __tmp->_M_weak_add_ref();
776 if (_M_pi !=
nullptr)
777 _M_pi->_M_weak_release();
783 operator=(__weak_count&& __r) noexcept
785 if (_M_pi !=
nullptr)
786 _M_pi->_M_weak_release();
793 _M_swap(__weak_count& __r) noexcept
795 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
801 _M_get_use_count()
const noexcept
802 {
return _M_pi !=
nullptr ? _M_pi->_M_get_use_count() : 0; }
805 _M_less(
const __weak_count& __rhs)
const noexcept
809 _M_less(
const __shared_count<_Lp>& __rhs)
const noexcept
814 operator==(
const __weak_count& __a,
const __weak_count& __b) noexcept
815 {
return __a._M_pi == __b._M_pi; }
818 friend class __shared_count<_Lp>;
820 _Sp_counted_base<_Lp>* _M_pi;
824 template<_Lock_policy _Lp>
826 __shared_count<_Lp>::__shared_count(
const __weak_count<_Lp>& __r)
829 if (_M_pi !=
nullptr)
830 _M_pi->_M_add_ref_lock();
832 __throw_bad_weak_ptr();
836 template<_Lock_policy _Lp>
838 __shared_count<_Lp>::
839 __shared_count(
const __weak_count<_Lp>& __r, std::nothrow_t)
842 if (_M_pi !=
nullptr)
843 if (!_M_pi->_M_add_ref_lock_nothrow())
850 template<_Lock_policy _Lp,
typename _Tp1,
typename _Tp2>
852 __enable_shared_from_this_helper(
const __shared_count<_Lp>&,
853 const __enable_shared_from_this<_Tp1,
854 _Lp>*,
const _Tp2*) noexcept;
857 template<
typename _Tp1,
typename _Tp2>
859 __enable_shared_from_this_helper(
const __shared_count<>&,
861 const _Tp2*) noexcept;
863 template<_Lock_policy _Lp>
865 __enable_shared_from_this_helper(
const __shared_count<_Lp>&, ...) noexcept
869 template<
typename _Tp, _Lock_policy _Lp>
872 template<
typename _Ptr>
874 =
typename enable_if<is_convertible<_Ptr, _Tp*>::value>::type;
877 typedef _Tp element_type;
879 constexpr __shared_ptr() noexcept
880 : _M_ptr(0), _M_refcount()
883 template<
typename _Tp1>
884 explicit __shared_ptr(_Tp1* __p)
885 : _M_ptr(__p), _M_refcount(__p)
887 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
889 static_assert(
sizeof(_Tp1) > 0,
"incomplete type" );
890 __enable_shared_from_this_helper(_M_refcount, __p, __p);
893 template<
typename _Tp1,
typename _Deleter>
894 __shared_ptr(_Tp1* __p, _Deleter __d)
895 : _M_ptr(__p), _M_refcount(__p, __d)
897 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
899 __enable_shared_from_this_helper(_M_refcount, __p, __p);
902 template<
typename _Tp1,
typename _Deleter,
typename _Alloc>
903 __shared_ptr(_Tp1* __p, _Deleter __d, _Alloc __a)
904 : _M_ptr(__p), _M_refcount(__p, __d, std::move(__a))
906 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
908 __enable_shared_from_this_helper(_M_refcount, __p, __p);
911 template<
typename _Deleter>
912 __shared_ptr(nullptr_t __p, _Deleter __d)
913 : _M_ptr(0), _M_refcount(__p, __d)
916 template<
typename _Deleter,
typename _Alloc>
917 __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
918 : _M_ptr(0), _M_refcount(__p, __d, std::move(__a))
921 template<
typename _Tp1>
922 __shared_ptr(
const __shared_ptr<_Tp1, _Lp>& __r, _Tp* __p) noexcept
923 : _M_ptr(__p), _M_refcount(__r._M_refcount)
926 __shared_ptr(
const __shared_ptr&) noexcept =
default;
927 __shared_ptr& operator=(
const __shared_ptr&) noexcept =
default;
928 ~__shared_ptr() =
default;
930 template<
typename _Tp1,
typename = _Convertible<_Tp1*>>
931 __shared_ptr(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
932 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
935 __shared_ptr(__shared_ptr&& __r) noexcept
936 : _M_ptr(__r._M_ptr), _M_refcount()
938 _M_refcount._M_swap(__r._M_refcount);
942 template<
typename _Tp1,
typename = _Convertible<_Tp1*>>
943 __shared_ptr(__shared_ptr<_Tp1, _Lp>&& __r) noexcept
944 : _M_ptr(__r._M_ptr), _M_refcount()
946 _M_refcount._M_swap(__r._M_refcount);
950 template<
typename _Tp1>
951 explicit __shared_ptr(
const __weak_ptr<_Tp1, _Lp>& __r)
952 : _M_refcount(__r._M_refcount)
954 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
962 template<
typename _Tp1,
typename _Del,
typename 963 = _Convertible<typename unique_ptr<_Tp1, _Del>::pointer>>
965 : _M_ptr(__r.get()), _M_refcount()
967 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
968 auto __raw = _S_raw_ptr(__r.get());
969 _M_refcount = __shared_count<_Lp>(std::move(__r));
970 __enable_shared_from_this_helper(_M_refcount, __raw, __raw);
973 #if _GLIBCXX_USE_DEPRECATED 975 template<
typename _Tp1>
979 constexpr __shared_ptr(nullptr_t) noexcept : __shared_ptr() { }
981 template<
typename _Tp1>
983 operator=(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
986 _M_refcount = __r._M_refcount;
990 #if _GLIBCXX_USE_DEPRECATED 991 template<
typename _Tp1>
995 __shared_ptr(std::move(__r)).swap(*
this);
1001 operator=(__shared_ptr&& __r) noexcept
1003 __shared_ptr(std::move(__r)).swap(*
this);
1007 template<
class _Tp1>
1009 operator=(__shared_ptr<_Tp1, _Lp>&& __r) noexcept
1011 __shared_ptr(std::move(__r)).swap(*
this);
1015 template<
typename _Tp1,
typename _Del>
1019 __shared_ptr(std::move(__r)).swap(*
this);
1025 { __shared_ptr().swap(*
this); }
1027 template<
typename _Tp1>
1032 __glibcxx_assert(__p == 0 || __p != _M_ptr);
1033 __shared_ptr(__p).swap(*
this);
1036 template<
typename _Tp1,
typename _Deleter>
1038 reset(_Tp1* __p, _Deleter __d)
1039 { __shared_ptr(__p, __d).swap(*
this); }
1041 template<
typename _Tp1,
typename _Deleter,
typename _Alloc>
1043 reset(_Tp1* __p, _Deleter __d, _Alloc __a)
1044 { __shared_ptr(__p, __d, std::move(__a)).swap(*
this); }
1047 typename std::add_lvalue_reference<_Tp>::type
1050 __glibcxx_assert(_M_ptr != 0);
1055 operator->()
const noexcept
1057 _GLIBCXX_DEBUG_PEDASSERT(_M_ptr != 0);
1062 get()
const noexcept
1065 explicit operator bool()
const 1066 {
return _M_ptr == 0 ?
false :
true; }
1069 unique()
const noexcept
1070 {
return _M_refcount._M_unique(); }
1073 use_count()
const noexcept
1074 {
return _M_refcount._M_get_use_count(); }
1077 swap(__shared_ptr<_Tp, _Lp>& __other) noexcept
1079 std::swap(_M_ptr, __other._M_ptr);
1080 _M_refcount._M_swap(__other._M_refcount);
1083 template<
typename _Tp1>
1085 owner_before(__shared_ptr<_Tp1, _Lp>
const& __rhs)
const 1086 {
return _M_refcount._M_less(__rhs._M_refcount); }
1088 template<
typename _Tp1>
1090 owner_before(__weak_ptr<_Tp1, _Lp>
const& __rhs)
const 1091 {
return _M_refcount._M_less(__rhs._M_refcount); }
1096 template<
typename _Alloc,
typename... _Args>
1097 __shared_ptr(_Sp_make_shared_tag __tag,
const _Alloc& __a,
1099 : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a,
1100 std::forward<_Args>(__args)...)
1104 void* __p = _M_refcount._M_get_deleter(
typeid(__tag));
1105 _M_ptr =
static_cast<_Tp*
>(__p);
1106 __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
1109 template<
typename _Alloc>
1112 void operator()(
typename _Alloc::value_type* __ptr)
1120 template<
typename _Alloc,
typename... _Args>
1121 __shared_ptr(_Sp_make_shared_tag __tag,
const _Alloc& __a,
1123 : _M_ptr(), _M_refcount()
1126 rebind_traits<typename std::remove_cv<_Tp>::type> __traits;
1127 _Deleter<typename __traits::allocator_type> __del = { __a };
1129 auto __ptr = __guard.get();
1132 __traits::construct(__del._M_alloc, __ptr,
1133 std::forward<_Args>(__args)...);
1135 __shared_count<_Lp> __count(__ptr, __del, __del._M_alloc);
1136 _M_refcount._M_swap(__count);
1138 __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
1142 template<
typename _Tp1, _Lock_policy _Lp1,
typename _Alloc,
1144 friend __shared_ptr<_Tp1, _Lp1>
1145 __allocate_shared(
const _Alloc& __a, _Args&&... __args);
1149 __shared_ptr(
const __weak_ptr<_Tp, _Lp>& __r, std::nothrow_t)
1150 : _M_refcount(__r._M_refcount, std::nothrow)
1152 _M_ptr = _M_refcount._M_get_use_count() ? __r._M_ptr :
nullptr;
1155 friend class __weak_ptr<_Tp, _Lp>;
1160 {
return _M_refcount._M_get_deleter(__ti); }
1162 template<
typename _Tp1>
1164 _S_raw_ptr(_Tp1* __ptr)
1167 template<
typename _Tp1>
1172 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
1173 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
1175 template<
typename _Del,
typename _Tp1, _Lock_policy _Lp1>
1176 friend _Del*
get_deleter(
const __shared_ptr<_Tp1, _Lp1>&) noexcept;
1179 __shared_count<_Lp> _M_refcount;
1184 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1186 operator==(
const __shared_ptr<_Tp1, _Lp>& __a,
1187 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1188 {
return __a.get() == __b.get(); }
1190 template<
typename _Tp, _Lock_policy _Lp>
1192 operator==(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1195 template<
typename _Tp, _Lock_policy _Lp>
1197 operator==(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1200 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1202 operator!=(
const __shared_ptr<_Tp1, _Lp>& __a,
1203 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1204 {
return __a.get() != __b.get(); }
1206 template<
typename _Tp, _Lock_policy _Lp>
1208 operator!=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1209 {
return (
bool)__a; }
1211 template<
typename _Tp, _Lock_policy _Lp>
1213 operator!=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1214 {
return (
bool)__a; }
1216 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1218 operator<(const __shared_ptr<_Tp1, _Lp>& __a,
1219 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1221 typedef typename std::common_type<_Tp1*, _Tp2*>::type _CT;
1225 template<
typename _Tp, _Lock_policy _Lp>
1227 operator<(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1230 template<
typename _Tp, _Lock_policy _Lp>
1232 operator<(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1235 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1237 operator<=(const __shared_ptr<_Tp1, _Lp>& __a,
1238 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1239 {
return !(__b < __a); }
1241 template<
typename _Tp, _Lock_policy _Lp>
1243 operator<=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1244 {
return !(
nullptr < __a); }
1246 template<
typename _Tp, _Lock_policy _Lp>
1248 operator<=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1249 {
return !(__a <
nullptr); }
1251 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1253 operator>(
const __shared_ptr<_Tp1, _Lp>& __a,
1254 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1255 {
return (__b < __a); }
1257 template<
typename _Tp, _Lock_policy _Lp>
1259 operator>(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1262 template<
typename _Tp, _Lock_policy _Lp>
1264 operator>(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1267 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1269 operator>=(
const __shared_ptr<_Tp1, _Lp>& __a,
1270 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1271 {
return !(__a < __b); }
1273 template<
typename _Tp, _Lock_policy _Lp>
1275 operator>=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1276 {
return !(__a <
nullptr); }
1278 template<
typename _Tp, _Lock_policy _Lp>
1280 operator>=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1281 {
return !(
nullptr < __a); }
1283 template<
typename _Sp>
1287 operator()(
const _Sp& __lhs,
const _Sp& __rhs)
const noexcept
1289 typedef typename _Sp::element_type element_type;
1294 template<
typename _Tp, _Lock_policy _Lp>
1295 struct less<__shared_ptr<_Tp, _Lp>>
1296 :
public _Sp_less<__shared_ptr<_Tp, _Lp>>
1300 template<
typename _Tp, _Lock_policy _Lp>
1302 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b) noexcept
1312 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1313 inline __shared_ptr<_Tp, _Lp>
1314 static_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1315 {
return __shared_ptr<_Tp, _Lp>(__r,
static_cast<_Tp*
>(__r.get())); }
1322 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1323 inline __shared_ptr<_Tp, _Lp>
1324 const_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1325 {
return __shared_ptr<_Tp, _Lp>(__r,
const_cast<_Tp*
>(__r.get())); }
1332 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1333 inline __shared_ptr<_Tp, _Lp>
1334 dynamic_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1336 if (_Tp* __p = dynamic_cast<_Tp*>(__r.get()))
1337 return __shared_ptr<_Tp, _Lp>(__r, __p);
1338 return __shared_ptr<_Tp, _Lp>();
1342 template<
typename _Tp, _Lock_policy _Lp>
1345 template<
typename _Ptr>
1347 =
typename enable_if<is_convertible<_Ptr, _Tp*>::value>::type;
1350 typedef _Tp element_type;
1352 constexpr __weak_ptr() noexcept
1353 : _M_ptr(
nullptr), _M_refcount()
1356 __weak_ptr(
const __weak_ptr&) noexcept =
default;
1358 ~__weak_ptr() =
default;
1374 template<
typename _Tp1,
typename = _Convertible<_Tp1*>>
1375 __weak_ptr(
const __weak_ptr<_Tp1, _Lp>& __r) noexcept
1376 : _M_refcount(__r._M_refcount)
1377 { _M_ptr = __r.lock().get(); }
1379 template<
typename _Tp1,
typename = _Convertible<_Tp1*>>
1380 __weak_ptr(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1381 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1384 __weak_ptr(__weak_ptr&& __r) noexcept
1385 : _M_ptr(__r._M_ptr), _M_refcount(std::move(__r._M_refcount))
1386 { __r._M_ptr =
nullptr; }
1388 template<
typename _Tp1,
typename = _Convertible<_Tp1*>>
1389 __weak_ptr(__weak_ptr<_Tp1, _Lp>&& __r) noexcept
1390 : _M_ptr(__r.lock().get()), _M_refcount(std::move(__r._M_refcount))
1391 { __r._M_ptr =
nullptr; }
1394 operator=(
const __weak_ptr& __r) noexcept =
default;
1396 template<
typename _Tp1>
1398 operator=(
const __weak_ptr<_Tp1, _Lp>& __r) noexcept
1400 _M_ptr = __r.lock().get();
1401 _M_refcount = __r._M_refcount;
1405 template<
typename _Tp1>
1407 operator=(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1409 _M_ptr = __r._M_ptr;
1410 _M_refcount = __r._M_refcount;
1415 operator=(__weak_ptr&& __r) noexcept
1417 _M_ptr = __r._M_ptr;
1418 _M_refcount = std::move(__r._M_refcount);
1419 __r._M_ptr =
nullptr;
1423 template<
typename _Tp1>
1425 operator=(__weak_ptr<_Tp1, _Lp>&& __r) noexcept
1427 _M_ptr = __r.lock().get();
1428 _M_refcount = std::move(__r._M_refcount);
1429 __r._M_ptr =
nullptr;
1433 __shared_ptr<_Tp, _Lp>
1434 lock()
const noexcept
1435 {
return __shared_ptr<element_type, _Lp>(*
this, std::nothrow); }
1438 use_count()
const noexcept
1439 {
return _M_refcount._M_get_use_count(); }
1442 expired()
const noexcept
1443 {
return _M_refcount._M_get_use_count() == 0; }
1445 template<
typename _Tp1>
1447 owner_before(
const __shared_ptr<_Tp1, _Lp>& __rhs)
const 1448 {
return _M_refcount._M_less(__rhs._M_refcount); }
1450 template<
typename _Tp1>
1452 owner_before(
const __weak_ptr<_Tp1, _Lp>& __rhs)
const 1453 {
return _M_refcount._M_less(__rhs._M_refcount); }
1457 { __weak_ptr().swap(*
this); }
1460 swap(__weak_ptr& __s) noexcept
1462 std::swap(_M_ptr, __s._M_ptr);
1463 _M_refcount._M_swap(__s._M_refcount);
1469 _M_assign(_Tp* __ptr,
const __shared_count<_Lp>& __refcount) noexcept
1471 if (use_count() == 0)
1474 _M_refcount = __refcount;
1478 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
1479 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
1480 friend class __enable_shared_from_this<_Tp, _Lp>;
1484 __weak_count<_Lp> _M_refcount;
1488 template<
typename _Tp, _Lock_policy _Lp>
1490 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b) noexcept
1493 template<
typename _Tp,
typename _Tp1>
1497 operator()(
const _Tp& __lhs,
const _Tp& __rhs)
const 1498 {
return __lhs.owner_before(__rhs); }
1501 operator()(
const _Tp& __lhs,
const _Tp1& __rhs)
const 1502 {
return __lhs.owner_before(__rhs); }
1505 operator()(
const _Tp1& __lhs,
const _Tp& __rhs)
const 1506 {
return __lhs.owner_before(__rhs); }
1509 template<
typename _Tp, _Lock_policy _Lp>
1511 :
public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
1514 template<
typename _Tp, _Lock_policy _Lp>
1516 :
public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
1520 template<
typename _Tp, _Lock_policy _Lp>
1521 class __enable_shared_from_this
1524 constexpr __enable_shared_from_this() noexcept { }
1526 __enable_shared_from_this(
const __enable_shared_from_this&) noexcept { }
1528 __enable_shared_from_this&
1529 operator=(
const __enable_shared_from_this&) noexcept
1532 ~__enable_shared_from_this() { }
1535 __shared_ptr<_Tp, _Lp>
1537 {
return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
1539 __shared_ptr<const _Tp, _Lp>
1540 shared_from_this()
const 1541 {
return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
1544 template<
typename _Tp1>
1546 _M_weak_assign(_Tp1* __p,
const __shared_count<_Lp>& __n)
const noexcept
1547 { _M_weak_this._M_assign(__p, __n); }
1549 template<_Lock_policy _Lp1,
typename _Tp1,
typename _Tp2>
1551 __enable_shared_from_this_helper(
const __shared_count<_Lp1>&,
1552 const __enable_shared_from_this<_Tp1,
1553 _Lp1>*,
const _Tp2*) noexcept;
1555 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
1558 template<_Lock_policy _Lp1,
typename _Tp1,
typename _Tp2>
1560 __enable_shared_from_this_helper(
const __shared_count<_Lp1>& __pn,
1561 const __enable_shared_from_this<_Tp1,
1563 const _Tp2* __px) noexcept
1565 if (__pe !=
nullptr)
1566 __pe->_M_weak_assign(const_cast<_Tp2*>(__px), __pn);
1569 template<
typename _Tp, _Lock_policy _Lp,
typename _Alloc,
typename... _Args>
1570 inline __shared_ptr<_Tp, _Lp>
1571 __allocate_shared(
const _Alloc& __a, _Args&&... __args)
1573 return __shared_ptr<_Tp, _Lp>(_Sp_make_shared_tag(), __a,
1574 std::forward<_Args>(__args)...);
1577 template<
typename _Tp, _Lock_policy _Lp,
typename... _Args>
1578 inline __shared_ptr<_Tp, _Lp>
1579 __make_shared(_Args&&... __args)
1581 typedef typename std::remove_const<_Tp>::type _Tp_nc;
1583 std::forward<_Args>(__args)...);
1587 template<
typename _Tp, _Lock_policy _Lp>
1588 struct hash<__shared_ptr<_Tp, _Lp>>
1589 :
public __hash_base<size_t, __shared_ptr<_Tp, _Lp>>
1592 operator()(
const __shared_ptr<_Tp, _Lp>& __s)
const noexcept
1596 _GLIBCXX_END_NAMESPACE_VERSION
1599 #endif // _SHARED_PTR_BASE_H
Base class for all library exceptions.
The standard allocator, as per [20.4].
complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
Non-standard RAII type for managing pointers obtained from allocators.
A smart pointer with reference-counted copy semantics.
Partial specializations for pointer types.
A simple smart pointer providing strict ownership semantics.
Primary class template for reference_wrapper.
_Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Exception possibly thrown by shared_ptr.
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.
One of the comparison functors.
void lock(_L1 &__l1, _L2 &__l2, _L3 &...__l3)
Generic lock.
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.
A smart pointer with weak semantics.
static void destroy(_Alloc &__a, _Tp *__p)
Destroy an object of type _Tp.
20.7.1.2 unique_ptr for single objects.
virtual char const * what() const noexcept
Primary template owner_less.
_Del * get_deleter(const __shared_ptr< _Tp, _Lp > &__p) noexcept
20.7.2.2.10 shared_ptr get_deleter
ISO C++ entities toplevel namespace is std.
Primary class template hash.
allocator<void> specialization.