1 // <tr1/shared_ptr.h> -*- C++ -*-
3 // Copyright (C) 2007-2014 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
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)
56 _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 _GLIBCXX_READ_MEM_BARRIER
;
149 _GLIBCXX_WRITE_MEM_BARRIER
;
152 // Be race-detector-friendly. For more info see bits/c++config.
153 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count
);
154 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count
,
157 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count
);
164 _M_weak_add_ref() // nothrow
165 { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count
, 1); }
168 _M_weak_release() // nothrow
170 // Be race-detector-friendly. For more info see bits/c++config.
171 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count
);
172 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count
, -1) == 1)
174 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count
);
175 if (_Mutex_base
<_Lp
>::_S_need_barriers
)
178 // destroy() must observe results of dispose()
179 _GLIBCXX_READ_MEM_BARRIER
;
180 _GLIBCXX_WRITE_MEM_BARRIER
;
187 _M_get_use_count() const // nothrow
189 // No memory barrier is used here so there is no synchronization
190 // with other threads.
191 return const_cast<const volatile _Atomic_word
&>(_M_use_count
);
195 _Sp_counted_base(_Sp_counted_base
const&);
196 _Sp_counted_base
& operator=(_Sp_counted_base
const&);
198 _Atomic_word _M_use_count
; // #shared
199 _Atomic_word _M_weak_count
; // #weak + (#shared != 0)
204 _Sp_counted_base
<_S_single
>::
207 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count
, 1) == 0)
210 __throw_bad_weak_ptr();
216 _Sp_counted_base
<_S_mutex
>::
219 __gnu_cxx::__scoped_lock
sentry(*this);
220 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count
, 1) == 0)
223 __throw_bad_weak_ptr();
229 _Sp_counted_base
<_S_atomic
>::
232 // Perform lock-free add-if-not-zero operation.
233 _Atomic_word __count
= _M_use_count
;
237 __throw_bad_weak_ptr();
238 // Replace the current counter value with the old value + 1, as
239 // long as it's not changed meanwhile.
241 while (!__atomic_compare_exchange_n(&_M_use_count
, &__count
, __count
+ 1,
242 true, __ATOMIC_ACQ_REL
,
246 template<typename _Ptr
, typename _Deleter
, _Lock_policy _Lp
>
247 class _Sp_counted_base_impl
248 : public _Sp_counted_base
<_Lp
>
251 // Precondition: __d(__p) must not throw.
252 _Sp_counted_base_impl(_Ptr __p
, _Deleter __d
)
253 : _M_ptr(__p
), _M_del(__d
) { }
256 _M_dispose() // nothrow
260 _M_get_deleter(const std::type_info
& __ti
)
263 return __ti
== typeid(_Deleter
) ? &_M_del
: 0;
270 _Sp_counted_base_impl(const _Sp_counted_base_impl
&);
271 _Sp_counted_base_impl
& operator=(const _Sp_counted_base_impl
&);
273 _Ptr _M_ptr
; // copy constructor must not throw
274 _Deleter _M_del
; // copy constructor must not throw
277 template<_Lock_policy _Lp
= __default_lock_policy
>
280 template<typename _Tp
>
283 typedef void result_type
;
284 typedef _Tp
* argument_type
;
285 void operator()(_Tp
* __p
) const { delete __p
; }
288 template<_Lock_policy _Lp
= __default_lock_policy
>
293 : _M_pi(0) // nothrow
296 template<typename _Ptr
>
297 __shared_count(_Ptr __p
) : _M_pi(0)
301 typedef typename
std::tr1::remove_pointer
<_Ptr
>::type _Tp
;
302 _M_pi
= new _Sp_counted_base_impl
<_Ptr
, _Sp_deleter
<_Tp
>, _Lp
>(
303 __p
, _Sp_deleter
<_Tp
>());
308 __throw_exception_again
;
312 template<typename _Ptr
, typename _Deleter
>
313 __shared_count(_Ptr __p
, _Deleter __d
) : _M_pi(0)
317 _M_pi
= new _Sp_counted_base_impl
<_Ptr
, _Deleter
, _Lp
>(__p
, __d
);
321 __d(__p
); // Call _Deleter on __p.
322 __throw_exception_again
;
326 // Special case for auto_ptr<_Tp> to provide the strong guarantee.
327 template<typename _Tp
>
329 __shared_count(std::auto_ptr
<_Tp
>& __r
)
330 : _M_pi(new _Sp_counted_base_impl
<_Tp
*,
331 _Sp_deleter
<_Tp
>, _Lp
>(__r
.get(), _Sp_deleter
<_Tp
>()))
334 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
336 __shared_count(const __weak_count
<_Lp
>& __r
);
338 ~__shared_count() // nothrow
344 __shared_count(const __shared_count
& __r
)
345 : _M_pi(__r
._M_pi
) // nothrow
348 _M_pi
->_M_add_ref_copy();
352 operator=(const __shared_count
& __r
) // nothrow
354 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
358 __tmp
->_M_add_ref_copy();
367 _M_swap(__shared_count
& __r
) // nothrow
369 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
375 _M_get_use_count() const // nothrow
376 { return _M_pi
!= 0 ? _M_pi
->_M_get_use_count() : 0; }
379 _M_unique() const // nothrow
380 { return this->_M_get_use_count() == 1; }
383 operator==(const __shared_count
& __a
, const __shared_count
& __b
)
384 { return __a
._M_pi
== __b
._M_pi
; }
387 operator<(const __shared_count
& __a
, const __shared_count
& __b
)
388 { return std::less
<_Sp_counted_base
<_Lp
>*>()(__a
._M_pi
, __b
._M_pi
); }
391 _M_get_deleter(const std::type_info
& __ti
) const
392 { return _M_pi
? _M_pi
->_M_get_deleter(__ti
) : 0; }
395 friend class __weak_count
<_Lp
>;
397 _Sp_counted_base
<_Lp
>* _M_pi
;
401 template<_Lock_policy _Lp
>
406 : _M_pi(0) // nothrow
409 __weak_count(const __shared_count
<_Lp
>& __r
)
410 : _M_pi(__r
._M_pi
) // nothrow
413 _M_pi
->_M_weak_add_ref();
416 __weak_count(const __weak_count
<_Lp
>& __r
)
417 : _M_pi(__r
._M_pi
) // nothrow
420 _M_pi
->_M_weak_add_ref();
423 ~__weak_count() // nothrow
426 _M_pi
->_M_weak_release();
430 operator=(const __shared_count
<_Lp
>& __r
) // nothrow
432 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
434 __tmp
->_M_weak_add_ref();
436 _M_pi
->_M_weak_release();
442 operator=(const __weak_count
<_Lp
>& __r
) // nothrow
444 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
446 __tmp
->_M_weak_add_ref();
448 _M_pi
->_M_weak_release();
454 _M_swap(__weak_count
<_Lp
>& __r
) // nothrow
456 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
462 _M_get_use_count() const // nothrow
463 { return _M_pi
!= 0 ? _M_pi
->_M_get_use_count() : 0; }
466 operator==(const __weak_count
<_Lp
>& __a
, const __weak_count
<_Lp
>& __b
)
467 { return __a
._M_pi
== __b
._M_pi
; }
470 operator<(const __weak_count
<_Lp
>& __a
, const __weak_count
<_Lp
>& __b
)
471 { return std::less
<_Sp_counted_base
<_Lp
>*>()(__a
._M_pi
, __b
._M_pi
); }
474 friend class __shared_count
<_Lp
>;
476 _Sp_counted_base
<_Lp
>* _M_pi
;
479 // now that __weak_count is defined we can define this constructor:
480 template<_Lock_policy _Lp
>
482 __shared_count
<_Lp
>::
483 __shared_count(const __weak_count
<_Lp
>& __r
)
487 _M_pi
->_M_add_ref_lock();
489 __throw_bad_weak_ptr();
492 // Forward declarations.
493 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
496 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
499 template<typename _Tp
, _Lock_policy _Lp
= __default_lock_policy
>
500 class __enable_shared_from_this
;
502 template<typename _Tp
>
505 template<typename _Tp
>
508 template<typename _Tp
>
509 class enable_shared_from_this
;
511 // Support for enable_shared_from_this.
513 // Friend of __enable_shared_from_this.
514 template<_Lock_policy _Lp
, typename _Tp1
, typename _Tp2
>
516 __enable_shared_from_this_helper(const __shared_count
<_Lp
>&,
517 const __enable_shared_from_this
<_Tp1
,
520 // Friend of enable_shared_from_this.
521 template<typename _Tp1
, typename _Tp2
>
523 __enable_shared_from_this_helper(const __shared_count
<>&,
524 const enable_shared_from_this
<_Tp1
>*,
527 template<_Lock_policy _Lp
>
529 __enable_shared_from_this_helper(const __shared_count
<_Lp
>&, ...)
533 struct __static_cast_tag
{ };
534 struct __const_cast_tag
{ };
535 struct __dynamic_cast_tag
{ };
537 // A smart pointer with reference-counted copy semantics. The
538 // object pointed to is deleted when the last shared_ptr pointing to
539 // it is destroyed or reset.
540 template<typename _Tp
, _Lock_policy _Lp
>
544 typedef _Tp element_type
;
547 : _M_ptr(0), _M_refcount() // never throws
550 template<typename _Tp1
>
552 __shared_ptr(_Tp1
* __p
)
553 : _M_ptr(__p
), _M_refcount(__p
)
555 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
556 typedef int _IsComplete
[sizeof(_Tp1
)];
557 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
560 template<typename _Tp1
, typename _Deleter
>
561 __shared_ptr(_Tp1
* __p
, _Deleter __d
)
562 : _M_ptr(__p
), _M_refcount(__p
, __d
)
564 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
565 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
566 __enable_shared_from_this_helper(_M_refcount
, __p
, __p
);
569 // generated copy constructor, assignment, destructor are fine.
571 template<typename _Tp1
>
572 __shared_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
573 : _M_ptr(__r
._M_ptr
), _M_refcount(__r
._M_refcount
) // never throws
574 { __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>) }
576 template<typename _Tp1
>
578 __shared_ptr(const __weak_ptr
<_Tp1
, _Lp
>& __r
)
579 : _M_refcount(__r
._M_refcount
) // may throw
581 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
582 // It is now safe to copy __r._M_ptr, as _M_refcount(__r._M_refcount)
587 #if (__cplusplus < 201103L) || _GLIBCXX_USE_DEPRECATED
588 // Postcondition: use_count() == 1 and __r.get() == 0
589 template<typename _Tp1
>
591 __shared_ptr(std::auto_ptr
<_Tp1
>& __r
)
592 : _M_ptr(__r
.get()), _M_refcount()
593 { // TODO requries delete __r.release() well-formed
594 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
595 typedef int _IsComplete
[sizeof(_Tp1
)];
596 _Tp1
* __tmp
= __r
.get();
597 _M_refcount
= __shared_count
<_Lp
>(__r
);
598 __enable_shared_from_this_helper(_M_refcount
, __tmp
, __tmp
);
603 template<typename _Tp1
>
604 __shared_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
, __static_cast_tag
)
605 : _M_ptr(static_cast<element_type
*>(__r
._M_ptr
)),
606 _M_refcount(__r
._M_refcount
)
609 template<typename _Tp1
>
610 __shared_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
, __const_cast_tag
)
611 : _M_ptr(const_cast<element_type
*>(__r
._M_ptr
)),
612 _M_refcount(__r
._M_refcount
)
615 template<typename _Tp1
>
616 __shared_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
, __dynamic_cast_tag
)
617 : _M_ptr(dynamic_cast<element_type
*>(__r
._M_ptr
)),
618 _M_refcount(__r
._M_refcount
)
620 if (_M_ptr
== 0) // need to allocate new counter -- the cast failed
621 _M_refcount
= __shared_count
<_Lp
>();
624 template<typename _Tp1
>
626 operator=(const __shared_ptr
<_Tp1
, _Lp
>& __r
) // never throws
629 _M_refcount
= __r
._M_refcount
; // __shared_count::op= doesn't throw
633 #if (__cplusplus < 201103L) || _GLIBCXX_USE_DEPRECATED
634 template<typename _Tp1
>
636 operator=(std::auto_ptr
<_Tp1
>& __r
)
638 __shared_ptr(__r
).swap(*this);
644 reset() // never throws
645 { __shared_ptr().swap(*this); }
647 template<typename _Tp1
>
649 reset(_Tp1
* __p
) // _Tp1 must be complete.
651 // Catch self-reset errors.
652 _GLIBCXX_DEBUG_ASSERT(__p
== 0 || __p
!= _M_ptr
);
653 __shared_ptr(__p
).swap(*this);
656 template<typename _Tp1
, typename _Deleter
>
658 reset(_Tp1
* __p
, _Deleter __d
)
659 { __shared_ptr(__p
, __d
).swap(*this); }
661 // Allow class instantiation when _Tp is [cv-qual] void.
662 typename
std::tr1::add_reference
<_Tp
>::type
663 operator*() const // never throws
665 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
670 operator->() const // never throws
672 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
677 get() const // never throws
680 // Implicit conversion to "bool"
682 typedef _Tp
* __shared_ptr::*__unspecified_bool_type
;
685 operator __unspecified_bool_type() const // never throws
686 { return _M_ptr
== 0 ? 0 : &__shared_ptr::_M_ptr
; }
689 unique() const // never throws
690 { return _M_refcount
._M_unique(); }
693 use_count() const // never throws
694 { return _M_refcount
._M_get_use_count(); }
697 swap(__shared_ptr
<_Tp
, _Lp
>& __other
) // never throws
699 std::swap(_M_ptr
, __other
._M_ptr
);
700 _M_refcount
._M_swap(__other
._M_refcount
);
705 _M_get_deleter(const std::type_info
& __ti
) const
706 { return _M_refcount
._M_get_deleter(__ti
); }
708 template<typename _Tp1
, _Lock_policy _Lp1
>
710 _M_less(const __shared_ptr
<_Tp1
, _Lp1
>& __rhs
) const
711 { return _M_refcount
< __rhs
._M_refcount
; }
713 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __shared_ptr
;
714 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __weak_ptr
;
716 template<typename _Del
, typename _Tp1
, _Lock_policy _Lp1
>
717 friend _Del
* get_deleter(const __shared_ptr
<_Tp1
, _Lp1
>&);
719 // Friends injected into enclosing namespace and found by ADL:
720 template<typename _Tp1
>
722 operator==(const __shared_ptr
& __a
, const __shared_ptr
<_Tp1
, _Lp
>& __b
)
723 { return __a
.get() == __b
.get(); }
725 template<typename _Tp1
>
727 operator!=(const __shared_ptr
& __a
, const __shared_ptr
<_Tp1
, _Lp
>& __b
)
728 { return __a
.get() != __b
.get(); }
730 template<typename _Tp1
>
732 operator<(const __shared_ptr
& __a
, const __shared_ptr
<_Tp1
, _Lp
>& __b
)
733 { return __a
._M_less(__b
); }
735 _Tp
* _M_ptr
; // Contained pointer.
736 __shared_count
<_Lp
> _M_refcount
; // Reference counter.
739 // 2.2.3.8 shared_ptr specialized algorithms.
740 template<typename _Tp
, _Lock_policy _Lp
>
742 swap(__shared_ptr
<_Tp
, _Lp
>& __a
, __shared_ptr
<_Tp
, _Lp
>& __b
)
745 // 2.2.3.9 shared_ptr casts
746 /* The seemingly equivalent
747 * shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))
748 * will eventually result in undefined behaviour,
749 * attempting to delete the same object twice.
751 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
752 inline __shared_ptr
<_Tp
, _Lp
>
753 static_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
754 { return __shared_ptr
<_Tp
, _Lp
>(__r
, __static_cast_tag()); }
756 /* The seemingly equivalent
757 * shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))
758 * will eventually result in undefined behaviour,
759 * attempting to delete the same object twice.
761 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
762 inline __shared_ptr
<_Tp
, _Lp
>
763 const_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
764 { return __shared_ptr
<_Tp
, _Lp
>(__r
, __const_cast_tag()); }
766 /* The seemingly equivalent
767 * shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))
768 * will eventually result in undefined behaviour,
769 * attempting to delete the same object twice.
771 template<typename _Tp
, typename _Tp1
, _Lock_policy _Lp
>
772 inline __shared_ptr
<_Tp
, _Lp
>
773 dynamic_pointer_cast(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
774 { return __shared_ptr
<_Tp
, _Lp
>(__r
, __dynamic_cast_tag()); }
776 // 2.2.3.7 shared_ptr I/O
777 template<typename _Ch
, typename _Tr
, typename _Tp
, _Lock_policy _Lp
>
778 std::basic_ostream
<_Ch
, _Tr
>&
779 operator<<(std::basic_ostream
<_Ch
, _Tr
>& __os
,
780 const __shared_ptr
<_Tp
, _Lp
>& __p
)
786 // 2.2.3.10 shared_ptr get_deleter (experimental)
787 template<typename _Del
, typename _Tp
, _Lock_policy _Lp
>
789 get_deleter(const __shared_ptr
<_Tp
, _Lp
>& __p
)
792 return static_cast<_Del
*>(__p
._M_get_deleter(typeid(_Del
)));
799 template<typename _Tp
, _Lock_policy _Lp
>
803 typedef _Tp element_type
;
806 : _M_ptr(0), _M_refcount() // never throws
809 // Generated copy constructor, assignment, destructor are fine.
811 // The "obvious" converting constructor implementation:
813 // template<typename _Tp1>
814 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
815 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
818 // has a serious problem.
820 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
821 // conversion may require access to *__r._M_ptr (virtual inheritance).
823 // It is not possible to avoid spurious access violations since
824 // in multithreaded programs __r._M_ptr may be invalidated at any point.
825 template<typename _Tp1
>
826 __weak_ptr(const __weak_ptr
<_Tp1
, _Lp
>& __r
)
827 : _M_refcount(__r
._M_refcount
) // never throws
829 __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>)
830 _M_ptr
= __r
.lock().get();
833 template<typename _Tp1
>
834 __weak_ptr(const __shared_ptr
<_Tp1
, _Lp
>& __r
)
835 : _M_ptr(__r
._M_ptr
), _M_refcount(__r
._M_refcount
) // never throws
836 { __glibcxx_function_requires(_ConvertibleConcept
<_Tp1
*, _Tp
*>) }
838 template<typename _Tp1
>
840 operator=(const __weak_ptr
<_Tp1
, _Lp
>& __r
) // never throws
842 _M_ptr
= __r
.lock().get();
843 _M_refcount
= __r
._M_refcount
;
847 template<typename _Tp1
>
849 operator=(const __shared_ptr
<_Tp1
, _Lp
>& __r
) // never throws
852 _M_refcount
= __r
._M_refcount
;
856 __shared_ptr
<_Tp
, _Lp
>
857 lock() const // never throws
860 // Optimization: avoid throw overhead.
862 return __shared_ptr
<element_type
, _Lp
>();
866 return __shared_ptr
<element_type
, _Lp
>(*this);
868 __catch(const bad_weak_ptr
&)
870 // Q: How can we get here?
871 // A: Another thread may have invalidated r after the
872 // use_count test above.
873 return __shared_ptr
<element_type
, _Lp
>();
877 // Optimization: avoid try/catch overhead when single threaded.
878 return expired() ? __shared_ptr
<element_type
, _Lp
>()
879 : __shared_ptr
<element_type
, _Lp
>(*this);
885 use_count() const // never throws
886 { return _M_refcount
._M_get_use_count(); }
889 expired() const // never throws
890 { return _M_refcount
._M_get_use_count() == 0; }
893 reset() // never throws
894 { __weak_ptr().swap(*this); }
897 swap(__weak_ptr
& __s
) // never throws
899 std::swap(_M_ptr
, __s
._M_ptr
);
900 _M_refcount
._M_swap(__s
._M_refcount
);
904 // Used by __enable_shared_from_this.
906 _M_assign(_Tp
* __ptr
, const __shared_count
<_Lp
>& __refcount
)
909 _M_refcount
= __refcount
;
912 template<typename _Tp1
>
914 _M_less(const __weak_ptr
<_Tp1
, _Lp
>& __rhs
) const
915 { return _M_refcount
< __rhs
._M_refcount
; }
917 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __shared_ptr
;
918 template<typename _Tp1
, _Lock_policy _Lp1
> friend class __weak_ptr
;
919 friend class __enable_shared_from_this
<_Tp
, _Lp
>;
920 friend class enable_shared_from_this
<_Tp
>;
922 // Friend injected into namespace and found by ADL.
923 template<typename _Tp1
>
925 operator<(const __weak_ptr
& __lhs
, const __weak_ptr
<_Tp1
, _Lp
>& __rhs
)
926 { return __lhs
._M_less(__rhs
); }
928 _Tp
* _M_ptr
; // Contained pointer.
929 __weak_count
<_Lp
> _M_refcount
; // Reference counter.
932 // 2.2.4.7 weak_ptr specialized algorithms.
933 template<typename _Tp
, _Lock_policy _Lp
>
935 swap(__weak_ptr
<_Tp
, _Lp
>& __a
, __weak_ptr
<_Tp
, _Lp
>& __b
)
939 template<typename _Tp
, _Lock_policy _Lp
>
940 class __enable_shared_from_this
943 __enable_shared_from_this() { }
945 __enable_shared_from_this(const __enable_shared_from_this
&) { }
947 __enable_shared_from_this
&
948 operator=(const __enable_shared_from_this
&)
951 ~__enable_shared_from_this() { }
954 __shared_ptr
<_Tp
, _Lp
>
956 { return __shared_ptr
<_Tp
, _Lp
>(this->_M_weak_this
); }
958 __shared_ptr
<const _Tp
, _Lp
>
959 shared_from_this() const
960 { return __shared_ptr
<const _Tp
, _Lp
>(this->_M_weak_this
); }
963 template<typename _Tp1
>
965 _M_weak_assign(_Tp1
* __p
, const __shared_count
<_Lp
>& __n
) const
966 { _M_weak_this
._M_assign(__p
, __n
); }
968 template<typename _Tp1
>
970 __enable_shared_from_this_helper(const __shared_count
<_Lp
>& __pn
,
971 const __enable_shared_from_this
* __pe
,
975 __pe
->_M_weak_assign(const_cast<_Tp1
*>(__px
), __pn
);
978 mutable __weak_ptr
<_Tp
, _Lp
> _M_weak_this
;
982 // The actual shared_ptr, with forwarding constructors and
983 // assignment operators.
984 template<typename _Tp
>
986 : public __shared_ptr
<_Tp
>
990 : __shared_ptr
<_Tp
>() { }
992 template<typename _Tp1
>
994 shared_ptr(_Tp1
* __p
)
995 : __shared_ptr
<_Tp
>(__p
) { }
997 template<typename _Tp1
, typename _Deleter
>
998 shared_ptr(_Tp1
* __p
, _Deleter __d
)
999 : __shared_ptr
<_Tp
>(__p
, __d
) { }
1001 template<typename _Tp1
>
1002 shared_ptr(const shared_ptr
<_Tp1
>& __r
)
1003 : __shared_ptr
<_Tp
>(__r
) { }
1005 template<typename _Tp1
>
1007 shared_ptr(const weak_ptr
<_Tp1
>& __r
)
1008 : __shared_ptr
<_Tp
>(__r
) { }
1010 #if (__cplusplus < 201103L) || _GLIBCXX_USE_DEPRECATED
1011 template<typename _Tp1
>
1013 shared_ptr(std::auto_ptr
<_Tp1
>& __r
)
1014 : __shared_ptr
<_Tp
>(__r
) { }
1017 template<typename _Tp1
>
1018 shared_ptr(const shared_ptr
<_Tp1
>& __r
, __static_cast_tag
)
1019 : __shared_ptr
<_Tp
>(__r
, __static_cast_tag()) { }
1021 template<typename _Tp1
>
1022 shared_ptr(const shared_ptr
<_Tp1
>& __r
, __const_cast_tag
)
1023 : __shared_ptr
<_Tp
>(__r
, __const_cast_tag()) { }
1025 template<typename _Tp1
>
1026 shared_ptr(const shared_ptr
<_Tp1
>& __r
, __dynamic_cast_tag
)
1027 : __shared_ptr
<_Tp
>(__r
, __dynamic_cast_tag()) { }
1029 template<typename _Tp1
>
1031 operator=(const shared_ptr
<_Tp1
>& __r
) // never throws
1033 this->__shared_ptr
<_Tp
>::operator=(__r
);
1037 #if (__cplusplus < 201103L) || _GLIBCXX_USE_DEPRECATED
1038 template<typename _Tp1
>
1040 operator=(std::auto_ptr
<_Tp1
>& __r
)
1042 this->__shared_ptr
<_Tp
>::operator=(__r
);
1048 // 2.2.3.8 shared_ptr specialized algorithms.
1049 template<typename _Tp
>
1051 swap(__shared_ptr
<_Tp
>& __a
, __shared_ptr
<_Tp
>& __b
)
1054 template<typename _Tp
, typename _Tp1
>
1055 inline shared_ptr
<_Tp
>
1056 static_pointer_cast(const shared_ptr
<_Tp1
>& __r
)
1057 { return shared_ptr
<_Tp
>(__r
, __static_cast_tag()); }
1059 template<typename _Tp
, typename _Tp1
>
1060 inline shared_ptr
<_Tp
>
1061 const_pointer_cast(const shared_ptr
<_Tp1
>& __r
)
1062 { return shared_ptr
<_Tp
>(__r
, __const_cast_tag()); }
1064 template<typename _Tp
, typename _Tp1
>
1065 inline shared_ptr
<_Tp
>
1066 dynamic_pointer_cast(const shared_ptr
<_Tp1
>& __r
)
1067 { return shared_ptr
<_Tp
>(__r
, __dynamic_cast_tag()); }
1070 // The actual weak_ptr, with forwarding constructors and
1071 // assignment operators.
1072 template<typename _Tp
>
1074 : public __weak_ptr
<_Tp
>
1078 : __weak_ptr
<_Tp
>() { }
1080 template<typename _Tp1
>
1081 weak_ptr(const weak_ptr
<_Tp1
>& __r
)
1082 : __weak_ptr
<_Tp
>(__r
) { }
1084 template<typename _Tp1
>
1085 weak_ptr(const shared_ptr
<_Tp1
>& __r
)
1086 : __weak_ptr
<_Tp
>(__r
) { }
1088 template<typename _Tp1
>
1090 operator=(const weak_ptr
<_Tp1
>& __r
) // never throws
1092 this->__weak_ptr
<_Tp
>::operator=(__r
);
1096 template<typename _Tp1
>
1098 operator=(const shared_ptr
<_Tp1
>& __r
) // never throws
1100 this->__weak_ptr
<_Tp
>::operator=(__r
);
1105 lock() const // never throws
1108 if (this->expired())
1109 return shared_ptr
<_Tp
>();
1113 return shared_ptr
<_Tp
>(*this);
1115 __catch(const bad_weak_ptr
&)
1117 return shared_ptr
<_Tp
>();
1120 return this->expired() ? shared_ptr
<_Tp
>()
1121 : shared_ptr
<_Tp
>(*this);
1126 template<typename _Tp
>
1127 class enable_shared_from_this
1130 enable_shared_from_this() { }
1132 enable_shared_from_this(const enable_shared_from_this
&) { }
1134 enable_shared_from_this
&
1135 operator=(const enable_shared_from_this
&)
1138 ~enable_shared_from_this() { }
1143 { return shared_ptr
<_Tp
>(this->_M_weak_this
); }
1145 shared_ptr
<const _Tp
>
1146 shared_from_this() const
1147 { return shared_ptr
<const _Tp
>(this->_M_weak_this
); }
1150 template<typename _Tp1
>
1152 _M_weak_assign(_Tp1
* __p
, const __shared_count
<>& __n
) const
1153 { _M_weak_this
._M_assign(__p
, __n
); }
1155 template<typename _Tp1
>
1157 __enable_shared_from_this_helper(const __shared_count
<>& __pn
,
1158 const enable_shared_from_this
* __pe
,
1162 __pe
->_M_weak_assign(const_cast<_Tp1
*>(__px
), __pn
);
1165 mutable weak_ptr
<_Tp
> _M_weak_this
;
1168 _GLIBCXX_END_NAMESPACE_VERSION
1172 #endif // _TR1_SHARED_PTR_H