1 // shared_ptr and weak_ptr implementation details -*- C++ -*-
3 // Copyright (C) 2007-2016 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 // GCC Note: Based on files from version 1.32.0 of the Boost library.
28 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
31 // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
32 // Copyright (C) 2001, 2002, 2003 Peter Dimov
35 // Copyright (C) 2001, 2002, 2003 Peter Dimov
37 // enable_shared_from_this.hpp
38 // Copyright (C) 2002 Peter Dimov
40 // Distributed under the Boost Software License, Version 1.0. (See
41 // accompanying file LICENSE_1_0.txt or copy at
42 // http://www.boost.org/LICENSE_1_0.txt)
44 /** @file bits/shared_ptr_base.h
45 * This is an internal header file, included by other library headers.
46 * Do not attempt to use it directly. @headername{memory}
49 #ifndef _SHARED_PTR_BASE_H
50 #define _SHARED_PTR_BASE_H 1
53 #include <bits/allocated_ptr.h>
54 #include <ext/aligned_buffer.h>
56 namespace std
_GLIBCXX_VISIBILITY(default)
58 _GLIBCXX_BEGIN_NAMESPACE_VERSION
60 #if _GLIBCXX_USE_DEPRECATED
61 template<typename
> class auto_ptr
;
65 * @brief Exception possibly thrown by @c shared_ptr.
68 class bad_weak_ptr
: public std::exception
71 virtual char const* what() const noexcept
;
73 virtual ~bad_weak_ptr() noexcept
;
76 // Substitute for bad_weak_ptr object in the case of -fno-exceptions.
78 __throw_bad_weak_ptr()
79 { _GLIBCXX_THROW_OR_ABORT(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
;
87 // Empty helper class except when the template argument is _S_mutex.
88 template<_Lock_policy _Lp
>
92 // The atomic policy uses fully-fenced builtins, single doesn't care.
93 enum { _S_need_barriers
= 0 };
97 class _Mutex_base
<_S_mutex
>
98 : public __gnu_cxx::__mutex
101 // This policy is used when atomic builtins are not available.
102 // The replacement atomic operations might not have the necessary
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
119 // Called when _M_use_count drops to zero, to release the resources
122 _M_dispose() noexcept
= 0;
124 // Called when _M_weak_count drops to zero.
126 _M_destroy() noexcept
130 _M_get_deleter(const std::type_info
&) noexcept
= 0;
134 { __gnu_cxx::__atomic_add_dispatch(&_M_use_count
, 1); }
140 _M_add_ref_lock_nothrow();
143 _M_release() noexcept
145 // Be race-detector-friendly. For more info see bits/c++config.
146 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count
);
147 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count
, -1) == 1)
149 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count
);
151 // There must be a memory barrier between dispose() and destroy()
152 // to ensure that the effects of dispose() are observed in the
153 // thread that runs destroy().
154 // See http://gcc.gnu.org/ml/libstdc++/2005-11/msg00136.html
155 if (_Mutex_base
<_Lp
>::_S_need_barriers
)
157 __atomic_thread_fence (__ATOMIC_ACQ_REL
);
160 // Be race-detector-friendly. For more info see bits/c++config.
161 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count
);
162 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count
,
165 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count
);
172 _M_weak_add_ref() noexcept
173 { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count
, 1); }
176 _M_weak_release() noexcept
178 // Be race-detector-friendly. For more info see bits/c++config.
179 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count
);
180 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count
, -1) == 1)
182 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count
);
183 if (_Mutex_base
<_Lp
>::_S_need_barriers
)
186 // destroy() must observe results of dispose()
187 __atomic_thread_fence (__ATOMIC_ACQ_REL
);
194 _M_get_use_count() const noexcept
196 // No memory barrier is used here so there is no synchronization
197 // with other threads.
198 return __atomic_load_n(&_M_use_count
, __ATOMIC_RELAXED
);
202 _Sp_counted_base(_Sp_counted_base
const&) = delete;
203 _Sp_counted_base
& operator=(_Sp_counted_base
const&) = delete;
205 _Atomic_word _M_use_count
; // #shared
206 _Atomic_word _M_weak_count
; // #weak + (#shared != 0)
211 _Sp_counted_base
<_S_single
>::
214 if (_M_use_count
== 0)
215 __throw_bad_weak_ptr();
221 _Sp_counted_base
<_S_mutex
>::
224 __gnu_cxx::__scoped_lock
sentry(*this);
225 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count
, 1) == 0)
228 __throw_bad_weak_ptr();
234 _Sp_counted_base
<_S_atomic
>::
237 // Perform lock-free add-if-not-zero operation.
238 _Atomic_word __count
= _M_get_use_count();
242 __throw_bad_weak_ptr();
243 // Replace the current counter value with the old value + 1, as
244 // long as it's not changed meanwhile.
246 while (!__atomic_compare_exchange_n(&_M_use_count
, &__count
, __count
+ 1,
247 true, __ATOMIC_ACQ_REL
,
253 _Sp_counted_base
<_S_single
>::
254 _M_add_ref_lock_nothrow()
256 if (_M_use_count
== 0)
264 _Sp_counted_base
<_S_mutex
>::
265 _M_add_ref_lock_nothrow()
267 __gnu_cxx::__scoped_lock
sentry(*this);
268 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count
, 1) == 0)
278 _Sp_counted_base
<_S_atomic
>::
279 _M_add_ref_lock_nothrow()
281 // Perform lock-free add-if-not-zero operation.
282 _Atomic_word __count
= _M_get_use_count();
287 // Replace the current counter value with the old value + 1, as
288 // long as it's not changed meanwhile.
290 while (!__atomic_compare_exchange_n(&_M_use_count
, &__count
, __count
+ 1,
291 true, __ATOMIC_ACQ_REL
,
298 _Sp_counted_base
<_S_single
>::_M_add_ref_copy()
303 _Sp_counted_base
<_S_single
>::_M_release() noexcept
305 if (--_M_use_count
== 0)
308 if (--_M_weak_count
== 0)
315 _Sp_counted_base
<_S_single
>::_M_weak_add_ref() noexcept
320 _Sp_counted_base
<_S_single
>::_M_weak_release() noexcept
322 if (--_M_weak_count
== 0)
328 _Sp_counted_base
<_S_single
>::_M_get_use_count() const noexcept
329 { return _M_use_count
; }
332 // Forward declarations.
333 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
336 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
339 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
340 class __enable_shared_from_this
;
342 template<typename _Tp
>
345 template<typename _Tp
>
348 template<typename _Tp
>
351 template<typename _Tp
>
352 class enable_shared_from_this
;
354 template<_Lock_policy _Lp
= __default_lock_policy
>
357 template<_Lock_policy _Lp
= __default_lock_policy
>
358 class __shared_count
;
361 // Counted ptr with no deleter or allocator support
362 template<typename _Ptr
, _Lock_policy _Lp
>
363 class _Sp_counted_ptr final
: public _Sp_counted_base
<_Lp
>
367 _Sp_counted_ptr(_Ptr __p
) noexcept
371 _M_dispose() noexcept
375 _M_destroy() noexcept
379 _M_get_deleter(const std::type_info
&) noexcept
382 _Sp_counted_ptr(const _Sp_counted_ptr
&) = delete;
383 _Sp_counted_ptr
& operator=(const _Sp_counted_ptr
&) = delete;
391 _Sp_counted_ptr
<nullptr_t
, _S_single
>::_M_dispose() noexcept
{ }
395 _Sp_counted_ptr
<nullptr_t
, _S_mutex
>::_M_dispose() noexcept
{ }
399 _Sp_counted_ptr
<nullptr_t
, _S_atomic
>::_M_dispose() noexcept
{ }
401 template<int _Nm
, typename _Tp
,
402 bool __use_ebo
= !__is_final(_Tp
) && __is_empty(_Tp
)>
403 struct _Sp_ebo_helper
;
405 /// Specialization using EBO.
406 template<int _Nm
, typename _Tp
>
407 struct _Sp_ebo_helper
<_Nm
, _Tp
, true> : private _Tp
409 explicit _Sp_ebo_helper(const _Tp
& __tp
) : _Tp(__tp
) { }
412 _S_get(_Sp_ebo_helper
& __eboh
) { return static_cast<_Tp
&>(__eboh
); }
415 /// Specialization not using EBO.
416 template<int _Nm
, typename _Tp
>
417 struct _Sp_ebo_helper
<_Nm
, _Tp
, false>
419 explicit _Sp_ebo_helper(const _Tp
& __tp
) : _M_tp(__tp
) { }
422 _S_get(_Sp_ebo_helper
& __eboh
)
423 { return __eboh
._M_tp
; }
429 // Support for custom deleter and/or allocator
430 template<typename _Ptr
, typename _Deleter
, typename _Alloc
, _Lock_policy _Lp
>
431 class _Sp_counted_deleter final
: public _Sp_counted_base
<_Lp
>
433 class _Impl
: _Sp_ebo_helper
<0, _Deleter
>, _Sp_ebo_helper
<1, _Alloc
>
435 typedef _Sp_ebo_helper
<0, _Deleter
> _Del_base
;
436 typedef _Sp_ebo_helper
<1, _Alloc
> _Alloc_base
;
439 _Impl(_Ptr __p
, _Deleter __d
, const _Alloc
& __a
) noexcept
440 : _M_ptr(__p
), _Del_base(__d
), _Alloc_base(__a
)
443 _Deleter
& _M_del() noexcept
{ return _Del_base::_S_get(*this); }
444 _Alloc
& _M_alloc() noexcept
{ return _Alloc_base::_S_get(*this); }
450 using __allocator_type
= __alloc_rebind
<_Alloc
, _Sp_counted_deleter
>;
452 // __d(__p) must not throw.
453 _Sp_counted_deleter(_Ptr __p
, _Deleter __d
) noexcept
454 : _M_impl(__p
, __d
, _Alloc()) { }
456 // __d(__p) must not throw.
457 _Sp_counted_deleter(_Ptr __p
, _Deleter __d
, const _Alloc
& __a
) noexcept
458 : _M_impl(__p
, __d
, __a
) { }
460 ~_Sp_counted_deleter() noexcept
{ }
463 _M_dispose() noexcept
464 { _M_impl
._M_del()(_M_impl
._M_ptr
); }
467 _M_destroy() noexcept
469 __allocator_type
__a(_M_impl
._M_alloc());
470 __allocated_ptr
<__allocator_type
> __guard_ptr
{ __a
, this };
471 this->~_Sp_counted_deleter();
475 _M_get_deleter(const std::type_info
& __ti
) noexcept
478 // _GLIBCXX_RESOLVE_LIB_DEFECTS
479 // 2400. shared_ptr's get_deleter() should use addressof()
480 return __ti
== typeid(_Deleter
)
481 ? std::__addressof(_M_impl
._M_del())
492 // helpers for make_shared / allocate_shared
494 struct _Sp_make_shared_tag
{ };
496 template<typename _Tp
, typename _Alloc
, _Lock_policy _Lp
>
497 class _Sp_counted_ptr_inplace final
: public _Sp_counted_base
<_Lp
>
499 class _Impl
: _Sp_ebo_helper
<0, _Alloc
>
501 typedef _Sp_ebo_helper
<0, _Alloc
> _A_base
;
504 explicit _Impl(_Alloc __a
) noexcept
: _A_base(__a
) { }
506 _Alloc
& _M_alloc() noexcept
{ return _A_base::_S_get(*this); }
508 __gnu_cxx::__aligned_buffer
<_Tp
> _M_storage
;
512 using __allocator_type
= __alloc_rebind
<_Alloc
, _Sp_counted_ptr_inplace
>;
514 template<typename
... _Args
>
515 _Sp_counted_ptr_inplace(_Alloc __a
, _Args
&&... __args
)
518 // _GLIBCXX_RESOLVE_LIB_DEFECTS
519 // 2070. allocate_shared should use allocator_traits<A>::construct
520 allocator_traits
<_Alloc
>::construct(__a
, _M_ptr(),
521 std::forward
<_Args
>(__args
)...); // might throw
524 ~_Sp_counted_ptr_inplace() noexcept
{ }
527 _M_dispose() noexcept
529 allocator_traits
<_Alloc
>::destroy(_M_impl
._M_alloc(), _M_ptr());
532 // Override because the allocator needs to know the dynamic type
534 _M_destroy() noexcept
536 __allocator_type
__a(_M_impl
._M_alloc());
537 __allocated_ptr
<__allocator_type
> __guard_ptr
{ __a
, this };
538 this->~_Sp_counted_ptr_inplace();
541 // Sneaky trick so __shared_ptr can get the managed pointer
543 _M_get_deleter(const std::type_info
& __ti
) noexcept
546 if (__ti
== typeid(_Sp_make_shared_tag
))
547 return const_cast<typename remove_cv
<_Tp
>::type
*>(_M_ptr());
553 _Tp
* _M_ptr() noexcept
{ return _M_impl
._M_storage
._M_ptr(); }
559 template<_Lock_policy _Lp
>
563 constexpr __shared_count() noexcept
: _M_pi(0)
566 template<typename _Ptr
>
568 __shared_count(_Ptr __p
) : _M_pi(0)
572 _M_pi
= new _Sp_counted_ptr
<_Ptr
, _Lp
>(__p
);
577 __throw_exception_again
;
581 template<typename _Ptr
, typename _Deleter
>
582 __shared_count(_Ptr __p
, _Deleter __d
)
583 : __shared_count(__p
, std::move(__d
), allocator
<void>())
586 template<typename _Ptr
, typename _Deleter
, typename _Alloc
>
587 __shared_count(_Ptr __p
, _Deleter __d
, _Alloc __a
) : _M_pi(0)
589 typedef _Sp_counted_deleter
<_Ptr
, _Deleter
, _Alloc
, _Lp
> _Sp_cd_type
;
592 typename
_Sp_cd_type::__allocator_type
__a2(__a
);
593 auto __guard
= std::__allocate_guarded(__a2
);
594 _Sp_cd_type
* __mem
= __guard
.get();
595 ::new (__mem
) _Sp_cd_type(__p
, std::move(__d
), std::move(__a
));
601 __d(__p
); // Call _Deleter on __p.
602 __throw_exception_again
;
606 template<typename _Tp
, typename _Alloc
, typename
... _Args
>
607 __shared_count(_Sp_make_shared_tag
, _Tp
*, const _Alloc
& __a
,
611 typedef _Sp_counted_ptr_inplace
<_Tp
, _Alloc
, _Lp
> _Sp_cp_type
;
612 typename
_Sp_cp_type::__allocator_type
__a2(__a
);
613 auto __guard
= std::__allocate_guarded(__a2
);
614 _Sp_cp_type
* __mem
= __guard
.get();
615 ::new (__mem
) _Sp_cp_type(std::move(__a
),
616 std::forward
<_Args
>(__args
)...);
621 #if _GLIBCXX_USE_DEPRECATED
622 // Special case for auto_ptr<_Tp> to provide the strong guarantee.
623 template<typename _Tp
>
625 __shared_count(std::auto_ptr
<_Tp
>&& __r
);
628 // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee.
629 template<typename _Tp
, typename _Del
>
631 __shared_count(std::unique_ptr
<_Tp
, _Del
>&& __r
) : _M_pi(0)
633 // _GLIBCXX_RESOLVE_LIB_DEFECTS
634 // 2415. Inconsistency between unique_ptr and shared_ptr
635 if (__r
.get() == nullptr)
638 using _Ptr
= typename unique_ptr
<_Tp
, _Del
>::pointer
;
639 using _Del2
= typename conditional
<is_reference
<_Del
>::value
,
640 reference_wrapper
<typename remove_reference
<_Del
>::type
>,
643 = _Sp_counted_deleter
<_Ptr
, _Del2
, allocator
<void>, _Lp
>;
644 using _Alloc
= allocator
<_Sp_cd_type
>;
645 using _Alloc_traits
= allocator_traits
<_Alloc
>;
647 _Sp_cd_type
* __mem
= _Alloc_traits::allocate(__a
, 1);
648 _Alloc_traits::construct(__a
, __mem
, __r
.release(),
649 __r
.get_deleter()); // non-throwing
653 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
654 explicit __shared_count(const __weak_count
<_Lp
>& __r
);
656 // Does not throw if __r._M_get_use_count() == 0, caller must check.
657 explicit __shared_count(const __weak_count
<_Lp
>& __r
, std::nothrow_t
);
659 ~__shared_count() noexcept
661 if (_M_pi
!= nullptr)
665 __shared_count(const __shared_count
& __r
) noexcept
669 _M_pi
->_M_add_ref_copy();
673 operator=(const __shared_count
& __r
) noexcept
675 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
679 __tmp
->_M_add_ref_copy();
688 _M_swap(__shared_count
& __r
) noexcept
690 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
696 _M_get_use_count() const noexcept
697 { return _M_pi
!= 0 ? _M_pi
->_M_get_use_count() : 0; }
700 _M_unique() const noexcept
701 { return this->_M_get_use_count() == 1; }
704 _M_get_deleter(const std::type_info
& __ti
) const noexcept
705 { return _M_pi
? _M_pi
->_M_get_deleter(__ti
) : nullptr; }
708 _M_less(const __shared_count
& __rhs
) const noexcept
709 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
712 _M_less(const __weak_count
<_Lp
>& __rhs
) const noexcept
713 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
715 // Friend function injected into enclosing namespace and found by ADL
717 operator==(const __shared_count
& __a
, const __shared_count
& __b
) noexcept
718 { return __a
._M_pi
== __b
._M_pi
; }
721 friend class __weak_count
<_Lp
>;
723 _Sp_counted_base
<_Lp
>* _M_pi
;
727 template<_Lock_policy _Lp
>
731 constexpr __weak_count() noexcept
: _M_pi(nullptr)
734 __weak_count(const __shared_count
<_Lp
>& __r
) noexcept
737 if (_M_pi
!= nullptr)
738 _M_pi
->_M_weak_add_ref();
741 __weak_count(const __weak_count
& __r
) noexcept
744 if (_M_pi
!= nullptr)
745 _M_pi
->_M_weak_add_ref();
748 __weak_count(__weak_count
&& __r
) noexcept
750 { __r
._M_pi
= nullptr; }
752 ~__weak_count() noexcept
754 if (_M_pi
!= nullptr)
755 _M_pi
->_M_weak_release();
759 operator=(const __shared_count
<_Lp
>& __r
) noexcept
761 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
762 if (__tmp
!= nullptr)
763 __tmp
->_M_weak_add_ref();
764 if (_M_pi
!= nullptr)
765 _M_pi
->_M_weak_release();
771 operator=(const __weak_count
& __r
) noexcept
773 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
774 if (__tmp
!= nullptr)
775 __tmp
->_M_weak_add_ref();
776 if (_M_pi
!= nullptr)
777 _M_pi
->_M_weak_release();
783 operator=(__weak_count
&& __r
) noexcept
785 if (_M_pi
!= nullptr)
786 _M_pi
->_M_weak_release();
793 _M_swap(__weak_count
& __r
) noexcept
795 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
801 _M_get_use_count() const noexcept
802 { return _M_pi
!= nullptr ? _M_pi
->_M_get_use_count() : 0; }
805 _M_less(const __weak_count
& __rhs
) const noexcept
806 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
809 _M_less(const __shared_count
<_Lp
>& __rhs
) const noexcept
810 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
812 // Friend function injected into enclosing namespace and found by ADL
814 operator==(const __weak_count
& __a
, const __weak_count
& __b
) noexcept
815 { return __a
._M_pi
== __b
._M_pi
; }
818 friend class __shared_count
<_Lp
>;
820 _Sp_counted_base
<_Lp
>* _M_pi
;
823 // Now that __weak_count is defined we can define this constructor:
824 template<_Lock_policy _Lp
>
826 __shared_count
<_Lp
>::__shared_count(const __weak_count
<_Lp
>& __r
)
829 if (_M_pi
!= nullptr)
830 _M_pi
->_M_add_ref_lock();
832 __throw_bad_weak_ptr();
835 // Now that __weak_count is defined we can define this constructor:
836 template<_Lock_policy _Lp
>
838 __shared_count
<_Lp
>::
839 __shared_count(const __weak_count
<_Lp
>& __r
, std::nothrow_t
)
842 if (_M_pi
!= nullptr)
843 if (!_M_pi
->_M_add_ref_lock_nothrow())
847 // Support for enable_shared_from_this.
849 // Friend of __enable_shared_from_this.
850 template<_Lock_policy _Lp
, typename _Tp1
, typename _Tp2
>
852 __enable_shared_from_this_helper(const __shared_count
<_Lp
>&,
853 const __enable_shared_from_this
<_Tp1
,
854 _Lp
>*, const _Tp2
*) noexcept
;
856 // Friend of enable_shared_from_this.
857 template<typename _Tp1
, typename _Tp2
>
859 __enable_shared_from_this_helper(const __shared_count
<>&,
860 const enable_shared_from_this
<_Tp1
>*,
861 const _Tp2
*) noexcept
;
863 template<_Lock_policy _Lp
>
865 __enable_shared_from_this_helper(const __shared_count
<_Lp
>&, ...) noexcept
869 template<typename _Tp
, _Lock_policy _Lp
>
872 template<typename _Ptr
>
874 = typename enable_if
<is_convertible
<_Ptr
, _Tp
*>::value
>::type
;
877 typedef _Tp element_type
;
879 constexpr __shared_ptr() noexcept
880 : _M_ptr(0), _M_refcount()
883 template<typename _Tp1
>
884 explicit __shared_ptr(_Tp1
* __p
)
885 : _M_ptr(__p
), _M_refcount(__p
)
887 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
888 static_assert( !is_void
<_Tp1
>::value
, "incomplete type" );
889 static_assert( sizeof(_Tp1
) > 0, "incomplete type" );
890 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
893 template<typename _Tp1
, typename _Deleter
>
894 __shared_ptr(_Tp1
* __p
, _Deleter __d
)
895 : _M_ptr(__p
), _M_refcount(__p
, __d
)
897 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
898 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
899 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
902 template<typename _Tp1
, typename _Deleter
, typename _Alloc
>
903 __shared_ptr(_Tp1
* __p
, _Deleter __d
, _Alloc __a
)
904 : _M_ptr(__p
), _M_refcount(__p
, __d
, std::move(__a
))
906 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
907 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
908 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
911 template<typename _Deleter
>
912 __shared_ptr(nullptr_t __p
, _Deleter __d
)
913 : _M_ptr(0), _M_refcount(__p
, __d
)
916 template<typename _Deleter
, typename _Alloc
>
917 __shared_ptr(nullptr_t __p
, _Deleter __d
, _Alloc __a
)
918 : _M_ptr(0), _M_refcount(__p
, __d
, std::move(__a
))
921 template<typename _Tp1
>
922 __shared_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
, _Tp
* __p
) noexcept
923 : _M_ptr(__p
), _M_refcount(__r
._M_refcount
) // never throws
926 __shared_ptr(const __shared_ptr
&) noexcept
= default;
927 __shared_ptr
& operator=(const __shared_ptr
&) noexcept
= default;
928 ~__shared_ptr() = default;
930 template<typename _Tp1
, typename
= _Convertible
<_Tp1
*>>
931 __shared_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
) noexcept
932 : _M_ptr(__r
._M_ptr
), _M_refcount(__r
._M_refcount
)
935 __shared_ptr(__shared_ptr
&& __r
) noexcept
936 : _M_ptr(__r
._M_ptr
), _M_refcount()
938 _M_refcount
._M_swap(__r
._M_refcount
);
942 template<typename _Tp1
, typename
= _Convertible
<_Tp1
*>>
943 __shared_ptr(__shared_ptr
<_Tp1
, _Lp
>&& __r
) noexcept
944 : _M_ptr(__r
._M_ptr
), _M_refcount()
946 _M_refcount
._M_swap(__r
._M_refcount
);
950 template<typename _Tp1
>
951 explicit __shared_ptr(const __weak_ptr
<_Tp1
, _Lp
>& __r
)
952 : _M_refcount(__r
._M_refcount
) // may throw
954 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
956 // It is now safe to copy __r._M_ptr, as
957 // _M_refcount(__r._M_refcount) did not throw.
961 // If an exception is thrown this constructor has no effect.
962 template<typename _Tp1
, typename _Del
, typename
963 = _Convertible
<typename unique_ptr
<_Tp1
, _Del
>::pointer
>>
964 __shared_ptr(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
965 : _M_ptr(__r
.get()), _M_refcount()
967 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
968 auto __raw
= _S_raw_ptr(__r
.get());
969 _M_refcount
= __shared_count
<_Lp
>(std::move(__r
));
970 __enable_shared_from_this_helper(_M_refcount
, __raw
, __raw
);
973 #if _GLIBCXX_USE_DEPRECATED
974 // Postcondition: use_count() == 1 and __r.get() == 0
975 template<typename _Tp1
>
976 __shared_ptr(std::auto_ptr
<_Tp1
>&& __r
);
979 constexpr __shared_ptr(nullptr_t
) noexcept
: __shared_ptr() { }
981 template<typename _Tp1
>
983 operator=(const __shared_ptr
<_Tp1
, _Lp
>& __r
) noexcept
986 _M_refcount
= __r
._M_refcount
; // __shared_count::op= doesn't throw
990 #if _GLIBCXX_USE_DEPRECATED
991 template<typename _Tp1
>
993 operator=(std::auto_ptr
<_Tp1
>&& __r
)
995 __shared_ptr(std::move(__r
)).swap(*this);
1001 operator=(__shared_ptr
&& __r
) noexcept
1003 __shared_ptr(std::move(__r
)).swap(*this);
1007 template<class _Tp1
>
1009 operator=(__shared_ptr
<_Tp1
, _Lp
>&& __r
) noexcept
1011 __shared_ptr(std::move(__r
)).swap(*this);
1015 template<typename _Tp1
, typename _Del
>
1017 operator=(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
1019 __shared_ptr(std::move(__r
)).swap(*this);
1025 { __shared_ptr().swap(*this); }
1027 template<typename _Tp1
>
1029 reset(_Tp1
* __p
) // _Tp1 must be complete.
1031 // Catch self-reset errors.
1032 __glibcxx_assert(__p
== 0 || __p
!= _M_ptr
);
1033 __shared_ptr(__p
).swap(*this);
1036 template<typename _Tp1
, typename _Deleter
>
1038 reset(_Tp1
* __p
, _Deleter __d
)
1039 { __shared_ptr(__p
, __d
).swap(*this); }
1041 template<typename _Tp1
, typename _Deleter
, typename _Alloc
>
1043 reset(_Tp1
* __p
, _Deleter __d
, _Alloc __a
)
1044 { __shared_ptr(__p
, __d
, std::move(__a
)).swap(*this); }
1046 // Allow class instantiation when _Tp is [cv-qual] void.
1047 typename
std::add_lvalue_reference
<_Tp
>::type
1048 operator*() const noexcept
1050 __glibcxx_assert(_M_ptr
!= 0);
1055 operator->() const noexcept
1057 _GLIBCXX_DEBUG_PEDASSERT(_M_ptr
!= 0);
1062 get() const noexcept
1065 explicit operator bool() const // never throws
1066 { return _M_ptr
== 0 ? false : true; }
1069 unique() const noexcept
1070 { return _M_refcount
._M_unique(); }
1073 use_count() const noexcept
1074 { return _M_refcount
._M_get_use_count(); }
1077 swap(__shared_ptr
<_Tp
, _Lp
>& __other
) noexcept
1079 std::swap(_M_ptr
, __other
._M_ptr
);
1080 _M_refcount
._M_swap(__other
._M_refcount
);
1083 template<typename _Tp1
>
1085 owner_before(__shared_ptr
<_Tp1
, _Lp
> const& __rhs
) const
1086 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1088 template<typename _Tp1
>
1090 owner_before(__weak_ptr
<_Tp1
, _Lp
> const& __rhs
) const
1091 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1095 // This constructor is non-standard, it is used by allocate_shared.
1096 template<typename _Alloc
, typename
... _Args
>
1097 __shared_ptr(_Sp_make_shared_tag __tag
, const _Alloc
& __a
,
1099 : _M_ptr(), _M_refcount(__tag
, (_Tp
*)0, __a
,
1100 std::forward
<_Args
>(__args
)...)
1102 // _M_ptr needs to point to the newly constructed object.
1103 // This relies on _Sp_counted_ptr_inplace::_M_get_deleter.
1104 void* __p
= _M_refcount
._M_get_deleter(typeid(__tag
));
1105 _M_ptr
= static_cast<_Tp
*>(__p
);
1106 __enable_shared_from_this_helper(_M_refcount
, _M_ptr
, _M_ptr
);
1109 template<typename _Alloc
>
1112 void operator()(typename
_Alloc::value_type
* __ptr
)
1114 __allocated_ptr
<_Alloc
> __guard
{ _M_alloc
, __ptr
};
1115 allocator_traits
<_Alloc
>::destroy(_M_alloc
, __guard
.get());
1120 template<typename _Alloc
, typename
... _Args
>
1121 __shared_ptr(_Sp_make_shared_tag __tag
, const _Alloc
& __a
,
1123 : _M_ptr(), _M_refcount()
1125 typedef typename allocator_traits
<_Alloc
>::template
1126 rebind_traits
<typename
std::remove_cv
<_Tp
>::type
> __traits
;
1127 _Deleter
<typename
__traits::allocator_type
> __del
= { __a
};
1128 auto __guard
= std::__allocate_guarded(__del
._M_alloc
);
1129 auto __ptr
= __guard
.get();
1130 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1131 // 2070. allocate_shared should use allocator_traits<A>::construct
1132 __traits::construct(__del
._M_alloc
, __ptr
,
1133 std::forward
<_Args
>(__args
)...);
1135 __shared_count
<_Lp
> __count(__ptr
, __del
, __del
._M_alloc
);
1136 _M_refcount
._M_swap(__count
);
1138 __enable_shared_from_this_helper(_M_refcount
, _M_ptr
, _M_ptr
);
1142 template<typename _Tp1
, _Lock_policy _Lp1
, typename _Alloc
,
1144 friend __shared_ptr
<_Tp1
, _Lp1
>
1145 __allocate_shared(const _Alloc
& __a
, _Args
&&... __args
);
1147 // This constructor is used by __weak_ptr::lock() and
1148 // shared_ptr::shared_ptr(const weak_ptr&, std::nothrow_t).
1149 __shared_ptr(const __weak_ptr
<_Tp
, _Lp
>& __r
, std::nothrow_t
)
1150 : _M_refcount(__r
._M_refcount
, std::nothrow
)
1152 _M_ptr
= _M_refcount
._M_get_use_count() ? __r
._M_ptr
: nullptr;
1155 friend class __weak_ptr
<_Tp
, _Lp
>;
1159 _M_get_deleter(const std::type_info
& __ti
) const noexcept
1160 { return _M_refcount
._M_get_deleter(__ti
); }
1162 template<typename _Tp1
>
1164 _S_raw_ptr(_Tp1
* __ptr
)
1167 template<typename _Tp1
>
1169 _S_raw_ptr(_Tp1 __ptr
) -> decltype(std::__addressof(*__ptr
))
1170 { return std::__addressof(*__ptr
); }
1172 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __shared_ptr
;
1173 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __weak_ptr
;
1175 template<typename _Del
, typename _Tp1
, _Lock_policy _Lp1
>
1176 friend _Del
* get_deleter(const __shared_ptr
<_Tp1
, _Lp1
>&) noexcept
;
1178 _Tp
* _M_ptr
; // Contained pointer.
1179 __shared_count
<_Lp
> _M_refcount
; // Reference counter.
1183 // 20.7.2.2.7 shared_ptr comparisons
1184 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1186 operator==(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1187 const __shared_ptr
<_Tp2
, _Lp
>& __b
) noexcept
1188 { return __a
.get() == __b
.get(); }
1190 template<typename _Tp
, _Lock_policy _Lp
>
1192 operator==(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
) noexcept
1195 template<typename _Tp
, _Lock_policy _Lp
>
1197 operator==(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __a
) noexcept
1200 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1202 operator!=(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1203 const __shared_ptr
<_Tp2
, _Lp
>& __b
) noexcept
1204 { return __a
.get() != __b
.get(); }
1206 template<typename _Tp
, _Lock_policy _Lp
>
1208 operator!=(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
) noexcept
1209 { return (bool)__a
; }
1211 template<typename _Tp
, _Lock_policy _Lp
>
1213 operator!=(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __a
) noexcept
1214 { return (bool)__a
; }
1216 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1218 operator<(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1219 const __shared_ptr
<_Tp2
, _Lp
>& __b
) noexcept
1221 typedef typename
std::common_type
<_Tp1
*, _Tp2
*>::type _CT
;
1222 return std::less
<_CT
>()(__a
.get(), __b
.get());
1225 template<typename _Tp
, _Lock_policy _Lp
>
1227 operator<(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
) noexcept
1228 { return std::less
<_Tp
*>()(__a
.get(), nullptr); }
1230 template<typename _Tp
, _Lock_policy _Lp
>
1232 operator<(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __a
) noexcept
1233 { return std::less
<_Tp
*>()(nullptr, __a
.get()); }
1235 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1237 operator<=(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1238 const __shared_ptr
<_Tp2
, _Lp
>& __b
) noexcept
1239 { return !(__b
< __a
); }
1241 template<typename _Tp
, _Lock_policy _Lp
>
1243 operator<=(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
) noexcept
1244 { return !(nullptr < __a
); }
1246 template<typename _Tp
, _Lock_policy _Lp
>
1248 operator<=(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __a
) noexcept
1249 { return !(__a
< nullptr); }
1251 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1253 operator>(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1254 const __shared_ptr
<_Tp2
, _Lp
>& __b
) noexcept
1255 { return (__b
< __a
); }
1257 template<typename _Tp
, _Lock_policy _Lp
>
1259 operator>(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
) noexcept
1260 { return std::less
<_Tp
*>()(nullptr, __a
.get()); }
1262 template<typename _Tp
, _Lock_policy _Lp
>
1264 operator>(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __a
) noexcept
1265 { return std::less
<_Tp
*>()(__a
.get(), nullptr); }
1267 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1269 operator>=(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1270 const __shared_ptr
<_Tp2
, _Lp
>& __b
) noexcept
1271 { return !(__a
< __b
); }
1273 template<typename _Tp
, _Lock_policy _Lp
>
1275 operator>=(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
) noexcept
1276 { return !(__a
< nullptr); }
1278 template<typename _Tp
, _Lock_policy _Lp
>
1280 operator>=(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __a
) noexcept
1281 { return !(nullptr < __a
); }
1283 template<typename _Sp
>
1284 struct _Sp_less
: public binary_function
<_Sp
, _Sp
, bool>
1287 operator()(const _Sp
& __lhs
, const _Sp
& __rhs
) const noexcept
1289 typedef typename
_Sp::element_type element_type
;
1290 return std::less
<element_type
*>()(__lhs
.get(), __rhs
.get());
1294 template<typename _Tp
, _Lock_policy _Lp
>
1295 struct less
<__shared_ptr
<_Tp
, _Lp
>>
1296 : public _Sp_less
<__shared_ptr
<_Tp
, _Lp
>>
1299 // 20.7.2.2.8 shared_ptr specialized algorithms.
1300 template<typename _Tp
, _Lock_policy _Lp
>
1302 swap(__shared_ptr
<_Tp
, _Lp
>& __a
, __shared_ptr
<_Tp
, _Lp
>& __b
) noexcept
1305 // 20.7.2.2.9 shared_ptr casts
1307 // The seemingly equivalent code:
1308 // shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))
1309 // will eventually result in undefined behaviour, attempting to
1310 // delete the same object twice.
1311 /// static_pointer_cast
1312 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
1313 inline __shared_ptr
<_Tp
, _Lp
>
1314 static_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
) noexcept
1315 { return __shared_ptr
<_Tp
, _Lp
>(__r
, static_cast<_Tp
*>(__r
.get())); }
1317 // The seemingly equivalent code:
1318 // shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))
1319 // will eventually result in undefined behaviour, attempting to
1320 // delete the same object twice.
1321 /// const_pointer_cast
1322 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
1323 inline __shared_ptr
<_Tp
, _Lp
>
1324 const_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
) noexcept
1325 { return __shared_ptr
<_Tp
, _Lp
>(__r
, const_cast<_Tp
*>(__r
.get())); }
1327 // The seemingly equivalent code:
1328 // shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))
1329 // will eventually result in undefined behaviour, attempting to
1330 // delete the same object twice.
1331 /// dynamic_pointer_cast
1332 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
1333 inline __shared_ptr
<_Tp
, _Lp
>
1334 dynamic_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
) noexcept
1336 if (_Tp
* __p
= dynamic_cast<_Tp
*>(__r
.get()))
1337 return __shared_ptr
<_Tp
, _Lp
>(__r
, __p
);
1338 return __shared_ptr
<_Tp
, _Lp
>();
1342 template<typename _Tp
, _Lock_policy _Lp
>
1345 template<typename _Ptr
>
1347 = typename enable_if
<is_convertible
<_Ptr
, _Tp
*>::value
>::type
;
1350 typedef _Tp element_type
;
1352 constexpr __weak_ptr() noexcept
1353 : _M_ptr(nullptr), _M_refcount()
1356 __weak_ptr(const __weak_ptr
&) noexcept
= default;
1358 ~__weak_ptr() = default;
1360 // The "obvious" converting constructor implementation:
1362 // template<typename _Tp1>
1363 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
1364 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
1367 // has a serious problem.
1369 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
1370 // conversion may require access to *__r._M_ptr (virtual inheritance).
1372 // It is not possible to avoid spurious access violations since
1373 // in multithreaded programs __r._M_ptr may be invalidated at any point.
1374 template<typename _Tp1
, typename
= _Convertible
<_Tp1
*>>
1375 __weak_ptr(const __weak_ptr
<_Tp1
, _Lp
>& __r
) noexcept
1376 : _M_refcount(__r
._M_refcount
)
1377 { _M_ptr
= __r
.lock().get(); }
1379 template<typename _Tp1
, typename
= _Convertible
<_Tp1
*>>
1380 __weak_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
) noexcept
1381 : _M_ptr(__r
._M_ptr
), _M_refcount(__r
._M_refcount
)
1384 __weak_ptr(__weak_ptr
&& __r
) noexcept
1385 : _M_ptr(__r
._M_ptr
), _M_refcount(std::move(__r
._M_refcount
))
1386 { __r
._M_ptr
= nullptr; }
1388 template<typename _Tp1
, typename
= _Convertible
<_Tp1
*>>
1389 __weak_ptr(__weak_ptr
<_Tp1
, _Lp
>&& __r
) noexcept
1390 : _M_ptr(__r
.lock().get()), _M_refcount(std::move(__r
._M_refcount
))
1391 { __r
._M_ptr
= nullptr; }
1394 operator=(const __weak_ptr
& __r
) noexcept
= default;
1396 template<typename _Tp1
>
1398 operator=(const __weak_ptr
<_Tp1
, _Lp
>& __r
) noexcept
1400 _M_ptr
= __r
.lock().get();
1401 _M_refcount
= __r
._M_refcount
;
1405 template<typename _Tp1
>
1407 operator=(const __shared_ptr
<_Tp1
, _Lp
>& __r
) noexcept
1409 _M_ptr
= __r
._M_ptr
;
1410 _M_refcount
= __r
._M_refcount
;
1415 operator=(__weak_ptr
&& __r
) noexcept
1417 _M_ptr
= __r
._M_ptr
;
1418 _M_refcount
= std::move(__r
._M_refcount
);
1419 __r
._M_ptr
= nullptr;
1423 template<typename _Tp1
>
1425 operator=(__weak_ptr
<_Tp1
, _Lp
>&& __r
) noexcept
1427 _M_ptr
= __r
.lock().get();
1428 _M_refcount
= std::move(__r
._M_refcount
);
1429 __r
._M_ptr
= nullptr;
1433 __shared_ptr
<_Tp
, _Lp
>
1434 lock() const noexcept
1435 { return __shared_ptr
<element_type
, _Lp
>(*this, std::nothrow
); }
1438 use_count() const noexcept
1439 { return _M_refcount
._M_get_use_count(); }
1442 expired() const noexcept
1443 { return _M_refcount
._M_get_use_count() == 0; }
1445 template<typename _Tp1
>
1447 owner_before(const __shared_ptr
<_Tp1
, _Lp
>& __rhs
) const
1448 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1450 template<typename _Tp1
>
1452 owner_before(const __weak_ptr
<_Tp1
, _Lp
>& __rhs
) const
1453 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1457 { __weak_ptr().swap(*this); }
1460 swap(__weak_ptr
& __s
) noexcept
1462 std::swap(_M_ptr
, __s
._M_ptr
);
1463 _M_refcount
._M_swap(__s
._M_refcount
);
1467 // Used by __enable_shared_from_this.
1469 _M_assign(_Tp
* __ptr
, const __shared_count
<_Lp
>& __refcount
) noexcept
1471 if (use_count() == 0)
1474 _M_refcount
= __refcount
;
1478 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __shared_ptr
;
1479 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __weak_ptr
;
1480 friend class __enable_shared_from_this
<_Tp
, _Lp
>;
1481 friend class enable_shared_from_this
<_Tp
>;
1483 _Tp
* _M_ptr
; // Contained pointer.
1484 __weak_count
<_Lp
> _M_refcount
; // Reference counter.
1487 // 20.7.2.3.6 weak_ptr specialized algorithms.
1488 template<typename _Tp
, _Lock_policy _Lp
>
1490 swap(__weak_ptr
<_Tp
, _Lp
>& __a
, __weak_ptr
<_Tp
, _Lp
>& __b
) noexcept
1493 template<typename _Tp
, typename _Tp1
>
1494 struct _Sp_owner_less
: public binary_function
<_Tp
, _Tp
, bool>
1497 operator()(const _Tp
& __lhs
, const _Tp
& __rhs
) const
1498 { return __lhs
.owner_before(__rhs
); }
1501 operator()(const _Tp
& __lhs
, const _Tp1
& __rhs
) const
1502 { return __lhs
.owner_before(__rhs
); }
1505 operator()(const _Tp1
& __lhs
, const _Tp
& __rhs
) const
1506 { return __lhs
.owner_before(__rhs
); }
1509 template<typename _Tp
, _Lock_policy _Lp
>
1510 struct owner_less
<__shared_ptr
<_Tp
, _Lp
>>
1511 : public _Sp_owner_less
<__shared_ptr
<_Tp
, _Lp
>, __weak_ptr
<_Tp
, _Lp
>>
1514 template<typename _Tp
, _Lock_policy _Lp
>
1515 struct owner_less
<__weak_ptr
<_Tp
, _Lp
>>
1516 : public _Sp_owner_less
<__weak_ptr
<_Tp
, _Lp
>, __shared_ptr
<_Tp
, _Lp
>>
1520 template<typename _Tp
, _Lock_policy _Lp
>
1521 class __enable_shared_from_this
1524 constexpr __enable_shared_from_this() noexcept
{ }
1526 __enable_shared_from_this(const __enable_shared_from_this
&) noexcept
{ }
1528 __enable_shared_from_this
&
1529 operator=(const __enable_shared_from_this
&) noexcept
1532 ~__enable_shared_from_this() { }
1535 __shared_ptr
<_Tp
, _Lp
>
1537 { return __shared_ptr
<_Tp
, _Lp
>(this->_M_weak_this
); }
1539 __shared_ptr
<const _Tp
, _Lp
>
1540 shared_from_this() const
1541 { return __shared_ptr
<const _Tp
, _Lp
>(this->_M_weak_this
); }
1544 template<typename _Tp1
>
1546 _M_weak_assign(_Tp1
* __p
, const __shared_count
<_Lp
>& __n
) const noexcept
1547 { _M_weak_this
._M_assign(__p
, __n
); }
1549 template<_Lock_policy _Lp1
, typename _Tp1
, typename _Tp2
>
1551 __enable_shared_from_this_helper(const __shared_count
<_Lp1
>&,
1552 const __enable_shared_from_this
<_Tp1
,
1553 _Lp1
>*, const _Tp2
*) noexcept
;
1555 mutable __weak_ptr
<_Tp
, _Lp
> _M_weak_this
;
1558 template<_Lock_policy _Lp1
, typename _Tp1
, typename _Tp2
>
1560 __enable_shared_from_this_helper(const __shared_count
<_Lp1
>& __pn
,
1561 const __enable_shared_from_this
<_Tp1
,
1563 const _Tp2
* __px
) noexcept
1565 if (__pe
!= nullptr)
1566 __pe
->_M_weak_assign(const_cast<_Tp2
*>(__px
), __pn
);
1569 template<typename _Tp
, _Lock_policy _Lp
, typename _Alloc
, typename
... _Args
>
1570 inline __shared_ptr
<_Tp
, _Lp
>
1571 __allocate_shared(const _Alloc
& __a
, _Args
&&... __args
)
1573 return __shared_ptr
<_Tp
, _Lp
>(_Sp_make_shared_tag(), __a
,
1574 std::forward
<_Args
>(__args
)...);
1577 template<typename _Tp
, _Lock_policy _Lp
, typename
... _Args
>
1578 inline __shared_ptr
<_Tp
, _Lp
>
1579 __make_shared(_Args
&&... __args
)
1581 typedef typename
std::remove_const
<_Tp
>::type _Tp_nc
;
1582 return std::__allocate_shared
<_Tp
, _Lp
>(std::allocator
<_Tp_nc
>(),
1583 std::forward
<_Args
>(__args
)...);
1586 /// std::hash specialization for __shared_ptr.
1587 template<typename _Tp
, _Lock_policy _Lp
>
1588 struct hash
<__shared_ptr
<_Tp
, _Lp
>>
1589 : public __hash_base
<size_t, __shared_ptr
<_Tp
, _Lp
>>
1592 operator()(const __shared_ptr
<_Tp
, _Lp
>& __s
) const noexcept
1593 { return std::hash
<_Tp
*>()(__s
.get()); }
1596 _GLIBCXX_END_NAMESPACE_VERSION
1599 #endif // _SHARED_PTR_BASE_H