49 #ifndef _SHARED_PTR_BASE_H
50 #define _SHARED_PTR_BASE_H 1
52 namespace std _GLIBCXX_VISIBILITY(default)
54 _GLIBCXX_BEGIN_NAMESPACE_VERSION
56 #if _GLIBCXX_USE_DEPRECATED
57 template<
typename>
class auto_ptr;
68 what()
const noexcept;
75 __throw_bad_weak_ptr()
78 using __gnu_cxx::_Lock_policy;
79 using __gnu_cxx::__default_lock_policy;
80 using __gnu_cxx::_S_single;
81 using __gnu_cxx::_S_mutex;
82 using __gnu_cxx::_S_atomic;
85 template<_Lock_policy _Lp>
90 enum { _S_need_barriers = 0 };
94 class _Mutex_base<_S_mutex>
95 :
public __gnu_cxx::__mutex
101 enum { _S_need_barriers = 1 };
104 template<_Lock_policy _Lp = __default_lock_policy>
105 class _Sp_counted_base
106 :
public _Mutex_base<_Lp>
109 _Sp_counted_base() noexcept
110 : _M_use_count(1), _M_weak_count(1) { }
113 ~_Sp_counted_base() noexcept
119 _M_dispose() noexcept = 0;
123 _M_destroy() noexcept
131 { __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); }
137 _M_release() noexcept
140 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count);
141 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
143 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count);
149 if (_Mutex_base<_Lp>::_S_need_barriers)
151 _GLIBCXX_READ_MEM_BARRIER;
152 _GLIBCXX_WRITE_MEM_BARRIER;
156 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
157 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count,
160 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
167 _M_weak_add_ref() noexcept
168 { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); }
171 _M_weak_release() noexcept
174 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
175 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1)
177 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
178 if (_Mutex_base<_Lp>::_S_need_barriers)
182 _GLIBCXX_READ_MEM_BARRIER;
183 _GLIBCXX_WRITE_MEM_BARRIER;
190 _M_get_use_count() const noexcept
194 return __atomic_load_n(&_M_use_count, __ATOMIC_RELAXED);
198 _Sp_counted_base(_Sp_counted_base
const&) =
delete;
199 _Sp_counted_base& operator=(_Sp_counted_base
const&) =
delete;
201 _Atomic_word _M_use_count;
202 _Atomic_word _M_weak_count;
207 _Sp_counted_base<_S_single>::
210 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 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 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
254 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
257 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
258 class __enable_shared_from_this;
260 template<
typename _Tp>
263 template<
typename _Tp>
266 template<
typename _Tp>
269 template<
typename _Tp>
270 class enable_shared_from_this;
272 template<_Lock_policy _Lp = __default_lock_policy>
275 template<_Lock_policy _Lp = __default_lock_policy>
276 class __shared_count;
280 template<
typename _Ptr, _Lock_policy _Lp>
281 class _Sp_counted_ptr final :
public _Sp_counted_base<_Lp>
285 _Sp_counted_ptr(_Ptr __p)
289 _M_dispose() noexcept
293 _M_destroy() noexcept
300 _Sp_counted_ptr(
const _Sp_counted_ptr&) =
delete;
301 _Sp_counted_ptr& operator=(
const _Sp_counted_ptr&) =
delete;
309 _Sp_counted_ptr<nullptr_t, _S_single>::_M_dispose() noexcept { }
313 _Sp_counted_ptr<nullptr_t, _S_mutex>::_M_dispose() noexcept { }
317 _Sp_counted_ptr<nullptr_t, _S_atomic>::_M_dispose() noexcept { }
320 template<
typename _Ptr,
typename _Deleter,
typename _Alloc, _Lock_policy _Lp>
321 class _Sp_counted_deleter final :
public _Sp_counted_base<_Lp>
330 _My_Deleter(_Deleter __d,
const _Alloc& __a)
331 : _Alloc(__a), _M_del(__d) { }
336 _Sp_counted_deleter(_Ptr __p, _Deleter __d)
337 : _M_ptr(__p), _M_del(__d, _Alloc()) { }
340 _Sp_counted_deleter(_Ptr __p, _Deleter __d,
const _Alloc& __a)
341 : _M_ptr(__p), _M_del(__d, __a) { }
343 ~_Sp_counted_deleter() noexcept { }
346 _M_dispose() noexcept
347 { _M_del._M_del(_M_ptr); }
350 _M_destroy() noexcept
352 typedef typename allocator_traits<_Alloc>::template
353 rebind_traits<_Sp_counted_deleter> _Alloc_traits;
354 typename _Alloc_traits::allocator_type __a(_M_del);
355 _Alloc_traits::destroy(__a,
this);
356 _Alloc_traits::deallocate(__a,
this, 1);
363 return __ti ==
typeid(_Deleter) ? &_M_del._M_del : 0;
376 struct _Sp_make_shared_tag { };
378 template<
typename _Tp,
typename _Alloc, _Lock_policy _Lp>
379 class _Sp_counted_ptr_inplace final :
public _Sp_counted_base<_Lp>
387 _Impl(_Alloc __a) : _Alloc(__a), _M_ptr() { }
392 template<
typename... _Args>
393 _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
394 : _M_impl(__a), _M_storage()
396 _M_impl._M_ptr =
static_cast<_Tp*
>(
static_cast<void*
>(&_M_storage));
400 std::forward<_Args>(__args)...);
403 ~_Sp_counted_ptr_inplace() noexcept { }
406 _M_dispose() noexcept
411 _M_destroy() noexcept
413 typedef typename allocator_traits<_Alloc>::template
414 rebind_traits<_Sp_counted_ptr_inplace> _Alloc_traits;
415 typename _Alloc_traits::allocator_type __a(_M_impl);
416 _Alloc_traits::destroy(__a,
this);
417 _Alloc_traits::deallocate(__a,
this, 1);
425 return __ti ==
typeid(_Sp_make_shared_tag)
426 ? static_cast<void*>(&_M_storage)
435 typename aligned_storage<sizeof(_Tp), alignment_of<_Tp>::value>::type
439 template<_Lock_policy _Lp>
443 constexpr __shared_count() noexcept : _M_pi(0)
446 template<
typename _Ptr>
448 __shared_count(_Ptr __p) : _M_pi(0)
452 _M_pi =
new _Sp_counted_ptr<_Ptr, _Lp>(__p);
457 __throw_exception_again;
461 template<
typename _Ptr,
typename _Deleter>
462 __shared_count(_Ptr __p, _Deleter __d) : _M_pi(0)
466 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
467 typedef typename allocator_traits<_Alloc>::template
468 rebind_traits<_Sp_cd_type> _Alloc_traits;
469 typename _Alloc_traits::allocator_type __a;
470 _Sp_cd_type* __mem = 0;
473 __mem = _Alloc_traits::allocate(__a, 1);
474 _Alloc_traits::construct(__a, __mem, __p, std::move(__d));
481 _Alloc_traits::deallocate(__a, __mem, 1);
482 __throw_exception_again;
486 template<
typename _Ptr,
typename _Deleter,
typename _Alloc>
487 __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
489 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
490 typedef typename allocator_traits<_Alloc>::template
491 rebind_traits<_Sp_cd_type> _Alloc_traits;
492 typename _Alloc_traits::allocator_type __a2(__a);
493 _Sp_cd_type* __mem = 0;
496 __mem = _Alloc_traits::allocate(__a2, 1);
497 _Alloc_traits::construct(__a2, __mem,
498 __p, std::move(__d), std::move(__a));
505 _Alloc_traits::deallocate(__a2, __mem, 1);
506 __throw_exception_again;
510 template<
typename _Tp,
typename _Alloc,
typename... _Args>
511 __shared_count(_Sp_make_shared_tag, _Tp*,
const _Alloc& __a,
515 typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type;
516 typedef typename allocator_traits<_Alloc>::template
517 rebind_traits<_Sp_cp_type> _Alloc_traits;
518 typename _Alloc_traits::allocator_type __a2(__a);
519 _Sp_cp_type* __mem = _Alloc_traits::allocate(__a2, 1);
522 _Alloc_traits::construct(__a2, __mem, std::move(__a),
523 std::forward<_Args>(__args)...);
528 _Alloc_traits::deallocate(__a2, __mem, 1);
529 __throw_exception_again;
533 #if _GLIBCXX_USE_DEPRECATED
535 template<
typename _Tp>
541 template<
typename _Tp,
typename _Del>
544 : _M_pi(_S_create_from_up(std::move(__r)))
548 explicit __shared_count(
const __weak_count<_Lp>& __r);
550 ~__shared_count() noexcept
556 __shared_count(
const __shared_count& __r) noexcept
560 _M_pi->_M_add_ref_copy();
564 operator=(
const __shared_count& __r) noexcept
566 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
570 __tmp->_M_add_ref_copy();
579 _M_swap(__shared_count& __r) noexcept
581 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
587 _M_get_use_count() const noexcept
588 {
return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
591 _M_unique() const noexcept
592 {
return this->_M_get_use_count() == 1; }
596 {
return _M_pi ? _M_pi->_M_get_deleter(__ti) : 0; }
599 _M_less(
const __shared_count& __rhs)
const noexcept
603 _M_less(
const __weak_count<_Lp>& __rhs)
const noexcept
608 operator==(
const __shared_count& __a,
const __shared_count& __b) noexcept
609 {
return __a._M_pi == __b._M_pi; }
612 friend class __weak_count<_Lp>;
614 template<
typename _Tp,
typename _Del>
615 static _Sp_counted_base<_Lp>*
619 typedef typename unique_ptr<_Tp, _Del>::pointer _Ptr;
620 return new _Sp_counted_deleter<_Ptr, _Del, std::allocator<void>,
621 _Lp>(__r.get(), __r.get_deleter());
624 template<
typename _Tp,
typename _Del>
625 static _Sp_counted_base<_Lp>*
629 typedef typename unique_ptr<_Tp, _Del>::pointer _Ptr;
630 typedef typename std::remove_reference<_Del>::type _Del1;
632 return new _Sp_counted_deleter<_Ptr, _Del2, std::allocator<void>,
633 _Lp>(__r.get(),
std::ref(__r.get_deleter()));
636 _Sp_counted_base<_Lp>* _M_pi;
640 template<_Lock_policy _Lp>
644 constexpr __weak_count() noexcept : _M_pi(0)
647 __weak_count(
const __shared_count<_Lp>& __r) noexcept
651 _M_pi->_M_weak_add_ref();
654 __weak_count(
const __weak_count<_Lp>& __r) noexcept
658 _M_pi->_M_weak_add_ref();
661 ~__weak_count() noexcept
664 _M_pi->_M_weak_release();
668 operator=(
const __shared_count<_Lp>& __r) noexcept
670 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
672 __tmp->_M_weak_add_ref();
674 _M_pi->_M_weak_release();
680 operator=(
const __weak_count<_Lp>& __r) noexcept
682 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
684 __tmp->_M_weak_add_ref();
686 _M_pi->_M_weak_release();
692 _M_swap(__weak_count<_Lp>& __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_less(
const __weak_count& __rhs)
const noexcept
708 _M_less(
const __shared_count<_Lp>& __rhs)
const noexcept
713 operator==(
const __weak_count& __a,
const __weak_count& __b) noexcept
714 {
return __a._M_pi == __b._M_pi; }
717 friend class __shared_count<_Lp>;
719 _Sp_counted_base<_Lp>* _M_pi;
723 template<_Lock_policy _Lp>
724 inline __shared_count<_Lp>:: __shared_count(
const __weak_count<_Lp>& __r)
728 _M_pi->_M_add_ref_lock();
730 __throw_bad_weak_ptr();
737 template<_Lock_policy _Lp,
typename _Tp1,
typename _Tp2>
739 __enable_shared_from_this_helper(
const __shared_count<_Lp>&,
740 const __enable_shared_from_this<_Tp1,
741 _Lp>*,
const _Tp2*) noexcept;
744 template<typename _Tp1, typename _Tp2>
746 __enable_shared_from_this_helper(const __shared_count<>&,
747 const enable_shared_from_this<_Tp1>*,
748 const _Tp2*) noexcept;
750 template<_Lock_policy _Lp>
752 __enable_shared_from_this_helper(const __shared_count<_Lp>&, ...) noexcept
756 template<
typename _Tp, _Lock_policy _Lp>
760 typedef _Tp element_type;
762 constexpr __shared_ptr() noexcept
763 : _M_ptr(0), _M_refcount()
766 template<
typename _Tp1>
767 explicit __shared_ptr(_Tp1* __p)
768 : _M_ptr(__p), _M_refcount(__p)
770 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
771 static_assert( sizeof(_Tp1) > 0, "incomplete type" );
772 __enable_shared_from_this_helper(_M_refcount, __p, __p);
775 template<typename _Tp1, typename _Deleter>
776 __shared_ptr(_Tp1* __p, _Deleter __d)
777 : _M_ptr(__p), _M_refcount(__p, __d)
779 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
781 __enable_shared_from_this_helper(_M_refcount, __p, __p);
784 template<typename _Tp1, typename _Deleter, typename _Alloc>
785 __shared_ptr(_Tp1* __p, _Deleter __d, _Alloc __a)
786 : _M_ptr(__p), _M_refcount(__p, __d, std::move(__a))
788 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
790 __enable_shared_from_this_helper(_M_refcount, __p, __p);
793 template<typename _Deleter>
794 __shared_ptr(nullptr_t __p, _Deleter __d)
795 : _M_ptr(0), _M_refcount(__p, __d)
798 template<
typename _Deleter,
typename _Alloc>
799 __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
800 : _M_ptr(0), _M_refcount(__p, __d, std::move(__a))
803 template<
typename _Tp1>
804 __shared_ptr(
const __shared_ptr<_Tp1, _Lp>& __r, _Tp* __p) noexcept
805 : _M_ptr(__p), _M_refcount(__r._M_refcount)
808 __shared_ptr(
const __shared_ptr&) noexcept = default;
809 __shared_ptr& operator=(const __shared_ptr&) noexcept = default;
810 ~__shared_ptr() = default;
812 template<typename _Tp1, typename = typename
813 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
814 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
815 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
818 __shared_ptr(__shared_ptr&& __r) noexcept
819 : _M_ptr(__r._M_ptr), _M_refcount()
821 _M_refcount._M_swap(__r._M_refcount);
825 template<
typename _Tp1,
typename =
typename
827 __shared_ptr(__shared_ptr<_Tp1, _Lp>&& __r) noexcept
828 : _M_ptr(__r._M_ptr), _M_refcount()
830 _M_refcount._M_swap(__r._M_refcount);
834 template<
typename _Tp1>
835 explicit __shared_ptr(
const __weak_ptr<_Tp1, _Lp>& __r)
836 : _M_refcount(__r._M_refcount)
838 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
846 template<typename _Tp1, typename _Del>
847 __shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r)
848 : _M_ptr(__r.get()), _M_refcount()
850 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
852 _M_refcount = __shared_count<_Lp>(std::move(__r));
853 __enable_shared_from_this_helper(_M_refcount, __tmp, __tmp);
856 #if _GLIBCXX_USE_DEPRECATED
858 template<
typename _Tp1>
863 constexpr __shared_ptr(nullptr_t) noexcept
864 : _M_ptr(0), _M_refcount()
867 template<
typename _Tp1>
869 operator=(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
872 _M_refcount = __r._M_refcount;
876 #if _GLIBCXX_USE_DEPRECATED
877 template<
typename _Tp1>
881 __shared_ptr(std::move(__r)).swap(*
this);
887 operator=(__shared_ptr&& __r) noexcept
889 __shared_ptr(std::move(__r)).swap(*
this);
895 operator=(__shared_ptr<_Tp1, _Lp>&& __r) noexcept
897 __shared_ptr(std::move(__r)).swap(*
this);
901 template<
typename _Tp1,
typename _Del>
905 __shared_ptr(std::move(__r)).swap(*
this);
911 { __shared_ptr().swap(*
this); }
913 template<
typename _Tp1>
918 _GLIBCXX_DEBUG_ASSERT(__p == 0 || __p != _M_ptr);
919 __shared_ptr(__p).swap(*
this);
922 template<
typename _Tp1,
typename _Deleter>
924 reset(_Tp1* __p, _Deleter __d)
925 { __shared_ptr(__p, __d).swap(*
this); }
927 template<
typename _Tp1,
typename _Deleter,
typename _Alloc>
929 reset(_Tp1* __p, _Deleter __d, _Alloc __a)
930 { __shared_ptr(__p, __d, std::move(__a)).swap(*
this); }
933 typename std::add_lvalue_reference<_Tp>::type
936 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
941 operator->() const noexcept
943 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
951 explicit operator bool() const
952 {
return _M_ptr == 0 ?
false :
true; }
955 unique() const noexcept
956 {
return _M_refcount._M_unique(); }
959 use_count() const noexcept
960 {
return _M_refcount._M_get_use_count(); }
963 swap(__shared_ptr<_Tp, _Lp>& __other) noexcept
965 std::swap(_M_ptr, __other._M_ptr);
966 _M_refcount._M_swap(__other._M_refcount);
969 template<
typename _Tp1>
971 owner_before(__shared_ptr<_Tp1, _Lp>
const& __rhs)
const
972 {
return _M_refcount._M_less(__rhs._M_refcount); }
974 template<
typename _Tp1>
976 owner_before(__weak_ptr<_Tp1, _Lp>
const& __rhs)
const
977 {
return _M_refcount._M_less(__rhs._M_refcount); }
982 template<
typename _Alloc,
typename... _Args>
983 __shared_ptr(_Sp_make_shared_tag __tag,
const _Alloc& __a,
985 : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a,
986 std::
forward<_Args>(__args)...)
990 void* __p = _M_refcount._M_get_deleter(
typeid(__tag));
991 _M_ptr =
static_cast<_Tp*
>(__p);
992 __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
995 template<
typename _Alloc>
998 void operator()(_Tp* __ptr)
1000 typedef allocator_traits<_Alloc> _Alloc_traits;
1001 _Alloc_traits::destroy(_M_alloc, __ptr);
1002 _Alloc_traits::deallocate(_M_alloc, __ptr, 1);
1007 template<
typename _Alloc,
typename... _Args>
1008 __shared_ptr(_Sp_make_shared_tag __tag,
const _Alloc& __a,
1010 : _M_ptr(), _M_refcount()
1012 typedef typename _Alloc::template rebind<_Tp>::other _Alloc2;
1013 _Deleter<_Alloc2> __del = { _Alloc2(__a) };
1014 typedef allocator_traits<_Alloc2> __traits;
1015 _M_ptr = __traits::allocate(__del._M_alloc, 1);
1020 __traits::construct(__del._M_alloc, _M_ptr,
1021 std::forward<_Args>(__args)...);
1025 __traits::deallocate(__del._M_alloc, _M_ptr, 1);
1026 __throw_exception_again;
1028 __shared_count<_Lp> __count(_M_ptr, __del, __del._M_alloc);
1029 _M_refcount._M_swap(__count);
1030 __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
1034 template<
typename _Tp1, _Lock_policy _Lp1,
typename _Alloc,
1036 friend __shared_ptr<_Tp1, _Lp1>
1037 __allocate_shared(
const _Alloc& __a, _Args&&... __args);
1042 {
return _M_refcount._M_get_deleter(__ti); }
1044 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
1045 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
1047 template<
typename _Del,
typename _Tp1, _Lock_policy _Lp1>
1048 friend _Del*
get_deleter(
const __shared_ptr<_Tp1, _Lp1>&) noexcept;
1051 __shared_count<_Lp> _M_refcount;
1056 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1058 operator==(const __shared_ptr<_Tp1, _Lp>& __a,
1059 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1060 {
return __a.get() == __b.get(); }
1062 template<
typename _Tp, _Lock_policy _Lp>
1064 operator==(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1067 template<
typename _Tp, _Lock_policy _Lp>
1069 operator==(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1072 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1074 operator!=(
const __shared_ptr<_Tp1, _Lp>& __a,
1075 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1076 {
return __a.get() != __b.get(); }
1078 template<
typename _Tp, _Lock_policy _Lp>
1080 operator!=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1081 {
return (
bool)__a; }
1083 template<
typename _Tp, _Lock_policy _Lp>
1085 operator!=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1086 {
return (
bool)__a; }
1088 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1090 operator<(const __shared_ptr<_Tp1, _Lp>& __a,
1091 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1097 template<
typename _Tp, _Lock_policy _Lp>
1099 operator<(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1102 template<
typename _Tp, _Lock_policy _Lp>
1104 operator<(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
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 !(__b < __a); }
1113 template<
typename _Tp, _Lock_policy _Lp>
1115 operator<=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1116 {
return !(
nullptr < __a); }
1118 template<
typename _Tp, _Lock_policy _Lp>
1120 operator<=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1121 {
return !(__a <
nullptr); }
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 (__b < __a); }
1129 template<
typename _Tp, _Lock_policy _Lp>
1131 operator>(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1134 template<
typename _Tp, _Lock_policy _Lp>
1136 operator>(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
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
1143 {
return !(__a < __b); }
1145 template<
typename _Tp, _Lock_policy _Lp>
1147 operator>=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1148 {
return !(__a <
nullptr); }
1150 template<
typename _Tp, _Lock_policy _Lp>
1152 operator>=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1153 {
return !(
nullptr < __a); }
1155 template<
typename _Sp>
1156 struct _Sp_less :
public binary_function<_Sp, _Sp, bool>
1159 operator()(
const _Sp& __lhs,
const _Sp& __rhs)
const noexcept
1161 typedef typename _Sp::element_type element_type;
1166 template<
typename _Tp, _Lock_policy _Lp>
1167 struct less<__shared_ptr<_Tp, _Lp>>
1168 :
public _Sp_less<__shared_ptr<_Tp, _Lp>>
1172 template<
typename _Tp, _Lock_policy _Lp>
1174 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b) noexcept
1184 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1185 inline __shared_ptr<_Tp, _Lp>
1186 static_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1187 {
return __shared_ptr<_Tp, _Lp>(__r,
static_cast<_Tp*
>(__r.get())); }
1194 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1195 inline __shared_ptr<_Tp, _Lp>
1196 const_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1197 {
return __shared_ptr<_Tp, _Lp>(__r,
const_cast<_Tp*
>(__r.get())); }
1204 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1205 inline __shared_ptr<_Tp, _Lp>
1206 dynamic_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1208 if (_Tp* __p = dynamic_cast<_Tp*>(__r.get()))
1209 return __shared_ptr<_Tp, _Lp>(__r, __p);
1210 return __shared_ptr<_Tp, _Lp>();
1214 template<
typename _Tp, _Lock_policy _Lp>
1218 typedef _Tp element_type;
1220 constexpr __weak_ptr() noexcept
1221 : _M_ptr(0), _M_refcount()
1224 __weak_ptr(
const __weak_ptr&) noexcept = default;
1225 __weak_ptr& operator=(const __weak_ptr&) noexcept = default;
1226 ~__weak_ptr() = default;
1242 template<typename _Tp1, typename = typename
1243 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
1244 __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r) noexcept
1245 : _M_refcount(__r._M_refcount)
1246 { _M_ptr = __r.lock().get(); }
1248 template<
typename _Tp1,
typename =
typename
1250 __weak_ptr(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1251 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1254 template<
typename _Tp1>
1256 operator=(
const __weak_ptr<_Tp1, _Lp>& __r) noexcept
1258 _M_ptr = __r.lock().get();
1259 _M_refcount = __r._M_refcount;
1263 template<
typename _Tp1>
1265 operator=(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1267 _M_ptr = __r._M_ptr;
1268 _M_refcount = __r._M_refcount;
1272 __shared_ptr<_Tp, _Lp>
1273 lock() const noexcept
1278 return __shared_ptr<element_type, _Lp>();
1282 return __shared_ptr<element_type, _Lp>(*this);
1284 __catch(
const bad_weak_ptr&)
1289 return __shared_ptr<element_type, _Lp>();
1294 return expired() ? __shared_ptr<element_type, _Lp>()
1295 : __shared_ptr<element_type, _Lp>(*
this);
1301 use_count() const noexcept
1302 {
return _M_refcount._M_get_use_count(); }
1305 expired() const noexcept
1306 {
return _M_refcount._M_get_use_count() == 0; }
1308 template<
typename _Tp1>
1310 owner_before(
const __shared_ptr<_Tp1, _Lp>& __rhs)
const
1311 {
return _M_refcount._M_less(__rhs._M_refcount); }
1313 template<
typename _Tp1>
1315 owner_before(
const __weak_ptr<_Tp1, _Lp>& __rhs)
const
1316 {
return _M_refcount._M_less(__rhs._M_refcount); }
1320 { __weak_ptr().swap(*
this); }
1323 swap(__weak_ptr& __s) noexcept
1325 std::swap(_M_ptr, __s._M_ptr);
1326 _M_refcount._M_swap(__s._M_refcount);
1332 _M_assign(_Tp* __ptr,
const __shared_count<_Lp>& __refcount) noexcept
1335 _M_refcount = __refcount;
1338 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
1339 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
1340 friend class __enable_shared_from_this<_Tp, _Lp>;
1341 friend class enable_shared_from_this<_Tp>;
1344 __weak_count<_Lp> _M_refcount;
1348 template<
typename _Tp, _Lock_policy _Lp>
1350 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b) noexcept
1353 template<
typename _Tp,
typename _Tp1>
1354 struct _Sp_owner_less :
public binary_function<_Tp, _Tp, bool>
1357 operator()(
const _Tp& __lhs,
const _Tp& __rhs)
const
1358 {
return __lhs.owner_before(__rhs); }
1361 operator()(
const _Tp& __lhs,
const _Tp1& __rhs)
const
1362 {
return __lhs.owner_before(__rhs); }
1365 operator()(
const _Tp1& __lhs,
const _Tp& __rhs)
const
1366 {
return __lhs.owner_before(__rhs); }
1369 template<
typename _Tp, _Lock_policy _Lp>
1370 struct owner_less<__shared_ptr<_Tp, _Lp>>
1371 :
public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
1374 template<
typename _Tp, _Lock_policy _Lp>
1375 struct owner_less<__weak_ptr<_Tp, _Lp>>
1376 :
public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
1380 template<
typename _Tp, _Lock_policy _Lp>
1381 class __enable_shared_from_this
1384 constexpr __enable_shared_from_this() noexcept { }
1386 __enable_shared_from_this(
const __enable_shared_from_this&) noexcept { }
1388 __enable_shared_from_this&
1389 operator=(
const __enable_shared_from_this&) noexcept
1392 ~__enable_shared_from_this() { }
1395 __shared_ptr<_Tp, _Lp>
1397 {
return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
1399 __shared_ptr<const _Tp, _Lp>
1400 shared_from_this()
const
1401 {
return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
1404 template<
typename _Tp1>
1406 _M_weak_assign(_Tp1* __p,
const __shared_count<_Lp>& __n)
const noexcept
1407 { _M_weak_this._M_assign(__p, __n); }
1409 template<
typename _Tp1>
1411 __enable_shared_from_this_helper(
const __shared_count<_Lp>& __pn,
1412 const __enable_shared_from_this* __pe,
1413 const _Tp1* __px) noexcept
1416 __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
1419 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
1423 template<
typename _Tp, _Lock_policy _Lp,
typename _Alloc,
typename... _Args>
1424 inline __shared_ptr<_Tp, _Lp>
1425 __allocate_shared(
const _Alloc& __a, _Args&&... __args)
1427 return __shared_ptr<_Tp, _Lp>(_Sp_make_shared_tag(), __a,
1428 std::forward<_Args>(__args)...);
1431 template<
typename _Tp, _Lock_policy _Lp,
typename... _Args>
1432 inline __shared_ptr<_Tp, _Lp>
1433 __make_shared(_Args&&... __args)
1435 typedef typename std::remove_const<_Tp>::type _Tp_nc;
1437 std::forward<_Args>(__args)...);
1441 template<
typename _Tp, _Lock_policy _Lp>
1442 struct hash<__shared_ptr<_Tp, _Lp>>
1443 :
public __hash_base<size_t, __shared_ptr<_Tp, _Lp>>
1446 operator()(
const __shared_ptr<_Tp, _Lp>& __s)
const noexcept
1450 _GLIBCXX_END_NAMESPACE_VERSION
1453 #endif // _SHARED_PTR_BASE_H