1 // <tr1/shared_ptr.h> -*- C++ -*-
3 // Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
27 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
30 // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
31 // Copyright (C) 2001, 2002, 2003 Peter Dimov
34 // Copyright (C) 2001, 2002, 2003 Peter Dimov
36 // enable_shared_from_this.hpp
37 // Copyright (C) 2002 Peter Dimov
39 // Distributed under the Boost Software License, Version 1.0. (See
40 // accompanying file LICENSE_1_0.txt or copy at
41 // http://www.boost.org/LICENSE_1_0.txt)
43 // GCC Note: based on version 1.32.0 of the Boost library.
45 /** @file tr1/shared_ptr.h
46 * This is an internal header file, included by other library headers.
47 * Do not attempt to use it directly. @headername{tr1/memory}
50 #ifndef _TR1_SHARED_PTR_H
51 #define _TR1_SHARED_PTR_H 1
53 namespace std _GLIBCXX_VISIBILITY(default)
57 _GLIBCXX_BEGIN_NAMESPACE_VERSION
60 * @brief Exception possibly thrown by @c shared_ptr.
63 class bad_weak_ptr : public std::exception
68 { return "tr1::bad_weak_ptr"; }
71 // Substitute for bad_weak_ptr object in the case of -fno-exceptions.
73 __throw_bad_weak_ptr()
82 using __gnu_cxx::_Lock_policy;
83 using __gnu_cxx::__default_lock_policy;
84 using __gnu_cxx::_S_single;
85 using __gnu_cxx::_S_mutex;
86 using __gnu_cxx::_S_atomic;
88 // Empty helper class except when the template argument is _S_mutex.
89 template<_Lock_policy _Lp>
93 // The atomic policy uses fully-fenced builtins, single doesn't care.
94 enum { _S_need_barriers = 0 };
98 class _Mutex_base<_S_mutex>
99 : public __gnu_cxx::__mutex
102 // This policy is used when atomic builtins are not available.
103 // The replacement atomic operations might not have the necessary
105 enum { _S_need_barriers = 1 };
108 template<_Lock_policy _Lp = __default_lock_policy>
109 class _Sp_counted_base
110 : public _Mutex_base<_Lp>
114 : _M_use_count(1), _M_weak_count(1) { }
117 ~_Sp_counted_base() // nothrow
120 // Called when _M_use_count drops to zero, to release the resources
123 _M_dispose() = 0; // nothrow
125 // Called when _M_weak_count drops to zero.
127 _M_destroy() // nothrow
131 _M_get_deleter(const std::type_info&) = 0;
135 { __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); }
141 _M_release() // nothrow
143 // Be race-detector-friendly. For more info see bits/c++config.
144 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count);
145 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
147 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count);
149 // There must be a memory barrier between dispose() and destroy()
150 // to ensure that the effects of dispose() are observed in the
151 // thread that runs destroy().
152 // See http://gcc.gnu.org/ml/libstdc++/2005-11/msg00136.html
153 if (_Mutex_base<_Lp>::_S_need_barriers)
155 _GLIBCXX_READ_MEM_BARRIER;
156 _GLIBCXX_WRITE_MEM_BARRIER;
159 // Be race-detector-friendly. For more info see bits/c++config.
160 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
161 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count,
164 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
171 _M_weak_add_ref() // nothrow
172 { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); }
175 _M_weak_release() // nothrow
177 // Be race-detector-friendly. For more info see bits/c++config.
178 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
179 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1)
181 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
182 if (_Mutex_base<_Lp>::_S_need_barriers)
185 // destroy() must observe results of dispose()
186 _GLIBCXX_READ_MEM_BARRIER;
187 _GLIBCXX_WRITE_MEM_BARRIER;
194 _M_get_use_count() const // nothrow
196 // No memory barrier is used here so there is no synchronization
197 // with other threads.
198 return const_cast<const volatile _Atomic_word&>(_M_use_count);
202 _Sp_counted_base(_Sp_counted_base const&);
203 _Sp_counted_base& operator=(_Sp_counted_base const&);
205 _Atomic_word _M_use_count; // #shared
206 _Atomic_word _M_weak_count; // #weak + (#shared != 0)
211 _Sp_counted_base<_S_single>::
214 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
217 __throw_bad_weak_ptr();
223 _Sp_counted_base<_S_mutex>::
226 __gnu_cxx::__scoped_lock sentry(*this);
227 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
230 __throw_bad_weak_ptr();
236 _Sp_counted_base<_S_atomic>::
239 // Perform lock-free add-if-not-zero operation.
240 _Atomic_word __count;
243 __count = _M_use_count;
245 __throw_bad_weak_ptr();
247 // Replace the current counter value with the old value + 1, as
248 // long as it's not changed meanwhile.
250 while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1,
251 true, __ATOMIC_ACQ_REL,
255 template<typename _Ptr, typename _Deleter, _Lock_policy _Lp>
256 class _Sp_counted_base_impl
257 : public _Sp_counted_base<_Lp>
260 // Precondition: __d(__p) must not throw.
261 _Sp_counted_base_impl(_Ptr __p, _Deleter __d)
262 : _M_ptr(__p), _M_del(__d) { }
265 _M_dispose() // nothrow
269 _M_get_deleter(const std::type_info& __ti)
272 return __ti == typeid(_Deleter) ? &_M_del : 0;
279 _Sp_counted_base_impl(const _Sp_counted_base_impl&);
280 _Sp_counted_base_impl& operator=(const _Sp_counted_base_impl&);
282 _Ptr _M_ptr; // copy constructor must not throw
283 _Deleter _M_del; // copy constructor must not throw
286 template<_Lock_policy _Lp = __default_lock_policy>
289 template<typename _Tp>
292 typedef void result_type;
293 typedef _Tp* argument_type;
294 void operator()(_Tp* __p) const { delete __p; }
297 template<_Lock_policy _Lp = __default_lock_policy>
302 : _M_pi(0) // nothrow
305 template<typename _Ptr>
306 __shared_count(_Ptr __p) : _M_pi(0)
310 typedef typename std::tr1::remove_pointer<_Ptr>::type _Tp;
311 _M_pi = new _Sp_counted_base_impl<_Ptr, _Sp_deleter<_Tp>, _Lp>(
312 __p, _Sp_deleter<_Tp>());
317 __throw_exception_again;
321 template<typename _Ptr, typename _Deleter>
322 __shared_count(_Ptr __p, _Deleter __d) : _M_pi(0)
326 _M_pi = new _Sp_counted_base_impl<_Ptr, _Deleter, _Lp>(__p, __d);
330 __d(__p); // Call _Deleter on __p.
331 __throw_exception_again;
335 // Special case for auto_ptr<_Tp> to provide the strong guarantee.
336 template<typename _Tp>
338 __shared_count(std::auto_ptr<_Tp>& __r)
339 : _M_pi(new _Sp_counted_base_impl<_Tp*,
340 _Sp_deleter<_Tp>, _Lp >(__r.get(), _Sp_deleter<_Tp>()))
343 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
345 __shared_count(const __weak_count<_Lp>& __r);
347 ~__shared_count() // nothrow
353 __shared_count(const __shared_count& __r)
354 : _M_pi(__r._M_pi) // nothrow
357 _M_pi->_M_add_ref_copy();
361 operator=(const __shared_count& __r) // nothrow
363 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
367 __tmp->_M_add_ref_copy();
376 _M_swap(__shared_count& __r) // nothrow
378 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
384 _M_get_use_count() const // nothrow
385 { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
388 _M_unique() const // nothrow
389 { return this->_M_get_use_count() == 1; }
392 operator==(const __shared_count& __a, const __shared_count& __b)
393 { return __a._M_pi == __b._M_pi; }
396 operator<(const __shared_count& __a, const __shared_count& __b)
397 { return std::less<_Sp_counted_base<_Lp>*>()(__a._M_pi, __b._M_pi); }
400 _M_get_deleter(const std::type_info& __ti) const
401 { return _M_pi ? _M_pi->_M_get_deleter(__ti) : 0; }
404 friend class __weak_count<_Lp>;
406 _Sp_counted_base<_Lp>* _M_pi;
410 template<_Lock_policy _Lp>
415 : _M_pi(0) // nothrow
418 __weak_count(const __shared_count<_Lp>& __r)
419 : _M_pi(__r._M_pi) // nothrow
422 _M_pi->_M_weak_add_ref();
425 __weak_count(const __weak_count<_Lp>& __r)
426 : _M_pi(__r._M_pi) // nothrow
429 _M_pi->_M_weak_add_ref();
432 ~__weak_count() // nothrow
435 _M_pi->_M_weak_release();
439 operator=(const __shared_count<_Lp>& __r) // nothrow
441 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
443 __tmp->_M_weak_add_ref();
445 _M_pi->_M_weak_release();
451 operator=(const __weak_count<_Lp>& __r) // nothrow
453 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
455 __tmp->_M_weak_add_ref();
457 _M_pi->_M_weak_release();
463 _M_swap(__weak_count<_Lp>& __r) // nothrow
465 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
471 _M_get_use_count() const // nothrow
472 { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
475 operator==(const __weak_count<_Lp>& __a, const __weak_count<_Lp>& __b)
476 { return __a._M_pi == __b._M_pi; }
479 operator<(const __weak_count<_Lp>& __a, const __weak_count<_Lp>& __b)
480 { return std::less<_Sp_counted_base<_Lp>*>()(__a._M_pi, __b._M_pi); }
483 friend class __shared_count<_Lp>;
485 _Sp_counted_base<_Lp>* _M_pi;
488 // now that __weak_count is defined we can define this constructor:
489 template<_Lock_policy _Lp>
491 __shared_count<_Lp>::
492 __shared_count(const __weak_count<_Lp>& __r)
496 _M_pi->_M_add_ref_lock();
498 __throw_bad_weak_ptr();
501 // Forward declarations.
502 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
505 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
508 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
509 class __enable_shared_from_this;
511 template<typename _Tp>
514 template<typename _Tp>
517 template<typename _Tp>
518 class enable_shared_from_this;
520 // Support for enable_shared_from_this.
522 // Friend of __enable_shared_from_this.
523 template<_Lock_policy _Lp, typename _Tp1, typename _Tp2>
525 __enable_shared_from_this_helper(const __shared_count<_Lp>&,
526 const __enable_shared_from_this<_Tp1,
529 // Friend of enable_shared_from_this.
530 template<typename _Tp1, typename _Tp2>
532 __enable_shared_from_this_helper(const __shared_count<>&,
533 const enable_shared_from_this<_Tp1>*,
536 template<_Lock_policy _Lp>
538 __enable_shared_from_this_helper(const __shared_count<_Lp>&, ...)
542 struct __static_cast_tag { };
543 struct __const_cast_tag { };
544 struct __dynamic_cast_tag { };
546 // A smart pointer with reference-counted copy semantics. The
547 // object pointed to is deleted when the last shared_ptr pointing to
548 // it is destroyed or reset.
549 template<typename _Tp, _Lock_policy _Lp>
553 typedef _Tp element_type;
556 : _M_ptr(0), _M_refcount() // never throws
559 template<typename _Tp1>
561 __shared_ptr(_Tp1* __p)
562 : _M_ptr(__p), _M_refcount(__p)
564 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
565 typedef int _IsComplete[sizeof(_Tp1)];
566 __enable_shared_from_this_helper(_M_refcount, __p, __p);
569 template<typename _Tp1, typename _Deleter>
570 __shared_ptr(_Tp1* __p, _Deleter __d)
571 : _M_ptr(__p), _M_refcount(__p, __d)
573 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
574 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
575 __enable_shared_from_this_helper(_M_refcount, __p, __p);
578 // generated copy constructor, assignment, destructor are fine.
580 template<typename _Tp1>
581 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r)
582 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
583 { __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) }
585 template<typename _Tp1>
587 __shared_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
588 : _M_refcount(__r._M_refcount) // may throw
590 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
591 // It is now safe to copy __r._M_ptr, as _M_refcount(__r._M_refcount)
596 #if !defined(__GXX_EXPERIMENTAL_CXX0X__) || _GLIBCXX_USE_DEPRECATED
597 // Postcondition: use_count() == 1 and __r.get() == 0
598 template<typename _Tp1>
600 __shared_ptr(std::auto_ptr<_Tp1>& __r)
601 : _M_ptr(__r.get()), _M_refcount()
602 { // TODO requries delete __r.release() well-formed
603 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
604 typedef int _IsComplete[sizeof(_Tp1)];
605 _Tp1* __tmp = __r.get();
606 _M_refcount = __shared_count<_Lp>(__r);
607 __enable_shared_from_this_helper(_M_refcount, __tmp, __tmp);
612 template<typename _Tp1>
613 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r, __static_cast_tag)
614 : _M_ptr(static_cast<element_type*>(__r._M_ptr)),
615 _M_refcount(__r._M_refcount)
618 template<typename _Tp1>
619 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r, __const_cast_tag)
620 : _M_ptr(const_cast<element_type*>(__r._M_ptr)),
621 _M_refcount(__r._M_refcount)
624 template<typename _Tp1>
625 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r, __dynamic_cast_tag)
626 : _M_ptr(dynamic_cast<element_type*>(__r._M_ptr)),
627 _M_refcount(__r._M_refcount)
629 if (_M_ptr == 0) // need to allocate new counter -- the cast failed
630 _M_refcount = __shared_count<_Lp>();
633 template<typename _Tp1>
635 operator=(const __shared_ptr<_Tp1, _Lp>& __r) // never throws
638 _M_refcount = __r._M_refcount; // __shared_count::op= doesn't throw
642 #if !defined(__GXX_EXPERIMENTAL_CXX0X__) || _GLIBCXX_USE_DEPRECATED
643 template<typename _Tp1>
645 operator=(std::auto_ptr<_Tp1>& __r)
647 __shared_ptr(__r).swap(*this);
653 reset() // never throws
654 { __shared_ptr().swap(*this); }
656 template<typename _Tp1>
658 reset(_Tp1* __p) // _Tp1 must be complete.
660 // Catch self-reset errors.
661 _GLIBCXX_DEBUG_ASSERT(__p == 0 || __p != _M_ptr);
662 __shared_ptr(__p).swap(*this);
665 template<typename _Tp1, typename _Deleter>
667 reset(_Tp1* __p, _Deleter __d)
668 { __shared_ptr(__p, __d).swap(*this); }
670 // Allow class instantiation when _Tp is [cv-qual] void.
671 typename std::tr1::add_reference<_Tp>::type
672 operator*() const // never throws
674 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
679 operator->() const // never throws
681 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
686 get() const // never throws
689 // Implicit conversion to "bool"
691 typedef _Tp* __shared_ptr::*__unspecified_bool_type;
694 operator __unspecified_bool_type() const // never throws
695 { return _M_ptr == 0 ? 0 : &__shared_ptr::_M_ptr; }
698 unique() const // never throws
699 { return _M_refcount._M_unique(); }
702 use_count() const // never throws
703 { return _M_refcount._M_get_use_count(); }
706 swap(__shared_ptr<_Tp, _Lp>& __other) // never throws
708 std::swap(_M_ptr, __other._M_ptr);
709 _M_refcount._M_swap(__other._M_refcount);
714 _M_get_deleter(const std::type_info& __ti) const
715 { return _M_refcount._M_get_deleter(__ti); }
717 template<typename _Tp1, _Lock_policy _Lp1>
719 _M_less(const __shared_ptr<_Tp1, _Lp1>& __rhs) const
720 { return _M_refcount < __rhs._M_refcount; }
722 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
723 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
725 template<typename _Del, typename _Tp1, _Lock_policy _Lp1>
726 friend _Del* get_deleter(const __shared_ptr<_Tp1, _Lp1>&);
728 // Friends injected into enclosing namespace and found by ADL:
729 template<typename _Tp1>
731 operator==(const __shared_ptr& __a, const __shared_ptr<_Tp1, _Lp>& __b)
732 { return __a.get() == __b.get(); }
734 template<typename _Tp1>
736 operator!=(const __shared_ptr& __a, const __shared_ptr<_Tp1, _Lp>& __b)
737 { return __a.get() != __b.get(); }
739 template<typename _Tp1>
741 operator<(const __shared_ptr& __a, const __shared_ptr<_Tp1, _Lp>& __b)
742 { return __a._M_less(__b); }
744 _Tp* _M_ptr; // Contained pointer.
745 __shared_count<_Lp> _M_refcount; // Reference counter.
748 // 2.2.3.8 shared_ptr specialized algorithms.
749 template<typename _Tp, _Lock_policy _Lp>
751 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b)
754 // 2.2.3.9 shared_ptr casts
755 /* The seemingly equivalent
756 * shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))
757 * will eventually result in undefined behaviour,
758 * attempting to delete the same object twice.
760 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
761 inline __shared_ptr<_Tp, _Lp>
762 static_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r)
763 { return __shared_ptr<_Tp, _Lp>(__r, __static_cast_tag()); }
765 /* The seemingly equivalent
766 * shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))
767 * will eventually result in undefined behaviour,
768 * attempting to delete the same object twice.
770 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
771 inline __shared_ptr<_Tp, _Lp>
772 const_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r)
773 { return __shared_ptr<_Tp, _Lp>(__r, __const_cast_tag()); }
775 /* The seemingly equivalent
776 * shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))
777 * will eventually result in undefined behaviour,
778 * attempting to delete the same object twice.
780 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
781 inline __shared_ptr<_Tp, _Lp>
782 dynamic_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r)
783 { return __shared_ptr<_Tp, _Lp>(__r, __dynamic_cast_tag()); }
785 // 2.2.3.7 shared_ptr I/O
786 template<typename _Ch, typename _Tr, typename _Tp, _Lock_policy _Lp>
787 std::basic_ostream<_Ch, _Tr>&
788 operator<<(std::basic_ostream<_Ch, _Tr>& __os,
789 const __shared_ptr<_Tp, _Lp>& __p)
795 // 2.2.3.10 shared_ptr get_deleter (experimental)
796 template<typename _Del, typename _Tp, _Lock_policy _Lp>
798 get_deleter(const __shared_ptr<_Tp, _Lp>& __p)
801 return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del)));
808 template<typename _Tp, _Lock_policy _Lp>
812 typedef _Tp element_type;
815 : _M_ptr(0), _M_refcount() // never throws
818 // Generated copy constructor, assignment, destructor are fine.
820 // The "obvious" converting constructor implementation:
822 // template<typename _Tp1>
823 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
824 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
827 // has a serious problem.
829 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
830 // conversion may require access to *__r._M_ptr (virtual inheritance).
832 // It is not possible to avoid spurious access violations since
833 // in multithreaded programs __r._M_ptr may be invalidated at any point.
834 template<typename _Tp1>
835 __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
836 : _M_refcount(__r._M_refcount) // never throws
838 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
839 _M_ptr = __r.lock().get();
842 template<typename _Tp1>
843 __weak_ptr(const __shared_ptr<_Tp1, _Lp>& __r)
844 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
845 { __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) }
847 template<typename _Tp1>
849 operator=(const __weak_ptr<_Tp1, _Lp>& __r) // never throws
851 _M_ptr = __r.lock().get();
852 _M_refcount = __r._M_refcount;
856 template<typename _Tp1>
858 operator=(const __shared_ptr<_Tp1, _Lp>& __r) // never throws
861 _M_refcount = __r._M_refcount;
865 __shared_ptr<_Tp, _Lp>
866 lock() const // never throws
869 // Optimization: avoid throw overhead.
871 return __shared_ptr<element_type, _Lp>();
875 return __shared_ptr<element_type, _Lp>(*this);
877 __catch(const bad_weak_ptr&)
879 // Q: How can we get here?
880 // A: Another thread may have invalidated r after the
881 // use_count test above.
882 return __shared_ptr<element_type, _Lp>();
886 // Optimization: avoid try/catch overhead when single threaded.
887 return expired() ? __shared_ptr<element_type, _Lp>()
888 : __shared_ptr<element_type, _Lp>(*this);
894 use_count() const // never throws
895 { return _M_refcount._M_get_use_count(); }
898 expired() const // never throws
899 { return _M_refcount._M_get_use_count() == 0; }
902 reset() // never throws
903 { __weak_ptr().swap(*this); }
906 swap(__weak_ptr& __s) // never throws
908 std::swap(_M_ptr, __s._M_ptr);
909 _M_refcount._M_swap(__s._M_refcount);
913 // Used by __enable_shared_from_this.
915 _M_assign(_Tp* __ptr, const __shared_count<_Lp>& __refcount)
918 _M_refcount = __refcount;
921 template<typename _Tp1>
923 _M_less(const __weak_ptr<_Tp1, _Lp>& __rhs) const
924 { return _M_refcount < __rhs._M_refcount; }
926 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
927 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
928 friend class __enable_shared_from_this<_Tp, _Lp>;
929 friend class enable_shared_from_this<_Tp>;
931 // Friend injected into namespace and found by ADL.
932 template<typename _Tp1>
934 operator<(const __weak_ptr& __lhs, const __weak_ptr<_Tp1, _Lp>& __rhs)
935 { return __lhs._M_less(__rhs); }
937 _Tp* _M_ptr; // Contained pointer.
938 __weak_count<_Lp> _M_refcount; // Reference counter.
941 // 2.2.4.7 weak_ptr specialized algorithms.
942 template<typename _Tp, _Lock_policy _Lp>
944 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b)
948 template<typename _Tp, _Lock_policy _Lp>
949 class __enable_shared_from_this
952 __enable_shared_from_this() { }
954 __enable_shared_from_this(const __enable_shared_from_this&) { }
956 __enable_shared_from_this&
957 operator=(const __enable_shared_from_this&)
960 ~__enable_shared_from_this() { }
963 __shared_ptr<_Tp, _Lp>
965 { return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
967 __shared_ptr<const _Tp, _Lp>
968 shared_from_this() const
969 { return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
972 template<typename _Tp1>
974 _M_weak_assign(_Tp1* __p, const __shared_count<_Lp>& __n) const
975 { _M_weak_this._M_assign(__p, __n); }
977 template<typename _Tp1>
979 __enable_shared_from_this_helper(const __shared_count<_Lp>& __pn,
980 const __enable_shared_from_this* __pe,
984 __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
987 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
991 // The actual shared_ptr, with forwarding constructors and
992 // assignment operators.
993 template<typename _Tp>
995 : public __shared_ptr<_Tp>
999 : __shared_ptr<_Tp>() { }
1001 template<typename _Tp1>
1003 shared_ptr(_Tp1* __p)
1004 : __shared_ptr<_Tp>(__p) { }
1006 template<typename _Tp1, typename _Deleter>
1007 shared_ptr(_Tp1* __p, _Deleter __d)
1008 : __shared_ptr<_Tp>(__p, __d) { }
1010 template<typename _Tp1>
1011 shared_ptr(const shared_ptr<_Tp1>& __r)
1012 : __shared_ptr<_Tp>(__r) { }
1014 template<typename _Tp1>
1016 shared_ptr(const weak_ptr<_Tp1>& __r)
1017 : __shared_ptr<_Tp>(__r) { }
1019 #if !defined(__GXX_EXPERIMENTAL_CXX0X__) || _GLIBCXX_USE_DEPRECATED
1020 template<typename _Tp1>
1022 shared_ptr(std::auto_ptr<_Tp1>& __r)
1023 : __shared_ptr<_Tp>(__r) { }
1026 template<typename _Tp1>
1027 shared_ptr(const shared_ptr<_Tp1>& __r, __static_cast_tag)
1028 : __shared_ptr<_Tp>(__r, __static_cast_tag()) { }
1030 template<typename _Tp1>
1031 shared_ptr(const shared_ptr<_Tp1>& __r, __const_cast_tag)
1032 : __shared_ptr<_Tp>(__r, __const_cast_tag()) { }
1034 template<typename _Tp1>
1035 shared_ptr(const shared_ptr<_Tp1>& __r, __dynamic_cast_tag)
1036 : __shared_ptr<_Tp>(__r, __dynamic_cast_tag()) { }
1038 template<typename _Tp1>
1040 operator=(const shared_ptr<_Tp1>& __r) // never throws
1042 this->__shared_ptr<_Tp>::operator=(__r);
1046 #if !defined(__GXX_EXPERIMENTAL_CXX0X__) || _GLIBCXX_USE_DEPRECATED
1047 template<typename _Tp1>
1049 operator=(std::auto_ptr<_Tp1>& __r)
1051 this->__shared_ptr<_Tp>::operator=(__r);
1057 // 2.2.3.8 shared_ptr specialized algorithms.
1058 template<typename _Tp>
1060 swap(__shared_ptr<_Tp>& __a, __shared_ptr<_Tp>& __b)
1063 template<typename _Tp, typename _Tp1>
1064 inline shared_ptr<_Tp>
1065 static_pointer_cast(const shared_ptr<_Tp1>& __r)
1066 { return shared_ptr<_Tp>(__r, __static_cast_tag()); }
1068 template<typename _Tp, typename _Tp1>
1069 inline shared_ptr<_Tp>
1070 const_pointer_cast(const shared_ptr<_Tp1>& __r)
1071 { return shared_ptr<_Tp>(__r, __const_cast_tag()); }
1073 template<typename _Tp, typename _Tp1>
1074 inline shared_ptr<_Tp>
1075 dynamic_pointer_cast(const shared_ptr<_Tp1>& __r)
1076 { return shared_ptr<_Tp>(__r, __dynamic_cast_tag()); }
1079 // The actual weak_ptr, with forwarding constructors and
1080 // assignment operators.
1081 template<typename _Tp>
1083 : public __weak_ptr<_Tp>
1087 : __weak_ptr<_Tp>() { }
1089 template<typename _Tp1>
1090 weak_ptr(const weak_ptr<_Tp1>& __r)
1091 : __weak_ptr<_Tp>(__r) { }
1093 template<typename _Tp1>
1094 weak_ptr(const shared_ptr<_Tp1>& __r)
1095 : __weak_ptr<_Tp>(__r) { }
1097 template<typename _Tp1>
1099 operator=(const weak_ptr<_Tp1>& __r) // never throws
1101 this->__weak_ptr<_Tp>::operator=(__r);
1105 template<typename _Tp1>
1107 operator=(const shared_ptr<_Tp1>& __r) // never throws
1109 this->__weak_ptr<_Tp>::operator=(__r);
1114 lock() const // never throws
1117 if (this->expired())
1118 return shared_ptr<_Tp>();
1122 return shared_ptr<_Tp>(*this);
1124 __catch(const bad_weak_ptr&)
1126 return shared_ptr<_Tp>();
1129 return this->expired() ? shared_ptr<_Tp>()
1130 : shared_ptr<_Tp>(*this);
1135 template<typename _Tp>
1136 class enable_shared_from_this
1139 enable_shared_from_this() { }
1141 enable_shared_from_this(const enable_shared_from_this&) { }
1143 enable_shared_from_this&
1144 operator=(const enable_shared_from_this&)
1147 ~enable_shared_from_this() { }
1152 { return shared_ptr<_Tp>(this->_M_weak_this); }
1154 shared_ptr<const _Tp>
1155 shared_from_this() const
1156 { return shared_ptr<const _Tp>(this->_M_weak_this); }
1159 template<typename _Tp1>
1161 _M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const
1162 { _M_weak_this._M_assign(__p, __n); }
1164 template<typename _Tp1>
1166 __enable_shared_from_this_helper(const __shared_count<>& __pn,
1167 const enable_shared_from_this* __pe,
1171 __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
1174 mutable weak_ptr<_Tp> _M_weak_this;
1177 _GLIBCXX_END_NAMESPACE_VERSION
1181 #endif // _TR1_SHARED_PTR_H