50 #define _SHARED_PTR_H 1
52 #ifndef __GXX_EXPERIMENTAL_CXX0X__
56 #if defined(_GLIBCXX_INCLUDE_AS_TR1)
57 # error C++0x header cannot be included from TR1 header
60 _GLIBCXX_BEGIN_NAMESPACE(
std)
68 template<typename _Ptr, _Lock_policy _Lp>
70 : public _Sp_counted_base<_Lp>
73 _Sp_counted_ptr(_Ptr __p)
85 _M_get_deleter(
const std::type_info& __ti)
88 _Sp_counted_ptr(
const _Sp_counted_ptr&) =
delete;
89 _Sp_counted_ptr& operator=(
const _Sp_counted_ptr&) =
delete;
96 template<
typename _Ptr,
typename _Deleter,
typename _Alloc, _Lock_policy _Lp>
97 class _Sp_counted_deleter
98 :
public _Sp_counted_ptr<_Ptr, _Lp>
100 typedef typename _Alloc::template
101 rebind<_Sp_counted_deleter>::other _My_alloc_type;
107 :
public _My_alloc_type
110 _My_Deleter(_Deleter __d,
const _Alloc& __a)
111 : _My_alloc_type(__a), _M_del(__d) { }
115 typedef _Sp_counted_ptr<_Ptr, _Lp> _Base_type;
122 _Sp_counted_deleter(_Ptr __p, _Deleter __d)
123 : _Base_type(__p), _M_del(__d, _Alloc()) { }
129 _Sp_counted_deleter(_Ptr __p, _Deleter __d,
const _Alloc& __a)
130 : _Base_type(__p), _M_del(__d, __a) { }
134 { _M_del._M_del(_Base_type::_M_ptr); }
139 _My_alloc_type __a(_M_del);
140 this->~_Sp_counted_deleter();
141 __a.deallocate(
this, 1);
145 _M_get_deleter(
const std::type_info& __ti)
146 {
return __ti ==
typeid(_Deleter) ? &_M_del._M_del : 0; }
154 template<
typename _Tp>
155 struct _Sp_destroy_inplace
157 void operator()(_Tp* __p)
const {
if (__p) __p->~_Tp(); }
160 struct _Sp_make_shared_tag { };
162 template<
typename _Tp,
typename _Alloc, _Lock_policy _Lp>
163 class _Sp_counted_ptr_inplace
164 :
public _Sp_counted_deleter<_Tp*, _Sp_destroy_inplace<_Tp>, _Alloc, _Lp>
166 typedef _Sp_counted_deleter<_Tp*, _Sp_destroy_inplace<_Tp>, _Alloc, _Lp>
170 _Sp_counted_ptr_inplace(_Alloc __a)
171 : _Base_type(static_cast<_Tp*>(0), _Sp_destroy_inplace<_Tp>(), __a)
174 void* __p = &_M_storage;
176 _Base_type::_Base_type::_M_ptr = static_cast<_Tp*>(__p);
179 template<typename... _Args>
180 _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
181 : _Base_type(static_cast<_Tp*>(0), _Sp_destroy_inplace<_Tp>(), __a)
184 void* __p = &_M_storage;
185 ::new (__p) _Tp(
std::forward<_Args>(__args)...);
186 _Base_type::_Base_type::_M_ptr = static_cast<_Tp*>(__p);
193 typedef typename _Alloc::template
194 rebind<_Sp_counted_ptr_inplace>::other _My_alloc_type;
195 _My_alloc_type __a(_Base_type::_M_del);
196 this->~_Sp_counted_ptr_inplace();
197 __a.deallocate(
this, 1);
202 _M_get_deleter(
const std::type_info& __ti)
204 return __ti ==
typeid(_Sp_make_shared_tag)
205 ? static_cast<void*>(&_M_storage)
206 : _Base_type::_M_get_deleter(__ti);
210 typename aligned_storage<sizeof(_Tp), alignment_of<_Tp>::value>::type
214 template<_Lock_policy _Lp = __default_lock_policy>
217 template<_Lock_policy _Lp = __default_lock_policy>
225 template<
typename _Ptr>
226 __shared_count(_Ptr __p) : _M_pi(0)
230 _M_pi =
new _Sp_counted_ptr<_Ptr, _Lp>(__p);
235 __throw_exception_again;
239 template<
typename _Ptr,
typename _Deleter>
240 __shared_count(_Ptr __p, _Deleter __d) : _M_pi(0)
244 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
249 _M_pi = __a2.allocate(1);
250 ::new(static_cast<void*>(_M_pi)) _Sp_cd_type(__p, __d);
256 __a2.deallocate(static_cast<_Sp_cd_type*>(_M_pi), 1);
257 __throw_exception_again;
261 template<
typename _Ptr,
typename _Deleter,
typename _Alloc>
262 __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
264 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
265 typedef typename _Alloc::template rebind<_Sp_cd_type>::other _Alloc2;
269 _M_pi = __a2.allocate(1);
270 ::new(static_cast<void*>(_M_pi)) _Sp_cd_type(__p, __d, __a);
276 __a2.deallocate(static_cast<_Sp_cd_type*>(_M_pi), 1);
277 __throw_exception_again;
281 template<
typename _Tp,
typename _Alloc,
typename... _Args>
282 __shared_count(_Sp_make_shared_tag, _Tp*, _Alloc __a, _Args&&... __args)
285 typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type;
286 typedef typename _Alloc::template rebind<_Sp_cp_type>::other _Alloc2;
290 _M_pi = __a2.allocate(1);
291 ::new(static_cast<void*>(_M_pi)) _Sp_cp_type(__a,
292 std::forward<_Args>(__args)...);
297 __a2.deallocate(static_cast<_Sp_cp_type*>(_M_pi), 1);
298 __throw_exception_again;
302 #if _GLIBCXX_DEPRECATED
304 template<
typename _Tp>
307 : _M_pi(new _Sp_counted_ptr<_Tp*, _Lp>(__r.get()))
312 template<
typename _Tp,
typename _Del>
315 : _M_pi(_S_create_from_up(
std::move(__r)))
320 __shared_count(
const __weak_count<_Lp>& __r);
328 __shared_count(
const __shared_count& __r)
332 _M_pi->_M_add_ref_copy();
336 operator=(
const __shared_count& __r)
338 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
342 __tmp->_M_add_ref_copy();
351 _M_swap(__shared_count& __r)
353 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
359 _M_get_use_count() const
360 {
return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
364 {
return this->_M_get_use_count() == 1; }
367 _M_get_deleter(
const std::type_info& __ti)
const
368 {
return _M_pi ? _M_pi->_M_get_deleter(__ti) : 0; }
371 _M_less(
const __shared_count& __rhs)
const
375 _M_less(
const __weak_count<_Lp>& __rhs)
const
380 operator==(
const __shared_count& __a,
const __shared_count& __b)
381 {
return __a._M_pi == __b._M_pi; }
384 friend class __weak_count<_Lp>;
386 template<
typename _Tp,
typename _Del>
387 static _Sp_counted_base<_Lp>*
389 typename std::enable_if<!std::is_reference<_Del>::value>::type* = 0)
391 return new _Sp_counted_deleter<_Tp*, _Del, std::allocator<_Tp>,
392 _Lp>(__r.get(), __r.get_deleter());
395 template<
typename _Tp,
typename _Del>
396 static _Sp_counted_base<_Lp>*
398 typename std::enable_if<std::is_reference<_Del>::value>::type* = 0)
400 typedef typename std::remove_reference<_Del>::type _Del1;
402 return new _Sp_counted_deleter<_Tp*, _Del2, std::allocator<_Tp>,
403 _Lp>(__r.get(), std::ref(__r.get_deleter()));
406 _Sp_counted_base<_Lp>* _M_pi;
410 template<_Lock_policy _Lp>
418 __weak_count(
const __shared_count<_Lp>& __r)
422 _M_pi->_M_weak_add_ref();
425 __weak_count(
const __weak_count<_Lp>& __r)
429 _M_pi->_M_weak_add_ref();
435 _M_pi->_M_weak_release();
439 operator=(
const __shared_count<_Lp>& __r)
441 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
443 __tmp->_M_weak_add_ref();
445 _M_pi->_M_weak_release();
451 operator=(
const __weak_count<_Lp>& __r)
453 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
455 __tmp->_M_weak_add_ref();
457 _M_pi->_M_weak_release();
463 _M_swap(__weak_count<_Lp>& __r)
465 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
471 _M_get_use_count() const
472 {
return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
475 _M_less(
const __weak_count& __rhs)
const
479 _M_less(
const __shared_count<_Lp>& __rhs)
const
484 operator==(
const __weak_count& __a,
const __weak_count& __b)
485 {
return __a._M_pi == __b._M_pi; }
488 friend class __shared_count<_Lp>;
490 _Sp_counted_base<_Lp>* _M_pi;
494 template<_Lock_policy _Lp>
496 __shared_count<_Lp>::
497 __shared_count(
const __weak_count<_Lp>& __r)
501 _M_pi->_M_add_ref_lock();
503 __throw_bad_weak_ptr();
507 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
510 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
513 template<
typename _Tp, _Lock_policy _Lp = __default_lock_policy>
514 class __enable_shared_from_this;
516 template<
typename _Tp>
519 template<
typename _Tp>
522 template<
typename _Tp>
523 class enable_shared_from_this;
528 template<_Lock_policy _Lp,
typename _Tp1,
typename _Tp2>
530 __enable_shared_from_this_helper(
const __shared_count<_Lp>&,
531 const __enable_shared_from_this<_Tp1,
535 template<
typename _Tp1,
typename _Tp2>
537 __enable_shared_from_this_helper(
const __shared_count<>&,
538 const enable_shared_from_this<_Tp1>*,
541 template<_Lock_policy _Lp>
543 __enable_shared_from_this_helper(
const __shared_count<_Lp>&, ...)
547 template<
typename _Tp, _Lock_policy _Lp>
551 typedef _Tp element_type;
557 : _M_ptr(0), _M_refcount()
565 template<
typename _Tp1>
567 __shared_ptr(_Tp1* __p)
568 : _M_ptr(__p), _M_refcount(__p)
570 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
572 __enable_shared_from_this_helper(_M_refcount, __p, __p);
588 template<typename _Tp1, typename _Deleter>
589 __shared_ptr(_Tp1* __p, _Deleter __d)
590 : _M_ptr(__p), _M_refcount(__p, __d)
592 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
594 __enable_shared_from_this_helper(_M_refcount, __p, __p);
612 template<typename _Tp1, typename _Deleter, typename _Alloc>
613 __shared_ptr(_Tp1* __p, _Deleter __d, const _Alloc& __a)
614 : _M_ptr(__p), _M_refcount(__p, __d, __a)
616 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
618 __enable_shared_from_this_helper(_M_refcount, __p, __p);
636 template<typename _Tp1>
637 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r, _Tp* __p)
638 : _M_ptr(__p), _M_refcount(__r._M_refcount)
649 template<
typename _Tp1>
650 __shared_ptr(
const __shared_ptr<_Tp1, _Lp>& __r)
651 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
652 { __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) }
658 __shared_ptr(__shared_ptr&& __r)
659 : _M_ptr(__r._M_ptr), _M_refcount()
661 _M_refcount._M_swap(__r._M_refcount);
669 template<
typename _Tp1>
670 __shared_ptr(__shared_ptr<_Tp1, _Lp>&& __r)
671 : _M_ptr(__r._M_ptr), _M_refcount()
673 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
674 _M_refcount._M_swap(__r._M_refcount);
685 template<typename _Tp1>
687 __shared_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
688 : _M_refcount(__r._M_refcount)
690 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
696 template<typename _Tp1, typename _Del>
698 __shared_ptr(const
std::unique_ptr<_Tp1, _Del>&) = delete;
703 template<typename _Tp1, typename _Del>
705 __shared_ptr(
std::unique_ptr<_Tp1, _Del>&& __r)
706 : _M_ptr(__r.get()), _M_refcount()
708 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
709 _Tp1* __tmp = __r.get();
710 _M_refcount = __shared_count<_Lp>(
std::move(__r));
711 __enable_shared_from_this_helper(_M_refcount, __tmp, __tmp);
714 #if _GLIBCXX_DEPRECATED
718 template<
typename _Tp1>
721 : _M_ptr(__r.get()), _M_refcount()
723 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
725 _Tp1* __tmp = __r.get();
726 _M_refcount = __shared_count<_Lp>(
std::move(__r));
727 __enable_shared_from_this_helper(_M_refcount, __tmp, __tmp);
731 template<
typename _Tp1>
733 operator=(
const __shared_ptr<_Tp1, _Lp>& __r)
736 _M_refcount = __r._M_refcount;
740 #if _GLIBCXX_DEPRECATED
741 template<
typename _Tp1>
745 __shared_ptr(std::move(__r)).swap(*
this);
751 operator=(__shared_ptr&& __r)
753 __shared_ptr(std::move(__r)).swap(*
this);
759 operator=(__shared_ptr<_Tp1, _Lp>&& __r)
761 __shared_ptr(std::move(__r)).swap(*
this);
765 template<
typename _Tp1,
typename _Del>
769 template<
typename _Tp1,
typename _Del>
773 __shared_ptr(std::move(__r)).swap(*
this);
779 { __shared_ptr().swap(*
this); }
781 template<
typename _Tp1>
786 _GLIBCXX_DEBUG_ASSERT(__p == 0 || __p != _M_ptr);
787 __shared_ptr(__p).swap(*
this);
790 template<
typename _Tp1,
typename _Deleter>
792 reset(_Tp1* __p, _Deleter __d)
793 { __shared_ptr(__p, __d).swap(*
this); }
795 template<
typename _Tp1,
typename _Deleter,
typename _Alloc>
797 reset(_Tp1* __p, _Deleter __d,
const _Alloc& __a)
798 { __shared_ptr(__p, __d, __a).swap(*
this); }
801 typename std::add_lvalue_reference<_Tp>::type
804 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
811 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
821 typedef _Tp* __shared_ptr::*__unspecified_bool_type;
824 operator __unspecified_bool_type() const
825 {
return _M_ptr == 0 ? 0 : &__shared_ptr::_M_ptr; }
829 {
return _M_refcount._M_unique(); }
833 {
return _M_refcount._M_get_use_count(); }
836 swap(__shared_ptr<_Tp, _Lp>&& __other)
839 _M_refcount._M_swap(__other._M_refcount);
842 template<
typename _Tp1>
844 owner_before(__shared_ptr<_Tp1, _Lp>
const& __rhs)
const
845 {
return _M_refcount._M_less(__rhs._M_refcount); }
847 template<
typename _Tp1>
849 owner_before(__weak_ptr<_Tp1, _Lp>
const& __rhs)
const
850 {
return _M_refcount._M_less(__rhs._M_refcount); }
854 template<
typename _Alloc,
typename... _Args>
855 __shared_ptr(_Sp_make_shared_tag __tag, _Alloc __a, _Args&&... __args)
856 : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a,
857 std::forward<_Args>(__args)...)
861 void* __p = _M_refcount._M_get_deleter(
typeid(__tag));
862 _M_ptr =
static_cast<_Tp*
>(__p);
863 __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
866 template<
typename _Tp1, _Lock_policy _Lp1,
typename _Alloc,
868 friend __shared_ptr<_Tp1, _Lp1>
869 __allocate_shared(_Alloc __a, _Args&&... __args);
873 _M_get_deleter(
const std::type_info& __ti)
const
874 {
return _M_refcount._M_get_deleter(__ti); }
876 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
877 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
879 template<
typename _Del,
typename _Tp1, _Lock_policy _Lp1>
880 friend _Del* get_deleter(
const __shared_ptr<_Tp1, _Lp1>&);
883 __shared_count<_Lp> _M_refcount;
887 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
889 operator==(
const __shared_ptr<_Tp1, _Lp>& __a,
890 const __shared_ptr<_Tp2, _Lp>& __b)
891 {
return __a.get() == __b.get(); }
893 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
895 operator!=(
const __shared_ptr<_Tp1, _Lp>& __a,
896 const __shared_ptr<_Tp2, _Lp>& __b)
897 {
return __a.get() != __b.get(); }
899 template<
typename _Tp1,
typename _Tp2, _Lock_policy _Lp>
901 operator<(const __shared_ptr<_Tp1, _Lp>& __a,
902 const __shared_ptr<_Tp2, _Lp>& __b)
903 {
return __a.get() < __b.get(); }
905 template<
typename _Sp>
906 struct _Sp_less :
public binary_function<_Sp, _Sp, bool>
909 operator()(
const _Sp& __lhs,
const _Sp& __rhs)
const
916 template<
typename _Tp, _Lock_policy _Lp>
917 struct less<__shared_ptr<_Tp, _Lp>>
918 :
public _Sp_less<__shared_ptr<_Tp, _Lp>>
922 template<
typename _Tp, _Lock_policy _Lp>
924 operator>(
const __shared_ptr<_Tp, _Lp>& __a,
925 const __shared_ptr<_Tp, _Lp>& __b)
926 {
return __a.get() > __b.get(); }
928 template<
typename _Tp, _Lock_policy _Lp>
931 const __shared_ptr<_Tp, _Lp>& __b)
932 {
return __a.get() >= __b.get(); }
934 template<
typename _Tp, _Lock_policy _Lp>
936 operator<=(const __shared_ptr<_Tp, _Lp>& __a,
937 const __shared_ptr<_Tp, _Lp>& __b)
938 {
return __a.get() <= __b.get(); }
941 template<
typename _Tp, _Lock_policy _Lp>
943 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b)
946 template<
typename _Tp, _Lock_policy _Lp>
948 swap(__shared_ptr<_Tp, _Lp>&& __a, __shared_ptr<_Tp, _Lp>& __b)
951 template<
typename _Tp, _Lock_policy _Lp>
953 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>&& __b)
962 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
963 inline __shared_ptr<_Tp, _Lp>
964 static_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r)
965 {
return __shared_ptr<_Tp, _Lp>(__r,
static_cast<_Tp*
>(__r.get())); }
972 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
973 inline __shared_ptr<_Tp, _Lp>
974 const_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r)
975 {
return __shared_ptr<_Tp, _Lp>(__r,
const_cast<_Tp*
>(__r.get())); }
982 template<
typename _Tp,
typename _Tp1, _Lock_policy _Lp>
983 inline __shared_ptr<_Tp, _Lp>
984 dynamic_pointer_cast(
const __shared_ptr<_Tp1, _Lp>& __r)
986 if (_Tp* __p = dynamic_cast<_Tp*>(__r.get()))
987 return __shared_ptr<_Tp, _Lp>(__r, __p);
988 return __shared_ptr<_Tp, _Lp>();
992 template<
typename _Ch,
typename _Tr,
typename _Tp, _Lock_policy _Lp>
993 std::basic_ostream<_Ch, _Tr>&
994 operator<<(std::basic_ostream<_Ch, _Tr>& __os,
995 const __shared_ptr<_Tp, _Lp>& __p)
1002 template<
typename _Del,
typename _Tp, _Lock_policy _Lp>
1004 get_deleter(
const __shared_ptr<_Tp, _Lp>& __p)
1005 {
return static_cast<_Del*
>(__p._M_get_deleter(
typeid(_Del))); }
1008 template<
typename _Tp, _Lock_policy _Lp>
1012 typedef _Tp element_type;
1015 : _M_ptr(0), _M_refcount()
1034 template<
typename _Tp1>
1035 __weak_ptr(
const __weak_ptr<_Tp1, _Lp>& __r)
1036 : _M_refcount(__r._M_refcount)
1038 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
1039 _M_ptr = __r.lock().get();
1042 template<typename _Tp1>
1043 __weak_ptr(const __shared_ptr<_Tp1, _Lp>& __r)
1044 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1045 { __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) }
1047 template<
typename _Tp1>
1049 operator=(
const __weak_ptr<_Tp1, _Lp>& __r)
1051 _M_ptr = __r.lock().get();
1052 _M_refcount = __r._M_refcount;
1056 template<
typename _Tp1>
1058 operator=(
const __shared_ptr<_Tp1, _Lp>& __r)
1060 _M_ptr = __r._M_ptr;
1061 _M_refcount = __r._M_refcount;
1065 __shared_ptr<_Tp, _Lp>
1071 return __shared_ptr<element_type, _Lp>();
1075 return __shared_ptr<element_type, _Lp>(*this);
1077 __catch(
const bad_weak_ptr&)
1082 return __shared_ptr<element_type, _Lp>();
1087 return expired() ? __shared_ptr<element_type, _Lp>()
1088 : __shared_ptr<element_type, _Lp>(*
this);
1095 {
return _M_refcount._M_get_use_count(); }
1099 {
return _M_refcount._M_get_use_count() == 0; }
1101 template<
typename _Tp1>
1103 owner_before(
const __shared_ptr<_Tp1, _Lp>& __rhs)
const
1104 {
return _M_refcount._M_less(__rhs._M_refcount); }
1106 template<
typename _Tp1>
1108 owner_before(
const __weak_ptr<_Tp1, _Lp>& __rhs)
const
1109 {
return _M_refcount._M_less(__rhs._M_refcount); }
1113 { __weak_ptr().swap(*
this); }
1116 swap(__weak_ptr& __s)
1119 _M_refcount._M_swap(__s._M_refcount);
1123 template<
typename _Tp1>
1124 bool operator<(const __weak_ptr<_Tp1, _Lp>&)
const =
delete;
1125 template<
typename _Tp1>
1126 bool operator<=(const __weak_ptr<_Tp1, _Lp>&)
const =
delete;
1127 template<
typename _Tp1>
1128 bool operator>(
const __weak_ptr<_Tp1, _Lp>&)
const =
delete;
1129 template<
typename _Tp1>
1130 bool operator>=(
const __weak_ptr<_Tp1, _Lp>&)
const =
delete;
1135 _M_assign(_Tp* __ptr,
const __shared_count<_Lp>& __refcount)
1138 _M_refcount = __refcount;
1141 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __shared_ptr;
1142 template<
typename _Tp1, _Lock_policy _Lp1>
friend class __weak_ptr;
1143 friend class __enable_shared_from_this<_Tp, _Lp>;
1144 friend class enable_shared_from_this<_Tp>;
1147 __weak_count<_Lp> _M_refcount;
1151 template<
typename _Tp, _Lock_policy _Lp>
1153 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b)
1157 template<
typename _Tp>
struct owner_less;
1159 template<
typename _Tp,
typename _Tp1>
1160 struct _Sp_owner_less :
public binary_function<_Tp, _Tp, bool>
1163 operator()(
const _Tp& __lhs,
const _Tp& __rhs)
const
1164 {
return __lhs.owner_before(__rhs); }
1166 operator()(
const _Tp& __lhs,
const _Tp1& __rhs)
const
1167 {
return __lhs.owner_before(__rhs); }
1169 operator()(
const _Tp1& __lhs,
const _Tp& __rhs)
const
1170 {
return __lhs.owner_before(__rhs); }
1173 template<
typename _Tp, _Lock_policy _Lp>
1174 struct owner_less<__shared_ptr<_Tp, _Lp>>
1175 :
public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
1178 template<
typename _Tp, _Lock_policy _Lp>
1179 struct owner_less<__weak_ptr<_Tp, _Lp>>
1180 :
public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
1185 template<
typename _Tp, _Lock_policy _Lp>
1186 class __enable_shared_from_this
1189 __enable_shared_from_this() { }
1191 __enable_shared_from_this(
const __enable_shared_from_this&) { }
1193 __enable_shared_from_this&
1194 operator=(
const __enable_shared_from_this&)
1197 ~__enable_shared_from_this() { }
1200 __shared_ptr<_Tp, _Lp>
1202 {
return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
1204 __shared_ptr<const _Tp, _Lp>
1205 shared_from_this()
const
1206 {
return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
1209 template<
typename _Tp1>
1211 _M_weak_assign(_Tp1* __p,
const __shared_count<_Lp>& __n)
const
1212 { _M_weak_this._M_assign(__p, __n); }
1214 template<
typename _Tp1>
1216 __enable_shared_from_this_helper(
const __shared_count<_Lp>& __pn,
1217 const __enable_shared_from_this* __pe,
1221 __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
1224 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
1233 template<
typename _Tp>
1235 :
public __shared_ptr<_Tp>
1239 : __shared_ptr<_Tp>() { }
1241 template<
typename _Tp1>
1243 shared_ptr(_Tp1* __p)
1244 : __shared_ptr<_Tp>(__p) { }
1246 template<
typename _Tp1,
typename _Deleter>
1247 shared_ptr(_Tp1* __p, _Deleter __d)
1248 : __shared_ptr<_Tp>(__p, __d) { }
1250 template<
typename _Tp1,
typename _Deleter,
typename _Alloc>
1251 shared_ptr(_Tp1* __p, _Deleter __d,
const _Alloc& __a)
1252 : __shared_ptr<_Tp>(__p, __d, __a) { }
1255 template<
typename _Tp1>
1256 shared_ptr(
const shared_ptr<_Tp1>& __r, _Tp* __p)
1257 : __shared_ptr<_Tp>(__r, __p) { }
1259 template<
typename _Tp1>
1260 shared_ptr(
const shared_ptr<_Tp1>& __r)
1261 : __shared_ptr<_Tp>(__r) { }
1263 shared_ptr(shared_ptr&& __r)
1264 : __shared_ptr<_Tp>(
std::move(__r)) { }
1266 template<
typename _Tp1>
1267 shared_ptr(shared_ptr<_Tp1>&& __r)
1268 : __shared_ptr<_Tp>(
std::move(__r)) { }
1270 template<
typename _Tp1>
1272 shared_ptr(
const weak_ptr<_Tp1>& __r)
1273 : __shared_ptr<_Tp>(__r) { }
1275 #if _GLIBCXX_DEPRECATED
1276 template<
typename _Tp1>
1279 : __shared_ptr<_Tp>(
std::move(__r)) { }
1282 template<
typename _Tp1,
typename _Del>
1286 template<
typename _Tp1,
typename _Del>
1289 : __shared_ptr<_Tp>(
std::move(__r)) { }
1291 template<
typename _Tp1>
1293 operator=(
const shared_ptr<_Tp1>& __r)
1295 this->__shared_ptr<_Tp>::operator=(__r);
1299 #if _GLIBCXX_DEPRECATED
1300 template<
typename _Tp1>
1304 this->__shared_ptr<_Tp>::operator=(std::move(__r));
1310 operator=(shared_ptr&& __r)
1312 this->__shared_ptr<_Tp>::operator=(std::move(__r));
1316 template<
class _Tp1>
1318 operator=(shared_ptr<_Tp1>&& __r)
1320 this->__shared_ptr<_Tp>::operator=(std::move(__r));
1324 template<
typename _Tp1,
typename _Del>
1328 template<
typename _Tp1,
typename _Del>
1332 this->__shared_ptr<_Tp>::operator=(std::move(__r));
1338 template<
typename _Alloc,
typename... _Args>
1339 shared_ptr(_Sp_make_shared_tag __tag, _Alloc __a, _Args&&... __args)
1340 : __shared_ptr<_Tp>(__tag, __a,
std::forward<_Args>(__args)...)
1343 template<
typename _Tp1,
typename _Alloc,
typename... _Args>
1344 friend shared_ptr<_Tp1>
1345 allocate_shared(_Alloc __a, _Args&&... __args);
1349 template<
typename _Tp1,
typename _Tp2>
1351 operator==(
const shared_ptr<_Tp1>& __a,
const shared_ptr<_Tp2>& __b)
1352 {
return __a.get() == __b.get(); }
1354 template<
typename _Tp1,
typename _Tp2>
1356 operator!=(
const shared_ptr<_Tp1>& __a,
const shared_ptr<_Tp2>& __b)
1357 {
return __a.get() != __b.get(); }
1359 template<
typename _Tp1,
typename _Tp2>
1361 operator<(const shared_ptr<_Tp1>& __a,
const shared_ptr<_Tp2>& __b)
1362 {
return __a.get() < __b.get(); }
1364 template<
typename _Tp>
1365 struct less<shared_ptr<_Tp>>
1366 :
public _Sp_less<shared_ptr<_Tp>>
1370 template<
typename _Tp>
1372 swap(shared_ptr<_Tp>& __a, shared_ptr<_Tp>& __b)
1375 template<
typename _Tp>
1377 swap(shared_ptr<_Tp>&& __a, shared_ptr<_Tp>& __b)
1380 template<
typename _Tp>
1382 swap(shared_ptr<_Tp>& __a, shared_ptr<_Tp>&& __b)
1386 template<
typename _Tp,
typename _Tp1>
1387 inline shared_ptr<_Tp>
1388 static_pointer_cast(
const shared_ptr<_Tp1>& __r)
1389 {
return shared_ptr<_Tp>(__r,
static_cast<_Tp*
>(__r.get())); }
1391 template<
typename _Tp,
typename _Tp1>
1392 inline shared_ptr<_Tp>
1393 const_pointer_cast(
const shared_ptr<_Tp1>& __r)
1394 {
return shared_ptr<_Tp>(__r,
const_cast<_Tp*
>(__r.get())); }
1396 template<
typename _Tp,
typename _Tp1>
1397 inline shared_ptr<_Tp>
1398 dynamic_pointer_cast(
const shared_ptr<_Tp1>& __r)
1400 if (_Tp* __p = dynamic_cast<_Tp*>(__r.get()))
1401 return shared_ptr<_Tp>(__r, __p);
1402 return shared_ptr<_Tp>();
1411 template<
typename _Tp>
1413 :
public __weak_ptr<_Tp>
1417 : __weak_ptr<_Tp>() { }
1419 template<
typename _Tp1>
1420 weak_ptr(
const weak_ptr<_Tp1>& __r)
1421 : __weak_ptr<_Tp>(__r) { }
1423 template<
typename _Tp1>
1424 weak_ptr(
const shared_ptr<_Tp1>& __r)
1425 : __weak_ptr<_Tp>(__r) { }
1427 template<
typename _Tp1>
1429 operator=(
const weak_ptr<_Tp1>& __r)
1431 this->__weak_ptr<_Tp>::operator=(__r);
1435 template<
typename _Tp1>
1437 operator=(
const shared_ptr<_Tp1>& __r)
1439 this->__weak_ptr<_Tp>::operator=(__r);
1447 if (this->expired())
1448 return shared_ptr<_Tp>();
1452 return shared_ptr<_Tp>(*this);
1454 __catch(
const bad_weak_ptr&)
1456 return shared_ptr<_Tp>();
1459 return this->expired() ? shared_ptr<_Tp>()
1460 : shared_ptr<_Tp>(*
this);
1465 template<
typename _Tp1>
1466 bool operator<(const weak_ptr<_Tp1>&)
const =
delete;
1467 template<
typename _Tp1>
1468 bool operator<=(const weak_ptr<_Tp1>&)
const =
delete;
1469 template<
typename _Tp1>
1470 bool operator>(
const weak_ptr<_Tp1>&)
const =
delete;
1471 template<
typename _Tp1>
1472 bool operator>=(
const weak_ptr<_Tp1>&)
const =
delete;
1476 template<
typename _Tp>
1478 swap(weak_ptr<_Tp>& __a, weak_ptr<_Tp>& __b)
1482 template<
typename _Tp>
1483 struct owner_less<shared_ptr<_Tp>>
1484 :
public _Sp_owner_less<shared_ptr<_Tp>, weak_ptr<_Tp>>
1487 template<
typename _Tp>
1488 struct owner_less<weak_ptr<_Tp>>
1489 :
public _Sp_owner_less<weak_ptr<_Tp>, shared_ptr<_Tp>>
1495 template<
typename _Tp>
1496 class enable_shared_from_this
1499 enable_shared_from_this() { }
1501 enable_shared_from_this(
const enable_shared_from_this&) { }
1503 enable_shared_from_this&
1504 operator=(
const enable_shared_from_this&)
1507 ~enable_shared_from_this() { }
1512 {
return shared_ptr<_Tp>(this->_M_weak_this); }
1514 shared_ptr<const _Tp>
1515 shared_from_this()
const
1516 {
return shared_ptr<const _Tp>(this->_M_weak_this); }
1519 template<
typename _Tp1>
1521 _M_weak_assign(_Tp1* __p,
const __shared_count<>& __n)
const
1522 { _M_weak_this._M_assign(__p, __n); }
1524 template<
typename _Tp1>
1526 __enable_shared_from_this_helper(
const __shared_count<>& __pn,
1527 const enable_shared_from_this* __pe,
1531 __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
1534 mutable weak_ptr<_Tp> _M_weak_this;
1537 template<
typename _Tp, _Lock_policy _Lp,
typename _Alloc,
typename... _Args>
1538 inline __shared_ptr<_Tp, _Lp>
1539 __allocate_shared(_Alloc __a, _Args&&... __args)
1541 return __shared_ptr<_Tp, _Lp>(_Sp_make_shared_tag(),
1542 std::forward<_Alloc>(__a), std::forward<_Args>(__args)...);
1545 template<
typename _Tp, _Lock_policy _Lp,
typename... _Args>
1546 inline __shared_ptr<_Tp, _Lp>
1547 __make_shared(_Args&&... __args)
1549 typedef typename std::remove_const<_Tp>::type _Tp_nc;
1551 std::forward<_Args>(__args)...);
1564 template<
typename _Tp,
typename _Alloc,
typename... _Args>
1565 inline shared_ptr<_Tp>
1566 allocate_shared(_Alloc __a, _Args&&... __args)
1568 return shared_ptr<_Tp>(_Sp_make_shared_tag(), std::forward<_Alloc>(__a),
1569 std::forward<_Args>(__args)...);
1578 template<
typename _Tp,
typename... _Args>
1579 inline shared_ptr<_Tp>
1580 make_shared(_Args&&... __args)
1582 typedef typename std::remove_const<_Tp>::type _Tp_nc;
1584 std::forward<_Args>(__args)...);
1589 _GLIBCXX_END_NAMESPACE
1591 #endif // _SHARED_PTR_H
20.7.12.2 unique_ptr for single objects.
A simple smart pointer providing strict ownership semantics.
The "standard" allocator, as per [20.4].Further details: http://gcc.gnu.org/onlinedocs/libstdc++/manu...
ISO C++ entities toplevel namespace is std.
One of the comparison functors.
A convenience wrapper for creating a pair from two objects.
void swap(_Tp &, _Tp &)
Swaps two values.
bool operator>(const basic_string< _CharT, _Traits, _Alloc > &__lhs, const basic_string< _CharT, _Traits, _Alloc > &__rhs)
Test if string follows string.
bool operator>=(const basic_string< _CharT, _Traits, _Alloc > &__lhs, const basic_string< _CharT, _Traits, _Alloc > &__rhs)
Test if string doesn't precede string.