49 #ifndef _SHARED_PTR_BASE_H
50 #define _SHARED_PTR_BASE_H 1
54 namespace std _GLIBCXX_VISIBILITY(default)
56 _GLIBCXX_BEGIN_NAMESPACE_VERSION
58 #if _GLIBCXX_USE_DEPRECATED
59 template<
typename>
class auto_ptr;
70 what()
const noexcept;
77 __throw_bad_weak_ptr()
80 using __gnu_cxx::_Lock_policy;
81 using __gnu_cxx::__default_lock_policy;
82 using __gnu_cxx::_S_single;
83 using __gnu_cxx::_S_mutex;
84 using __gnu_cxx::_S_atomic;
87 template<_Lock_policy _Lp>
92 enum { _S_need_barriers = 0 };
96 class _Mutex_base<_S_mutex>
97 :
public __gnu_cxx::__mutex
103 enum { _S_need_barriers = 1 };
106 template<_Lock_policy _Lp = __default_lock_policy>
107 class _Sp_counted_base
108 :
public _Mutex_base<_Lp>
111 _Sp_counted_base() noexcept
112 : _M_use_count(1), _M_weak_count(1) { }
115 ~_Sp_counted_base() noexcept
121 _M_dispose() noexcept = 0;
125 _M_destroy() noexcept
133 { __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); }
139 _M_release() noexcept
142 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count);
143 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
145 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count);
151 if (_Mutex_base<_Lp>::_S_need_barriers)
153 _GLIBCXX_READ_MEM_BARRIER;
154 _GLIBCXX_WRITE_MEM_BARRIER;
158 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
159 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count,
162 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
169 _M_weak_add_ref() noexcept
170 { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); }
173 _M_weak_release() noexcept
176 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
177 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1)
179 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
180 if (_Mutex_base<_Lp>::_S_need_barriers)
184 _GLIBCXX_READ_MEM_BARRIER;
185 _GLIBCXX_WRITE_MEM_BARRIER;
192 _M_get_use_count() const noexcept
196 return __atomic_load_n(&_M_use_count, __ATOMIC_RELAXED);
200 _Sp_counted_base(_Sp_counted_base
const&) =
delete;
201 _Sp_counted_base& operator=(_Sp_counted_base
const&) =
delete;
203 _Atomic_word _M_use_count;
204 _Atomic_word _M_weak_count;
209 _Sp_counted_base<_S_single>::
212 if (_M_use_count == 0)
213 __throw_bad_weak_ptr();
219 _Sp_counted_base<_S_mutex>::
223 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
226 __throw_bad_weak_ptr();
232 _Sp_counted_base<_S_atomic>::
236 _Atomic_word __count = _M_use_count;
240 __throw_bad_weak_ptr();
244 while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1,
245 true, __ATOMIC_ACQ_REL,
251 _Sp_counted_base<_S_single>::_M_add_ref_copy()
256 _Sp_counted_base<_S_single>::_M_release() noexcept
258 if (--_M_use_count == 0)
261 if (--_M_weak_count == 0)
268 _Sp_counted_base<_S_single>::_M_weak_add_ref() noexcept
273 _Sp_counted_base<_S_single>::_M_weak_release() noexcept
275 if (--_M_weak_count == 0)
281 _Sp_counted_base<_S_single>::_M_get_use_count() const noexcept
282 {
return _M_use_count; }
286 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
289 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
292 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
293 class __enable_shared_from_this;
295 template<
typename _Tp>
298 template<
typename _Tp>
301 template<
typename _Tp>
304 template<
typename _Tp>
305 class enable_shared_from_this;
307 template<_Lock_policy _Lp = __default_lock_policy>
310 template<_Lock_policy _Lp = __default_lock_policy>
311 class __shared_count;
315 template<
typename _Ptr, _Lock_policy _Lp>
316 class _Sp_counted_ptr final :
public _Sp_counted_base<_Lp>
320 _Sp_counted_ptr(_Ptr __p) noexcept
324 _M_dispose() noexcept
328 _M_destroy() noexcept
335 _Sp_counted_ptr(
const _Sp_counted_ptr&) =
delete;
336 _Sp_counted_ptr& operator=(
const _Sp_counted_ptr&) =
delete;
344 _Sp_counted_ptr<nullptr_t, _S_single>::_M_dispose() noexcept { }
348 _Sp_counted_ptr<nullptr_t, _S_mutex>::_M_dispose() noexcept { }
352 _Sp_counted_ptr<nullptr_t, _S_atomic>::_M_dispose() noexcept { }
354 template<
int _Nm,
typename _Tp,
355 bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
356 struct _Sp_ebo_helper;
359 template<
int _Nm,
typename _Tp>
360 struct _Sp_ebo_helper<_Nm, _Tp, true> :
private _Tp
362 explicit _Sp_ebo_helper(
const _Tp& __tp) : _Tp(__tp) { }
365 _S_get(_Sp_ebo_helper& __eboh) {
return static_cast<_Tp&
>(__eboh); }
369 template<
int _Nm,
typename _Tp>
370 struct _Sp_ebo_helper<_Nm, _Tp, false>
372 explicit _Sp_ebo_helper(
const _Tp& __tp) : _M_tp(__tp) { }
375 _S_get(_Sp_ebo_helper& __eboh)
376 {
return __eboh._M_tp; }
383 template<
typename _Ptr,
typename _Deleter,
typename _Alloc, _Lock_policy _Lp>
384 class _Sp_counted_deleter final :
public _Sp_counted_base<_Lp>
386 class _Impl : _Sp_ebo_helper<0, _Deleter>, _Sp_ebo_helper<1, _Alloc>
388 typedef _Sp_ebo_helper<0, _Deleter> _Del_base;
389 typedef _Sp_ebo_helper<1, _Alloc> _Alloc_base;
392 _Impl(_Ptr __p, _Deleter __d,
const _Alloc& __a) noexcept
393 : _M_ptr(__p), _Del_base(__d), _Alloc_base(__a)
396 _Deleter& _M_del() noexcept {
return _Del_base::_S_get(*
this); }
397 _Alloc& _M_alloc() noexcept {
return _Alloc_base::_S_get(*
this); }
404 _Sp_counted_deleter(_Ptr __p, _Deleter __d) noexcept
405 : _M_impl(__p, __d, _Alloc()) { }
408 _Sp_counted_deleter(_Ptr __p, _Deleter __d,
const _Alloc& __a) noexcept
409 : _M_impl(__p, __d, __a) { }
411 ~_Sp_counted_deleter() noexcept { }
414 _M_dispose() noexcept
415 { _M_impl._M_del()(_M_impl._M_ptr); }
418 _M_destroy() noexcept
420 typedef typename allocator_traits<_Alloc>::template
421 rebind_traits<_Sp_counted_deleter> _Alloc_traits;
422 typename _Alloc_traits::allocator_type __a(_M_impl._M_alloc());
423 _Alloc_traits::destroy(__a,
this);
424 _Alloc_traits::deallocate(__a,
this, 1);
431 return __ti ==
typeid(_Deleter) ? &_M_impl._M_del() :
nullptr;
443 struct _Sp_make_shared_tag { };
445 template<
typename _Tp,
typename _Alloc, _Lock_policy _Lp>
446 class _Sp_counted_ptr_inplace final :
public _Sp_counted_base<_Lp>
448 class _Impl : _Sp_ebo_helper<0, _Alloc>
450 typedef _Sp_ebo_helper<0, _Alloc> _A_base;
453 explicit _Impl(_Alloc __a) noexcept : _A_base(__a) { }
455 _Alloc& _M_alloc() noexcept {
return _A_base::_S_get(*
this); }
457 __gnu_cxx::__aligned_buffer<_Tp> _M_storage;
461 template<
typename... _Args>
462 _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
468 std::forward<_Args>(__args)...);
471 ~_Sp_counted_ptr_inplace() noexcept { }
474 _M_dispose() noexcept
481 _M_destroy() noexcept
483 typedef typename allocator_traits<_Alloc>::template
484 rebind_traits<_Sp_counted_ptr_inplace> _Alloc_traits;
485 typename _Alloc_traits::allocator_type __a(_M_impl._M_alloc());
486 _Alloc_traits::destroy(__a,
this);
487 _Alloc_traits::deallocate(__a,
this, 1);
495 if (__ti ==
typeid(_Sp_make_shared_tag))
496 return const_cast<typename remove_cv<_Tp>::type*
>(_M_ptr());
502 _Tp* _M_ptr() noexcept {
return _M_impl._M_storage._M_ptr(); }
508 template<_Lock_policy _Lp>
512 constexpr __shared_count() noexcept : _M_pi(0)
515 template<
typename _Ptr>
517 __shared_count(_Ptr __p) : _M_pi(0)
521 _M_pi =
new _Sp_counted_ptr<_Ptr, _Lp>(__p);
526 __throw_exception_again;
530 template<
typename _Ptr,
typename _Deleter>
531 __shared_count(_Ptr __p, _Deleter __d)
532 : __shared_count(__p, std::move(__d), allocator<void>())
535 template<
typename _Ptr,
typename _Deleter,
typename _Alloc>
536 __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
538 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
539 typedef typename allocator_traits<_Alloc>::template
540 rebind_traits<_Sp_cd_type> _Alloc_traits;
541 typename _Alloc_traits::allocator_type __a2(__a);
542 _Sp_cd_type* __mem = 0;
545 __mem = _Alloc_traits::allocate(__a2, 1);
546 _Alloc_traits::construct(__a2, __mem,
547 __p, std::move(__d), std::move(__a));
554 _Alloc_traits::deallocate(__a2, __mem, 1);
555 __throw_exception_again;
559 template<
typename _Tp,
typename _Alloc,
typename... _Args>
560 __shared_count(_Sp_make_shared_tag, _Tp*,
const _Alloc& __a,
564 typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type;
565 typedef typename allocator_traits<_Alloc>::template
566 rebind_traits<_Sp_cp_type> _Alloc_traits;
567 typename _Alloc_traits::allocator_type __a2(__a);
568 _Sp_cp_type* __mem = _Alloc_traits::allocate(__a2, 1);
571 _Alloc_traits::construct(__a2, __mem, std::move(__a),
572 std::forward<_Args>(__args)...);
577 _Alloc_traits::deallocate(__a2, __mem, 1);
578 __throw_exception_again;
582 #if _GLIBCXX_USE_DEPRECATED
584 template<
typename _Tp>
590 template<
typename _Tp,
typename _Del>
594 using _Ptr =
typename unique_ptr<_Tp, _Del>::pointer;
595 using _Del2 =
typename conditional<is_reference<_Del>::value,
596 reference_wrapper<typename remove_reference<_Del>::type>,
599 = _Sp_counted_deleter<_Ptr, _Del2, allocator<void>, _Lp>;
600 using _Alloc = allocator<_Sp_cd_type>;
601 using _Alloc_traits = allocator_traits<_Alloc>;
603 _Sp_cd_type* __mem = _Alloc_traits::allocate(__a, 1);
604 _Alloc_traits::construct(__a, __mem, __r.release(),
610 explicit __shared_count(
const __weak_count<_Lp>& __r);
612 ~__shared_count() noexcept
614 if (_M_pi !=
nullptr)
618 __shared_count(
const __shared_count& __r) noexcept
622 _M_pi->_M_add_ref_copy();
626 operator=(
const __shared_count& __r) noexcept
628 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
632 __tmp->_M_add_ref_copy();
641 _M_swap(__shared_count& __r) noexcept
643 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
649 _M_get_use_count() const noexcept
650 {
return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
653 _M_unique() const noexcept
654 {
return this->_M_get_use_count() == 1; }
658 {
return _M_pi ? _M_pi->_M_get_deleter(__ti) :
nullptr; }
661 _M_less(
const __shared_count& __rhs)
const noexcept
665 _M_less(
const __weak_count<_Lp>& __rhs)
const noexcept
670 operator==(
const __shared_count& __a,
const __shared_count& __b) noexcept
671 {
return __a._M_pi == __b._M_pi; }
674 friend class __weak_count<_Lp>;
676 _Sp_counted_base<_Lp>* _M_pi;
680 template<_Lock_policy _Lp>
684 constexpr __weak_count() noexcept : _M_pi(0)
687 __weak_count(
const __shared_count<_Lp>& __r) noexcept
691 _M_pi->_M_weak_add_ref();
694 __weak_count(
const __weak_count<_Lp>& __r) noexcept
698 _M_pi->_M_weak_add_ref();
701 ~__weak_count() noexcept
704 _M_pi->_M_weak_release();
708 operator=(
const __shared_count<_Lp>& __r) noexcept
710 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
712 __tmp->_M_weak_add_ref();
714 _M_pi->_M_weak_release();
720 operator=(
const __weak_count<_Lp>& __r) noexcept
722 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
724 __tmp->_M_weak_add_ref();
726 _M_pi->_M_weak_release();
732 _M_swap(__weak_count<_Lp>& __r) noexcept
734 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
740 _M_get_use_count() const noexcept
741 {
return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
744 _M_less(
const __weak_count& __rhs)
const noexcept
748 _M_less(
const __shared_count<_Lp>& __rhs)
const noexcept
753 operator==(
const __weak_count& __a,
const __weak_count& __b) noexcept
754 {
return __a._M_pi == __b._M_pi; }
757 friend class __shared_count<_Lp>;
759 _Sp_counted_base<_Lp>* _M_pi;
763 template<_Lock_policy _Lp>
764 inline __shared_count<_Lp>:: __shared_count(
const __weak_count<_Lp>& __r)
768 _M_pi->_M_add_ref_lock();
770 __throw_bad_weak_ptr();
777 template<_Lock_policy _Lp,
typename _Tp1,
typename _Tp2>
779 __enable_shared_from_this_helper(
const __shared_count<_Lp>&,
780 const __enable_shared_from_this<_Tp1,
781 _Lp>*,
const _Tp2*) noexcept;
784 template<typename _Tp1, typename _Tp2>
786 __enable_shared_from_this_helper(const __shared_count<>&,
787 const enable_shared_from_this<_Tp1>*,
788 const _Tp2*) noexcept;
790 template<_Lock_policy _Lp>
792 __enable_shared_from_this_helper(const __shared_count<_Lp>&, ...) noexcept
796 template<
typename _Tp, _Lock_policy _Lp>
800 typedef _Tp element_type;
802 constexpr __shared_ptr() noexcept
803 : _M_ptr(0), _M_refcount()
806 template<
typename _Tp1>
807 explicit __shared_ptr(_Tp1* __p)
808 : _M_ptr(__p), _M_refcount(__p)
810 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
811 static_assert( !is_void<_Tp>::value, "incomplete type" );
812 static_assert( sizeof(_Tp1) > 0, "incomplete type" );
813 __enable_shared_from_this_helper(_M_refcount, __p, __p);
816 template<typename _Tp1, typename _Deleter>
817 __shared_ptr(_Tp1* __p, _Deleter __d)
818 : _M_ptr(__p), _M_refcount(__p, __d)
820 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
822 __enable_shared_from_this_helper(_M_refcount, __p, __p);
825 template<typename _Tp1, typename _Deleter, typename _Alloc>
826 __shared_ptr(_Tp1* __p, _Deleter __d, _Alloc __a)
827 : _M_ptr(__p), _M_refcount(__p, __d, std::move(__a))
829 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
831 __enable_shared_from_this_helper(_M_refcount, __p, __p);
834 template<typename _Deleter>
835 __shared_ptr(nullptr_t __p, _Deleter __d)
836 : _M_ptr(0), _M_refcount(__p, __d)
839 template<
typename _Deleter,
typename _Alloc>
840 __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
841 : _M_ptr(0), _M_refcount(__p, __d, std::move(__a))
844 template<
typename _Tp1>
845 __shared_ptr(
const __shared_ptr<_Tp1, _Lp>& __r, _Tp* __p) noexcept
846 : _M_ptr(__p), _M_refcount(__r._M_refcount)
849 __shared_ptr(
const __shared_ptr&) noexcept = default;
850 __shared_ptr& operator=(const __shared_ptr&) noexcept = default;
851 ~__shared_ptr() = default;
853 template<typename _Tp1, typename = typename
854 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
855 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
856 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
859 __shared_ptr(__shared_ptr&& __r) noexcept
860 : _M_ptr(__r._M_ptr), _M_refcount()
862 _M_refcount._M_swap(__r._M_refcount);
866 template<
typename _Tp1,
typename =
typename
867 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
868 __shared_ptr(__shared_ptr<_Tp1, _Lp>&& __r) noexcept
869 : _M_ptr(__r._M_ptr), _M_refcount()
871 _M_refcount._M_swap(__r._M_refcount);
875 template<
typename _Tp1>
876 explicit __shared_ptr(
const __weak_ptr<_Tp1, _Lp>& __r)
877 : _M_refcount(__r._M_refcount)
879 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
887 template<typename _Tp1, typename _Del>
888 __shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r)
889 : _M_ptr(__r.get()), _M_refcount()
891 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
892 auto __raw = _S_raw_ptr(__r.get());
893 _M_refcount = __shared_count<_Lp>(std::move(__r));
894 __enable_shared_from_this_helper(_M_refcount, __raw, __raw);
897 #if _GLIBCXX_USE_DEPRECATED
899 template<
typename _Tp1>
904 constexpr __shared_ptr(nullptr_t) noexcept
905 : _M_ptr(0), _M_refcount()
908 template<
typename _Tp1>
910 operator=(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
913 _M_refcount = __r._M_refcount;
917 #if _GLIBCXX_USE_DEPRECATED
918 template<
typename _Tp1>
922 __shared_ptr(std::move(__r)).swap(*
this);
928 operator=(__shared_ptr&& __r) noexcept
930 __shared_ptr(std::move(__r)).swap(*
this);
936 operator=(__shared_ptr<_Tp1, _Lp>&& __r) noexcept
938 __shared_ptr(std::move(__r)).swap(*
this);
942 template<
typename _Tp1,
typename _Del>
946 __shared_ptr(std::move(__r)).swap(*
this);
952 { __shared_ptr().swap(*
this); }
954 template<
typename _Tp1>
959 _GLIBCXX_DEBUG_ASSERT(__p == 0 || __p != _M_ptr);
960 __shared_ptr(__p).swap(*
this);
963 template<
typename _Tp1,
typename _Deleter>
965 reset(_Tp1* __p, _Deleter __d)
966 { __shared_ptr(__p, __d).swap(*
this); }
968 template<
typename _Tp1,
typename _Deleter,
typename _Alloc>
970 reset(_Tp1* __p, _Deleter __d, _Alloc __a)
971 { __shared_ptr(__p, __d, std::move(__a)).swap(*
this); }
974 typename std::add_lvalue_reference<_Tp>::type
977 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
982 operator->() const noexcept
984 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
992 explicit operator bool() const
993 {
return _M_ptr == 0 ?
false :
true; }
996 unique() const noexcept
997 {
return _M_refcount._M_unique(); }
1000 use_count() const noexcept
1001 {
return _M_refcount._M_get_use_count(); }
1004 swap(__shared_ptr<_Tp, _Lp>& __other) noexcept
1007 _M_refcount._M_swap(__other._M_refcount);
1010 template<
typename _Tp1>
1012 owner_before(__shared_ptr<_Tp1, _Lp>
const& __rhs)
const
1013 {
return _M_refcount._M_less(__rhs._M_refcount); }
1015 template<
typename _Tp1>
1017 owner_before(__weak_ptr<_Tp1, _Lp>
const& __rhs)
const
1018 {
return _M_refcount._M_less(__rhs._M_refcount); }
1023 template<
typename _Alloc,
typename... _Args>
1024 __shared_ptr(_Sp_make_shared_tag __tag,
const _Alloc& __a,
1026 : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a,
1027 std::
forward<_Args>(__args)...)
1031 void* __p = _M_refcount._M_get_deleter(
typeid(__tag));
1032 _M_ptr =
static_cast<_Tp*
>(__p);
1033 __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
1036 template<
typename _Alloc>
1039 void operator()(_Tp* __ptr)
1041 typedef allocator_traits<_Alloc> _Alloc_traits;
1042 _Alloc_traits::destroy(_M_alloc, __ptr);
1043 _Alloc_traits::deallocate(_M_alloc, __ptr, 1);
1048 template<
typename _Alloc,
typename... _Args>
1049 __shared_ptr(_Sp_make_shared_tag __tag,
const _Alloc& __a,
1051 : _M_ptr(), _M_refcount()
1053 typedef typename _Alloc::template rebind<_Tp>::other _Alloc2;
1054 _Deleter<_Alloc2> __del = { _Alloc2(__a) };
1055 typedef allocator_traits<_Alloc2> __traits;
1056 _M_ptr = __traits::allocate(__del._M_alloc, 1);
1061 __traits::construct(__del._M_alloc, _M_ptr,
1062 std::forward<_Args>(__args)...);
1066 __traits::deallocate(__del._M_alloc, _M_ptr, 1);
1067 __throw_exception_again;
1069 __shared_count<_Lp> __count(_M_ptr, __del, __del._M_alloc);
1070 _M_refcount._M_swap(__count);
1071 __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
1075 template<
typename _Tp1, _Lock_policy _Lp1,
typename _Alloc,
1077 friend __shared_ptr<_Tp1, _Lp1>
1078 __allocate_shared(
const _Alloc& __a, _Args&&... __args);
1083 {
return _M_refcount._M_get_deleter(__ti); }
1085 template<
typename _Tp1>
1087 _S_raw_ptr(_Tp1* __ptr)
1090 template<
typename _Tp1>
1095 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
1096 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
1098 template<
typename _Del,
typename _Tp1, _Lock_policy _Lp1>
1099 friend _Del*
get_deleter(
const __shared_ptr<_Tp1, _Lp1>&) noexcept;
1102 __shared_count<_Lp> _M_refcount;
1107 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1109 operator==(const __shared_ptr<_Tp1, _Lp>& __a,
1110 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1111 {
return __a.get() == __b.get(); }
1113 template<
typename _Tp, _Lock_policy _Lp>
1115 operator==(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1118 template<
typename _Tp, _Lock_policy _Lp>
1120 operator==(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1123 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1125 operator!=(
const __shared_ptr<_Tp1, _Lp>& __a,
1126 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1127 {
return __a.get() != __b.get(); }
1129 template<
typename _Tp, _Lock_policy _Lp>
1131 operator!=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1132 {
return (
bool)__a; }
1134 template<
typename _Tp, _Lock_policy _Lp>
1136 operator!=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1137 {
return (
bool)__a; }
1139 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1141 operator<(const __shared_ptr<_Tp1, _Lp>& __a,
1142 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1144 typedef typename std::common_type<_Tp1*, _Tp2*>::type _CT;
1148 template<
typename _Tp, _Lock_policy _Lp>
1150 operator<(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1153 template<
typename _Tp, _Lock_policy _Lp>
1155 operator<(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1158 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1160 operator<=(const __shared_ptr<_Tp1, _Lp>& __a,
1161 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1162 {
return !(__b < __a); }
1164 template<
typename _Tp, _Lock_policy _Lp>
1166 operator<=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1167 {
return !(
nullptr < __a); }
1169 template<
typename _Tp, _Lock_policy _Lp>
1171 operator<=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1172 {
return !(__a <
nullptr); }
1174 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1176 operator>(
const __shared_ptr<_Tp1, _Lp>& __a,
1177 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1178 {
return (__b < __a); }
1180 template<
typename _Tp, _Lock_policy _Lp>
1182 operator>(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1185 template<
typename _Tp, _Lock_policy _Lp>
1187 operator>(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1190 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1192 operator>=(
const __shared_ptr<_Tp1, _Lp>& __a,
1193 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1194 {
return !(__a < __b); }
1196 template<
typename _Tp, _Lock_policy _Lp>
1198 operator>=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1199 {
return !(__a <
nullptr); }
1201 template<
typename _Tp, _Lock_policy _Lp>
1203 operator>=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1204 {
return !(
nullptr < __a); }
1206 template<
typename _Sp>
1207 struct _Sp_less :
public binary_function<_Sp, _Sp, bool>
1210 operator()(
const _Sp& __lhs,
const _Sp& __rhs)
const noexcept
1212 typedef typename _Sp::element_type element_type;
1217 template<
typename _Tp, _Lock_policy _Lp>
1218 struct less<__shared_ptr<_Tp, _Lp>>
1219 :
public _Sp_less<__shared_ptr<_Tp, _Lp>>
1223 template<
typename _Tp, _Lock_policy _Lp>
1225 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b) noexcept
1235 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1236 inline __shared_ptr<_Tp, _Lp>
1237 static_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1238 {
return __shared_ptr<_Tp, _Lp>(__r,
static_cast<_Tp*
>(__r.get())); }
1245 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1246 inline __shared_ptr<_Tp, _Lp>
1247 const_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1248 {
return __shared_ptr<_Tp, _Lp>(__r,
const_cast<_Tp*
>(__r.get())); }
1255 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1256 inline __shared_ptr<_Tp, _Lp>
1257 dynamic_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1259 if (_Tp* __p = dynamic_cast<_Tp*>(__r.get()))
1260 return __shared_ptr<_Tp, _Lp>(__r, __p);
1261 return __shared_ptr<_Tp, _Lp>();
1265 template<
typename _Tp, _Lock_policy _Lp>
1269 typedef _Tp element_type;
1271 constexpr __weak_ptr() noexcept
1272 : _M_ptr(0), _M_refcount()
1275 __weak_ptr(
const __weak_ptr&) noexcept = default;
1276 __weak_ptr& operator=(const __weak_ptr&) noexcept = default;
1277 ~__weak_ptr() = default;
1293 template<typename _Tp1, typename = typename
1294 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
1295 __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r) noexcept
1296 : _M_refcount(__r._M_refcount)
1297 { _M_ptr = __r.lock().get(); }
1299 template<
typename _Tp1,
typename =
typename
1300 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
1301 __weak_ptr(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1302 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1305 template<
typename _Tp1>
1307 operator=(
const __weak_ptr<_Tp1, _Lp>& __r) noexcept
1309 _M_ptr = __r.lock().get();
1310 _M_refcount = __r._M_refcount;
1314 template<
typename _Tp1>
1316 operator=(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1318 _M_ptr = __r._M_ptr;
1319 _M_refcount = __r._M_refcount;
1323 __shared_ptr<_Tp, _Lp>
1324 lock() const noexcept
1329 return __shared_ptr<element_type, _Lp>();
1333 return __shared_ptr<element_type, _Lp>(*this);
1335 __catch(
const bad_weak_ptr&)
1340 return __shared_ptr<element_type, _Lp>();
1345 return expired() ? __shared_ptr<element_type, _Lp>()
1346 : __shared_ptr<element_type, _Lp>(*
this);
1352 use_count() const noexcept
1353 {
return _M_refcount._M_get_use_count(); }
1356 expired() const noexcept
1357 {
return _M_refcount._M_get_use_count() == 0; }
1359 template<
typename _Tp1>
1361 owner_before(
const __shared_ptr<_Tp1, _Lp>& __rhs)
const
1362 {
return _M_refcount._M_less(__rhs._M_refcount); }
1364 template<
typename _Tp1>
1366 owner_before(
const __weak_ptr<_Tp1, _Lp>& __rhs)
const
1367 {
return _M_refcount._M_less(__rhs._M_refcount); }
1371 { __weak_ptr().swap(*
this); }
1374 swap(__weak_ptr& __s) noexcept
1377 _M_refcount._M_swap(__s._M_refcount);
1383 _M_assign(_Tp* __ptr,
const __shared_count<_Lp>& __refcount) noexcept
1386 _M_refcount = __refcount;
1389 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
1390 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
1391 friend class __enable_shared_from_this<_Tp, _Lp>;
1392 friend class enable_shared_from_this<_Tp>;
1395 __weak_count<_Lp> _M_refcount;
1399 template<
typename _Tp, _Lock_policy _Lp>
1401 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b) noexcept
1404 template<
typename _Tp,
typename _Tp1>
1405 struct _Sp_owner_less :
public binary_function<_Tp, _Tp, bool>
1408 operator()(
const _Tp& __lhs,
const _Tp& __rhs)
const
1409 {
return __lhs.owner_before(__rhs); }
1412 operator()(
const _Tp& __lhs,
const _Tp1& __rhs)
const
1413 {
return __lhs.owner_before(__rhs); }
1416 operator()(
const _Tp1& __lhs,
const _Tp& __rhs)
const
1417 {
return __lhs.owner_before(__rhs); }
1420 template<
typename _Tp, _Lock_policy _Lp>
1421 struct owner_less<__shared_ptr<_Tp, _Lp>>
1422 :
public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
1425 template<
typename _Tp, _Lock_policy _Lp>
1426 struct owner_less<__weak_ptr<_Tp, _Lp>>
1427 :
public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
1431 template<
typename _Tp, _Lock_policy _Lp>
1432 class __enable_shared_from_this
1435 constexpr __enable_shared_from_this() noexcept { }
1437 __enable_shared_from_this(
const __enable_shared_from_this&) noexcept { }
1439 __enable_shared_from_this&
1440 operator=(
const __enable_shared_from_this&) noexcept
1443 ~__enable_shared_from_this() { }
1446 __shared_ptr<_Tp, _Lp>
1448 {
return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
1450 __shared_ptr<const _Tp, _Lp>
1451 shared_from_this()
const
1452 {
return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
1455 template<
typename _Tp1>
1457 _M_weak_assign(_Tp1* __p,
const __shared_count<_Lp>& __n)
const noexcept
1458 { _M_weak_this._M_assign(__p, __n); }
1460 template<
typename _Tp1>
1462 __enable_shared_from_this_helper(
const __shared_count<_Lp>& __pn,
1463 const __enable_shared_from_this* __pe,
1464 const _Tp1* __px) noexcept
1467 __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
1470 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
1474 template<
typename _Tp, _Lock_policy _Lp,
typename _Alloc,
typename... _Args>
1475 inline __shared_ptr<_Tp, _Lp>
1476 __allocate_shared(
const _Alloc& __a, _Args&&... __args)
1478 return __shared_ptr<_Tp, _Lp>(_Sp_make_shared_tag(), __a,
1479 std::forward<_Args>(__args)...);
1482 template<
typename _Tp, _Lock_policy _Lp,
typename... _Args>
1483 inline __shared_ptr<_Tp, _Lp>
1484 __make_shared(_Args&&... __args)
1486 typedef typename std::remove_const<_Tp>::type _Tp_nc;
1488 std::forward<_Args>(__args)...);
1492 template<
typename _Tp, _Lock_policy _Lp>
1493 struct hash<__shared_ptr<_Tp, _Lp>>
1494 :
public __hash_base<size_t, __shared_ptr<_Tp, _Lp>>
1497 operator()(
const __shared_ptr<_Tp, _Lp>& __s)
const noexcept
1501 _GLIBCXX_END_NAMESPACE_VERSION
1504 #endif // _SHARED_PTR_BASE_H
Exception possibly thrown by shared_ptr.
20.7.1.2 unique_ptr for single objects.
bitset< _Nb > & reset() noexcept
Sets every bit to false.
Base class for all library exceptions.
static auto construct(_Alloc &__a, _Tp *__p, _Args &&...__args) -> decltype(_S_construct(__a, __p, std::forward< _Args >(__args)...))
Construct an object of type _Tp.
One of the comparison functors.
void lock(_L1 &__l1, _L2 &__l2, _L3 &...__l3)
Generic lock.
_Del * get_deleter(const __shared_ptr< _Tp, _Lp > &__p) noexcept
20.7.2.2.10 shared_ptr get_deleter
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
The standard allocator, as per [20.4].
void swap(function< _Res(_Args...)> &__x, function< _Res(_Args...)> &__y)
Swap the targets of two polymorphic function object wrappers.
complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
virtual char const * what() const noexcept
_Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
static void destroy(_Alloc &__a, _Tp *__p)
Destroy an object of type _Tp.
Partial specializations for pointer types.
Primary class template hash.
A simple smart pointer providing strict ownership semantics.