1 // <tr1_impl/boost_shared_ptr.h> -*- C++ -*-
3 // Copyright (C) 2007 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 2, 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 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
31 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
34 // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
35 // Copyright (C) 2001, 2002, 2003 Peter Dimov
38 // Copyright (C) 2001, 2002, 2003 Peter Dimov
40 // enable_shared_from_this.hpp
41 // Copyright (C) 2002 Peter Dimov
43 // Distributed under the Boost Software License, Version 1.0. (See
44 // accompanying file LICENSE_1_0.txt or copy at
45 // http://www.boost.org/LICENSE_1_0.txt)
47 // GCC Note: based on version 1.32.0 of the Boost library.
49 /** @file tr1_impl/boost_shared_ptr.h
50 * This is an internal header file, included by other library headers.
51 * You should not attempt to use it directly.
56 _GLIBCXX_BEGIN_NAMESPACE_TR1
58 class bad_weak_ptr : public std::exception
63 { return "tr1::bad_weak_ptr"; }
66 // Substitute for bad_weak_ptr object in the case of -fno-exceptions.
68 __throw_bad_weak_ptr()
77 using __gnu_cxx::_Lock_policy;
78 using __gnu_cxx::__default_lock_policy;
79 using __gnu_cxx::_S_single;
80 using __gnu_cxx::_S_mutex;
81 using __gnu_cxx::_S_atomic;
83 template<typename _Tp>
86 typedef void result_type;
87 typedef _Tp* argument_type;
90 operator()(_Tp* __p) const
94 // Empty helper class except when the template argument is _S_mutex.
95 template<_Lock_policy _Lp>
100 class _Mutex_base<_S_mutex>
101 : public __gnu_cxx::__mutex
104 template<_Lock_policy _Lp = __default_lock_policy>
105 class _Sp_counted_base
106 : public _Mutex_base<_Lp>
110 : _M_use_count(1), _M_weak_count(1) { }
113 ~_Sp_counted_base() // nothrow
116 // Called when _M_use_count drops to zero, to release the resources
119 _M_dispose() = 0; // nothrow
121 // Called when _M_weak_count drops to zero.
123 _M_destroy() // nothrow
127 _M_get_deleter(const std::type_info&) = 0;
131 { __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); }
137 _M_release() // nothrow
139 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count,
144 _GLIBCXX_READ_MEM_BARRIER;
145 _GLIBCXX_WRITE_MEM_BARRIER;
147 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count,
154 _M_weak_add_ref() // nothrow
155 { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); }
158 _M_weak_release() // nothrow
160 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1)
163 _GLIBCXX_READ_MEM_BARRIER;
164 _GLIBCXX_WRITE_MEM_BARRIER;
171 _M_get_use_count() const // nothrow
172 { return _M_use_count; } // XXX is this MT safe?
175 _Sp_counted_base(_Sp_counted_base const&);
176 _Sp_counted_base& operator=(_Sp_counted_base const&);
178 _Atomic_word _M_use_count; // #shared
179 _Atomic_word _M_weak_count; // #weak + (#shared != 0)
184 _Sp_counted_base<_S_single>::
187 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
190 __throw_bad_weak_ptr();
196 _Sp_counted_base<_S_mutex>::
199 __gnu_cxx::__scoped_lock sentry(*this);
200 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
203 __throw_bad_weak_ptr();
209 _Sp_counted_base<_S_atomic>::
212 // Perform lock-free add-if-not-zero operation.
213 _Atomic_word __count;
216 __count = _M_use_count;
218 __throw_bad_weak_ptr();
220 // Replace the current counter value with the old value + 1, as
221 // long as it's not changed meanwhile.
223 while (!__sync_bool_compare_and_swap(&_M_use_count, __count,
227 template<typename _Ptr, typename _Deleter, _Lock_policy _Lp>
228 class _Sp_counted_base_impl
229 : public _Sp_counted_base<_Lp>
234 * @pre __d(__p) must not throw.
236 _Sp_counted_base_impl(_Ptr __p, _Deleter __d)
237 : _M_ptr(__p), _M_del(__d) { }
240 _M_dispose() // nothrow
244 _M_get_deleter(const std::type_info& __ti)
245 { return __ti == typeid(_Deleter) ? &_M_del : 0; }
248 _Sp_counted_base_impl(const _Sp_counted_base_impl&);
249 _Sp_counted_base_impl& operator=(const _Sp_counted_base_impl&);
251 _Ptr _M_ptr; // copy constructor must not throw
252 _Deleter _M_del; // copy constructor must not throw
255 template<_Lock_policy _Lp = __default_lock_policy>
258 template<_Lock_policy _Lp = __default_lock_policy>
263 : _M_pi(0) // nothrow
266 template<typename _Ptr, typename _Deleter>
267 __shared_count(_Ptr __p, _Deleter __d) : _M_pi(0)
271 _M_pi = new _Sp_counted_base_impl<_Ptr, _Deleter, _Lp>(__p, __d);
275 __d(__p); // Call _Deleter on __p.
276 __throw_exception_again;
280 // Special case for auto_ptr<_Tp> to provide the strong guarantee.
281 template<typename _Tp>
283 __shared_count(std::auto_ptr<_Tp>& __r)
284 : _M_pi(new _Sp_counted_base_impl<_Tp*,
285 _Sp_deleter<_Tp>, _Lp >(__r.get(), _Sp_deleter<_Tp>()))
288 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
290 __shared_count(const __weak_count<_Lp>& __r);
292 ~__shared_count() // nothrow
298 __shared_count(const __shared_count& __r)
299 : _M_pi(__r._M_pi) // nothrow
302 _M_pi->_M_add_ref_copy();
306 operator=(const __shared_count& __r) // nothrow
308 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
312 __tmp->_M_add_ref_copy();
321 _M_swap(__shared_count& __r) // nothrow
323 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
329 _M_get_use_count() const // nothrow
330 { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
333 _M_unique() const // nothrow
334 { return this->_M_get_use_count() == 1; }
337 operator==(const __shared_count& __a, const __shared_count& __b)
338 { return __a._M_pi == __b._M_pi; }
341 operator<(const __shared_count& __a, const __shared_count& __b)
342 { return std::less<_Sp_counted_base<_Lp>*>()(__a._M_pi, __b._M_pi); }
345 _M_get_deleter(const std::type_info& __ti) const
346 { return _M_pi ? _M_pi->_M_get_deleter(__ti) : 0; }
349 friend class __weak_count<_Lp>;
351 _Sp_counted_base<_Lp>* _M_pi;
354 template<_Lock_policy _Lp>
359 : _M_pi(0) // nothrow
362 __weak_count(const __shared_count<_Lp>& __r)
363 : _M_pi(__r._M_pi) // nothrow
366 _M_pi->_M_weak_add_ref();
369 __weak_count(const __weak_count<_Lp>& __r)
370 : _M_pi(__r._M_pi) // nothrow
373 _M_pi->_M_weak_add_ref();
376 ~__weak_count() // nothrow
379 _M_pi->_M_weak_release();
383 operator=(const __shared_count<_Lp>& __r) // nothrow
385 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
387 __tmp->_M_weak_add_ref();
389 _M_pi->_M_weak_release();
395 operator=(const __weak_count<_Lp>& __r) // nothrow
397 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
399 __tmp->_M_weak_add_ref();
401 _M_pi->_M_weak_release();
407 _M_swap(__weak_count<_Lp>& __r) // nothrow
409 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
415 _M_get_use_count() const // nothrow
416 { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
419 operator==(const __weak_count<_Lp>& __a, const __weak_count<_Lp>& __b)
420 { return __a._M_pi == __b._M_pi; }
423 operator<(const __weak_count<_Lp>& __a, const __weak_count<_Lp>& __b)
424 { return std::less<_Sp_counted_base<_Lp>*>()(__a._M_pi, __b._M_pi); }
427 friend class __shared_count<_Lp>;
429 _Sp_counted_base<_Lp>* _M_pi;
432 template<_Lock_policy _Lp>
434 __shared_count<_Lp>::
435 __shared_count(const __weak_count<_Lp>& __r)
439 _M_pi->_M_add_ref_lock();
441 __throw_bad_weak_ptr();
445 // Forward declarations.
446 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
449 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
452 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
453 class __enable_shared_from_this;
455 template<typename _Tp>
458 template<typename _Tp>
461 template<typename _Tp>
462 class enable_shared_from_this;
464 // Support for enable_shared_from_this.
466 // Friend of __enable_shared_from_this.
467 template<_Lock_policy _Lp, typename _Tp1, typename _Tp2>
469 __enable_shared_from_this_helper(const __shared_count<_Lp>&,
470 const __enable_shared_from_this<_Tp1,
473 // Friend of enable_shared_from_this.
474 template<typename _Tp1, typename _Tp2>
476 __enable_shared_from_this_helper(const __shared_count<>&,
477 const enable_shared_from_this<_Tp1>*,
480 template<_Lock_policy _Lp>
482 __enable_shared_from_this_helper(const __shared_count<_Lp>&, ...)
486 struct __static_cast_tag { };
487 struct __const_cast_tag { };
488 struct __dynamic_cast_tag { };
491 * @class shared_ptr <tr1/memory>
493 * A smart pointer with reference-counted copy semantics.
494 * The object pointed to is deleted when the last shared_ptr pointing to
495 * it is destroyed or reset.
497 template<typename _Tp, _Lock_policy _Lp>
501 typedef _Tp element_type;
503 /** @brief Construct an empty %__shared_ptr.
504 * @post use_count()==0 && get()==0
507 : _M_ptr(0), _M_refcount() // never throws
510 /** @brief Construct a %__shared_ptr that owns the pointer @a __p.
511 * @param __p A pointer that is convertible to element_type*.
512 * @post use_count() == 1 && get() == __p
513 * @throw std::bad_alloc, in which case @c delete @a __p is called.
515 template<typename _Tp1>
517 __shared_ptr(_Tp1* __p)
518 : _M_ptr(__p), _M_refcount(__p, _Sp_deleter<_Tp1>())
520 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
521 // __glibcxx_function_requires(_CompleteConcept<_Tp1*>)
522 __enable_shared_from_this_helper(_M_refcount, __p, __p);
526 // Requirements: _Deleter' copy constructor and destructor must not throw
528 // __shared_ptr will release __p by calling __d(__p)
530 /** @brief Construct a %__shared_ptr that owns the pointer @a __p
531 * and the deleter @a __d.
532 * @param __p A pointer.
533 * @param __d A deleter.
534 * @post use_count() == 1 && get() == __p
535 * @throw std::bad_alloc, in which case @a __d(__p) is called.
537 template<typename _Tp1, typename _Deleter>
538 __shared_ptr(_Tp1* __p, _Deleter __d)
539 : _M_ptr(__p), _M_refcount(__p, __d)
541 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
542 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
543 __enable_shared_from_this_helper(_M_refcount, __p, __p);
546 // generated copy constructor, assignment, destructor are fine.
548 /** @brief If @a __r is empty, constructs an empty %__shared_ptr;
549 * otherwise construct a %__shared_ptr that shares ownership
551 * @param __r A %__shared_ptr.
552 * @post get() == __r.get() && use_count() == __r.use_count()
553 * @throw std::bad_alloc, in which case
555 template<typename _Tp1>
556 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r)
557 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
558 { __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) }
560 /** @brief Constructs a %__shared_ptr that shares ownership with @a __r
561 * and stores a copy of the pointer stored in @a __r.
562 * @param __r A weak_ptr.
563 * @post use_count() == __r.use_count()
564 * @throw bad_weak_ptr when __r.expired(),
565 * in which case the constructor has no effect.
567 template<typename _Tp1>
569 __shared_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
570 : _M_refcount(__r._M_refcount) // may throw
572 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
573 // It is now safe to copy __r._M_ptr, as _M_refcount(__r._M_refcount)
579 * @post use_count() == 1 and __r.get() == 0
581 template<typename _Tp1>
583 __shared_ptr(std::auto_ptr<_Tp1>& __r)
584 : _M_ptr(__r.get()), _M_refcount()
586 // TODO requires __r.release() convertible to _Tp*, _Tp1 is complete,
587 // delete __r.release() well-formed
588 _Tp1* __tmp = __r.get();
589 _M_refcount = __shared_count<_Lp>(__r);
590 __enable_shared_from_this_helper(_M_refcount, __tmp, __tmp);
593 template<typename _Tp1>
594 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r, __static_cast_tag)
595 : _M_ptr(static_cast<element_type*>(__r._M_ptr)),
596 _M_refcount(__r._M_refcount)
599 template<typename _Tp1>
600 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r, __const_cast_tag)
601 : _M_ptr(const_cast<element_type*>(__r._M_ptr)),
602 _M_refcount(__r._M_refcount)
605 template<typename _Tp1>
606 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r, __dynamic_cast_tag)
607 : _M_ptr(dynamic_cast<element_type*>(__r._M_ptr)),
608 _M_refcount(__r._M_refcount)
610 if (_M_ptr == 0) // need to allocate new counter -- the cast failed
611 _M_refcount = __shared_count<_Lp>();
614 template<typename _Tp1>
616 operator=(const __shared_ptr<_Tp1, _Lp>& __r) // never throws
619 _M_refcount = __r._M_refcount; // __shared_count::op= doesn't throw
623 template<typename _Tp1>
625 operator=(std::auto_ptr<_Tp1>& __r)
627 __shared_ptr(__r).swap(*this);
632 reset() // never throws
633 { __shared_ptr().swap(*this); }
635 template<typename _Tp1>
637 reset(_Tp1* __p) // _Tp1 must be complete.
639 // Catch self-reset errors.
640 _GLIBCXX_DEBUG_ASSERT(__p == 0 || __p != _M_ptr);
641 __shared_ptr(__p).swap(*this);
644 template<typename _Tp1, typename _Deleter>
646 reset(_Tp1* __p, _Deleter __d)
647 { __shared_ptr(__p, __d).swap(*this); }
649 // Allow class instantiation when _Tp is [cv-qual] void.
650 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
651 typename std::add_lvalue_reference<_Tp>::type
653 typename std::tr1::add_reference<_Tp>::type
655 operator*() const // never throws
657 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
662 operator->() const // never throws
664 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
669 get() const // never throws
672 // Implicit conversion to "bool"
674 typedef _Tp* __shared_ptr::*__unspecified_bool_type;
677 operator __unspecified_bool_type() const // never throws
678 { return _M_ptr == 0 ? 0 : &__shared_ptr::_M_ptr; }
681 unique() const // never throws
682 { return _M_refcount._M_unique(); }
685 use_count() const // never throws
686 { return _M_refcount._M_get_use_count(); }
689 swap(__shared_ptr<_Tp, _Lp>& __other) // never throws
691 std::swap(_M_ptr, __other._M_ptr);
692 _M_refcount._M_swap(__other._M_refcount);
697 _M_get_deleter(const std::type_info& __ti) const
698 { return _M_refcount._M_get_deleter(__ti); }
700 template<typename _Tp1, _Lock_policy _Lp1>
702 _M_less(const __shared_ptr<_Tp1, _Lp1>& __rhs) const
703 { return _M_refcount < __rhs._M_refcount; }
705 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
706 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
708 template<typename _Del, typename _Tp1, _Lock_policy _Lp1>
709 friend _Del* get_deleter(const __shared_ptr<_Tp1, _Lp1>&);
711 // Friends injected into enclosing namespace and found by ADL:
712 template<typename _Tp1>
714 operator==(const __shared_ptr& __a, const __shared_ptr<_Tp1, _Lp>& __b)
715 { return __a.get() == __b.get(); }
717 template<typename _Tp1>
719 operator!=(const __shared_ptr& __a, const __shared_ptr<_Tp1, _Lp>& __b)
720 { return __a.get() != __b.get(); }
722 template<typename _Tp1>
724 operator<(const __shared_ptr& __a, const __shared_ptr<_Tp1, _Lp>& __b)
725 { return __a._M_less(__b); }
727 _Tp* _M_ptr; // Contained pointer.
728 __shared_count<_Lp> _M_refcount; // Reference counter.
731 // 2.2.3.8 shared_ptr specialized algorithms.
732 template<typename _Tp, _Lock_policy _Lp>
734 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b)
737 // 2.2.3.9 shared_ptr casts
738 /** @warning The seemingly equivalent
739 * <code>shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))</code>
740 * will eventually result in undefined behaviour,
741 * attempting to delete the same object twice.
743 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
744 __shared_ptr<_Tp, _Lp>
745 static_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r)
746 { return __shared_ptr<_Tp, _Lp>(__r, __static_cast_tag()); }
748 /** @warning The seemingly equivalent
749 * <code>shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))</code>
750 * will eventually result in undefined behaviour,
751 * attempting to delete the same object twice.
753 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
754 __shared_ptr<_Tp, _Lp>
755 const_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r)
756 { return __shared_ptr<_Tp, _Lp>(__r, __const_cast_tag()); }
758 /** @warning The seemingly equivalent
759 * <code>shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))</code>
760 * will eventually result in undefined behaviour,
761 * attempting to delete the same object twice.
763 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
764 __shared_ptr<_Tp, _Lp>
765 dynamic_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r)
766 { return __shared_ptr<_Tp, _Lp>(__r, __dynamic_cast_tag()); }
768 // 2.2.3.7 shared_ptr I/O
769 template<typename _Ch, typename _Tr, typename _Tp, _Lock_policy _Lp>
770 std::basic_ostream<_Ch, _Tr>&
771 operator<<(std::basic_ostream<_Ch, _Tr>& __os,
772 const __shared_ptr<_Tp, _Lp>& __p)
778 // 2.2.3.10 shared_ptr get_deleter (experimental)
779 template<typename _Del, typename _Tp, _Lock_policy _Lp>
781 get_deleter(const __shared_ptr<_Tp, _Lp>& __p)
782 { return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del))); }
785 template<typename _Tp, _Lock_policy _Lp>
789 typedef _Tp element_type;
792 : _M_ptr(0), _M_refcount() // never throws
795 // Generated copy constructor, assignment, destructor are fine.
797 // The "obvious" converting constructor implementation:
799 // template<typename _Tp1>
800 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
801 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
804 // has a serious problem.
806 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
807 // conversion may require access to *__r._M_ptr (virtual inheritance).
809 // It is not possible to avoid spurious access violations since
810 // in multithreaded programs __r._M_ptr may be invalidated at any point.
811 template<typename _Tp1>
812 __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
813 : _M_refcount(__r._M_refcount) // never throws
815 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
816 _M_ptr = __r.lock().get();
819 template<typename _Tp1>
820 __weak_ptr(const __shared_ptr<_Tp1, _Lp>& __r)
821 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
822 { __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) }
824 template<typename _Tp1>
826 operator=(const __weak_ptr<_Tp1, _Lp>& __r) // never throws
828 _M_ptr = __r.lock().get();
829 _M_refcount = __r._M_refcount;
833 template<typename _Tp1>
835 operator=(const __shared_ptr<_Tp1, _Lp>& __r) // never throws
838 _M_refcount = __r._M_refcount;
842 __shared_ptr<_Tp, _Lp>
843 lock() const // never throws
846 // Optimization: avoid throw overhead.
848 return __shared_ptr<element_type, _Lp>();
852 return __shared_ptr<element_type, _Lp>(*this);
854 catch(const bad_weak_ptr&)
856 // Q: How can we get here?
857 // A: Another thread may have invalidated r after the
858 // use_count test above.
859 return __shared_ptr<element_type>();
863 // Optimization: avoid try/catch overhead when single threaded.
864 return expired() ? __shared_ptr<element_type, _Lp>()
865 : __shared_ptr<element_type, _Lp>(*this);
871 use_count() const // never throws
872 { return _M_refcount._M_get_use_count(); }
875 expired() const // never throws
876 { return _M_refcount._M_get_use_count() == 0; }
879 reset() // never throws
880 { __weak_ptr().swap(*this); }
883 swap(__weak_ptr& __s) // never throws
885 std::swap(_M_ptr, __s._M_ptr);
886 _M_refcount._M_swap(__s._M_refcount);
890 // Used by __enable_shared_from_this.
892 _M_assign(_Tp* __ptr, const __shared_count<_Lp>& __refcount)
895 _M_refcount = __refcount;
898 template<typename _Tp1>
900 _M_less(const __weak_ptr<_Tp1, _Lp>& __rhs) const
901 { return _M_refcount < __rhs._M_refcount; }
903 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
904 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
905 friend class __enable_shared_from_this<_Tp, _Lp>;
906 friend class enable_shared_from_this<_Tp>;
908 // Friend injected into namespace and found by ADL.
909 template<typename _Tp1>
911 operator<(const __weak_ptr& __lhs, const __weak_ptr<_Tp1, _Lp>& __rhs)
912 { return __lhs._M_less(__rhs); }
914 _Tp* _M_ptr; // Contained pointer.
915 __weak_count<_Lp> _M_refcount; // Reference counter.
918 // 2.2.4.7 weak_ptr specialized algorithms.
919 template<typename _Tp, _Lock_policy _Lp>
921 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b)
925 template<typename _Tp, _Lock_policy _Lp>
926 class __enable_shared_from_this
929 __enable_shared_from_this() { }
931 __enable_shared_from_this(const __enable_shared_from_this&) { }
933 __enable_shared_from_this&
934 operator=(const __enable_shared_from_this&)
937 ~__enable_shared_from_this() { }
940 __shared_ptr<_Tp, _Lp>
942 { return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
944 __shared_ptr<const _Tp, _Lp>
945 shared_from_this() const
946 { return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
949 template<typename _Tp1>
951 _M_weak_assign(_Tp1* __p, const __shared_count<_Lp>& __n) const
952 { _M_weak_this._M_assign(__p, __n); }
954 template<typename _Tp1>
956 __enable_shared_from_this_helper(const __shared_count<_Lp>& __pn,
957 const __enable_shared_from_this* __pe,
961 __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
964 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
968 // The actual TR1 shared_ptr, with forwarding constructors and
969 // assignment operators.
970 template<typename _Tp>
972 : public __shared_ptr<_Tp>
976 : __shared_ptr<_Tp>() { }
978 template<typename _Tp1>
980 shared_ptr(_Tp1* __p)
981 : __shared_ptr<_Tp>(__p) { }
983 template<typename _Tp1, typename _Deleter>
984 shared_ptr(_Tp1* __p, _Deleter __d)
985 : __shared_ptr<_Tp>(__p, __d) { }
987 template<typename _Tp1>
988 shared_ptr(const shared_ptr<_Tp1>& __r)
989 : __shared_ptr<_Tp>(__r) { }
991 template<typename _Tp1>
993 shared_ptr(const weak_ptr<_Tp1>& __r)
994 : __shared_ptr<_Tp>(__r) { }
996 template<typename _Tp1>
998 shared_ptr(std::auto_ptr<_Tp1>& __r)
999 : __shared_ptr<_Tp>(__r) { }
1001 template<typename _Tp1>
1002 shared_ptr(const shared_ptr<_Tp1>& __r, __static_cast_tag)
1003 : __shared_ptr<_Tp>(__r, __static_cast_tag()) { }
1005 template<typename _Tp1>
1006 shared_ptr(const shared_ptr<_Tp1>& __r, __const_cast_tag)
1007 : __shared_ptr<_Tp>(__r, __const_cast_tag()) { }
1009 template<typename _Tp1>
1010 shared_ptr(const shared_ptr<_Tp1>& __r, __dynamic_cast_tag)
1011 : __shared_ptr<_Tp>(__r, __dynamic_cast_tag()) { }
1013 template<typename _Tp1>
1015 operator=(const shared_ptr<_Tp1>& __r) // never throws
1017 this->__shared_ptr<_Tp>::operator=(__r);
1021 template<typename _Tp1>
1023 operator=(std::auto_ptr<_Tp1>& __r)
1025 this->__shared_ptr<_Tp>::operator=(__r);
1030 template<typename _Tp, typename _Tp1>
1032 static_pointer_cast(const shared_ptr<_Tp1>& __r)
1033 { return shared_ptr<_Tp>(__r, __static_cast_tag()); }
1035 template<typename _Tp, typename _Tp1>
1037 const_pointer_cast(const shared_ptr<_Tp1>& __r)
1038 { return shared_ptr<_Tp>(__r, __const_cast_tag()); }
1040 template<typename _Tp, typename _Tp1>
1042 dynamic_pointer_cast(const shared_ptr<_Tp1>& __r)
1043 { return shared_ptr<_Tp>(__r, __dynamic_cast_tag()); }
1046 // The actual TR1 weak_ptr, with forwarding constructors and
1047 // assignment operators.
1048 template<typename _Tp>
1050 : public __weak_ptr<_Tp>
1054 : __weak_ptr<_Tp>() { }
1056 template<typename _Tp1>
1057 weak_ptr(const weak_ptr<_Tp1>& __r)
1058 : __weak_ptr<_Tp>(__r) { }
1060 template<typename _Tp1>
1061 weak_ptr(const shared_ptr<_Tp1>& __r)
1062 : __weak_ptr<_Tp>(__r) { }
1064 template<typename _Tp1>
1066 operator=(const weak_ptr<_Tp1>& __r) // never throws
1068 this->__weak_ptr<_Tp>::operator=(__r);
1072 template<typename _Tp1>
1074 operator=(const shared_ptr<_Tp1>& __r) // never throws
1076 this->__weak_ptr<_Tp>::operator=(__r);
1081 lock() const // never throws
1084 if (this->expired())
1085 return shared_ptr<_Tp>();
1089 return shared_ptr<_Tp>(*this);
1091 catch(const bad_weak_ptr&)
1093 return shared_ptr<_Tp>();
1096 return this->expired() ? shared_ptr<_Tp>()
1097 : shared_ptr<_Tp>(*this);
1103 template<typename _Tp>
1104 class enable_shared_from_this
1107 enable_shared_from_this() { }
1109 enable_shared_from_this(const enable_shared_from_this&) { }
1111 enable_shared_from_this&
1112 operator=(const enable_shared_from_this&)
1115 ~enable_shared_from_this() { }
1120 { return shared_ptr<_Tp>(this->_M_weak_this); }
1122 shared_ptr<const _Tp>
1123 shared_from_this() const
1124 { return shared_ptr<const _Tp>(this->_M_weak_this); }
1127 template<typename _Tp1>
1129 _M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const
1130 { _M_weak_this._M_assign(__p, __n); }
1132 template<typename _Tp1>
1134 __enable_shared_from_this_helper(const __shared_count<>& __pn,
1135 const enable_shared_from_this* __pe,
1139 __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
1142 mutable weak_ptr<_Tp> _M_weak_this;
1145 _GLIBCXX_END_NAMESPACE_TR1