50 #ifndef _SHARED_PTR_BASE_H
51 #define _SHARED_PTR_BASE_H 1
53 namespace std _GLIBCXX_VISIBILITY(default)
55 _GLIBCXX_BEGIN_NAMESPACE_VERSION
65 what()
const noexcept;
72 __throw_bad_weak_ptr()
81 using __gnu_cxx::_Lock_policy;
82 using __gnu_cxx::__default_lock_policy;
83 using __gnu_cxx::_S_single;
84 using __gnu_cxx::_S_mutex;
85 using __gnu_cxx::_S_atomic;
88 template<_Lock_policy _Lp>
93 enum { _S_need_barriers = 0 };
97 class _Mutex_base<_S_mutex>
98 :
public __gnu_cxx::__mutex
104 enum { _S_need_barriers = 1 };
107 template<_Lock_policy _Lp = __default_lock_policy>
108 class _Sp_counted_base
109 :
public _Mutex_base<_Lp>
112 _Sp_counted_base() noexcept
113 : _M_use_count(1), _M_weak_count(1) { }
116 ~_Sp_counted_base() noexcept
122 _M_dispose() noexcept = 0;
126 _M_destroy() noexcept
134 { __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); }
140 _M_release() noexcept
143 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count);
144 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
146 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count);
152 if (_Mutex_base<_Lp>::_S_need_barriers)
154 _GLIBCXX_READ_MEM_BARRIER;
155 _GLIBCXX_WRITE_MEM_BARRIER;
159 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
160 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count,
163 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
170 _M_weak_add_ref() noexcept
171 { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); }
174 _M_weak_release() noexcept
177 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
178 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1)
180 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
181 if (_Mutex_base<_Lp>::_S_need_barriers)
185 _GLIBCXX_READ_MEM_BARRIER;
186 _GLIBCXX_WRITE_MEM_BARRIER;
193 _M_get_use_count() const noexcept
197 return __atomic_load_n(&_M_use_count, __ATOMIC_RELAXED);
201 _Sp_counted_base(_Sp_counted_base
const&) =
delete;
202 _Sp_counted_base& operator=(_Sp_counted_base
const&) =
delete;
204 _Atomic_word _M_use_count;
205 _Atomic_word _M_weak_count;
210 _Sp_counted_base<_S_single>::
213 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 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_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 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
257 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
260 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
261 class __enable_shared_from_this;
263 template<
typename _Tp>
266 template<
typename _Tp>
269 template<
typename _Tp>
272 template<
typename _Tp>
273 class enable_shared_from_this;
275 template<_Lock_policy _Lp = __default_lock_policy>
278 template<_Lock_policy _Lp = __default_lock_policy>
279 class __shared_count;
283 template<
typename _Ptr, _Lock_policy _Lp>
284 class _Sp_counted_ptr final :
public _Sp_counted_base<_Lp>
288 _Sp_counted_ptr(_Ptr __p)
292 _M_dispose() noexcept
296 _M_destroy() noexcept
303 _Sp_counted_ptr(
const _Sp_counted_ptr&) =
delete;
304 _Sp_counted_ptr& operator=(
const _Sp_counted_ptr&) =
delete;
312 _Sp_counted_ptr<nullptr_t, _S_single>::_M_dispose() noexcept { }
316 _Sp_counted_ptr<nullptr_t, _S_mutex>::_M_dispose() noexcept { }
320 _Sp_counted_ptr<nullptr_t, _S_atomic>::_M_dispose() noexcept { }
323 template<
typename _Ptr,
typename _Deleter,
typename _Alloc, _Lock_policy _Lp>
324 class _Sp_counted_deleter final :
public _Sp_counted_base<_Lp>
333 _My_Deleter(_Deleter __d,
const _Alloc& __a)
334 : _Alloc(__a), _M_del(__d) { }
339 _Sp_counted_deleter(_Ptr __p, _Deleter __d)
340 : _M_ptr(__p), _M_del(__d, _Alloc()) { }
343 _Sp_counted_deleter(_Ptr __p, _Deleter __d,
const _Alloc& __a)
344 : _M_ptr(__p), _M_del(__d, __a) { }
346 ~_Sp_counted_deleter() noexcept { }
349 _M_dispose() noexcept
350 { _M_del._M_del(_M_ptr); }
353 _M_destroy() noexcept
355 typedef typename allocator_traits<_Alloc>::template
356 rebind_traits<_Sp_counted_deleter> _Alloc_traits;
357 typename _Alloc_traits::allocator_type __a(_M_del);
358 _Alloc_traits::destroy(__a,
this);
359 _Alloc_traits::deallocate(__a,
this, 1);
366 return __ti ==
typeid(_Deleter) ? &_M_del._M_del : 0;
379 struct _Sp_make_shared_tag { };
381 template<
typename _Tp,
typename _Alloc, _Lock_policy _Lp>
382 class _Sp_counted_ptr_inplace final :
public _Sp_counted_base<_Lp>
390 _Impl(_Alloc __a) : _Alloc(__a), _M_ptr() { }
395 template<
typename... _Args>
396 _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
399 _M_impl._M_ptr =
static_cast<_Tp*
>(
static_cast<void*
>(&_M_storage));
403 std::forward<_Args>(__args)...);
406 ~_Sp_counted_ptr_inplace() noexcept { }
409 _M_dispose() noexcept
414 _M_destroy() noexcept
416 typedef typename allocator_traits<_Alloc>::template
417 rebind_traits<_Sp_counted_ptr_inplace> _Alloc_traits;
418 typename _Alloc_traits::allocator_type __a(_M_impl);
419 _Alloc_traits::destroy(__a,
this);
420 _Alloc_traits::deallocate(__a,
this, 1);
428 return __ti ==
typeid(_Sp_make_shared_tag)
429 ? static_cast<void*>(&_M_storage)
438 typename aligned_storage<sizeof(_Tp), alignment_of<_Tp>::value>::type
442 template<_Lock_policy _Lp>
446 constexpr __shared_count() noexcept : _M_pi(0)
449 template<
typename _Ptr>
451 __shared_count(_Ptr __p) : _M_pi(0)
455 _M_pi =
new _Sp_counted_ptr<_Ptr, _Lp>(__p);
460 __throw_exception_again;
464 template<
typename _Ptr,
typename _Deleter>
465 __shared_count(_Ptr __p, _Deleter __d) : _M_pi(0)
469 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
470 typedef typename allocator_traits<_Alloc>::template
471 rebind_traits<_Sp_cd_type> _Alloc_traits;
472 typename _Alloc_traits::allocator_type __a;
473 _Sp_cd_type* __mem = 0;
476 __mem = _Alloc_traits::allocate(__a, 1);
477 _Alloc_traits::construct(__a, __mem, __p, std::move(__d));
484 _Alloc_traits::deallocate(__a, __mem, 1);
485 __throw_exception_again;
489 template<
typename _Ptr,
typename _Deleter,
typename _Alloc>
490 __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
492 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
493 typedef typename allocator_traits<_Alloc>::template
494 rebind_traits<_Sp_cd_type> _Alloc_traits;
495 typename _Alloc_traits::allocator_type __a2(__a);
496 _Sp_cd_type* __mem = 0;
499 __mem = _Alloc_traits::allocate(__a2, 1);
500 _Alloc_traits::construct(__a2, __mem,
501 __p, std::move(__d), std::move(__a));
508 _Alloc_traits::deallocate(__a2, __mem, 1);
509 __throw_exception_again;
513 template<
typename _Tp,
typename _Alloc,
typename... _Args>
514 __shared_count(_Sp_make_shared_tag, _Tp*,
const _Alloc& __a,
518 typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type;
519 typedef typename allocator_traits<_Alloc>::template
520 rebind_traits<_Sp_cp_type> _Alloc_traits;
521 typename _Alloc_traits::allocator_type __a2(__a);
522 _Sp_cp_type* __mem = _Alloc_traits::allocate(__a2, 1);
525 _Alloc_traits::construct(__a2, __mem, std::move(__a),
526 std::forward<_Args>(__args)...);
531 _Alloc_traits::deallocate(__a2, __mem, 1);
532 __throw_exception_again;
536 #if _GLIBCXX_USE_DEPRECATED
538 template<
typename _Tp>
541 : _M_pi(new _Sp_counted_ptr<_Tp*, _Lp>(__r.get()))
546 template<
typename _Tp,
typename _Del>
549 : _M_pi(_S_create_from_up(std::move(__r)))
553 explicit __shared_count(
const __weak_count<_Lp>& __r);
555 ~__shared_count() noexcept
561 __shared_count(
const __shared_count& __r) noexcept
565 _M_pi->_M_add_ref_copy();
569 operator=(
const __shared_count& __r) noexcept
571 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
575 __tmp->_M_add_ref_copy();
584 _M_swap(__shared_count& __r) noexcept
586 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
592 _M_get_use_count() const noexcept
593 {
return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
596 _M_unique() const noexcept
597 {
return this->_M_get_use_count() == 1; }
601 {
return _M_pi ? _M_pi->_M_get_deleter(__ti) : 0; }
604 _M_less(
const __shared_count& __rhs)
const noexcept
608 _M_less(
const __weak_count<_Lp>& __rhs)
const noexcept
613 operator==(
const __shared_count& __a,
const __shared_count& __b) noexcept
614 {
return __a._M_pi == __b._M_pi; }
617 friend class __weak_count<_Lp>;
619 template<
typename _Tp,
typename _Del>
620 static _Sp_counted_base<_Lp>*
624 return new _Sp_counted_deleter<_Tp*, _Del, std::allocator<void>,
625 _Lp>(__r.get(), __r.get_deleter());
628 template<
typename _Tp,
typename _Del>
629 static _Sp_counted_base<_Lp>*
633 typedef typename std::remove_reference<_Del>::type _Del1;
635 return new _Sp_counted_deleter<_Tp*, _Del2, std::allocator<void>,
636 _Lp>(__r.get(),
std::ref(__r.get_deleter()));
639 _Sp_counted_base<_Lp>* _M_pi;
643 template<_Lock_policy _Lp>
647 constexpr __weak_count() noexcept : _M_pi(0)
650 __weak_count(
const __shared_count<_Lp>& __r) noexcept
654 _M_pi->_M_weak_add_ref();
657 __weak_count(
const __weak_count<_Lp>& __r) noexcept
661 _M_pi->_M_weak_add_ref();
664 ~__weak_count() noexcept
667 _M_pi->_M_weak_release();
671 operator=(
const __shared_count<_Lp>& __r) noexcept
673 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
675 __tmp->_M_weak_add_ref();
677 _M_pi->_M_weak_release();
683 operator=(
const __weak_count<_Lp>& __r) noexcept
685 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
687 __tmp->_M_weak_add_ref();
689 _M_pi->_M_weak_release();
695 _M_swap(__weak_count<_Lp>& __r) noexcept
697 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
703 _M_get_use_count() const noexcept
704 {
return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
707 _M_less(
const __weak_count& __rhs)
const noexcept
711 _M_less(
const __shared_count<_Lp>& __rhs)
const noexcept
716 operator==(
const __weak_count& __a,
const __weak_count& __b) noexcept
717 {
return __a._M_pi == __b._M_pi; }
720 friend class __shared_count<_Lp>;
722 _Sp_counted_base<_Lp>* _M_pi;
726 template<_Lock_policy _Lp>
727 inline __shared_count<_Lp>:: __shared_count(
const __weak_count<_Lp>& __r)
731 _M_pi->_M_add_ref_lock();
733 __throw_bad_weak_ptr();
740 template<_Lock_policy _Lp,
typename _Tp1,
typename _Tp2>
742 __enable_shared_from_this_helper(
const __shared_count<_Lp>&,
743 const __enable_shared_from_this<_Tp1,
744 _Lp>*,
const _Tp2*) noexcept;
747 template<typename _Tp1, typename _Tp2>
749 __enable_shared_from_this_helper(const __shared_count<>&,
750 const enable_shared_from_this<_Tp1>*,
751 const _Tp2*) noexcept;
753 template<_Lock_policy _Lp>
755 __enable_shared_from_this_helper(const __shared_count<_Lp>&, ...) noexcept
759 template<
typename _Tp, _Lock_policy _Lp>
763 typedef _Tp element_type;
765 constexpr __shared_ptr() noexcept
766 : _M_ptr(0), _M_refcount()
769 template<
typename _Tp1>
770 explicit __shared_ptr(_Tp1* __p)
771 : _M_ptr(__p), _M_refcount(__p)
773 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
774 static_assert( sizeof(_Tp1) > 0, "incomplete type" );
775 __enable_shared_from_this_helper(_M_refcount, __p, __p);
778 template<typename _Tp1, typename _Deleter>
779 __shared_ptr(_Tp1* __p, _Deleter __d)
780 : _M_ptr(__p), _M_refcount(__p, __d)
782 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
784 __enable_shared_from_this_helper(_M_refcount, __p, __p);
787 template<typename _Tp1, typename _Deleter, typename _Alloc>
788 __shared_ptr(_Tp1* __p, _Deleter __d, _Alloc __a)
789 : _M_ptr(__p), _M_refcount(__p, __d, std::move(__a))
791 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
793 __enable_shared_from_this_helper(_M_refcount, __p, __p);
796 template<typename _Deleter>
797 __shared_ptr(nullptr_t __p, _Deleter __d)
798 : _M_ptr(0), _M_refcount(__p, __d)
801 template<
typename _Deleter,
typename _Alloc>
802 __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
803 : _M_ptr(0), _M_refcount(__p, __d, std::move(__a))
806 template<
typename _Tp1>
807 __shared_ptr(
const __shared_ptr<_Tp1, _Lp>& __r, _Tp* __p) noexcept
808 : _M_ptr(__p), _M_refcount(__r._M_refcount)
811 __shared_ptr(
const __shared_ptr&) noexcept = default;
812 __shared_ptr& operator=(const __shared_ptr&) noexcept = default;
813 ~__shared_ptr() = default;
815 template<typename _Tp1, typename = typename
816 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
817 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
818 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
821 __shared_ptr(__shared_ptr&& __r) noexcept
822 : _M_ptr(__r._M_ptr), _M_refcount()
824 _M_refcount._M_swap(__r._M_refcount);
828 template<
typename _Tp1,
typename =
typename
830 __shared_ptr(__shared_ptr<_Tp1, _Lp>&& __r) noexcept
831 : _M_ptr(__r._M_ptr), _M_refcount()
833 _M_refcount._M_swap(__r._M_refcount);
837 template<
typename _Tp1>
838 explicit __shared_ptr(
const __weak_ptr<_Tp1, _Lp>& __r)
839 : _M_refcount(__r._M_refcount)
841 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
849 template<typename _Tp1, typename _Del>
850 __shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r)
851 : _M_ptr(__r.get()), _M_refcount()
853 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
854 _Tp1* __tmp = __r.get();
855 _M_refcount = __shared_count<_Lp>(std::move(__r));
856 __enable_shared_from_this_helper(_M_refcount, __tmp, __tmp);
859 #if _GLIBCXX_USE_DEPRECATED
861 template<
typename _Tp1>
863 : _M_ptr(__r.get()), _M_refcount()
865 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
866 static_assert( sizeof(_Tp1) > 0, "incomplete type" );
867 _Tp1* __tmp = __r.get();
868 _M_refcount = __shared_count<_Lp>(std::move(__r));
869 __enable_shared_from_this_helper(_M_refcount, __tmp, __tmp);
874 constexpr __shared_ptr(nullptr_t) noexcept
875 : _M_ptr(0), _M_refcount()
878 template<
typename _Tp1>
880 operator=(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
883 _M_refcount = __r._M_refcount;
887 #if _GLIBCXX_USE_DEPRECATED
888 template<
typename _Tp1>
892 __shared_ptr(std::move(__r)).swap(*
this);
898 operator=(__shared_ptr&& __r) noexcept
900 __shared_ptr(std::move(__r)).swap(*
this);
906 operator=(__shared_ptr<_Tp1, _Lp>&& __r) noexcept
908 __shared_ptr(std::move(__r)).swap(*
this);
912 template<
typename _Tp1,
typename _Del>
916 __shared_ptr(std::move(__r)).swap(*
this);
922 { __shared_ptr().swap(*
this); }
924 template<
typename _Tp1>
929 _GLIBCXX_DEBUG_ASSERT(__p == 0 || __p != _M_ptr);
930 __shared_ptr(__p).swap(*
this);
933 template<
typename _Tp1,
typename _Deleter>
935 reset(_Tp1* __p, _Deleter __d)
936 { __shared_ptr(__p, __d).swap(*
this); }
938 template<
typename _Tp1,
typename _Deleter,
typename _Alloc>
940 reset(_Tp1* __p, _Deleter __d, _Alloc __a)
941 { __shared_ptr(__p, __d, std::move(__a)).swap(*
this); }
944 typename std::add_lvalue_reference<_Tp>::type
947 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
952 operator->() const noexcept
954 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
962 explicit operator bool() const
963 {
return _M_ptr == 0 ?
false :
true; }
966 unique() const noexcept
967 {
return _M_refcount._M_unique(); }
970 use_count() const noexcept
971 {
return _M_refcount._M_get_use_count(); }
974 swap(__shared_ptr<_Tp, _Lp>& __other) noexcept
976 std::swap(_M_ptr, __other._M_ptr);
977 _M_refcount._M_swap(__other._M_refcount);
980 template<
typename _Tp1>
982 owner_before(__shared_ptr<_Tp1, _Lp>
const& __rhs)
const
983 {
return _M_refcount._M_less(__rhs._M_refcount); }
985 template<
typename _Tp1>
987 owner_before(__weak_ptr<_Tp1, _Lp>
const& __rhs)
const
988 {
return _M_refcount._M_less(__rhs._M_refcount); }
993 template<
typename _Alloc,
typename... _Args>
994 __shared_ptr(_Sp_make_shared_tag __tag,
const _Alloc& __a,
996 : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a,
997 std::
forward<_Args>(__args)...)
1001 void* __p = _M_refcount._M_get_deleter(
typeid(__tag));
1002 _M_ptr =
static_cast<_Tp*
>(__p);
1003 __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
1006 template<
typename _Alloc>
1009 void operator()(_Tp* __ptr)
1011 typedef allocator_traits<_Alloc> _Alloc_traits;
1012 _Alloc_traits::destroy(_M_alloc, __ptr);
1013 _Alloc_traits::deallocate(_M_alloc, __ptr, 1);
1018 template<
typename _Alloc,
typename... _Args>
1019 __shared_ptr(_Sp_make_shared_tag __tag,
const _Alloc& __a,
1021 : _M_ptr(), _M_refcount()
1023 typedef typename _Alloc::template rebind<_Tp>::other _Alloc2;
1024 _Deleter<_Alloc2> __del = { _Alloc2(__a) };
1025 typedef allocator_traits<_Alloc2> __traits;
1026 _M_ptr = __traits::allocate(__del._M_alloc, 1);
1031 __traits::construct(__del._M_alloc, _M_ptr,
1032 std::forward<_Args>(__args)...);
1036 __traits::deallocate(__del._M_alloc, _M_ptr, 1);
1037 __throw_exception_again;
1039 __shared_count<_Lp> __count(_M_ptr, __del, __del._M_alloc);
1040 _M_refcount._M_swap(__count);
1041 __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
1045 template<
typename _Tp1, _Lock_policy _Lp1,
typename _Alloc,
1047 friend __shared_ptr<_Tp1, _Lp1>
1048 __allocate_shared(
const _Alloc& __a, _Args&&... __args);
1053 {
return _M_refcount._M_get_deleter(__ti); }
1055 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
1056 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
1058 template<
typename _Del,
typename _Tp1, _Lock_policy _Lp1>
1059 friend _Del*
get_deleter(
const __shared_ptr<_Tp1, _Lp1>&) noexcept;
1062 __shared_count<_Lp> _M_refcount;
1067 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1069 operator==(const __shared_ptr<_Tp1, _Lp>& __a,
1070 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1071 {
return __a.get() == __b.get(); }
1073 template<
typename _Tp, _Lock_policy _Lp>
1075 operator==(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1078 template<
typename _Tp, _Lock_policy _Lp>
1080 operator==(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1083 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1085 operator!=(
const __shared_ptr<_Tp1, _Lp>& __a,
1086 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1087 {
return __a.get() != __b.get(); }
1089 template<
typename _Tp, _Lock_policy _Lp>
1091 operator!=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1092 {
return (
bool)__a; }
1094 template<
typename _Tp, _Lock_policy _Lp>
1096 operator!=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1097 {
return (
bool)__a; }
1099 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1101 operator<(const __shared_ptr<_Tp1, _Lp>& __a,
1102 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1108 template<
typename _Tp, _Lock_policy _Lp>
1110 operator<(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1113 template<
typename _Tp, _Lock_policy _Lp>
1115 operator<(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1118 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1120 operator<=(const __shared_ptr<_Tp1, _Lp>& __a,
1121 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1122 {
return !(__b < __a); }
1124 template<
typename _Tp, _Lock_policy _Lp>
1126 operator<=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1127 {
return !(
nullptr < __a); }
1129 template<
typename _Tp, _Lock_policy _Lp>
1131 operator<=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1132 {
return !(__a <
nullptr); }
1134 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1136 operator>(
const __shared_ptr<_Tp1, _Lp>& __a,
1137 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1138 {
return (__b < __a); }
1140 template<
typename _Tp, _Lock_policy _Lp>
1142 operator>(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1145 template<
typename _Tp, _Lock_policy _Lp>
1147 operator>(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1150 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
1152 operator>=(
const __shared_ptr<_Tp1, _Lp>& __a,
1153 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1154 {
return !(__a < __b); }
1156 template<
typename _Tp, _Lock_policy _Lp>
1158 operator>=(
const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1159 {
return !(__a <
nullptr); }
1161 template<
typename _Tp, _Lock_policy _Lp>
1163 operator>=(nullptr_t,
const __shared_ptr<_Tp, _Lp>& __a) noexcept
1164 {
return !(
nullptr < __a); }
1166 template<
typename _Sp>
1167 struct _Sp_less :
public binary_function<_Sp, _Sp, bool>
1170 operator()(
const _Sp& __lhs,
const _Sp& __rhs)
const noexcept
1172 typedef typename _Sp::element_type element_type;
1177 template<
typename _Tp, _Lock_policy _Lp>
1178 struct less<__shared_ptr<_Tp, _Lp>>
1179 :
public _Sp_less<__shared_ptr<_Tp, _Lp>>
1183 template<
typename _Tp, _Lock_policy _Lp>
1185 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b) noexcept
1195 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1196 inline __shared_ptr<_Tp, _Lp>
1197 static_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1198 {
return __shared_ptr<_Tp, _Lp>(__r,
static_cast<_Tp*
>(__r.get())); }
1205 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1206 inline __shared_ptr<_Tp, _Lp>
1207 const_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1208 {
return __shared_ptr<_Tp, _Lp>(__r,
const_cast<_Tp*
>(__r.get())); }
1215 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
1216 inline __shared_ptr<_Tp, _Lp>
1217 dynamic_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1219 if (_Tp* __p = dynamic_cast<_Tp*>(__r.get()))
1220 return __shared_ptr<_Tp, _Lp>(__r, __p);
1221 return __shared_ptr<_Tp, _Lp>();
1225 template<
typename _Tp, _Lock_policy _Lp>
1229 typedef _Tp element_type;
1231 constexpr __weak_ptr() noexcept
1232 : _M_ptr(0), _M_refcount()
1235 __weak_ptr(
const __weak_ptr&) noexcept = default;
1236 __weak_ptr& operator=(const __weak_ptr&) noexcept = default;
1237 ~__weak_ptr() = default;
1253 template<typename _Tp1, typename = typename
1254 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
1255 __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r) noexcept
1256 : _M_refcount(__r._M_refcount)
1257 { _M_ptr = __r.lock().get(); }
1259 template<
typename _Tp1,
typename =
typename
1261 __weak_ptr(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1262 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1265 template<
typename _Tp1>
1267 operator=(
const __weak_ptr<_Tp1, _Lp>& __r) noexcept
1269 _M_ptr = __r.lock().get();
1270 _M_refcount = __r._M_refcount;
1274 template<
typename _Tp1>
1276 operator=(
const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1278 _M_ptr = __r._M_ptr;
1279 _M_refcount = __r._M_refcount;
1283 __shared_ptr<_Tp, _Lp>
1284 lock() const noexcept
1289 return __shared_ptr<element_type, _Lp>();
1293 return __shared_ptr<element_type, _Lp>(*this);
1295 __catch(
const bad_weak_ptr&)
1300 return __shared_ptr<element_type, _Lp>();
1305 return expired() ? __shared_ptr<element_type, _Lp>()
1306 : __shared_ptr<element_type, _Lp>(*
this);
1312 use_count() const noexcept
1313 {
return _M_refcount._M_get_use_count(); }
1316 expired() const noexcept
1317 {
return _M_refcount._M_get_use_count() == 0; }
1319 template<
typename _Tp1>
1321 owner_before(
const __shared_ptr<_Tp1, _Lp>& __rhs)
const
1322 {
return _M_refcount._M_less(__rhs._M_refcount); }
1324 template<
typename _Tp1>
1326 owner_before(
const __weak_ptr<_Tp1, _Lp>& __rhs)
const
1327 {
return _M_refcount._M_less(__rhs._M_refcount); }
1331 { __weak_ptr().swap(*
this); }
1334 swap(__weak_ptr& __s) noexcept
1336 std::swap(_M_ptr, __s._M_ptr);
1337 _M_refcount._M_swap(__s._M_refcount);
1343 _M_assign(_Tp* __ptr,
const __shared_count<_Lp>& __refcount) noexcept
1346 _M_refcount = __refcount;
1349 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
1350 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
1351 friend class __enable_shared_from_this<_Tp, _Lp>;
1352 friend class enable_shared_from_this<_Tp>;
1355 __weak_count<_Lp> _M_refcount;
1359 template<
typename _Tp, _Lock_policy _Lp>
1361 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b) noexcept
1364 template<
typename _Tp,
typename _Tp1>
1365 struct _Sp_owner_less :
public binary_function<_Tp, _Tp, bool>
1368 operator()(
const _Tp& __lhs,
const _Tp& __rhs)
const
1369 {
return __lhs.owner_before(__rhs); }
1372 operator()(
const _Tp& __lhs,
const _Tp1& __rhs)
const
1373 {
return __lhs.owner_before(__rhs); }
1376 operator()(
const _Tp1& __lhs,
const _Tp& __rhs)
const
1377 {
return __lhs.owner_before(__rhs); }
1380 template<
typename _Tp, _Lock_policy _Lp>
1381 struct owner_less<__shared_ptr<_Tp, _Lp>>
1382 :
public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
1385 template<
typename _Tp, _Lock_policy _Lp>
1386 struct owner_less<__weak_ptr<_Tp, _Lp>>
1387 :
public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
1391 template<
typename _Tp, _Lock_policy _Lp>
1392 class __enable_shared_from_this
1395 constexpr __enable_shared_from_this() noexcept { }
1397 __enable_shared_from_this(
const __enable_shared_from_this&) noexcept { }
1399 __enable_shared_from_this&
1400 operator=(
const __enable_shared_from_this&) noexcept
1403 ~__enable_shared_from_this() { }
1406 __shared_ptr<_Tp, _Lp>
1408 {
return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
1410 __shared_ptr<const _Tp, _Lp>
1411 shared_from_this()
const
1412 {
return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
1415 template<
typename _Tp1>
1417 _M_weak_assign(_Tp1* __p,
const __shared_count<_Lp>& __n)
const noexcept
1418 { _M_weak_this._M_assign(__p, __n); }
1420 template<
typename _Tp1>
1422 __enable_shared_from_this_helper(
const __shared_count<_Lp>& __pn,
1423 const __enable_shared_from_this* __pe,
1424 const _Tp1* __px) noexcept
1427 __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
1430 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
1434 template<
typename _Tp, _Lock_policy _Lp,
typename _Alloc,
typename... _Args>
1435 inline __shared_ptr<_Tp, _Lp>
1436 __allocate_shared(
const _Alloc& __a, _Args&&... __args)
1438 return __shared_ptr<_Tp, _Lp>(_Sp_make_shared_tag(), __a,
1439 std::forward<_Args>(__args)...);
1442 template<
typename _Tp, _Lock_policy _Lp,
typename... _Args>
1443 inline __shared_ptr<_Tp, _Lp>
1444 __make_shared(_Args&&... __args)
1446 typedef typename std::remove_const<_Tp>::type _Tp_nc;
1448 std::forward<_Args>(__args)...);
1452 template<
typename _Tp, _Lock_policy _Lp>
1453 struct hash<__shared_ptr<_Tp, _Lp>>
1454 :
public __hash_base<size_t, __shared_ptr<_Tp, _Lp>>
1457 operator()(
const __shared_ptr<_Tp, _Lp>& __s)
const noexcept
1461 _GLIBCXX_END_NAMESPACE_VERSION
1464 #endif // _SHARED_PTR_BASE_H
The standard allocator, as per [20.4].
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
20.7.12.2 unique_ptr for single objects.
reference_wrapper< _Tp > ref(_Tp &__t) noexcept
Denotes a reference should be taken to a variable.
Define a member typedef type only if a boolean constant is true.
A simple smart pointer providing strict ownership semantics.
One of the comparison functors.
Exception possibly thrown by shared_ptr.
Primary class template for reference_wrapper.
static auto construct(_Alloc &__a, _Tp *__p, _Args &&...__args) -> decltype(_S_construct(__a, __p, std::forward< _Args >(__args)...))
Construct an object of type _Tp.
Base class for all library exceptions.
bitset< _Nb > & reset() _GLIBCXX_NOEXCEPT
Sets every bit to false.
void lock(_L1 &__l1, _L2 &__l2, _L3 &...__l3)
Generic lock.
_Del * get_deleter(const __shared_ptr< _Tp, _Lp > &__p) noexcept
2.2.3.10 shared_ptr get_deleter (experimental)
static void destroy(_Alloc &__a, _Tp *__p)
Destroy an object of type _Tp.
Primary class template hash.
Partial specializations for pointer types.
complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
virtual char const * what() const noexcept