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 (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 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_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 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
256 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
259 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
260 class __enable_shared_from_this;
262 template<
typename _Tp>
265 template<
typename _Tp>
268 template<
typename _Tp>
271 template<
typename _Tp>
272 class enable_shared_from_this;
274 template<_Lock_policy _Lp = __default_lock_policy>
277 template<_Lock_policy _Lp = __default_lock_policy>
278 class __shared_count;
282 template<
typename _Ptr, _Lock_policy _Lp>
283 class _Sp_counted_ptr final :
public _Sp_counted_base<_Lp>
287 _Sp_counted_ptr(_Ptr __p) noexcept
291 _M_dispose() noexcept
295 _M_destroy() noexcept
302 _Sp_counted_ptr(
const _Sp_counted_ptr&) =
delete;
303 _Sp_counted_ptr& operator=(
const _Sp_counted_ptr&) =
delete;
311 _Sp_counted_ptr<nullptr_t, _S_single>::_M_dispose() noexcept { }
315 _Sp_counted_ptr<nullptr_t, _S_mutex>::_M_dispose() noexcept { }
319 _Sp_counted_ptr<nullptr_t, _S_atomic>::_M_dispose() noexcept { }
321 template<
int _Nm,
typename _Tp,
322 bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
323 struct _Sp_ebo_helper;
326 template<
int _Nm,
typename _Tp>
327 struct _Sp_ebo_helper<_Nm, _Tp, true> :
private _Tp
329 explicit _Sp_ebo_helper(
const _Tp& __tp) : _Tp(__tp) { }
332 _S_get(_Sp_ebo_helper& __eboh) {
return static_cast<_Tp&
>(__eboh); }
336 template<
int _Nm,
typename _Tp>
337 struct _Sp_ebo_helper<_Nm, _Tp, false>
339 explicit _Sp_ebo_helper(
const _Tp& __tp) : _M_tp(__tp) { }
342 _S_get(_Sp_ebo_helper& __eboh)
343 {
return __eboh._M_tp; }
350 template<
typename _Ptr,
typename _Deleter,
typename _Alloc, _Lock_policy _Lp>
351 class _Sp_counted_deleter final :
public _Sp_counted_base<_Lp>
353 class _Impl : _Sp_ebo_helper<0, _Deleter>, _Sp_ebo_helper<1, _Alloc>
355 typedef _Sp_ebo_helper<0, _Deleter> _Del_base;
356 typedef _Sp_ebo_helper<1, _Alloc> _Alloc_base;
359 _Impl(_Ptr __p, _Deleter __d,
const _Alloc& __a) noexcept
360 : _M_ptr(__p), _Del_base(__d), _Alloc_base(__a)
363 _Deleter& _M_del() noexcept {
return _Del_base::_S_get(*
this); }
364 _Alloc& _M_alloc() noexcept {
return _Alloc_base::_S_get(*
this); }
371 _Sp_counted_deleter(_Ptr __p, _Deleter __d) noexcept
372 : _M_impl(__p, __d, _Alloc()) { }
375 _Sp_counted_deleter(_Ptr __p, _Deleter __d,
const _Alloc& __a) noexcept
376 : _M_impl(__p, __d, __a) { }
378 ~_Sp_counted_deleter() noexcept { }
381 _M_dispose() noexcept
382 { _M_impl._M_del()(_M_impl._M_ptr); }
385 _M_destroy() noexcept
387 typedef typename allocator_traits<_Alloc>::template
388 rebind_traits<_Sp_counted_deleter> _Alloc_traits;
389 typename _Alloc_traits::allocator_type __a(_M_impl._M_alloc());
390 _Alloc_traits::destroy(__a,
this);
391 _Alloc_traits::deallocate(__a,
this, 1);
398 return __ti ==
typeid(_Deleter) ? &_M_impl._M_del() :
nullptr;
410 struct _Sp_make_shared_tag { };
412 template<
typename _Tp,
typename _Alloc, _Lock_policy _Lp>
413 class _Sp_counted_ptr_inplace final :
public _Sp_counted_base<_Lp>
415 class _Impl : _Sp_ebo_helper<0, _Alloc>
417 typedef _Sp_ebo_helper<0, _Alloc> _A_base;
420 explicit _Impl(_Alloc __a) noexcept : _A_base(__a) { }
422 _Alloc& _M_alloc() noexcept {
return _A_base::_S_get(*
this); }
424 __gnu_cxx::__aligned_buffer<_Tp> _M_storage;
428 template<
typename... _Args>
429 _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
435 std::forward<_Args>(__args)...);
438 ~_Sp_counted_ptr_inplace() noexcept { }
441 _M_dispose() noexcept
448 _M_destroy() noexcept
450 typedef typename allocator_traits<_Alloc>::template
451 rebind_traits<_Sp_counted_ptr_inplace> _Alloc_traits;
452 typename _Alloc_traits::allocator_type __a(_M_impl._M_alloc());
453 _Alloc_traits::destroy(__a,
this);
454 _Alloc_traits::deallocate(__a,
this, 1);
462 return __ti ==
typeid(_Sp_make_shared_tag) ? _M_ptr() :
nullptr;
469 _Tp* _M_ptr() noexcept {
return _M_impl._M_storage._M_ptr(); }
475 template<_Lock_policy _Lp>
479 constexpr __shared_count() noexcept : _M_pi(0)
482 template<
typename _Ptr>
484 __shared_count(_Ptr __p) : _M_pi(0)
488 _M_pi =
new _Sp_counted_ptr<_Ptr, _Lp>(__p);
493 __throw_exception_again;
497 template<
typename _Ptr,
typename _Deleter>
498 __shared_count(_Ptr __p, _Deleter __d) : _M_pi(0)
502 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
503 typedef typename allocator_traits<_Alloc>::template
504 rebind_traits<_Sp_cd_type> _Alloc_traits;
505 typename _Alloc_traits::allocator_type __a;
506 _Sp_cd_type* __mem = 0;
509 __mem = _Alloc_traits::allocate(__a, 1);
510 _Alloc_traits::construct(__a, __mem, __p, std::move(__d));
517 _Alloc_traits::deallocate(__a, __mem, 1);
518 __throw_exception_again;
522 template<
typename _Ptr,
typename _Deleter,
typename _Alloc>
523 __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
525 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
526 typedef typename allocator_traits<_Alloc>::template
527 rebind_traits<_Sp_cd_type> _Alloc_traits;
528 typename _Alloc_traits::allocator_type __a2(__a);
529 _Sp_cd_type* __mem = 0;
532 __mem = _Alloc_traits::allocate(__a2, 1);
533 _Alloc_traits::construct(__a2, __mem,
534 __p, std::move(__d), std::move(__a));
541 _Alloc_traits::deallocate(__a2, __mem, 1);
542 __throw_exception_again;
546 template<
typename _Tp,
typename _Alloc,
typename... _Args>
547 __shared_count(_Sp_make_shared_tag, _Tp*,
const _Alloc& __a,
551 typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type;
552 typedef typename allocator_traits<_Alloc>::template
553 rebind_traits<_Sp_cp_type> _Alloc_traits;
554 typename _Alloc_traits::allocator_type __a2(__a);
555 _Sp_cp_type* __mem = _Alloc_traits::allocate(__a2, 1);
558 _Alloc_traits::construct(__a2, __mem, std::move(__a),
559 std::forward<_Args>(__args)...);
564 _Alloc_traits::deallocate(__a2, __mem, 1);
565 __throw_exception_again;
569 #if _GLIBCXX_USE_DEPRECATED
571 template<
typename _Tp>
577 template<
typename _Tp,
typename _Del>
580 : _M_pi(_S_create_from_up(std::move(__r)))
584 explicit __shared_count(
const __weak_count<_Lp>& __r);
586 ~__shared_count() noexcept
592 __shared_count(
const __shared_count& __r) noexcept
596 _M_pi->_M_add_ref_copy();
600 operator=(
const __shared_count& __r) noexcept
602 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
606 __tmp->_M_add_ref_copy();
615 _M_swap(__shared_count& __r) noexcept
617 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
623 _M_get_use_count() const noexcept
624 {
return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
627 _M_unique() const noexcept
628 {
return this->_M_get_use_count() == 1; }
632 {
return _M_pi ? _M_pi->_M_get_deleter(__ti) :
nullptr; }
635 _M_less(
const __shared_count& __rhs)
const noexcept
639 _M_less(
const __weak_count<_Lp>& __rhs)
const noexcept
644 operator==(
const __shared_count& __a,
const __shared_count& __b) noexcept
645 {
return __a._M_pi == __b._M_pi; }
648 friend class __weak_count<_Lp>;
650 template<
typename _Tp,
typename _Del>
651 static _Sp_counted_base<_Lp>*
655 typedef typename unique_ptr<_Tp, _Del>::pointer _Ptr;
656 return new _Sp_counted_deleter<_Ptr, _Del, std::allocator<void>,
657 _Lp>(__r.get(), __r.get_deleter());
660 template<
typename _Tp,
typename _Del>
661 static _Sp_counted_base<_Lp>*
665 typedef typename unique_ptr<_Tp, _Del>::pointer _Ptr;
666 typedef typename std::remove_reference<_Del>::type _Del1;
668 return new _Sp_counted_deleter<_Ptr, _Del2, std::allocator<void>,
669 _Lp>(__r.get(),
std::ref(__r.get_deleter()));
672 _Sp_counted_base<_Lp>* _M_pi;
676 template<_Lock_policy _Lp>
680 constexpr __weak_count() noexcept : _M_pi(0)
683 __weak_count(
const __shared_count<_Lp>& __r) noexcept
687 _M_pi->_M_weak_add_ref();
690 __weak_count(
const __weak_count<_Lp>& __r) noexcept
694 _M_pi->_M_weak_add_ref();
697 ~__weak_count() noexcept
700 _M_pi->_M_weak_release();
704 operator=(
const __shared_count<_Lp>& __r) noexcept
706 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
708 __tmp->_M_weak_add_ref();
710 _M_pi->_M_weak_release();
716 operator=(
const __weak_count<_Lp>& __r) noexcept
718 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
720 __tmp->_M_weak_add_ref();
722 _M_pi->_M_weak_release();
728 _M_swap(__weak_count<_Lp>& __r) noexcept
730 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
736 _M_get_use_count() const noexcept
737 {
return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
740 _M_less(
const __weak_count& __rhs)
const noexcept
744 _M_less(
const __shared_count<_Lp>& __rhs)
const noexcept
749 operator==(
const __weak_count& __a,
const __weak_count& __b) noexcept
750 {
return __a._M_pi == __b._M_pi; }
753 friend class __shared_count<_Lp>;
755 _Sp_counted_base<_Lp>* _M_pi;
759 template<_Lock_policy _Lp>
760 inline __shared_count<_Lp>:: __shared_count(
const __weak_count<_Lp>& __r)
764 _M_pi->_M_add_ref_lock();
766 __throw_bad_weak_ptr();
773 template<_Lock_policy _Lp,
typename _Tp1,
typename _Tp2>
775 __enable_shared_from_this_helper(
const __shared_count<_Lp>&,
776 const __enable_shared_from_this<_Tp1,
777 _Lp>*,
const _Tp2*) noexcept;
780 template<typename _Tp1, typename _Tp2>
782 __enable_shared_from_this_helper(const __shared_count<>&,
783 const enable_shared_from_this<_Tp1>*,
784 const _Tp2*) noexcept;
786 template<_Lock_policy _Lp>
788 __enable_shared_from_this_helper(const __shared_count<_Lp>&, ...) noexcept
792 template<
typename _Tp, _Lock_policy _Lp>
796 typedef _Tp element_type;
798 constexpr __shared_ptr() noexcept
799 : _M_ptr(0), _M_refcount()
802 template<
typename _Tp1>
803 explicit __shared_ptr(_Tp1* __p)
804 : _M_ptr(__p), _M_refcount(__p)
806 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
807 static_assert( sizeof(_Tp1) > 0, "incomplete type" );
808 __enable_shared_from_this_helper(_M_refcount, __p, __p);
811 template<typename _Tp1, typename _Deleter>
812 __shared_ptr(_Tp1* __p, _Deleter __d)
813 : _M_ptr(__p), _M_refcount(__p, __d)
815 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
817 __enable_shared_from_this_helper(_M_refcount, __p, __p);
820 template<typename _Tp1, typename _Deleter, typename _Alloc>
821 __shared_ptr(_Tp1* __p, _Deleter __d, _Alloc __a)
822 : _M_ptr(__p), _M_refcount(__p, __d, std::move(__a))
824 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
826 __enable_shared_from_this_helper(_M_refcount, __p, __p);
829 template<typename _Deleter>
830 __shared_ptr(nullptr_t __p, _Deleter __d)
831 : _M_ptr(0), _M_refcount(__p, __d)
834 template<
typename _Deleter,
typename _Alloc>
835 __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
836 : _M_ptr(0), _M_refcount(__p, __d, std::move(__a))
839 template<
typename _Tp1>
840 __shared_ptr(
const __shared_ptr<_Tp1, _Lp>& __r, _Tp* __p) noexcept
841 : _M_ptr(__p), _M_refcount(__r._M_refcount)
844 __shared_ptr(
const __shared_ptr&) noexcept = default;
845 __shared_ptr& operator=(const __shared_ptr&) noexcept = default;
846 ~__shared_ptr() = default;
848 template<typename _Tp1, typename = typename
849 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
850 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
851 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
854 __shared_ptr(__shared_ptr&& __r) noexcept
855 : _M_ptr(__r._M_ptr), _M_refcount()
857 _M_refcount._M_swap(__r._M_refcount);
861 template<
typename _Tp1,
typename =
typename
862 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
863 __shared_ptr(__shared_ptr<_Tp1, _Lp>&& __r) noexcept
864 : _M_ptr(__r._M_ptr), _M_refcount()
866 _M_refcount._M_swap(__r._M_refcount);
870 template<
typename _Tp1>
871 explicit __shared_ptr(
const __weak_ptr<_Tp1, _Lp>& __r)
872 : _M_refcount(__r._M_refcount)
874 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
882 template<typename _Tp1, typename _Del>
883 __shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r)
884 : _M_ptr(__r.get()), _M_refcount()
886 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
888 _M_refcount = __shared_count<_Lp>(std::move(__r));
889 __enable_shared_from_this_helper(_M_refcount, __tmp, __tmp);
892 #if _GLIBCXX_USE_DEPRECATED
894 template<
typename _Tp1>
899 constexpr __shared_ptr(nullptr_t) noexcept
900 : _M_ptr(0), _M_refcount()
903 template<
typename _Tp1>
905 operator=(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
908 _M_refcount = __r._M_refcount;
912 #if _GLIBCXX_USE_DEPRECATED
913 template<
typename _Tp1>
917 __shared_ptr(std::move(__r)).swap(*
this);
923 operator=(__shared_ptr&& __r) noexcept
925 __shared_ptr(std::move(__r)).swap(*
this);
931 operator=(__shared_ptr<_Tp1, _Lp>&& __r) noexcept
933 __shared_ptr(std::move(__r)).swap(*
this);
937 template<
typename _Tp1,
typename _Del>
941 __shared_ptr(std::move(__r)).swap(*
this);
947 { __shared_ptr().swap(*
this); }
949 template<
typename _Tp1>
954 _GLIBCXX_DEBUG_ASSERT(__p == 0 || __p != _M_ptr);
955 __shared_ptr(__p).swap(*
this);
958 template<
typename _Tp1,
typename _Deleter>
960 reset(_Tp1* __p, _Deleter __d)
961 { __shared_ptr(__p, __d).swap(*
this); }
963 template<
typename _Tp1,
typename _Deleter,
typename _Alloc>
965 reset(_Tp1* __p, _Deleter __d, _Alloc __a)
966 { __shared_ptr(__p, __d, std::move(__a)).swap(*
this); }
969 typename std::add_lvalue_reference<_Tp>::type
972 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
977 operator->() const noexcept
979 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
987 explicit operator bool() const
988 {
return _M_ptr == 0 ?
false :
true; }
991 unique() const noexcept
992 {
return _M_refcount._M_unique(); }
995 use_count() const noexcept
996 {
return _M_refcount._M_get_use_count(); }
999 swap(__shared_ptr<_Tp, _Lp>& __other) noexcept
1001 std::swap(_M_ptr, __other._M_ptr);
1002 _M_refcount._M_swap(__other._M_refcount);
1005 template<
typename _Tp1>
1007 owner_before(__shared_ptr<_Tp1, _Lp>
const& __rhs)
const
1008 {
return _M_refcount._M_less(__rhs._M_refcount); }
1010 template<
typename _Tp1>
1012 owner_before(__weak_ptr<_Tp1, _Lp>
const& __rhs)
const
1013 {
return _M_refcount._M_less(__rhs._M_refcount); }
1018 template<
typename _Alloc,
typename... _Args>
1019 __shared_ptr(_Sp_make_shared_tag __tag,
const _Alloc& __a,
1021 : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a,
1022 std::
forward<_Args>(__args)...)
1026 void* __p = _M_refcount._M_get_deleter(
typeid(__tag));
1027 _M_ptr =
static_cast<_Tp*
>(__p);
1028 __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
1031 template<
typename _Alloc>
1034 void operator()(_Tp* __ptr)
1036 typedef allocator_traits<_Alloc> _Alloc_traits;
1037 _Alloc_traits::destroy(_M_alloc, __ptr);
1038 _Alloc_traits::deallocate(_M_alloc, __ptr, 1);
1043 template<
typename _Alloc,
typename... _Args>
1044 __shared_ptr(_Sp_make_shared_tag __tag,
const _Alloc& __a,
1046 : _M_ptr(), _M_refcount()
1048 typedef typename _Alloc::template rebind<_Tp>::other _Alloc2;
1049 _Deleter<_Alloc2> __del = { _Alloc2(__a) };
1050 typedef allocator_traits<_Alloc2> __traits;
1051 _M_ptr = __traits::allocate(__del._M_alloc, 1);
1056 __traits::construct(__del._M_alloc, _M_ptr,
1057 std::forward<_Args>(__args)...);
1061 __traits::deallocate(__del._M_alloc, _M_ptr, 1);
1062 __throw_exception_again;
1064 __shared_count<_Lp> __count(_M_ptr, __del, __del._M_alloc);
1065 _M_refcount._M_swap(__count);
1066 __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
1070 template<
typename _Tp1, _Lock_policy _Lp1,
typename _Alloc,
1072 friend __shared_ptr<_Tp1, _Lp1>
1073 __allocate_shared(
const _Alloc& __a, _Args&&... __args);
1078 {
return _M_refcount._M_get_deleter(__ti); }
1080 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
1081 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
1083 template<
typename _Del,
typename _Tp1, _Lock_policy _Lp1>
1084 friend _Del*
get_deleter(
const __shared_ptr<_Tp1, _Lp1>&) noexcept;
1087 __shared_count<_Lp> _M_refcount;
1092 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1094 operator==(const __shared_ptr<_Tp1, _Lp>& __a,
1095 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1096 {
return __a.get() == __b.get(); }
1098 template<
typename _Tp, _Lock_policy _Lp>
1100 operator==(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1103 template<
typename _Tp, _Lock_policy _Lp>
1105 operator==(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1108 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1110 operator!=(
const __shared_ptr<_Tp1, _Lp>& __a,
1111 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1112 {
return __a.get() != __b.get(); }
1114 template<
typename _Tp, _Lock_policy _Lp>
1116 operator!=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1117 {
return (
bool)__a; }
1119 template<
typename _Tp, _Lock_policy _Lp>
1121 operator!=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1122 {
return (
bool)__a; }
1124 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1126 operator<(const __shared_ptr<_Tp1, _Lp>& __a,
1127 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1129 typedef typename std::common_type<_Tp1*, _Tp2*>::type _CT;
1133 template<
typename _Tp, _Lock_policy _Lp>
1135 operator<(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1138 template<
typename _Tp, _Lock_policy _Lp>
1140 operator<(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1143 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1145 operator<=(const __shared_ptr<_Tp1, _Lp>& __a,
1146 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1147 {
return !(__b < __a); }
1149 template<
typename _Tp, _Lock_policy _Lp>
1151 operator<=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1152 {
return !(
nullptr < __a); }
1154 template<
typename _Tp, _Lock_policy _Lp>
1156 operator<=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1157 {
return !(__a <
nullptr); }
1159 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1161 operator>(
const __shared_ptr<_Tp1, _Lp>& __a,
1162 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1163 {
return (__b < __a); }
1165 template<
typename _Tp, _Lock_policy _Lp>
1167 operator>(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1170 template<
typename _Tp, _Lock_policy _Lp>
1172 operator>(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1175 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1177 operator>=(
const __shared_ptr<_Tp1, _Lp>& __a,
1178 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1179 {
return !(__a < __b); }
1181 template<
typename _Tp, _Lock_policy _Lp>
1183 operator>=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1184 {
return !(__a <
nullptr); }
1186 template<
typename _Tp, _Lock_policy _Lp>
1188 operator>=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1189 {
return !(
nullptr < __a); }
1191 template<
typename _Sp>
1192 struct _Sp_less :
public binary_function<_Sp, _Sp, bool>
1195 operator()(
const _Sp& __lhs,
const _Sp& __rhs)
const noexcept
1197 typedef typename _Sp::element_type element_type;
1202 template<
typename _Tp, _Lock_policy _Lp>
1203 struct less<__shared_ptr<_Tp, _Lp>>
1204 :
public _Sp_less<__shared_ptr<_Tp, _Lp>>
1208 template<
typename _Tp, _Lock_policy _Lp>
1210 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b) noexcept
1220 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1221 inline __shared_ptr<_Tp, _Lp>
1222 static_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1223 {
return __shared_ptr<_Tp, _Lp>(__r,
static_cast<_Tp*
>(__r.get())); }
1230 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1231 inline __shared_ptr<_Tp, _Lp>
1232 const_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1233 {
return __shared_ptr<_Tp, _Lp>(__r,
const_cast<_Tp*
>(__r.get())); }
1240 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1241 inline __shared_ptr<_Tp, _Lp>
1242 dynamic_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1244 if (_Tp* __p = dynamic_cast<_Tp*>(__r.get()))
1245 return __shared_ptr<_Tp, _Lp>(__r, __p);
1246 return __shared_ptr<_Tp, _Lp>();
1250 template<
typename _Tp, _Lock_policy _Lp>
1254 typedef _Tp element_type;
1256 constexpr __weak_ptr() noexcept
1257 : _M_ptr(0), _M_refcount()
1260 __weak_ptr(
const __weak_ptr&) noexcept = default;
1261 __weak_ptr& operator=(const __weak_ptr&) noexcept = default;
1262 ~__weak_ptr() = default;
1278 template<typename _Tp1, typename = typename
1279 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
1280 __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r) noexcept
1281 : _M_refcount(__r._M_refcount)
1282 { _M_ptr = __r.lock().get(); }
1284 template<
typename _Tp1,
typename =
typename
1285 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
1286 __weak_ptr(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1287 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1290 template<
typename _Tp1>
1292 operator=(
const __weak_ptr<_Tp1, _Lp>& __r) noexcept
1294 _M_ptr = __r.lock().get();
1295 _M_refcount = __r._M_refcount;
1299 template<
typename _Tp1>
1301 operator=(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1303 _M_ptr = __r._M_ptr;
1304 _M_refcount = __r._M_refcount;
1308 __shared_ptr<_Tp, _Lp>
1309 lock() const noexcept
1314 return __shared_ptr<element_type, _Lp>();
1318 return __shared_ptr<element_type, _Lp>(*this);
1320 __catch(
const bad_weak_ptr&)
1325 return __shared_ptr<element_type, _Lp>();
1330 return expired() ? __shared_ptr<element_type, _Lp>()
1331 : __shared_ptr<element_type, _Lp>(*
this);
1337 use_count() const noexcept
1338 {
return _M_refcount._M_get_use_count(); }
1341 expired() const noexcept
1342 {
return _M_refcount._M_get_use_count() == 0; }
1344 template<
typename _Tp1>
1346 owner_before(
const __shared_ptr<_Tp1, _Lp>& __rhs)
const
1347 {
return _M_refcount._M_less(__rhs._M_refcount); }
1349 template<
typename _Tp1>
1351 owner_before(
const __weak_ptr<_Tp1, _Lp>& __rhs)
const
1352 {
return _M_refcount._M_less(__rhs._M_refcount); }
1356 { __weak_ptr().swap(*
this); }
1359 swap(__weak_ptr& __s) noexcept
1361 std::swap(_M_ptr, __s._M_ptr);
1362 _M_refcount._M_swap(__s._M_refcount);
1368 _M_assign(_Tp* __ptr,
const __shared_count<_Lp>& __refcount) noexcept
1371 _M_refcount = __refcount;
1374 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
1375 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
1376 friend class __enable_shared_from_this<_Tp, _Lp>;
1377 friend class enable_shared_from_this<_Tp>;
1380 __weak_count<_Lp> _M_refcount;
1384 template<
typename _Tp, _Lock_policy _Lp>
1386 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b) noexcept
1389 template<
typename _Tp,
typename _Tp1>
1390 struct _Sp_owner_less :
public binary_function<_Tp, _Tp, bool>
1393 operator()(
const _Tp& __lhs,
const _Tp& __rhs)
const
1394 {
return __lhs.owner_before(__rhs); }
1397 operator()(
const _Tp& __lhs,
const _Tp1& __rhs)
const
1398 {
return __lhs.owner_before(__rhs); }
1401 operator()(
const _Tp1& __lhs,
const _Tp& __rhs)
const
1402 {
return __lhs.owner_before(__rhs); }
1405 template<
typename _Tp, _Lock_policy _Lp>
1406 struct owner_less<__shared_ptr<_Tp, _Lp>>
1407 :
public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
1410 template<
typename _Tp, _Lock_policy _Lp>
1411 struct owner_less<__weak_ptr<_Tp, _Lp>>
1412 :
public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
1416 template<
typename _Tp, _Lock_policy _Lp>
1417 class __enable_shared_from_this
1420 constexpr __enable_shared_from_this() noexcept { }
1422 __enable_shared_from_this(
const __enable_shared_from_this&) noexcept { }
1424 __enable_shared_from_this&
1425 operator=(
const __enable_shared_from_this&) noexcept
1428 ~__enable_shared_from_this() { }
1431 __shared_ptr<_Tp, _Lp>
1433 {
return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
1435 __shared_ptr<const _Tp, _Lp>
1436 shared_from_this()
const
1437 {
return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
1440 template<
typename _Tp1>
1442 _M_weak_assign(_Tp1* __p,
const __shared_count<_Lp>& __n)
const noexcept
1443 { _M_weak_this._M_assign(__p, __n); }
1445 template<
typename _Tp1>
1447 __enable_shared_from_this_helper(
const __shared_count<_Lp>& __pn,
1448 const __enable_shared_from_this* __pe,
1449 const _Tp1* __px) noexcept
1452 __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
1455 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
1459 template<
typename _Tp, _Lock_policy _Lp,
typename _Alloc,
typename... _Args>
1460 inline __shared_ptr<_Tp, _Lp>
1461 __allocate_shared(
const _Alloc& __a, _Args&&... __args)
1463 return __shared_ptr<_Tp, _Lp>(_Sp_make_shared_tag(), __a,
1464 std::forward<_Args>(__args)...);
1467 template<
typename _Tp, _Lock_policy _Lp,
typename... _Args>
1468 inline __shared_ptr<_Tp, _Lp>
1469 __make_shared(_Args&&... __args)
1471 typedef typename std::remove_const<_Tp>::type _Tp_nc;
1473 std::forward<_Args>(__args)...);
1477 template<
typename _Tp, _Lock_policy _Lp>
1478 struct hash<__shared_ptr<_Tp, _Lp>>
1479 :
public __hash_base<size_t, __shared_ptr<_Tp, _Lp>>
1482 operator()(
const __shared_ptr<_Tp, _Lp>& __s)
const noexcept
1486 _GLIBCXX_END_NAMESPACE_VERSION
1489 #endif // _SHARED_PTR_BASE_H