# shared_ptr.h

Go to the documentation of this file.

    00001 // shared_ptr and weak_ptr implementation -*- C++ -*-
    00002
    00003 // Copyright (C) 2007, 2008, 2009, 2010 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
    00017 // permissions described in the GCC Runtime Library Exception, version
    00018 // 3.1, as published by the Free Software Foundation.
    00019
    00020 // You should have received a copy of the GNU General Public License and
    00021 // a copy of the GCC Runtime Library Exception along with this program;
    00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
    00023 // <http://www.gnu.org/licenses/>.
    00024
    00025 // GCC Note: Based on files from version 1.32.0 of the Boost library.
    00026
    00027 //  shared_count.hpp
    00028 //  Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
    00029
    00030 //  shared_ptr.hpp
    00031 //  Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
    00032 //  Copyright (C) 2001, 2002, 2003 Peter Dimov
    00033
    00034 //  weak_ptr.hpp
    00035 //  Copyright (C) 2001, 2002, 2003 Peter Dimov
    00036
    00037 //  enable_shared_from_this.hpp
    00038 //  Copyright (C) 2002 Peter Dimov
    00039
    00040 // Distributed under the Boost Software License, Version 1.0. (See
    00041 // accompanying file LICENSE_1_0.txt or copy at
    00042 // http://www.boost.org/LICENSE_1_0.txt)
    00043
    00044 /** @file bits/shared_ptr.h
    00045  *  This is an internal header file, included by other library headers.
    00046  *  You should not attempt to use it directly.
    00047  */
    00048
    00049 #ifndef _SHARED_PTR_H
    00050 #define _SHARED_PTR_H 1
    00051
    00052 #include <bits/shared_ptr_base.h>
    00053
    00054 _GLIBCXX_BEGIN_NAMESPACE(std)
    00055
    00056   /**
    00057    * @addtogroup pointer_abstractions
    00058    * @{
    00059    */
    00060
    00061   /// 2.2.3.7 shared_ptr I/O
    00062   template<typename _Ch, typename _Tr, typename _Tp, _Lock_policy _Lp>
    00063     inline std::basic_ostream<_Ch, _Tr>&
    00064     operator<<(std::basic_ostream<_Ch, _Tr>& __os,
    00065            const __shared_ptr<_Tp, _Lp>& __p)
    00066     {
    00067       __os << __p.get();
    00068       return __os;
    00069     }
    00070
    00071   /// 2.2.3.10 shared_ptr get_deleter (experimental)
    00072   template<typename _Del, typename _Tp, _Lock_policy _Lp>
    00073     inline _Del*
    00074     get_deleter(const __shared_ptr<_Tp, _Lp>& __p)
    00075     {
    00076 #ifdef __GXX_RTTI
    00077       return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del)));
    00078 #else
    00079       return 0;
    00080 #endif
    00081     }
    00082
    00083
    00084   /**
    00085    *  @brief  A smart pointer with reference-counted copy semantics.
    00086    *
    00087    *  The object pointed to is deleted when the last shared_ptr pointing to
    00088    *  it is destroyed or reset.
    00089   */
    00090   template<typename _Tp>
    00091     class shared_ptr : public __shared_ptr<_Tp>
    00092     {
    00093     public:
    00094       /**
    00095        *  @brief  Construct an empty %shared_ptr.
    00096        *  @post   use_count()==0 && get()==0
    00097        */
    00098       shared_ptr() : __shared_ptr<_Tp>() { }
    00099
    00100       /**
    00101        *  @brief  Construct a %shared_ptr that owns the pointer @a __p.
    00102        *  @param  __p  A pointer that is convertible to element_type*.
    00103        *  @post   use_count() == 1 && get() == __p
    00104        *  @throw  std::bad_alloc, in which case @c delete @a __p is called.
    00105        */
    00106       template<typename _Tp1>
    00107     explicit shared_ptr(_Tp1* __p) : __shared_ptr<_Tp>(__p) { }
    00108
    00109       /**
    00110        *  @brief  Construct a %shared_ptr that owns the pointer @a __p
    00111        *          and the deleter @a __d.
    00112        *  @param  __p  A pointer.
    00113        *  @param  __d  A deleter.
    00114        *  @post   use_count() == 1 && get() == __p
    00115        *  @throw  std::bad_alloc, in which case @a __d(__p) is called.
    00116        *
    00117        *  Requirements: _Deleter's copy constructor and destructor must
    00118        *  not throw
    00119        *
    00120        *  __shared_ptr will release __p by calling __d(__p)
    00121        */
    00122       template<typename _Tp1, typename _Deleter>
    00123     shared_ptr(_Tp1* __p, _Deleter __d) : __shared_ptr<_Tp>(__p, __d) { }
    00124
    00125       /**
    00126        *  @brief  Construct a %shared_ptr that owns the pointer @a __p
    00127        *          and the deleter @a __d.
    00128        *  @param  __p  A pointer.
    00129        *  @param  __d  A deleter.
    00130        *  @param  __a  An allocator.
    00131        *  @post   use_count() == 1 && get() == __p
    00132        *  @throw  std::bad_alloc, in which case @a __d(__p) is called.
    00133        *
    00134        *  Requirements: _Deleter's copy constructor and destructor must
    00135        *  not throw _Alloc's copy constructor and destructor must not
    00136        *  throw.
    00137        *
    00138        *  __shared_ptr will release __p by calling __d(__p)
    00139        */
    00140       template<typename _Tp1, typename _Deleter, typename _Alloc>
    00141     shared_ptr(_Tp1* __p, _Deleter __d, const _Alloc& __a)
    00142     : __shared_ptr<_Tp>(__p, __d, __a) { }
    00143
    00144       // Aliasing constructor
    00145
    00146       /**
    00147        *  @brief  Constructs a %shared_ptr instance that stores @a __p
    00148        *          and shares ownership with @a __r.
    00149        *  @param  __r  A %shared_ptr.
    00150        *  @param  __p  A pointer that will remain valid while @a *__r is valid.
    00151        *  @post   get() == __p && use_count() == __r.use_count()
    00152        *
    00153        *  This can be used to construct a @c shared_ptr to a sub-object
    00154        *  of an object managed by an existing @c shared_ptr.
    00155        *
    00156        * @code
    00157        * shared_ptr< pair<int,int> > pii(new pair<int,int>());
    00158        * shared_ptr<int> pi(pii, &pii->first);
    00159        * assert(pii.use_count() == 2);
    00160        * @endcode
    00161        */
    00162       template<typename _Tp1>
    00163     shared_ptr(const shared_ptr<_Tp1>& __r, _Tp* __p)
    00164     : __shared_ptr<_Tp>(__r, __p) { }
    00165
    00166       /**
    00167        *  @brief  If @a __r is empty, constructs an empty %shared_ptr;
    00168        *          otherwise construct a %shared_ptr that shares ownership
    00169        *          with @a __r.
    00170        *  @param  __r  A %shared_ptr.
    00171        *  @post   get() == __r.get() && use_count() == __r.use_count()
    00172        */
    00173       template<typename _Tp1>
    00174     shared_ptr(const shared_ptr<_Tp1>& __r) : __shared_ptr<_Tp>(__r) { }
    00175
    00176       /**
    00177        *  @brief  Move-constructs a %shared_ptr instance from @a __r.
    00178        *  @param  __r  A %shared_ptr rvalue.
    00179        *  @post   *this contains the old value of @a __r, @a __r is empty.
    00180        */
    00181       shared_ptr(shared_ptr&& __r)
    00182       : __shared_ptr<_Tp>(std::move(__r)) { }
    00183
    00184       /**
    00185        *  @brief  Move-constructs a %shared_ptr instance from @a __r.
    00186        *  @param  __r  A %shared_ptr rvalue.
    00187        *  @post   *this contains the old value of @a __r, @a __r is empty.
    00188        */
    00189       template<typename _Tp1>
    00190     shared_ptr(shared_ptr<_Tp1>&& __r)
    00191     : __shared_ptr<_Tp>(std::move(__r)) { }
    00192
    00193       /**
    00194        *  @brief  Constructs a %shared_ptr that shares ownership with @a __r
    00195        *          and stores a copy of the pointer stored in @a __r.
    00196        *  @param  __r  A weak_ptr.
    00197        *  @post   use_count() == __r.use_count()
    00198        *  @throw  bad_weak_ptr when __r.expired(),
    00199        *          in which case the constructor has no effect.
    00200        */
    00201       template<typename _Tp1>
    00202     explicit shared_ptr(const weak_ptr<_Tp1>& __r)
    00203     : __shared_ptr<_Tp>(__r) { }
    00204
    00205 #if _GLIBCXX_DEPRECATED
    00206       template<typename _Tp1>
    00207     shared_ptr(std::auto_ptr<_Tp1>&& __r)
    00208     : __shared_ptr<_Tp>(std::move(__r)) { }
    00209 #endif
    00210
    00211       template<typename _Tp1, typename _Del>
    00212     shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r)
    00213     : __shared_ptr<_Tp>(std::move(__r)) { }
    00214
    00215       template<typename _Tp1>
    00216     shared_ptr&
    00217     operator=(const shared_ptr<_Tp1>& __r) // never throws
    00218     {
    00219       this->__shared_ptr<_Tp>::operator=(__r);
    00220       return *this;
    00221     }
    00222
    00223 #if _GLIBCXX_DEPRECATED
    00224       template<typename _Tp1>
    00225     shared_ptr&
    00226     operator=(std::auto_ptr<_Tp1>&& __r)
    00227     {
    00228       this->__shared_ptr<_Tp>::operator=(std::move(__r));
    00229       return *this;
    00230     }
    00231 #endif
    00232
    00233       shared_ptr&
    00234       operator=(shared_ptr&& __r)
    00235       {
    00236     this->__shared_ptr<_Tp>::operator=(std::move(__r));
    00237     return *this;
    00238       }
    00239
    00240       template<class _Tp1>
    00241     shared_ptr&
    00242     operator=(shared_ptr<_Tp1>&& __r)
    00243     {
    00244       this->__shared_ptr<_Tp>::operator=(std::move(__r));
    00245       return *this;
    00246     }
    00247
    00248       template<typename _Tp1, typename _Del>
    00249     shared_ptr&
    00250     operator=(std::unique_ptr<_Tp1, _Del>&& __r)
    00251     {
    00252       this->__shared_ptr<_Tp>::operator=(std::move(__r));
    00253       return *this;
    00254     }
    00255
    00256     private:
    00257       // This constructor is non-standard, it is used by allocate_shared.
    00258       template<typename _Alloc, typename... _Args>
    00259     shared_ptr(_Sp_make_shared_tag __tag, _Alloc __a, _Args&&... __args)
    00260     : __shared_ptr<_Tp>(__tag, __a, std::forward<_Args>(__args)...)
    00261     { }
    00262
    00263       template<typename _Tp1, typename _Alloc, typename... _Args>
    00264     friend shared_ptr<_Tp1>
    00265     allocate_shared(_Alloc __a, _Args&&... __args);
    00266     };
    00267
    00268   // 20.8.13.2.7 shared_ptr comparisons
    00269   template<typename _Tp1, typename _Tp2>
    00270     inline bool
    00271     operator==(const shared_ptr<_Tp1>& __a, const shared_ptr<_Tp2>& __b)
    00272     { return __a.get() == __b.get(); }
    00273
    00274   template<typename _Tp1, typename _Tp2>
    00275     inline bool
    00276     operator!=(const shared_ptr<_Tp1>& __a, const shared_ptr<_Tp2>& __b)
    00277     { return __a.get() != __b.get(); }
    00278
    00279   template<typename _Tp1, typename _Tp2>
    00280     inline bool
    00281     operator<(const shared_ptr<_Tp1>& __a, const shared_ptr<_Tp2>& __b)
    00282     { return __a.get() < __b.get(); }
    00283
    00284   template<typename _Tp>
    00285     struct less<shared_ptr<_Tp>> : public _Sp_less<shared_ptr<_Tp>>
    00286     { };
    00287
    00288   // 20.8.13.2.9 shared_ptr specialized algorithms.
    00289   template<typename _Tp>
    00290     inline void
    00291     swap(shared_ptr<_Tp>& __a, shared_ptr<_Tp>& __b)
    00292     { __a.swap(__b); }
    00293
    00294   // 20.8.13.2.10 shared_ptr casts.
    00295   template<typename _Tp, typename _Tp1>
    00296     inline shared_ptr<_Tp>
    00297     static_pointer_cast(const shared_ptr<_Tp1>& __r)
    00298     { return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get())); }
    00299
    00300   template<typename _Tp, typename _Tp1>
    00301     inline shared_ptr<_Tp>
    00302     const_pointer_cast(const shared_ptr<_Tp1>& __r)
    00303     { return shared_ptr<_Tp>(__r, const_cast<_Tp*>(__r.get())); }
    00304
    00305   template<typename _Tp, typename _Tp1>
    00306     inline shared_ptr<_Tp>
    00307     dynamic_pointer_cast(const shared_ptr<_Tp1>& __r)
    00308     {
    00309       if (_Tp* __p = dynamic_cast<_Tp*>(__r.get()))
    00310     return shared_ptr<_Tp>(__r, __p);
    00311       return shared_ptr<_Tp>();
    00312     }
    00313
    00314
    00315   /**
    00316    *  @brief  A smart pointer with weak semantics.
    00317    *
    00318    *  With forwarding constructors and assignment operators.
    00319    */
    00320   template<typename _Tp>
    00321     class weak_ptr : public __weak_ptr<_Tp>
    00322     {
    00323     public:
    00324       weak_ptr() : __weak_ptr<_Tp>() { }
    00325
    00326       template<typename _Tp1>
    00327     weak_ptr(const weak_ptr<_Tp1>& __r)
    00328     : __weak_ptr<_Tp>(__r) { }
    00329
    00330       template<typename _Tp1>
    00331     weak_ptr(const shared_ptr<_Tp1>& __r)
    00332     : __weak_ptr<_Tp>(__r) { }
    00333
    00334       template<typename _Tp1>
    00335     weak_ptr&
    00336     operator=(const weak_ptr<_Tp1>& __r) // never throws
    00337     {
    00338       this->__weak_ptr<_Tp>::operator=(__r);
    00339       return *this;
    00340     }
    00341
    00342       template<typename _Tp1>
    00343     weak_ptr&
    00344     operator=(const shared_ptr<_Tp1>& __r) // never throws
    00345     {
    00346       this->__weak_ptr<_Tp>::operator=(__r);
    00347       return *this;
    00348     }
    00349
    00350       shared_ptr<_Tp>
    00351       lock() const // never throws
    00352       {
    00353 #ifdef __GTHREADS
    00354     if (this->expired())
    00355       return shared_ptr<_Tp>();
    00356
    00357     __try
    00358       {
    00359         return shared_ptr<_Tp>(*this);
    00360       }
    00361     __catch(const bad_weak_ptr&)
    00362       {
    00363         return shared_ptr<_Tp>();
    00364       }
    00365 #else
    00366     return this->expired() ? shared_ptr<_Tp>() : shared_ptr<_Tp>(*this);
    00367 #endif
    00368       }
    00369     };
    00370
    00371   // 20.8.13.3.7 weak_ptr specialized algorithms.
    00372   template<typename _Tp>
    00373     inline void
    00374     swap(weak_ptr<_Tp>& __a, weak_ptr<_Tp>& __b)
    00375     { __a.swap(__b); }
    00376
    00377
    00378   /// Primary template owner_less
    00379   template<typename _Tp>
    00380     struct owner_less;
    00381
    00382   /// Partial specialization of owner_less for shared_ptr.
    00383   template<typename _Tp>
    00384     struct owner_less<shared_ptr<_Tp>>
    00385     : public _Sp_owner_less<shared_ptr<_Tp>, weak_ptr<_Tp>>
    00386     { };
    00387
    00388   /// Partial specialization of owner_less for weak_ptr.
    00389   template<typename _Tp>
    00390     struct owner_less<weak_ptr<_Tp>>
    00391     : public _Sp_owner_less<weak_ptr<_Tp>, shared_ptr<_Tp>>
    00392     { };
    00393
    00394   /**
    00395    *  @brief Base class allowing use of member function shared_from_this.
    00396    */
    00397   template<typename _Tp>
    00398     class enable_shared_from_this
    00399     {
    00400     protected:
    00401       enable_shared_from_this() { }
    00402
    00403       enable_shared_from_this(const enable_shared_from_this&) { }
    00404
    00405       enable_shared_from_this&
    00406       operator=(const enable_shared_from_this&)
    00407       { return *this; }
    00408
    00409       ~enable_shared_from_this() { }
    00410
    00411     public:
    00412       shared_ptr<_Tp>
    00413       shared_from_this()
    00414       { return shared_ptr<_Tp>(this->_M_weak_this); }
    00415
    00416       shared_ptr<const _Tp>
    00417       shared_from_this() const
    00418       { return shared_ptr<const _Tp>(this->_M_weak_this); }
    00419
    00420     private:
    00421       template<typename _Tp1>
    00422     void
    00423     _M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const
    00424     { _M_weak_this._M_assign(__p, __n); }
    00425
    00426       template<typename _Tp1>
    00427     friend void
    00428     __enable_shared_from_this_helper(const __shared_count<>& __pn,
    00429                      const enable_shared_from_this* __pe,
    00430                      const _Tp1* __px)
    00431     {
    00432       if (__pe != 0)
    00433         __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
    00434     }
    00435
    00436       mutable weak_ptr<_Tp>  _M_weak_this;
    00437     };
    00438
    00439   /**
    00440    *  @brief  Create an object that is owned by a shared_ptr.
    00441    *  @param  __a     An allocator.
    00442    *  @param  __args  Arguments for the @a _Tp object's constructor.
    00443    *  @return A shared_ptr that owns the newly created object.
    00444    *  @throw  An exception thrown from @a _Alloc::allocate or from the
    00445    *          constructor of @a _Tp.
    00446    *
    00447    *  A copy of @a __a will be used to allocate memory for the shared_ptr
    00448    *  and the new object.
    00449    */
    00450   template<typename _Tp, typename _Alloc, typename... _Args>
    00451     inline shared_ptr<_Tp>
    00452     allocate_shared(_Alloc __a, _Args&&... __args)
    00453     {
    00454       return shared_ptr<_Tp>(_Sp_make_shared_tag(), std::forward<_Alloc>(__a),
    00455                  std::forward<_Args>(__args)...);
    00456     }
    00457
    00458   /**
    00459    *  @brief  Create an object that is owned by a shared_ptr.
    00460    *  @param  __args  Arguments for the @a _Tp object's constructor.
    00461    *  @return A shared_ptr that owns the newly created object.
    00462    *  @throw  std::bad_alloc, or an exception thrown from the
    00463    *          constructor of @a _Tp.
    00464    */
    00465   template<typename _Tp, typename... _Args>
    00466     inline shared_ptr<_Tp>
    00467     make_shared(_Args&&... __args)
    00468     {
    00469       typedef typename std::remove_const<_Tp>::type _Tp_nc;
    00470       return allocate_shared<_Tp>(std::allocator<_Tp_nc>(),
    00471                   std::forward<_Args>(__args)...);
    00472     }
    00473
    00474   // @} group pointer_abstractions
    00475
    00476 _GLIBCXX_END_NAMESPACE
    00477
    00478 #endif // _SHARED_PTR_H