2005-03-24 Benjamin Kosnik <bkoz@redhat.com>
[official-gcc.git] / libstdc++-v3 / include / tr1 / boost_shared_ptr.h
blobf55c0537586522076111e6bd194ba56f22199d30
1 // <tr1/boost_shared_ptr.h> -*- C++ -*-
3 // Copyright (C) 2005 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
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.
30 // shared_count.hpp
31 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
33 // shared_ptr.hpp
34 // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
35 // Copyright (C) 2001, 2002, 2003 Peter Dimov
37 // weak_ptr.hpp
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 boost_memory.h
50 * This is an internal header file, included by other library headers.
51 * You should not attempt to use it directly.
54 #ifndef _BOOST_SHARED_PTR_H
55 #define _BOOST_SHARED_PTR_H 1
57 // namespace std::tr1
58 namespace std
60 namespace tr1
63 class bad_weak_ptr : public std::exception
65 public:
67 virtual char const* what() const throw()
69 return "tr1::bad_weak_ptr";
73 // Helper for exception objects in <tr1/memory>
74 // TODO this should be defined in a different file.
75 inline void
76 __throw_bad_weak_ptr()
78 #if __EXCEPTIONS
79 throw bad_weak_ptr();
80 #else
81 std::abort();
82 #endif
86 template <typename _Tp>
87 struct _Sp_deleter
89 typedef void result_type;
90 typedef _Tp* argument_type;
92 void
93 operator()(_Tp* p) const
94 { delete p; }
98 class _Sp_counted_base
100 public:
102 _Sp_counted_base()
103 : _M_use_count(1), _M_weak_count(1)
106 virtual
107 ~_Sp_counted_base() // nothrow
110 // dispose() is called when _M_use_count drops to zero, to release
111 // the resources managed by *this.
112 virtual void
113 dispose() = 0; // nothrow
115 // destroy() is called when _M_weak_count drops to zero.
116 virtual void
117 destroy() // nothrow
119 delete this;
122 virtual void*
123 get_deleter(const std::type_info&) = 0;
125 void
126 add_ref_copy()
128 __gnu_cxx::__atomic_add(&_M_use_count, 1);
131 void
132 add_ref_lock()
134 if (_M_use_count <= 0) // TODO not yet MT safe XXX
136 __throw_bad_weak_ptr();
138 __gnu_cxx::__atomic_add(&_M_use_count, 1);
141 void
142 release() // nothrow
144 if (__gnu_cxx::__exchange_and_add(&_M_use_count, -1) <= 1)
146 dispose();
147 weak_release();
151 void
152 weak_add_ref() // nothrow
154 __gnu_cxx::__atomic_add(&_M_weak_count, 1);
157 void
158 weak_release() // nothrow
160 if (__gnu_cxx::__exchange_and_add(&_M_weak_count, -1) <= 1)
162 destroy();
166 long
167 use_count() const // nothrow
169 return _M_use_count; // XXX is this MT safe?
172 private:
174 _Sp_counted_base(_Sp_counted_base const&);
175 _Sp_counted_base& operator= (_Sp_counted_base const&);
177 _Atomic_word _M_use_count; // #shared
178 _Atomic_word _M_weak_count; // #weak + (#shared != 0)
181 template <typename _Ptr, typename _Deleter>
182 class _Sp_counted_base_impl : public _Sp_counted_base
184 public:
187 * @brief
188 * @pre d(p) must not throw.
190 _Sp_counted_base_impl(_Ptr __p, _Deleter __d)
191 : _M_ptr(__p), _M_del(__d)
194 virtual void
195 dispose() // nothrow
197 _M_del(_M_ptr);
200 virtual void*
201 get_deleter(const std::type_info& __ti)
203 return __ti == typeid(_Deleter) ? &_M_del : 0;
206 private:
207 _Sp_counted_base_impl(const _Sp_counted_base_impl&);
208 _Sp_counted_base_impl& operator=(const _Sp_counted_base_impl&);
210 _Ptr _M_ptr; // copy constructor must not throw
211 _Deleter _M_del; // copy constructor must not throw
214 class weak_count;
216 class shared_count
218 private:
220 _Sp_counted_base* _M_pi;
222 friend class weak_count;
224 public:
226 shared_count()
227 : _M_pi(0) // nothrow
230 template <typename _Ptr, typename _Deleter>
231 shared_count(_Ptr __p, _Deleter __d)
232 : _M_pi(0)
236 _M_pi = new _Sp_counted_base_impl<_Ptr, _Deleter>(__p, __d);
238 catch(...)
240 __d(__p); // delete __p
241 __throw_exception_again;
245 // auto_ptr<_Tp> is special cased to provide the strong guarantee
247 template <typename _Tp>
248 explicit shared_count(std::auto_ptr<_Tp>& __r)
249 : _M_pi(new _Sp_counted_base_impl<_Tp*,_Sp_deleter<_Tp> >(
250 __r.get(), _Sp_deleter<_Tp>()
252 { __r.release(); }
254 // throws bad_weak_ptr when __r.use_count() == 0
255 explicit shared_count(const weak_count& __r);
257 ~shared_count() // nothrow
259 if (_M_pi != 0)
260 _M_pi->release();
263 shared_count(const shared_count& __r)
264 : _M_pi(__r._M_pi) // nothrow
266 if (_M_pi != 0)
267 _M_pi->add_ref_copy();
270 shared_count&
271 operator=(const shared_count& __r) // nothrow
273 _Sp_counted_base* __tmp = __r._M_pi;
275 if(__tmp != _M_pi)
277 if(__tmp != 0)
278 __tmp->add_ref_copy();
279 if(_M_pi != 0)
280 _M_pi->release();
281 _M_pi = __tmp;
283 return *this;
286 void swap(shared_count& __r) // nothrow
288 _Sp_counted_base* __tmp = __r._M_pi;
289 __r._M_pi = _M_pi;
290 _M_pi = __tmp;
293 long
294 use_count() const // nothrow
295 { return _M_pi != 0 ? _M_pi->use_count() : 0; }
297 bool
298 unique() const // nothrow
299 { return this->use_count() == 1; }
301 friend inline bool
302 operator==(const shared_count& __a, const shared_count& __b)
303 { return __a._M_pi == __b._M_pi; }
305 friend inline bool
306 operator<(const shared_count& __a, const shared_count& __b)
307 { return std::less<_Sp_counted_base*>()(__a._M_pi, __b._M_pi); }
309 void*
310 get_deleter(const std::type_info& __ti) const
311 { return _M_pi ? _M_pi->get_deleter(__ti) : 0; }
315 class weak_count
317 private:
319 _Sp_counted_base * _M_pi;
321 friend class shared_count;
323 public:
325 weak_count()
326 : _M_pi(0) // nothrow
329 weak_count(const shared_count& __r)
330 : _M_pi(__r._M_pi) // nothrow
332 if (_M_pi != 0)
333 _M_pi->weak_add_ref();
336 weak_count(const weak_count& __r)
337 : _M_pi(__r._M_pi) // nothrow
339 if (_M_pi != 0)
340 _M_pi->weak_add_ref();
343 ~weak_count() // nothrow
345 if (_M_pi != 0)
346 _M_pi->weak_release();
349 weak_count&
350 operator=(const shared_count& __r) // nothrow
352 _Sp_counted_base* __tmp = __r._M_pi;
353 if (__tmp != 0)
354 __tmp->weak_add_ref();
355 if (_M_pi != 0)
356 _M_pi->weak_release();
357 _M_pi = __tmp;
359 return *this;
362 weak_count&
363 operator=(const weak_count& __r) // nothrow
365 _Sp_counted_base * __tmp = __r._M_pi;
366 if (__tmp != 0)
367 __tmp->weak_add_ref();
368 if (_M_pi != 0)
369 _M_pi->weak_release();
370 _M_pi = __tmp;
372 return *this;
375 void
376 swap(weak_count& __r) // nothrow
378 _Sp_counted_base * __tmp = __r._M_pi;
379 __r._M_pi = _M_pi;
380 _M_pi = __tmp;
383 long
384 use_count() const // nothrow
385 { return _M_pi != 0 ? _M_pi->use_count() : 0; }
387 friend inline bool
388 operator==(const weak_count& __a, const weak_count& __b)
389 { return __a._M_pi == __b._M_pi; }
391 friend inline bool
392 operator<(const weak_count& __a, const weak_count& __b)
393 { return std::less<_Sp_counted_base*>()(__a._M_pi, __b._M_pi); }
396 inline
397 shared_count::shared_count(const weak_count& __r)
398 : _M_pi(__r._M_pi)
400 if (_M_pi != 0)
402 _M_pi->add_ref_lock();
404 else
406 __throw_bad_weak_ptr();
410 // fwd decls
411 template <typename _Tp> class weak_ptr;
412 template <typename _Tp> class enable_shared_from_this;
414 struct __static_cast_tag {};
415 struct __const_cast_tag {};
416 struct __dynamic_cast_tag {};
417 struct __polymorphic_cast_tag {};
419 template<class _Tp> struct shared_ptr_traits
421 typedef _Tp & reference;
424 template<> struct shared_ptr_traits<void>
426 typedef void reference;
429 template<> struct shared_ptr_traits<void const>
431 typedef void reference;
434 template<> struct shared_ptr_traits<void volatile>
436 typedef void reference;
439 template<> struct shared_ptr_traits<void const volatile>
441 typedef void reference;
445 // enable_shared_from_this support
447 // friend of enable_shared_from_this
448 template <typename _Tp1, typename _Tp2>
449 void
450 __enable_shared_from_this( const shared_count& __pn,
451 const enable_shared_from_this<_Tp1>* __pe,
452 const _Tp2* __px );
454 inline void
455 __enable_shared_from_this(const shared_count&, ...)
459 * @class shared_ptr <tr1/memory>
461 * A smart pointer with reference-counted copy semantics.
462 * The object pointed to is deleted when the last shared_ptr pointing to it
463 * is destroyed or reset.
466 template <typename _Tp>
467 class shared_ptr
469 typedef typename shared_ptr_traits<_Tp>::reference _Reference;
471 public:
473 typedef _Tp element_type;
475 /** @brief Construct an empty %shared_ptr.
476 * @post use_count()==0 && get()==0
478 shared_ptr() : _M_ptr(0), _M_refcount() // never throws
481 /** @brief Construct a %shared_ptr that owns the pointer @a p.
482 * @param p A pointer that is convertible to element_type*.
483 * @post use_count()==1 && get()==p
484 * @throw std::bad_alloc, in which case @c delete @a p is called.
486 template <typename _Tp1>
487 explicit shared_ptr(_Tp1* __p)
488 : _M_ptr(__p), _M_refcount(__p, _Sp_deleter<_Tp1>())
490 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
491 // __glibcxx_function_requires(_CompleteConcept<_Tp1*>)
493 __enable_shared_from_this( _M_refcount, __p, __p );
497 // Requirements: D's copy constructor and destructor must not throw
499 // shared_ptr will release p by calling d(p)
501 /** @brief Construct a %shared_ptr that owns the pointer @a p
502 * and the deleter @a d.
503 * @param p A pointer.
504 * @param d A deleter.
505 * @post use_count()==1 && get()==p
506 * @throw std::bad_alloc, in which case @a d(p) is called.
508 template <typename _Tp1, typename _Deleter>
509 shared_ptr(_Tp1* __p, _Deleter __d)
510 : _M_ptr(__p), _M_refcount(__p, __d)
512 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
513 // TODO requires D is CopyConstructible and d(p) well-formed
515 __enable_shared_from_this( _M_refcount, __p, __p );
518 // generated copy constructor, assignment, destructor are fine.
520 /** @brief If @a r is empty, constructs an empty %shared_ptr; otherwise
521 * construct a %shared_ptr that shares ownership with @a r.
522 * @param r A %shared_ptr.
523 * @post get()==r.get() && use_count()==r.use_count()
524 * @throw std::bad_alloc, in which case
526 template <typename _Tp1>
527 shared_ptr(const shared_ptr<_Tp1>& __r)
528 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
530 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
533 /** @brief Constructs a %shared_ptr that shares ownership with @a r
534 * and stores a copy of the pointer stored in @a r.
535 * @param r A weak_ptr.
536 * @post use_count()==r.use_count()
537 * @throw bad_weak_ptr when r.expired(),
538 * in which case the constructor has no effect.
540 template <typename _Tp1>
541 explicit shared_ptr(const weak_ptr<_Tp1>& __r)
542 : _M_refcount(__r._M_refcount) // may throw
544 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
545 // it is now safe to copy r__._M_ptr, as _M_refcount(__r._M_refcount)
546 // did not throw
547 _M_ptr = __r._M_ptr;
551 * @post use_count()==1 and r.get()==0
553 template <typename _Tp1>
554 explicit shared_ptr(std::auto_ptr<_Tp1>& __r)
555 : _M_ptr(__r.get()), _M_refcount()
557 // TODO requires r.release() convertible to _Tp*, Tp1 is complete,
558 // delete r.release() well-formed
559 _Tp1 * __tmp = __r.get();
560 _M_refcount = shared_count(__r);
562 __enable_shared_from_this( _M_refcount, __tmp, __tmp );
565 template <typename _Tp1>
566 shared_ptr(const shared_ptr<_Tp1>& __r, __static_cast_tag)
567 : _M_ptr(static_cast<element_type*>(__r._M_ptr))
568 , _M_refcount(__r._M_refcount)
571 template <typename _Tp1>
572 shared_ptr(const shared_ptr<_Tp1>& __r, __const_cast_tag)
573 : _M_ptr(const_cast<element_type*>(__r._M_ptr))
574 , _M_refcount(__r._M_refcount)
577 template <typename _Tp1>
578 shared_ptr(const shared_ptr<_Tp1>& __r, __dynamic_cast_tag)
579 : _M_ptr(dynamic_cast<element_type*>(__r._M_ptr))
580 , _M_refcount(__r._M_refcount)
582 if (_M_ptr == 0) // need to allocate new counter -- the cast failed
584 _M_refcount = shared_count();
588 template <typename _Tp1>
589 shared_ptr&
590 operator=(const shared_ptr<_Tp1>& __r) // never throws
592 _M_ptr = __r._M_ptr;
593 _M_refcount = __r._M_refcount; // shared_count::op= doesn't throw
594 return *this;
597 template <typename _Tp1>
598 shared_ptr&
599 operator=(std::auto_ptr<_Tp1>& __r)
601 shared_ptr(__r).swap(*this);
602 return *this;
605 void
606 reset() // never throws
607 { shared_ptr().swap(*this); }
609 template <typename _Tp1>
610 void
611 reset(_Tp1* __p) // _Tp1 must be complete
613 _GLIBCXX_DEBUG_ASSERT(__p == 0 || __p != _M_ptr); // catch self-reset errors
614 shared_ptr(__p).swap(*this);
617 template <typename _Tp1, typename _Deleter>
618 void
619 reset(_Tp1 * __p, _Deleter __d)
620 { shared_ptr(__p, __d).swap(*this); }
622 // error to instantiate if _Tp is [cv-qual] void
623 _Reference
624 operator*() const // never throws
626 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
627 return *_M_ptr;
630 _Tp*
631 operator->() const // never throws
633 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
634 return _M_ptr;
637 _Tp*
638 get() const // never throws
639 { return _M_ptr; }
641 // implicit conversion to "bool"
642 private:
643 typedef _Tp* shared_ptr::*__unspecified_bool_type;
645 public:
646 operator __unspecified_bool_type() const // never throws
647 { return _M_ptr == 0 ? 0 : &shared_ptr::_M_ptr; }
649 bool
650 unique() const // never throws
651 { return _M_refcount.unique(); }
653 long
654 use_count() const // never throws
655 { return _M_refcount.use_count(); }
657 void
658 swap(shared_ptr<_Tp>& __other) // never throws
660 std::swap(_M_ptr, __other._M_ptr);
661 _M_refcount.swap(__other._M_refcount);
664 private:
665 template <typename _Tp1>
666 bool
667 _M_less(const shared_ptr<_Tp1>& __rhs) const
668 { return _M_refcount < __rhs._M_refcount; }
670 void*
671 _M_get_deleter(const std::type_info& __ti) const
672 { return _M_refcount.get_deleter(__ti); }
674 template <typename _Tp1> friend class shared_ptr;
675 template <typename _Tp1> friend class weak_ptr;
677 // friends injected into enclosing namespace and found by ADL:
679 // get_deleter (experimental)
680 template <typename _Del>
681 friend inline _Del*
682 get_deleter(const shared_ptr& __p)
683 { return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del))); }
685 template <typename _Tp1>
686 friend inline bool
687 operator==(const shared_ptr& __a, const shared_ptr<_Tp1>& __b)
688 { return __a.get() == __b.get(); }
690 template <typename _Tp1>
691 friend inline bool
692 operator!=(const shared_ptr& __a, const shared_ptr<_Tp1>& __b)
693 { return __a.get() != __b.get(); }
695 template <typename _Tp1>
696 friend inline bool
697 operator<(const shared_ptr& __a, const shared_ptr<_Tp1>& __b)
698 { return __a._M_less(__b); }
700 _Tp* _M_ptr; // contained pointer
701 shared_count _M_refcount; // reference counter
702 }; // shared_ptr
704 // 2.2.3.9 shared_ptr casts
706 /** @warning The seemingly equivalent
707 * <code>shared_ptr<T>(static_cast<T*>(r.get()))</code>
708 * will eventually result in undefined behaviour,
709 * attempting to delete the same object twice.
711 template <typename _Tp, typename _Tp1>
712 shared_ptr<_Tp>
713 static_pointer_cast(const shared_ptr<_Tp1>& __r)
715 return shared_ptr<_Tp>(__r, __static_cast_tag());
718 /** @warning The seemingly equivalent
719 * <code>shared_ptr<T>(const_cast<T*>(r.get()))</code>
720 * will eventually result in undefined behaviour,
721 * attempting to delete the same object twice.
723 template <typename _Tp, typename _Tp1>
724 shared_ptr<_Tp>
725 const_pointer_cast(const shared_ptr<_Tp1>& __r)
727 return shared_ptr<_Tp>(__r, __const_cast_tag());
730 /** @warning The seemingly equivalent
731 * <code>shared_ptr<T>(dynamic_cast<T*>(r.get()))</code>
732 * will eventually result in undefined behaviour,
733 * attempting to delete the same object twice.
735 template <typename _Tp, typename _Tp1>
736 shared_ptr<_Tp>
737 dynamic_pointer_cast(const shared_ptr<_Tp1>& __r)
739 return shared_ptr<_Tp>(__r, __dynamic_cast_tag());
742 // operator<<
743 template <typename _Ch, typename _Tr, typename _Tp>
744 std::basic_ostream<_Ch,_Tr>&
745 operator<<(std::basic_ostream<_Ch,_Tr>& __os, const shared_ptr<_Tp>& __p)
747 __os << __p.get();
748 return __os;
752 template <typename _Tp>
753 class weak_ptr
755 public:
757 typedef _Tp element_type;
759 weak_ptr()
760 : _M_ptr(0), _M_refcount() // never throws
763 // generated copy constructor, assignment, destructor are fine
766 // The "obvious" converting constructor implementation:
768 // template<class Y>
769 // weak_ptr(weak_ptr<Y> const & r): _M_ptr(r._M_ptr), _M_refcount(r._M_refcount) // never throws
770 // {
771 // }
773 // has a serious problem.
775 // r._M_ptr may already have been invalidated. The _M_ptr(r._M_ptr)
776 // conversion may require access to *r._M_ptr (virtual inheritance).
778 // It is not possible to avoid spurious access violations since
779 // in multithreaded programs r._M_ptr may be invalidated at any point.
782 template <typename _Tp1>
783 weak_ptr(const weak_ptr<_Tp1>& r)
784 : _M_refcount(r._M_refcount) // never throws
786 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
787 _M_ptr = r.lock().get();
790 template <typename _Tp1>
791 weak_ptr(const shared_ptr<_Tp1>& r)
792 : _M_ptr(r._M_ptr), _M_refcount(r._M_refcount) // never throws
794 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
797 template <typename _Tp1>
798 weak_ptr&
799 operator=(const weak_ptr<_Tp1>& r) // never throws
801 _M_ptr = r.lock().get();
802 _M_refcount = r._M_refcount;
803 return *this;
806 template <typename _Tp1>
807 weak_ptr&
808 operator=(const shared_ptr<_Tp1>& r) // never throws
810 _M_ptr = r._M_ptr;
811 _M_refcount = r._M_refcount;
812 return *this;
815 shared_ptr<_Tp>
816 lock() const // never throws
818 #ifdef __GTHREADS
820 // optimization: avoid throw overhead
821 if (expired())
823 return shared_ptr<element_type>();
828 return shared_ptr<element_type>(*this);
830 catch (const bad_weak_ptr&)
832 // Q: how can we get here?
833 // A: another thread may have invalidated r after the use_count test above.
834 return shared_ptr<element_type>();
837 #else
839 // optimization: avoid try/catch overhead when single threaded
840 return expired() ? shared_ptr<element_type>() : shared_ptr<element_type>(*this);
842 #endif
843 } // XXX MT
846 long
847 use_count() const // never throws
848 { return _M_refcount.use_count(); }
850 bool
851 expired() const // never throws
852 { return _M_refcount.use_count() == 0; }
854 void
855 reset() // never throws
856 { weak_ptr().swap(*this); }
858 void
859 swap(weak_ptr& __s) // never throws
861 std::swap(_M_ptr, __s._M_ptr);
862 _M_refcount.swap(__s._M_refcount);
865 private:
867 template <typename _Tp1>
868 bool
869 _M_less(const weak_ptr<_Tp1>& __rhs) const
870 { return _M_refcount < __rhs._M_refcount; }
872 // used by __enable_shared_from_this
873 void
874 _M_assign(_Tp* __ptr, const shared_count& __refcount)
876 _M_ptr = __ptr;
877 _M_refcount = __refcount;
880 // friend injected into namespace and found by ADL
882 template <typename _Tp1>
883 friend inline bool
884 operator<(const weak_ptr& __lhs, const weak_ptr<_Tp1>& __rhs)
885 { return __lhs._M_less(__rhs); }
887 template <typename _Tp1> friend class weak_ptr;
888 template <typename _Tp1> friend class shared_ptr;
889 friend class enable_shared_from_this<_Tp>;
891 _Tp* _M_ptr; // contained pointer
892 weak_count _M_refcount; // reference counter
894 }; // weak_ptr
898 template <typename _Tp>
899 class enable_shared_from_this
901 protected:
903 enable_shared_from_this()
906 enable_shared_from_this(const enable_shared_from_this&)
909 enable_shared_from_this&
910 operator=(const enable_shared_from_this&)
911 { return *this; }
913 ~enable_shared_from_this()
916 public:
918 shared_ptr<_Tp>
919 shared_from_this()
921 shared_ptr<_Tp> p(this->_M_weak_this);
922 return p;
925 shared_ptr<const _Tp>
926 shared_from_this() const
928 shared_ptr<const _Tp> p(this->_M_weak_this);
929 return p;
932 private:
933 template <typename _Tp1>
934 void
935 _M_weak_assign(_Tp1* __p, const shared_count& __n) const
936 { _M_weak_this._M_assign(__p, __n); }
938 template <typename _Tp1>
939 friend void
940 __enable_shared_from_this( const shared_count& __pn, const enable_shared_from_this* __pe, const _Tp1* __px)
942 if(__pe != 0)
943 __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
946 mutable weak_ptr<_Tp> _M_weak_this;
949 } // namespace tr1
952 * @brief std::swap() specialisation for shared_ptr.
953 * @relates shared_ptr.
955 template <typename _Tp>
956 inline void
957 swap(tr1::shared_ptr<_Tp>& __a, tr1::shared_ptr<_Tp>& __b)
959 __a.swap(__b);
963 * @brief std::swap() specialisation for weak_ptr.
964 * @relates weak_ptr.
966 template <typename _Tp>
967 void
968 swap(tr1::weak_ptr<_Tp>& __a, tr1::weak_ptr<_Tp>& __b)
970 __a.swap(__b);
973 } // namespace std
975 #endif