1 // <tr1/shared_ptr.h> -*- C++ -*-
3 // Copyright (C) 2007-2018 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
26 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
29 // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
30 // Copyright (C) 2001, 2002, 2003 Peter Dimov
33 // Copyright (C) 2001, 2002, 2003 Peter Dimov
35 // enable_shared_from_this.hpp
36 // Copyright (C) 2002 Peter Dimov
38 // Distributed under the Boost Software License, Version 1.0. (See
39 // accompanying file LICENSE_1_0.txt or copy at
40 // http://www.boost.org/LICENSE_1_0.txt)
42 // GCC Note: based on version 1.32.0 of the Boost library.
44 /** @file tr1/shared_ptr.h
45 * This is an internal header file, included by other library headers.
46 * Do not attempt to use it directly. @headername{tr1/memory}
49 #ifndef _TR1_SHARED_PTR_H
50 #define _TR1_SHARED_PTR_H 1
52 namespace std
_GLIBCXX_VISIBILITY(default)
54 _GLIBCXX_BEGIN_NAMESPACE_VERSION
59 * @brief Exception possibly thrown by @c shared_ptr.
62 class bad_weak_ptr
: public std::exception
67 { return "tr1::bad_weak_ptr"; }
70 // Substitute for bad_weak_ptr object in the case of -fno-exceptions.
72 __throw_bad_weak_ptr()
73 { _GLIBCXX_THROW_OR_ABORT(bad_weak_ptr()); }
75 using __gnu_cxx::_Lock_policy
;
76 using __gnu_cxx::__default_lock_policy
;
77 using __gnu_cxx::_S_single
;
78 using __gnu_cxx::_S_mutex
;
79 using __gnu_cxx::_S_atomic
;
81 // Empty helper class except when the template argument is _S_mutex.
82 template<_Lock_policy _Lp
>
86 // The atomic policy uses fully-fenced builtins, single doesn't care.
87 enum { _S_need_barriers
= 0 };
91 class _Mutex_base
<_S_mutex
>
92 : public __gnu_cxx::__mutex
95 // This policy is used when atomic builtins are not available.
96 // The replacement atomic operations might not have the necessary
98 enum { _S_need_barriers
= 1 };
101 template<_Lock_policy _Lp
= __default_lock_policy
>
102 class _Sp_counted_base
103 : public _Mutex_base
<_Lp
>
107 : _M_use_count(1), _M_weak_count(1) { }
110 ~_Sp_counted_base() // nothrow
113 // Called when _M_use_count drops to zero, to release the resources
116 _M_dispose() = 0; // nothrow
118 // Called when _M_weak_count drops to zero.
120 _M_destroy() // nothrow
124 _M_get_deleter(const std::type_info
&) = 0;
128 { __gnu_cxx::__atomic_add_dispatch(&_M_use_count
, 1); }
134 _M_release() // nothrow
136 // Be race-detector-friendly. For more info see bits/c++config.
137 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count
);
138 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count
, -1) == 1)
140 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count
);
142 // There must be a memory barrier between dispose() and destroy()
143 // to ensure that the effects of dispose() are observed in the
144 // thread that runs destroy().
145 // See http://gcc.gnu.org/ml/libstdc++/2005-11/msg00136.html
146 if (_Mutex_base
<_Lp
>::_S_need_barriers
)
148 __atomic_thread_fence (__ATOMIC_ACQ_REL
);
151 // Be race-detector-friendly. For more info see bits/c++config.
152 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count
);
153 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count
,
156 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count
);
163 _M_weak_add_ref() // nothrow
164 { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count
, 1); }
167 _M_weak_release() // nothrow
169 // Be race-detector-friendly. For more info see bits/c++config.
170 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count
);
171 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count
, -1) == 1)
173 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count
);
174 if (_Mutex_base
<_Lp
>::_S_need_barriers
)
177 // destroy() must observe results of dispose()
178 __atomic_thread_fence (__ATOMIC_ACQ_REL
);
185 _M_get_use_count() const // nothrow
187 // No memory barrier is used here so there is no synchronization
188 // with other threads.
189 return const_cast<const volatile _Atomic_word
&>(_M_use_count
);
193 _Sp_counted_base(_Sp_counted_base
const&);
194 _Sp_counted_base
& operator=(_Sp_counted_base
const&);
196 _Atomic_word _M_use_count
; // #shared
197 _Atomic_word _M_weak_count
; // #weak + (#shared != 0)
202 _Sp_counted_base
<_S_single
>::
205 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count
, 1) == 0)
208 __throw_bad_weak_ptr();
214 _Sp_counted_base
<_S_mutex
>::
217 __gnu_cxx::__scoped_lock
sentry(*this);
218 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count
, 1) == 0)
221 __throw_bad_weak_ptr();
227 _Sp_counted_base
<_S_atomic
>::
230 // Perform lock-free add-if-not-zero operation.
231 _Atomic_word __count
= _M_use_count
;
235 __throw_bad_weak_ptr();
236 // Replace the current counter value with the old value + 1, as
237 // long as it's not changed meanwhile.
239 while (!__atomic_compare_exchange_n(&_M_use_count
, &__count
, __count
+ 1,
240 true, __ATOMIC_ACQ_REL
,
244 template<typename _Ptr
, typename _Deleter
, _Lock_policy _Lp
>
245 class _Sp_counted_base_impl
246 : public _Sp_counted_base
<_Lp
>
249 // Precondition: __d(__p) must not throw.
250 _Sp_counted_base_impl(_Ptr __p
, _Deleter __d
)
251 : _M_ptr(__p
), _M_del(__d
) { }
254 _M_dispose() // nothrow
258 _M_get_deleter(const std::type_info
& __ti
)
261 return __ti
== typeid(_Deleter
) ? &_M_del
: 0;
268 _Sp_counted_base_impl(const _Sp_counted_base_impl
&);
269 _Sp_counted_base_impl
& operator=(const _Sp_counted_base_impl
&);
271 _Ptr _M_ptr
; // copy constructor must not throw
272 _Deleter _M_del
; // copy constructor must not throw
275 template<_Lock_policy _Lp
= __default_lock_policy
>
278 template<typename _Tp
>
281 typedef void result_type
;
282 typedef _Tp
* argument_type
;
283 void operator()(_Tp
* __p
) const { delete __p
; }
286 template<_Lock_policy _Lp
= __default_lock_policy
>
291 : _M_pi(0) // nothrow
294 template<typename _Ptr
>
295 __shared_count(_Ptr __p
) : _M_pi(0)
299 typedef typename
std::tr1::remove_pointer
<_Ptr
>::type _Tp
;
300 _M_pi
= new _Sp_counted_base_impl
<_Ptr
, _Sp_deleter
<_Tp
>, _Lp
>(
301 __p
, _Sp_deleter
<_Tp
>());
306 __throw_exception_again
;
310 template<typename _Ptr
, typename _Deleter
>
311 __shared_count(_Ptr __p
, _Deleter __d
) : _M_pi(0)
315 _M_pi
= new _Sp_counted_base_impl
<_Ptr
, _Deleter
, _Lp
>(__p
, __d
);
319 __d(__p
); // Call _Deleter on __p.
320 __throw_exception_again
;
324 // Special case for auto_ptr<_Tp> to provide the strong guarantee.
325 template<typename _Tp
>
327 __shared_count(std::auto_ptr
<_Tp
>& __r
)
328 : _M_pi(new _Sp_counted_base_impl
<_Tp
*,
329 _Sp_deleter
<_Tp
>, _Lp
>(__r
.get(), _Sp_deleter
<_Tp
>()))
332 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
334 __shared_count(const __weak_count
<_Lp
>& __r
);
336 ~__shared_count() // nothrow
342 __shared_count(const __shared_count
& __r
)
343 : _M_pi(__r
._M_pi
) // nothrow
346 _M_pi
->_M_add_ref_copy();
350 operator=(const __shared_count
& __r
) // nothrow
352 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
356 __tmp
->_M_add_ref_copy();
365 _M_swap(__shared_count
& __r
) // nothrow
367 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
373 _M_get_use_count() const // nothrow
374 { return _M_pi
!= 0 ? _M_pi
->_M_get_use_count() : 0; }
377 _M_unique() const // nothrow
378 { return this->_M_get_use_count() == 1; }
381 operator==(const __shared_count
& __a
, const __shared_count
& __b
)
382 { return __a
._M_pi
== __b
._M_pi
; }
385 operator<(const __shared_count
& __a
, const __shared_count
& __b
)
386 { return std::less
<_Sp_counted_base
<_Lp
>*>()(__a
._M_pi
, __b
._M_pi
); }
389 _M_get_deleter(const std::type_info
& __ti
) const
390 { return _M_pi
? _M_pi
->_M_get_deleter(__ti
) : 0; }
393 friend class __weak_count
<_Lp
>;
395 _Sp_counted_base
<_Lp
>* _M_pi
;
399 template<_Lock_policy _Lp
>
404 : _M_pi(0) // nothrow
407 __weak_count(const __shared_count
<_Lp
>& __r
)
408 : _M_pi(__r
._M_pi
) // nothrow
411 _M_pi
->_M_weak_add_ref();
414 __weak_count(const __weak_count
<_Lp
>& __r
)
415 : _M_pi(__r
._M_pi
) // nothrow
418 _M_pi
->_M_weak_add_ref();
421 ~__weak_count() // nothrow
424 _M_pi
->_M_weak_release();
428 operator=(const __shared_count
<_Lp
>& __r
) // nothrow
430 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
432 __tmp
->_M_weak_add_ref();
434 _M_pi
->_M_weak_release();
440 operator=(const __weak_count
<_Lp
>& __r
) // nothrow
442 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
444 __tmp
->_M_weak_add_ref();
446 _M_pi
->_M_weak_release();
452 _M_swap(__weak_count
<_Lp
>& __r
) // nothrow
454 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
460 _M_get_use_count() const // nothrow
461 { return _M_pi
!= 0 ? _M_pi
->_M_get_use_count() : 0; }
464 operator==(const __weak_count
<_Lp
>& __a
, const __weak_count
<_Lp
>& __b
)
465 { return __a
._M_pi
== __b
._M_pi
; }
468 operator<(const __weak_count
<_Lp
>& __a
, const __weak_count
<_Lp
>& __b
)
469 { return std::less
<_Sp_counted_base
<_Lp
>*>()(__a
._M_pi
, __b
._M_pi
); }
472 friend class __shared_count
<_Lp
>;
474 _Sp_counted_base
<_Lp
>* _M_pi
;
477 // now that __weak_count is defined we can define this constructor:
478 template<_Lock_policy _Lp
>
480 __shared_count
<_Lp
>::
481 __shared_count(const __weak_count
<_Lp
>& __r
)
485 _M_pi
->_M_add_ref_lock();
487 __throw_bad_weak_ptr();
490 // Forward declarations.
491 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
494 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
497 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
498 class __enable_shared_from_this
;
500 template<typename _Tp
>
503 template<typename _Tp
>
506 template<typename _Tp
>
507 class enable_shared_from_this
;
509 // Support for enable_shared_from_this.
511 // Friend of __enable_shared_from_this.
512 template<_Lock_policy _Lp
, typename _Tp1
, typename _Tp2
>
514 __enable_shared_from_this_helper(const __shared_count
<_Lp
>&,
515 const __enable_shared_from_this
<_Tp1
,
518 // Friend of enable_shared_from_this.
519 template<typename _Tp1
, typename _Tp2
>
521 __enable_shared_from_this_helper(const __shared_count
<>&,
522 const enable_shared_from_this
<_Tp1
>*,
525 template<_Lock_policy _Lp
>
527 __enable_shared_from_this_helper(const __shared_count
<_Lp
>&, ...)
531 struct __static_cast_tag
{ };
532 struct __const_cast_tag
{ };
533 struct __dynamic_cast_tag
{ };
535 // A smart pointer with reference-counted copy semantics. The
536 // object pointed to is deleted when the last shared_ptr pointing to
537 // it is destroyed or reset.
538 template<typename _Tp
, _Lock_policy _Lp
>
542 typedef _Tp element_type
;
545 : _M_ptr(0), _M_refcount() // never throws
548 template<typename _Tp1
>
550 __shared_ptr(_Tp1
* __p
)
551 : _M_ptr(__p
), _M_refcount(__p
)
553 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
554 typedef int _IsComplete
[sizeof(_Tp1
)];
555 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
558 template<typename _Tp1
, typename _Deleter
>
559 __shared_ptr(_Tp1
* __p
, _Deleter __d
)
560 : _M_ptr(__p
), _M_refcount(__p
, __d
)
562 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
563 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
564 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
567 // generated copy constructor, assignment, destructor are fine.
569 template<typename _Tp1
>
570 __shared_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
571 : _M_ptr(__r
._M_ptr
), _M_refcount(__r
._M_refcount
) // never throws
572 { __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>) }
574 template<typename _Tp1
>
576 __shared_ptr(const __weak_ptr
<_Tp1
, _Lp
>& __r
)
577 : _M_refcount(__r
._M_refcount
) // may throw
579 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
580 // It is now safe to copy __r._M_ptr, as _M_refcount(__r._M_refcount)
585 #if (__cplusplus < 201103L) || _GLIBCXX_USE_DEPRECATED
586 // Postcondition: use_count() == 1 and __r.get() == 0
587 template<typename _Tp1
>
589 __shared_ptr(std::auto_ptr
<_Tp1
>& __r
)
590 : _M_ptr(__r
.get()), _M_refcount()
591 { // TODO requries delete __r.release() well-formed
592 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
593 typedef int _IsComplete
[sizeof(_Tp1
)];
594 _Tp1
* __tmp
= __r
.get();
595 _M_refcount
= __shared_count
<_Lp
>(__r
);
596 __enable_shared_from_this_helper(_M_refcount
, __tmp
, __tmp
);
601 template<typename _Tp1
>
602 __shared_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
, __static_cast_tag
)
603 : _M_ptr(static_cast<element_type
*>(__r
._M_ptr
)),
604 _M_refcount(__r
._M_refcount
)
607 template<typename _Tp1
>
608 __shared_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
, __const_cast_tag
)
609 : _M_ptr(const_cast<element_type
*>(__r
._M_ptr
)),
610 _M_refcount(__r
._M_refcount
)
613 template<typename _Tp1
>
614 __shared_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
, __dynamic_cast_tag
)
615 : _M_ptr(dynamic_cast<element_type
*>(__r
._M_ptr
)),
616 _M_refcount(__r
._M_refcount
)
618 if (_M_ptr
== 0) // need to allocate new counter -- the cast failed
619 _M_refcount
= __shared_count
<_Lp
>();
622 template<typename _Tp1
>
624 operator=(const __shared_ptr
<_Tp1
, _Lp
>& __r
) // never throws
627 _M_refcount
= __r
._M_refcount
; // __shared_count::op= doesn't throw
631 #if (__cplusplus < 201103L) || _GLIBCXX_USE_DEPRECATED
632 template<typename _Tp1
>
634 operator=(std::auto_ptr
<_Tp1
>& __r
)
636 __shared_ptr(__r
).swap(*this);
642 reset() // never throws
643 { __shared_ptr().swap(*this); }
645 template<typename _Tp1
>
647 reset(_Tp1
* __p
) // _Tp1 must be complete.
649 // Catch self-reset errors.
650 _GLIBCXX_DEBUG_ASSERT(__p
== 0 || __p
!= _M_ptr
);
651 __shared_ptr(__p
).swap(*this);
654 template<typename _Tp1
, typename _Deleter
>
656 reset(_Tp1
* __p
, _Deleter __d
)
657 { __shared_ptr(__p
, __d
).swap(*this); }
659 // Allow class instantiation when _Tp is [cv-qual] void.
660 typename
std::tr1::add_reference
<_Tp
>::type
661 operator*() const // never throws
663 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
668 operator->() const // never throws
670 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
675 get() const // never throws
678 // Implicit conversion to "bool"
680 typedef _Tp
* __shared_ptr::*__unspecified_bool_type
;
683 operator __unspecified_bool_type() const // never throws
684 { return _M_ptr
== 0 ? 0 : &__shared_ptr::_M_ptr
; }
687 unique() const // never throws
688 { return _M_refcount
._M_unique(); }
691 use_count() const // never throws
692 { return _M_refcount
._M_get_use_count(); }
695 swap(__shared_ptr
<_Tp
, _Lp
>& __other
) // never throws
697 std::swap(_M_ptr
, __other
._M_ptr
);
698 _M_refcount
._M_swap(__other
._M_refcount
);
703 _M_get_deleter(const std::type_info
& __ti
) const
704 { return _M_refcount
._M_get_deleter(__ti
); }
706 template<typename _Tp1
, _Lock_policy _Lp1
>
708 _M_less(const __shared_ptr
<_Tp1
, _Lp1
>& __rhs
) const
709 { return _M_refcount
< __rhs
._M_refcount
; }
711 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __shared_ptr
;
712 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __weak_ptr
;
714 template<typename _Del
, typename _Tp1
, _Lock_policy _Lp1
>
715 friend _Del
* get_deleter(const __shared_ptr
<_Tp1
, _Lp1
>&);
717 // Friends injected into enclosing namespace and found by ADL:
718 template<typename _Tp1
>
720 operator==(const __shared_ptr
& __a
, const __shared_ptr
<_Tp1
, _Lp
>& __b
)
721 { return __a
.get() == __b
.get(); }
723 template<typename _Tp1
>
725 operator!=(const __shared_ptr
& __a
, const __shared_ptr
<_Tp1
, _Lp
>& __b
)
726 { return __a
.get() != __b
.get(); }
728 template<typename _Tp1
>
730 operator<(const __shared_ptr
& __a
, const __shared_ptr
<_Tp1
, _Lp
>& __b
)
731 { return __a
._M_less(__b
); }
733 _Tp
* _M_ptr
; // Contained pointer.
734 __shared_count
<_Lp
> _M_refcount
; // Reference counter.
737 // 2.2.3.8 shared_ptr specialized algorithms.
738 template<typename _Tp
, _Lock_policy _Lp
>
740 swap(__shared_ptr
<_Tp
, _Lp
>& __a
, __shared_ptr
<_Tp
, _Lp
>& __b
)
743 // 2.2.3.9 shared_ptr casts
744 /* The seemingly equivalent
745 * shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))
746 * will eventually result in undefined behaviour,
747 * attempting to delete the same object twice.
749 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
750 inline __shared_ptr
<_Tp
, _Lp
>
751 static_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
752 { return __shared_ptr
<_Tp
, _Lp
>(__r
, __static_cast_tag()); }
754 /* The seemingly equivalent
755 * shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))
756 * will eventually result in undefined behaviour,
757 * attempting to delete the same object twice.
759 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
760 inline __shared_ptr
<_Tp
, _Lp
>
761 const_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
762 { return __shared_ptr
<_Tp
, _Lp
>(__r
, __const_cast_tag()); }
764 /* The seemingly equivalent
765 * shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))
766 * will eventually result in undefined behaviour,
767 * attempting to delete the same object twice.
769 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
770 inline __shared_ptr
<_Tp
, _Lp
>
771 dynamic_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
772 { return __shared_ptr
<_Tp
, _Lp
>(__r
, __dynamic_cast_tag()); }
774 // 2.2.3.7 shared_ptr I/O
775 template<typename _Ch
, typename _Tr
, typename _Tp
, _Lock_policy _Lp
>
776 std::basic_ostream
<_Ch
, _Tr
>&
777 operator<<(std::basic_ostream
<_Ch
, _Tr
>& __os
,
778 const __shared_ptr
<_Tp
, _Lp
>& __p
)
784 // 2.2.3.10 shared_ptr get_deleter (experimental)
785 template<typename _Del
, typename _Tp
, _Lock_policy _Lp
>
787 get_deleter(const __shared_ptr
<_Tp
, _Lp
>& __p
)
790 return static_cast<_Del
*>(__p
._M_get_deleter(typeid(_Del
)));
797 template<typename _Tp
, _Lock_policy _Lp
>
801 typedef _Tp element_type
;
804 : _M_ptr(0), _M_refcount() // never throws
807 // Generated copy constructor, assignment, destructor are fine.
809 // The "obvious" converting constructor implementation:
811 // template<typename _Tp1>
812 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
813 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
816 // has a serious problem.
818 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
819 // conversion may require access to *__r._M_ptr (virtual inheritance).
821 // It is not possible to avoid spurious access violations since
822 // in multithreaded programs __r._M_ptr may be invalidated at any point.
823 template<typename _Tp1
>
824 __weak_ptr(const __weak_ptr
<_Tp1
, _Lp
>& __r
)
825 : _M_refcount(__r
._M_refcount
) // never throws
827 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
828 _M_ptr
= __r
.lock().get();
831 template<typename _Tp1
>
832 __weak_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
833 : _M_ptr(__r
._M_ptr
), _M_refcount(__r
._M_refcount
) // never throws
834 { __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>) }
836 template<typename _Tp1
>
838 operator=(const __weak_ptr
<_Tp1
, _Lp
>& __r
) // never throws
840 _M_ptr
= __r
.lock().get();
841 _M_refcount
= __r
._M_refcount
;
845 template<typename _Tp1
>
847 operator=(const __shared_ptr
<_Tp1
, _Lp
>& __r
) // never throws
850 _M_refcount
= __r
._M_refcount
;
854 __shared_ptr
<_Tp
, _Lp
>
855 lock() const // never throws
858 // Optimization: avoid throw overhead.
860 return __shared_ptr
<element_type
, _Lp
>();
864 return __shared_ptr
<element_type
, _Lp
>(*this);
866 __catch(const bad_weak_ptr
&)
868 // Q: How can we get here?
869 // A: Another thread may have invalidated r after the
870 // use_count test above.
871 return __shared_ptr
<element_type
, _Lp
>();
875 // Optimization: avoid try/catch overhead when single threaded.
876 return expired() ? __shared_ptr
<element_type
, _Lp
>()
877 : __shared_ptr
<element_type
, _Lp
>(*this);
883 use_count() const // never throws
884 { return _M_refcount
._M_get_use_count(); }
887 expired() const // never throws
888 { return _M_refcount
._M_get_use_count() == 0; }
891 reset() // never throws
892 { __weak_ptr().swap(*this); }
895 swap(__weak_ptr
& __s
) // never throws
897 std::swap(_M_ptr
, __s
._M_ptr
);
898 _M_refcount
._M_swap(__s
._M_refcount
);
902 // Used by __enable_shared_from_this.
904 _M_assign(_Tp
* __ptr
, const __shared_count
<_Lp
>& __refcount
)
907 _M_refcount
= __refcount
;
910 template<typename _Tp1
>
912 _M_less(const __weak_ptr
<_Tp1
, _Lp
>& __rhs
) const
913 { return _M_refcount
< __rhs
._M_refcount
; }
915 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __shared_ptr
;
916 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __weak_ptr
;
917 friend class __enable_shared_from_this
<_Tp
, _Lp
>;
918 friend class enable_shared_from_this
<_Tp
>;
920 // Friend injected into namespace and found by ADL.
921 template<typename _Tp1
>
923 operator<(const __weak_ptr
& __lhs
, const __weak_ptr
<_Tp1
, _Lp
>& __rhs
)
924 { return __lhs
._M_less(__rhs
); }
926 _Tp
* _M_ptr
; // Contained pointer.
927 __weak_count
<_Lp
> _M_refcount
; // Reference counter.
930 // 2.2.4.7 weak_ptr specialized algorithms.
931 template<typename _Tp
, _Lock_policy _Lp
>
933 swap(__weak_ptr
<_Tp
, _Lp
>& __a
, __weak_ptr
<_Tp
, _Lp
>& __b
)
937 template<typename _Tp
, _Lock_policy _Lp
>
938 class __enable_shared_from_this
941 __enable_shared_from_this() { }
943 __enable_shared_from_this(const __enable_shared_from_this
&) { }
945 __enable_shared_from_this
&
946 operator=(const __enable_shared_from_this
&)
949 ~__enable_shared_from_this() { }
952 __shared_ptr
<_Tp
, _Lp
>
954 { return __shared_ptr
<_Tp
, _Lp
>(this->_M_weak_this
); }
956 __shared_ptr
<const _Tp
, _Lp
>
957 shared_from_this() const
958 { return __shared_ptr
<const _Tp
, _Lp
>(this->_M_weak_this
); }
961 template<typename _Tp1
>
963 _M_weak_assign(_Tp1
* __p
, const __shared_count
<_Lp
>& __n
) const
964 { _M_weak_this
._M_assign(__p
, __n
); }
966 template<typename _Tp1
>
968 __enable_shared_from_this_helper(const __shared_count
<_Lp
>& __pn
,
969 const __enable_shared_from_this
* __pe
,
973 __pe
->_M_weak_assign(const_cast<_Tp1
*>(__px
), __pn
);
976 mutable __weak_ptr
<_Tp
, _Lp
> _M_weak_this
;
980 // The actual shared_ptr, with forwarding constructors and
981 // assignment operators.
982 template<typename _Tp
>
984 : public __shared_ptr
<_Tp
>
988 : __shared_ptr
<_Tp
>() { }
990 template<typename _Tp1
>
992 shared_ptr(_Tp1
* __p
)
993 : __shared_ptr
<_Tp
>(__p
) { }
995 template<typename _Tp1
, typename _Deleter
>
996 shared_ptr(_Tp1
* __p
, _Deleter __d
)
997 : __shared_ptr
<_Tp
>(__p
, __d
) { }
999 template<typename _Tp1
>
1000 shared_ptr(const shared_ptr
<_Tp1
>& __r
)
1001 : __shared_ptr
<_Tp
>(__r
) { }
1003 template<typename _Tp1
>
1005 shared_ptr(const weak_ptr
<_Tp1
>& __r
)
1006 : __shared_ptr
<_Tp
>(__r
) { }
1008 #if (__cplusplus < 201103L) || _GLIBCXX_USE_DEPRECATED
1009 template<typename _Tp1
>
1011 shared_ptr(std::auto_ptr
<_Tp1
>& __r
)
1012 : __shared_ptr
<_Tp
>(__r
) { }
1015 template<typename _Tp1
>
1016 shared_ptr(const shared_ptr
<_Tp1
>& __r
, __static_cast_tag
)
1017 : __shared_ptr
<_Tp
>(__r
, __static_cast_tag()) { }
1019 template<typename _Tp1
>
1020 shared_ptr(const shared_ptr
<_Tp1
>& __r
, __const_cast_tag
)
1021 : __shared_ptr
<_Tp
>(__r
, __const_cast_tag()) { }
1023 template<typename _Tp1
>
1024 shared_ptr(const shared_ptr
<_Tp1
>& __r
, __dynamic_cast_tag
)
1025 : __shared_ptr
<_Tp
>(__r
, __dynamic_cast_tag()) { }
1027 template<typename _Tp1
>
1029 operator=(const shared_ptr
<_Tp1
>& __r
) // never throws
1031 this->__shared_ptr
<_Tp
>::operator=(__r
);
1035 #if (__cplusplus < 201103L) || _GLIBCXX_USE_DEPRECATED
1036 template<typename _Tp1
>
1038 operator=(std::auto_ptr
<_Tp1
>& __r
)
1040 this->__shared_ptr
<_Tp
>::operator=(__r
);
1046 // 2.2.3.8 shared_ptr specialized algorithms.
1047 template<typename _Tp
>
1049 swap(__shared_ptr
<_Tp
>& __a
, __shared_ptr
<_Tp
>& __b
)
1052 template<typename _Tp
, typename _Tp1
>
1053 inline shared_ptr
<_Tp
>
1054 static_pointer_cast(const shared_ptr
<_Tp1
>& __r
)
1055 { return shared_ptr
<_Tp
>(__r
, __static_cast_tag()); }
1057 template<typename _Tp
, typename _Tp1
>
1058 inline shared_ptr
<_Tp
>
1059 const_pointer_cast(const shared_ptr
<_Tp1
>& __r
)
1060 { return shared_ptr
<_Tp
>(__r
, __const_cast_tag()); }
1062 template<typename _Tp
, typename _Tp1
>
1063 inline shared_ptr
<_Tp
>
1064 dynamic_pointer_cast(const shared_ptr
<_Tp1
>& __r
)
1065 { return shared_ptr
<_Tp
>(__r
, __dynamic_cast_tag()); }
1068 // The actual weak_ptr, with forwarding constructors and
1069 // assignment operators.
1070 template<typename _Tp
>
1072 : public __weak_ptr
<_Tp
>
1076 : __weak_ptr
<_Tp
>() { }
1078 template<typename _Tp1
>
1079 weak_ptr(const weak_ptr
<_Tp1
>& __r
)
1080 : __weak_ptr
<_Tp
>(__r
) { }
1082 template<typename _Tp1
>
1083 weak_ptr(const shared_ptr
<_Tp1
>& __r
)
1084 : __weak_ptr
<_Tp
>(__r
) { }
1086 template<typename _Tp1
>
1088 operator=(const weak_ptr
<_Tp1
>& __r
) // never throws
1090 this->__weak_ptr
<_Tp
>::operator=(__r
);
1094 template<typename _Tp1
>
1096 operator=(const shared_ptr
<_Tp1
>& __r
) // never throws
1098 this->__weak_ptr
<_Tp
>::operator=(__r
);
1103 lock() const // never throws
1106 if (this->expired())
1107 return shared_ptr
<_Tp
>();
1111 return shared_ptr
<_Tp
>(*this);
1113 __catch(const bad_weak_ptr
&)
1115 return shared_ptr
<_Tp
>();
1118 return this->expired() ? shared_ptr
<_Tp
>()
1119 : shared_ptr
<_Tp
>(*this);
1124 template<typename _Tp
>
1125 class enable_shared_from_this
1128 enable_shared_from_this() { }
1130 enable_shared_from_this(const enable_shared_from_this
&) { }
1132 enable_shared_from_this
&
1133 operator=(const enable_shared_from_this
&)
1136 ~enable_shared_from_this() { }
1141 { return shared_ptr
<_Tp
>(this->_M_weak_this
); }
1143 shared_ptr
<const _Tp
>
1144 shared_from_this() const
1145 { return shared_ptr
<const _Tp
>(this->_M_weak_this
); }
1148 template<typename _Tp1
>
1150 _M_weak_assign(_Tp1
* __p
, const __shared_count
<>& __n
) const
1151 { _M_weak_this
._M_assign(__p
, __n
); }
1153 template<typename _Tp1
>
1155 __enable_shared_from_this_helper(const __shared_count
<>& __pn
,
1156 const enable_shared_from_this
* __pe
,
1160 __pe
->_M_weak_assign(const_cast<_Tp1
*>(__px
), __pn
);
1163 mutable weak_ptr
<_Tp
> _M_weak_this
;
1167 _GLIBCXX_END_NAMESPACE_VERSION
1170 #endif // _TR1_SHARED_PTR_H