# boost_shared_ptr.h

    00001 // <tr1/boost_shared_ptr.h> -*- C++ -*-
    00002
    00003 // Copyright (C) 2005 Free Software Foundation, Inc.
    00004 //
    00005 // This file is part of the GNU ISO C++ Library.  This library is free
    00006 // software; you can redistribute it and/or modify it under the
    00007 // terms of the GNU General Public License as published by the
    00008 // Free Software Foundation; either version 2, or (at your option)
    00009 // any later version.
    00010
    00011 // This library is distributed in the hope that it will be useful,
    00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00014 // GNU General Public License for more details.
    00015
    00016 // You should have received a copy of the GNU General Public License along
    00017 // with this library; see the file COPYING.  If not, write to the Free
    00018 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
    00019 // USA.
    00020
    00021 // As a special exception, you may use this file as part of a free software
    00022 // library without restriction.  Specifically, if other files instantiate
    00023 // templates or use macros or inline functions from this file, or you compile
    00024 // this file and link it with other files to produce an executable, this
    00025 // file does not by itself cause the resulting executable to be covered by
    00026 // the GNU General Public License.  This exception does not however
    00027 // invalidate any other reasons why the executable file might be covered by
    00028 // the GNU General Public License.
    00029
    00030 //  shared_count.hpp
    00031 //  Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
    00032
    00033 //  shared_ptr.hpp
    00034 //  Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
    00035 //  Copyright (C) 2001, 2002, 2003 Peter Dimov
    00036
    00037 //  weak_ptr.hpp
    00038 //  Copyright (C) 2001, 2002, 2003 Peter Dimov
    00039
    00040 //  enable_shared_from_this.hpp
    00041 //  Copyright (C) 2002 Peter Dimov
    00042
    00043 // Distributed under the Boost Software License, Version 1.0. (See
    00044 // accompanying file LICENSE_1_0.txt or copy at
    00045 // http://www.boost.org/LICENSE_1_0.txt)
    00046
    00047 // GCC Note:  based on version 1.32.0 of the Boost library.
    00048
    00049 /** @file boost_memory.h
    00050  *  This is an internal header file, included by other library headers.
    00051  *  You should not attempt to use it directly.
    00052  */
    00053
    00054 #ifndef _BOOST_SHARED_PTR_H
    00055 #define _BOOST_SHARED_PTR_H 1
    00056
    00057 // namespace std::tr1
    00058 namespace std
    00059 {
    00060 namespace tr1
    00061 {
    00062
    00063 class bad_weak_ptr : public std::exception
    00064 {
    00065 public:
    00066
    00067   virtual char const* what() const throw()
    00068   {
    00069     return "tr1::bad_weak_ptr";
    00070   }
    00071 };
    00072
    00073 // Helper for exception objects in <tr1/memory>
    00074 // TODO this should be defined in a different file.
    00075 inline void
    00076 __throw_bad_weak_ptr()
    00077 {
    00078 #if __EXCEPTIONS
    00079   throw bad_weak_ptr();
    00080 #else
    00081   std::abort();
    00082 #endif
    00083 }
    00084
    00085
    00086 template <typename _Tp>
    00087   struct _Sp_deleter
    00088   {
    00089     typedef void result_type;
    00090     typedef _Tp* argument_type;
    00091
    00092     void
    00093     operator()(_Tp* p) const
    00094     { delete p; }
    00095   };
    00096
    00097
    00098 class _Sp_counted_base
    00099 {
    00100 public:
    00101
    00102   _Sp_counted_base()
    00103   : _M_use_count(1), _M_weak_count(1)
    00104   { }
    00105
    00106   virtual
    00107   ~_Sp_counted_base() // nothrow
    00108   { }
    00109
    00110   // dispose() is called when _M_use_count drops to zero, to release
    00111   // the resources managed by *this.
    00112   virtual void
    00113   dispose() = 0; // nothrow
    00114
    00115   // destroy() is called when _M_weak_count drops to zero.
    00116   virtual void
    00117   destroy() // nothrow
    00118   {
    00119     delete this;
    00120   }
    00121
    00122   virtual void*
    00123   get_deleter(const std::type_info&) = 0;
    00124
    00125   void
    00126   add_ref_copy()
    00127   {
    00128     __gnu_cxx::__atomic_add(&_M_use_count, 1);
    00129   }
    00130
    00131   void
    00132   add_ref_lock()
    00133   {
    00134     __gnu_cxx::lock lock(_M_mutex);
    00135     if (__gnu_cxx::__exchange_and_add(&_M_use_count, 1) == 0)
    00136     {
    00137       _M_use_count = 0;
    00138       __throw_bad_weak_ptr();
    00139     }
    00140   }
    00141
    00142   void
    00143   release() // nothrow
    00144   {
    00145     if (__gnu_cxx::__exchange_and_add(&_M_use_count, -1) == 1)
    00146     {
    00147       dispose();
    00148       __glibcxx_mutex_lock(_M_mutex);
    00149       __glibcxx_mutex_unlock(_M_mutex);
    00150       weak_release();
    00151     }
    00152   }
    00153
    00154   void
    00155   weak_add_ref() // nothrow
    00156   {
    00157     __gnu_cxx::__atomic_add(&_M_weak_count, 1);
    00158   }
    00159
    00160   void
    00161   weak_release() // nothrow
    00162   {
    00163     if (__gnu_cxx::__exchange_and_add(&_M_weak_count, -1) == 1)
    00164     {
    00165       __glibcxx_mutex_lock(_M_mutex);
    00166       __glibcxx_mutex_unlock(_M_mutex);
    00167       destroy();
    00168     }
    00169   }
    00170
    00171   long
    00172   use_count() const // nothrow
    00173   {
    00174     return _M_use_count;  // XXX is this MT safe?
    00175   }
    00176
    00177 private:
    00178
    00179   _Sp_counted_base(_Sp_counted_base const&);
    00180   _Sp_counted_base& operator= (_Sp_counted_base const&);
    00181
    00182   _Atomic_word _M_use_count;        // #shared
    00183   _Atomic_word _M_weak_count;       // #weak + (#shared != 0)
    00184   __gnu_cxx::mutex_type _M_mutex;
    00185 };
    00186
    00187 template <typename _Ptr, typename _Deleter>
    00188 class _Sp_counted_base_impl : public _Sp_counted_base
    00189 {
    00190 public:
    00191
    00192   /**
    00193    *  @brief
    00194    *  @pre     d(p) must not throw.
    00195    */
    00196   _Sp_counted_base_impl(_Ptr __p, _Deleter __d)
    00197   : _M_ptr(__p), _M_del(__d)
    00198   { }
    00199
    00200   virtual void
    00201   dispose() // nothrow
    00202   {
    00203     _M_del(_M_ptr);
    00204   }
    00205
    00206   virtual void*
    00207   get_deleter(const std::type_info& __ti)
    00208   {
    00209     return __ti == typeid(_Deleter) ? &_M_del : 0;
    00210   }
    00211
    00212 private:
    00213   _Sp_counted_base_impl(const _Sp_counted_base_impl&);
    00214   _Sp_counted_base_impl& operator=(const _Sp_counted_base_impl&);
    00215
    00216   _Ptr     _M_ptr; // copy constructor must not throw
    00217   _Deleter _M_del; // copy constructor must not throw
    00218 };
    00219
    00220 class weak_count;
    00221
    00222 class shared_count
    00223 {
    00224 private:
    00225
    00226   _Sp_counted_base* _M_pi;
    00227
    00228   friend class weak_count;
    00229
    00230 public:
    00231
    00232   shared_count()
    00233   : _M_pi(0) // nothrow
    00234   { }
    00235
    00236   template <typename _Ptr, typename _Deleter>
    00237     shared_count(_Ptr __p, _Deleter __d)
    00238     : _M_pi(0)
    00239     {
    00240       try
    00241       {
    00242         _M_pi = new _Sp_counted_base_impl<_Ptr, _Deleter>(__p, __d);
    00243       }
    00244       catch(...)
    00245       {
    00246         __d(__p); // delete __p
    00247         __throw_exception_again;
    00248       }
    00249     }
    00250
    00251   // auto_ptr<_Tp> is special cased to provide the strong guarantee
    00252
    00253   template <typename _Tp>
    00254     explicit shared_count(std::auto_ptr<_Tp>& __r)
    00255     : _M_pi(new _Sp_counted_base_impl<_Tp*,_Sp_deleter<_Tp> >(
    00256             __r.get(), _Sp_deleter<_Tp>()
    00257             ))
    00258     { __r.release(); }
    00259
    00260   // throws bad_weak_ptr when __r.use_count() == 0
    00261   explicit shared_count(const weak_count& __r);
    00262
    00263   ~shared_count() // nothrow
    00264   {
    00265     if (_M_pi != 0)
    00266       _M_pi->release();
    00267   }
    00268
    00269   shared_count(const shared_count& __r)
    00270   : _M_pi(__r._M_pi) // nothrow
    00271   {
    00272     if (_M_pi != 0)
    00273       _M_pi->add_ref_copy();
    00274   }
    00275
    00276   shared_count&
    00277   operator=(const shared_count& __r) // nothrow
    00278   {
    00279     _Sp_counted_base* __tmp = __r._M_pi;
    00280
    00281     if(__tmp != _M_pi)
    00282     {
    00283       if(__tmp != 0)
    00284         __tmp->add_ref_copy();
    00285       if(_M_pi != 0)
    00286         _M_pi->release();
    00287       _M_pi = __tmp;
    00288     }
    00289     return *this;
    00290   }
    00291
    00292   void swap(shared_count& __r) // nothrow
    00293   {
    00294     _Sp_counted_base* __tmp = __r._M_pi;
    00295     __r._M_pi = _M_pi;
    00296     _M_pi = __tmp;
    00297   }
    00298
    00299   long
    00300   use_count() const // nothrow
    00301   { return _M_pi != 0 ? _M_pi->use_count() : 0; }
    00302
    00303   bool
    00304   unique() const // nothrow
    00305   { return this->use_count() == 1; }
    00306
    00307   friend inline bool
    00308   operator==(const shared_count& __a, const shared_count& __b)
    00309   { return __a._M_pi == __b._M_pi; }
    00310
    00311   friend inline bool
    00312   operator<(const shared_count& __a, const shared_count& __b)
    00313   { return std::less<_Sp_counted_base*>()(__a._M_pi, __b._M_pi); }
    00314
    00315   void*
    00316   get_deleter(const std::type_info& __ti) const
    00317   { return _M_pi ? _M_pi->get_deleter(__ti) : 0; }
    00318 };
    00319
    00320
    00321 class weak_count
    00322 {
    00323 private:
    00324
    00325   _Sp_counted_base * _M_pi;
    00326
    00327   friend class shared_count;
    00328
    00329 public:
    00330
    00331   weak_count()
    00332   : _M_pi(0) // nothrow
    00333   { }
    00334
    00335   weak_count(const shared_count& __r)
    00336   : _M_pi(__r._M_pi) // nothrow
    00337   {
    00338     if (_M_pi != 0)
    00339       _M_pi->weak_add_ref();
    00340   }
    00341
    00342   weak_count(const weak_count& __r)
    00343   : _M_pi(__r._M_pi) // nothrow
    00344   {
    00345     if (_M_pi != 0)
    00346       _M_pi->weak_add_ref();
    00347   }
    00348
    00349   ~weak_count() // nothrow
    00350   {
    00351     if (_M_pi != 0)
    00352       _M_pi->weak_release();
    00353   }
    00354
    00355   weak_count&
    00356   operator=(const shared_count& __r) // nothrow
    00357   {
    00358     _Sp_counted_base* __tmp = __r._M_pi;
    00359     if (__tmp != 0)
    00360       __tmp->weak_add_ref();
    00361     if (_M_pi != 0)
    00362       _M_pi->weak_release();
    00363     _M_pi = __tmp;
    00364
    00365     return *this;
    00366   }
    00367
    00368   weak_count&
    00369   operator=(const weak_count& __r) // nothrow
    00370   {
    00371     _Sp_counted_base * __tmp = __r._M_pi;
    00372     if (__tmp != 0)
    00373       __tmp->weak_add_ref();
    00374     if (_M_pi != 0)
    00375       _M_pi->weak_release();
    00376     _M_pi = __tmp;
    00377
    00378     return *this;
    00379   }
    00380
    00381   void
    00382   swap(weak_count& __r) // nothrow
    00383   {
    00384     _Sp_counted_base * __tmp = __r._M_pi;
    00385     __r._M_pi = _M_pi;
    00386     _M_pi = __tmp;
    00387   }
    00388
    00389   long
    00390   use_count() const // nothrow
    00391   { return _M_pi != 0 ? _M_pi->use_count() : 0; }
    00392
    00393   friend inline bool
    00394   operator==(const weak_count& __a, const weak_count& __b)
    00395   { return __a._M_pi == __b._M_pi; }
    00396
    00397   friend inline bool
    00398   operator<(const weak_count& __a, const weak_count& __b)
    00399   { return std::less<_Sp_counted_base*>()(__a._M_pi, __b._M_pi); }
    00400 };
    00401
    00402 inline
    00403 shared_count::shared_count(const weak_count& __r)
    00404 : _M_pi(__r._M_pi)
    00405 {
    00406   if (_M_pi != 0)
    00407   {
    00408     _M_pi->add_ref_lock();
    00409   }
    00410   else
    00411   {
    00412     __throw_bad_weak_ptr();
    00413   }
    00414 }
    00415
    00416 // fwd decls
    00417 template <typename _Tp> class weak_ptr;
    00418 template <typename _Tp> class enable_shared_from_this;
    00419
    00420 struct __static_cast_tag {};
    00421 struct __const_cast_tag {};
    00422 struct __dynamic_cast_tag {};
    00423 struct __polymorphic_cast_tag {};
    00424
    00425 template<class _Tp> struct shared_ptr_traits
    00426 {
    00427     typedef _Tp & reference;
    00428 };
    00429
    00430 template<> struct shared_ptr_traits<void>
    00431 {
    00432     typedef void reference;
    00433 };
    00434
    00435 template<> struct shared_ptr_traits<void const>
    00436 {
    00437     typedef void reference;
    00438 };
    00439
    00440 template<> struct shared_ptr_traits<void volatile>
    00441 {
    00442     typedef void reference;
    00443 };
    00444
    00445 template<> struct shared_ptr_traits<void const volatile>
    00446 {
    00447     typedef void reference;
    00448 };
    00449
    00450
    00451 // enable_shared_from_this support
    00452
    00453 // friend of enable_shared_from_this
    00454 template <typename _Tp1, typename _Tp2>
    00455   void
    00456   __enable_shared_from_this( const shared_count& __pn,
    00457                              const enable_shared_from_this<_Tp1>* __pe,
    00458                              const _Tp2* __px );
    00459
    00460 inline void
    00461 __enable_shared_from_this(const shared_count&, ...)
    00462 { }
    00463
    00464 /**
    00465  *  @class shared_ptr <tr1/memory>
    00466  *
    00467  *  A smart pointer with reference-counted copy semantics.
    00468  *  The object pointed to is deleted when the last shared_ptr pointing to it
    00469  *  is destroyed or reset.
    00470  */
    00471
    00472 template <typename _Tp>
    00473   class shared_ptr
    00474   {
    00475     typedef typename shared_ptr_traits<_Tp>::reference _Reference;
    00476
    00477   public:
    00478
    00479     typedef _Tp   element_type;
    00480
    00481     /** @brief  Construct an empty %shared_ptr.
    00482      *  @post   use_count()==0 && get()==0
    00483      */
    00484     shared_ptr() : _M_ptr(0), _M_refcount() // never throws
    00485     { }
    00486
    00487     /** @brief  Construct a %shared_ptr that owns the pointer @a p.
    00488      *  @param  p  A pointer that is convertible to element_type*.
    00489      *  @post   use_count()==1 && get()==p
    00490      *  @throw  std::bad_alloc, in which case @c delete @a p is called.
    00491      */
    00492     template <typename _Tp1>
    00493       explicit shared_ptr(_Tp1* __p)
    00494       : _M_ptr(__p), _M_refcount(__p, _Sp_deleter<_Tp1>())
    00495       {
    00496         __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
    00497         // __glibcxx_function_requires(_CompleteConcept<_Tp1*>)
    00498
    00499         __enable_shared_from_this( _M_refcount, __p, __p );
    00500       }
    00501
    00502     //
    00503     // Requirements: D's copy constructor and destructor must not throw
    00504     //
    00505     // shared_ptr will release p by calling d(p)
    00506     //
    00507     /** @brief  Construct a %shared_ptr that owns the pointer @a p
    00508      *          and the deleter @a d.
    00509      *  @param  p  A pointer.
    00510      *  @param  d  A deleter.
    00511      *  @post   use_count()==1 && get()==p
    00512      *  @throw  std::bad_alloc, in which case @a d(p) is called.
    00513      */
    00514     template <typename _Tp1, typename _Deleter>
    00515       shared_ptr(_Tp1* __p, _Deleter __d)
    00516       : _M_ptr(__p), _M_refcount(__p, __d)
    00517       {
    00518         __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
    00519         // TODO requires D is CopyConstructible and d(p) well-formed
    00520
    00521         __enable_shared_from_this( _M_refcount, __p, __p );
    00522       }
    00523
    00524     //  generated copy constructor, assignment, destructor are fine.
    00525
    00526     /** @brief  If @a r is empty, constructs an empty %shared_ptr; otherwise
    00527      *          construct a %shared_ptr that shares ownership with @a r.
    00528      *  @param  r  A %shared_ptr.
    00529      *  @post   get()==r.get() && use_count()==r.use_count()
    00530      *  @throw  std::bad_alloc, in which case
    00531      */
    00532     template <typename _Tp1>
    00533       shared_ptr(const shared_ptr<_Tp1>& __r)
    00534       : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
    00535       {
    00536         __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
    00537       }
    00538
    00539     /** @brief  Constructs a %shared_ptr that shares ownership with @a r
    00540      *          and stores a copy of the pointer stored in @a r.
    00541      *  @param  r  A weak_ptr.
    00542      *  @post   use_count()==r.use_count()
    00543      *  @throw  bad_weak_ptr when r.expired(),
    00544      *          in which case the constructor has no effect.
    00545      */
    00546     template <typename _Tp1>
    00547       explicit shared_ptr(const weak_ptr<_Tp1>& __r)
    00548       : _M_refcount(__r._M_refcount) // may throw
    00549       {
    00550         __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
    00551         // it is now safe to copy r__._M_ptr, as _M_refcount(__r._M_refcount)
    00552         // did not throw
    00553         _M_ptr = __r._M_ptr;
    00554       }
    00555
    00556     /**
    00557      * @post use_count()==1 and r.get()==0
    00558      */
    00559     template <typename _Tp1>
    00560       explicit shared_ptr(std::auto_ptr<_Tp1>& __r)
    00561       : _M_ptr(__r.get()), _M_refcount()
    00562       {
    00563         // TODO requires r.release() convertible to _Tp*, Tp1 is complete,
    00564         // delete r.release() well-formed
    00565         _Tp1 * __tmp = __r.get();
    00566         _M_refcount = shared_count(__r);
    00567
    00568         __enable_shared_from_this( _M_refcount, __tmp, __tmp );
    00569       }
    00570
    00571     template <typename _Tp1>
    00572       shared_ptr(const shared_ptr<_Tp1>& __r, __static_cast_tag)
    00573       : _M_ptr(static_cast<element_type*>(__r._M_ptr))
    00574       , _M_refcount(__r._M_refcount)
    00575       { }
    00576
    00577     template <typename _Tp1>
    00578       shared_ptr(const shared_ptr<_Tp1>& __r, __const_cast_tag)
    00579       : _M_ptr(const_cast<element_type*>(__r._M_ptr))
    00580       , _M_refcount(__r._M_refcount)
    00581       { }
    00582
    00583     template <typename _Tp1>
    00584       shared_ptr(const shared_ptr<_Tp1>& __r, __dynamic_cast_tag)
    00585       : _M_ptr(dynamic_cast<element_type*>(__r._M_ptr))
    00586       , _M_refcount(__r._M_refcount)
    00587       {
    00588         if (_M_ptr == 0) // need to allocate new counter -- the cast failed
    00589         {
    00590           _M_refcount = shared_count();
    00591         }
    00592       }
    00593
    00594     template <typename _Tp1>
    00595       shared_ptr&
    00596       operator=(const shared_ptr<_Tp1>& __r) // never throws
    00597       {
    00598         _M_ptr = __r._M_ptr;
    00599         _M_refcount = __r._M_refcount; // shared_count::op= doesn't throw
    00600         return *this;
    00601       }
    00602
    00603     template <typename _Tp1>
    00604       shared_ptr&
    00605       operator=(std::auto_ptr<_Tp1>& __r)
    00606       {
    00607         shared_ptr(__r).swap(*this);
    00608         return *this;
    00609       }
    00610
    00611     void
    00612     reset() // never throws
    00613     { shared_ptr().swap(*this); }
    00614
    00615     template <typename _Tp1>
    00616       void
    00617       reset(_Tp1* __p) // _Tp1 must be complete
    00618       {
    00619         _GLIBCXX_DEBUG_ASSERT(__p == 0 || __p != _M_ptr); // catch self-reset errors
    00620         shared_ptr(__p).swap(*this);
    00621       }
    00622
    00623     template <typename _Tp1, typename _Deleter>
    00624       void
    00625       reset(_Tp1 * __p, _Deleter __d)
    00626       { shared_ptr(__p, __d).swap(*this); }
    00627
    00628     // error to instantiate if _Tp is [cv-qual] void
    00629     _Reference
    00630     operator*() const // never throws
    00631     {
    00632       _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
    00633       return *_M_ptr;
    00634     }
    00635
    00636     _Tp*
    00637     operator->() const // never throws
    00638     {
    00639       _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
    00640       return _M_ptr;
    00641     }
    00642
    00643     _Tp*
    00644     get() const // never throws
    00645     { return _M_ptr; }
    00646
    00647     // implicit conversion to "bool"
    00648   private:
    00649     typedef _Tp* shared_ptr::*__unspecified_bool_type;
    00650
    00651   public:
    00652     operator __unspecified_bool_type() const // never throws
    00653     { return _M_ptr == 0 ? 0 : &shared_ptr::_M_ptr; }
    00654
    00655     bool
    00656     unique() const // never throws
    00657     { return _M_refcount.unique(); }
    00658
    00659     long
    00660     use_count() const // never throws
    00661     { return _M_refcount.use_count(); }
    00662
    00663     void
    00664     swap(shared_ptr<_Tp>& __other) // never throws
    00665     {
    00666       std::swap(_M_ptr, __other._M_ptr);
    00667       _M_refcount.swap(__other._M_refcount);
    00668     }
    00669
    00670   private:
    00671     template <typename _Tp1>
    00672       bool
    00673       _M_less(const shared_ptr<_Tp1>& __rhs) const
    00674       { return _M_refcount < __rhs._M_refcount; }
    00675
    00676     void*
    00677     _M_get_deleter(const std::type_info& __ti) const
    00678     { return _M_refcount.get_deleter(__ti); }
    00679
    00680     template <typename _Tp1> friend class shared_ptr;
    00681     template <typename _Tp1> friend class weak_ptr;
    00682
    00683     // friends injected into enclosing namespace and found by ADL:
    00684
    00685     // get_deleter (experimental)
    00686     template <typename _Del>
    00687       friend inline _Del*
    00688       get_deleter(const shared_ptr& __p)
    00689       { return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del))); }
    00690
    00691     template <typename _Tp1>
    00692       friend inline bool
    00693       operator==(const shared_ptr& __a, const shared_ptr<_Tp1>& __b)
    00694       { return __a.get() == __b.get(); }
    00695
    00696     template <typename _Tp1>
    00697       friend inline bool
    00698       operator!=(const shared_ptr& __a, const shared_ptr<_Tp1>& __b)
    00699       { return __a.get() != __b.get(); }
    00700
    00701     template <typename _Tp1>
    00702       friend inline bool
    00703       operator<(const shared_ptr& __a, const shared_ptr<_Tp1>& __b)
    00704       { return __a._M_less(__b); }
    00705
    00706     _Tp*         _M_ptr;         // contained pointer
    00707     shared_count _M_refcount;    // reference counter
    00708   };  // shared_ptr
    00709
    00710 // 2.2.3.9 shared_ptr casts
    00711
    00712 /** @warning The seemingly equivalent
    00713  *           <code>shared_ptr<T>(static_cast<T*>(r.get()))</code>
    00714  *           will eventually result in undefined behaviour,
    00715  *           attempting to delete the same object twice.
    00716  */
    00717 template <typename _Tp, typename _Tp1>
    00718   shared_ptr<_Tp>
    00719   static_pointer_cast(const shared_ptr<_Tp1>& __r)
    00720   {
    00721     return shared_ptr<_Tp>(__r, __static_cast_tag());
    00722   }
    00723
    00724 /** @warning The seemingly equivalent
    00725  *           <code>shared_ptr<T>(const_cast<T*>(r.get()))</code>
    00726  *           will eventually result in undefined behaviour,
    00727  *           attempting to delete the same object twice.
    00728  */
    00729 template <typename _Tp, typename _Tp1>
    00730   shared_ptr<_Tp>
    00731   const_pointer_cast(const shared_ptr<_Tp1>& __r)
    00732   {
    00733     return shared_ptr<_Tp>(__r, __const_cast_tag());
    00734   }
    00735
    00736 /** @warning The seemingly equivalent
    00737  *           <code>shared_ptr<T>(dynamic_cast<T*>(r.get()))</code>
    00738  *           will eventually result in undefined behaviour,
    00739  *           attempting to delete the same object twice.
    00740  */
    00741 template <typename _Tp, typename _Tp1>
    00742   shared_ptr<_Tp>
    00743   dynamic_pointer_cast(const shared_ptr<_Tp1>& __r)
    00744   {
    00745     return shared_ptr<_Tp>(__r, __dynamic_cast_tag());
    00746   }
    00747
    00748 // operator<<
    00749 template <typename _Ch, typename _Tr, typename _Tp>
    00750   std::basic_ostream<_Ch,_Tr>&
    00751   operator<<(std::basic_ostream<_Ch,_Tr>& __os, const shared_ptr<_Tp>& __p)
    00752   {
    00753     __os << __p.get();
    00754     return __os;
    00755   }
    00756
    00757
    00758 template <typename _Tp>
    00759   class weak_ptr
    00760   {
    00761   public:
    00762
    00763     typedef _Tp element_type;
    00764
    00765     weak_ptr()
    00766     : _M_ptr(0), _M_refcount() // never throws
    00767     { }
    00768
    00769   //  generated copy constructor, assignment, destructor are fine
    00770
    00771   //
    00772   //  The "obvious" converting constructor implementation:
    00773   //
    00774   //  template<class Y>
    00775   //  weak_ptr(weak_ptr<Y> const & r): _M_ptr(r._M_ptr), _M_refcount(r._M_refcount) // never throws
    00776   //  {
    00777   //  }
    00778   //
    00779   //  has a serious problem.
    00780   //
    00781   //  r._M_ptr may already have been invalidated. The _M_ptr(r._M_ptr)
    00782   //  conversion may require access to *r._M_ptr (virtual inheritance).
    00783   //
    00784   //  It is not possible to avoid spurious access violations since
    00785   //  in multithreaded programs r._M_ptr may be invalidated at any point.
    00786   //
    00787
    00788     template <typename _Tp1>
    00789       weak_ptr(const weak_ptr<_Tp1>& r)
    00790       : _M_refcount(r._M_refcount) // never throws
    00791       {
    00792         __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
    00793         _M_ptr = r.lock().get();
    00794       }
    00795
    00796     template <typename _Tp1>
    00797       weak_ptr(const shared_ptr<_Tp1>& r)
    00798       : _M_ptr(r._M_ptr), _M_refcount(r._M_refcount) // never throws
    00799       {
    00800         __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
    00801       }
    00802
    00803     template <typename _Tp1>
    00804       weak_ptr&
    00805       operator=(const weak_ptr<_Tp1>& r) // never throws
    00806       {
    00807         _M_ptr = r.lock().get();
    00808         _M_refcount = r._M_refcount;
    00809         return *this;
    00810       }
    00811
    00812     template <typename _Tp1>
    00813       weak_ptr&
    00814       operator=(const shared_ptr<_Tp1>& r) // never throws
    00815       {
    00816         _M_ptr = r._M_ptr;
    00817         _M_refcount = r._M_refcount;
    00818         return *this;
    00819       }
    00820
    00821     shared_ptr<_Tp>
    00822     lock() const // never throws
    00823     {
    00824 #ifdef __GTHREADS
    00825
    00826       // optimization: avoid throw overhead
    00827       if (expired())
    00828       {
    00829         return shared_ptr<element_type>();
    00830       }
    00831
    00832       try
    00833       {
    00834         return shared_ptr<element_type>(*this);
    00835       }
    00836       catch (const bad_weak_ptr&)
    00837       {
    00838         // Q: how can we get here?
    00839         // A: another thread may have invalidated r after the use_count test above.
    00840         return shared_ptr<element_type>();
    00841       }
    00842
    00843 #else
    00844
    00845       // optimization: avoid try/catch overhead when single threaded
    00846       return expired() ? shared_ptr<element_type>() : shared_ptr<element_type>(*this);
    00847
    00848 #endif
    00849     } // XXX MT
    00850
    00851
    00852     long
    00853     use_count() const // never throws
    00854     { return _M_refcount.use_count(); }
    00855
    00856     bool
    00857     expired() const // never throws
    00858     { return _M_refcount.use_count() == 0; }
    00859
    00860     void
    00861     reset() // never throws
    00862     { weak_ptr().swap(*this); }
    00863
    00864     void
    00865     swap(weak_ptr& __s) // never throws
    00866     {
    00867       std::swap(_M_ptr, __s._M_ptr);
    00868       _M_refcount.swap(__s._M_refcount);
    00869     }
    00870
    00871   private:
    00872
    00873     template <typename _Tp1>
    00874       bool
    00875       _M_less(const weak_ptr<_Tp1>& __rhs) const
    00876       { return _M_refcount < __rhs._M_refcount; }
    00877
    00878     // used by __enable_shared_from_this
    00879     void
    00880     _M_assign(_Tp* __ptr, const shared_count& __refcount)
    00881     {
    00882       _M_ptr = __ptr;
    00883       _M_refcount = __refcount;
    00884     }
    00885
    00886     // friend injected into namespace and found by ADL
    00887
    00888     template <typename _Tp1>
    00889       friend inline bool
    00890       operator<(const weak_ptr& __lhs, const weak_ptr<_Tp1>& __rhs)
    00891       { return __lhs._M_less(__rhs); }
    00892
    00893     template <typename _Tp1> friend class weak_ptr;
    00894     template <typename _Tp1> friend class shared_ptr;
    00895     friend class enable_shared_from_this<_Tp>;
    00896
    00897     _Tp*       _M_ptr;           // contained pointer
    00898     weak_count _M_refcount;      // reference counter
    00899
    00900   };  // weak_ptr
    00901
    00902
    00903
    00904 template <typename _Tp>
    00905   class enable_shared_from_this
    00906   {
    00907   protected:
    00908
    00909     enable_shared_from_this()
    00910     { }
    00911
    00912     enable_shared_from_this(const enable_shared_from_this&)
    00913     { }
    00914
    00915     enable_shared_from_this&
    00916     operator=(const enable_shared_from_this&)
    00917     { return *this; }
    00918
    00919     ~enable_shared_from_this()
    00920     { }
    00921
    00922   public:
    00923
    00924     shared_ptr<_Tp>
    00925     shared_from_this()
    00926     {
    00927       shared_ptr<_Tp> p(this->_M_weak_this);
    00928       return p;
    00929     }
    00930
    00931     shared_ptr<const _Tp>
    00932     shared_from_this() const
    00933     {
    00934       shared_ptr<const _Tp> p(this->_M_weak_this);
    00935       return p;
    00936     }
    00937
    00938   private:
    00939     template <typename _Tp1>
    00940       void
    00941       _M_weak_assign(_Tp1* __p, const shared_count& __n) const
    00942       { _M_weak_this._M_assign(__p, __n); }
    00943
    00944     template <typename _Tp1>
    00945       friend void
    00946       __enable_shared_from_this( const shared_count& __pn, const enable_shared_from_this* __pe, const _Tp1* __px)
    00947       {
    00948         if(__pe != 0)
    00949           __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
    00950       }
    00951
    00952     mutable weak_ptr<_Tp> _M_weak_this;
    00953   };
    00954
    00955 } // namespace tr1
    00956
    00957 /**
    00958  *  @brief   std::swap() specialisation for shared_ptr.
    00959  *  @relates shared_ptr.
    00960  */
    00961 template <typename _Tp>
    00962   inline void
    00963   swap(tr1::shared_ptr<_Tp>& __a, tr1::shared_ptr<_Tp>& __b)
    00964   {
    00965     __a.swap(__b);
    00966   }
    00967
    00968 /**
    00969  *  @brief   std::swap() specialisation for weak_ptr.
    00970  *  @relates weak_ptr.
    00971  */
    00972 template <typename _Tp>
    00973   void
    00974   swap(tr1::weak_ptr<_Tp>& __a, tr1::weak_ptr<_Tp>& __b)
    00975   {
    00976     __a.swap(__b);
    00977   }
    00978
    00979 } // namespace std
    00980
    00981 #endif