PR libstdc++/79467 use lvalues in is_callable check
[official-gcc.git] / libstdc++-v3 / include / bits / shared_ptr_base.h
blob770a0948d4036fec578c39b477d746eb6817c67b
1 // shared_ptr and weak_ptr implementation details -*- C++ -*-
3 // Copyright (C) 2007-2017 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
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.
27 // shared_count.hpp
28 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
30 // shared_ptr.hpp
31 // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
32 // Copyright (C) 2001, 2002, 2003 Peter Dimov
34 // weak_ptr.hpp
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 #if __cpp_rtti
53 # include <typeinfo>
54 #endif
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)
62 _GLIBCXX_BEGIN_NAMESPACE_VERSION
64 #if _GLIBCXX_USE_DEPRECATED
65 template<typename> class auto_ptr;
66 #endif
68 /**
69 * @brief Exception possibly thrown by @c shared_ptr.
70 * @ingroup exceptions
72 class bad_weak_ptr : public std::exception
74 public:
75 virtual char const* what() const noexcept;
77 virtual ~bad_weak_ptr() noexcept;
80 // Substitute for bad_weak_ptr object in the case of -fno-exceptions.
81 inline void
82 __throw_bad_weak_ptr()
83 { _GLIBCXX_THROW_OR_ABORT(bad_weak_ptr()); }
85 using __gnu_cxx::_Lock_policy;
86 using __gnu_cxx::__default_lock_policy;
87 using __gnu_cxx::_S_single;
88 using __gnu_cxx::_S_mutex;
89 using __gnu_cxx::_S_atomic;
91 // Empty helper class except when the template argument is _S_mutex.
92 template<_Lock_policy _Lp>
93 class _Mutex_base
95 protected:
96 // The atomic policy uses fully-fenced builtins, single doesn't care.
97 enum { _S_need_barriers = 0 };
100 template<>
101 class _Mutex_base<_S_mutex>
102 : public __gnu_cxx::__mutex
104 protected:
105 // This policy is used when atomic builtins are not available.
106 // The replacement atomic operations might not have the necessary
107 // memory barriers.
108 enum { _S_need_barriers = 1 };
111 template<_Lock_policy _Lp = __default_lock_policy>
112 class _Sp_counted_base
113 : public _Mutex_base<_Lp>
115 public:
116 _Sp_counted_base() noexcept
117 : _M_use_count(1), _M_weak_count(1) { }
119 virtual
120 ~_Sp_counted_base() noexcept
123 // Called when _M_use_count drops to zero, to release the resources
124 // managed by *this.
125 virtual void
126 _M_dispose() noexcept = 0;
128 // Called when _M_weak_count drops to zero.
129 virtual void
130 _M_destroy() noexcept
131 { delete this; }
133 virtual void*
134 _M_get_deleter(const std::type_info&) noexcept = 0;
136 void
137 _M_add_ref_copy()
138 { __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); }
140 void
141 _M_add_ref_lock();
143 bool
144 _M_add_ref_lock_nothrow();
146 void
147 _M_release() noexcept
149 // Be race-detector-friendly. For more info see bits/c++config.
150 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count);
151 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
153 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count);
154 _M_dispose();
155 // There must be a memory barrier between dispose() and destroy()
156 // to ensure that the effects of dispose() are observed in the
157 // thread that runs destroy().
158 // See http://gcc.gnu.org/ml/libstdc++/2005-11/msg00136.html
159 if (_Mutex_base<_Lp>::_S_need_barriers)
161 __atomic_thread_fence (__ATOMIC_ACQ_REL);
164 // Be race-detector-friendly. For more info see bits/c++config.
165 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
166 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count,
167 -1) == 1)
169 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
170 _M_destroy();
175 void
176 _M_weak_add_ref() noexcept
177 { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); }
179 void
180 _M_weak_release() noexcept
182 // Be race-detector-friendly. For more info see bits/c++config.
183 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
184 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1)
186 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
187 if (_Mutex_base<_Lp>::_S_need_barriers)
189 // See _M_release(),
190 // destroy() must observe results of dispose()
191 __atomic_thread_fence (__ATOMIC_ACQ_REL);
193 _M_destroy();
197 long
198 _M_get_use_count() const noexcept
200 // No memory barrier is used here so there is no synchronization
201 // with other threads.
202 return __atomic_load_n(&_M_use_count, __ATOMIC_RELAXED);
205 private:
206 _Sp_counted_base(_Sp_counted_base const&) = delete;
207 _Sp_counted_base& operator=(_Sp_counted_base const&) = delete;
209 _Atomic_word _M_use_count; // #shared
210 _Atomic_word _M_weak_count; // #weak + (#shared != 0)
213 template<>
214 inline void
215 _Sp_counted_base<_S_single>::
216 _M_add_ref_lock()
218 if (_M_use_count == 0)
219 __throw_bad_weak_ptr();
220 ++_M_use_count;
223 template<>
224 inline void
225 _Sp_counted_base<_S_mutex>::
226 _M_add_ref_lock()
228 __gnu_cxx::__scoped_lock sentry(*this);
229 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
231 _M_use_count = 0;
232 __throw_bad_weak_ptr();
236 template<>
237 inline void
238 _Sp_counted_base<_S_atomic>::
239 _M_add_ref_lock()
241 // Perform lock-free add-if-not-zero operation.
242 _Atomic_word __count = _M_get_use_count();
245 if (__count == 0)
246 __throw_bad_weak_ptr();
247 // Replace the current counter value with the old value + 1, as
248 // long as it's not changed meanwhile.
250 while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1,
251 true, __ATOMIC_ACQ_REL,
252 __ATOMIC_RELAXED));
255 template<>
256 inline bool
257 _Sp_counted_base<_S_single>::
258 _M_add_ref_lock_nothrow()
260 if (_M_use_count == 0)
261 return false;
262 ++_M_use_count;
263 return true;
266 template<>
267 inline bool
268 _Sp_counted_base<_S_mutex>::
269 _M_add_ref_lock_nothrow()
271 __gnu_cxx::__scoped_lock sentry(*this);
272 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
274 _M_use_count = 0;
275 return false;
277 return true;
280 template<>
281 inline bool
282 _Sp_counted_base<_S_atomic>::
283 _M_add_ref_lock_nothrow()
285 // Perform lock-free add-if-not-zero operation.
286 _Atomic_word __count = _M_get_use_count();
289 if (__count == 0)
290 return false;
291 // Replace the current counter value with the old value + 1, as
292 // long as it's not changed meanwhile.
294 while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1,
295 true, __ATOMIC_ACQ_REL,
296 __ATOMIC_RELAXED));
297 return true;
300 template<>
301 inline void
302 _Sp_counted_base<_S_single>::_M_add_ref_copy()
303 { ++_M_use_count; }
305 template<>
306 inline void
307 _Sp_counted_base<_S_single>::_M_release() noexcept
309 if (--_M_use_count == 0)
311 _M_dispose();
312 if (--_M_weak_count == 0)
313 _M_destroy();
317 template<>
318 inline void
319 _Sp_counted_base<_S_single>::_M_weak_add_ref() noexcept
320 { ++_M_weak_count; }
322 template<>
323 inline void
324 _Sp_counted_base<_S_single>::_M_weak_release() noexcept
326 if (--_M_weak_count == 0)
327 _M_destroy();
330 template<>
331 inline long
332 _Sp_counted_base<_S_single>::_M_get_use_count() const noexcept
333 { return _M_use_count; }
336 // Forward declarations.
337 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
338 class __shared_ptr;
340 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
341 class __weak_ptr;
343 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
344 class __enable_shared_from_this;
346 template<typename _Tp>
347 class shared_ptr;
349 template<typename _Tp>
350 class weak_ptr;
352 template<typename _Tp>
353 struct owner_less;
355 template<typename _Tp>
356 class enable_shared_from_this;
358 template<_Lock_policy _Lp = __default_lock_policy>
359 class __weak_count;
361 template<_Lock_policy _Lp = __default_lock_policy>
362 class __shared_count;
365 // Counted ptr with no deleter or allocator support
366 template<typename _Ptr, _Lock_policy _Lp>
367 class _Sp_counted_ptr final : public _Sp_counted_base<_Lp>
369 public:
370 explicit
371 _Sp_counted_ptr(_Ptr __p) noexcept
372 : _M_ptr(__p) { }
374 virtual void
375 _M_dispose() noexcept
376 { delete _M_ptr; }
378 virtual void
379 _M_destroy() noexcept
380 { delete this; }
382 virtual void*
383 _M_get_deleter(const std::type_info&) noexcept
384 { return nullptr; }
386 _Sp_counted_ptr(const _Sp_counted_ptr&) = delete;
387 _Sp_counted_ptr& operator=(const _Sp_counted_ptr&) = delete;
389 private:
390 _Ptr _M_ptr;
393 template<>
394 inline void
395 _Sp_counted_ptr<nullptr_t, _S_single>::_M_dispose() noexcept { }
397 template<>
398 inline void
399 _Sp_counted_ptr<nullptr_t, _S_mutex>::_M_dispose() noexcept { }
401 template<>
402 inline void
403 _Sp_counted_ptr<nullptr_t, _S_atomic>::_M_dispose() noexcept { }
405 template<int _Nm, typename _Tp,
406 bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
407 struct _Sp_ebo_helper;
409 /// Specialization using EBO.
410 template<int _Nm, typename _Tp>
411 struct _Sp_ebo_helper<_Nm, _Tp, true> : private _Tp
413 explicit _Sp_ebo_helper(const _Tp& __tp) : _Tp(__tp) { }
415 static _Tp&
416 _S_get(_Sp_ebo_helper& __eboh) { return static_cast<_Tp&>(__eboh); }
419 /// Specialization not using EBO.
420 template<int _Nm, typename _Tp>
421 struct _Sp_ebo_helper<_Nm, _Tp, false>
423 explicit _Sp_ebo_helper(const _Tp& __tp) : _M_tp(__tp) { }
425 static _Tp&
426 _S_get(_Sp_ebo_helper& __eboh)
427 { return __eboh._M_tp; }
429 private:
430 _Tp _M_tp;
433 // Support for custom deleter and/or allocator
434 template<typename _Ptr, typename _Deleter, typename _Alloc, _Lock_policy _Lp>
435 class _Sp_counted_deleter final : public _Sp_counted_base<_Lp>
437 class _Impl : _Sp_ebo_helper<0, _Deleter>, _Sp_ebo_helper<1, _Alloc>
439 typedef _Sp_ebo_helper<0, _Deleter> _Del_base;
440 typedef _Sp_ebo_helper<1, _Alloc> _Alloc_base;
442 public:
443 _Impl(_Ptr __p, _Deleter __d, const _Alloc& __a) noexcept
444 : _M_ptr(__p), _Del_base(__d), _Alloc_base(__a)
447 _Deleter& _M_del() noexcept { return _Del_base::_S_get(*this); }
448 _Alloc& _M_alloc() noexcept { return _Alloc_base::_S_get(*this); }
450 _Ptr _M_ptr;
453 public:
454 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_deleter>;
456 // __d(__p) must not throw.
457 _Sp_counted_deleter(_Ptr __p, _Deleter __d) noexcept
458 : _M_impl(__p, __d, _Alloc()) { }
460 // __d(__p) must not throw.
461 _Sp_counted_deleter(_Ptr __p, _Deleter __d, const _Alloc& __a) noexcept
462 : _M_impl(__p, __d, __a) { }
464 ~_Sp_counted_deleter() noexcept { }
466 virtual void
467 _M_dispose() noexcept
468 { _M_impl._M_del()(_M_impl._M_ptr); }
470 virtual void
471 _M_destroy() noexcept
473 __allocator_type __a(_M_impl._M_alloc());
474 __allocated_ptr<__allocator_type> __guard_ptr{ __a, this };
475 this->~_Sp_counted_deleter();
478 virtual void*
479 _M_get_deleter(const std::type_info& __ti) noexcept
481 #if __cpp_rtti
482 // _GLIBCXX_RESOLVE_LIB_DEFECTS
483 // 2400. shared_ptr's get_deleter() should use addressof()
484 return __ti == typeid(_Deleter)
485 ? std::__addressof(_M_impl._M_del())
486 : nullptr;
487 #else
488 return nullptr;
489 #endif
492 private:
493 _Impl _M_impl;
496 // helpers for make_shared / allocate_shared
498 struct _Sp_make_shared_tag { };
500 template<typename _Tp, typename _Alloc, _Lock_policy _Lp>
501 class _Sp_counted_ptr_inplace final : public _Sp_counted_base<_Lp>
503 class _Impl : _Sp_ebo_helper<0, _Alloc>
505 typedef _Sp_ebo_helper<0, _Alloc> _A_base;
507 public:
508 explicit _Impl(_Alloc __a) noexcept : _A_base(__a) { }
510 _Alloc& _M_alloc() noexcept { return _A_base::_S_get(*this); }
512 __gnu_cxx::__aligned_buffer<_Tp> _M_storage;
515 public:
516 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>;
518 template<typename... _Args>
519 _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
520 : _M_impl(__a)
522 // _GLIBCXX_RESOLVE_LIB_DEFECTS
523 // 2070. allocate_shared should use allocator_traits<A>::construct
524 allocator_traits<_Alloc>::construct(__a, _M_ptr(),
525 std::forward<_Args>(__args)...); // might throw
528 ~_Sp_counted_ptr_inplace() noexcept { }
530 virtual void
531 _M_dispose() noexcept
533 allocator_traits<_Alloc>::destroy(_M_impl._M_alloc(), _M_ptr());
536 // Override because the allocator needs to know the dynamic type
537 virtual void
538 _M_destroy() noexcept
540 __allocator_type __a(_M_impl._M_alloc());
541 __allocated_ptr<__allocator_type> __guard_ptr{ __a, this };
542 this->~_Sp_counted_ptr_inplace();
545 // Sneaky trick so __shared_ptr can get the managed pointer
546 virtual void*
547 _M_get_deleter(const std::type_info& __ti) noexcept
549 #if __cpp_rtti
550 if (__ti == typeid(_Sp_make_shared_tag))
551 return const_cast<typename remove_cv<_Tp>::type*>(_M_ptr());
552 #endif
553 return nullptr;
556 private:
557 _Tp* _M_ptr() noexcept { return _M_impl._M_storage._M_ptr(); }
559 _Impl _M_impl;
562 // The default deleter for shared_ptr<T[]> and shared_ptr<T[N]>.
563 struct __sp_array_delete
565 template<typename _Yp>
566 void operator()(_Yp* __p) const { delete[] __p; }
569 template<_Lock_policy _Lp>
570 class __shared_count
572 public:
573 constexpr __shared_count() noexcept : _M_pi(0)
576 template<typename _Ptr>
577 explicit
578 __shared_count(_Ptr __p) : _M_pi(0)
580 __try
582 _M_pi = new _Sp_counted_ptr<_Ptr, _Lp>(__p);
584 __catch(...)
586 delete __p;
587 __throw_exception_again;
591 template<typename _Ptr>
592 __shared_count(_Ptr __p, /* is_array = */ false_type)
593 : __shared_count(__p)
596 template<typename _Ptr>
597 __shared_count(_Ptr __p, /* is_array = */ true_type)
598 : __shared_count(__p, __sp_array_delete{}, allocator<void>())
601 template<typename _Ptr, typename _Deleter>
602 __shared_count(_Ptr __p, _Deleter __d)
603 : __shared_count(__p, std::move(__d), allocator<void>())
606 template<typename _Ptr, typename _Deleter, typename _Alloc>
607 __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
609 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
610 __try
612 typename _Sp_cd_type::__allocator_type __a2(__a);
613 auto __guard = std::__allocate_guarded(__a2);
614 _Sp_cd_type* __mem = __guard.get();
615 ::new (__mem) _Sp_cd_type(__p, std::move(__d), std::move(__a));
616 _M_pi = __mem;
617 __guard = nullptr;
619 __catch(...)
621 __d(__p); // Call _Deleter on __p.
622 __throw_exception_again;
626 template<typename _Tp, typename _Alloc, typename... _Args>
627 __shared_count(_Sp_make_shared_tag, _Tp*, const _Alloc& __a,
628 _Args&&... __args)
629 : _M_pi(0)
631 typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type;
632 typename _Sp_cp_type::__allocator_type __a2(__a);
633 auto __guard = std::__allocate_guarded(__a2);
634 _Sp_cp_type* __mem = __guard.get();
635 ::new (__mem) _Sp_cp_type(std::move(__a),
636 std::forward<_Args>(__args)...);
637 _M_pi = __mem;
638 __guard = nullptr;
641 #if _GLIBCXX_USE_DEPRECATED
642 // Special case for auto_ptr<_Tp> to provide the strong guarantee.
643 template<typename _Tp>
644 explicit
645 __shared_count(std::auto_ptr<_Tp>&& __r);
646 #endif
648 // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee.
649 template<typename _Tp, typename _Del>
650 explicit
651 __shared_count(std::unique_ptr<_Tp, _Del>&& __r) : _M_pi(0)
653 // _GLIBCXX_RESOLVE_LIB_DEFECTS
654 // 2415. Inconsistency between unique_ptr and shared_ptr
655 if (__r.get() == nullptr)
656 return;
658 using _Ptr = typename unique_ptr<_Tp, _Del>::pointer;
659 using _Del2 = typename conditional<is_reference<_Del>::value,
660 reference_wrapper<typename remove_reference<_Del>::type>,
661 _Del>::type;
662 using _Sp_cd_type
663 = _Sp_counted_deleter<_Ptr, _Del2, allocator<void>, _Lp>;
664 using _Alloc = allocator<_Sp_cd_type>;
665 using _Alloc_traits = allocator_traits<_Alloc>;
666 _Alloc __a;
667 _Sp_cd_type* __mem = _Alloc_traits::allocate(__a, 1);
668 _Alloc_traits::construct(__a, __mem, __r.release(),
669 __r.get_deleter()); // non-throwing
670 _M_pi = __mem;
673 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
674 explicit __shared_count(const __weak_count<_Lp>& __r);
676 // Does not throw if __r._M_get_use_count() == 0, caller must check.
677 explicit __shared_count(const __weak_count<_Lp>& __r, std::nothrow_t);
679 ~__shared_count() noexcept
681 if (_M_pi != nullptr)
682 _M_pi->_M_release();
685 __shared_count(const __shared_count& __r) noexcept
686 : _M_pi(__r._M_pi)
688 if (_M_pi != 0)
689 _M_pi->_M_add_ref_copy();
692 __shared_count&
693 operator=(const __shared_count& __r) noexcept
695 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
696 if (__tmp != _M_pi)
698 if (__tmp != 0)
699 __tmp->_M_add_ref_copy();
700 if (_M_pi != 0)
701 _M_pi->_M_release();
702 _M_pi = __tmp;
704 return *this;
707 void
708 _M_swap(__shared_count& __r) noexcept
710 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
711 __r._M_pi = _M_pi;
712 _M_pi = __tmp;
715 long
716 _M_get_use_count() const noexcept
717 { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
719 bool
720 _M_unique() const noexcept
721 { return this->_M_get_use_count() == 1; }
723 void*
724 _M_get_deleter(const std::type_info& __ti) const noexcept
725 { return _M_pi ? _M_pi->_M_get_deleter(__ti) : nullptr; }
727 bool
728 _M_less(const __shared_count& __rhs) const noexcept
729 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
731 bool
732 _M_less(const __weak_count<_Lp>& __rhs) const noexcept
733 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
735 // Friend function injected into enclosing namespace and found by ADL
736 friend inline bool
737 operator==(const __shared_count& __a, const __shared_count& __b) noexcept
738 { return __a._M_pi == __b._M_pi; }
740 private:
741 friend class __weak_count<_Lp>;
743 _Sp_counted_base<_Lp>* _M_pi;
747 template<_Lock_policy _Lp>
748 class __weak_count
750 public:
751 constexpr __weak_count() noexcept : _M_pi(nullptr)
754 __weak_count(const __shared_count<_Lp>& __r) noexcept
755 : _M_pi(__r._M_pi)
757 if (_M_pi != nullptr)
758 _M_pi->_M_weak_add_ref();
761 __weak_count(const __weak_count& __r) noexcept
762 : _M_pi(__r._M_pi)
764 if (_M_pi != nullptr)
765 _M_pi->_M_weak_add_ref();
768 __weak_count(__weak_count&& __r) noexcept
769 : _M_pi(__r._M_pi)
770 { __r._M_pi = nullptr; }
772 ~__weak_count() noexcept
774 if (_M_pi != nullptr)
775 _M_pi->_M_weak_release();
778 __weak_count&
779 operator=(const __shared_count<_Lp>& __r) noexcept
781 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
782 if (__tmp != nullptr)
783 __tmp->_M_weak_add_ref();
784 if (_M_pi != nullptr)
785 _M_pi->_M_weak_release();
786 _M_pi = __tmp;
787 return *this;
790 __weak_count&
791 operator=(const __weak_count& __r) noexcept
793 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
794 if (__tmp != nullptr)
795 __tmp->_M_weak_add_ref();
796 if (_M_pi != nullptr)
797 _M_pi->_M_weak_release();
798 _M_pi = __tmp;
799 return *this;
802 __weak_count&
803 operator=(__weak_count&& __r) noexcept
805 if (_M_pi != nullptr)
806 _M_pi->_M_weak_release();
807 _M_pi = __r._M_pi;
808 __r._M_pi = nullptr;
809 return *this;
812 void
813 _M_swap(__weak_count& __r) noexcept
815 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
816 __r._M_pi = _M_pi;
817 _M_pi = __tmp;
820 long
821 _M_get_use_count() const noexcept
822 { return _M_pi != nullptr ? _M_pi->_M_get_use_count() : 0; }
824 bool
825 _M_less(const __weak_count& __rhs) const noexcept
826 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
828 bool
829 _M_less(const __shared_count<_Lp>& __rhs) const noexcept
830 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
832 // Friend function injected into enclosing namespace and found by ADL
833 friend inline bool
834 operator==(const __weak_count& __a, const __weak_count& __b) noexcept
835 { return __a._M_pi == __b._M_pi; }
837 private:
838 friend class __shared_count<_Lp>;
840 _Sp_counted_base<_Lp>* _M_pi;
843 // Now that __weak_count is defined we can define this constructor:
844 template<_Lock_policy _Lp>
845 inline
846 __shared_count<_Lp>::__shared_count(const __weak_count<_Lp>& __r)
847 : _M_pi(__r._M_pi)
849 if (_M_pi != nullptr)
850 _M_pi->_M_add_ref_lock();
851 else
852 __throw_bad_weak_ptr();
855 // Now that __weak_count is defined we can define this constructor:
856 template<_Lock_policy _Lp>
857 inline
858 __shared_count<_Lp>::
859 __shared_count(const __weak_count<_Lp>& __r, std::nothrow_t)
860 : _M_pi(__r._M_pi)
862 if (_M_pi != nullptr)
863 if (!_M_pi->_M_add_ref_lock_nothrow())
864 _M_pi = nullptr;
867 #define __cpp_lib_shared_ptr_arrays 201603
869 // Helper traits for shared_ptr of array:
871 // A pointer type Y* is said to be compatible with a pointer type T* when
872 // either Y* is convertible to T* or Y is U[N] and T is U cv [].
873 template<typename _Yp_ptr, typename _Tp_ptr>
874 struct __sp_compatible_with
875 : false_type
876 { };
878 template<typename _Yp, typename _Tp>
879 struct __sp_compatible_with<_Yp*, _Tp*>
880 : is_convertible<_Yp*, _Tp*>::type
881 { };
883 template<typename _Up, size_t _Nm>
884 struct __sp_compatible_with<_Up(*)[_Nm], _Up(*)[]>
885 : true_type
886 { };
888 template<typename _Up, size_t _Nm>
889 struct __sp_compatible_with<_Up(*)[_Nm], const _Up(*)[]>
890 : true_type
891 { };
893 template<typename _Up, size_t _Nm>
894 struct __sp_compatible_with<_Up(*)[_Nm], volatile _Up(*)[]>
895 : true_type
896 { };
898 template<typename _Up, size_t _Nm>
899 struct __sp_compatible_with<_Up(*)[_Nm], const volatile _Up(*)[]>
900 : true_type
901 { };
903 // Test conversion from Y(*)[N] to U(*)[N] without forming invalid type Y[N].
904 template<typename _Up, size_t _Nm, typename _Yp, typename = void>
905 struct __sp_is_constructible_arrN
906 : false_type
907 { };
909 template<typename _Up, size_t _Nm, typename _Yp>
910 struct __sp_is_constructible_arrN<_Up, _Nm, _Yp, __void_t<_Yp[_Nm]>>
911 : is_convertible<_Yp(*)[_Nm], _Up(*)[_Nm]>::type
912 { };
914 // Test conversion from Y(*)[] to U(*)[] without forming invalid type Y[].
915 template<typename _Up, typename _Yp, typename = void>
916 struct __sp_is_constructible_arr
917 : false_type
918 { };
920 template<typename _Up, typename _Yp>
921 struct __sp_is_constructible_arr<_Up, _Yp, __void_t<_Yp[]>>
922 : is_convertible<_Yp(*)[], _Up(*)[]>::type
923 { };
925 // Trait to check if shared_ptr<T> can be constructed from Y*.
926 template<typename _Tp, typename _Yp>
927 struct __sp_is_constructible;
929 // When T is U[N], Y(*)[N] shall be convertible to T*;
930 template<typename _Up, size_t _Nm, typename _Yp>
931 struct __sp_is_constructible<_Up[_Nm], _Yp>
932 : __sp_is_constructible_arrN<_Up, _Nm, _Yp>::type
933 { };
935 // when T is U[], Y(*)[] shall be convertible to T*;
936 template<typename _Up, typename _Yp>
937 struct __sp_is_constructible<_Up[], _Yp>
938 : __sp_is_constructible_arr<_Up, _Yp>::type
939 { };
941 // otherwise, Y* shall be convertible to T*.
942 template<typename _Tp, typename _Yp>
943 struct __sp_is_constructible
944 : is_convertible<_Yp*, _Tp*>::type
945 { };
948 // Define operator* and operator-> for shared_ptr<T>.
949 template<typename _Tp, _Lock_policy _Lp,
950 bool = is_array<_Tp>::value, bool = is_void<_Tp>::value>
951 class __shared_ptr_access
953 public:
954 using element_type = _Tp;
956 element_type&
957 operator*() const noexcept
959 __glibcxx_assert(_M_get() != nullptr);
960 return *_M_get();
963 element_type*
964 operator->() const noexcept
966 _GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr);
967 return _M_get();
970 private:
971 element_type*
972 _M_get() const noexcept
973 { return static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get(); }
976 // Define operator-> for shared_ptr<cv void>.
977 template<typename _Tp, _Lock_policy _Lp>
978 class __shared_ptr_access<_Tp, _Lp, false, true>
980 public:
981 using element_type = _Tp;
983 element_type*
984 operator->() const noexcept
986 auto __ptr = static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get();
987 _GLIBCXX_DEBUG_PEDASSERT(__ptr != nullptr);
988 return __ptr;
992 // Define operator[] for shared_ptr<T[]> and shared_ptr<T[N]>.
993 template<typename _Tp, _Lock_policy _Lp>
994 class __shared_ptr_access<_Tp, _Lp, true, false>
996 public:
997 using element_type = typename remove_extent<_Tp>::type;
999 #if __cplusplus <= 201402L
1000 [[__deprecated__("shared_ptr<T[]>::operator* is absent from C++17")]]
1001 element_type&
1002 operator*() const noexcept
1004 __glibcxx_assert(_M_get() != nullptr);
1005 return *_M_get();
1008 [[__deprecated__("shared_ptr<T[]>::operator-> is absent from C++17")]]
1009 element_type*
1010 operator->() const noexcept
1012 _GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr);
1013 return _M_get();
1015 #endif
1017 element_type&
1018 operator[](ptrdiff_t __i) const
1020 __glibcxx_assert(_M_get() != nullptr);
1021 __glibcxx_assert(!extent<_Tp>::value || __i < extent<_Tp>::value);
1022 return _M_get()[__i];
1025 private:
1026 element_type*
1027 _M_get() const noexcept
1028 { return static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get(); }
1031 template<typename _Tp, _Lock_policy _Lp>
1032 class __shared_ptr
1033 : public __shared_ptr_access<_Tp, _Lp>
1035 public:
1036 using element_type = typename remove_extent<_Tp>::type;
1038 private:
1039 // Constraint for taking ownership of a pointer of type _Yp*:
1040 template<typename _Yp>
1041 using _SafeConv
1042 = typename enable_if<__sp_is_constructible<_Tp, _Yp>::value>::type;
1044 // Constraint for construction from shared_ptr and weak_ptr:
1045 template<typename _Yp, typename _Res = void>
1046 using _Compatible = typename
1047 enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type;
1049 // Constraint for assignment from shared_ptr and weak_ptr:
1050 template<typename _Yp>
1051 using _Assignable = _Compatible<_Yp, __shared_ptr&>;
1053 // Constraint for construction from unique_ptr:
1054 template<typename _Yp, typename _Del, typename _Res = void,
1055 typename _Ptr = typename unique_ptr<_Yp, _Del>::pointer>
1056 using _UniqCompatible = typename enable_if<__and_<
1057 __sp_compatible_with<_Yp*, _Tp*>, is_convertible<_Ptr, element_type*>
1058 >::value, _Res>::type;
1060 // Constraint for assignment from unique_ptr:
1061 template<typename _Yp, typename _Del>
1062 using _UniqAssignable = _UniqCompatible<_Yp, _Del, __shared_ptr&>;
1064 public:
1066 #if __cplusplus > 201402L
1067 using weak_type = __weak_ptr<_Tp, _Lp>;
1068 #endif
1070 constexpr __shared_ptr() noexcept
1071 : _M_ptr(0), _M_refcount()
1074 template<typename _Yp, typename = _SafeConv<_Yp>>
1075 explicit
1076 __shared_ptr(_Yp* __p)
1077 : _M_ptr(__p), _M_refcount(__p, typename is_array<_Tp>::type())
1079 static_assert( !is_void<_Yp>::value, "incomplete type" );
1080 static_assert( sizeof(_Yp) > 0, "incomplete type" );
1081 _M_enable_shared_from_this_with(__p);
1084 template<typename _Yp, typename _Deleter, typename = _SafeConv<_Yp>>
1085 __shared_ptr(_Yp* __p, _Deleter __d)
1086 : _M_ptr(__p), _M_refcount(__p, __d)
1088 static_assert(__is_callable<_Deleter&(_Yp*&)>::value,
1089 "deleter expression d(p) is well-formed");
1090 _M_enable_shared_from_this_with(__p);
1093 template<typename _Yp, typename _Deleter, typename _Alloc,
1094 typename = _SafeConv<_Yp>>
1095 __shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a)
1096 : _M_ptr(__p), _M_refcount(__p, __d, std::move(__a))
1098 static_assert(__is_callable<_Deleter&(_Yp*&)>::value,
1099 "deleter expression d(p) is well-formed");
1100 _M_enable_shared_from_this_with(__p);
1103 template<typename _Deleter>
1104 __shared_ptr(nullptr_t __p, _Deleter __d)
1105 : _M_ptr(0), _M_refcount(__p, __d)
1108 template<typename _Deleter, typename _Alloc>
1109 __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
1110 : _M_ptr(0), _M_refcount(__p, __d, std::move(__a))
1113 template<typename _Yp>
1114 __shared_ptr(const __shared_ptr<_Yp, _Lp>& __r,
1115 element_type* __p) noexcept
1116 : _M_ptr(__p), _M_refcount(__r._M_refcount) // never throws
1119 __shared_ptr(const __shared_ptr&) noexcept = default;
1120 __shared_ptr& operator=(const __shared_ptr&) noexcept = default;
1121 ~__shared_ptr() = default;
1123 template<typename _Yp, typename = _Compatible<_Yp>>
1124 __shared_ptr(const __shared_ptr<_Yp, _Lp>& __r) noexcept
1125 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1128 __shared_ptr(__shared_ptr&& __r) noexcept
1129 : _M_ptr(__r._M_ptr), _M_refcount()
1131 _M_refcount._M_swap(__r._M_refcount);
1132 __r._M_ptr = 0;
1135 template<typename _Yp, typename = _Compatible<_Yp>>
1136 __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r) noexcept
1137 : _M_ptr(__r._M_ptr), _M_refcount()
1139 _M_refcount._M_swap(__r._M_refcount);
1140 __r._M_ptr = 0;
1143 template<typename _Yp, typename = _Compatible<_Yp>>
1144 explicit __shared_ptr(const __weak_ptr<_Yp, _Lp>& __r)
1145 : _M_refcount(__r._M_refcount) // may throw
1147 // It is now safe to copy __r._M_ptr, as
1148 // _M_refcount(__r._M_refcount) did not throw.
1149 _M_ptr = __r._M_ptr;
1152 // If an exception is thrown this constructor has no effect.
1153 template<typename _Yp, typename _Del,
1154 typename = _UniqCompatible<_Yp, _Del>>
1155 __shared_ptr(unique_ptr<_Yp, _Del>&& __r)
1156 : _M_ptr(__r.get()), _M_refcount()
1158 auto __raw = _S_raw_ptr(__r.get());
1159 _M_refcount = __shared_count<_Lp>(std::move(__r));
1160 _M_enable_shared_from_this_with(__raw);
1163 #if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED
1164 protected:
1165 // If an exception is thrown this constructor has no effect.
1166 template<typename _Tp1, typename _Del,
1167 typename enable_if<__and_<
1168 __not_<is_array<_Tp>>, is_array<_Tp1>,
1169 is_convertible<typename unique_ptr<_Tp1, _Del>::pointer, _Tp*>
1170 >::value, bool>::type = true>
1171 __shared_ptr(unique_ptr<_Tp1, _Del>&& __r, __sp_array_delete)
1172 : _M_ptr(__r.get()), _M_refcount()
1174 auto __raw = _S_raw_ptr(__r.get());
1175 _M_refcount = __shared_count<_Lp>(std::move(__r));
1176 _M_enable_shared_from_this_with(__raw);
1178 public:
1179 #endif
1181 #if _GLIBCXX_USE_DEPRECATED
1182 // Postcondition: use_count() == 1 and __r.get() == 0
1183 template<typename _Yp, typename = _Compatible<_Yp>>
1184 __shared_ptr(auto_ptr<_Yp>&& __r);
1185 #endif
1187 constexpr __shared_ptr(nullptr_t) noexcept : __shared_ptr() { }
1189 template<typename _Yp>
1190 _Assignable<_Yp>
1191 operator=(const __shared_ptr<_Yp, _Lp>& __r) noexcept
1193 _M_ptr = __r._M_ptr;
1194 _M_refcount = __r._M_refcount; // __shared_count::op= doesn't throw
1195 return *this;
1198 #if _GLIBCXX_USE_DEPRECATED
1199 template<typename _Yp>
1200 _Assignable<_Yp>
1201 operator=(auto_ptr<_Yp>&& __r)
1203 __shared_ptr(std::move(__r)).swap(*this);
1204 return *this;
1206 #endif
1208 __shared_ptr&
1209 operator=(__shared_ptr&& __r) noexcept
1211 __shared_ptr(std::move(__r)).swap(*this);
1212 return *this;
1215 template<class _Yp>
1216 _Assignable<_Yp>
1217 operator=(__shared_ptr<_Yp, _Lp>&& __r) noexcept
1219 __shared_ptr(std::move(__r)).swap(*this);
1220 return *this;
1223 template<typename _Yp, typename _Del>
1224 _UniqAssignable<_Yp, _Del>
1225 operator=(unique_ptr<_Yp, _Del>&& __r)
1227 __shared_ptr(std::move(__r)).swap(*this);
1228 return *this;
1231 void
1232 reset() noexcept
1233 { __shared_ptr().swap(*this); }
1235 template<typename _Yp>
1236 _SafeConv<_Yp>
1237 reset(_Yp* __p) // _Yp must be complete.
1239 // Catch self-reset errors.
1240 __glibcxx_assert(__p == 0 || __p != _M_ptr);
1241 __shared_ptr(__p).swap(*this);
1244 template<typename _Yp, typename _Deleter>
1245 _SafeConv<_Yp>
1246 reset(_Yp* __p, _Deleter __d)
1247 { __shared_ptr(__p, __d).swap(*this); }
1249 template<typename _Yp, typename _Deleter, typename _Alloc>
1250 _SafeConv<_Yp>
1251 reset(_Yp* __p, _Deleter __d, _Alloc __a)
1252 { __shared_ptr(__p, __d, std::move(__a)).swap(*this); }
1254 element_type*
1255 get() const noexcept
1256 { return _M_ptr; }
1258 explicit operator bool() const // never throws
1259 { return _M_ptr == 0 ? false : true; }
1261 bool
1262 unique() const noexcept
1263 { return _M_refcount._M_unique(); }
1265 long
1266 use_count() const noexcept
1267 { return _M_refcount._M_get_use_count(); }
1269 void
1270 swap(__shared_ptr<_Tp, _Lp>& __other) noexcept
1272 std::swap(_M_ptr, __other._M_ptr);
1273 _M_refcount._M_swap(__other._M_refcount);
1276 template<typename _Tp1>
1277 bool
1278 owner_before(__shared_ptr<_Tp1, _Lp> const& __rhs) const
1279 { return _M_refcount._M_less(__rhs._M_refcount); }
1281 template<typename _Tp1>
1282 bool
1283 owner_before(__weak_ptr<_Tp1, _Lp> const& __rhs) const
1284 { return _M_refcount._M_less(__rhs._M_refcount); }
1286 #if __cpp_rtti
1287 protected:
1288 // This constructor is non-standard, it is used by allocate_shared.
1289 template<typename _Alloc, typename... _Args>
1290 __shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a,
1291 _Args&&... __args)
1292 : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a,
1293 std::forward<_Args>(__args)...)
1295 // _M_ptr needs to point to the newly constructed object.
1296 // This relies on _Sp_counted_ptr_inplace::_M_get_deleter.
1297 void* __p = _M_refcount._M_get_deleter(typeid(__tag));
1298 _M_ptr = static_cast<_Tp*>(__p);
1299 _M_enable_shared_from_this_with(_M_ptr);
1301 #else
1302 template<typename _Alloc>
1303 struct _Deleter
1305 void operator()(typename _Alloc::value_type* __ptr)
1307 __allocated_ptr<_Alloc> __guard{ _M_alloc, __ptr };
1308 allocator_traits<_Alloc>::destroy(_M_alloc, __guard.get());
1310 _Alloc _M_alloc;
1313 template<typename _Alloc, typename... _Args>
1314 __shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a,
1315 _Args&&... __args)
1316 : _M_ptr(), _M_refcount()
1318 typedef typename allocator_traits<_Alloc>::template
1319 rebind_traits<typename std::remove_cv<_Tp>::type> __traits;
1320 _Deleter<typename __traits::allocator_type> __del = { __a };
1321 auto __guard = std::__allocate_guarded(__del._M_alloc);
1322 auto __ptr = __guard.get();
1323 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1324 // 2070. allocate_shared should use allocator_traits<A>::construct
1325 __traits::construct(__del._M_alloc, __ptr,
1326 std::forward<_Args>(__args)...);
1327 __guard = nullptr;
1328 __shared_count<_Lp> __count(__ptr, __del, __del._M_alloc);
1329 _M_refcount._M_swap(__count);
1330 _M_ptr = __ptr;
1331 _M_enable_shared_from_this_with(_M_ptr);
1333 #endif
1335 template<typename _Tp1, _Lock_policy _Lp1, typename _Alloc,
1336 typename... _Args>
1337 friend __shared_ptr<_Tp1, _Lp1>
1338 __allocate_shared(const _Alloc& __a, _Args&&... __args);
1340 // This constructor is used by __weak_ptr::lock() and
1341 // shared_ptr::shared_ptr(const weak_ptr&, std::nothrow_t).
1342 __shared_ptr(const __weak_ptr<_Tp, _Lp>& __r, std::nothrow_t)
1343 : _M_refcount(__r._M_refcount, std::nothrow)
1345 _M_ptr = _M_refcount._M_get_use_count() ? __r._M_ptr : nullptr;
1348 friend class __weak_ptr<_Tp, _Lp>;
1350 private:
1352 template<typename _Yp>
1353 using __esft_base_t = decltype(__enable_shared_from_this_base(
1354 std::declval<const __shared_count<_Lp>&>(),
1355 std::declval<_Yp*>()));
1357 // Detect an accessible and unambiguous enable_shared_from_this base.
1358 template<typename _Yp, typename = void>
1359 struct __has_esft_base
1360 : false_type { };
1362 template<typename _Yp>
1363 struct __has_esft_base<_Yp, __void_t<__esft_base_t<_Yp>>>
1364 : __not_<is_array<_Tp>> { }; // No enable shared_from_this for arrays
1366 template<typename _Yp>
1367 typename enable_if<__has_esft_base<_Yp>::value>::type
1368 _M_enable_shared_from_this_with(const _Yp* __p) noexcept
1370 if (auto __base = __enable_shared_from_this_base(_M_refcount, __p))
1371 __base->_M_weak_assign(const_cast<_Yp*>(__p), _M_refcount);
1374 template<typename _Yp>
1375 typename enable_if<!__has_esft_base<_Yp>::value>::type
1376 _M_enable_shared_from_this_with(const _Yp*) noexcept
1379 void*
1380 _M_get_deleter(const std::type_info& __ti) const noexcept
1381 { return _M_refcount._M_get_deleter(__ti); }
1383 template<typename _Tp1>
1384 static _Tp1*
1385 _S_raw_ptr(_Tp1* __ptr)
1386 { return __ptr; }
1388 template<typename _Tp1>
1389 static auto
1390 _S_raw_ptr(_Tp1 __ptr) -> decltype(std::__addressof(*__ptr))
1391 { return std::__addressof(*__ptr); }
1393 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
1394 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
1396 template<typename _Del, typename _Tp1, _Lock_policy _Lp1>
1397 friend _Del* get_deleter(const __shared_ptr<_Tp1, _Lp1>&) noexcept;
1399 element_type* _M_ptr; // Contained pointer.
1400 __shared_count<_Lp> _M_refcount; // Reference counter.
1404 // 20.7.2.2.7 shared_ptr comparisons
1405 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1406 inline bool
1407 operator==(const __shared_ptr<_Tp1, _Lp>& __a,
1408 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1409 { return __a.get() == __b.get(); }
1411 template<typename _Tp, _Lock_policy _Lp>
1412 inline bool
1413 operator==(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1414 { return !__a; }
1416 template<typename _Tp, _Lock_policy _Lp>
1417 inline bool
1418 operator==(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1419 { return !__a; }
1421 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1422 inline bool
1423 operator!=(const __shared_ptr<_Tp1, _Lp>& __a,
1424 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1425 { return __a.get() != __b.get(); }
1427 template<typename _Tp, _Lock_policy _Lp>
1428 inline bool
1429 operator!=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1430 { return (bool)__a; }
1432 template<typename _Tp, _Lock_policy _Lp>
1433 inline bool
1434 operator!=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1435 { return (bool)__a; }
1437 template<typename _Tp, typename _Up, _Lock_policy _Lp>
1438 inline bool
1439 operator<(const __shared_ptr<_Tp, _Lp>& __a,
1440 const __shared_ptr<_Up, _Lp>& __b) noexcept
1442 using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type;
1443 using _Up_elt = typename __shared_ptr<_Up, _Lp>::element_type;
1444 using _Vp = typename common_type<_Tp_elt*, _Up_elt*>::type;
1445 return less<_Vp>()(__a.get(), __b.get());
1448 template<typename _Tp, _Lock_policy _Lp>
1449 inline bool
1450 operator<(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1452 using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type;
1453 return less<_Tp_elt*>()(__a.get(), nullptr);
1456 template<typename _Tp, _Lock_policy _Lp>
1457 inline bool
1458 operator<(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1460 using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type;
1461 return less<_Tp_elt*>()(nullptr, __a.get());
1464 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1465 inline bool
1466 operator<=(const __shared_ptr<_Tp1, _Lp>& __a,
1467 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1468 { return !(__b < __a); }
1470 template<typename _Tp, _Lock_policy _Lp>
1471 inline bool
1472 operator<=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1473 { return !(nullptr < __a); }
1475 template<typename _Tp, _Lock_policy _Lp>
1476 inline bool
1477 operator<=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1478 { return !(__a < nullptr); }
1480 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1481 inline bool
1482 operator>(const __shared_ptr<_Tp1, _Lp>& __a,
1483 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1484 { return (__b < __a); }
1486 template<typename _Tp, _Lock_policy _Lp>
1487 inline bool
1488 operator>(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1489 { return nullptr < __a; }
1491 template<typename _Tp, _Lock_policy _Lp>
1492 inline bool
1493 operator>(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1494 { return __a < nullptr; }
1496 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1497 inline bool
1498 operator>=(const __shared_ptr<_Tp1, _Lp>& __a,
1499 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1500 { return !(__a < __b); }
1502 template<typename _Tp, _Lock_policy _Lp>
1503 inline bool
1504 operator>=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1505 { return !(__a < nullptr); }
1507 template<typename _Tp, _Lock_policy _Lp>
1508 inline bool
1509 operator>=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1510 { return !(nullptr < __a); }
1512 template<typename _Sp>
1513 struct _Sp_less : public binary_function<_Sp, _Sp, bool>
1515 bool
1516 operator()(const _Sp& __lhs, const _Sp& __rhs) const noexcept
1518 typedef typename _Sp::element_type element_type;
1519 return std::less<element_type*>()(__lhs.get(), __rhs.get());
1523 template<typename _Tp, _Lock_policy _Lp>
1524 struct less<__shared_ptr<_Tp, _Lp>>
1525 : public _Sp_less<__shared_ptr<_Tp, _Lp>>
1526 { };
1528 // 20.7.2.2.8 shared_ptr specialized algorithms.
1529 template<typename _Tp, _Lock_policy _Lp>
1530 inline void
1531 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b) noexcept
1532 { __a.swap(__b); }
1534 // 20.7.2.2.9 shared_ptr casts
1536 // The seemingly equivalent code:
1537 // shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))
1538 // will eventually result in undefined behaviour, attempting to
1539 // delete the same object twice.
1540 /// static_pointer_cast
1541 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1542 inline __shared_ptr<_Tp, _Lp>
1543 static_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1545 using _Sp = __shared_ptr<_Tp, _Lp>;
1546 return _Sp(__r, static_cast<typename _Sp::element_type*>(__r.get()));
1549 // The seemingly equivalent code:
1550 // shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))
1551 // will eventually result in undefined behaviour, attempting to
1552 // delete the same object twice.
1553 /// const_pointer_cast
1554 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1555 inline __shared_ptr<_Tp, _Lp>
1556 const_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1558 using _Sp = __shared_ptr<_Tp, _Lp>;
1559 return _Sp(__r, const_cast<typename _Sp::element_type*>(__r.get()));
1562 // The seemingly equivalent code:
1563 // shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))
1564 // will eventually result in undefined behaviour, attempting to
1565 // delete the same object twice.
1566 /// dynamic_pointer_cast
1567 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1568 inline __shared_ptr<_Tp, _Lp>
1569 dynamic_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1571 using _Sp = __shared_ptr<_Tp, _Lp>;
1572 if (auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get()))
1573 return _Sp(__r, __p);
1574 return _Sp();
1577 #if __cplusplus > 201402L
1578 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1579 inline __shared_ptr<_Tp, _Lp>
1580 reinterpret_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1582 using _Sp = __shared_ptr<_Tp, _Lp>;
1583 return _Sp(__r, reinterpret_cast<typename _Sp::element_type*>(__r.get()));
1585 #endif
1587 template<typename _Tp, _Lock_policy _Lp>
1588 class __weak_ptr
1590 template<typename _Yp, typename _Res = void>
1591 using _Compatible = typename
1592 enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type;
1594 // Constraint for assignment from shared_ptr and weak_ptr:
1595 template<typename _Yp>
1596 using _Assignable = _Compatible<_Yp, __weak_ptr&>;
1598 public:
1599 using element_type = typename remove_extent<_Tp>::type;
1601 constexpr __weak_ptr() noexcept
1602 : _M_ptr(nullptr), _M_refcount()
1605 __weak_ptr(const __weak_ptr&) noexcept = default;
1607 ~__weak_ptr() = default;
1609 // The "obvious" converting constructor implementation:
1611 // template<typename _Tp1>
1612 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
1613 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
1614 // { }
1616 // has a serious problem.
1618 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
1619 // conversion may require access to *__r._M_ptr (virtual inheritance).
1621 // It is not possible to avoid spurious access violations since
1622 // in multithreaded programs __r._M_ptr may be invalidated at any point.
1623 template<typename _Yp, typename = _Compatible<_Yp>>
1624 __weak_ptr(const __weak_ptr<_Yp, _Lp>& __r) noexcept
1625 : _M_refcount(__r._M_refcount)
1626 { _M_ptr = __r.lock().get(); }
1628 template<typename _Yp, typename = _Compatible<_Yp>>
1629 __weak_ptr(const __shared_ptr<_Yp, _Lp>& __r) noexcept
1630 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1633 __weak_ptr(__weak_ptr&& __r) noexcept
1634 : _M_ptr(__r._M_ptr), _M_refcount(std::move(__r._M_refcount))
1635 { __r._M_ptr = nullptr; }
1637 template<typename _Yp, typename = _Compatible<_Yp>>
1638 __weak_ptr(__weak_ptr<_Yp, _Lp>&& __r) noexcept
1639 : _M_ptr(__r.lock().get()), _M_refcount(std::move(__r._M_refcount))
1640 { __r._M_ptr = nullptr; }
1642 __weak_ptr&
1643 operator=(const __weak_ptr& __r) noexcept = default;
1645 template<typename _Yp>
1646 _Assignable<_Yp>
1647 operator=(const __weak_ptr<_Yp, _Lp>& __r) noexcept
1649 _M_ptr = __r.lock().get();
1650 _M_refcount = __r._M_refcount;
1651 return *this;
1654 template<typename _Yp>
1655 _Assignable<_Yp>
1656 operator=(const __shared_ptr<_Yp, _Lp>& __r) noexcept
1658 _M_ptr = __r._M_ptr;
1659 _M_refcount = __r._M_refcount;
1660 return *this;
1663 __weak_ptr&
1664 operator=(__weak_ptr&& __r) noexcept
1666 _M_ptr = __r._M_ptr;
1667 _M_refcount = std::move(__r._M_refcount);
1668 __r._M_ptr = nullptr;
1669 return *this;
1672 template<typename _Yp>
1673 _Assignable<_Yp>
1674 operator=(__weak_ptr<_Yp, _Lp>&& __r) noexcept
1676 _M_ptr = __r.lock().get();
1677 _M_refcount = std::move(__r._M_refcount);
1678 __r._M_ptr = nullptr;
1679 return *this;
1682 __shared_ptr<_Tp, _Lp>
1683 lock() const noexcept
1684 { return __shared_ptr<element_type, _Lp>(*this, std::nothrow); }
1686 long
1687 use_count() const noexcept
1688 { return _M_refcount._M_get_use_count(); }
1690 bool
1691 expired() const noexcept
1692 { return _M_refcount._M_get_use_count() == 0; }
1694 template<typename _Tp1>
1695 bool
1696 owner_before(const __shared_ptr<_Tp1, _Lp>& __rhs) const
1697 { return _M_refcount._M_less(__rhs._M_refcount); }
1699 template<typename _Tp1>
1700 bool
1701 owner_before(const __weak_ptr<_Tp1, _Lp>& __rhs) const
1702 { return _M_refcount._M_less(__rhs._M_refcount); }
1704 void
1705 reset() noexcept
1706 { __weak_ptr().swap(*this); }
1708 void
1709 swap(__weak_ptr& __s) noexcept
1711 std::swap(_M_ptr, __s._M_ptr);
1712 _M_refcount._M_swap(__s._M_refcount);
1715 private:
1716 // Used by __enable_shared_from_this.
1717 void
1718 _M_assign(_Tp* __ptr, const __shared_count<_Lp>& __refcount) noexcept
1720 if (use_count() == 0)
1722 _M_ptr = __ptr;
1723 _M_refcount = __refcount;
1727 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
1728 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
1729 friend class __enable_shared_from_this<_Tp, _Lp>;
1730 friend class enable_shared_from_this<_Tp>;
1732 element_type* _M_ptr; // Contained pointer.
1733 __weak_count<_Lp> _M_refcount; // Reference counter.
1736 // 20.7.2.3.6 weak_ptr specialized algorithms.
1737 template<typename _Tp, _Lock_policy _Lp>
1738 inline void
1739 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b) noexcept
1740 { __a.swap(__b); }
1742 template<typename _Tp, typename _Tp1>
1743 struct _Sp_owner_less : public binary_function<_Tp, _Tp, bool>
1745 bool
1746 operator()(const _Tp& __lhs, const _Tp& __rhs) const
1747 { return __lhs.owner_before(__rhs); }
1749 bool
1750 operator()(const _Tp& __lhs, const _Tp1& __rhs) const
1751 { return __lhs.owner_before(__rhs); }
1753 bool
1754 operator()(const _Tp1& __lhs, const _Tp& __rhs) const
1755 { return __lhs.owner_before(__rhs); }
1758 template<>
1759 struct _Sp_owner_less<void, void>
1761 template<typename _Tp, typename _Up>
1762 auto
1763 operator()(const _Tp& __lhs, const _Up& __rhs) const
1764 -> decltype(__lhs.owner_before(__rhs))
1765 { return __lhs.owner_before(__rhs); }
1767 using is_transparent = void;
1770 template<typename _Tp, _Lock_policy _Lp>
1771 struct owner_less<__shared_ptr<_Tp, _Lp>>
1772 : public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
1773 { };
1775 template<typename _Tp, _Lock_policy _Lp>
1776 struct owner_less<__weak_ptr<_Tp, _Lp>>
1777 : public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
1778 { };
1781 template<typename _Tp, _Lock_policy _Lp>
1782 class __enable_shared_from_this
1784 protected:
1785 constexpr __enable_shared_from_this() noexcept { }
1787 __enable_shared_from_this(const __enable_shared_from_this&) noexcept { }
1789 __enable_shared_from_this&
1790 operator=(const __enable_shared_from_this&) noexcept
1791 { return *this; }
1793 ~__enable_shared_from_this() { }
1795 public:
1796 __shared_ptr<_Tp, _Lp>
1797 shared_from_this()
1798 { return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
1800 __shared_ptr<const _Tp, _Lp>
1801 shared_from_this() const
1802 { return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
1804 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
1805 __weak_ptr<_Tp, _Lp>
1806 weak_from_this() noexcept
1807 { return this->_M_weak_this; }
1809 __weak_ptr<const _Tp, _Lp>
1810 weak_from_this() const noexcept
1811 { return this->_M_weak_this; }
1812 #endif
1814 private:
1815 template<typename _Tp1>
1816 void
1817 _M_weak_assign(_Tp1* __p, const __shared_count<_Lp>& __n) const noexcept
1818 { _M_weak_this._M_assign(__p, __n); }
1820 friend const __enable_shared_from_this*
1821 __enable_shared_from_this_base(const __shared_count<_Lp>&,
1822 const __enable_shared_from_this* __p)
1823 { return __p; }
1825 template<typename, _Lock_policy>
1826 friend class __shared_ptr;
1828 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
1831 template<typename _Tp, _Lock_policy _Lp, typename _Alloc, typename... _Args>
1832 inline __shared_ptr<_Tp, _Lp>
1833 __allocate_shared(const _Alloc& __a, _Args&&... __args)
1835 return __shared_ptr<_Tp, _Lp>(_Sp_make_shared_tag(), __a,
1836 std::forward<_Args>(__args)...);
1839 template<typename _Tp, _Lock_policy _Lp, typename... _Args>
1840 inline __shared_ptr<_Tp, _Lp>
1841 __make_shared(_Args&&... __args)
1843 typedef typename std::remove_const<_Tp>::type _Tp_nc;
1844 return std::__allocate_shared<_Tp, _Lp>(std::allocator<_Tp_nc>(),
1845 std::forward<_Args>(__args)...);
1848 /// std::hash specialization for __shared_ptr.
1849 template<typename _Tp, _Lock_policy _Lp>
1850 struct hash<__shared_ptr<_Tp, _Lp>>
1851 : public __hash_base<size_t, __shared_ptr<_Tp, _Lp>>
1853 size_t
1854 operator()(const __shared_ptr<_Tp, _Lp>& __s) const noexcept
1856 return hash<typename __shared_ptr<_Tp, _Lp>::element_type*>()(
1857 __s.get());
1861 _GLIBCXX_END_NAMESPACE_VERSION
1862 } // namespace
1864 #endif // _SHARED_PTR_BASE_H