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
129 _M_get_deleter(
const std::type_info&) noexcept = 0;
133 { __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); }
139 _M_add_ref_lock_nothrow();
142 _M_release() noexcept
145 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count);
146 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
148 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count);
154 if (_Mutex_base<_Lp>::_S_need_barriers)
156 _GLIBCXX_READ_MEM_BARRIER;
157 _GLIBCXX_WRITE_MEM_BARRIER;
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 _GLIBCXX_READ_MEM_BARRIER;
188 _GLIBCXX_WRITE_MEM_BARRIER;
195 _M_get_use_count() const noexcept
199 return __atomic_load_n(&_M_use_count, __ATOMIC_RELAXED);
203 _Sp_counted_base(_Sp_counted_base
const&) =
delete;
204 _Sp_counted_base& operator=(_Sp_counted_base
const&) =
delete;
206 _Atomic_word _M_use_count;
207 _Atomic_word _M_weak_count;
212 _Sp_counted_base<_S_single>::
215 if (_M_use_count == 0)
216 __throw_bad_weak_ptr();
222 _Sp_counted_base<_S_mutex>::
226 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
229 __throw_bad_weak_ptr();
235 _Sp_counted_base<_S_atomic>::
239 _Atomic_word __count = _M_get_use_count();
243 __throw_bad_weak_ptr();
247 while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1,
248 true, __ATOMIC_ACQ_REL,
254 _Sp_counted_base<_S_single>::
255 _M_add_ref_lock_nothrow()
257 if (_M_use_count == 0)
265 _Sp_counted_base<_S_mutex>::
266 _M_add_ref_lock_nothrow()
269 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
279 _Sp_counted_base<_S_atomic>::
280 _M_add_ref_lock_nothrow()
283 _Atomic_word __count = _M_get_use_count();
291 while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1,
292 true, __ATOMIC_ACQ_REL,
299 _Sp_counted_base<_S_single>::_M_add_ref_copy()
304 _Sp_counted_base<_S_single>::_M_release() noexcept
306 if (--_M_use_count == 0)
309 if (--_M_weak_count == 0)
316 _Sp_counted_base<_S_single>::_M_weak_add_ref() noexcept
321 _Sp_counted_base<_S_single>::_M_weak_release() noexcept
323 if (--_M_weak_count == 0)
329 _Sp_counted_base<_S_single>::_M_get_use_count() const noexcept
330 {
return _M_use_count; }
334 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
337 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
340 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
341 class __enable_shared_from_this;
343 template<
typename _Tp>
346 template<
typename _Tp>
349 template<
typename _Tp>
352 template<
typename _Tp>
353 class enable_shared_from_this;
355 template<_Lock_policy _Lp = __default_lock_policy>
358 template<_Lock_policy _Lp = __default_lock_policy>
359 class __shared_count;
363 template<
typename _Ptr, _Lock_policy _Lp>
364 class _Sp_counted_ptr final :
public _Sp_counted_base<_Lp>
368 _Sp_counted_ptr(_Ptr __p) noexcept
372 _M_dispose() noexcept
376 _M_destroy() noexcept
380 _M_get_deleter(
const std::type_info&) noexcept
383 _Sp_counted_ptr(
const _Sp_counted_ptr&) =
delete;
384 _Sp_counted_ptr& operator=(
const _Sp_counted_ptr&) =
delete;
392 _Sp_counted_ptr<nullptr_t, _S_single>::_M_dispose() noexcept { }
396 _Sp_counted_ptr<nullptr_t, _S_mutex>::_M_dispose() noexcept { }
400 _Sp_counted_ptr<nullptr_t, _S_atomic>::_M_dispose() noexcept { }
402 template<
int _Nm,
typename _Tp,
403 bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
404 struct _Sp_ebo_helper;
407 template<
int _Nm,
typename _Tp>
408 struct _Sp_ebo_helper<_Nm, _Tp, true> :
private _Tp
410 explicit _Sp_ebo_helper(
const _Tp& __tp) : _Tp(__tp) { }
413 _S_get(_Sp_ebo_helper& __eboh) {
return static_cast<_Tp&
>(__eboh); }
417 template<
int _Nm,
typename _Tp>
418 struct _Sp_ebo_helper<_Nm, _Tp, false>
420 explicit _Sp_ebo_helper(
const _Tp& __tp) : _M_tp(__tp) { }
423 _S_get(_Sp_ebo_helper& __eboh)
424 {
return __eboh._M_tp; }
431 template<
typename _Ptr,
typename _Deleter,
typename _Alloc, _Lock_policy _Lp>
432 class _Sp_counted_deleter final :
public _Sp_counted_base<_Lp>
434 class _Impl : _Sp_ebo_helper<0, _Deleter>, _Sp_ebo_helper<1, _Alloc>
436 typedef _Sp_ebo_helper<0, _Deleter> _Del_base;
437 typedef _Sp_ebo_helper<1, _Alloc> _Alloc_base;
440 _Impl(_Ptr __p, _Deleter __d,
const _Alloc& __a) noexcept
441 : _M_ptr(__p), _Del_base(__d), _Alloc_base(__a)
444 _Deleter& _M_del() noexcept {
return _Del_base::_S_get(*
this); }
445 _Alloc& _M_alloc() noexcept {
return _Alloc_base::_S_get(*
this); }
452 _Sp_counted_deleter(_Ptr __p, _Deleter __d) noexcept
453 : _M_impl(__p, __d, _Alloc()) { }
456 _Sp_counted_deleter(_Ptr __p, _Deleter __d,
const _Alloc& __a) noexcept
457 : _M_impl(__p, __d, __a) { }
459 ~_Sp_counted_deleter() noexcept { }
462 _M_dispose() noexcept
463 { _M_impl._M_del()(_M_impl._M_ptr); }
466 _M_destroy() noexcept
468 typedef typename allocator_traits<_Alloc>::template
469 rebind_traits<_Sp_counted_deleter> _Alloc_traits;
470 typename _Alloc_traits::allocator_type __a(_M_impl._M_alloc());
471 _Alloc_traits::destroy(__a,
this);
472 _Alloc_traits::deallocate(__a,
this, 1);
476 _M_get_deleter(
const std::type_info& __ti) noexcept
479 return __ti ==
typeid(_Deleter) ? &_M_impl._M_del() :
nullptr;
491 struct _Sp_make_shared_tag { };
493 template<
typename _Tp,
typename _Alloc, _Lock_policy _Lp>
494 class _Sp_counted_ptr_inplace final :
public _Sp_counted_base<_Lp>
496 class _Impl : _Sp_ebo_helper<0, _Alloc>
498 typedef _Sp_ebo_helper<0, _Alloc> _A_base;
501 explicit _Impl(_Alloc __a) noexcept : _A_base(__a) { }
503 _Alloc& _M_alloc() noexcept {
return _A_base::_S_get(*
this); }
505 __gnu_cxx::__aligned_buffer<_Tp> _M_storage;
509 template<
typename... _Args>
510 _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
516 std::forward<_Args>(__args)...);
519 ~_Sp_counted_ptr_inplace() noexcept { }
522 _M_dispose() noexcept
529 _M_destroy() noexcept
531 typedef typename allocator_traits<_Alloc>::template
532 rebind_traits<_Sp_counted_ptr_inplace> _Alloc_traits;
533 typename _Alloc_traits::allocator_type __a(_M_impl._M_alloc());
534 _Alloc_traits::destroy(__a,
this);
535 _Alloc_traits::deallocate(__a,
this, 1);
540 _M_get_deleter(
const std::type_info& __ti) noexcept
543 if (__ti ==
typeid(_Sp_make_shared_tag))
544 return const_cast<typename remove_cv<_Tp>::type*
>(_M_ptr());
550 _Tp* _M_ptr() noexcept {
return _M_impl._M_storage._M_ptr(); }
556 template<_Lock_policy _Lp>
560 constexpr __shared_count() noexcept : _M_pi(0)
563 template<
typename _Ptr>
565 __shared_count(_Ptr __p) : _M_pi(0)
569 _M_pi =
new _Sp_counted_ptr<_Ptr, _Lp>(__p);
574 __throw_exception_again;
578 template<
typename _Ptr,
typename _Deleter>
579 __shared_count(_Ptr __p, _Deleter __d)
580 : __shared_count(__p,
std::
move(__d), allocator<void>())
583 template<
typename _Ptr,
typename _Deleter,
typename _Alloc>
584 __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
586 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
587 typedef typename allocator_traits<_Alloc>::template
588 rebind_traits<_Sp_cd_type> _Alloc_traits;
589 typename _Alloc_traits::allocator_type __a2(__a);
590 _Sp_cd_type* __mem = 0;
593 __mem = _Alloc_traits::allocate(__a2, 1);
594 _Alloc_traits::construct(__a2, __mem,
602 _Alloc_traits::deallocate(__a2, __mem, 1);
603 __throw_exception_again;
607 template<
typename _Tp,
typename _Alloc,
typename... _Args>
608 __shared_count(_Sp_make_shared_tag, _Tp*,
const _Alloc& __a,
612 typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type;
613 typedef typename allocator_traits<_Alloc>::template
614 rebind_traits<_Sp_cp_type> _Alloc_traits;
615 typename _Alloc_traits::allocator_type __a2(__a);
616 _Sp_cp_type* __mem = _Alloc_traits::allocate(__a2, 1);
619 _Alloc_traits::construct(__a2, __mem,
std::move(__a),
620 std::forward<_Args>(__args)...);
625 _Alloc_traits::deallocate(__a2, __mem, 1);
626 __throw_exception_again;
630 #if _GLIBCXX_USE_DEPRECATED
632 template<
typename _Tp>
638 template<
typename _Tp,
typename _Del>
642 using _Ptr =
typename unique_ptr<_Tp, _Del>::pointer;
643 using _Del2 =
typename conditional<is_reference<_Del>::value,
644 reference_wrapper<typename remove_reference<_Del>::type>,
647 = _Sp_counted_deleter<_Ptr, _Del2, allocator<void>, _Lp>;
648 using _Alloc = allocator<_Sp_cd_type>;
649 using _Alloc_traits = allocator_traits<_Alloc>;
651 _Sp_cd_type* __mem = _Alloc_traits::allocate(__a, 1);
652 _Alloc_traits::construct(__a, __mem, __r.release(),
658 explicit __shared_count(
const __weak_count<_Lp>& __r);
661 explicit __shared_count(
const __weak_count<_Lp>& __r, std::nothrow_t);
663 ~__shared_count() noexcept
665 if (_M_pi !=
nullptr)
669 __shared_count(
const __shared_count& __r) noexcept
673 _M_pi->_M_add_ref_copy();
677 operator=(
const __shared_count& __r) noexcept
679 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
683 __tmp->_M_add_ref_copy();
692 _M_swap(__shared_count& __r) noexcept
694 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
700 _M_get_use_count() const noexcept
701 {
return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
704 _M_unique() const noexcept
705 {
return this->_M_get_use_count() == 1; }
708 _M_get_deleter(
const std::type_info& __ti)
const noexcept
709 {
return _M_pi ? _M_pi->_M_get_deleter(__ti) :
nullptr; }
712 _M_less(
const __shared_count& __rhs)
const noexcept
716 _M_less(
const __weak_count<_Lp>& __rhs)
const noexcept
721 operator==(
const __shared_count& __a,
const __shared_count& __b) noexcept
722 {
return __a._M_pi == __b._M_pi; }
725 friend class __weak_count<_Lp>;
727 _Sp_counted_base<_Lp>* _M_pi;
731 template<_Lock_policy _Lp>
735 constexpr __weak_count() noexcept : _M_pi(0)
738 __weak_count(
const __shared_count<_Lp>& __r) noexcept
742 _M_pi->_M_weak_add_ref();
745 __weak_count(
const __weak_count<_Lp>& __r) noexcept
749 _M_pi->_M_weak_add_ref();
752 ~__weak_count() noexcept
755 _M_pi->_M_weak_release();
759 operator=(
const __shared_count<_Lp>& __r) noexcept
761 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
763 __tmp->_M_weak_add_ref();
765 _M_pi->_M_weak_release();
771 operator=(
const __weak_count<_Lp>& __r) noexcept
773 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
775 __tmp->_M_weak_add_ref();
777 _M_pi->_M_weak_release();
783 _M_swap(__weak_count<_Lp>& __r) noexcept
785 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
791 _M_get_use_count() const noexcept
792 {
return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
795 _M_less(
const __weak_count& __rhs)
const noexcept
799 _M_less(
const __shared_count<_Lp>& __rhs)
const noexcept
804 operator==(
const __weak_count& __a,
const __weak_count& __b) noexcept
805 {
return __a._M_pi == __b._M_pi; }
808 friend class __shared_count<_Lp>;
810 _Sp_counted_base<_Lp>* _M_pi;
814 template<_Lock_policy _Lp>
816 __shared_count<_Lp>::__shared_count(
const __weak_count<_Lp>& __r)
819 if (_M_pi !=
nullptr)
820 _M_pi->_M_add_ref_lock();
822 __throw_bad_weak_ptr();
826 template<_Lock_policy _Lp>
828 __shared_count<_Lp>::
829 __shared_count(
const __weak_count<_Lp>& __r, std::nothrow_t)
832 if (_M_pi !=
nullptr)
833 if (!_M_pi->_M_add_ref_lock_nothrow())
840 template<_Lock_policy _Lp,
typename _Tp1,
typename _Tp2>
842 __enable_shared_from_this_helper(
const __shared_count<_Lp>&,
843 const __enable_shared_from_this<_Tp1,
844 _Lp>*,
const _Tp2*) noexcept;
847 template<typename _Tp1, typename _Tp2>
849 __enable_shared_from_this_helper(const __shared_count<>&,
850 const enable_shared_from_this<_Tp1>*,
851 const _Tp2*) noexcept;
853 template<_Lock_policy _Lp>
855 __enable_shared_from_this_helper(const __shared_count<_Lp>&, ...) noexcept
859 template<
typename _Tp, _Lock_policy _Lp>
863 typedef _Tp element_type;
865 constexpr __shared_ptr() noexcept
866 : _M_ptr(0), _M_refcount()
869 template<
typename _Tp1>
870 explicit __shared_ptr(_Tp1* __p)
871 : _M_ptr(__p), _M_refcount(__p)
873 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
874 static_assert( !is_void<_Tp1>::value, "incomplete type" );
875 static_assert( sizeof(_Tp1) > 0, "incomplete type" );
876 __enable_shared_from_this_helper(_M_refcount, __p, __p);
879 template<typename _Tp1, typename _Deleter>
880 __shared_ptr(_Tp1* __p, _Deleter __d)
881 : _M_ptr(__p), _M_refcount(__p, __d)
883 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
885 __enable_shared_from_this_helper(_M_refcount, __p, __p);
888 template<typename _Tp1, typename _Deleter, typename _Alloc>
889 __shared_ptr(_Tp1* __p, _Deleter __d, _Alloc __a)
890 : _M_ptr(__p), _M_refcount(__p, __d,
std::
move(__a))
892 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
894 __enable_shared_from_this_helper(_M_refcount, __p, __p);
897 template<typename _Deleter>
898 __shared_ptr(nullptr_t __p, _Deleter __d)
899 : _M_ptr(0), _M_refcount(__p, __d)
902 template<
typename _Deleter,
typename _Alloc>
903 __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
904 : _M_ptr(0), _M_refcount(__p, __d,
std::
move(__a))
907 template<
typename _Tp1>
908 __shared_ptr(
const __shared_ptr<_Tp1, _Lp>& __r, _Tp* __p) noexcept
909 : _M_ptr(__p), _M_refcount(__r._M_refcount)
912 __shared_ptr(
const __shared_ptr&) noexcept = default;
913 __shared_ptr& operator=(const __shared_ptr&) noexcept = default;
914 ~__shared_ptr() = default;
916 template<typename _Tp1, typename = typename
917 std::enable_if<
std::is_convertible<_Tp1*, _Tp*>::value>::type>
918 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
919 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
922 __shared_ptr(__shared_ptr&& __r) noexcept
923 : _M_ptr(__r._M_ptr), _M_refcount()
925 _M_refcount._M_swap(__r._M_refcount);
929 template<
typename _Tp1,
typename =
typename
930 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
931 __shared_ptr(__shared_ptr<_Tp1, _Lp>&& __r) noexcept
932 : _M_ptr(__r._M_ptr), _M_refcount()
934 _M_refcount._M_swap(__r._M_refcount);
938 template<
typename _Tp1>
939 explicit __shared_ptr(
const __weak_ptr<_Tp1, _Lp>& __r)
940 : _M_refcount(__r._M_refcount)
942 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
950 template<typename _Tp1, typename _Del>
951 __shared_ptr(
std::unique_ptr<_Tp1, _Del>&& __r)
952 : _M_ptr(__r.get()), _M_refcount()
954 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
955 auto __raw = _S_raw_ptr(__r.get());
956 _M_refcount = __shared_count<_Lp>(
std::
move(__r));
957 __enable_shared_from_this_helper(_M_refcount, __raw, __raw);
960 #if _GLIBCXX_USE_DEPRECATED
962 template<
typename _Tp1>
967 constexpr __shared_ptr(nullptr_t) noexcept
968 : _M_ptr(0), _M_refcount()
971 template<
typename _Tp1>
973 operator=(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
976 _M_refcount = __r._M_refcount;
980 #if _GLIBCXX_USE_DEPRECATED
981 template<
typename _Tp1>
985 __shared_ptr(
std::move(__r)).swap(*
this);
991 operator=(__shared_ptr&& __r) noexcept
993 __shared_ptr(
std::move(__r)).swap(*
this);
999 operator=(__shared_ptr<_Tp1, _Lp>&& __r) noexcept
1001 __shared_ptr(
std::move(__r)).swap(*
this);
1005 template<
typename _Tp1,
typename _Del>
1009 __shared_ptr(
std::move(__r)).swap(*
this);
1015 { __shared_ptr().swap(*
this); }
1017 template<
typename _Tp1>
1022 _GLIBCXX_DEBUG_ASSERT(__p == 0 || __p != _M_ptr);
1023 __shared_ptr(__p).swap(*
this);
1026 template<
typename _Tp1,
typename _Deleter>
1028 reset(_Tp1* __p, _Deleter __d)
1029 { __shared_ptr(__p, __d).swap(*
this); }
1031 template<
typename _Tp1,
typename _Deleter,
typename _Alloc>
1033 reset(_Tp1* __p, _Deleter __d, _Alloc __a)
1034 { __shared_ptr(__p, __d,
std::move(__a)).swap(*
this); }
1037 typename std::add_lvalue_reference<_Tp>::type
1038 operator*() const noexcept
1040 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
1045 operator->() const noexcept
1047 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
1052 get()
const noexcept
1055 explicit operator bool() const
1056 {
return _M_ptr == 0 ?
false :
true; }
1059 unique() const noexcept
1060 {
return _M_refcount._M_unique(); }
1063 use_count() const noexcept
1064 {
return _M_refcount._M_get_use_count(); }
1067 swap(__shared_ptr<_Tp, _Lp>& __other) noexcept
1070 _M_refcount._M_swap(__other._M_refcount);
1073 template<
typename _Tp1>
1075 owner_before(__shared_ptr<_Tp1, _Lp>
const& __rhs)
const
1076 {
return _M_refcount._M_less(__rhs._M_refcount); }
1078 template<
typename _Tp1>
1080 owner_before(__weak_ptr<_Tp1, _Lp>
const& __rhs)
const
1081 {
return _M_refcount._M_less(__rhs._M_refcount); }
1086 template<
typename _Alloc,
typename... _Args>
1087 __shared_ptr(_Sp_make_shared_tag __tag,
const _Alloc& __a,
1089 : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a,
1094 void* __p = _M_refcount._M_get_deleter(
typeid(__tag));
1095 _M_ptr =
static_cast<_Tp*
>(__p);
1096 __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
1099 template<
typename _Alloc>
1102 void operator()(_Tp* __ptr)
1104 typedef allocator_traits<_Alloc> _Alloc_traits;
1105 _Alloc_traits::destroy(_M_alloc, __ptr);
1106 _Alloc_traits::deallocate(_M_alloc, __ptr, 1);
1111 template<
typename _Alloc,
typename... _Args>
1112 __shared_ptr(_Sp_make_shared_tag __tag,
const _Alloc& __a,
1114 : _M_ptr(), _M_refcount()
1116 typedef typename _Alloc::template rebind<_Tp>::other _Alloc2;
1117 _Deleter<_Alloc2> __del = { _Alloc2(__a) };
1118 typedef allocator_traits<_Alloc2> __traits;
1119 _M_ptr = __traits::allocate(__del._M_alloc, 1);
1124 __traits::construct(__del._M_alloc, _M_ptr,
1125 std::forward<_Args>(__args)...);
1129 __traits::deallocate(__del._M_alloc, _M_ptr, 1);
1130 __throw_exception_again;
1132 __shared_count<_Lp> __count(_M_ptr, __del, __del._M_alloc);
1133 _M_refcount._M_swap(__count);
1134 __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
1138 template<
typename _Tp1, _Lock_policy _Lp1,
typename _Alloc,
1140 friend __shared_ptr<_Tp1, _Lp1>
1141 __allocate_shared(
const _Alloc& __a, _Args&&... __args);
1145 __shared_ptr(
const __weak_ptr<_Tp, _Lp>& __r, std::nothrow_t)
1146 : _M_refcount(__r._M_refcount,
std::nothrow)
1148 _M_ptr = _M_refcount._M_get_use_count() ? __r._M_ptr :
nullptr;
1151 friend class __weak_ptr<_Tp, _Lp>;
1155 _M_get_deleter(
const std::type_info& __ti)
const noexcept
1156 {
return _M_refcount._M_get_deleter(__ti); }
1158 template<
typename _Tp1>
1160 _S_raw_ptr(_Tp1* __ptr)
1163 template<
typename _Tp1>
1168 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
1169 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
1171 template<
typename _Del,
typename _Tp1, _Lock_policy _Lp1>
1172 friend _Del*
get_deleter(
const __shared_ptr<_Tp1, _Lp1>&) noexcept;
1175 __shared_count<_Lp> _M_refcount;
1180 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1182 operator==(const __shared_ptr<_Tp1, _Lp>& __a,
1183 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1184 {
return __a.get() == __b.get(); }
1186 template<
typename _Tp, _Lock_policy _Lp>
1188 operator==(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1191 template<
typename _Tp, _Lock_policy _Lp>
1193 operator==(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1196 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1198 operator!=(
const __shared_ptr<_Tp1, _Lp>& __a,
1199 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1200 {
return __a.get() != __b.get(); }
1202 template<
typename _Tp, _Lock_policy _Lp>
1204 operator!=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1205 {
return (
bool)__a; }
1207 template<
typename _Tp, _Lock_policy _Lp>
1209 operator!=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1210 {
return (
bool)__a; }
1212 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1214 operator<(const __shared_ptr<_Tp1, _Lp>& __a,
1215 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1217 typedef typename std::common_type<_Tp1*, _Tp2*>::type _CT;
1221 template<
typename _Tp, _Lock_policy _Lp>
1223 operator<(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1226 template<
typename _Tp, _Lock_policy _Lp>
1228 operator<(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1231 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1233 operator<=(const __shared_ptr<_Tp1, _Lp>& __a,
1234 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1235 {
return !(__b < __a); }
1237 template<
typename _Tp, _Lock_policy _Lp>
1239 operator<=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1240 {
return !(
nullptr < __a); }
1242 template<
typename _Tp, _Lock_policy _Lp>
1244 operator<=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1245 {
return !(__a <
nullptr); }
1247 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1249 operator>(
const __shared_ptr<_Tp1, _Lp>& __a,
1250 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1251 {
return (__b < __a); }
1253 template<
typename _Tp, _Lock_policy _Lp>
1255 operator>(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1258 template<
typename _Tp, _Lock_policy _Lp>
1260 operator>(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1263 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1265 operator>=(
const __shared_ptr<_Tp1, _Lp>& __a,
1266 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1267 {
return !(__a < __b); }
1269 template<
typename _Tp, _Lock_policy _Lp>
1271 operator>=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1272 {
return !(__a <
nullptr); }
1274 template<
typename _Tp, _Lock_policy _Lp>
1276 operator>=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1277 {
return !(
nullptr < __a); }
1279 template<
typename _Sp>
1280 struct _Sp_less :
public binary_function<_Sp, _Sp, bool>
1283 operator()(
const _Sp& __lhs,
const _Sp& __rhs)
const noexcept
1285 typedef typename _Sp::element_type element_type;
1290 template<
typename _Tp, _Lock_policy _Lp>
1291 struct less<__shared_ptr<_Tp, _Lp>>
1292 :
public _Sp_less<__shared_ptr<_Tp, _Lp>>
1296 template<
typename _Tp, _Lock_policy _Lp>
1298 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b) noexcept
1308 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1309 inline __shared_ptr<_Tp, _Lp>
1310 static_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1311 {
return __shared_ptr<_Tp, _Lp>(__r,
static_cast<_Tp*
>(__r.get())); }
1318 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1319 inline __shared_ptr<_Tp, _Lp>
1320 const_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1321 {
return __shared_ptr<_Tp, _Lp>(__r,
const_cast<_Tp*
>(__r.get())); }
1328 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1329 inline __shared_ptr<_Tp, _Lp>
1330 dynamic_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1332 if (_Tp* __p = dynamic_cast<_Tp*>(__r.get()))
1333 return __shared_ptr<_Tp, _Lp>(__r, __p);
1334 return __shared_ptr<_Tp, _Lp>();
1338 template<
typename _Tp, _Lock_policy _Lp>
1342 typedef _Tp element_type;
1344 constexpr __weak_ptr() noexcept
1345 : _M_ptr(0), _M_refcount()
1348 __weak_ptr(
const __weak_ptr&) noexcept = default;
1349 __weak_ptr& operator=(const __weak_ptr&) noexcept = default;
1350 ~__weak_ptr() = default;
1366 template<typename _Tp1, typename = typename
1367 std::enable_if<
std::is_convertible<_Tp1*, _Tp*>::value>::type>
1368 __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r) noexcept
1369 : _M_refcount(__r._M_refcount)
1370 { _M_ptr = __r.lock().get(); }
1372 template<
typename _Tp1,
typename =
typename
1373 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
1374 __weak_ptr(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1375 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1378 template<
typename _Tp1>
1380 operator=(
const __weak_ptr<_Tp1, _Lp>& __r) noexcept
1382 _M_ptr = __r.lock().get();
1383 _M_refcount = __r._M_refcount;
1387 template<
typename _Tp1>
1389 operator=(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1391 _M_ptr = __r._M_ptr;
1392 _M_refcount = __r._M_refcount;
1396 __shared_ptr<_Tp, _Lp>
1397 lock() const noexcept
1398 {
return __shared_ptr<element_type, _Lp>(*
this, std::nothrow); }
1401 use_count() const noexcept
1402 {
return _M_refcount._M_get_use_count(); }
1405 expired() const noexcept
1406 {
return _M_refcount._M_get_use_count() == 0; }
1408 template<
typename _Tp1>
1410 owner_before(
const __shared_ptr<_Tp1, _Lp>& __rhs)
const
1411 {
return _M_refcount._M_less(__rhs._M_refcount); }
1413 template<
typename _Tp1>
1415 owner_before(
const __weak_ptr<_Tp1, _Lp>& __rhs)
const
1416 {
return _M_refcount._M_less(__rhs._M_refcount); }
1420 { __weak_ptr().swap(*
this); }
1423 swap(__weak_ptr& __s) noexcept
1426 _M_refcount._M_swap(__s._M_refcount);
1432 _M_assign(_Tp* __ptr,
const __shared_count<_Lp>& __refcount) noexcept
1435 _M_refcount = __refcount;
1438 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
1439 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
1440 friend class __enable_shared_from_this<_Tp, _Lp>;
1441 friend class enable_shared_from_this<_Tp>;
1444 __weak_count<_Lp> _M_refcount;
1448 template<
typename _Tp, _Lock_policy _Lp>
1450 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b) noexcept
1453 template<
typename _Tp,
typename _Tp1>
1454 struct _Sp_owner_less :
public binary_function<_Tp, _Tp, bool>
1457 operator()(
const _Tp& __lhs,
const _Tp& __rhs)
const
1458 {
return __lhs.owner_before(__rhs); }
1461 operator()(
const _Tp& __lhs,
const _Tp1& __rhs)
const
1462 {
return __lhs.owner_before(__rhs); }
1465 operator()(
const _Tp1& __lhs,
const _Tp& __rhs)
const
1466 {
return __lhs.owner_before(__rhs); }
1469 template<
typename _Tp, _Lock_policy _Lp>
1470 struct owner_less<__shared_ptr<_Tp, _Lp>>
1471 :
public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
1474 template<
typename _Tp, _Lock_policy _Lp>
1475 struct owner_less<__weak_ptr<_Tp, _Lp>>
1476 :
public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
1480 template<
typename _Tp, _Lock_policy _Lp>
1481 class __enable_shared_from_this
1484 constexpr __enable_shared_from_this() noexcept { }
1486 __enable_shared_from_this(
const __enable_shared_from_this&) noexcept { }
1488 __enable_shared_from_this&
1489 operator=(
const __enable_shared_from_this&) noexcept
1492 ~__enable_shared_from_this() { }
1495 __shared_ptr<_Tp, _Lp>
1497 {
return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
1499 __shared_ptr<const _Tp, _Lp>
1500 shared_from_this()
const
1501 {
return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
1504 template<
typename _Tp1>
1506 _M_weak_assign(_Tp1* __p,
const __shared_count<_Lp>& __n)
const noexcept
1507 { _M_weak_this._M_assign(__p, __n); }
1509 template<
typename _Tp1>
1511 __enable_shared_from_this_helper(
const __shared_count<_Lp>& __pn,
1512 const __enable_shared_from_this* __pe,
1513 const _Tp1* __px) noexcept
1516 __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
1519 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
1523 template<
typename _Tp, _Lock_policy _Lp,
typename _Alloc,
typename... _Args>
1524 inline __shared_ptr<_Tp, _Lp>
1525 __allocate_shared(
const _Alloc& __a, _Args&&... __args)
1527 return __shared_ptr<_Tp, _Lp>(_Sp_make_shared_tag(), __a,
1528 std::forward<_Args>(__args)...);
1531 template<
typename _Tp, _Lock_policy _Lp,
typename... _Args>
1532 inline __shared_ptr<_Tp, _Lp>
1533 __make_shared(_Args&&... __args)
1535 typedef typename std::remove_const<_Tp>::type _Tp_nc;
1537 std::forward<_Args>(__args)...);
1541 template<
typename _Tp, _Lock_policy _Lp>
1542 struct hash<__shared_ptr<_Tp, _Lp>>
1543 :
public __hash_base<size_t, __shared_ptr<_Tp, _Lp>>
1546 operator()(
const __shared_ptr<_Tp, _Lp>& __s)
const noexcept
1550 _GLIBCXX_END_NAMESPACE_VERSION
1553 #endif // _SHARED_PTR_BASE_H
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Partial specializations for pointer 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.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
static void destroy(_Alloc &__a, _Tp *__p)
Destroy an object of type _Tp.
A simple smart pointer providing strict ownership semantics.
Exception possibly thrown by shared_ptr.
ISO C++ entities toplevel namespace is std.
The standard allocator, as per [20.4].
bool operator>=(const basic_string< _CharT, _Traits, _Alloc > &__lhs, const basic_string< _CharT, _Traits, _Alloc > &__rhs)
Test if string doesn't precede string.
_Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Primary class template hash.
bool operator>(const basic_string< _CharT, _Traits, _Alloc > &__lhs, const basic_string< _CharT, _Traits, _Alloc > &__rhs)
Test if string follows string.
void swap(_Tp &, _Tp &) noexcept(__and_< is_nothrow_move_constructible< _Tp >, is_nothrow_move_assignable< _Tp >>::value)
Swaps two values.
One of the comparison functors.
_Del * get_deleter(const __shared_ptr< _Tp, _Lp > &__p) noexcept
20.7.2.2.10 shared_ptr get_deleter
20.7.1.2 unique_ptr for single objects.