* zh_CN.po: Update.
[official-gcc.git] / libstdc++-v3 / include / tr1 / boost_shared_ptr.h
blobe47c570c8817d9efe893e52f9014681cd0f36bb1
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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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 _GLIBCXX_BEGIN_NAMESPACE(tr1)
62 class bad_weak_ptr : public std::exception
64 public:
66 virtual char const*
67 what() const throw()
68 { return "tr1::bad_weak_ptr"; }
71 // Helper for exception objects in <tr1/memory>
72 // TODO this should be defined in a different file.
73 inline void
74 __throw_bad_weak_ptr()
76 #if __EXCEPTIONS
77 throw bad_weak_ptr();
78 #else
79 std::abort();
80 #endif
84 template<typename _Tp>
85 struct _Sp_deleter
87 typedef void result_type;
88 typedef _Tp* argument_type;
90 void
91 operator()(_Tp* p) const
92 { delete p; }
96 class _Sp_counted_base
98 public:
100 _Sp_counted_base()
101 : _M_use_count(1), _M_weak_count(1)
103 // For the case of __GTHREAD_MUTEX_INIT we haven't initialised
104 // the mutex yet, so do it now.
105 #if defined(__GTHREADS) && defined(__GTHREAD_MUTEX_INIT)
106 __gthread_mutex_t __tmp = __GTHREAD_MUTEX_INIT;
107 _M_mutex = __tmp;
108 #endif
111 virtual
112 ~_Sp_counted_base() // nothrow
115 // dispose() is called when _M_use_count drops to zero, to release
116 // the resources managed by *this.
117 virtual void
118 dispose() = 0; // nothrow
120 // destroy() is called when _M_weak_count drops to zero.
121 virtual void
122 destroy() // nothrow
124 delete this;
127 virtual void*
128 get_deleter(const std::type_info&) = 0;
130 void
131 add_ref_copy()
133 __gnu_cxx::__atomic_add(&_M_use_count, 1);
136 void
137 add_ref_lock()
139 __gnu_cxx::lock lock(_M_mutex);
140 if (__gnu_cxx::__exchange_and_add(&_M_use_count, 1) == 0)
142 _M_use_count = 0;
143 __throw_bad_weak_ptr();
147 void
148 release() // nothrow
150 if (__gnu_cxx::__exchange_and_add(&_M_use_count, -1) == 1)
152 dispose();
153 #ifdef __GTHREADS
154 _GLIBCXX_READ_MEM_BARRIER;
155 _GLIBCXX_WRITE_MEM_BARRIER;
156 #endif
157 if (__gnu_cxx::__exchange_and_add(&_M_weak_count, -1) == 1)
158 destroy();
162 void
163 weak_add_ref() // nothrow
165 __gnu_cxx::__atomic_add(&_M_weak_count, 1);
168 void
169 weak_release() // nothrow
171 if (__gnu_cxx::__exchange_and_add(&_M_weak_count, -1) == 1)
173 #ifdef __GTHREADS
174 _GLIBCXX_READ_MEM_BARRIER;
175 _GLIBCXX_WRITE_MEM_BARRIER;
176 #endif
177 destroy();
181 long
182 use_count() const // nothrow
184 return _M_use_count; // XXX is this MT safe?
187 private:
189 _Sp_counted_base(_Sp_counted_base const&);
190 _Sp_counted_base& operator=(_Sp_counted_base const&);
192 _Atomic_word _M_use_count; // #shared
193 _Atomic_word _M_weak_count; // #weak + (#shared != 0)
194 __gnu_cxx::mutex_type _M_mutex;
197 template<typename _Ptr, typename _Deleter>
198 class _Sp_counted_base_impl
199 : public _Sp_counted_base
201 public:
204 * @brief
205 * @pre d(p) must not throw.
207 _Sp_counted_base_impl(_Ptr __p, _Deleter __d)
208 : _M_ptr(__p), _M_del(__d)
211 virtual void
212 dispose() // nothrow
214 _M_del(_M_ptr);
217 virtual void*
218 get_deleter(const std::type_info& __ti)
220 return __ti == typeid(_Deleter) ? &_M_del : 0;
223 private:
224 _Sp_counted_base_impl(const _Sp_counted_base_impl&);
225 _Sp_counted_base_impl& operator=(const _Sp_counted_base_impl&);
227 _Ptr _M_ptr; // copy constructor must not throw
228 _Deleter _M_del; // copy constructor must not throw
231 class weak_count;
233 class shared_count
235 private:
237 _Sp_counted_base* _M_pi;
239 friend class weak_count;
241 public:
243 shared_count()
244 : _M_pi(0) // nothrow
247 template<typename _Ptr, typename _Deleter>
248 shared_count(_Ptr __p, _Deleter __d)
249 : _M_pi(0)
253 _M_pi = new _Sp_counted_base_impl<_Ptr, _Deleter>(__p, __d);
255 catch(...)
257 __d(__p); // delete __p
258 __throw_exception_again;
262 // auto_ptr<_Tp> is special cased to provide the strong guarantee
264 template<typename _Tp>
265 explicit shared_count(std::auto_ptr<_Tp>& __r)
266 : _M_pi(new _Sp_counted_base_impl<_Tp*,
267 _Sp_deleter<_Tp> >(__r.get(), _Sp_deleter<_Tp>()))
268 { __r.release(); }
270 // throws bad_weak_ptr when __r.use_count() == 0
271 explicit shared_count(const weak_count& __r);
273 ~shared_count() // nothrow
275 if (_M_pi != 0)
276 _M_pi->release();
279 shared_count(const shared_count& __r)
280 : _M_pi(__r._M_pi) // nothrow
282 if (_M_pi != 0)
283 _M_pi->add_ref_copy();
286 shared_count&
287 operator=(const shared_count& __r) // nothrow
289 _Sp_counted_base* __tmp = __r._M_pi;
291 if(__tmp != _M_pi)
293 if(__tmp != 0)
294 __tmp->add_ref_copy();
295 if(_M_pi != 0)
296 _M_pi->release();
297 _M_pi = __tmp;
299 return *this;
302 void swap(shared_count& __r) // nothrow
304 _Sp_counted_base* __tmp = __r._M_pi;
305 __r._M_pi = _M_pi;
306 _M_pi = __tmp;
309 long
310 use_count() const // nothrow
311 { return _M_pi != 0 ? _M_pi->use_count() : 0; }
313 bool
314 unique() const // nothrow
315 { return this->use_count() == 1; }
317 friend inline bool
318 operator==(const shared_count& __a, const shared_count& __b)
319 { return __a._M_pi == __b._M_pi; }
321 friend inline bool
322 operator<(const shared_count& __a, const shared_count& __b)
323 { return std::less<_Sp_counted_base*>()(__a._M_pi, __b._M_pi); }
325 void*
326 get_deleter(const std::type_info& __ti) const
327 { return _M_pi ? _M_pi->get_deleter(__ti) : 0; }
330 class weak_count
332 private:
334 _Sp_counted_base* _M_pi;
336 friend class shared_count;
338 public:
340 weak_count()
341 : _M_pi(0) // nothrow
344 weak_count(const shared_count& __r)
345 : _M_pi(__r._M_pi) // nothrow
347 if (_M_pi != 0)
348 _M_pi->weak_add_ref();
351 weak_count(const weak_count& __r)
352 : _M_pi(__r._M_pi) // nothrow
354 if (_M_pi != 0)
355 _M_pi->weak_add_ref();
358 ~weak_count() // nothrow
360 if (_M_pi != 0)
361 _M_pi->weak_release();
364 weak_count&
365 operator=(const shared_count& __r) // nothrow
367 _Sp_counted_base* __tmp = __r._M_pi;
368 if (__tmp != 0)
369 __tmp->weak_add_ref();
370 if (_M_pi != 0)
371 _M_pi->weak_release();
372 _M_pi = __tmp;
374 return *this;
377 weak_count&
378 operator=(const weak_count& __r) // nothrow
380 _Sp_counted_base * __tmp = __r._M_pi;
381 if (__tmp != 0)
382 __tmp->weak_add_ref();
383 if (_M_pi != 0)
384 _M_pi->weak_release();
385 _M_pi = __tmp;
387 return *this;
390 void
391 swap(weak_count& __r) // nothrow
393 _Sp_counted_base * __tmp = __r._M_pi;
394 __r._M_pi = _M_pi;
395 _M_pi = __tmp;
398 long
399 use_count() const // nothrow
400 { return _M_pi != 0 ? _M_pi->use_count() : 0; }
402 friend inline bool
403 operator==(const weak_count& __a, const weak_count& __b)
404 { return __a._M_pi == __b._M_pi; }
406 friend inline bool
407 operator<(const weak_count& __a, const weak_count& __b)
408 { return std::less<_Sp_counted_base*>()(__a._M_pi, __b._M_pi); }
411 inline
412 shared_count::shared_count(const weak_count& __r)
413 : _M_pi(__r._M_pi)
415 if (_M_pi != 0)
416 _M_pi->add_ref_lock();
417 else
418 __throw_bad_weak_ptr();
422 // fwd decls
423 template<typename _Tp>
424 class shared_ptr;
426 template<typename _Tp>
427 class weak_ptr;
429 template<typename _Tp>
430 class enable_shared_from_this;
432 struct __static_cast_tag {};
433 struct __const_cast_tag {};
434 struct __dynamic_cast_tag {};
435 struct __polymorphic_cast_tag {};
437 template<class _Tp>
438 struct shared_ptr_traits
439 { typedef _Tp& reference; };
441 template<>
442 struct shared_ptr_traits<void>
443 { typedef void reference; };
445 template<>
446 struct shared_ptr_traits<void const>
447 { typedef void reference; };
449 template<>
450 struct shared_ptr_traits<void volatile>
451 { typedef void reference; };
453 template<>
454 struct shared_ptr_traits<void const volatile>
455 { typedef void reference; };
458 // enable_shared_from_this support
460 // friend of enable_shared_from_this
461 template<typename _Tp1, typename _Tp2>
462 void
463 __enable_shared_from_this(const shared_count& __pn,
464 const enable_shared_from_this<_Tp1>* __pe,
465 const _Tp2* __px );
467 inline void
468 __enable_shared_from_this(const shared_count&, ...)
472 // get_deleter must be declared before friend declaration by shared_ptr.
473 template<typename _Del, typename _Tp>
474 _Del* get_deleter(const shared_ptr<_Tp>&);
477 * @class shared_ptr <tr1/memory>
479 * A smart pointer with reference-counted copy semantics.
480 * The object pointed to is deleted when the last shared_ptr pointing to it
481 * is destroyed or reset.
483 template<typename _Tp>
484 class shared_ptr
486 typedef typename shared_ptr_traits<_Tp>::reference _Reference;
488 public:
490 typedef _Tp element_type;
492 /** @brief Construct an empty %shared_ptr.
493 * @post use_count()==0 && get()==0
495 shared_ptr()
496 : _M_ptr(0), _M_refcount() // never throws
499 /** @brief Construct a %shared_ptr that owns the pointer @a p.
500 * @param p A pointer that is convertible to element_type*.
501 * @post use_count() == 1 && get() == p
502 * @throw std::bad_alloc, in which case @c delete @a p is called.
504 template<typename _Tp1>
505 explicit shared_ptr(_Tp1* __p)
506 : _M_ptr(__p), _M_refcount(__p, _Sp_deleter<_Tp1>())
508 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
509 // __glibcxx_function_requires(_CompleteConcept<_Tp1*>)
511 __enable_shared_from_this( _M_refcount, __p, __p );
515 // Requirements: D's copy constructor and destructor must not throw
517 // shared_ptr will release p by calling d(p)
519 /** @brief Construct a %shared_ptr that owns the pointer @a p
520 * and the deleter @a d.
521 * @param p A pointer.
522 * @param d A deleter.
523 * @post use_count() == 1 && get() == p
524 * @throw std::bad_alloc, in which case @a d(p) is called.
526 template<typename _Tp1, typename _Deleter>
527 shared_ptr(_Tp1* __p, _Deleter __d)
528 : _M_ptr(__p), _M_refcount(__p, __d)
530 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
531 // TODO requires D is CopyConstructible and d(p) well-formed
533 __enable_shared_from_this( _M_refcount, __p, __p );
536 // generated copy constructor, assignment, destructor are fine.
538 /** @brief If @a r is empty, constructs an empty %shared_ptr; otherwise
539 * construct a %shared_ptr that shares ownership with @a r.
540 * @param r A %shared_ptr.
541 * @post get() == r.get() && use_count() == r.use_count()
542 * @throw std::bad_alloc, in which case
544 template<typename _Tp1>
545 shared_ptr(const shared_ptr<_Tp1>& __r)
546 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
548 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
551 /** @brief Constructs a %shared_ptr that shares ownership with @a r
552 * and stores a copy of the pointer stored in @a r.
553 * @param r A weak_ptr.
554 * @post use_count() == r.use_count()
555 * @throw bad_weak_ptr when r.expired(),
556 * in which case the constructor has no effect.
558 template<typename _Tp1>
559 explicit shared_ptr(const weak_ptr<_Tp1>& __r)
560 : _M_refcount(__r._M_refcount) // may throw
562 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
563 // it is now safe to copy r__._M_ptr, as _M_refcount(__r._M_refcount)
564 // did not throw
565 _M_ptr = __r._M_ptr;
569 * @post use_count() == 1 and r.get() == 0
571 template<typename _Tp1>
572 explicit shared_ptr(std::auto_ptr<_Tp1>& __r)
573 : _M_ptr(__r.get()), _M_refcount()
575 // TODO requires r.release() convertible to _Tp*, Tp1 is complete,
576 // delete r.release() well-formed
577 _Tp1 * __tmp = __r.get();
578 _M_refcount = shared_count(__r);
580 __enable_shared_from_this( _M_refcount, __tmp, __tmp );
583 template<typename _Tp1>
584 shared_ptr(const shared_ptr<_Tp1>& __r, __static_cast_tag)
585 : _M_ptr(static_cast<element_type*>(__r._M_ptr)),
586 _M_refcount(__r._M_refcount)
589 template<typename _Tp1>
590 shared_ptr(const shared_ptr<_Tp1>& __r, __const_cast_tag)
591 : _M_ptr(const_cast<element_type*>(__r._M_ptr)),
592 _M_refcount(__r._M_refcount)
595 template<typename _Tp1>
596 shared_ptr(const shared_ptr<_Tp1>& __r, __dynamic_cast_tag)
597 : _M_ptr(dynamic_cast<element_type*>(__r._M_ptr)),
598 _M_refcount(__r._M_refcount)
600 if (_M_ptr == 0) // need to allocate new counter -- the cast failed
601 _M_refcount = shared_count();
604 template<typename _Tp1>
605 shared_ptr&
606 operator=(const shared_ptr<_Tp1>& __r) // never throws
608 _M_ptr = __r._M_ptr;
609 _M_refcount = __r._M_refcount; // shared_count::op= doesn't throw
610 return *this;
613 template<typename _Tp1>
614 shared_ptr&
615 operator=(std::auto_ptr<_Tp1>& __r)
617 shared_ptr(__r).swap(*this);
618 return *this;
621 void
622 reset() // never throws
623 { shared_ptr().swap(*this); }
625 template<typename _Tp1>
626 void
627 reset(_Tp1* __p) // _Tp1 must be complete
629 _GLIBCXX_DEBUG_ASSERT(__p == 0 || __p != _M_ptr); // catch self-reset
630 // errors
631 shared_ptr(__p).swap(*this);
634 template<typename _Tp1, typename _Deleter>
635 void
636 reset(_Tp1 * __p, _Deleter __d)
637 { shared_ptr(__p, __d).swap(*this); }
639 // error to instantiate if _Tp is [cv-qual] void
640 _Reference
641 operator*() const // never throws
643 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
644 return *_M_ptr;
647 _Tp*
648 operator->() const // never throws
650 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
651 return _M_ptr;
654 _Tp*
655 get() const // never throws
656 { return _M_ptr; }
658 // implicit conversion to "bool"
659 private:
660 typedef _Tp* shared_ptr::*__unspecified_bool_type;
662 public:
663 operator __unspecified_bool_type() const // never throws
664 { return _M_ptr == 0 ? 0 : &shared_ptr::_M_ptr; }
666 bool
667 unique() const // never throws
668 { return _M_refcount.unique(); }
670 long
671 use_count() const // never throws
672 { return _M_refcount.use_count(); }
674 void
675 swap(shared_ptr<_Tp>& __other) // never throws
677 std::swap(_M_ptr, __other._M_ptr);
678 _M_refcount.swap(__other._M_refcount);
681 private:
682 void*
683 _M_get_deleter(const std::type_info& __ti) const
684 { return _M_refcount.get_deleter(__ti); }
686 template<typename _Tp1>
687 bool
688 _M_less(const shared_ptr<_Tp1>& __rhs) const
689 { return _M_refcount < __rhs._M_refcount; }
691 template<typename _Tp1> friend class shared_ptr;
692 template<typename _Tp1> friend class weak_ptr;
694 template<typename _Del, typename _Tp1>
695 friend _Del* get_deleter(const shared_ptr<_Tp1>&);
697 // friends injected into enclosing namespace and found by ADL:
698 template<typename _Tp1>
699 friend inline bool
700 operator==(const shared_ptr& __a, const shared_ptr<_Tp1>& __b)
701 { return __a.get() == __b.get(); }
703 template<typename _Tp1>
704 friend inline bool
705 operator!=(const shared_ptr& __a, const shared_ptr<_Tp1>& __b)
706 { return __a.get() != __b.get(); }
708 template<typename _Tp1>
709 friend inline bool
710 operator<(const shared_ptr& __a, const shared_ptr<_Tp1>& __b)
711 { return __a._M_less(__b); }
713 _Tp* _M_ptr; // contained pointer
714 shared_count _M_refcount; // reference counter
715 }; // shared_ptr
717 // 2.2.3.8 shared_ptr specialized algorithms.
718 template<typename _Tp>
719 inline void
720 swap(shared_ptr<_Tp>& __a, shared_ptr<_Tp>& __b)
721 { __a.swap(__b); }
723 // 2.2.3.9 shared_ptr casts
724 /** @warning The seemingly equivalent
725 * <code>shared_ptr<T>(static_cast<T*>(r.get()))</code>
726 * will eventually result in undefined behaviour,
727 * attempting to delete the same object twice.
729 template<typename _Tp, typename _Tp1>
730 shared_ptr<_Tp>
731 static_pointer_cast(const shared_ptr<_Tp1>& __r)
733 return shared_ptr<_Tp>(__r, __static_cast_tag());
736 /** @warning The seemingly equivalent
737 * <code>shared_ptr<T>(const_cast<T*>(r.get()))</code>
738 * will eventually result in undefined behaviour,
739 * attempting to delete the same object twice.
741 template<typename _Tp, typename _Tp1>
742 shared_ptr<_Tp>
743 const_pointer_cast(const shared_ptr<_Tp1>& __r)
745 return shared_ptr<_Tp>(__r, __const_cast_tag());
748 /** @warning The seemingly equivalent
749 * <code>shared_ptr<T>(dynamic_cast<T*>(r.get()))</code>
750 * will eventually result in undefined behaviour,
751 * attempting to delete the same object twice.
753 template<typename _Tp, typename _Tp1>
754 shared_ptr<_Tp>
755 dynamic_pointer_cast(const shared_ptr<_Tp1>& __r)
757 return shared_ptr<_Tp>(__r, __dynamic_cast_tag());
760 // 2.2.3.7 shared_ptr I/O
761 template<typename _Ch, typename _Tr, typename _Tp>
762 std::basic_ostream<_Ch, _Tr>&
763 operator<<(std::basic_ostream<_Ch, _Tr>& __os, const shared_ptr<_Tp>& __p)
765 __os << __p.get();
766 return __os;
769 // 2.2.3.10 shared_ptr get_deleter (experimental)
770 template<typename _Del, typename _Tp>
771 inline _Del*
772 get_deleter(const shared_ptr<_Tp>& __p)
773 { return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del))); }
776 template<typename _Tp>
777 class weak_ptr
779 public:
781 typedef _Tp element_type;
783 weak_ptr()
784 : _M_ptr(0), _M_refcount() // never throws
787 // generated copy constructor, assignment, destructor are fine
790 // The "obvious" converting constructor implementation:
792 // template<class Y>
793 // weak_ptr(weak_ptr<Y> const & r)
794 // : _M_ptr(r._M_ptr), _M_refcount(r._M_refcount) // never throws
795 // { }
797 // has a serious problem.
799 // r._M_ptr may already have been invalidated. The _M_ptr(r._M_ptr)
800 // conversion may require access to *r._M_ptr (virtual inheritance).
802 // It is not possible to avoid spurious access violations since
803 // in multithreaded programs r._M_ptr may be invalidated at any point.
806 template<typename _Tp1>
807 weak_ptr(const weak_ptr<_Tp1>& r)
808 : _M_refcount(r._M_refcount) // never throws
810 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
811 _M_ptr = r.lock().get();
814 template<typename _Tp1>
815 weak_ptr(const shared_ptr<_Tp1>& r)
816 : _M_ptr(r._M_ptr), _M_refcount(r._M_refcount) // never throws
818 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
821 template<typename _Tp1>
822 weak_ptr&
823 operator=(const weak_ptr<_Tp1>& r) // never throws
825 _M_ptr = r.lock().get();
826 _M_refcount = r._M_refcount;
827 return *this;
830 template<typename _Tp1>
831 weak_ptr&
832 operator=(const shared_ptr<_Tp1>& r) // never throws
834 _M_ptr = r._M_ptr;
835 _M_refcount = r._M_refcount;
836 return *this;
839 shared_ptr<_Tp>
840 lock() const // never throws
842 #ifdef __GTHREADS
844 // optimization: avoid throw overhead
845 if (expired())
846 return shared_ptr<element_type>();
850 return shared_ptr<element_type>(*this);
852 catch (const bad_weak_ptr&)
854 // Q: how can we get here?
855 // A: another thread may have invalidated r after the
856 // use_count test above.
857 return shared_ptr<element_type>();
860 #else
862 // optimization: avoid try/catch overhead when single threaded
863 return expired() ? shared_ptr<element_type>()
864 : shared_ptr<element_type>(*this);
866 #endif
867 } // XXX MT
869 long
870 use_count() const // never throws
871 { return _M_refcount.use_count(); }
873 bool
874 expired() const // never throws
875 { return _M_refcount.use_count() == 0; }
877 void
878 reset() // never throws
879 { weak_ptr().swap(*this); }
881 void
882 swap(weak_ptr& __s) // never throws
884 std::swap(_M_ptr, __s._M_ptr);
885 _M_refcount.swap(__s._M_refcount);
888 private:
890 template<typename _Tp1>
891 bool
892 _M_less(const weak_ptr<_Tp1>& __rhs) const
893 { return _M_refcount < __rhs._M_refcount; }
895 // used by __enable_shared_from_this
896 void
897 _M_assign(_Tp* __ptr, const shared_count& __refcount)
899 _M_ptr = __ptr;
900 _M_refcount = __refcount;
903 // friend injected into namespace and found by ADL
905 template<typename _Tp1>
906 friend inline bool
907 operator<(const weak_ptr& __lhs, const weak_ptr<_Tp1>& __rhs)
908 { return __lhs._M_less(__rhs); }
910 template<typename _Tp1> friend class weak_ptr;
911 template<typename _Tp1> friend class shared_ptr;
912 friend class enable_shared_from_this<_Tp>;
914 _Tp* _M_ptr; // contained pointer
915 weak_count _M_refcount; // reference counter
917 }; // weak_ptr
919 // 2.2.4.7 weak_ptr specialized algorithms.
920 template<typename _Tp>
921 void
922 swap(weak_ptr<_Tp>& __a, weak_ptr<_Tp>& __b)
923 { __a.swap(__b); }
926 template<typename _Tp>
927 class enable_shared_from_this
929 protected:
931 enable_shared_from_this()
934 enable_shared_from_this(const enable_shared_from_this&)
937 enable_shared_from_this&
938 operator=(const enable_shared_from_this&)
939 { return *this; }
941 ~enable_shared_from_this()
944 public:
946 shared_ptr<_Tp>
947 shared_from_this()
949 shared_ptr<_Tp> __p(this->_M_weak_this);
950 return __p;
953 shared_ptr<const _Tp>
954 shared_from_this() const
956 shared_ptr<const _Tp> __p(this->_M_weak_this);
957 return __p;
960 private:
961 template<typename _Tp1>
962 void
963 _M_weak_assign(_Tp1* __p, const shared_count& __n) const
964 { _M_weak_this._M_assign(__p, __n); }
966 template<typename _Tp1>
967 friend void
968 __enable_shared_from_this(const shared_count& __pn,
969 const enable_shared_from_this* __pe,
970 const _Tp1* __px)
972 if(__pe != 0)
973 __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
976 mutable weak_ptr<_Tp> _M_weak_this;
979 _GLIBCXX_END_NAMESPACE
980 } // namespace std
982 #endif