PR libstdc++/58839
[official-gcc.git] / libstdc++-v3 / include / bits / shared_ptr_base.h
blob91b63677b73816e22178090167c7130434c1f44d
1 // shared_ptr and weak_ptr implementation details -*- C++ -*-
3 // Copyright (C) 2007-2013 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 #include <ext/aligned_buffer.h>
54 namespace std _GLIBCXX_VISIBILITY(default)
56 _GLIBCXX_BEGIN_NAMESPACE_VERSION
58 #if _GLIBCXX_USE_DEPRECATED
59 template<typename> class auto_ptr;
60 #endif
62 /**
63 * @brief Exception possibly thrown by @c shared_ptr.
64 * @ingroup exceptions
66 class bad_weak_ptr : public std::exception
68 public:
69 virtual char const*
70 what() const noexcept;
72 virtual ~bad_weak_ptr() noexcept;
75 // Substitute for bad_weak_ptr object in the case of -fno-exceptions.
76 inline void
77 __throw_bad_weak_ptr()
78 { _GLIBCXX_THROW_OR_ABORT(bad_weak_ptr()); }
80 using __gnu_cxx::_Lock_policy;
81 using __gnu_cxx::__default_lock_policy;
82 using __gnu_cxx::_S_single;
83 using __gnu_cxx::_S_mutex;
84 using __gnu_cxx::_S_atomic;
86 // Empty helper class except when the template argument is _S_mutex.
87 template<_Lock_policy _Lp>
88 class _Mutex_base
90 protected:
91 // The atomic policy uses fully-fenced builtins, single doesn't care.
92 enum { _S_need_barriers = 0 };
95 template<>
96 class _Mutex_base<_S_mutex>
97 : public __gnu_cxx::__mutex
99 protected:
100 // This policy is used when atomic builtins are not available.
101 // The replacement atomic operations might not have the necessary
102 // memory barriers.
103 enum { _S_need_barriers = 1 };
106 template<_Lock_policy _Lp = __default_lock_policy>
107 class _Sp_counted_base
108 : public _Mutex_base<_Lp>
110 public:
111 _Sp_counted_base() noexcept
112 : _M_use_count(1), _M_weak_count(1) { }
114 virtual
115 ~_Sp_counted_base() noexcept
118 // Called when _M_use_count drops to zero, to release the resources
119 // managed by *this.
120 virtual void
121 _M_dispose() noexcept = 0;
123 // Called when _M_weak_count drops to zero.
124 virtual void
125 _M_destroy() noexcept
126 { delete this; }
128 virtual void*
129 _M_get_deleter(const std::type_info&) noexcept = 0;
131 void
132 _M_add_ref_copy()
133 { __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); }
135 void
136 _M_add_ref_lock();
138 void
139 _M_release() noexcept
141 // Be race-detector-friendly. For more info see bits/c++config.
142 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count);
143 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
145 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count);
146 _M_dispose();
147 // There must be a memory barrier between dispose() and destroy()
148 // to ensure that the effects of dispose() are observed in the
149 // thread that runs destroy().
150 // See http://gcc.gnu.org/ml/libstdc++/2005-11/msg00136.html
151 if (_Mutex_base<_Lp>::_S_need_barriers)
153 _GLIBCXX_READ_MEM_BARRIER;
154 _GLIBCXX_WRITE_MEM_BARRIER;
157 // Be race-detector-friendly. For more info see bits/c++config.
158 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
159 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count,
160 -1) == 1)
162 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
163 _M_destroy();
168 void
169 _M_weak_add_ref() noexcept
170 { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); }
172 void
173 _M_weak_release() noexcept
175 // Be race-detector-friendly. For more info see bits/c++config.
176 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
177 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1)
179 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
180 if (_Mutex_base<_Lp>::_S_need_barriers)
182 // See _M_release(),
183 // destroy() must observe results of dispose()
184 _GLIBCXX_READ_MEM_BARRIER;
185 _GLIBCXX_WRITE_MEM_BARRIER;
187 _M_destroy();
191 long
192 _M_get_use_count() const noexcept
194 // No memory barrier is used here so there is no synchronization
195 // with other threads.
196 return __atomic_load_n(&_M_use_count, __ATOMIC_RELAXED);
199 private:
200 _Sp_counted_base(_Sp_counted_base const&) = delete;
201 _Sp_counted_base& operator=(_Sp_counted_base const&) = delete;
203 _Atomic_word _M_use_count; // #shared
204 _Atomic_word _M_weak_count; // #weak + (#shared != 0)
207 template<>
208 inline void
209 _Sp_counted_base<_S_single>::
210 _M_add_ref_lock()
212 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
214 _M_use_count = 0;
215 __throw_bad_weak_ptr();
219 template<>
220 inline void
221 _Sp_counted_base<_S_mutex>::
222 _M_add_ref_lock()
224 __gnu_cxx::__scoped_lock sentry(*this);
225 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
227 _M_use_count = 0;
228 __throw_bad_weak_ptr();
232 template<>
233 inline void
234 _Sp_counted_base<_S_atomic>::
235 _M_add_ref_lock()
237 // Perform lock-free add-if-not-zero operation.
238 _Atomic_word __count = _M_use_count;
241 if (__count == 0)
242 __throw_bad_weak_ptr();
243 // Replace the current counter value with the old value + 1, as
244 // long as it's not changed meanwhile.
246 while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1,
247 true, __ATOMIC_ACQ_REL,
248 __ATOMIC_RELAXED));
252 // Forward declarations.
253 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
254 class __shared_ptr;
256 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
257 class __weak_ptr;
259 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
260 class __enable_shared_from_this;
262 template<typename _Tp>
263 class shared_ptr;
265 template<typename _Tp>
266 class weak_ptr;
268 template<typename _Tp>
269 struct owner_less;
271 template<typename _Tp>
272 class enable_shared_from_this;
274 template<_Lock_policy _Lp = __default_lock_policy>
275 class __weak_count;
277 template<_Lock_policy _Lp = __default_lock_policy>
278 class __shared_count;
281 // Counted ptr with no deleter or allocator support
282 template<typename _Ptr, _Lock_policy _Lp>
283 class _Sp_counted_ptr final : public _Sp_counted_base<_Lp>
285 public:
286 explicit
287 _Sp_counted_ptr(_Ptr __p) noexcept
288 : _M_ptr(__p) { }
290 virtual void
291 _M_dispose() noexcept
292 { delete _M_ptr; }
294 virtual void
295 _M_destroy() noexcept
296 { delete this; }
298 virtual void*
299 _M_get_deleter(const std::type_info&) noexcept
300 { return nullptr; }
302 _Sp_counted_ptr(const _Sp_counted_ptr&) = delete;
303 _Sp_counted_ptr& operator=(const _Sp_counted_ptr&) = delete;
305 private:
306 _Ptr _M_ptr;
309 template<>
310 inline void
311 _Sp_counted_ptr<nullptr_t, _S_single>::_M_dispose() noexcept { }
313 template<>
314 inline void
315 _Sp_counted_ptr<nullptr_t, _S_mutex>::_M_dispose() noexcept { }
317 template<>
318 inline void
319 _Sp_counted_ptr<nullptr_t, _S_atomic>::_M_dispose() noexcept { }
321 template<int _Nm, typename _Tp,
322 bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
323 struct _Sp_ebo_helper;
325 /// Specialization using EBO.
326 template<int _Nm, typename _Tp>
327 struct _Sp_ebo_helper<_Nm, _Tp, true> : private _Tp
329 explicit _Sp_ebo_helper(const _Tp& __tp) : _Tp(__tp) { }
331 static _Tp&
332 _S_get(_Sp_ebo_helper& __eboh) { return static_cast<_Tp&>(__eboh); }
335 /// Specialization not using EBO.
336 template<int _Nm, typename _Tp>
337 struct _Sp_ebo_helper<_Nm, _Tp, false>
339 explicit _Sp_ebo_helper(const _Tp& __tp) : _M_tp(__tp) { }
341 static _Tp&
342 _S_get(_Sp_ebo_helper& __eboh)
343 { return __eboh._M_tp; }
345 private:
346 _Tp _M_tp;
349 // Support for custom deleter and/or allocator
350 template<typename _Ptr, typename _Deleter, typename _Alloc, _Lock_policy _Lp>
351 class _Sp_counted_deleter final : public _Sp_counted_base<_Lp>
353 class _Impl : _Sp_ebo_helper<0, _Deleter>, _Sp_ebo_helper<1, _Alloc>
355 typedef _Sp_ebo_helper<0, _Deleter> _Del_base;
356 typedef _Sp_ebo_helper<1, _Alloc> _Alloc_base;
358 public:
359 _Impl(_Ptr __p, _Deleter __d, const _Alloc& __a) noexcept
360 : _M_ptr(__p), _Del_base(__d), _Alloc_base(__a)
363 _Deleter& _M_del() noexcept { return _Del_base::_S_get(*this); }
364 _Alloc& _M_alloc() noexcept { return _Alloc_base::_S_get(*this); }
366 _Ptr _M_ptr;
369 public:
370 // __d(__p) must not throw.
371 _Sp_counted_deleter(_Ptr __p, _Deleter __d) noexcept
372 : _M_impl(__p, __d, _Alloc()) { }
374 // __d(__p) must not throw.
375 _Sp_counted_deleter(_Ptr __p, _Deleter __d, const _Alloc& __a) noexcept
376 : _M_impl(__p, __d, __a) { }
378 ~_Sp_counted_deleter() noexcept { }
380 virtual void
381 _M_dispose() noexcept
382 { _M_impl._M_del()(_M_impl._M_ptr); }
384 virtual void
385 _M_destroy() noexcept
387 typedef typename allocator_traits<_Alloc>::template
388 rebind_traits<_Sp_counted_deleter> _Alloc_traits;
389 typename _Alloc_traits::allocator_type __a(_M_impl._M_alloc());
390 _Alloc_traits::destroy(__a, this);
391 _Alloc_traits::deallocate(__a, this, 1);
394 virtual void*
395 _M_get_deleter(const std::type_info& __ti) noexcept
397 #ifdef __GXX_RTTI
398 return __ti == typeid(_Deleter) ? &_M_impl._M_del() : nullptr;
399 #else
400 return nullptr;
401 #endif
404 private:
405 _Impl _M_impl;
408 // helpers for make_shared / allocate_shared
410 struct _Sp_make_shared_tag { };
412 template<typename _Tp, typename _Alloc, _Lock_policy _Lp>
413 class _Sp_counted_ptr_inplace final : public _Sp_counted_base<_Lp>
415 class _Impl : _Sp_ebo_helper<0, _Alloc>
417 typedef _Sp_ebo_helper<0, _Alloc> _A_base;
419 public:
420 explicit _Impl(_Alloc __a) noexcept : _A_base(__a) { }
422 _Alloc& _M_alloc() noexcept { return _A_base::_S_get(*this); }
424 __gnu_cxx::__aligned_buffer<_Tp> _M_storage;
427 public:
428 template<typename... _Args>
429 _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
430 : _M_impl(__a)
432 // _GLIBCXX_RESOLVE_LIB_DEFECTS
433 // 2070. allocate_shared should use allocator_traits<A>::construct
434 allocator_traits<_Alloc>::construct(__a, _M_ptr(),
435 std::forward<_Args>(__args)...); // might throw
438 ~_Sp_counted_ptr_inplace() noexcept { }
440 virtual void
441 _M_dispose() noexcept
443 allocator_traits<_Alloc>::destroy(_M_impl._M_alloc(), _M_ptr());
446 // Override because the allocator needs to know the dynamic type
447 virtual void
448 _M_destroy() noexcept
450 typedef typename allocator_traits<_Alloc>::template
451 rebind_traits<_Sp_counted_ptr_inplace> _Alloc_traits;
452 typename _Alloc_traits::allocator_type __a(_M_impl._M_alloc());
453 _Alloc_traits::destroy(__a, this);
454 _Alloc_traits::deallocate(__a, this, 1);
457 // Sneaky trick so __shared_ptr can get the managed pointer
458 virtual void*
459 _M_get_deleter(const std::type_info& __ti) noexcept
461 #ifdef __GXX_RTTI
462 if (__ti == typeid(_Sp_make_shared_tag))
463 return const_cast<typename remove_cv<_Tp>::type*>(_M_ptr());
464 #endif
465 return nullptr;
468 private:
469 _Tp* _M_ptr() noexcept { return _M_impl._M_storage._M_ptr(); }
471 _Impl _M_impl;
475 template<_Lock_policy _Lp>
476 class __shared_count
478 public:
479 constexpr __shared_count() noexcept : _M_pi(0)
482 template<typename _Ptr>
483 explicit
484 __shared_count(_Ptr __p) : _M_pi(0)
486 __try
488 _M_pi = new _Sp_counted_ptr<_Ptr, _Lp>(__p);
490 __catch(...)
492 delete __p;
493 __throw_exception_again;
497 template<typename _Ptr, typename _Deleter>
498 __shared_count(_Ptr __p, _Deleter __d)
499 : __shared_count(__p, std::move(__d), allocator<void>())
502 template<typename _Ptr, typename _Deleter, typename _Alloc>
503 __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
505 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
506 typedef typename allocator_traits<_Alloc>::template
507 rebind_traits<_Sp_cd_type> _Alloc_traits;
508 typename _Alloc_traits::allocator_type __a2(__a);
509 _Sp_cd_type* __mem = 0;
510 __try
512 __mem = _Alloc_traits::allocate(__a2, 1);
513 _Alloc_traits::construct(__a2, __mem,
514 __p, std::move(__d), std::move(__a));
515 _M_pi = __mem;
517 __catch(...)
519 __d(__p); // Call _Deleter on __p.
520 if (__mem)
521 _Alloc_traits::deallocate(__a2, __mem, 1);
522 __throw_exception_again;
526 template<typename _Tp, typename _Alloc, typename... _Args>
527 __shared_count(_Sp_make_shared_tag, _Tp*, const _Alloc& __a,
528 _Args&&... __args)
529 : _M_pi(0)
531 typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type;
532 typedef typename allocator_traits<_Alloc>::template
533 rebind_traits<_Sp_cp_type> _Alloc_traits;
534 typename _Alloc_traits::allocator_type __a2(__a);
535 _Sp_cp_type* __mem = _Alloc_traits::allocate(__a2, 1);
536 __try
538 _Alloc_traits::construct(__a2, __mem, std::move(__a),
539 std::forward<_Args>(__args)...);
540 _M_pi = __mem;
542 __catch(...)
544 _Alloc_traits::deallocate(__a2, __mem, 1);
545 __throw_exception_again;
549 #if _GLIBCXX_USE_DEPRECATED
550 // Special case for auto_ptr<_Tp> to provide the strong guarantee.
551 template<typename _Tp>
552 explicit
553 __shared_count(std::auto_ptr<_Tp>&& __r);
554 #endif
556 // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee.
557 template<typename _Tp, typename _Del>
558 explicit
559 __shared_count(std::unique_ptr<_Tp, _Del>&& __r) : _M_pi(0)
561 using _Ptr = typename unique_ptr<_Tp, _Del>::pointer;
562 using _Del2 = typename conditional<is_reference<_Del>::value,
563 reference_wrapper<typename remove_reference<_Del>::type>,
564 _Del>::type;
565 using _Sp_cd_type
566 = _Sp_counted_deleter<_Ptr, _Del2, allocator<void>, _Lp>;
567 using _Alloc = allocator<_Sp_cd_type>;
568 using _Alloc_traits = allocator_traits<_Alloc>;
569 _Alloc __a;
570 _Sp_cd_type* __mem = _Alloc_traits::allocate(__a, 1);
571 _Alloc_traits::construct(__a, __mem, __r.release(),
572 __r.get_deleter()); // non-throwing
573 _M_pi = __mem;
576 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
577 explicit __shared_count(const __weak_count<_Lp>& __r);
579 ~__shared_count() noexcept
581 if (_M_pi != nullptr)
582 _M_pi->_M_release();
585 __shared_count(const __shared_count& __r) noexcept
586 : _M_pi(__r._M_pi)
588 if (_M_pi != 0)
589 _M_pi->_M_add_ref_copy();
592 __shared_count&
593 operator=(const __shared_count& __r) noexcept
595 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
596 if (__tmp != _M_pi)
598 if (__tmp != 0)
599 __tmp->_M_add_ref_copy();
600 if (_M_pi != 0)
601 _M_pi->_M_release();
602 _M_pi = __tmp;
604 return *this;
607 void
608 _M_swap(__shared_count& __r) noexcept
610 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
611 __r._M_pi = _M_pi;
612 _M_pi = __tmp;
615 long
616 _M_get_use_count() const noexcept
617 { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
619 bool
620 _M_unique() const noexcept
621 { return this->_M_get_use_count() == 1; }
623 void*
624 _M_get_deleter(const std::type_info& __ti) const noexcept
625 { return _M_pi ? _M_pi->_M_get_deleter(__ti) : nullptr; }
627 bool
628 _M_less(const __shared_count& __rhs) const noexcept
629 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
631 bool
632 _M_less(const __weak_count<_Lp>& __rhs) const noexcept
633 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
635 // Friend function injected into enclosing namespace and found by ADL
636 friend inline bool
637 operator==(const __shared_count& __a, const __shared_count& __b) noexcept
638 { return __a._M_pi == __b._M_pi; }
640 private:
641 friend class __weak_count<_Lp>;
643 _Sp_counted_base<_Lp>* _M_pi;
647 template<_Lock_policy _Lp>
648 class __weak_count
650 public:
651 constexpr __weak_count() noexcept : _M_pi(0)
654 __weak_count(const __shared_count<_Lp>& __r) noexcept
655 : _M_pi(__r._M_pi)
657 if (_M_pi != 0)
658 _M_pi->_M_weak_add_ref();
661 __weak_count(const __weak_count<_Lp>& __r) noexcept
662 : _M_pi(__r._M_pi)
664 if (_M_pi != 0)
665 _M_pi->_M_weak_add_ref();
668 ~__weak_count() noexcept
670 if (_M_pi != 0)
671 _M_pi->_M_weak_release();
674 __weak_count<_Lp>&
675 operator=(const __shared_count<_Lp>& __r) noexcept
677 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
678 if (__tmp != 0)
679 __tmp->_M_weak_add_ref();
680 if (_M_pi != 0)
681 _M_pi->_M_weak_release();
682 _M_pi = __tmp;
683 return *this;
686 __weak_count<_Lp>&
687 operator=(const __weak_count<_Lp>& __r) noexcept
689 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
690 if (__tmp != 0)
691 __tmp->_M_weak_add_ref();
692 if (_M_pi != 0)
693 _M_pi->_M_weak_release();
694 _M_pi = __tmp;
695 return *this;
698 void
699 _M_swap(__weak_count<_Lp>& __r) noexcept
701 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
702 __r._M_pi = _M_pi;
703 _M_pi = __tmp;
706 long
707 _M_get_use_count() const noexcept
708 { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
710 bool
711 _M_less(const __weak_count& __rhs) const noexcept
712 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
714 bool
715 _M_less(const __shared_count<_Lp>& __rhs) const noexcept
716 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
718 // Friend function injected into enclosing namespace and found by ADL
719 friend inline bool
720 operator==(const __weak_count& __a, const __weak_count& __b) noexcept
721 { return __a._M_pi == __b._M_pi; }
723 private:
724 friend class __shared_count<_Lp>;
726 _Sp_counted_base<_Lp>* _M_pi;
729 // Now that __weak_count is defined we can define this constructor:
730 template<_Lock_policy _Lp>
731 inline __shared_count<_Lp>:: __shared_count(const __weak_count<_Lp>& __r)
732 : _M_pi(__r._M_pi)
734 if (_M_pi != 0)
735 _M_pi->_M_add_ref_lock();
736 else
737 __throw_bad_weak_ptr();
741 // Support for enable_shared_from_this.
743 // Friend of __enable_shared_from_this.
744 template<_Lock_policy _Lp, typename _Tp1, typename _Tp2>
745 void
746 __enable_shared_from_this_helper(const __shared_count<_Lp>&,
747 const __enable_shared_from_this<_Tp1,
748 _Lp>*, const _Tp2*) noexcept;
750 // Friend of enable_shared_from_this.
751 template<typename _Tp1, typename _Tp2>
752 void
753 __enable_shared_from_this_helper(const __shared_count<>&,
754 const enable_shared_from_this<_Tp1>*,
755 const _Tp2*) noexcept;
757 template<_Lock_policy _Lp>
758 inline void
759 __enable_shared_from_this_helper(const __shared_count<_Lp>&, ...) noexcept
763 template<typename _Tp, _Lock_policy _Lp>
764 class __shared_ptr
766 public:
767 typedef _Tp element_type;
769 constexpr __shared_ptr() noexcept
770 : _M_ptr(0), _M_refcount()
773 template<typename _Tp1>
774 explicit __shared_ptr(_Tp1* __p)
775 : _M_ptr(__p), _M_refcount(__p)
777 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
778 static_assert( sizeof(_Tp1) > 0, "incomplete type" );
779 __enable_shared_from_this_helper(_M_refcount, __p, __p);
782 template<typename _Tp1, typename _Deleter>
783 __shared_ptr(_Tp1* __p, _Deleter __d)
784 : _M_ptr(__p), _M_refcount(__p, __d)
786 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
787 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
788 __enable_shared_from_this_helper(_M_refcount, __p, __p);
791 template<typename _Tp1, typename _Deleter, typename _Alloc>
792 __shared_ptr(_Tp1* __p, _Deleter __d, _Alloc __a)
793 : _M_ptr(__p), _M_refcount(__p, __d, std::move(__a))
795 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
796 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
797 __enable_shared_from_this_helper(_M_refcount, __p, __p);
800 template<typename _Deleter>
801 __shared_ptr(nullptr_t __p, _Deleter __d)
802 : _M_ptr(0), _M_refcount(__p, __d)
805 template<typename _Deleter, typename _Alloc>
806 __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
807 : _M_ptr(0), _M_refcount(__p, __d, std::move(__a))
810 template<typename _Tp1>
811 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r, _Tp* __p) noexcept
812 : _M_ptr(__p), _M_refcount(__r._M_refcount) // never throws
815 __shared_ptr(const __shared_ptr&) noexcept = default;
816 __shared_ptr& operator=(const __shared_ptr&) noexcept = default;
817 ~__shared_ptr() = default;
819 template<typename _Tp1, typename = typename
820 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
821 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
822 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
825 __shared_ptr(__shared_ptr&& __r) noexcept
826 : _M_ptr(__r._M_ptr), _M_refcount()
828 _M_refcount._M_swap(__r._M_refcount);
829 __r._M_ptr = 0;
832 template<typename _Tp1, typename = typename
833 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
834 __shared_ptr(__shared_ptr<_Tp1, _Lp>&& __r) noexcept
835 : _M_ptr(__r._M_ptr), _M_refcount()
837 _M_refcount._M_swap(__r._M_refcount);
838 __r._M_ptr = 0;
841 template<typename _Tp1>
842 explicit __shared_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
843 : _M_refcount(__r._M_refcount) // may throw
845 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
847 // It is now safe to copy __r._M_ptr, as
848 // _M_refcount(__r._M_refcount) did not throw.
849 _M_ptr = __r._M_ptr;
852 // If an exception is thrown this constructor has no effect.
853 template<typename _Tp1, typename _Del>
854 __shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r)
855 : _M_ptr(__r.get()), _M_refcount()
857 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
858 auto __raw = _S_raw_ptr(__r.get());
859 _M_refcount = __shared_count<_Lp>(std::move(__r));
860 __enable_shared_from_this_helper(_M_refcount, __raw, __raw);
863 #if _GLIBCXX_USE_DEPRECATED
864 // Postcondition: use_count() == 1 and __r.get() == 0
865 template<typename _Tp1>
866 __shared_ptr(std::auto_ptr<_Tp1>&& __r);
867 #endif
869 /* TODO: use delegating constructor */
870 constexpr __shared_ptr(nullptr_t) noexcept
871 : _M_ptr(0), _M_refcount()
874 template<typename _Tp1>
875 __shared_ptr&
876 operator=(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
878 _M_ptr = __r._M_ptr;
879 _M_refcount = __r._M_refcount; // __shared_count::op= doesn't throw
880 return *this;
883 #if _GLIBCXX_USE_DEPRECATED
884 template<typename _Tp1>
885 __shared_ptr&
886 operator=(std::auto_ptr<_Tp1>&& __r)
888 __shared_ptr(std::move(__r)).swap(*this);
889 return *this;
891 #endif
893 __shared_ptr&
894 operator=(__shared_ptr&& __r) noexcept
896 __shared_ptr(std::move(__r)).swap(*this);
897 return *this;
900 template<class _Tp1>
901 __shared_ptr&
902 operator=(__shared_ptr<_Tp1, _Lp>&& __r) noexcept
904 __shared_ptr(std::move(__r)).swap(*this);
905 return *this;
908 template<typename _Tp1, typename _Del>
909 __shared_ptr&
910 operator=(std::unique_ptr<_Tp1, _Del>&& __r)
912 __shared_ptr(std::move(__r)).swap(*this);
913 return *this;
916 void
917 reset() noexcept
918 { __shared_ptr().swap(*this); }
920 template<typename _Tp1>
921 void
922 reset(_Tp1* __p) // _Tp1 must be complete.
924 // Catch self-reset errors.
925 _GLIBCXX_DEBUG_ASSERT(__p == 0 || __p != _M_ptr);
926 __shared_ptr(__p).swap(*this);
929 template<typename _Tp1, typename _Deleter>
930 void
931 reset(_Tp1* __p, _Deleter __d)
932 { __shared_ptr(__p, __d).swap(*this); }
934 template<typename _Tp1, typename _Deleter, typename _Alloc>
935 void
936 reset(_Tp1* __p, _Deleter __d, _Alloc __a)
937 { __shared_ptr(__p, __d, std::move(__a)).swap(*this); }
939 // Allow class instantiation when _Tp is [cv-qual] void.
940 typename std::add_lvalue_reference<_Tp>::type
941 operator*() const noexcept
943 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
944 return *_M_ptr;
947 _Tp*
948 operator->() const noexcept
950 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
951 return _M_ptr;
954 _Tp*
955 get() const noexcept
956 { return _M_ptr; }
958 explicit operator bool() const // never throws
959 { return _M_ptr == 0 ? false : true; }
961 bool
962 unique() const noexcept
963 { return _M_refcount._M_unique(); }
965 long
966 use_count() const noexcept
967 { return _M_refcount._M_get_use_count(); }
969 void
970 swap(__shared_ptr<_Tp, _Lp>& __other) noexcept
972 std::swap(_M_ptr, __other._M_ptr);
973 _M_refcount._M_swap(__other._M_refcount);
976 template<typename _Tp1>
977 bool
978 owner_before(__shared_ptr<_Tp1, _Lp> const& __rhs) const
979 { return _M_refcount._M_less(__rhs._M_refcount); }
981 template<typename _Tp1>
982 bool
983 owner_before(__weak_ptr<_Tp1, _Lp> const& __rhs) const
984 { return _M_refcount._M_less(__rhs._M_refcount); }
986 #ifdef __GXX_RTTI
987 protected:
988 // This constructor is non-standard, it is used by allocate_shared.
989 template<typename _Alloc, typename... _Args>
990 __shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a,
991 _Args&&... __args)
992 : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a,
993 std::forward<_Args>(__args)...)
995 // _M_ptr needs to point to the newly constructed object.
996 // This relies on _Sp_counted_ptr_inplace::_M_get_deleter.
997 void* __p = _M_refcount._M_get_deleter(typeid(__tag));
998 _M_ptr = static_cast<_Tp*>(__p);
999 __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
1001 #else
1002 template<typename _Alloc>
1003 struct _Deleter
1005 void operator()(_Tp* __ptr)
1007 typedef allocator_traits<_Alloc> _Alloc_traits;
1008 _Alloc_traits::destroy(_M_alloc, __ptr);
1009 _Alloc_traits::deallocate(_M_alloc, __ptr, 1);
1011 _Alloc _M_alloc;
1014 template<typename _Alloc, typename... _Args>
1015 __shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a,
1016 _Args&&... __args)
1017 : _M_ptr(), _M_refcount()
1019 typedef typename _Alloc::template rebind<_Tp>::other _Alloc2;
1020 _Deleter<_Alloc2> __del = { _Alloc2(__a) };
1021 typedef allocator_traits<_Alloc2> __traits;
1022 _M_ptr = __traits::allocate(__del._M_alloc, 1);
1023 __try
1025 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1026 // 2070. allocate_shared should use allocator_traits<A>::construct
1027 __traits::construct(__del._M_alloc, _M_ptr,
1028 std::forward<_Args>(__args)...);
1030 __catch(...)
1032 __traits::deallocate(__del._M_alloc, _M_ptr, 1);
1033 __throw_exception_again;
1035 __shared_count<_Lp> __count(_M_ptr, __del, __del._M_alloc);
1036 _M_refcount._M_swap(__count);
1037 __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
1039 #endif
1041 template<typename _Tp1, _Lock_policy _Lp1, typename _Alloc,
1042 typename... _Args>
1043 friend __shared_ptr<_Tp1, _Lp1>
1044 __allocate_shared(const _Alloc& __a, _Args&&... __args);
1046 private:
1047 void*
1048 _M_get_deleter(const std::type_info& __ti) const noexcept
1049 { return _M_refcount._M_get_deleter(__ti); }
1051 template<typename _Tp1>
1052 static _Tp1*
1053 _S_raw_ptr(_Tp1* __ptr)
1054 { return __ptr; }
1056 template<typename _Tp1>
1057 static auto
1058 _S_raw_ptr(_Tp1 __ptr) -> decltype(std::__addressof(*__ptr))
1059 { return std::__addressof(*__ptr); }
1061 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
1062 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
1064 template<typename _Del, typename _Tp1, _Lock_policy _Lp1>
1065 friend _Del* get_deleter(const __shared_ptr<_Tp1, _Lp1>&) noexcept;
1067 _Tp* _M_ptr; // Contained pointer.
1068 __shared_count<_Lp> _M_refcount; // Reference counter.
1072 // 20.7.2.2.7 shared_ptr comparisons
1073 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1074 inline bool
1075 operator==(const __shared_ptr<_Tp1, _Lp>& __a,
1076 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1077 { return __a.get() == __b.get(); }
1079 template<typename _Tp, _Lock_policy _Lp>
1080 inline bool
1081 operator==(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1082 { return !__a; }
1084 template<typename _Tp, _Lock_policy _Lp>
1085 inline bool
1086 operator==(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1087 { return !__a; }
1089 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1090 inline bool
1091 operator!=(const __shared_ptr<_Tp1, _Lp>& __a,
1092 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1093 { return __a.get() != __b.get(); }
1095 template<typename _Tp, _Lock_policy _Lp>
1096 inline bool
1097 operator!=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1098 { return (bool)__a; }
1100 template<typename _Tp, _Lock_policy _Lp>
1101 inline bool
1102 operator!=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1103 { return (bool)__a; }
1105 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1106 inline bool
1107 operator<(const __shared_ptr<_Tp1, _Lp>& __a,
1108 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1110 typedef typename std::common_type<_Tp1*, _Tp2*>::type _CT;
1111 return std::less<_CT>()(__a.get(), __b.get());
1114 template<typename _Tp, _Lock_policy _Lp>
1115 inline bool
1116 operator<(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1117 { return std::less<_Tp*>()(__a.get(), nullptr); }
1119 template<typename _Tp, _Lock_policy _Lp>
1120 inline bool
1121 operator<(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1122 { return std::less<_Tp*>()(nullptr, __a.get()); }
1124 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1125 inline bool
1126 operator<=(const __shared_ptr<_Tp1, _Lp>& __a,
1127 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1128 { return !(__b < __a); }
1130 template<typename _Tp, _Lock_policy _Lp>
1131 inline bool
1132 operator<=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1133 { return !(nullptr < __a); }
1135 template<typename _Tp, _Lock_policy _Lp>
1136 inline bool
1137 operator<=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1138 { return !(__a < nullptr); }
1140 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1141 inline bool
1142 operator>(const __shared_ptr<_Tp1, _Lp>& __a,
1143 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1144 { return (__b < __a); }
1146 template<typename _Tp, _Lock_policy _Lp>
1147 inline bool
1148 operator>(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1149 { return std::less<_Tp*>()(nullptr, __a.get()); }
1151 template<typename _Tp, _Lock_policy _Lp>
1152 inline bool
1153 operator>(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1154 { return std::less<_Tp*>()(__a.get(), nullptr); }
1156 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1157 inline bool
1158 operator>=(const __shared_ptr<_Tp1, _Lp>& __a,
1159 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1160 { return !(__a < __b); }
1162 template<typename _Tp, _Lock_policy _Lp>
1163 inline bool
1164 operator>=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1165 { return !(__a < nullptr); }
1167 template<typename _Tp, _Lock_policy _Lp>
1168 inline bool
1169 operator>=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1170 { return !(nullptr < __a); }
1172 template<typename _Sp>
1173 struct _Sp_less : public binary_function<_Sp, _Sp, bool>
1175 bool
1176 operator()(const _Sp& __lhs, const _Sp& __rhs) const noexcept
1178 typedef typename _Sp::element_type element_type;
1179 return std::less<element_type*>()(__lhs.get(), __rhs.get());
1183 template<typename _Tp, _Lock_policy _Lp>
1184 struct less<__shared_ptr<_Tp, _Lp>>
1185 : public _Sp_less<__shared_ptr<_Tp, _Lp>>
1186 { };
1188 // 20.7.2.2.8 shared_ptr specialized algorithms.
1189 template<typename _Tp, _Lock_policy _Lp>
1190 inline void
1191 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b) noexcept
1192 { __a.swap(__b); }
1194 // 20.7.2.2.9 shared_ptr casts
1196 // The seemingly equivalent code:
1197 // shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))
1198 // will eventually result in undefined behaviour, attempting to
1199 // delete the same object twice.
1200 /// static_pointer_cast
1201 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1202 inline __shared_ptr<_Tp, _Lp>
1203 static_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1204 { return __shared_ptr<_Tp, _Lp>(__r, static_cast<_Tp*>(__r.get())); }
1206 // The seemingly equivalent code:
1207 // shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))
1208 // will eventually result in undefined behaviour, attempting to
1209 // delete the same object twice.
1210 /// const_pointer_cast
1211 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1212 inline __shared_ptr<_Tp, _Lp>
1213 const_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1214 { return __shared_ptr<_Tp, _Lp>(__r, const_cast<_Tp*>(__r.get())); }
1216 // The seemingly equivalent code:
1217 // shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))
1218 // will eventually result in undefined behaviour, attempting to
1219 // delete the same object twice.
1220 /// dynamic_pointer_cast
1221 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1222 inline __shared_ptr<_Tp, _Lp>
1223 dynamic_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1225 if (_Tp* __p = dynamic_cast<_Tp*>(__r.get()))
1226 return __shared_ptr<_Tp, _Lp>(__r, __p);
1227 return __shared_ptr<_Tp, _Lp>();
1231 template<typename _Tp, _Lock_policy _Lp>
1232 class __weak_ptr
1234 public:
1235 typedef _Tp element_type;
1237 constexpr __weak_ptr() noexcept
1238 : _M_ptr(0), _M_refcount()
1241 __weak_ptr(const __weak_ptr&) noexcept = default;
1242 __weak_ptr& operator=(const __weak_ptr&) noexcept = default;
1243 ~__weak_ptr() = default;
1245 // The "obvious" converting constructor implementation:
1247 // template<typename _Tp1>
1248 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
1249 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
1250 // { }
1252 // has a serious problem.
1254 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
1255 // conversion may require access to *__r._M_ptr (virtual inheritance).
1257 // It is not possible to avoid spurious access violations since
1258 // in multithreaded programs __r._M_ptr may be invalidated at any point.
1259 template<typename _Tp1, typename = typename
1260 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
1261 __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r) noexcept
1262 : _M_refcount(__r._M_refcount)
1263 { _M_ptr = __r.lock().get(); }
1265 template<typename _Tp1, typename = typename
1266 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
1267 __weak_ptr(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1268 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1271 template<typename _Tp1>
1272 __weak_ptr&
1273 operator=(const __weak_ptr<_Tp1, _Lp>& __r) noexcept
1275 _M_ptr = __r.lock().get();
1276 _M_refcount = __r._M_refcount;
1277 return *this;
1280 template<typename _Tp1>
1281 __weak_ptr&
1282 operator=(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1284 _M_ptr = __r._M_ptr;
1285 _M_refcount = __r._M_refcount;
1286 return *this;
1289 __shared_ptr<_Tp, _Lp>
1290 lock() const noexcept
1292 #ifdef __GTHREADS
1293 // Optimization: avoid throw overhead.
1294 if (expired())
1295 return __shared_ptr<element_type, _Lp>();
1297 __try
1299 return __shared_ptr<element_type, _Lp>(*this);
1301 __catch(const bad_weak_ptr&)
1303 // Q: How can we get here?
1304 // A: Another thread may have invalidated r after the
1305 // use_count test above.
1306 return __shared_ptr<element_type, _Lp>();
1309 #else
1310 // Optimization: avoid try/catch overhead when single threaded.
1311 return expired() ? __shared_ptr<element_type, _Lp>()
1312 : __shared_ptr<element_type, _Lp>(*this);
1314 #endif
1315 } // XXX MT
1317 long
1318 use_count() const noexcept
1319 { return _M_refcount._M_get_use_count(); }
1321 bool
1322 expired() const noexcept
1323 { return _M_refcount._M_get_use_count() == 0; }
1325 template<typename _Tp1>
1326 bool
1327 owner_before(const __shared_ptr<_Tp1, _Lp>& __rhs) const
1328 { return _M_refcount._M_less(__rhs._M_refcount); }
1330 template<typename _Tp1>
1331 bool
1332 owner_before(const __weak_ptr<_Tp1, _Lp>& __rhs) const
1333 { return _M_refcount._M_less(__rhs._M_refcount); }
1335 void
1336 reset() noexcept
1337 { __weak_ptr().swap(*this); }
1339 void
1340 swap(__weak_ptr& __s) noexcept
1342 std::swap(_M_ptr, __s._M_ptr);
1343 _M_refcount._M_swap(__s._M_refcount);
1346 private:
1347 // Used by __enable_shared_from_this.
1348 void
1349 _M_assign(_Tp* __ptr, const __shared_count<_Lp>& __refcount) noexcept
1351 _M_ptr = __ptr;
1352 _M_refcount = __refcount;
1355 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
1356 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
1357 friend class __enable_shared_from_this<_Tp, _Lp>;
1358 friend class enable_shared_from_this<_Tp>;
1360 _Tp* _M_ptr; // Contained pointer.
1361 __weak_count<_Lp> _M_refcount; // Reference counter.
1364 // 20.7.2.3.6 weak_ptr specialized algorithms.
1365 template<typename _Tp, _Lock_policy _Lp>
1366 inline void
1367 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b) noexcept
1368 { __a.swap(__b); }
1370 template<typename _Tp, typename _Tp1>
1371 struct _Sp_owner_less : public binary_function<_Tp, _Tp, bool>
1373 bool
1374 operator()(const _Tp& __lhs, const _Tp& __rhs) const
1375 { return __lhs.owner_before(__rhs); }
1377 bool
1378 operator()(const _Tp& __lhs, const _Tp1& __rhs) const
1379 { return __lhs.owner_before(__rhs); }
1381 bool
1382 operator()(const _Tp1& __lhs, const _Tp& __rhs) const
1383 { return __lhs.owner_before(__rhs); }
1386 template<typename _Tp, _Lock_policy _Lp>
1387 struct owner_less<__shared_ptr<_Tp, _Lp>>
1388 : public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
1389 { };
1391 template<typename _Tp, _Lock_policy _Lp>
1392 struct owner_less<__weak_ptr<_Tp, _Lp>>
1393 : public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
1394 { };
1397 template<typename _Tp, _Lock_policy _Lp>
1398 class __enable_shared_from_this
1400 protected:
1401 constexpr __enable_shared_from_this() noexcept { }
1403 __enable_shared_from_this(const __enable_shared_from_this&) noexcept { }
1405 __enable_shared_from_this&
1406 operator=(const __enable_shared_from_this&) noexcept
1407 { return *this; }
1409 ~__enable_shared_from_this() { }
1411 public:
1412 __shared_ptr<_Tp, _Lp>
1413 shared_from_this()
1414 { return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
1416 __shared_ptr<const _Tp, _Lp>
1417 shared_from_this() const
1418 { return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
1420 private:
1421 template<typename _Tp1>
1422 void
1423 _M_weak_assign(_Tp1* __p, const __shared_count<_Lp>& __n) const noexcept
1424 { _M_weak_this._M_assign(__p, __n); }
1426 template<typename _Tp1>
1427 friend void
1428 __enable_shared_from_this_helper(const __shared_count<_Lp>& __pn,
1429 const __enable_shared_from_this* __pe,
1430 const _Tp1* __px) noexcept
1432 if (__pe != 0)
1433 __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
1436 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
1440 template<typename _Tp, _Lock_policy _Lp, typename _Alloc, typename... _Args>
1441 inline __shared_ptr<_Tp, _Lp>
1442 __allocate_shared(const _Alloc& __a, _Args&&... __args)
1444 return __shared_ptr<_Tp, _Lp>(_Sp_make_shared_tag(), __a,
1445 std::forward<_Args>(__args)...);
1448 template<typename _Tp, _Lock_policy _Lp, typename... _Args>
1449 inline __shared_ptr<_Tp, _Lp>
1450 __make_shared(_Args&&... __args)
1452 typedef typename std::remove_const<_Tp>::type _Tp_nc;
1453 return std::__allocate_shared<_Tp, _Lp>(std::allocator<_Tp_nc>(),
1454 std::forward<_Args>(__args)...);
1457 /// std::hash specialization for __shared_ptr.
1458 template<typename _Tp, _Lock_policy _Lp>
1459 struct hash<__shared_ptr<_Tp, _Lp>>
1460 : public __hash_base<size_t, __shared_ptr<_Tp, _Lp>>
1462 size_t
1463 operator()(const __shared_ptr<_Tp, _Lp>& __s) const noexcept
1464 { return std::hash<_Tp*>()(__s.get()); }
1467 _GLIBCXX_END_NAMESPACE_VERSION
1468 } // namespace
1470 #endif // _SHARED_PTR_BASE_H