49 #ifndef _SHARED_PTR_BASE_H 50 #define _SHARED_PTR_BASE_H 1 55 namespace std _GLIBCXX_VISIBILITY(default)
57 _GLIBCXX_BEGIN_NAMESPACE_VERSION
59 #if _GLIBCXX_USE_DEPRECATED 60 template<
typename>
class auto_ptr;
71 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 _GLIBCXX_READ_MEM_BARRIER;
158 _GLIBCXX_WRITE_MEM_BARRIER;
162 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
163 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count,
166 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
173 _M_weak_add_ref() noexcept
174 { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); }
177 _M_weak_release() noexcept
180 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
181 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1)
183 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
184 if (_Mutex_base<_Lp>::_S_need_barriers)
188 _GLIBCXX_READ_MEM_BARRIER;
189 _GLIBCXX_WRITE_MEM_BARRIER;
196 _M_get_use_count()
const noexcept
200 return __atomic_load_n(&_M_use_count, __ATOMIC_RELAXED);
204 _Sp_counted_base(_Sp_counted_base
const&) =
delete;
205 _Sp_counted_base& operator=(_Sp_counted_base
const&) =
delete;
207 _Atomic_word _M_use_count;
208 _Atomic_word _M_weak_count;
213 _Sp_counted_base<_S_single>::
216 if (_M_use_count == 0)
217 __throw_bad_weak_ptr();
223 _Sp_counted_base<_S_mutex>::
227 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
230 __throw_bad_weak_ptr();
236 _Sp_counted_base<_S_atomic>::
240 _Atomic_word __count = _M_get_use_count();
244 __throw_bad_weak_ptr();
248 while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1,
249 true, __ATOMIC_ACQ_REL,
255 _Sp_counted_base<_S_single>::
256 _M_add_ref_lock_nothrow()
258 if (_M_use_count == 0)
266 _Sp_counted_base<_S_mutex>::
267 _M_add_ref_lock_nothrow()
270 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
280 _Sp_counted_base<_S_atomic>::
281 _M_add_ref_lock_nothrow()
284 _Atomic_word __count = _M_get_use_count();
292 while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1,
293 true, __ATOMIC_ACQ_REL,
300 _Sp_counted_base<_S_single>::_M_add_ref_copy()
305 _Sp_counted_base<_S_single>::_M_release() noexcept
307 if (--_M_use_count == 0)
310 if (--_M_weak_count == 0)
317 _Sp_counted_base<_S_single>::_M_weak_add_ref() noexcept
322 _Sp_counted_base<_S_single>::_M_weak_release() noexcept
324 if (--_M_weak_count == 0)
330 _Sp_counted_base<_S_single>::_M_get_use_count()
const noexcept
331 {
return _M_use_count; }
335 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
338 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
341 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
342 class __enable_shared_from_this;
344 template<
typename _Tp>
347 template<
typename _Tp>
350 template<
typename _Tp>
353 template<
typename _Tp>
356 template<_Lock_policy _Lp = __default_lock_policy>
359 template<_Lock_policy _Lp = __default_lock_policy>
360 class __shared_count;
364 template<
typename _Ptr, _Lock_policy _Lp>
365 class _Sp_counted_ptr final :
public _Sp_counted_base<_Lp>
369 _Sp_counted_ptr(_Ptr __p) noexcept
373 _M_dispose() noexcept
377 _M_destroy() noexcept
384 _Sp_counted_ptr(
const _Sp_counted_ptr&) =
delete;
385 _Sp_counted_ptr& operator=(
const _Sp_counted_ptr&) =
delete;
393 _Sp_counted_ptr<nullptr_t, _S_single>::_M_dispose() noexcept { }
397 _Sp_counted_ptr<nullptr_t, _S_mutex>::_M_dispose() noexcept { }
401 _Sp_counted_ptr<nullptr_t, _S_atomic>::_M_dispose() noexcept { }
403 template<
int _Nm,
typename _Tp,
404 bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
405 struct _Sp_ebo_helper;
408 template<
int _Nm,
typename _Tp>
409 struct _Sp_ebo_helper<_Nm, _Tp, true> :
private _Tp
411 explicit _Sp_ebo_helper(
const _Tp& __tp) : _Tp(__tp) { }
414 _S_get(_Sp_ebo_helper& __eboh) {
return static_cast<_Tp&
>(__eboh); }
418 template<
int _Nm,
typename _Tp>
419 struct _Sp_ebo_helper<_Nm, _Tp, false>
421 explicit _Sp_ebo_helper(
const _Tp& __tp) : _M_tp(__tp) { }
424 _S_get(_Sp_ebo_helper& __eboh)
425 {
return __eboh._M_tp; }
432 template<
typename _Ptr,
typename _Deleter,
typename _Alloc, _Lock_policy _Lp>
433 class _Sp_counted_deleter final :
public _Sp_counted_base<_Lp>
435 class _Impl : _Sp_ebo_helper<0, _Deleter>, _Sp_ebo_helper<1, _Alloc>
437 typedef _Sp_ebo_helper<0, _Deleter> _Del_base;
438 typedef _Sp_ebo_helper<1, _Alloc> _Alloc_base;
441 _Impl(_Ptr __p, _Deleter __d,
const _Alloc& __a) noexcept
442 : _M_ptr(__p), _Del_base(__d), _Alloc_base(__a)
445 _Deleter& _M_del() noexcept {
return _Del_base::_S_get(*
this); }
446 _Alloc& _M_alloc() noexcept {
return _Alloc_base::_S_get(*
this); }
452 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_deleter>;
455 _Sp_counted_deleter(_Ptr __p, _Deleter __d) noexcept
456 : _M_impl(__p, __d, _Alloc()) { }
459 _Sp_counted_deleter(_Ptr __p, _Deleter __d,
const _Alloc& __a) noexcept
460 : _M_impl(__p, __d, __a) { }
462 ~_Sp_counted_deleter() noexcept { }
465 _M_dispose() noexcept
466 { _M_impl._M_del()(_M_impl._M_ptr); }
469 _M_destroy() noexcept
471 __allocator_type __a(_M_impl._M_alloc());
473 this->~_Sp_counted_deleter();
482 return __ti ==
typeid(_Deleter)
496 struct _Sp_make_shared_tag { };
498 template<
typename _Tp,
typename _Alloc, _Lock_policy _Lp>
499 class _Sp_counted_ptr_inplace final :
public _Sp_counted_base<_Lp>
501 class _Impl : _Sp_ebo_helper<0, _Alloc>
503 typedef _Sp_ebo_helper<0, _Alloc> _A_base;
506 explicit _Impl(_Alloc __a) noexcept : _A_base(__a) { }
508 _Alloc& _M_alloc() noexcept {
return _A_base::_S_get(*
this); }
510 __gnu_cxx::__aligned_buffer<_Tp> _M_storage;
514 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>;
516 template<
typename... _Args>
517 _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
523 std::forward<_Args>(__args)...);
526 ~_Sp_counted_ptr_inplace() noexcept { }
529 _M_dispose() noexcept
536 _M_destroy() noexcept
538 __allocator_type __a(_M_impl._M_alloc());
540 this->~_Sp_counted_ptr_inplace();
548 if (__ti ==
typeid(_Sp_make_shared_tag))
549 return const_cast<typename remove_cv<_Tp>::type*
>(_M_ptr());
555 _Tp* _M_ptr() noexcept {
return _M_impl._M_storage._M_ptr(); }
561 template<_Lock_policy _Lp>
565 constexpr __shared_count() noexcept : _M_pi(0)
568 template<
typename _Ptr>
570 __shared_count(_Ptr __p) : _M_pi(0)
574 _M_pi =
new _Sp_counted_ptr<_Ptr, _Lp>(__p);
579 __throw_exception_again;
583 template<
typename _Ptr,
typename _Deleter>
584 __shared_count(_Ptr __p, _Deleter __d)
588 template<
typename _Ptr,
typename _Deleter,
typename _Alloc>
589 __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
591 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
594 typename _Sp_cd_type::__allocator_type __a2(__a);
596 _Sp_cd_type* __mem = __guard.get();
597 ::new (__mem) _Sp_cd_type(__p, std::move(__d), std::move(__a));
604 __throw_exception_again;
608 template<
typename _Tp,
typename _Alloc,
typename... _Args>
609 __shared_count(_Sp_make_shared_tag, _Tp*,
const _Alloc& __a,
613 typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type;
614 typename _Sp_cp_type::__allocator_type __a2(__a);
616 _Sp_cp_type* __mem = __guard.get();
617 ::new (__mem) _Sp_cp_type(std::move(__a),
618 std::forward<_Args>(__args)...);
623 #if _GLIBCXX_USE_DEPRECATED 625 template<
typename _Tp>
631 template<
typename _Tp,
typename _Del>
635 using _Ptr =
typename unique_ptr<_Tp, _Del>::pointer;
636 using _Del2 =
typename conditional<is_reference<_Del>::value,
640 = _Sp_counted_deleter<_Ptr, _Del2, allocator<void>, _Lp>;
644 _Sp_cd_type* __mem = _Alloc_traits::allocate(__a, 1);
645 _Alloc_traits::construct(__a, __mem, __r.release(),
651 explicit __shared_count(
const __weak_count<_Lp>& __r);
654 explicit __shared_count(
const __weak_count<_Lp>& __r, std::nothrow_t);
656 ~__shared_count() noexcept
658 if (_M_pi !=
nullptr)
662 __shared_count(
const __shared_count& __r) noexcept
666 _M_pi->_M_add_ref_copy();
670 operator=(
const __shared_count& __r) noexcept
672 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
676 __tmp->_M_add_ref_copy();
685 _M_swap(__shared_count& __r) noexcept
687 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
693 _M_get_use_count()
const noexcept
694 {
return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
697 _M_unique()
const noexcept
698 {
return this->_M_get_use_count() == 1; }
702 {
return _M_pi ? _M_pi->_M_get_deleter(__ti) :
nullptr; }
705 _M_less(
const __shared_count& __rhs)
const noexcept
709 _M_less(
const __weak_count<_Lp>& __rhs)
const noexcept
714 operator==(
const __shared_count& __a,
const __shared_count& __b) noexcept
715 {
return __a._M_pi == __b._M_pi; }
718 friend class __weak_count<_Lp>;
720 _Sp_counted_base<_Lp>* _M_pi;
724 template<_Lock_policy _Lp>
728 constexpr __weak_count() noexcept : _M_pi(
nullptr)
731 __weak_count(
const __shared_count<_Lp>& __r) noexcept
734 if (_M_pi !=
nullptr)
735 _M_pi->_M_weak_add_ref();
738 __weak_count(
const __weak_count& __r) noexcept
741 if (_M_pi !=
nullptr)
742 _M_pi->_M_weak_add_ref();
745 __weak_count(__weak_count&& __r) noexcept
747 { __r._M_pi =
nullptr; }
749 ~__weak_count() noexcept
751 if (_M_pi !=
nullptr)
752 _M_pi->_M_weak_release();
756 operator=(
const __shared_count<_Lp>& __r) noexcept
758 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
759 if (__tmp !=
nullptr)
760 __tmp->_M_weak_add_ref();
761 if (_M_pi !=
nullptr)
762 _M_pi->_M_weak_release();
768 operator=(
const __weak_count& __r) noexcept
770 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
771 if (__tmp !=
nullptr)
772 __tmp->_M_weak_add_ref();
773 if (_M_pi !=
nullptr)
774 _M_pi->_M_weak_release();
780 operator=(__weak_count&& __r) noexcept
782 if (_M_pi !=
nullptr)
783 _M_pi->_M_weak_release();
790 _M_swap(__weak_count& __r) noexcept
792 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
798 _M_get_use_count()
const noexcept
799 {
return _M_pi !=
nullptr ? _M_pi->_M_get_use_count() : 0; }
802 _M_less(
const __weak_count& __rhs)
const noexcept
806 _M_less(
const __shared_count<_Lp>& __rhs)
const noexcept
811 operator==(
const __weak_count& __a,
const __weak_count& __b) noexcept
812 {
return __a._M_pi == __b._M_pi; }
815 friend class __shared_count<_Lp>;
817 _Sp_counted_base<_Lp>* _M_pi;
821 template<_Lock_policy _Lp>
823 __shared_count<_Lp>::__shared_count(
const __weak_count<_Lp>& __r)
826 if (_M_pi !=
nullptr)
827 _M_pi->_M_add_ref_lock();
829 __throw_bad_weak_ptr();
833 template<_Lock_policy _Lp>
835 __shared_count<_Lp>::
836 __shared_count(
const __weak_count<_Lp>& __r, std::nothrow_t)
839 if (_M_pi !=
nullptr)
840 if (!_M_pi->_M_add_ref_lock_nothrow())
847 template<_Lock_policy _Lp,
typename _Tp1,
typename _Tp2>
849 __enable_shared_from_this_helper(
const __shared_count<_Lp>&,
850 const __enable_shared_from_this<_Tp1,
851 _Lp>*,
const _Tp2*) noexcept;
854 template<
typename _Tp1,
typename _Tp2>
856 __enable_shared_from_this_helper(
const __shared_count<>&,
858 const _Tp2*) noexcept;
860 template<_Lock_policy _Lp>
862 __enable_shared_from_this_helper(
const __shared_count<_Lp>&, ...) noexcept
866 template<
typename _Tp, _Lock_policy _Lp>
869 template<
typename _Ptr>
871 =
typename enable_if<is_convertible<_Ptr, _Tp*>::value>::type;
874 typedef _Tp element_type;
876 constexpr __shared_ptr() noexcept
877 : _M_ptr(0), _M_refcount()
880 template<
typename _Tp1>
881 explicit __shared_ptr(_Tp1* __p)
882 : _M_ptr(__p), _M_refcount(__p)
884 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
886 static_assert(
sizeof(_Tp1) > 0,
"incomplete type" );
887 __enable_shared_from_this_helper(_M_refcount, __p, __p);
890 template<
typename _Tp1,
typename _Deleter>
891 __shared_ptr(_Tp1* __p, _Deleter __d)
892 : _M_ptr(__p), _M_refcount(__p, __d)
894 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
896 __enable_shared_from_this_helper(_M_refcount, __p, __p);
899 template<
typename _Tp1,
typename _Deleter,
typename _Alloc>
900 __shared_ptr(_Tp1* __p, _Deleter __d, _Alloc __a)
901 : _M_ptr(__p), _M_refcount(__p, __d, std::move(__a))
903 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
905 __enable_shared_from_this_helper(_M_refcount, __p, __p);
908 template<
typename _Deleter>
909 __shared_ptr(nullptr_t __p, _Deleter __d)
910 : _M_ptr(0), _M_refcount(__p, __d)
913 template<
typename _Deleter,
typename _Alloc>
914 __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
915 : _M_ptr(0), _M_refcount(__p, __d, std::move(__a))
918 template<
typename _Tp1>
919 __shared_ptr(
const __shared_ptr<_Tp1, _Lp>& __r, _Tp* __p) noexcept
920 : _M_ptr(__p), _M_refcount(__r._M_refcount)
923 __shared_ptr(
const __shared_ptr&) noexcept =
default;
924 __shared_ptr& operator=(
const __shared_ptr&) noexcept =
default;
925 ~__shared_ptr() =
default;
927 template<
typename _Tp1,
typename = _Convertible<_Tp1*>>
928 __shared_ptr(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
929 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
932 __shared_ptr(__shared_ptr&& __r) noexcept
933 : _M_ptr(__r._M_ptr), _M_refcount()
935 _M_refcount._M_swap(__r._M_refcount);
939 template<
typename _Tp1,
typename = _Convertible<_Tp1*>>
940 __shared_ptr(__shared_ptr<_Tp1, _Lp>&& __r) noexcept
941 : _M_ptr(__r._M_ptr), _M_refcount()
943 _M_refcount._M_swap(__r._M_refcount);
947 template<
typename _Tp1>
948 explicit __shared_ptr(
const __weak_ptr<_Tp1, _Lp>& __r)
949 : _M_refcount(__r._M_refcount)
951 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
959 template<
typename _Tp1,
typename _Del,
typename 960 = _Convertible<typename unique_ptr<_Tp1, _Del>::pointer>>
962 : _M_ptr(__r.get()), _M_refcount()
964 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
965 auto __raw = _S_raw_ptr(__r.get());
966 _M_refcount = __shared_count<_Lp>(std::move(__r));
967 __enable_shared_from_this_helper(_M_refcount, __raw, __raw);
970 #if _GLIBCXX_USE_DEPRECATED 972 template<
typename _Tp1>
976 constexpr __shared_ptr(nullptr_t) noexcept : __shared_ptr() { }
978 template<
typename _Tp1>
980 operator=(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
983 _M_refcount = __r._M_refcount;
987 #if _GLIBCXX_USE_DEPRECATED 988 template<
typename _Tp1>
992 __shared_ptr(std::move(__r)).swap(*
this);
998 operator=(__shared_ptr&& __r) noexcept
1000 __shared_ptr(std::move(__r)).swap(*
this);
1004 template<
class _Tp1>
1006 operator=(__shared_ptr<_Tp1, _Lp>&& __r) noexcept
1008 __shared_ptr(std::move(__r)).swap(*
this);
1012 template<
typename _Tp1,
typename _Del>
1016 __shared_ptr(std::move(__r)).swap(*
this);
1022 { __shared_ptr().swap(*
this); }
1024 template<
typename _Tp1>
1029 _GLIBCXX_DEBUG_ASSERT(__p == 0 || __p != _M_ptr);
1030 __shared_ptr(__p).swap(*
this);
1033 template<
typename _Tp1,
typename _Deleter>
1035 reset(_Tp1* __p, _Deleter __d)
1036 { __shared_ptr(__p, __d).swap(*
this); }
1038 template<
typename _Tp1,
typename _Deleter,
typename _Alloc>
1040 reset(_Tp1* __p, _Deleter __d, _Alloc __a)
1041 { __shared_ptr(__p, __d, std::move(__a)).swap(*
this); }
1044 typename std::add_lvalue_reference<_Tp>::type
1047 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
1052 operator->()
const noexcept
1054 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
1059 get()
const noexcept
1062 explicit operator bool()
const 1063 {
return _M_ptr == 0 ?
false :
true; }
1066 unique()
const noexcept
1067 {
return _M_refcount._M_unique(); }
1070 use_count()
const noexcept
1071 {
return _M_refcount._M_get_use_count(); }
1074 swap(__shared_ptr<_Tp, _Lp>& __other) noexcept
1076 std::swap(_M_ptr, __other._M_ptr);
1077 _M_refcount._M_swap(__other._M_refcount);
1080 template<
typename _Tp1>
1082 owner_before(__shared_ptr<_Tp1, _Lp>
const& __rhs)
const 1083 {
return _M_refcount._M_less(__rhs._M_refcount); }
1085 template<
typename _Tp1>
1087 owner_before(__weak_ptr<_Tp1, _Lp>
const& __rhs)
const 1088 {
return _M_refcount._M_less(__rhs._M_refcount); }
1093 template<
typename _Alloc,
typename... _Args>
1094 __shared_ptr(_Sp_make_shared_tag __tag,
const _Alloc& __a,
1096 : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a,
1097 std::forward<_Args>(__args)...)
1101 void* __p = _M_refcount._M_get_deleter(
typeid(__tag));
1102 _M_ptr =
static_cast<_Tp*
>(__p);
1103 __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
1106 template<
typename _Alloc>
1109 void operator()(
typename _Alloc::value_type* __ptr)
1117 template<
typename _Alloc,
typename... _Args>
1118 __shared_ptr(_Sp_make_shared_tag __tag,
const _Alloc& __a,
1120 : _M_ptr(), _M_refcount()
1123 rebind_traits<typename std::remove_cv<_Tp>::type> __traits;
1124 _Deleter<typename __traits::allocator_type> __del = { __a };
1126 auto __ptr = __guard.get();
1129 __traits::construct(__del._M_alloc, __ptr,
1130 std::forward<_Args>(__args)...);
1132 __shared_count<_Lp> __count(__ptr, __del, __del._M_alloc);
1133 _M_refcount._M_swap(__count);
1135 __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
1139 template<
typename _Tp1, _Lock_policy _Lp1,
typename _Alloc,
1141 friend __shared_ptr<_Tp1, _Lp1>
1142 __allocate_shared(
const _Alloc& __a, _Args&&... __args);
1146 __shared_ptr(
const __weak_ptr<_Tp, _Lp>& __r, std::nothrow_t)
1147 : _M_refcount(__r._M_refcount, std::nothrow)
1149 _M_ptr = _M_refcount._M_get_use_count() ? __r._M_ptr :
nullptr;
1152 friend class __weak_ptr<_Tp, _Lp>;
1157 {
return _M_refcount._M_get_deleter(__ti); }
1159 template<
typename _Tp1>
1161 _S_raw_ptr(_Tp1* __ptr)
1164 template<
typename _Tp1>
1169 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
1170 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
1172 template<
typename _Del,
typename _Tp1, _Lock_policy _Lp1>
1173 friend _Del*
get_deleter(
const __shared_ptr<_Tp1, _Lp1>&) noexcept;
1176 __shared_count<_Lp> _M_refcount;
1181 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1183 operator==(
const __shared_ptr<_Tp1, _Lp>& __a,
1184 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1185 {
return __a.get() == __b.get(); }
1187 template<
typename _Tp, _Lock_policy _Lp>
1189 operator==(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1192 template<
typename _Tp, _Lock_policy _Lp>
1194 operator==(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1197 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1199 operator!=(
const __shared_ptr<_Tp1, _Lp>& __a,
1200 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1201 {
return __a.get() != __b.get(); }
1203 template<
typename _Tp, _Lock_policy _Lp>
1205 operator!=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1206 {
return (
bool)__a; }
1208 template<
typename _Tp, _Lock_policy _Lp>
1210 operator!=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1211 {
return (
bool)__a; }
1213 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1215 operator<(const __shared_ptr<_Tp1, _Lp>& __a,
1216 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1218 typedef typename std::common_type<_Tp1*, _Tp2*>::type _CT;
1222 template<
typename _Tp, _Lock_policy _Lp>
1224 operator<(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1227 template<
typename _Tp, _Lock_policy _Lp>
1229 operator<(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1232 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1234 operator<=(const __shared_ptr<_Tp1, _Lp>& __a,
1235 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1236 {
return !(__b < __a); }
1238 template<
typename _Tp, _Lock_policy _Lp>
1240 operator<=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1241 {
return !(
nullptr < __a); }
1243 template<
typename _Tp, _Lock_policy _Lp>
1245 operator<=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1246 {
return !(__a <
nullptr); }
1248 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1250 operator>(
const __shared_ptr<_Tp1, _Lp>& __a,
1251 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1252 {
return (__b < __a); }
1254 template<
typename _Tp, _Lock_policy _Lp>
1256 operator>(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1259 template<
typename _Tp, _Lock_policy _Lp>
1261 operator>(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1264 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1266 operator>=(
const __shared_ptr<_Tp1, _Lp>& __a,
1267 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1268 {
return !(__a < __b); }
1270 template<
typename _Tp, _Lock_policy _Lp>
1272 operator>=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1273 {
return !(__a <
nullptr); }
1275 template<
typename _Tp, _Lock_policy _Lp>
1277 operator>=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1278 {
return !(
nullptr < __a); }
1280 template<
typename _Sp>
1284 operator()(
const _Sp& __lhs,
const _Sp& __rhs)
const noexcept
1286 typedef typename _Sp::element_type element_type;
1291 template<
typename _Tp, _Lock_policy _Lp>
1292 struct less<__shared_ptr<_Tp, _Lp>>
1293 :
public _Sp_less<__shared_ptr<_Tp, _Lp>>
1297 template<
typename _Tp, _Lock_policy _Lp>
1299 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b) noexcept
1309 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1310 inline __shared_ptr<_Tp, _Lp>
1311 static_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1312 {
return __shared_ptr<_Tp, _Lp>(__r,
static_cast<_Tp*
>(__r.get())); }
1319 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1320 inline __shared_ptr<_Tp, _Lp>
1321 const_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1322 {
return __shared_ptr<_Tp, _Lp>(__r,
const_cast<_Tp*
>(__r.get())); }
1329 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1330 inline __shared_ptr<_Tp, _Lp>
1331 dynamic_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1333 if (_Tp* __p = dynamic_cast<_Tp*>(__r.get()))
1334 return __shared_ptr<_Tp, _Lp>(__r, __p);
1335 return __shared_ptr<_Tp, _Lp>();
1339 template<
typename _Tp, _Lock_policy _Lp>
1342 template<
typename _Ptr>
1344 =
typename enable_if<is_convertible<_Ptr, _Tp*>::value>::type;
1347 typedef _Tp element_type;
1349 constexpr __weak_ptr() noexcept
1350 : _M_ptr(
nullptr), _M_refcount()
1353 __weak_ptr(
const __weak_ptr&) noexcept =
default;
1355 ~__weak_ptr() =
default;
1371 template<
typename _Tp1,
typename = _Convertible<_Tp1*>>
1372 __weak_ptr(
const __weak_ptr<_Tp1, _Lp>& __r) noexcept
1373 : _M_refcount(__r._M_refcount)
1374 { _M_ptr = __r.lock().get(); }
1376 template<
typename _Tp1,
typename = _Convertible<_Tp1*>>
1377 __weak_ptr(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1378 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1381 __weak_ptr(__weak_ptr&& __r) noexcept
1382 : _M_ptr(__r._M_ptr), _M_refcount(std::move(__r._M_refcount))
1383 { __r._M_ptr =
nullptr; }
1385 template<
typename _Tp1,
typename = _Convertible<_Tp1*>>
1386 __weak_ptr(__weak_ptr<_Tp1, _Lp>&& __r) noexcept
1387 : _M_ptr(__r.lock().get()), _M_refcount(std::move(__r._M_refcount))
1388 { __r._M_ptr =
nullptr; }
1391 operator=(
const __weak_ptr& __r) noexcept =
default;
1393 template<
typename _Tp1>
1395 operator=(
const __weak_ptr<_Tp1, _Lp>& __r) noexcept
1397 _M_ptr = __r.lock().get();
1398 _M_refcount = __r._M_refcount;
1402 template<
typename _Tp1>
1404 operator=(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1406 _M_ptr = __r._M_ptr;
1407 _M_refcount = __r._M_refcount;
1412 operator=(__weak_ptr&& __r) noexcept
1414 _M_ptr = __r._M_ptr;
1415 _M_refcount = std::move(__r._M_refcount);
1416 __r._M_ptr =
nullptr;
1420 template<
typename _Tp1>
1422 operator=(__weak_ptr<_Tp1, _Lp>&& __r) noexcept
1424 _M_ptr = __r.lock().get();
1425 _M_refcount = std::move(__r._M_refcount);
1426 __r._M_ptr =
nullptr;
1430 __shared_ptr<_Tp, _Lp>
1431 lock()
const noexcept
1432 {
return __shared_ptr<element_type, _Lp>(*
this, std::nothrow); }
1435 use_count()
const noexcept
1436 {
return _M_refcount._M_get_use_count(); }
1439 expired()
const noexcept
1440 {
return _M_refcount._M_get_use_count() == 0; }
1442 template<
typename _Tp1>
1444 owner_before(
const __shared_ptr<_Tp1, _Lp>& __rhs)
const 1445 {
return _M_refcount._M_less(__rhs._M_refcount); }
1447 template<
typename _Tp1>
1449 owner_before(
const __weak_ptr<_Tp1, _Lp>& __rhs)
const 1450 {
return _M_refcount._M_less(__rhs._M_refcount); }
1454 { __weak_ptr().swap(*
this); }
1457 swap(__weak_ptr& __s) noexcept
1459 std::swap(_M_ptr, __s._M_ptr);
1460 _M_refcount._M_swap(__s._M_refcount);
1466 _M_assign(_Tp* __ptr,
const __shared_count<_Lp>& __refcount) noexcept
1469 _M_refcount = __refcount;
1472 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
1473 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
1474 friend class __enable_shared_from_this<_Tp, _Lp>;
1478 __weak_count<_Lp> _M_refcount;
1482 template<
typename _Tp, _Lock_policy _Lp>
1484 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b) noexcept
1487 template<
typename _Tp,
typename _Tp1>
1491 operator()(
const _Tp& __lhs,
const _Tp& __rhs)
const 1492 {
return __lhs.owner_before(__rhs); }
1495 operator()(
const _Tp& __lhs,
const _Tp1& __rhs)
const 1496 {
return __lhs.owner_before(__rhs); }
1499 operator()(
const _Tp1& __lhs,
const _Tp& __rhs)
const 1500 {
return __lhs.owner_before(__rhs); }
1503 template<
typename _Tp, _Lock_policy _Lp>
1505 :
public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
1508 template<
typename _Tp, _Lock_policy _Lp>
1510 :
public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
1514 template<
typename _Tp, _Lock_policy _Lp>
1515 class __enable_shared_from_this
1518 constexpr __enable_shared_from_this() noexcept { }
1520 __enable_shared_from_this(
const __enable_shared_from_this&) noexcept { }
1522 __enable_shared_from_this&
1523 operator=(
const __enable_shared_from_this&) noexcept
1526 ~__enable_shared_from_this() { }
1529 __shared_ptr<_Tp, _Lp>
1531 {
return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
1533 __shared_ptr<const _Tp, _Lp>
1534 shared_from_this()
const 1535 {
return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
1538 template<
typename _Tp1>
1540 _M_weak_assign(_Tp1* __p,
const __shared_count<_Lp>& __n)
const noexcept
1541 { _M_weak_this._M_assign(__p, __n); }
1543 template<_Lock_policy _Lp1,
typename _Tp1,
typename _Tp2>
1545 __enable_shared_from_this_helper(
const __shared_count<_Lp1>&,
1546 const __enable_shared_from_this<_Tp1,
1547 _Lp1>*,
const _Tp2*) noexcept;
1549 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
1552 template<_Lock_policy _Lp1,
typename _Tp1,
typename _Tp2>
1554 __enable_shared_from_this_helper(
const __shared_count<_Lp1>& __pn,
1555 const __enable_shared_from_this<_Tp1,
1557 const _Tp2* __px) noexcept
1559 if (__pe !=
nullptr)
1560 __pe->_M_weak_assign(const_cast<_Tp2*>(__px), __pn);
1563 template<
typename _Tp, _Lock_policy _Lp,
typename _Alloc,
typename... _Args>
1564 inline __shared_ptr<_Tp, _Lp>
1565 __allocate_shared(
const _Alloc& __a, _Args&&... __args)
1567 return __shared_ptr<_Tp, _Lp>(_Sp_make_shared_tag(), __a,
1568 std::forward<_Args>(__args)...);
1571 template<
typename _Tp, _Lock_policy _Lp,
typename... _Args>
1572 inline __shared_ptr<_Tp, _Lp>
1573 __make_shared(_Args&&... __args)
1575 typedef typename std::remove_const<_Tp>::type _Tp_nc;
1577 std::forward<_Args>(__args)...);
1581 template<
typename _Tp, _Lock_policy _Lp>
1582 struct hash<__shared_ptr<_Tp, _Lp>>
1583 :
public __hash_base<size_t, __shared_ptr<_Tp, _Lp>>
1586 operator()(
const __shared_ptr<_Tp, _Lp>& __s)
const noexcept
1590 _GLIBCXX_END_NAMESPACE_VERSION
1593 #endif // _SHARED_PTR_BASE_H
__allocated_ptr< _Alloc > __allocate_guarded(_Alloc &__a)
Allocate space for a single object using __a.
Uniform interface to all allocator types.
_Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
allocator<void> specialization.
_Del * get_deleter(const __shared_ptr< _Tp, _Lp > &__p) noexcept
20.7.2.2.10 shared_ptr get_deleter
complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
The standard allocator, as per [20.4].
virtual char const * what() const noexcept
Base class for all library exceptions.
Non-standard RAII type for managing pointers obtained from allocators.
Partial specializations for pointer types.
A smart pointer with reference-counted copy semantics.
One of the comparison functors.
Primary class template hash.
A smart pointer with weak semantics.
20.7.1.2 unique_ptr for single objects.
A simple smart pointer providing strict ownership semantics.
Primary class template for reference_wrapper.
Primary template owner_less.
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.
void lock(_L1 &__l1, _L2 &__l2, _L3 &...__l3)
Generic lock.
Exception possibly thrown by shared_ptr.
ISO C++ entities toplevel namespace is std.
Base class allowing use of member function shared_from_this.