1 // shared_ptr and weak_ptr implementation details -*- C++ -*-
3 // Copyright (C) 2007-2014 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
52 #include <ext/aligned_buffer.h>
54 namespace std
_GLIBCXX_VISIBILITY(default)
56 _GLIBCXX_BEGIN_NAMESPACE_VERSION
58 #if _GLIBCXX_USE_DEPRECATED
59 template<typename
> class auto_ptr
;
63 * @brief Exception possibly thrown by @c shared_ptr.
66 class bad_weak_ptr
: public std::exception
70 what() const noexcept
;
72 virtual ~bad_weak_ptr() noexcept
;
75 // Substitute for bad_weak_ptr object in the case of -fno-exceptions.
77 __throw_bad_weak_ptr()
78 { _GLIBCXX_THROW_OR_ABORT(bad_weak_ptr()); }
80 using __gnu_cxx::_Lock_policy
;
81 using __gnu_cxx::__default_lock_policy
;
82 using __gnu_cxx::_S_single
;
83 using __gnu_cxx::_S_mutex
;
84 using __gnu_cxx::_S_atomic
;
86 // Empty helper class except when the template argument is _S_mutex.
87 template<_Lock_policy _Lp
>
91 // The atomic policy uses fully-fenced builtins, single doesn't care.
92 enum { _S_need_barriers
= 0 };
96 class _Mutex_base
<_S_mutex
>
97 : public __gnu_cxx::__mutex
100 // This policy is used when atomic builtins are not available.
101 // The replacement atomic operations might not have the necessary
103 enum { _S_need_barriers
= 1 };
106 template<_Lock_policy _Lp
= __default_lock_policy
>
107 class _Sp_counted_base
108 : public _Mutex_base
<_Lp
>
111 _Sp_counted_base() noexcept
112 : _M_use_count(1), _M_weak_count(1) { }
115 ~_Sp_counted_base() noexcept
118 // Called when _M_use_count drops to zero, to release the resources
121 _M_dispose() noexcept
= 0;
123 // Called when _M_weak_count drops to zero.
125 _M_destroy() noexcept
129 _M_get_deleter(const std::type_info
&) noexcept
= 0;
133 { __gnu_cxx::__atomic_add_dispatch(&_M_use_count
, 1); }
139 _M_add_ref_lock_nothrow();
142 _M_release() noexcept
144 // Be race-detector-friendly. For more info see bits/c++config.
145 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count
);
146 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count
, -1) == 1)
148 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count
);
150 // There must be a memory barrier between dispose() and destroy()
151 // to ensure that the effects of dispose() are observed in the
152 // thread that runs destroy().
153 // See http://gcc.gnu.org/ml/libstdc++/2005-11/msg00136.html
154 if (_Mutex_base
<_Lp
>::_S_need_barriers
)
156 _GLIBCXX_READ_MEM_BARRIER
;
157 _GLIBCXX_WRITE_MEM_BARRIER
;
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 _GLIBCXX_READ_MEM_BARRIER
;
188 _GLIBCXX_WRITE_MEM_BARRIER
;
195 _M_get_use_count() const noexcept
197 // No memory barrier is used here so there is no synchronization
198 // with other threads.
199 return __atomic_load_n(&_M_use_count
, __ATOMIC_RELAXED
);
203 _Sp_counted_base(_Sp_counted_base
const&) = delete;
204 _Sp_counted_base
& operator=(_Sp_counted_base
const&) = delete;
206 _Atomic_word _M_use_count
; // #shared
207 _Atomic_word _M_weak_count
; // #weak + (#shared != 0)
212 _Sp_counted_base
<_S_single
>::
215 if (_M_use_count
== 0)
216 __throw_bad_weak_ptr();
222 _Sp_counted_base
<_S_mutex
>::
225 __gnu_cxx::__scoped_lock
sentry(*this);
226 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count
, 1) == 0)
229 __throw_bad_weak_ptr();
235 _Sp_counted_base
<_S_atomic
>::
238 // Perform lock-free add-if-not-zero operation.
239 _Atomic_word __count
= _M_get_use_count();
243 __throw_bad_weak_ptr();
244 // Replace the current counter value with the old value + 1, as
245 // long as it's not changed meanwhile.
247 while (!__atomic_compare_exchange_n(&_M_use_count
, &__count
, __count
+ 1,
248 true, __ATOMIC_ACQ_REL
,
254 _Sp_counted_base
<_S_single
>::
255 _M_add_ref_lock_nothrow()
257 if (_M_use_count
== 0)
265 _Sp_counted_base
<_S_mutex
>::
266 _M_add_ref_lock_nothrow()
268 __gnu_cxx::__scoped_lock
sentry(*this);
269 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count
, 1) == 0)
279 _Sp_counted_base
<_S_atomic
>::
280 _M_add_ref_lock_nothrow()
282 // Perform lock-free add-if-not-zero operation.
283 _Atomic_word __count
= _M_get_use_count();
288 // Replace the current counter value with the old value + 1, as
289 // long as it's not changed meanwhile.
291 while (!__atomic_compare_exchange_n(&_M_use_count
, &__count
, __count
+ 1,
292 true, __ATOMIC_ACQ_REL
,
299 _Sp_counted_base
<_S_single
>::_M_add_ref_copy()
304 _Sp_counted_base
<_S_single
>::_M_release() noexcept
306 if (--_M_use_count
== 0)
309 if (--_M_weak_count
== 0)
316 _Sp_counted_base
<_S_single
>::_M_weak_add_ref() noexcept
321 _Sp_counted_base
<_S_single
>::_M_weak_release() noexcept
323 if (--_M_weak_count
== 0)
329 _Sp_counted_base
<_S_single
>::_M_get_use_count() const noexcept
330 { return _M_use_count
; }
333 // Forward declarations.
334 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
337 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
340 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
341 class __enable_shared_from_this
;
343 template<typename _Tp
>
346 template<typename _Tp
>
349 template<typename _Tp
>
352 template<typename _Tp
>
353 class enable_shared_from_this
;
355 template<_Lock_policy _Lp
= __default_lock_policy
>
358 template<_Lock_policy _Lp
= __default_lock_policy
>
359 class __shared_count
;
362 // Counted ptr with no deleter or allocator support
363 template<typename _Ptr
, _Lock_policy _Lp
>
364 class _Sp_counted_ptr final
: public _Sp_counted_base
<_Lp
>
368 _Sp_counted_ptr(_Ptr __p
) noexcept
372 _M_dispose() noexcept
376 _M_destroy() noexcept
380 _M_get_deleter(const std::type_info
&) noexcept
383 _Sp_counted_ptr(const _Sp_counted_ptr
&) = delete;
384 _Sp_counted_ptr
& operator=(const _Sp_counted_ptr
&) = delete;
392 _Sp_counted_ptr
<nullptr_t
, _S_single
>::_M_dispose() noexcept
{ }
396 _Sp_counted_ptr
<nullptr_t
, _S_mutex
>::_M_dispose() noexcept
{ }
400 _Sp_counted_ptr
<nullptr_t
, _S_atomic
>::_M_dispose() noexcept
{ }
402 template<int _Nm
, typename _Tp
,
403 bool __use_ebo
= !__is_final(_Tp
) && __is_empty(_Tp
)>
404 struct _Sp_ebo_helper
;
406 /// Specialization using EBO.
407 template<int _Nm
, typename _Tp
>
408 struct _Sp_ebo_helper
<_Nm
, _Tp
, true> : private _Tp
410 explicit _Sp_ebo_helper(const _Tp
& __tp
) : _Tp(__tp
) { }
413 _S_get(_Sp_ebo_helper
& __eboh
) { return static_cast<_Tp
&>(__eboh
); }
416 /// Specialization not using EBO.
417 template<int _Nm
, typename _Tp
>
418 struct _Sp_ebo_helper
<_Nm
, _Tp
, false>
420 explicit _Sp_ebo_helper(const _Tp
& __tp
) : _M_tp(__tp
) { }
423 _S_get(_Sp_ebo_helper
& __eboh
)
424 { return __eboh
._M_tp
; }
430 // Support for custom deleter and/or allocator
431 template<typename _Ptr
, typename _Deleter
, typename _Alloc
, _Lock_policy _Lp
>
432 class _Sp_counted_deleter final
: public _Sp_counted_base
<_Lp
>
434 class _Impl
: _Sp_ebo_helper
<0, _Deleter
>, _Sp_ebo_helper
<1, _Alloc
>
436 typedef _Sp_ebo_helper
<0, _Deleter
> _Del_base
;
437 typedef _Sp_ebo_helper
<1, _Alloc
> _Alloc_base
;
440 _Impl(_Ptr __p
, _Deleter __d
, const _Alloc
& __a
) noexcept
441 : _M_ptr(__p
), _Del_base(__d
), _Alloc_base(__a
)
444 _Deleter
& _M_del() noexcept
{ return _Del_base::_S_get(*this); }
445 _Alloc
& _M_alloc() noexcept
{ return _Alloc_base::_S_get(*this); }
451 // __d(__p) must not throw.
452 _Sp_counted_deleter(_Ptr __p
, _Deleter __d
) noexcept
453 : _M_impl(__p
, __d
, _Alloc()) { }
455 // __d(__p) must not throw.
456 _Sp_counted_deleter(_Ptr __p
, _Deleter __d
, const _Alloc
& __a
) noexcept
457 : _M_impl(__p
, __d
, __a
) { }
459 ~_Sp_counted_deleter() noexcept
{ }
462 _M_dispose() noexcept
463 { _M_impl
._M_del()(_M_impl
._M_ptr
); }
466 _M_destroy() noexcept
468 typedef typename allocator_traits
<_Alloc
>::template
469 rebind_traits
<_Sp_counted_deleter
> _Alloc_traits
;
470 typename
_Alloc_traits::allocator_type
__a(_M_impl
._M_alloc());
471 _Alloc_traits::destroy(__a
, this);
472 _Alloc_traits::deallocate(__a
, this, 1);
476 _M_get_deleter(const std::type_info
& __ti
) noexcept
479 return __ti
== typeid(_Deleter
) ? &_M_impl
._M_del() : nullptr;
489 // helpers for make_shared / allocate_shared
491 struct _Sp_make_shared_tag
{ };
493 template<typename _Tp
, typename _Alloc
, _Lock_policy _Lp
>
494 class _Sp_counted_ptr_inplace final
: public _Sp_counted_base
<_Lp
>
496 class _Impl
: _Sp_ebo_helper
<0, _Alloc
>
498 typedef _Sp_ebo_helper
<0, _Alloc
> _A_base
;
501 explicit _Impl(_Alloc __a
) noexcept
: _A_base(__a
) { }
503 _Alloc
& _M_alloc() noexcept
{ return _A_base::_S_get(*this); }
505 __gnu_cxx::__aligned_buffer
<_Tp
> _M_storage
;
509 template<typename
... _Args
>
510 _Sp_counted_ptr_inplace(_Alloc __a
, _Args
&&... __args
)
513 // _GLIBCXX_RESOLVE_LIB_DEFECTS
514 // 2070. allocate_shared should use allocator_traits<A>::construct
515 allocator_traits
<_Alloc
>::construct(__a
, _M_ptr(),
516 std::forward
<_Args
>(__args
)...); // might throw
519 ~_Sp_counted_ptr_inplace() noexcept
{ }
522 _M_dispose() noexcept
524 allocator_traits
<_Alloc
>::destroy(_M_impl
._M_alloc(), _M_ptr());
527 // Override because the allocator needs to know the dynamic type
529 _M_destroy() noexcept
531 typedef typename allocator_traits
<_Alloc
>::template
532 rebind_traits
<_Sp_counted_ptr_inplace
> _Alloc_traits
;
533 typename
_Alloc_traits::allocator_type
__a(_M_impl
._M_alloc());
534 _Alloc_traits::destroy(__a
, this);
535 _Alloc_traits::deallocate(__a
, this, 1);
538 // Sneaky trick so __shared_ptr can get the managed pointer
540 _M_get_deleter(const std::type_info
& __ti
) noexcept
543 if (__ti
== typeid(_Sp_make_shared_tag
))
544 return const_cast<typename remove_cv
<_Tp
>::type
*>(_M_ptr());
550 _Tp
* _M_ptr() noexcept
{ return _M_impl
._M_storage
._M_ptr(); }
556 template<_Lock_policy _Lp
>
560 constexpr __shared_count() noexcept
: _M_pi(0)
563 template<typename _Ptr
>
565 __shared_count(_Ptr __p
) : _M_pi(0)
569 _M_pi
= new _Sp_counted_ptr
<_Ptr
, _Lp
>(__p
);
574 __throw_exception_again
;
578 template<typename _Ptr
, typename _Deleter
>
579 __shared_count(_Ptr __p
, _Deleter __d
)
580 : __shared_count(__p
, std::move(__d
), allocator
<void>())
583 template<typename _Ptr
, typename _Deleter
, typename _Alloc
>
584 __shared_count(_Ptr __p
, _Deleter __d
, _Alloc __a
) : _M_pi(0)
586 typedef _Sp_counted_deleter
<_Ptr
, _Deleter
, _Alloc
, _Lp
> _Sp_cd_type
;
587 typedef typename allocator_traits
<_Alloc
>::template
588 rebind_traits
<_Sp_cd_type
> _Alloc_traits
;
589 typename
_Alloc_traits::allocator_type
__a2(__a
);
590 _Sp_cd_type
* __mem
= 0;
593 __mem
= _Alloc_traits::allocate(__a2
, 1);
594 _Alloc_traits::construct(__a2
, __mem
,
595 __p
, std::move(__d
), std::move(__a
));
600 __d(__p
); // Call _Deleter on __p.
602 _Alloc_traits::deallocate(__a2
, __mem
, 1);
603 __throw_exception_again
;
607 template<typename _Tp
, typename _Alloc
, typename
... _Args
>
608 __shared_count(_Sp_make_shared_tag
, _Tp
*, const _Alloc
& __a
,
612 typedef _Sp_counted_ptr_inplace
<_Tp
, _Alloc
, _Lp
> _Sp_cp_type
;
613 typedef typename allocator_traits
<_Alloc
>::template
614 rebind_traits
<_Sp_cp_type
> _Alloc_traits
;
615 typename
_Alloc_traits::allocator_type
__a2(__a
);
616 _Sp_cp_type
* __mem
= _Alloc_traits::allocate(__a2
, 1);
619 _Alloc_traits::construct(__a2
, __mem
, std::move(__a
),
620 std::forward
<_Args
>(__args
)...);
625 _Alloc_traits::deallocate(__a2
, __mem
, 1);
626 __throw_exception_again
;
630 #if _GLIBCXX_USE_DEPRECATED
631 // Special case for auto_ptr<_Tp> to provide the strong guarantee.
632 template<typename _Tp
>
634 __shared_count(std::auto_ptr
<_Tp
>&& __r
);
637 // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee.
638 template<typename _Tp
, typename _Del
>
640 __shared_count(std::unique_ptr
<_Tp
, _Del
>&& __r
) : _M_pi(0)
642 using _Ptr
= typename unique_ptr
<_Tp
, _Del
>::pointer
;
643 using _Del2
= typename conditional
<is_reference
<_Del
>::value
,
644 reference_wrapper
<typename remove_reference
<_Del
>::type
>,
647 = _Sp_counted_deleter
<_Ptr
, _Del2
, allocator
<void>, _Lp
>;
648 using _Alloc
= allocator
<_Sp_cd_type
>;
649 using _Alloc_traits
= allocator_traits
<_Alloc
>;
651 _Sp_cd_type
* __mem
= _Alloc_traits::allocate(__a
, 1);
652 _Alloc_traits::construct(__a
, __mem
, __r
.release(),
653 __r
.get_deleter()); // non-throwing
657 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
658 explicit __shared_count(const __weak_count
<_Lp
>& __r
);
660 // Does not throw if __r._M_get_use_count() == 0, caller must check.
661 explicit __shared_count(const __weak_count
<_Lp
>& __r
, std::nothrow_t
);
663 ~__shared_count() noexcept
665 if (_M_pi
!= nullptr)
669 __shared_count(const __shared_count
& __r
) noexcept
673 _M_pi
->_M_add_ref_copy();
677 operator=(const __shared_count
& __r
) noexcept
679 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
683 __tmp
->_M_add_ref_copy();
692 _M_swap(__shared_count
& __r
) noexcept
694 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
700 _M_get_use_count() const noexcept
701 { return _M_pi
!= 0 ? _M_pi
->_M_get_use_count() : 0; }
704 _M_unique() const noexcept
705 { return this->_M_get_use_count() == 1; }
708 _M_get_deleter(const std::type_info
& __ti
) const noexcept
709 { return _M_pi
? _M_pi
->_M_get_deleter(__ti
) : nullptr; }
712 _M_less(const __shared_count
& __rhs
) const noexcept
713 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
716 _M_less(const __weak_count
<_Lp
>& __rhs
) const noexcept
717 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
719 // Friend function injected into enclosing namespace and found by ADL
721 operator==(const __shared_count
& __a
, const __shared_count
& __b
) noexcept
722 { return __a
._M_pi
== __b
._M_pi
; }
725 friend class __weak_count
<_Lp
>;
727 _Sp_counted_base
<_Lp
>* _M_pi
;
731 template<_Lock_policy _Lp
>
735 constexpr __weak_count() noexcept
: _M_pi(0)
738 __weak_count(const __shared_count
<_Lp
>& __r
) noexcept
742 _M_pi
->_M_weak_add_ref();
745 __weak_count(const __weak_count
<_Lp
>& __r
) noexcept
749 _M_pi
->_M_weak_add_ref();
752 ~__weak_count() noexcept
755 _M_pi
->_M_weak_release();
759 operator=(const __shared_count
<_Lp
>& __r
) noexcept
761 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
763 __tmp
->_M_weak_add_ref();
765 _M_pi
->_M_weak_release();
771 operator=(const __weak_count
<_Lp
>& __r
) noexcept
773 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
775 __tmp
->_M_weak_add_ref();
777 _M_pi
->_M_weak_release();
783 _M_swap(__weak_count
<_Lp
>& __r
) noexcept
785 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
791 _M_get_use_count() const noexcept
792 { return _M_pi
!= 0 ? _M_pi
->_M_get_use_count() : 0; }
795 _M_less(const __weak_count
& __rhs
) const noexcept
796 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
799 _M_less(const __shared_count
<_Lp
>& __rhs
) const noexcept
800 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
802 // Friend function injected into enclosing namespace and found by ADL
804 operator==(const __weak_count
& __a
, const __weak_count
& __b
) noexcept
805 { return __a
._M_pi
== __b
._M_pi
; }
808 friend class __shared_count
<_Lp
>;
810 _Sp_counted_base
<_Lp
>* _M_pi
;
813 // Now that __weak_count is defined we can define this constructor:
814 template<_Lock_policy _Lp
>
816 __shared_count
<_Lp
>::__shared_count(const __weak_count
<_Lp
>& __r
)
819 if (_M_pi
!= nullptr)
820 _M_pi
->_M_add_ref_lock();
822 __throw_bad_weak_ptr();
825 // Now that __weak_count is defined we can define this constructor:
826 template<_Lock_policy _Lp
>
828 __shared_count
<_Lp
>::
829 __shared_count(const __weak_count
<_Lp
>& __r
, std::nothrow_t
)
832 if (_M_pi
!= nullptr)
833 if (!_M_pi
->_M_add_ref_lock_nothrow())
837 // Support for enable_shared_from_this.
839 // Friend of __enable_shared_from_this.
840 template<_Lock_policy _Lp
, typename _Tp1
, typename _Tp2
>
842 __enable_shared_from_this_helper(const __shared_count
<_Lp
>&,
843 const __enable_shared_from_this
<_Tp1
,
844 _Lp
>*, const _Tp2
*) noexcept
;
846 // Friend of enable_shared_from_this.
847 template<typename _Tp1
, typename _Tp2
>
849 __enable_shared_from_this_helper(const __shared_count
<>&,
850 const enable_shared_from_this
<_Tp1
>*,
851 const _Tp2
*) noexcept
;
853 template<_Lock_policy _Lp
>
855 __enable_shared_from_this_helper(const __shared_count
<_Lp
>&, ...) noexcept
859 template<typename _Tp
, _Lock_policy _Lp
>
863 typedef _Tp element_type
;
865 constexpr __shared_ptr() noexcept
866 : _M_ptr(0), _M_refcount()
869 template<typename _Tp1
>
870 explicit __shared_ptr(_Tp1
* __p
)
871 : _M_ptr(__p
), _M_refcount(__p
)
873 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
874 static_assert( !is_void
<_Tp
>::value
, "incomplete type" );
875 static_assert( sizeof(_Tp1
) > 0, "incomplete type" );
876 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
879 template<typename _Tp1
, typename _Deleter
>
880 __shared_ptr(_Tp1
* __p
, _Deleter __d
)
881 : _M_ptr(__p
), _M_refcount(__p
, __d
)
883 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
884 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
885 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
888 template<typename _Tp1
, typename _Deleter
, typename _Alloc
>
889 __shared_ptr(_Tp1
* __p
, _Deleter __d
, _Alloc __a
)
890 : _M_ptr(__p
), _M_refcount(__p
, __d
, std::move(__a
))
892 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
893 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
894 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
897 template<typename _Deleter
>
898 __shared_ptr(nullptr_t __p
, _Deleter __d
)
899 : _M_ptr(0), _M_refcount(__p
, __d
)
902 template<typename _Deleter
, typename _Alloc
>
903 __shared_ptr(nullptr_t __p
, _Deleter __d
, _Alloc __a
)
904 : _M_ptr(0), _M_refcount(__p
, __d
, std::move(__a
))
907 template<typename _Tp1
>
908 __shared_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
, _Tp
* __p
) noexcept
909 : _M_ptr(__p
), _M_refcount(__r
._M_refcount
) // never throws
912 __shared_ptr(const __shared_ptr
&) noexcept
= default;
913 __shared_ptr
& operator=(const __shared_ptr
&) noexcept
= default;
914 ~__shared_ptr() = default;
916 template<typename _Tp1
, typename
= typename
917 std::enable_if
<std::is_convertible
<_Tp1
*, _Tp
*>::value
>::type
>
918 __shared_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
) noexcept
919 : _M_ptr(__r
._M_ptr
), _M_refcount(__r
._M_refcount
)
922 __shared_ptr(__shared_ptr
&& __r
) noexcept
923 : _M_ptr(__r
._M_ptr
), _M_refcount()
925 _M_refcount
._M_swap(__r
._M_refcount
);
929 template<typename _Tp1
, typename
= typename
930 std::enable_if
<std::is_convertible
<_Tp1
*, _Tp
*>::value
>::type
>
931 __shared_ptr(__shared_ptr
<_Tp1
, _Lp
>&& __r
) noexcept
932 : _M_ptr(__r
._M_ptr
), _M_refcount()
934 _M_refcount
._M_swap(__r
._M_refcount
);
938 template<typename _Tp1
>
939 explicit __shared_ptr(const __weak_ptr
<_Tp1
, _Lp
>& __r
)
940 : _M_refcount(__r
._M_refcount
) // may throw
942 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
944 // It is now safe to copy __r._M_ptr, as
945 // _M_refcount(__r._M_refcount) did not throw.
949 // If an exception is thrown this constructor has no effect.
950 template<typename _Tp1
, typename _Del
>
951 __shared_ptr(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
952 : _M_ptr(__r
.get()), _M_refcount()
954 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
955 auto __raw
= _S_raw_ptr(__r
.get());
956 _M_refcount
= __shared_count
<_Lp
>(std::move(__r
));
957 __enable_shared_from_this_helper(_M_refcount
, __raw
, __raw
);
960 #if _GLIBCXX_USE_DEPRECATED
961 // Postcondition: use_count() == 1 and __r.get() == 0
962 template<typename _Tp1
>
963 __shared_ptr(std::auto_ptr
<_Tp1
>&& __r
);
966 /* TODO: use delegating constructor */
967 constexpr __shared_ptr(nullptr_t
) noexcept
968 : _M_ptr(0), _M_refcount()
971 template<typename _Tp1
>
973 operator=(const __shared_ptr
<_Tp1
, _Lp
>& __r
) noexcept
976 _M_refcount
= __r
._M_refcount
; // __shared_count::op= doesn't throw
980 #if _GLIBCXX_USE_DEPRECATED
981 template<typename _Tp1
>
983 operator=(std::auto_ptr
<_Tp1
>&& __r
)
985 __shared_ptr(std::move(__r
)).swap(*this);
991 operator=(__shared_ptr
&& __r
) noexcept
993 __shared_ptr(std::move(__r
)).swap(*this);
999 operator=(__shared_ptr
<_Tp1
, _Lp
>&& __r
) noexcept
1001 __shared_ptr(std::move(__r
)).swap(*this);
1005 template<typename _Tp1
, typename _Del
>
1007 operator=(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
1009 __shared_ptr(std::move(__r
)).swap(*this);
1015 { __shared_ptr().swap(*this); }
1017 template<typename _Tp1
>
1019 reset(_Tp1
* __p
) // _Tp1 must be complete.
1021 // Catch self-reset errors.
1022 _GLIBCXX_DEBUG_ASSERT(__p
== 0 || __p
!= _M_ptr
);
1023 __shared_ptr(__p
).swap(*this);
1026 template<typename _Tp1
, typename _Deleter
>
1028 reset(_Tp1
* __p
, _Deleter __d
)
1029 { __shared_ptr(__p
, __d
).swap(*this); }
1031 template<typename _Tp1
, typename _Deleter
, typename _Alloc
>
1033 reset(_Tp1
* __p
, _Deleter __d
, _Alloc __a
)
1034 { __shared_ptr(__p
, __d
, std::move(__a
)).swap(*this); }
1036 // Allow class instantiation when _Tp is [cv-qual] void.
1037 typename
std::add_lvalue_reference
<_Tp
>::type
1038 operator*() const noexcept
1040 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
1045 operator->() const noexcept
1047 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
1052 get() const noexcept
1055 explicit operator bool() const // never throws
1056 { return _M_ptr
== 0 ? false : true; }
1059 unique() const noexcept
1060 { return _M_refcount
._M_unique(); }
1063 use_count() const noexcept
1064 { return _M_refcount
._M_get_use_count(); }
1067 swap(__shared_ptr
<_Tp
, _Lp
>& __other
) noexcept
1069 std::swap(_M_ptr
, __other
._M_ptr
);
1070 _M_refcount
._M_swap(__other
._M_refcount
);
1073 template<typename _Tp1
>
1075 owner_before(__shared_ptr
<_Tp1
, _Lp
> const& __rhs
) const
1076 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1078 template<typename _Tp1
>
1080 owner_before(__weak_ptr
<_Tp1
, _Lp
> const& __rhs
) const
1081 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1085 // This constructor is non-standard, it is used by allocate_shared.
1086 template<typename _Alloc
, typename
... _Args
>
1087 __shared_ptr(_Sp_make_shared_tag __tag
, const _Alloc
& __a
,
1089 : _M_ptr(), _M_refcount(__tag
, (_Tp
*)0, __a
,
1090 std::forward
<_Args
>(__args
)...)
1092 // _M_ptr needs to point to the newly constructed object.
1093 // This relies on _Sp_counted_ptr_inplace::_M_get_deleter.
1094 void* __p
= _M_refcount
._M_get_deleter(typeid(__tag
));
1095 _M_ptr
= static_cast<_Tp
*>(__p
);
1096 __enable_shared_from_this_helper(_M_refcount
, _M_ptr
, _M_ptr
);
1099 template<typename _Alloc
>
1102 void operator()(_Tp
* __ptr
)
1104 typedef allocator_traits
<_Alloc
> _Alloc_traits
;
1105 _Alloc_traits::destroy(_M_alloc
, __ptr
);
1106 _Alloc_traits::deallocate(_M_alloc
, __ptr
, 1);
1111 template<typename _Alloc
, typename
... _Args
>
1112 __shared_ptr(_Sp_make_shared_tag __tag
, const _Alloc
& __a
,
1114 : _M_ptr(), _M_refcount()
1116 typedef typename
_Alloc::template rebind
<_Tp
>::other _Alloc2
;
1117 _Deleter
<_Alloc2
> __del
= { _Alloc2(__a
) };
1118 typedef allocator_traits
<_Alloc2
> __traits
;
1119 _M_ptr
= __traits::allocate(__del
._M_alloc
, 1);
1122 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1123 // 2070. allocate_shared should use allocator_traits<A>::construct
1124 __traits::construct(__del
._M_alloc
, _M_ptr
,
1125 std::forward
<_Args
>(__args
)...);
1129 __traits::deallocate(__del
._M_alloc
, _M_ptr
, 1);
1130 __throw_exception_again
;
1132 __shared_count
<_Lp
> __count(_M_ptr
, __del
, __del
._M_alloc
);
1133 _M_refcount
._M_swap(__count
);
1134 __enable_shared_from_this_helper(_M_refcount
, _M_ptr
, _M_ptr
);
1138 template<typename _Tp1
, _Lock_policy _Lp1
, typename _Alloc
,
1140 friend __shared_ptr
<_Tp1
, _Lp1
>
1141 __allocate_shared(const _Alloc
& __a
, _Args
&&... __args
);
1143 // This constructor is used by __weak_ptr::lock() and
1144 // shared_ptr::shared_ptr(const weak_ptr&, std::nothrow_t).
1145 __shared_ptr(const __weak_ptr
<_Tp
, _Lp
>& __r
, std::nothrow_t
)
1146 : _M_refcount(__r
._M_refcount
, std::nothrow
)
1148 _M_ptr
= _M_refcount
._M_get_use_count() ? __r
._M_ptr
: nullptr;
1151 friend class __weak_ptr
<_Tp
, _Lp
>;
1155 _M_get_deleter(const std::type_info
& __ti
) const noexcept
1156 { return _M_refcount
._M_get_deleter(__ti
); }
1158 template<typename _Tp1
>
1160 _S_raw_ptr(_Tp1
* __ptr
)
1163 template<typename _Tp1
>
1165 _S_raw_ptr(_Tp1 __ptr
) -> decltype(std::__addressof(*__ptr
))
1166 { return std::__addressof(*__ptr
); }
1168 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __shared_ptr
;
1169 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __weak_ptr
;
1171 template<typename _Del
, typename _Tp1
, _Lock_policy _Lp1
>
1172 friend _Del
* get_deleter(const __shared_ptr
<_Tp1
, _Lp1
>&) noexcept
;
1174 _Tp
* _M_ptr
; // Contained pointer.
1175 __shared_count
<_Lp
> _M_refcount
; // Reference counter.
1179 // 20.7.2.2.7 shared_ptr comparisons
1180 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1182 operator==(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1183 const __shared_ptr
<_Tp2
, _Lp
>& __b
) noexcept
1184 { return __a
.get() == __b
.get(); }
1186 template<typename _Tp
, _Lock_policy _Lp
>
1188 operator==(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
) noexcept
1191 template<typename _Tp
, _Lock_policy _Lp
>
1193 operator==(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __a
) noexcept
1196 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1198 operator!=(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1199 const __shared_ptr
<_Tp2
, _Lp
>& __b
) noexcept
1200 { return __a
.get() != __b
.get(); }
1202 template<typename _Tp
, _Lock_policy _Lp
>
1204 operator!=(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
) noexcept
1205 { return (bool)__a
; }
1207 template<typename _Tp
, _Lock_policy _Lp
>
1209 operator!=(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __a
) noexcept
1210 { return (bool)__a
; }
1212 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1214 operator<(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1215 const __shared_ptr
<_Tp2
, _Lp
>& __b
) noexcept
1217 typedef typename
std::common_type
<_Tp1
*, _Tp2
*>::type _CT
;
1218 return std::less
<_CT
>()(__a
.get(), __b
.get());
1221 template<typename _Tp
, _Lock_policy _Lp
>
1223 operator<(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
) noexcept
1224 { return std::less
<_Tp
*>()(__a
.get(), nullptr); }
1226 template<typename _Tp
, _Lock_policy _Lp
>
1228 operator<(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __a
) noexcept
1229 { return std::less
<_Tp
*>()(nullptr, __a
.get()); }
1231 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1233 operator<=(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1234 const __shared_ptr
<_Tp2
, _Lp
>& __b
) noexcept
1235 { return !(__b
< __a
); }
1237 template<typename _Tp
, _Lock_policy _Lp
>
1239 operator<=(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
) noexcept
1240 { return !(nullptr < __a
); }
1242 template<typename _Tp
, _Lock_policy _Lp
>
1244 operator<=(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __a
) noexcept
1245 { return !(__a
< nullptr); }
1247 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1249 operator>(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1250 const __shared_ptr
<_Tp2
, _Lp
>& __b
) noexcept
1251 { return (__b
< __a
); }
1253 template<typename _Tp
, _Lock_policy _Lp
>
1255 operator>(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
) noexcept
1256 { return std::less
<_Tp
*>()(nullptr, __a
.get()); }
1258 template<typename _Tp
, _Lock_policy _Lp
>
1260 operator>(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __a
) noexcept
1261 { return std::less
<_Tp
*>()(__a
.get(), nullptr); }
1263 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1265 operator>=(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1266 const __shared_ptr
<_Tp2
, _Lp
>& __b
) noexcept
1267 { return !(__a
< __b
); }
1269 template<typename _Tp
, _Lock_policy _Lp
>
1271 operator>=(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
) noexcept
1272 { return !(__a
< nullptr); }
1274 template<typename _Tp
, _Lock_policy _Lp
>
1276 operator>=(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __a
) noexcept
1277 { return !(nullptr < __a
); }
1279 template<typename _Sp
>
1280 struct _Sp_less
: public binary_function
<_Sp
, _Sp
, bool>
1283 operator()(const _Sp
& __lhs
, const _Sp
& __rhs
) const noexcept
1285 typedef typename
_Sp::element_type element_type
;
1286 return std::less
<element_type
*>()(__lhs
.get(), __rhs
.get());
1290 template<typename _Tp
, _Lock_policy _Lp
>
1291 struct less
<__shared_ptr
<_Tp
, _Lp
>>
1292 : public _Sp_less
<__shared_ptr
<_Tp
, _Lp
>>
1295 // 20.7.2.2.8 shared_ptr specialized algorithms.
1296 template<typename _Tp
, _Lock_policy _Lp
>
1298 swap(__shared_ptr
<_Tp
, _Lp
>& __a
, __shared_ptr
<_Tp
, _Lp
>& __b
) noexcept
1301 // 20.7.2.2.9 shared_ptr casts
1303 // The seemingly equivalent code:
1304 // shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))
1305 // will eventually result in undefined behaviour, attempting to
1306 // delete the same object twice.
1307 /// static_pointer_cast
1308 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
1309 inline __shared_ptr
<_Tp
, _Lp
>
1310 static_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
) noexcept
1311 { return __shared_ptr
<_Tp
, _Lp
>(__r
, static_cast<_Tp
*>(__r
.get())); }
1313 // The seemingly equivalent code:
1314 // shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))
1315 // will eventually result in undefined behaviour, attempting to
1316 // delete the same object twice.
1317 /// const_pointer_cast
1318 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
1319 inline __shared_ptr
<_Tp
, _Lp
>
1320 const_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
) noexcept
1321 { return __shared_ptr
<_Tp
, _Lp
>(__r
, const_cast<_Tp
*>(__r
.get())); }
1323 // The seemingly equivalent code:
1324 // shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))
1325 // will eventually result in undefined behaviour, attempting to
1326 // delete the same object twice.
1327 /// dynamic_pointer_cast
1328 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
1329 inline __shared_ptr
<_Tp
, _Lp
>
1330 dynamic_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
) noexcept
1332 if (_Tp
* __p
= dynamic_cast<_Tp
*>(__r
.get()))
1333 return __shared_ptr
<_Tp
, _Lp
>(__r
, __p
);
1334 return __shared_ptr
<_Tp
, _Lp
>();
1338 template<typename _Tp
, _Lock_policy _Lp
>
1342 typedef _Tp element_type
;
1344 constexpr __weak_ptr() noexcept
1345 : _M_ptr(0), _M_refcount()
1348 __weak_ptr(const __weak_ptr
&) noexcept
= default;
1349 __weak_ptr
& operator=(const __weak_ptr
&) noexcept
= default;
1350 ~__weak_ptr() = default;
1352 // The "obvious" converting constructor implementation:
1354 // template<typename _Tp1>
1355 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
1356 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
1359 // has a serious problem.
1361 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
1362 // conversion may require access to *__r._M_ptr (virtual inheritance).
1364 // It is not possible to avoid spurious access violations since
1365 // in multithreaded programs __r._M_ptr may be invalidated at any point.
1366 template<typename _Tp1
, typename
= typename
1367 std::enable_if
<std::is_convertible
<_Tp1
*, _Tp
*>::value
>::type
>
1368 __weak_ptr(const __weak_ptr
<_Tp1
, _Lp
>& __r
) noexcept
1369 : _M_refcount(__r
._M_refcount
)
1370 { _M_ptr
= __r
.lock().get(); }
1372 template<typename _Tp1
, typename
= typename
1373 std::enable_if
<std::is_convertible
<_Tp1
*, _Tp
*>::value
>::type
>
1374 __weak_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
) noexcept
1375 : _M_ptr(__r
._M_ptr
), _M_refcount(__r
._M_refcount
)
1378 template<typename _Tp1
>
1380 operator=(const __weak_ptr
<_Tp1
, _Lp
>& __r
) noexcept
1382 _M_ptr
= __r
.lock().get();
1383 _M_refcount
= __r
._M_refcount
;
1387 template<typename _Tp1
>
1389 operator=(const __shared_ptr
<_Tp1
, _Lp
>& __r
) noexcept
1391 _M_ptr
= __r
._M_ptr
;
1392 _M_refcount
= __r
._M_refcount
;
1396 __shared_ptr
<_Tp
, _Lp
>
1397 lock() const noexcept
1398 { return __shared_ptr
<element_type
, _Lp
>(*this, std::nothrow
); }
1401 use_count() const noexcept
1402 { return _M_refcount
._M_get_use_count(); }
1405 expired() const noexcept
1406 { return _M_refcount
._M_get_use_count() == 0; }
1408 template<typename _Tp1
>
1410 owner_before(const __shared_ptr
<_Tp1
, _Lp
>& __rhs
) const
1411 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1413 template<typename _Tp1
>
1415 owner_before(const __weak_ptr
<_Tp1
, _Lp
>& __rhs
) const
1416 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1420 { __weak_ptr().swap(*this); }
1423 swap(__weak_ptr
& __s
) noexcept
1425 std::swap(_M_ptr
, __s
._M_ptr
);
1426 _M_refcount
._M_swap(__s
._M_refcount
);
1430 // Used by __enable_shared_from_this.
1432 _M_assign(_Tp
* __ptr
, const __shared_count
<_Lp
>& __refcount
) noexcept
1435 _M_refcount
= __refcount
;
1438 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __shared_ptr
;
1439 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __weak_ptr
;
1440 friend class __enable_shared_from_this
<_Tp
, _Lp
>;
1441 friend class enable_shared_from_this
<_Tp
>;
1443 _Tp
* _M_ptr
; // Contained pointer.
1444 __weak_count
<_Lp
> _M_refcount
; // Reference counter.
1447 // 20.7.2.3.6 weak_ptr specialized algorithms.
1448 template<typename _Tp
, _Lock_policy _Lp
>
1450 swap(__weak_ptr
<_Tp
, _Lp
>& __a
, __weak_ptr
<_Tp
, _Lp
>& __b
) noexcept
1453 template<typename _Tp
, typename _Tp1
>
1454 struct _Sp_owner_less
: public binary_function
<_Tp
, _Tp
, bool>
1457 operator()(const _Tp
& __lhs
, const _Tp
& __rhs
) const
1458 { return __lhs
.owner_before(__rhs
); }
1461 operator()(const _Tp
& __lhs
, const _Tp1
& __rhs
) const
1462 { return __lhs
.owner_before(__rhs
); }
1465 operator()(const _Tp1
& __lhs
, const _Tp
& __rhs
) const
1466 { return __lhs
.owner_before(__rhs
); }
1469 template<typename _Tp
, _Lock_policy _Lp
>
1470 struct owner_less
<__shared_ptr
<_Tp
, _Lp
>>
1471 : public _Sp_owner_less
<__shared_ptr
<_Tp
, _Lp
>, __weak_ptr
<_Tp
, _Lp
>>
1474 template<typename _Tp
, _Lock_policy _Lp
>
1475 struct owner_less
<__weak_ptr
<_Tp
, _Lp
>>
1476 : public _Sp_owner_less
<__weak_ptr
<_Tp
, _Lp
>, __shared_ptr
<_Tp
, _Lp
>>
1480 template<typename _Tp
, _Lock_policy _Lp
>
1481 class __enable_shared_from_this
1484 constexpr __enable_shared_from_this() noexcept
{ }
1486 __enable_shared_from_this(const __enable_shared_from_this
&) noexcept
{ }
1488 __enable_shared_from_this
&
1489 operator=(const __enable_shared_from_this
&) noexcept
1492 ~__enable_shared_from_this() { }
1495 __shared_ptr
<_Tp
, _Lp
>
1497 { return __shared_ptr
<_Tp
, _Lp
>(this->_M_weak_this
); }
1499 __shared_ptr
<const _Tp
, _Lp
>
1500 shared_from_this() const
1501 { return __shared_ptr
<const _Tp
, _Lp
>(this->_M_weak_this
); }
1504 template<typename _Tp1
>
1506 _M_weak_assign(_Tp1
* __p
, const __shared_count
<_Lp
>& __n
) const noexcept
1507 { _M_weak_this
._M_assign(__p
, __n
); }
1509 template<typename _Tp1
>
1511 __enable_shared_from_this_helper(const __shared_count
<_Lp
>& __pn
,
1512 const __enable_shared_from_this
* __pe
,
1513 const _Tp1
* __px
) noexcept
1516 __pe
->_M_weak_assign(const_cast<_Tp1
*>(__px
), __pn
);
1519 mutable __weak_ptr
<_Tp
, _Lp
> _M_weak_this
;
1523 template<typename _Tp
, _Lock_policy _Lp
, typename _Alloc
, typename
... _Args
>
1524 inline __shared_ptr
<_Tp
, _Lp
>
1525 __allocate_shared(const _Alloc
& __a
, _Args
&&... __args
)
1527 return __shared_ptr
<_Tp
, _Lp
>(_Sp_make_shared_tag(), __a
,
1528 std::forward
<_Args
>(__args
)...);
1531 template<typename _Tp
, _Lock_policy _Lp
, typename
... _Args
>
1532 inline __shared_ptr
<_Tp
, _Lp
>
1533 __make_shared(_Args
&&... __args
)
1535 typedef typename
std::remove_const
<_Tp
>::type _Tp_nc
;
1536 return std::__allocate_shared
<_Tp
, _Lp
>(std::allocator
<_Tp_nc
>(),
1537 std::forward
<_Args
>(__args
)...);
1540 /// std::hash specialization for __shared_ptr.
1541 template<typename _Tp
, _Lock_policy _Lp
>
1542 struct hash
<__shared_ptr
<_Tp
, _Lp
>>
1543 : public __hash_base
<size_t, __shared_ptr
<_Tp
, _Lp
>>
1546 operator()(const __shared_ptr
<_Tp
, _Lp
>& __s
) const noexcept
1547 { return std::hash
<_Tp
*>()(__s
.get()); }
1550 _GLIBCXX_END_NAMESPACE_VERSION
1553 #endif // _SHARED_PTR_BASE_H