1 // <tr1/shared_ptr.h> -*- C++ -*-
3 // Copyright (C) 2007, 2008, 2009 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/shared_ptr.h
50 * This is an internal header file, included by other library headers.
51 * You should not attempt to use it directly.
54 #if defined(_GLIBCXX_INCLUDE_AS_CXX0X)
55 # error TR1 header cannot be included from C++0x header
63 template<typename _Ptr, typename _Deleter, _Lock_policy _Lp>
64 class _Sp_counted_base_impl
65 : public _Sp_counted_base<_Lp>
70 * @pre __d(__p) must not throw.
72 _Sp_counted_base_impl(_Ptr __p, _Deleter __d)
73 : _M_ptr(__p), _M_del(__d) { }
76 _M_dispose() // nothrow
80 _M_get_deleter(const std::type_info& __ti)
81 { return __ti == typeid(_Deleter) ? &_M_del : 0; }
84 _Sp_counted_base_impl(const _Sp_counted_base_impl&);
85 _Sp_counted_base_impl& operator=(const _Sp_counted_base_impl&);
87 _Ptr _M_ptr; // copy constructor must not throw
88 _Deleter _M_del; // copy constructor must not throw
91 template<_Lock_policy _Lp = __default_lock_policy>
94 template<typename _Tp>
97 typedef void result_type;
98 typedef _Tp* argument_type;
99 void operator()(_Tp* __p) const { delete __p; }
102 template<_Lock_policy _Lp = __default_lock_policy>
107 : _M_pi(0) // nothrow
110 template<typename _Ptr>
111 __shared_count(_Ptr __p) : _M_pi(0)
115 typedef typename std::tr1::remove_pointer<_Ptr>::type _Tp;
116 _M_pi = new _Sp_counted_base_impl<_Ptr, _Sp_deleter<_Tp>, _Lp>(
117 __p, _Sp_deleter<_Tp>());
122 __throw_exception_again;
126 template<typename _Ptr, typename _Deleter>
127 __shared_count(_Ptr __p, _Deleter __d) : _M_pi(0)
131 _M_pi = new _Sp_counted_base_impl<_Ptr, _Deleter, _Lp>(__p, __d);
135 __d(__p); // Call _Deleter on __p.
136 __throw_exception_again;
140 // Special case for auto_ptr<_Tp> to provide the strong guarantee.
141 template<typename _Tp>
143 __shared_count(std::auto_ptr<_Tp>& __r)
144 : _M_pi(new _Sp_counted_base_impl<_Tp*,
145 _Sp_deleter<_Tp>, _Lp >(__r.get(), _Sp_deleter<_Tp>()))
148 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
150 __shared_count(const __weak_count<_Lp>& __r);
152 ~__shared_count() // nothrow
158 __shared_count(const __shared_count& __r)
159 : _M_pi(__r._M_pi) // nothrow
162 _M_pi->_M_add_ref_copy();
166 operator=(const __shared_count& __r) // nothrow
168 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
172 __tmp->_M_add_ref_copy();
181 _M_swap(__shared_count& __r) // nothrow
183 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
189 _M_get_use_count() const // nothrow
190 { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
193 _M_unique() const // nothrow
194 { return this->_M_get_use_count() == 1; }
197 operator==(const __shared_count& __a, const __shared_count& __b)
198 { return __a._M_pi == __b._M_pi; }
201 operator<(const __shared_count& __a, const __shared_count& __b)
202 { return std::less<_Sp_counted_base<_Lp>*>()(__a._M_pi, __b._M_pi); }
205 _M_get_deleter(const std::type_info& __ti) const
206 { return _M_pi ? _M_pi->_M_get_deleter(__ti) : 0; }
209 friend class __weak_count<_Lp>;
211 _Sp_counted_base<_Lp>* _M_pi;
215 template<_Lock_policy _Lp>
220 : _M_pi(0) // nothrow
223 __weak_count(const __shared_count<_Lp>& __r)
224 : _M_pi(__r._M_pi) // nothrow
227 _M_pi->_M_weak_add_ref();
230 __weak_count(const __weak_count<_Lp>& __r)
231 : _M_pi(__r._M_pi) // nothrow
234 _M_pi->_M_weak_add_ref();
237 ~__weak_count() // nothrow
240 _M_pi->_M_weak_release();
244 operator=(const __shared_count<_Lp>& __r) // nothrow
246 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
248 __tmp->_M_weak_add_ref();
250 _M_pi->_M_weak_release();
256 operator=(const __weak_count<_Lp>& __r) // nothrow
258 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
260 __tmp->_M_weak_add_ref();
262 _M_pi->_M_weak_release();
268 _M_swap(__weak_count<_Lp>& __r) // nothrow
270 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
276 _M_get_use_count() const // nothrow
277 { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
280 operator==(const __weak_count<_Lp>& __a, const __weak_count<_Lp>& __b)
281 { return __a._M_pi == __b._M_pi; }
284 operator<(const __weak_count<_Lp>& __a, const __weak_count<_Lp>& __b)
285 { return std::less<_Sp_counted_base<_Lp>*>()(__a._M_pi, __b._M_pi); }
288 friend class __shared_count<_Lp>;
290 _Sp_counted_base<_Lp>* _M_pi;
293 // now that __weak_count is defined we can define this constructor:
294 template<_Lock_policy _Lp>
296 __shared_count<_Lp>::
297 __shared_count(const __weak_count<_Lp>& __r)
301 _M_pi->_M_add_ref_lock();
303 __throw_bad_weak_ptr();
306 // Forward declarations.
307 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
310 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
313 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
314 class __enable_shared_from_this;
316 template<typename _Tp>
319 template<typename _Tp>
322 template<typename _Tp>
323 class enable_shared_from_this;
325 // Support for enable_shared_from_this.
327 // Friend of __enable_shared_from_this.
328 template<_Lock_policy _Lp, typename _Tp1, typename _Tp2>
330 __enable_shared_from_this_helper(const __shared_count<_Lp>&,
331 const __enable_shared_from_this<_Tp1,
334 // Friend of enable_shared_from_this.
335 template<typename _Tp1, typename _Tp2>
337 __enable_shared_from_this_helper(const __shared_count<>&,
338 const enable_shared_from_this<_Tp1>*,
341 template<_Lock_policy _Lp>
343 __enable_shared_from_this_helper(const __shared_count<_Lp>&, ...)
347 struct __static_cast_tag { };
348 struct __const_cast_tag { };
349 struct __dynamic_cast_tag { };
352 * @class __shared_ptr
354 * A smart pointer with reference-counted copy semantics.
355 * The object pointed to is deleted when the last shared_ptr pointing to
356 * it is destroyed or reset.
358 template<typename _Tp, _Lock_policy _Lp>
362 typedef _Tp element_type;
364 /** @brief Construct an empty %__shared_ptr.
365 * @post use_count()==0 && get()==0
368 : _M_ptr(0), _M_refcount() // never throws
371 /** @brief Construct a %__shared_ptr that owns the pointer @a __p.
372 * @param __p A pointer that is convertible to element_type*.
373 * @post use_count() == 1 && get() == __p
374 * @throw std::bad_alloc, in which case @c delete @a __p is called.
376 template<typename _Tp1>
378 __shared_ptr(_Tp1* __p)
379 : _M_ptr(__p), _M_refcount(__p)
381 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
382 // __glibcxx_function_requires(_CompleteConcept<_Tp1*>)
383 __enable_shared_from_this_helper(_M_refcount, __p, __p);
387 // Requirements: _Deleter's copy constructor and destructor must
390 // __shared_ptr will release __p by calling __d(__p)
392 /** @brief Construct a %__shared_ptr that owns the pointer @a __p
393 * and the deleter @a __d.
394 * @param __p A pointer.
395 * @param __d A deleter.
396 * @post use_count() == 1 && get() == __p
397 * @throw std::bad_alloc, in which case @a __d(__p) is called.
399 template<typename _Tp1, typename _Deleter>
400 __shared_ptr(_Tp1* __p, _Deleter __d)
401 : _M_ptr(__p), _M_refcount(__p, __d)
403 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
404 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
405 __enable_shared_from_this_helper(_M_refcount, __p, __p);
408 // generated copy constructor, assignment, destructor are fine.
410 /** @brief If @a __r is empty, constructs an empty %__shared_ptr;
411 * otherwise construct a %__shared_ptr that shares ownership
413 * @param __r A %__shared_ptr.
414 * @post get() == __r.get() && use_count() == __r.use_count()
416 template<typename _Tp1>
417 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r)
418 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
419 { __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) }
421 /** @brief Constructs a %__shared_ptr that shares ownership with @a __r
422 * and stores a copy of the pointer stored in @a __r.
423 * @param __r A weak_ptr.
424 * @post use_count() == __r.use_count()
425 * @throw bad_weak_ptr when __r.expired(),
426 * in which case the constructor has no effect.
428 template<typename _Tp1>
430 __shared_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
431 : _M_refcount(__r._M_refcount) // may throw
433 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
434 // It is now safe to copy __r._M_ptr, as _M_refcount(__r._M_refcount)
439 #if !defined(__GXX_EXPERIMENTAL_CXX0X__) || _GLIBCXX_DEPRECATED
441 * @post use_count() == 1 and __r.get() == 0
443 template<typename _Tp1>
445 __shared_ptr(std::auto_ptr<_Tp1>& __r)
446 : _M_ptr(__r.get()), _M_refcount()
448 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
449 // TODO requires _Tp1 is complete, delete __r.release() well-formed
450 _Tp1* __tmp = __r.get();
451 _M_refcount = __shared_count<_Lp>(__r);
452 __enable_shared_from_this_helper(_M_refcount, __tmp, __tmp);
457 template<typename _Tp1>
458 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r, __static_cast_tag)
459 : _M_ptr(static_cast<element_type*>(__r._M_ptr)),
460 _M_refcount(__r._M_refcount)
463 template<typename _Tp1>
464 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r, __const_cast_tag)
465 : _M_ptr(const_cast<element_type*>(__r._M_ptr)),
466 _M_refcount(__r._M_refcount)
469 template<typename _Tp1>
470 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r, __dynamic_cast_tag)
471 : _M_ptr(dynamic_cast<element_type*>(__r._M_ptr)),
472 _M_refcount(__r._M_refcount)
474 if (_M_ptr == 0) // need to allocate new counter -- the cast failed
475 _M_refcount = __shared_count<_Lp>();
478 template<typename _Tp1>
480 operator=(const __shared_ptr<_Tp1, _Lp>& __r) // never throws
483 _M_refcount = __r._M_refcount; // __shared_count::op= doesn't throw
487 #if !defined(__GXX_EXPERIMENTAL_CXX0X__) || _GLIBCXX_DEPRECATED
488 template<typename _Tp1>
490 operator=(std::auto_ptr<_Tp1>& __r)
492 __shared_ptr(__r).swap(*this);
498 reset() // never throws
499 { __shared_ptr().swap(*this); }
501 template<typename _Tp1>
503 reset(_Tp1* __p) // _Tp1 must be complete.
505 // Catch self-reset errors.
506 _GLIBCXX_DEBUG_ASSERT(__p == 0 || __p != _M_ptr);
507 __shared_ptr(__p).swap(*this);
510 template<typename _Tp1, typename _Deleter>
512 reset(_Tp1* __p, _Deleter __d)
513 { __shared_ptr(__p, __d).swap(*this); }
515 // Allow class instantiation when _Tp is [cv-qual] void.
516 typename std::tr1::add_reference<_Tp>::type
517 operator*() const // never throws
519 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
524 operator->() const // never throws
526 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
531 get() const // never throws
534 // Implicit conversion to "bool"
536 typedef _Tp* __shared_ptr::*__unspecified_bool_type;
539 operator __unspecified_bool_type() const // never throws
540 { return _M_ptr == 0 ? 0 : &__shared_ptr::_M_ptr; }
543 unique() const // never throws
544 { return _M_refcount._M_unique(); }
547 use_count() const // never throws
548 { return _M_refcount._M_get_use_count(); }
551 swap(__shared_ptr<_Tp, _Lp>& __other) // never throws
553 std::swap(_M_ptr, __other._M_ptr);
554 _M_refcount._M_swap(__other._M_refcount);
559 _M_get_deleter(const std::type_info& __ti) const
560 { return _M_refcount._M_get_deleter(__ti); }
562 template<typename _Tp1, _Lock_policy _Lp1>
564 _M_less(const __shared_ptr<_Tp1, _Lp1>& __rhs) const
565 { return _M_refcount < __rhs._M_refcount; }
567 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
568 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
570 template<typename _Del, typename _Tp1, _Lock_policy _Lp1>
571 friend _Del* get_deleter(const __shared_ptr<_Tp1, _Lp1>&);
573 // Friends injected into enclosing namespace and found by ADL:
574 template<typename _Tp1>
576 operator==(const __shared_ptr& __a, const __shared_ptr<_Tp1, _Lp>& __b)
577 { return __a.get() == __b.get(); }
579 template<typename _Tp1>
581 operator!=(const __shared_ptr& __a, const __shared_ptr<_Tp1, _Lp>& __b)
582 { return __a.get() != __b.get(); }
584 template<typename _Tp1>
586 operator<(const __shared_ptr& __a, const __shared_ptr<_Tp1, _Lp>& __b)
587 { return __a._M_less(__b); }
589 _Tp* _M_ptr; // Contained pointer.
590 __shared_count<_Lp> _M_refcount; // Reference counter.
593 // 2.2.3.8 shared_ptr specialized algorithms.
594 template<typename _Tp, _Lock_policy _Lp>
596 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b)
599 // 2.2.3.9 shared_ptr casts
600 /** @warning The seemingly equivalent
601 * <code>shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))</code>
602 * will eventually result in undefined behaviour,
603 * attempting to delete the same object twice.
605 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
606 inline __shared_ptr<_Tp, _Lp>
607 static_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r)
608 { return __shared_ptr<_Tp, _Lp>(__r, __static_cast_tag()); }
610 /** @warning The seemingly equivalent
611 * <code>shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))</code>
612 * will eventually result in undefined behaviour,
613 * attempting to delete the same object twice.
615 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
616 inline __shared_ptr<_Tp, _Lp>
617 const_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r)
618 { return __shared_ptr<_Tp, _Lp>(__r, __const_cast_tag()); }
620 /** @warning The seemingly equivalent
621 * <code>shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))</code>
622 * will eventually result in undefined behaviour,
623 * attempting to delete the same object twice.
625 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
626 inline __shared_ptr<_Tp, _Lp>
627 dynamic_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r)
628 { return __shared_ptr<_Tp, _Lp>(__r, __dynamic_cast_tag()); }
630 // 2.2.3.7 shared_ptr I/O
631 template<typename _Ch, typename _Tr, typename _Tp, _Lock_policy _Lp>
632 std::basic_ostream<_Ch, _Tr>&
633 operator<<(std::basic_ostream<_Ch, _Tr>& __os,
634 const __shared_ptr<_Tp, _Lp>& __p)
640 // 2.2.3.10 shared_ptr get_deleter (experimental)
641 template<typename _Del, typename _Tp, _Lock_policy _Lp>
643 get_deleter(const __shared_ptr<_Tp, _Lp>& __p)
644 { return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del))); }
647 template<typename _Tp, _Lock_policy _Lp>
651 typedef _Tp element_type;
654 : _M_ptr(0), _M_refcount() // never throws
657 // Generated copy constructor, assignment, destructor are fine.
659 // The "obvious" converting constructor implementation:
661 // template<typename _Tp1>
662 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
663 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
666 // has a serious problem.
668 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
669 // conversion may require access to *__r._M_ptr (virtual inheritance).
671 // It is not possible to avoid spurious access violations since
672 // in multithreaded programs __r._M_ptr may be invalidated at any point.
673 template<typename _Tp1>
674 __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
675 : _M_refcount(__r._M_refcount) // never throws
677 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
678 _M_ptr = __r.lock().get();
681 template<typename _Tp1>
682 __weak_ptr(const __shared_ptr<_Tp1, _Lp>& __r)
683 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
684 { __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) }
686 template<typename _Tp1>
688 operator=(const __weak_ptr<_Tp1, _Lp>& __r) // never throws
690 _M_ptr = __r.lock().get();
691 _M_refcount = __r._M_refcount;
695 template<typename _Tp1>
697 operator=(const __shared_ptr<_Tp1, _Lp>& __r) // never throws
700 _M_refcount = __r._M_refcount;
704 __shared_ptr<_Tp, _Lp>
705 lock() const // never throws
708 // Optimization: avoid throw overhead.
710 return __shared_ptr<element_type, _Lp>();
714 return __shared_ptr<element_type, _Lp>(*this);
716 __catch(const bad_weak_ptr&)
718 // Q: How can we get here?
719 // A: Another thread may have invalidated r after the
720 // use_count test above.
721 return __shared_ptr<element_type, _Lp>();
725 // Optimization: avoid try/catch overhead when single threaded.
726 return expired() ? __shared_ptr<element_type, _Lp>()
727 : __shared_ptr<element_type, _Lp>(*this);
733 use_count() const // never throws
734 { return _M_refcount._M_get_use_count(); }
737 expired() const // never throws
738 { return _M_refcount._M_get_use_count() == 0; }
741 reset() // never throws
742 { __weak_ptr().swap(*this); }
745 swap(__weak_ptr& __s) // never throws
747 std::swap(_M_ptr, __s._M_ptr);
748 _M_refcount._M_swap(__s._M_refcount);
752 // Used by __enable_shared_from_this.
754 _M_assign(_Tp* __ptr, const __shared_count<_Lp>& __refcount)
757 _M_refcount = __refcount;
760 template<typename _Tp1>
762 _M_less(const __weak_ptr<_Tp1, _Lp>& __rhs) const
763 { return _M_refcount < __rhs._M_refcount; }
765 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
766 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
767 friend class __enable_shared_from_this<_Tp, _Lp>;
768 friend class enable_shared_from_this<_Tp>;
770 // Friend injected into namespace and found by ADL.
771 template<typename _Tp1>
773 operator<(const __weak_ptr& __lhs, const __weak_ptr<_Tp1, _Lp>& __rhs)
774 { return __lhs._M_less(__rhs); }
776 _Tp* _M_ptr; // Contained pointer.
777 __weak_count<_Lp> _M_refcount; // Reference counter.
780 // 2.2.4.7 weak_ptr specialized algorithms.
781 template<typename _Tp, _Lock_policy _Lp>
783 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b)
787 template<typename _Tp, _Lock_policy _Lp>
788 class __enable_shared_from_this
791 __enable_shared_from_this() { }
793 __enable_shared_from_this(const __enable_shared_from_this&) { }
795 __enable_shared_from_this&
796 operator=(const __enable_shared_from_this&)
799 ~__enable_shared_from_this() { }
802 __shared_ptr<_Tp, _Lp>
804 { return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
806 __shared_ptr<const _Tp, _Lp>
807 shared_from_this() const
808 { return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
811 template<typename _Tp1>
813 _M_weak_assign(_Tp1* __p, const __shared_count<_Lp>& __n) const
814 { _M_weak_this._M_assign(__p, __n); }
816 template<typename _Tp1>
818 __enable_shared_from_this_helper(const __shared_count<_Lp>& __pn,
819 const __enable_shared_from_this* __pe,
823 __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
826 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
831 // The actual shared_ptr, with forwarding constructors and
832 // assignment operators.
833 template<typename _Tp>
835 : public __shared_ptr<_Tp>
839 : __shared_ptr<_Tp>() { }
841 template<typename _Tp1>
843 shared_ptr(_Tp1* __p)
844 : __shared_ptr<_Tp>(__p) { }
846 template<typename _Tp1, typename _Deleter>
847 shared_ptr(_Tp1* __p, _Deleter __d)
848 : __shared_ptr<_Tp>(__p, __d) { }
850 template<typename _Tp1>
851 shared_ptr(const shared_ptr<_Tp1>& __r)
852 : __shared_ptr<_Tp>(__r) { }
854 template<typename _Tp1>
856 shared_ptr(const weak_ptr<_Tp1>& __r)
857 : __shared_ptr<_Tp>(__r) { }
859 #if !defined(__GXX_EXPERIMENTAL_CXX0X__) || _GLIBCXX_DEPRECATED
860 template<typename _Tp1>
862 shared_ptr(std::auto_ptr<_Tp1>& __r)
863 : __shared_ptr<_Tp>(__r) { }
866 template<typename _Tp1>
867 shared_ptr(const shared_ptr<_Tp1>& __r, __static_cast_tag)
868 : __shared_ptr<_Tp>(__r, __static_cast_tag()) { }
870 template<typename _Tp1>
871 shared_ptr(const shared_ptr<_Tp1>& __r, __const_cast_tag)
872 : __shared_ptr<_Tp>(__r, __const_cast_tag()) { }
874 template<typename _Tp1>
875 shared_ptr(const shared_ptr<_Tp1>& __r, __dynamic_cast_tag)
876 : __shared_ptr<_Tp>(__r, __dynamic_cast_tag()) { }
878 template<typename _Tp1>
880 operator=(const shared_ptr<_Tp1>& __r) // never throws
882 this->__shared_ptr<_Tp>::operator=(__r);
886 #if !defined(__GXX_EXPERIMENTAL_CXX0X__) || _GLIBCXX_DEPRECATED
887 template<typename _Tp1>
889 operator=(std::auto_ptr<_Tp1>& __r)
891 this->__shared_ptr<_Tp>::operator=(__r);
897 // 2.2.3.8 shared_ptr specialized algorithms.
898 template<typename _Tp>
900 swap(__shared_ptr<_Tp>& __a, __shared_ptr<_Tp>& __b)
903 template<typename _Tp, typename _Tp1>
904 inline shared_ptr<_Tp>
905 static_pointer_cast(const shared_ptr<_Tp1>& __r)
906 { return shared_ptr<_Tp>(__r, __static_cast_tag()); }
908 template<typename _Tp, typename _Tp1>
909 inline shared_ptr<_Tp>
910 const_pointer_cast(const shared_ptr<_Tp1>& __r)
911 { return shared_ptr<_Tp>(__r, __const_cast_tag()); }
913 template<typename _Tp, typename _Tp1>
914 inline shared_ptr<_Tp>
915 dynamic_pointer_cast(const shared_ptr<_Tp1>& __r)
916 { return shared_ptr<_Tp>(__r, __dynamic_cast_tag()); }
920 // The actual weak_ptr, with forwarding constructors and
921 // assignment operators.
922 template<typename _Tp>
924 : public __weak_ptr<_Tp>
928 : __weak_ptr<_Tp>() { }
930 template<typename _Tp1>
931 weak_ptr(const weak_ptr<_Tp1>& __r)
932 : __weak_ptr<_Tp>(__r) { }
934 template<typename _Tp1>
935 weak_ptr(const shared_ptr<_Tp1>& __r)
936 : __weak_ptr<_Tp>(__r) { }
938 template<typename _Tp1>
940 operator=(const weak_ptr<_Tp1>& __r) // never throws
942 this->__weak_ptr<_Tp>::operator=(__r);
946 template<typename _Tp1>
948 operator=(const shared_ptr<_Tp1>& __r) // never throws
950 this->__weak_ptr<_Tp>::operator=(__r);
955 lock() const // never throws
959 return shared_ptr<_Tp>();
963 return shared_ptr<_Tp>(*this);
965 __catch(const bad_weak_ptr&)
967 return shared_ptr<_Tp>();
970 return this->expired() ? shared_ptr<_Tp>()
971 : shared_ptr<_Tp>(*this);
976 /// enable_shared_from_this
977 template<typename _Tp>
978 class enable_shared_from_this
981 enable_shared_from_this() { }
983 enable_shared_from_this(const enable_shared_from_this&) { }
985 enable_shared_from_this&
986 operator=(const enable_shared_from_this&)
989 ~enable_shared_from_this() { }
994 { return shared_ptr<_Tp>(this->_M_weak_this); }
996 shared_ptr<const _Tp>
997 shared_from_this() const
998 { return shared_ptr<const _Tp>(this->_M_weak_this); }
1001 template<typename _Tp1>
1003 _M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const
1004 { _M_weak_this._M_assign(__p, __n); }
1006 template<typename _Tp1>
1008 __enable_shared_from_this_helper(const __shared_count<>& __pn,
1009 const enable_shared_from_this* __pe,
1013 __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
1016 mutable weak_ptr<_Tp> _M_weak_this;