1 // shared_ptr and weak_ptr implementation details -*- C++ -*-
3 // Copyright (C) 2007-2017 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
55 #include <bits/allocated_ptr.h>
56 #include <bits/refwrap.h>
57 #include <bits/stl_function.h>
58 #include <ext/aligned_buffer.h>
60 namespace std
_GLIBCXX_VISIBILITY(default)
66 _GLIBCXX_BEGIN_NAMESPACE_VERSION
68 #if _GLIBCXX_USE_DEPRECATED
69 #pragma GCC diagnostic push
70 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
71 template<typename
> class auto_ptr
;
72 #pragma GCC diagnostic pop
76 * @brief Exception possibly thrown by @c shared_ptr.
79 class bad_weak_ptr
: public std::exception
82 virtual char const* what() const noexcept
;
84 virtual ~bad_weak_ptr() noexcept
;
87 // Substitute for bad_weak_ptr object in the case of -fno-exceptions.
89 __throw_bad_weak_ptr()
90 { _GLIBCXX_THROW_OR_ABORT(bad_weak_ptr()); }
92 using __gnu_cxx::_Lock_policy
;
93 using __gnu_cxx::__default_lock_policy
;
94 using __gnu_cxx::_S_single
;
95 using __gnu_cxx::_S_mutex
;
96 using __gnu_cxx::_S_atomic
;
98 // Empty helper class except when the template argument is _S_mutex.
99 template<_Lock_policy _Lp
>
103 // The atomic policy uses fully-fenced builtins, single doesn't care.
104 enum { _S_need_barriers
= 0 };
108 class _Mutex_base
<_S_mutex
>
109 : public __gnu_cxx::__mutex
112 // This policy is used when atomic builtins are not available.
113 // The replacement atomic operations might not have the necessary
115 enum { _S_need_barriers
= 1 };
118 template<_Lock_policy _Lp
= __default_lock_policy
>
119 class _Sp_counted_base
120 : public _Mutex_base
<_Lp
>
123 _Sp_counted_base() noexcept
124 : _M_use_count(1), _M_weak_count(1) { }
127 ~_Sp_counted_base() noexcept
130 // Called when _M_use_count drops to zero, to release the resources
133 _M_dispose() noexcept
= 0;
135 // Called when _M_weak_count drops to zero.
137 _M_destroy() noexcept
141 _M_get_deleter(const std::type_info
&) noexcept
= 0;
145 { __gnu_cxx::__atomic_add_dispatch(&_M_use_count
, 1); }
151 _M_add_ref_lock_nothrow();
154 _M_release() noexcept
156 // Be race-detector-friendly. For more info see bits/c++config.
157 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count
);
158 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count
, -1) == 1)
160 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count
);
162 // There must be a memory barrier between dispose() and destroy()
163 // to ensure that the effects of dispose() are observed in the
164 // thread that runs destroy().
165 // See http://gcc.gnu.org/ml/libstdc++/2005-11/msg00136.html
166 if (_Mutex_base
<_Lp
>::_S_need_barriers
)
168 __atomic_thread_fence (__ATOMIC_ACQ_REL
);
171 // Be race-detector-friendly. For more info see bits/c++config.
172 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count
);
173 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count
,
176 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count
);
183 _M_weak_add_ref() noexcept
184 { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count
, 1); }
187 _M_weak_release() noexcept
189 // Be race-detector-friendly. For more info see bits/c++config.
190 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count
);
191 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count
, -1) == 1)
193 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count
);
194 if (_Mutex_base
<_Lp
>::_S_need_barriers
)
197 // destroy() must observe results of dispose()
198 __atomic_thread_fence (__ATOMIC_ACQ_REL
);
205 _M_get_use_count() const noexcept
207 // No memory barrier is used here so there is no synchronization
208 // with other threads.
209 return __atomic_load_n(&_M_use_count
, __ATOMIC_RELAXED
);
213 _Sp_counted_base(_Sp_counted_base
const&) = delete;
214 _Sp_counted_base
& operator=(_Sp_counted_base
const&) = delete;
216 _Atomic_word _M_use_count
; // #shared
217 _Atomic_word _M_weak_count
; // #weak + (#shared != 0)
222 _Sp_counted_base
<_S_single
>::
225 if (_M_use_count
== 0)
226 __throw_bad_weak_ptr();
232 _Sp_counted_base
<_S_mutex
>::
235 __gnu_cxx::__scoped_lock
sentry(*this);
236 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count
, 1) == 0)
239 __throw_bad_weak_ptr();
245 _Sp_counted_base
<_S_atomic
>::
248 // Perform lock-free add-if-not-zero operation.
249 _Atomic_word __count
= _M_get_use_count();
253 __throw_bad_weak_ptr();
254 // Replace the current counter value with the old value + 1, as
255 // long as it's not changed meanwhile.
257 while (!__atomic_compare_exchange_n(&_M_use_count
, &__count
, __count
+ 1,
258 true, __ATOMIC_ACQ_REL
,
264 _Sp_counted_base
<_S_single
>::
265 _M_add_ref_lock_nothrow()
267 if (_M_use_count
== 0)
275 _Sp_counted_base
<_S_mutex
>::
276 _M_add_ref_lock_nothrow()
278 __gnu_cxx::__scoped_lock
sentry(*this);
279 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count
, 1) == 0)
289 _Sp_counted_base
<_S_atomic
>::
290 _M_add_ref_lock_nothrow()
292 // Perform lock-free add-if-not-zero operation.
293 _Atomic_word __count
= _M_get_use_count();
298 // Replace the current counter value with the old value + 1, as
299 // long as it's not changed meanwhile.
301 while (!__atomic_compare_exchange_n(&_M_use_count
, &__count
, __count
+ 1,
302 true, __ATOMIC_ACQ_REL
,
309 _Sp_counted_base
<_S_single
>::_M_add_ref_copy()
314 _Sp_counted_base
<_S_single
>::_M_release() noexcept
316 if (--_M_use_count
== 0)
319 if (--_M_weak_count
== 0)
326 _Sp_counted_base
<_S_single
>::_M_weak_add_ref() noexcept
331 _Sp_counted_base
<_S_single
>::_M_weak_release() noexcept
333 if (--_M_weak_count
== 0)
339 _Sp_counted_base
<_S_single
>::_M_get_use_count() const noexcept
340 { return _M_use_count
; }
343 // Forward declarations.
344 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
347 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
350 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
351 class __enable_shared_from_this
;
353 template<typename _Tp
>
356 template<typename _Tp
>
359 template<typename _Tp
>
362 template<typename _Tp
>
363 class enable_shared_from_this
;
365 template<_Lock_policy _Lp
= __default_lock_policy
>
368 template<_Lock_policy _Lp
= __default_lock_policy
>
369 class __shared_count
;
372 // Counted ptr with no deleter or allocator support
373 template<typename _Ptr
, _Lock_policy _Lp
>
374 class _Sp_counted_ptr final
: public _Sp_counted_base
<_Lp
>
378 _Sp_counted_ptr(_Ptr __p
) noexcept
382 _M_dispose() noexcept
386 _M_destroy() noexcept
390 _M_get_deleter(const std::type_info
&) noexcept
393 _Sp_counted_ptr(const _Sp_counted_ptr
&) = delete;
394 _Sp_counted_ptr
& operator=(const _Sp_counted_ptr
&) = delete;
402 _Sp_counted_ptr
<nullptr_t
, _S_single
>::_M_dispose() noexcept
{ }
406 _Sp_counted_ptr
<nullptr_t
, _S_mutex
>::_M_dispose() noexcept
{ }
410 _Sp_counted_ptr
<nullptr_t
, _S_atomic
>::_M_dispose() noexcept
{ }
412 template<int _Nm
, typename _Tp
,
413 bool __use_ebo
= !__is_final(_Tp
) && __is_empty(_Tp
)>
414 struct _Sp_ebo_helper
;
416 /// Specialization using EBO.
417 template<int _Nm
, typename _Tp
>
418 struct _Sp_ebo_helper
<_Nm
, _Tp
, true> : private _Tp
420 explicit _Sp_ebo_helper(const _Tp
& __tp
) : _Tp(__tp
) { }
421 explicit _Sp_ebo_helper(_Tp
&& __tp
) : _Tp(std::move(__tp
)) { }
424 _S_get(_Sp_ebo_helper
& __eboh
) { return static_cast<_Tp
&>(__eboh
); }
427 /// Specialization not using EBO.
428 template<int _Nm
, typename _Tp
>
429 struct _Sp_ebo_helper
<_Nm
, _Tp
, false>
431 explicit _Sp_ebo_helper(const _Tp
& __tp
) : _M_tp(__tp
) { }
432 explicit _Sp_ebo_helper(_Tp
&& __tp
) : _M_tp(std::move(__tp
)) { }
435 _S_get(_Sp_ebo_helper
& __eboh
)
436 { return __eboh
._M_tp
; }
442 // Support for custom deleter and/or allocator
443 template<typename _Ptr
, typename _Deleter
, typename _Alloc
, _Lock_policy _Lp
>
444 class _Sp_counted_deleter final
: public _Sp_counted_base
<_Lp
>
446 class _Impl
: _Sp_ebo_helper
<0, _Deleter
>, _Sp_ebo_helper
<1, _Alloc
>
448 typedef _Sp_ebo_helper
<0, _Deleter
> _Del_base
;
449 typedef _Sp_ebo_helper
<1, _Alloc
> _Alloc_base
;
452 _Impl(_Ptr __p
, _Deleter __d
, const _Alloc
& __a
) noexcept
453 : _M_ptr(__p
), _Del_base(std::move(__d
)), _Alloc_base(__a
)
456 _Deleter
& _M_del() noexcept
{ return _Del_base::_S_get(*this); }
457 _Alloc
& _M_alloc() noexcept
{ return _Alloc_base::_S_get(*this); }
463 using __allocator_type
= __alloc_rebind
<_Alloc
, _Sp_counted_deleter
>;
465 // __d(__p) must not throw.
466 _Sp_counted_deleter(_Ptr __p
, _Deleter __d
) noexcept
467 : _M_impl(__p
, std::move(__d
), _Alloc()) { }
469 // __d(__p) must not throw.
470 _Sp_counted_deleter(_Ptr __p
, _Deleter __d
, const _Alloc
& __a
) noexcept
471 : _M_impl(__p
, std::move(__d
), __a
) { }
473 ~_Sp_counted_deleter() noexcept
{ }
476 _M_dispose() noexcept
477 { _M_impl
._M_del()(_M_impl
._M_ptr
); }
480 _M_destroy() noexcept
482 __allocator_type
__a(_M_impl
._M_alloc());
483 __allocated_ptr
<__allocator_type
> __guard_ptr
{ __a
, this };
484 this->~_Sp_counted_deleter();
488 _M_get_deleter(const std::type_info
& __ti
) noexcept
491 // _GLIBCXX_RESOLVE_LIB_DEFECTS
492 // 2400. shared_ptr's get_deleter() should use addressof()
493 return __ti
== typeid(_Deleter
)
494 ? std::__addressof(_M_impl
._M_del())
505 // helpers for make_shared / allocate_shared
507 struct _Sp_make_shared_tag
511 template<typename _Tp
, _Lock_policy _Lp
>
512 friend class __shared_ptr
;
513 template<typename _Tp
, typename _Alloc
, _Lock_policy _Lp
>
514 friend class _Sp_counted_ptr_inplace
;
516 static const type_info
&
519 static constexpr _Sp_make_shared_tag __tag
;
520 return reinterpret_cast<const type_info
&>(__tag
);
525 template<typename _Tp
, typename _Alloc
, _Lock_policy _Lp
>
526 class _Sp_counted_ptr_inplace final
: public _Sp_counted_base
<_Lp
>
528 class _Impl
: _Sp_ebo_helper
<0, _Alloc
>
530 typedef _Sp_ebo_helper
<0, _Alloc
> _A_base
;
533 explicit _Impl(_Alloc __a
) noexcept
: _A_base(__a
) { }
535 _Alloc
& _M_alloc() noexcept
{ return _A_base::_S_get(*this); }
537 __gnu_cxx::__aligned_buffer
<_Tp
> _M_storage
;
541 using __allocator_type
= __alloc_rebind
<_Alloc
, _Sp_counted_ptr_inplace
>;
543 template<typename
... _Args
>
544 _Sp_counted_ptr_inplace(_Alloc __a
, _Args
&&... __args
)
547 // _GLIBCXX_RESOLVE_LIB_DEFECTS
548 // 2070. allocate_shared should use allocator_traits<A>::construct
549 allocator_traits
<_Alloc
>::construct(__a
, _M_ptr(),
550 std::forward
<_Args
>(__args
)...); // might throw
553 ~_Sp_counted_ptr_inplace() noexcept
{ }
556 _M_dispose() noexcept
558 allocator_traits
<_Alloc
>::destroy(_M_impl
._M_alloc(), _M_ptr());
561 // Override because the allocator needs to know the dynamic type
563 _M_destroy() noexcept
565 __allocator_type
__a(_M_impl
._M_alloc());
566 __allocated_ptr
<__allocator_type
> __guard_ptr
{ __a
, this };
567 this->~_Sp_counted_ptr_inplace();
570 // Sneaky trick so __shared_ptr can get the managed pointer
572 _M_get_deleter(const std::type_info
& __ti
) noexcept
575 if (__ti
== typeid(_Sp_make_shared_tag
))
577 if (&__ti
== &_Sp_make_shared_tag::_S_ti())
579 return const_cast<typename remove_cv
<_Tp
>::type
*>(_M_ptr());
584 _Tp
* _M_ptr() noexcept
{ return _M_impl
._M_storage
._M_ptr(); }
589 // The default deleter for shared_ptr<T[]> and shared_ptr<T[N]>.
590 struct __sp_array_delete
592 template<typename _Yp
>
593 void operator()(_Yp
* __p
) const { delete[] __p
; }
596 template<_Lock_policy _Lp
>
600 constexpr __shared_count() noexcept
: _M_pi(0)
603 template<typename _Ptr
>
605 __shared_count(_Ptr __p
) : _M_pi(0)
609 _M_pi
= new _Sp_counted_ptr
<_Ptr
, _Lp
>(__p
);
614 __throw_exception_again
;
618 template<typename _Ptr
>
619 __shared_count(_Ptr __p
, /* is_array = */ false_type
)
620 : __shared_count(__p
)
623 template<typename _Ptr
>
624 __shared_count(_Ptr __p
, /* is_array = */ true_type
)
625 : __shared_count(__p
, __sp_array_delete
{}, allocator
<void>())
628 template<typename _Ptr
, typename _Deleter
>
629 __shared_count(_Ptr __p
, _Deleter __d
)
630 : __shared_count(__p
, std::move(__d
), allocator
<void>())
633 template<typename _Ptr
, typename _Deleter
, typename _Alloc
>
634 __shared_count(_Ptr __p
, _Deleter __d
, _Alloc __a
) : _M_pi(0)
636 typedef _Sp_counted_deleter
<_Ptr
, _Deleter
, _Alloc
, _Lp
> _Sp_cd_type
;
639 typename
_Sp_cd_type::__allocator_type
__a2(__a
);
640 auto __guard
= std::__allocate_guarded(__a2
);
641 _Sp_cd_type
* __mem
= __guard
.get();
642 ::new (__mem
) _Sp_cd_type(__p
, std::move(__d
), std::move(__a
));
648 __d(__p
); // Call _Deleter on __p.
649 __throw_exception_again
;
653 template<typename _Tp
, typename _Alloc
, typename
... _Args
>
654 __shared_count(_Sp_make_shared_tag
, _Tp
*, const _Alloc
& __a
,
658 typedef _Sp_counted_ptr_inplace
<_Tp
, _Alloc
, _Lp
> _Sp_cp_type
;
659 typename
_Sp_cp_type::__allocator_type
__a2(__a
);
660 auto __guard
= std::__allocate_guarded(__a2
);
661 _Sp_cp_type
* __mem
= __guard
.get();
662 ::new (__mem
) _Sp_cp_type(std::move(__a
),
663 std::forward
<_Args
>(__args
)...);
668 #if _GLIBCXX_USE_DEPRECATED
669 #pragma GCC diagnostic push
670 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
671 // Special case for auto_ptr<_Tp> to provide the strong guarantee.
672 template<typename _Tp
>
674 __shared_count(std::auto_ptr
<_Tp
>&& __r
);
675 #pragma GCC diagnostic pop
678 // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee.
679 template<typename _Tp
, typename _Del
>
681 __shared_count(std::unique_ptr
<_Tp
, _Del
>&& __r
) : _M_pi(0)
683 // _GLIBCXX_RESOLVE_LIB_DEFECTS
684 // 2415. Inconsistency between unique_ptr and shared_ptr
685 if (__r
.get() == nullptr)
688 using _Ptr
= typename unique_ptr
<_Tp
, _Del
>::pointer
;
689 using _Del2
= typename conditional
<is_reference
<_Del
>::value
,
690 reference_wrapper
<typename remove_reference
<_Del
>::type
>,
693 = _Sp_counted_deleter
<_Ptr
, _Del2
, allocator
<void>, _Lp
>;
694 using _Alloc
= allocator
<_Sp_cd_type
>;
695 using _Alloc_traits
= allocator_traits
<_Alloc
>;
697 _Sp_cd_type
* __mem
= _Alloc_traits::allocate(__a
, 1);
698 _Alloc_traits::construct(__a
, __mem
, __r
.release(),
699 __r
.get_deleter()); // non-throwing
703 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
704 explicit __shared_count(const __weak_count
<_Lp
>& __r
);
706 // Does not throw if __r._M_get_use_count() == 0, caller must check.
707 explicit __shared_count(const __weak_count
<_Lp
>& __r
, std::nothrow_t
);
709 ~__shared_count() noexcept
711 if (_M_pi
!= nullptr)
715 __shared_count(const __shared_count
& __r
) noexcept
719 _M_pi
->_M_add_ref_copy();
723 operator=(const __shared_count
& __r
) noexcept
725 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
729 __tmp
->_M_add_ref_copy();
738 _M_swap(__shared_count
& __r
) noexcept
740 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
746 _M_get_use_count() const noexcept
747 { return _M_pi
!= 0 ? _M_pi
->_M_get_use_count() : 0; }
750 _M_unique() const noexcept
751 { return this->_M_get_use_count() == 1; }
754 _M_get_deleter(const std::type_info
& __ti
) const noexcept
755 { return _M_pi
? _M_pi
->_M_get_deleter(__ti
) : nullptr; }
758 _M_less(const __shared_count
& __rhs
) const noexcept
759 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
762 _M_less(const __weak_count
<_Lp
>& __rhs
) const noexcept
763 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
765 // Friend function injected into enclosing namespace and found by ADL
767 operator==(const __shared_count
& __a
, const __shared_count
& __b
) noexcept
768 { return __a
._M_pi
== __b
._M_pi
; }
771 friend class __weak_count
<_Lp
>;
773 _Sp_counted_base
<_Lp
>* _M_pi
;
777 template<_Lock_policy _Lp
>
781 constexpr __weak_count() noexcept
: _M_pi(nullptr)
784 __weak_count(const __shared_count
<_Lp
>& __r
) noexcept
787 if (_M_pi
!= nullptr)
788 _M_pi
->_M_weak_add_ref();
791 __weak_count(const __weak_count
& __r
) noexcept
794 if (_M_pi
!= nullptr)
795 _M_pi
->_M_weak_add_ref();
798 __weak_count(__weak_count
&& __r
) noexcept
800 { __r
._M_pi
= nullptr; }
802 ~__weak_count() noexcept
804 if (_M_pi
!= nullptr)
805 _M_pi
->_M_weak_release();
809 operator=(const __shared_count
<_Lp
>& __r
) noexcept
811 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
812 if (__tmp
!= nullptr)
813 __tmp
->_M_weak_add_ref();
814 if (_M_pi
!= nullptr)
815 _M_pi
->_M_weak_release();
821 operator=(const __weak_count
& __r
) noexcept
823 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
824 if (__tmp
!= nullptr)
825 __tmp
->_M_weak_add_ref();
826 if (_M_pi
!= nullptr)
827 _M_pi
->_M_weak_release();
833 operator=(__weak_count
&& __r
) noexcept
835 if (_M_pi
!= nullptr)
836 _M_pi
->_M_weak_release();
843 _M_swap(__weak_count
& __r
) noexcept
845 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
851 _M_get_use_count() const noexcept
852 { return _M_pi
!= nullptr ? _M_pi
->_M_get_use_count() : 0; }
855 _M_less(const __weak_count
& __rhs
) const noexcept
856 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
859 _M_less(const __shared_count
<_Lp
>& __rhs
) const noexcept
860 { return std::less
<_Sp_counted_base
<_Lp
>*>()(this->_M_pi
, __rhs
._M_pi
); }
862 // Friend function injected into enclosing namespace and found by ADL
864 operator==(const __weak_count
& __a
, const __weak_count
& __b
) noexcept
865 { return __a
._M_pi
== __b
._M_pi
; }
868 friend class __shared_count
<_Lp
>;
870 _Sp_counted_base
<_Lp
>* _M_pi
;
873 // Now that __weak_count is defined we can define this constructor:
874 template<_Lock_policy _Lp
>
876 __shared_count
<_Lp
>::__shared_count(const __weak_count
<_Lp
>& __r
)
879 if (_M_pi
!= nullptr)
880 _M_pi
->_M_add_ref_lock();
882 __throw_bad_weak_ptr();
885 // Now that __weak_count is defined we can define this constructor:
886 template<_Lock_policy _Lp
>
888 __shared_count
<_Lp
>::
889 __shared_count(const __weak_count
<_Lp
>& __r
, std::nothrow_t
)
892 if (_M_pi
!= nullptr)
893 if (!_M_pi
->_M_add_ref_lock_nothrow())
897 #define __cpp_lib_shared_ptr_arrays 201603
899 // Helper traits for shared_ptr of array:
901 // A pointer type Y* is said to be compatible with a pointer type T* when
902 // either Y* is convertible to T* or Y is U[N] and T is U cv [].
903 template<typename _Yp_ptr
, typename _Tp_ptr
>
904 struct __sp_compatible_with
908 template<typename _Yp
, typename _Tp
>
909 struct __sp_compatible_with
<_Yp
*, _Tp
*>
910 : is_convertible
<_Yp
*, _Tp
*>::type
913 template<typename _Up
, size_t _Nm
>
914 struct __sp_compatible_with
<_Up(*)[_Nm
], _Up(*)[]>
918 template<typename _Up
, size_t _Nm
>
919 struct __sp_compatible_with
<_Up(*)[_Nm
], const _Up(*)[]>
923 template<typename _Up
, size_t _Nm
>
924 struct __sp_compatible_with
<_Up(*)[_Nm
], volatile _Up(*)[]>
928 template<typename _Up
, size_t _Nm
>
929 struct __sp_compatible_with
<_Up(*)[_Nm
], const volatile _Up(*)[]>
933 // Test conversion from Y(*)[N] to U(*)[N] without forming invalid type Y[N].
934 template<typename _Up
, size_t _Nm
, typename _Yp
, typename
= void>
935 struct __sp_is_constructible_arrN
939 template<typename _Up
, size_t _Nm
, typename _Yp
>
940 struct __sp_is_constructible_arrN
<_Up
, _Nm
, _Yp
, __void_t
<_Yp
[_Nm
]>>
941 : is_convertible
<_Yp(*)[_Nm
], _Up(*)[_Nm
]>::type
944 // Test conversion from Y(*)[] to U(*)[] without forming invalid type Y[].
945 template<typename _Up
, typename _Yp
, typename
= void>
946 struct __sp_is_constructible_arr
950 template<typename _Up
, typename _Yp
>
951 struct __sp_is_constructible_arr
<_Up
, _Yp
, __void_t
<_Yp
[]>>
952 : is_convertible
<_Yp(*)[], _Up(*)[]>::type
955 // Trait to check if shared_ptr<T> can be constructed from Y*.
956 template<typename _Tp
, typename _Yp
>
957 struct __sp_is_constructible
;
959 // When T is U[N], Y(*)[N] shall be convertible to T*;
960 template<typename _Up
, size_t _Nm
, typename _Yp
>
961 struct __sp_is_constructible
<_Up
[_Nm
], _Yp
>
962 : __sp_is_constructible_arrN
<_Up
, _Nm
, _Yp
>::type
965 // when T is U[], Y(*)[] shall be convertible to T*;
966 template<typename _Up
, typename _Yp
>
967 struct __sp_is_constructible
<_Up
[], _Yp
>
968 : __sp_is_constructible_arr
<_Up
, _Yp
>::type
971 // otherwise, Y* shall be convertible to T*.
972 template<typename _Tp
, typename _Yp
>
973 struct __sp_is_constructible
974 : is_convertible
<_Yp
*, _Tp
*>::type
978 // Define operator* and operator-> for shared_ptr<T>.
979 template<typename _Tp
, _Lock_policy _Lp
,
980 bool = is_array
<_Tp
>::value
, bool = is_void
<_Tp
>::value
>
981 class __shared_ptr_access
984 using element_type
= _Tp
;
987 operator*() const noexcept
989 __glibcxx_assert(_M_get() != nullptr);
994 operator->() const noexcept
996 _GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr);
1002 _M_get() const noexcept
1003 { return static_cast<const __shared_ptr
<_Tp
, _Lp
>*>(this)->get(); }
1006 // Define operator-> for shared_ptr<cv void>.
1007 template<typename _Tp
, _Lock_policy _Lp
>
1008 class __shared_ptr_access
<_Tp
, _Lp
, false, true>
1011 using element_type
= _Tp
;
1014 operator->() const noexcept
1016 auto __ptr
= static_cast<const __shared_ptr
<_Tp
, _Lp
>*>(this)->get();
1017 _GLIBCXX_DEBUG_PEDASSERT(__ptr
!= nullptr);
1022 // Define operator[] for shared_ptr<T[]> and shared_ptr<T[N]>.
1023 template<typename _Tp
, _Lock_policy _Lp
>
1024 class __shared_ptr_access
<_Tp
, _Lp
, true, false>
1027 using element_type
= typename remove_extent
<_Tp
>::type
;
1029 #if __cplusplus <= 201402L
1030 [[__deprecated__("shared_ptr<T[]>::operator* is absent from C++17")]]
1032 operator*() const noexcept
1034 __glibcxx_assert(_M_get() != nullptr);
1038 [[__deprecated__("shared_ptr<T[]>::operator-> is absent from C++17")]]
1040 operator->() const noexcept
1042 _GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr);
1048 operator[](ptrdiff_t __i
) const
1050 __glibcxx_assert(_M_get() != nullptr);
1051 __glibcxx_assert(!extent
<_Tp
>::value
|| __i
< extent
<_Tp
>::value
);
1052 return _M_get()[__i
];
1057 _M_get() const noexcept
1058 { return static_cast<const __shared_ptr
<_Tp
, _Lp
>*>(this)->get(); }
1061 template<typename _Tp
, _Lock_policy _Lp
>
1063 : public __shared_ptr_access
<_Tp
, _Lp
>
1066 using element_type
= typename remove_extent
<_Tp
>::type
;
1069 // Constraint for taking ownership of a pointer of type _Yp*:
1070 template<typename _Yp
>
1072 = typename enable_if
<__sp_is_constructible
<_Tp
, _Yp
>::value
>::type
;
1074 // Constraint for construction from shared_ptr and weak_ptr:
1075 template<typename _Yp
, typename _Res
= void>
1076 using _Compatible
= typename
1077 enable_if
<__sp_compatible_with
<_Yp
*, _Tp
*>::value
, _Res
>::type
;
1079 // Constraint for assignment from shared_ptr and weak_ptr:
1080 template<typename _Yp
>
1081 using _Assignable
= _Compatible
<_Yp
, __shared_ptr
&>;
1083 // Constraint for construction from unique_ptr:
1084 template<typename _Yp
, typename _Del
, typename _Res
= void,
1085 typename _Ptr
= typename unique_ptr
<_Yp
, _Del
>::pointer
>
1086 using _UniqCompatible
= typename enable_if
<__and_
<
1087 __sp_compatible_with
<_Yp
*, _Tp
*>, is_convertible
<_Ptr
, element_type
*>
1088 >::value
, _Res
>::type
;
1090 // Constraint for assignment from unique_ptr:
1091 template<typename _Yp
, typename _Del
>
1092 using _UniqAssignable
= _UniqCompatible
<_Yp
, _Del
, __shared_ptr
&>;
1096 #if __cplusplus > 201402L
1097 using weak_type
= __weak_ptr
<_Tp
, _Lp
>;
1100 constexpr __shared_ptr() noexcept
1101 : _M_ptr(0), _M_refcount()
1104 template<typename _Yp
, typename
= _SafeConv
<_Yp
>>
1106 __shared_ptr(_Yp
* __p
)
1107 : _M_ptr(__p
), _M_refcount(__p
, typename is_array
<_Tp
>::type())
1109 static_assert( !is_void
<_Yp
>::value
, "incomplete type" );
1110 static_assert( sizeof(_Yp
) > 0, "incomplete type" );
1111 _M_enable_shared_from_this_with(__p
);
1114 template<typename _Yp
, typename _Deleter
, typename
= _SafeConv
<_Yp
>>
1115 __shared_ptr(_Yp
* __p
, _Deleter __d
)
1116 : _M_ptr(__p
), _M_refcount(__p
, std::move(__d
))
1118 static_assert(__is_invocable
<_Deleter
&, _Yp
*&>::value
,
1119 "deleter expression d(p) is well-formed");
1120 _M_enable_shared_from_this_with(__p
);
1123 template<typename _Yp
, typename _Deleter
, typename _Alloc
,
1124 typename
= _SafeConv
<_Yp
>>
1125 __shared_ptr(_Yp
* __p
, _Deleter __d
, _Alloc __a
)
1126 : _M_ptr(__p
), _M_refcount(__p
, std::move(__d
), std::move(__a
))
1128 static_assert(__is_invocable
<_Deleter
&, _Yp
*&>::value
,
1129 "deleter expression d(p) is well-formed");
1130 _M_enable_shared_from_this_with(__p
);
1133 template<typename _Deleter
>
1134 __shared_ptr(nullptr_t __p
, _Deleter __d
)
1135 : _M_ptr(0), _M_refcount(__p
, std::move(__d
))
1138 template<typename _Deleter
, typename _Alloc
>
1139 __shared_ptr(nullptr_t __p
, _Deleter __d
, _Alloc __a
)
1140 : _M_ptr(0), _M_refcount(__p
, std::move(__d
), std::move(__a
))
1143 template<typename _Yp
>
1144 __shared_ptr(const __shared_ptr
<_Yp
, _Lp
>& __r
,
1145 element_type
* __p
) noexcept
1146 : _M_ptr(__p
), _M_refcount(__r
._M_refcount
) // never throws
1149 __shared_ptr(const __shared_ptr
&) noexcept
= default;
1150 __shared_ptr
& operator=(const __shared_ptr
&) noexcept
= default;
1151 ~__shared_ptr() = default;
1153 template<typename _Yp
, typename
= _Compatible
<_Yp
>>
1154 __shared_ptr(const __shared_ptr
<_Yp
, _Lp
>& __r
) noexcept
1155 : _M_ptr(__r
._M_ptr
), _M_refcount(__r
._M_refcount
)
1158 __shared_ptr(__shared_ptr
&& __r
) noexcept
1159 : _M_ptr(__r
._M_ptr
), _M_refcount()
1161 _M_refcount
._M_swap(__r
._M_refcount
);
1165 template<typename _Yp
, typename
= _Compatible
<_Yp
>>
1166 __shared_ptr(__shared_ptr
<_Yp
, _Lp
>&& __r
) noexcept
1167 : _M_ptr(__r
._M_ptr
), _M_refcount()
1169 _M_refcount
._M_swap(__r
._M_refcount
);
1173 template<typename _Yp
, typename
= _Compatible
<_Yp
>>
1174 explicit __shared_ptr(const __weak_ptr
<_Yp
, _Lp
>& __r
)
1175 : _M_refcount(__r
._M_refcount
) // may throw
1177 // It is now safe to copy __r._M_ptr, as
1178 // _M_refcount(__r._M_refcount) did not throw.
1179 _M_ptr
= __r
._M_ptr
;
1182 // If an exception is thrown this constructor has no effect.
1183 template<typename _Yp
, typename _Del
,
1184 typename
= _UniqCompatible
<_Yp
, _Del
>>
1185 __shared_ptr(unique_ptr
<_Yp
, _Del
>&& __r
)
1186 : _M_ptr(__r
.get()), _M_refcount()
1188 auto __raw
= __to_address(__r
.get());
1189 _M_refcount
= __shared_count
<_Lp
>(std::move(__r
));
1190 _M_enable_shared_from_this_with(__raw
);
1193 #if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED
1195 // If an exception is thrown this constructor has no effect.
1196 template<typename _Tp1
, typename _Del
,
1197 typename enable_if
<__and_
<
1198 __not_
<is_array
<_Tp
>>, is_array
<_Tp1
>,
1199 is_convertible
<typename unique_ptr
<_Tp1
, _Del
>::pointer
, _Tp
*>
1200 >::value
, bool>::type
= true>
1201 __shared_ptr(unique_ptr
<_Tp1
, _Del
>&& __r
, __sp_array_delete
)
1202 : _M_ptr(__r
.get()), _M_refcount()
1204 auto __raw
= __to_address(__r
.get());
1205 _M_refcount
= __shared_count
<_Lp
>(std::move(__r
));
1206 _M_enable_shared_from_this_with(__raw
);
1211 #if _GLIBCXX_USE_DEPRECATED
1212 #pragma GCC diagnostic push
1213 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1214 // Postcondition: use_count() == 1 and __r.get() == 0
1215 template<typename _Yp
, typename
= _Compatible
<_Yp
>>
1216 __shared_ptr(auto_ptr
<_Yp
>&& __r
);
1217 #pragma GCC diagnostic pop
1220 constexpr __shared_ptr(nullptr_t
) noexcept
: __shared_ptr() { }
1222 template<typename _Yp
>
1224 operator=(const __shared_ptr
<_Yp
, _Lp
>& __r
) noexcept
1226 _M_ptr
= __r
._M_ptr
;
1227 _M_refcount
= __r
._M_refcount
; // __shared_count::op= doesn't throw
1231 #if _GLIBCXX_USE_DEPRECATED
1232 #pragma GCC diagnostic push
1233 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1234 template<typename _Yp
>
1236 operator=(auto_ptr
<_Yp
>&& __r
)
1238 __shared_ptr(std::move(__r
)).swap(*this);
1241 #pragma GCC diagnostic pop
1245 operator=(__shared_ptr
&& __r
) noexcept
1247 __shared_ptr(std::move(__r
)).swap(*this);
1253 operator=(__shared_ptr
<_Yp
, _Lp
>&& __r
) noexcept
1255 __shared_ptr(std::move(__r
)).swap(*this);
1259 template<typename _Yp
, typename _Del
>
1260 _UniqAssignable
<_Yp
, _Del
>
1261 operator=(unique_ptr
<_Yp
, _Del
>&& __r
)
1263 __shared_ptr(std::move(__r
)).swap(*this);
1269 { __shared_ptr().swap(*this); }
1271 template<typename _Yp
>
1273 reset(_Yp
* __p
) // _Yp must be complete.
1275 // Catch self-reset errors.
1276 __glibcxx_assert(__p
== 0 || __p
!= _M_ptr
);
1277 __shared_ptr(__p
).swap(*this);
1280 template<typename _Yp
, typename _Deleter
>
1282 reset(_Yp
* __p
, _Deleter __d
)
1283 { __shared_ptr(__p
, std::move(__d
)).swap(*this); }
1285 template<typename _Yp
, typename _Deleter
, typename _Alloc
>
1287 reset(_Yp
* __p
, _Deleter __d
, _Alloc __a
)
1288 { __shared_ptr(__p
, std::move(__d
), std::move(__a
)).swap(*this); }
1291 get() const noexcept
1294 explicit operator bool() const // never throws
1295 { return _M_ptr
== 0 ? false : true; }
1298 unique() const noexcept
1299 { return _M_refcount
._M_unique(); }
1302 use_count() const noexcept
1303 { return _M_refcount
._M_get_use_count(); }
1306 swap(__shared_ptr
<_Tp
, _Lp
>& __other
) noexcept
1308 std::swap(_M_ptr
, __other
._M_ptr
);
1309 _M_refcount
._M_swap(__other
._M_refcount
);
1312 template<typename _Tp1
>
1314 owner_before(__shared_ptr
<_Tp1
, _Lp
> const& __rhs
) const noexcept
1315 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1317 template<typename _Tp1
>
1319 owner_before(__weak_ptr
<_Tp1
, _Lp
> const& __rhs
) const noexcept
1320 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1323 // This constructor is non-standard, it is used by allocate_shared.
1324 template<typename _Alloc
, typename
... _Args
>
1325 __shared_ptr(_Sp_make_shared_tag __tag
, const _Alloc
& __a
,
1327 : _M_ptr(), _M_refcount(__tag
, (_Tp
*)0, __a
,
1328 std::forward
<_Args
>(__args
)...)
1330 // _M_ptr needs to point to the newly constructed object.
1331 // This relies on _Sp_counted_ptr_inplace::_M_get_deleter.
1333 void* __p
= _M_refcount
._M_get_deleter(typeid(__tag
));
1335 void* __p
= _M_refcount
._M_get_deleter(_Sp_make_shared_tag::_S_ti());
1337 _M_ptr
= static_cast<_Tp
*>(__p
);
1338 _M_enable_shared_from_this_with(_M_ptr
);
1341 template<typename _Tp1
, _Lock_policy _Lp1
, typename _Alloc
,
1343 friend __shared_ptr
<_Tp1
, _Lp1
>
1344 __allocate_shared(const _Alloc
& __a
, _Args
&&... __args
);
1346 // This constructor is used by __weak_ptr::lock() and
1347 // shared_ptr::shared_ptr(const weak_ptr&, std::nothrow_t).
1348 __shared_ptr(const __weak_ptr
<_Tp
, _Lp
>& __r
, std::nothrow_t
)
1349 : _M_refcount(__r
._M_refcount
, std::nothrow
)
1351 _M_ptr
= _M_refcount
._M_get_use_count() ? __r
._M_ptr
: nullptr;
1354 friend class __weak_ptr
<_Tp
, _Lp
>;
1358 template<typename _Yp
>
1359 using __esft_base_t
= decltype(__enable_shared_from_this_base(
1360 std::declval
<const __shared_count
<_Lp
>&>(),
1361 std::declval
<_Yp
*>()));
1363 // Detect an accessible and unambiguous enable_shared_from_this base.
1364 template<typename _Yp
, typename
= void>
1365 struct __has_esft_base
1368 template<typename _Yp
>
1369 struct __has_esft_base
<_Yp
, __void_t
<__esft_base_t
<_Yp
>>>
1370 : __not_
<is_array
<_Tp
>> { }; // No enable shared_from_this for arrays
1372 template<typename _Yp
, typename _Yp2
= typename remove_cv
<_Yp
>::type
>
1373 typename enable_if
<__has_esft_base
<_Yp2
>::value
>::type
1374 _M_enable_shared_from_this_with(_Yp
* __p
) noexcept
1376 if (auto __base
= __enable_shared_from_this_base(_M_refcount
, __p
))
1377 __base
->_M_weak_assign(const_cast<_Yp2
*>(__p
), _M_refcount
);
1380 template<typename _Yp
, typename _Yp2
= typename remove_cv
<_Yp
>::type
>
1381 typename enable_if
<!__has_esft_base
<_Yp2
>::value
>::type
1382 _M_enable_shared_from_this_with(_Yp
*) noexcept
1386 _M_get_deleter(const std::type_info
& __ti
) const noexcept
1387 { return _M_refcount
._M_get_deleter(__ti
); }
1389 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __shared_ptr
;
1390 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __weak_ptr
;
1392 template<typename _Del
, typename _Tp1
, _Lock_policy _Lp1
>
1393 friend _Del
* get_deleter(const __shared_ptr
<_Tp1
, _Lp1
>&) noexcept
;
1395 template<typename _Del
, typename _Tp1
>
1396 friend _Del
* get_deleter(const shared_ptr
<_Tp1
>&) noexcept
;
1398 element_type
* _M_ptr
; // Contained pointer.
1399 __shared_count
<_Lp
> _M_refcount
; // Reference counter.
1403 // 20.7.2.2.7 shared_ptr comparisons
1404 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1406 operator==(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1407 const __shared_ptr
<_Tp2
, _Lp
>& __b
) noexcept
1408 { return __a
.get() == __b
.get(); }
1410 template<typename _Tp
, _Lock_policy _Lp
>
1412 operator==(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
) noexcept
1415 template<typename _Tp
, _Lock_policy _Lp
>
1417 operator==(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __a
) noexcept
1420 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1422 operator!=(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1423 const __shared_ptr
<_Tp2
, _Lp
>& __b
) noexcept
1424 { return __a
.get() != __b
.get(); }
1426 template<typename _Tp
, _Lock_policy _Lp
>
1428 operator!=(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
) noexcept
1429 { return (bool)__a
; }
1431 template<typename _Tp
, _Lock_policy _Lp
>
1433 operator!=(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __a
) noexcept
1434 { return (bool)__a
; }
1436 template<typename _Tp
, typename _Up
, _Lock_policy _Lp
>
1438 operator<(const __shared_ptr
<_Tp
, _Lp
>& __a
,
1439 const __shared_ptr
<_Up
, _Lp
>& __b
) noexcept
1441 using _Tp_elt
= typename __shared_ptr
<_Tp
, _Lp
>::element_type
;
1442 using _Up_elt
= typename __shared_ptr
<_Up
, _Lp
>::element_type
;
1443 using _Vp
= typename common_type
<_Tp_elt
*, _Up_elt
*>::type
;
1444 return less
<_Vp
>()(__a
.get(), __b
.get());
1447 template<typename _Tp
, _Lock_policy _Lp
>
1449 operator<(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
) noexcept
1451 using _Tp_elt
= typename __shared_ptr
<_Tp
, _Lp
>::element_type
;
1452 return less
<_Tp_elt
*>()(__a
.get(), nullptr);
1455 template<typename _Tp
, _Lock_policy _Lp
>
1457 operator<(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __a
) noexcept
1459 using _Tp_elt
= typename __shared_ptr
<_Tp
, _Lp
>::element_type
;
1460 return less
<_Tp_elt
*>()(nullptr, __a
.get());
1463 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1465 operator<=(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1466 const __shared_ptr
<_Tp2
, _Lp
>& __b
) noexcept
1467 { return !(__b
< __a
); }
1469 template<typename _Tp
, _Lock_policy _Lp
>
1471 operator<=(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
) noexcept
1472 { return !(nullptr < __a
); }
1474 template<typename _Tp
, _Lock_policy _Lp
>
1476 operator<=(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __a
) noexcept
1477 { return !(__a
< nullptr); }
1479 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1481 operator>(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1482 const __shared_ptr
<_Tp2
, _Lp
>& __b
) noexcept
1483 { return (__b
< __a
); }
1485 template<typename _Tp
, _Lock_policy _Lp
>
1487 operator>(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
) noexcept
1488 { return nullptr < __a
; }
1490 template<typename _Tp
, _Lock_policy _Lp
>
1492 operator>(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __a
) noexcept
1493 { return __a
< nullptr; }
1495 template<typename _Tp1
, typename _Tp2
, _Lock_policy _Lp
>
1497 operator>=(const __shared_ptr
<_Tp1
, _Lp
>& __a
,
1498 const __shared_ptr
<_Tp2
, _Lp
>& __b
) noexcept
1499 { return !(__a
< __b
); }
1501 template<typename _Tp
, _Lock_policy _Lp
>
1503 operator>=(const __shared_ptr
<_Tp
, _Lp
>& __a
, nullptr_t
) noexcept
1504 { return !(__a
< nullptr); }
1506 template<typename _Tp
, _Lock_policy _Lp
>
1508 operator>=(nullptr_t
, const __shared_ptr
<_Tp
, _Lp
>& __a
) noexcept
1509 { return !(nullptr < __a
); }
1511 template<typename _Sp
>
1512 struct _Sp_less
: public binary_function
<_Sp
, _Sp
, bool>
1515 operator()(const _Sp
& __lhs
, const _Sp
& __rhs
) const noexcept
1517 typedef typename
_Sp::element_type element_type
;
1518 return std::less
<element_type
*>()(__lhs
.get(), __rhs
.get());
1522 template<typename _Tp
, _Lock_policy _Lp
>
1523 struct less
<__shared_ptr
<_Tp
, _Lp
>>
1524 : public _Sp_less
<__shared_ptr
<_Tp
, _Lp
>>
1527 // 20.7.2.2.8 shared_ptr specialized algorithms.
1528 template<typename _Tp
, _Lock_policy _Lp
>
1530 swap(__shared_ptr
<_Tp
, _Lp
>& __a
, __shared_ptr
<_Tp
, _Lp
>& __b
) noexcept
1533 // 20.7.2.2.9 shared_ptr casts
1535 // The seemingly equivalent code:
1536 // shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))
1537 // will eventually result in undefined behaviour, attempting to
1538 // delete the same object twice.
1539 /// static_pointer_cast
1540 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
1541 inline __shared_ptr
<_Tp
, _Lp
>
1542 static_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
) noexcept
1544 using _Sp
= __shared_ptr
<_Tp
, _Lp
>;
1545 return _Sp(__r
, static_cast<typename
_Sp::element_type
*>(__r
.get()));
1548 // The seemingly equivalent code:
1549 // shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))
1550 // will eventually result in undefined behaviour, attempting to
1551 // delete the same object twice.
1552 /// const_pointer_cast
1553 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
1554 inline __shared_ptr
<_Tp
, _Lp
>
1555 const_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
) noexcept
1557 using _Sp
= __shared_ptr
<_Tp
, _Lp
>;
1558 return _Sp(__r
, const_cast<typename
_Sp::element_type
*>(__r
.get()));
1561 // The seemingly equivalent code:
1562 // shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))
1563 // will eventually result in undefined behaviour, attempting to
1564 // delete the same object twice.
1565 /// dynamic_pointer_cast
1566 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
1567 inline __shared_ptr
<_Tp
, _Lp
>
1568 dynamic_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
) noexcept
1570 using _Sp
= __shared_ptr
<_Tp
, _Lp
>;
1571 if (auto* __p
= dynamic_cast<typename
_Sp::element_type
*>(__r
.get()))
1572 return _Sp(__r
, __p
);
1576 #if __cplusplus > 201402L
1577 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
1578 inline __shared_ptr
<_Tp
, _Lp
>
1579 reinterpret_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
) noexcept
1581 using _Sp
= __shared_ptr
<_Tp
, _Lp
>;
1582 return _Sp(__r
, reinterpret_cast<typename
_Sp::element_type
*>(__r
.get()));
1586 template<typename _Tp
, _Lock_policy _Lp
>
1589 template<typename _Yp
, typename _Res
= void>
1590 using _Compatible
= typename
1591 enable_if
<__sp_compatible_with
<_Yp
*, _Tp
*>::value
, _Res
>::type
;
1593 // Constraint for assignment from shared_ptr and weak_ptr:
1594 template<typename _Yp
>
1595 using _Assignable
= _Compatible
<_Yp
, __weak_ptr
&>;
1598 using element_type
= typename remove_extent
<_Tp
>::type
;
1600 constexpr __weak_ptr() noexcept
1601 : _M_ptr(nullptr), _M_refcount()
1604 __weak_ptr(const __weak_ptr
&) noexcept
= default;
1606 ~__weak_ptr() = default;
1608 // The "obvious" converting constructor implementation:
1610 // template<typename _Tp1>
1611 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
1612 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
1615 // has a serious problem.
1617 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
1618 // conversion may require access to *__r._M_ptr (virtual inheritance).
1620 // It is not possible to avoid spurious access violations since
1621 // in multithreaded programs __r._M_ptr may be invalidated at any point.
1622 template<typename _Yp
, typename
= _Compatible
<_Yp
>>
1623 __weak_ptr(const __weak_ptr
<_Yp
, _Lp
>& __r
) noexcept
1624 : _M_refcount(__r
._M_refcount
)
1625 { _M_ptr
= __r
.lock().get(); }
1627 template<typename _Yp
, typename
= _Compatible
<_Yp
>>
1628 __weak_ptr(const __shared_ptr
<_Yp
, _Lp
>& __r
) noexcept
1629 : _M_ptr(__r
._M_ptr
), _M_refcount(__r
._M_refcount
)
1632 __weak_ptr(__weak_ptr
&& __r
) noexcept
1633 : _M_ptr(__r
._M_ptr
), _M_refcount(std::move(__r
._M_refcount
))
1634 { __r
._M_ptr
= nullptr; }
1636 template<typename _Yp
, typename
= _Compatible
<_Yp
>>
1637 __weak_ptr(__weak_ptr
<_Yp
, _Lp
>&& __r
) noexcept
1638 : _M_ptr(__r
.lock().get()), _M_refcount(std::move(__r
._M_refcount
))
1639 { __r
._M_ptr
= nullptr; }
1642 operator=(const __weak_ptr
& __r
) noexcept
= default;
1644 template<typename _Yp
>
1646 operator=(const __weak_ptr
<_Yp
, _Lp
>& __r
) noexcept
1648 _M_ptr
= __r
.lock().get();
1649 _M_refcount
= __r
._M_refcount
;
1653 template<typename _Yp
>
1655 operator=(const __shared_ptr
<_Yp
, _Lp
>& __r
) noexcept
1657 _M_ptr
= __r
._M_ptr
;
1658 _M_refcount
= __r
._M_refcount
;
1663 operator=(__weak_ptr
&& __r
) noexcept
1665 _M_ptr
= __r
._M_ptr
;
1666 _M_refcount
= std::move(__r
._M_refcount
);
1667 __r
._M_ptr
= nullptr;
1671 template<typename _Yp
>
1673 operator=(__weak_ptr
<_Yp
, _Lp
>&& __r
) noexcept
1675 _M_ptr
= __r
.lock().get();
1676 _M_refcount
= std::move(__r
._M_refcount
);
1677 __r
._M_ptr
= nullptr;
1681 __shared_ptr
<_Tp
, _Lp
>
1682 lock() const noexcept
1683 { return __shared_ptr
<element_type
, _Lp
>(*this, std::nothrow
); }
1686 use_count() const noexcept
1687 { return _M_refcount
._M_get_use_count(); }
1690 expired() const noexcept
1691 { return _M_refcount
._M_get_use_count() == 0; }
1693 template<typename _Tp1
>
1695 owner_before(const __shared_ptr
<_Tp1
, _Lp
>& __rhs
) const noexcept
1696 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1698 template<typename _Tp1
>
1700 owner_before(const __weak_ptr
<_Tp1
, _Lp
>& __rhs
) const noexcept
1701 { return _M_refcount
._M_less(__rhs
._M_refcount
); }
1705 { __weak_ptr().swap(*this); }
1708 swap(__weak_ptr
& __s
) noexcept
1710 std::swap(_M_ptr
, __s
._M_ptr
);
1711 _M_refcount
._M_swap(__s
._M_refcount
);
1715 // Used by __enable_shared_from_this.
1717 _M_assign(_Tp
* __ptr
, const __shared_count
<_Lp
>& __refcount
) noexcept
1719 if (use_count() == 0)
1722 _M_refcount
= __refcount
;
1726 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __shared_ptr
;
1727 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __weak_ptr
;
1728 friend class __enable_shared_from_this
<_Tp
, _Lp
>;
1729 friend class enable_shared_from_this
<_Tp
>;
1731 element_type
* _M_ptr
; // Contained pointer.
1732 __weak_count
<_Lp
> _M_refcount
; // Reference counter.
1735 // 20.7.2.3.6 weak_ptr specialized algorithms.
1736 template<typename _Tp
, _Lock_policy _Lp
>
1738 swap(__weak_ptr
<_Tp
, _Lp
>& __a
, __weak_ptr
<_Tp
, _Lp
>& __b
) noexcept
1741 template<typename _Tp
, typename _Tp1
>
1742 struct _Sp_owner_less
: public binary_function
<_Tp
, _Tp
, bool>
1745 operator()(const _Tp
& __lhs
, const _Tp
& __rhs
) const noexcept
1746 { return __lhs
.owner_before(__rhs
); }
1749 operator()(const _Tp
& __lhs
, const _Tp1
& __rhs
) const noexcept
1750 { return __lhs
.owner_before(__rhs
); }
1753 operator()(const _Tp1
& __lhs
, const _Tp
& __rhs
) const noexcept
1754 { return __lhs
.owner_before(__rhs
); }
1758 struct _Sp_owner_less
<void, void>
1760 template<typename _Tp
, typename _Up
>
1762 operator()(const _Tp
& __lhs
, const _Up
& __rhs
) const noexcept
1763 -> decltype(__lhs
.owner_before(__rhs
))
1764 { return __lhs
.owner_before(__rhs
); }
1766 using is_transparent
= void;
1769 template<typename _Tp
, _Lock_policy _Lp
>
1770 struct owner_less
<__shared_ptr
<_Tp
, _Lp
>>
1771 : public _Sp_owner_less
<__shared_ptr
<_Tp
, _Lp
>, __weak_ptr
<_Tp
, _Lp
>>
1774 template<typename _Tp
, _Lock_policy _Lp
>
1775 struct owner_less
<__weak_ptr
<_Tp
, _Lp
>>
1776 : public _Sp_owner_less
<__weak_ptr
<_Tp
, _Lp
>, __shared_ptr
<_Tp
, _Lp
>>
1780 template<typename _Tp
, _Lock_policy _Lp
>
1781 class __enable_shared_from_this
1784 constexpr __enable_shared_from_this() noexcept
{ }
1786 __enable_shared_from_this(const __enable_shared_from_this
&) noexcept
{ }
1788 __enable_shared_from_this
&
1789 operator=(const __enable_shared_from_this
&) noexcept
1792 ~__enable_shared_from_this() { }
1795 __shared_ptr
<_Tp
, _Lp
>
1797 { return __shared_ptr
<_Tp
, _Lp
>(this->_M_weak_this
); }
1799 __shared_ptr
<const _Tp
, _Lp
>
1800 shared_from_this() const
1801 { return __shared_ptr
<const _Tp
, _Lp
>(this->_M_weak_this
); }
1803 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
1804 __weak_ptr
<_Tp
, _Lp
>
1805 weak_from_this() noexcept
1806 { return this->_M_weak_this
; }
1808 __weak_ptr
<const _Tp
, _Lp
>
1809 weak_from_this() const noexcept
1810 { return this->_M_weak_this
; }
1814 template<typename _Tp1
>
1816 _M_weak_assign(_Tp1
* __p
, const __shared_count
<_Lp
>& __n
) const noexcept
1817 { _M_weak_this
._M_assign(__p
, __n
); }
1819 friend const __enable_shared_from_this
*
1820 __enable_shared_from_this_base(const __shared_count
<_Lp
>&,
1821 const __enable_shared_from_this
* __p
)
1824 template<typename
, _Lock_policy
>
1825 friend class __shared_ptr
;
1827 mutable __weak_ptr
<_Tp
, _Lp
> _M_weak_this
;
1830 template<typename _Tp
, _Lock_policy _Lp
, typename _Alloc
, typename
... _Args
>
1831 inline __shared_ptr
<_Tp
, _Lp
>
1832 __allocate_shared(const _Alloc
& __a
, _Args
&&... __args
)
1834 return __shared_ptr
<_Tp
, _Lp
>(_Sp_make_shared_tag(), __a
,
1835 std::forward
<_Args
>(__args
)...);
1838 template<typename _Tp
, _Lock_policy _Lp
, typename
... _Args
>
1839 inline __shared_ptr
<_Tp
, _Lp
>
1840 __make_shared(_Args
&&... __args
)
1842 typedef typename
std::remove_const
<_Tp
>::type _Tp_nc
;
1843 return std::__allocate_shared
<_Tp
, _Lp
>(std::allocator
<_Tp_nc
>(),
1844 std::forward
<_Args
>(__args
)...);
1847 /// std::hash specialization for __shared_ptr.
1848 template<typename _Tp
, _Lock_policy _Lp
>
1849 struct hash
<__shared_ptr
<_Tp
, _Lp
>>
1850 : public __hash_base
<size_t, __shared_ptr
<_Tp
, _Lp
>>
1853 operator()(const __shared_ptr
<_Tp
, _Lp
>& __s
) const noexcept
1855 return hash
<typename __shared_ptr
<_Tp
, _Lp
>::element_type
*>()(
1860 _GLIBCXX_END_NAMESPACE_VERSION
1863 #endif // _SHARED_PTR_BASE_H