# bits/boost_sp_shared_count.h

Go to the documentation of this file.

    00001 // <bits/boost_sp_shared_count.h> -*- C++ -*-
    00002
    00003 // Copyright (C) 2007 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
    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 bits/boost_sp_shared_count.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 __GXX_EXPERIMENTAL_CXX0X__
    00055 # include <c++0x_warning.h>
    00056 #endif
    00057
    00058 #if defined(_GLIBCXX_INCLUDE_AS_TR1)
    00059 #  error C++0x header cannot be included from TR1 header
    00060 #endif
    00061
    00062 namespace std
    00063 {
    00064   // counted ptr with no deleter or allocator support
    00065   template<typename _Ptr, _Lock_policy _Lp>
    00066     class _Sp_counted_ptr
    00067     : public _Sp_counted_base<_Lp>
    00068     {
    00069     public:
    00070       _Sp_counted_ptr(_Ptr __p)
    00071       : _M_ptr(__p) { }
    00072
    00073       virtual void
    00074       _M_dispose() // nothrow
    00075       { delete _M_ptr; }
    00076
    00077       virtual void
    00078       _M_destroy() // nothrow
    00079       { delete this; }
    00080
    00081       virtual void*
    00082       _M_get_deleter(const std::type_info& __ti)
    00083       { return 0; }
    00084
    00085     private:
    00086       _Sp_counted_ptr(const _Sp_counted_ptr&);
    00087       _Sp_counted_ptr& operator=(const _Sp_counted_ptr&);
    00088
    00089     protected:
    00090       _Ptr             _M_ptr;  // copy constructor must not throw
    00091     };
    00092
    00093   // support for custom deleter and/or allocator
    00094   template<typename _Ptr, typename _Deleter, typename _Alloc, _Lock_policy _Lp>
    00095     class _Sp_counted_deleter
    00096     : public _Sp_counted_ptr<_Ptr, _Lp>
    00097     {
    00098       typedef typename _Alloc::template
    00099           rebind<_Sp_counted_deleter>::other _My_alloc_type;
    00100
    00101       // Helper class that stores the Deleter and also acts as an allocator.
    00102       // Used to dispose of the owned pointer and the internal refcount
    00103       // Requires that copies of _Alloc can free each other's memory.
    00104       struct _My_Deleter
    00105       : public _My_alloc_type    // copy constructor must not throw
    00106       {
    00107         _Deleter _M_del;         // copy constructor must not throw
    00108         _My_Deleter(_Deleter __d, const _Alloc& __a)
    00109           : _My_alloc_type(__a), _M_del(__d) { }
    00110       };
    00111
    00112     protected:
    00113       typedef _Sp_counted_ptr<_Ptr, _Lp> _Base_type;
    00114
    00115     public:
    00116       /**
    00117        *  @brief
    00118        *  @pre     __d(__p) must not throw.
    00119        */
    00120       _Sp_counted_deleter(_Ptr __p, _Deleter __d)
    00121       : _Base_type(__p), _M_del(__d, _Alloc()) { }
    00122
    00123       /**
    00124        *  @brief
    00125        *  @pre     __d(__p) must not throw.
    00126        */
    00127       _Sp_counted_deleter(_Ptr __p, _Deleter __d, const _Alloc& __a)
    00128       : _Base_type(__p), _M_del(__d, __a) { }
    00129
    00130       virtual void
    00131       _M_dispose() // nothrow
    00132       { _M_del._M_del(_Base_type::_M_ptr); }
    00133
    00134       virtual void
    00135       _M_destroy() // nothrow
    00136       {
    00137         _My_alloc_type __a(_M_del);
    00138         this->~_Sp_counted_deleter();
    00139         __a.deallocate(this, 1);
    00140       }
    00141
    00142       virtual void*
    00143       _M_get_deleter(const std::type_info& __ti)
    00144       { return __ti == typeid(_Deleter) ? &_M_del._M_del : 0; }
    00145
    00146     private:
    00147       _Sp_counted_deleter(const _Sp_counted_deleter&);
    00148       _Sp_counted_deleter& operator=(const _Sp_counted_deleter&);
    00149
    00150     protected:
    00151       _My_Deleter      _M_del;  // copy constructor must not throw
    00152     };
    00153
    00154   // helpers for make_shared / allocate_shared
    00155
    00156   template<typename _Tp>
    00157     struct _Sp_destroy_inplace
    00158     {
    00159       void operator()(_Tp* __p) const { if (__p) __p->~_Tp(); }
    00160     };
    00161
    00162   struct _Sp_make_shared_tag { };
    00163
    00164   template<typename _Tp, typename _Alloc, _Lock_policy _Lp>
    00165     class _Sp_counted_ptr_inplace
    00166     : public _Sp_counted_deleter<_Tp*, _Sp_destroy_inplace<_Tp>, _Alloc, _Lp>
    00167     {
    00168       typedef _Sp_counted_deleter<_Tp*, _Sp_destroy_inplace<_Tp>, _Alloc, _Lp>
    00169         _Base_type;
    00170
    00171     public:
    00172       _Sp_counted_ptr_inplace(_Alloc __a)
    00173       : _Base_type(static_cast<_Tp*>(0), _Sp_destroy_inplace<_Tp>(), __a)
    00174       , _M_storage()
    00175       {
    00176         void* __p = &_M_storage;
    00177         ::new (__p) _Tp();  // might throw
    00178         _Base_type::_Base_type::_M_ptr = static_cast<_Tp*>(__p);
    00179       }
    00180
    00181       template<typename... _Args>
    00182         _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
    00183         : _Base_type(static_cast<_Tp*>(0), _Sp_destroy_inplace<_Tp>(), __a)
    00184         , _M_storage()
    00185         {
    00186           void* __p = &_M_storage;
    00187           ::new (__p) _Tp(std::forward<_Args>(__args)...);  // might throw
    00188           _Base_type::_Base_type::_M_ptr = static_cast<_Tp*>(__p);
    00189         }
    00190
    00191       // override because the allocator needs to know the dynamic type
    00192       virtual void
    00193       _M_destroy() // nothrow
    00194       {
    00195         typedef typename _Alloc::template
    00196             rebind<_Sp_counted_ptr_inplace>::other _My_alloc_type;
    00197         _My_alloc_type __a(_Base_type::_M_del);
    00198         this->~_Sp_counted_ptr_inplace();
    00199         __a.deallocate(this, 1);
    00200       }
    00201
    00202       // sneaky trick so __shared_ptr can get the managed pointer
    00203       virtual void*
    00204       _M_get_deleter(const std::type_info& __ti)
    00205       {
    00206         return __ti == typeid(_Sp_make_shared_tag)
    00207                ? static_cast<void*>(&_M_storage)
    00208                : _Base_type::_M_get_deleter(__ti);
    00209       }
    00210
    00211     private:
    00212       typename aligned_storage<sizeof(_Tp), alignment_of<_Tp>::value>::type
    00213         _M_storage;
    00214     };
    00215
    00216   template<_Lock_policy _Lp = __default_lock_policy>
    00217     class __weak_count;
    00218
    00219   template<_Lock_policy _Lp = __default_lock_policy>
    00220     class __shared_count
    00221     {
    00222     public:
    00223       __shared_count()
    00224       : _M_pi(0) // nothrow
    00225       { }
    00226
    00227       template<typename _Ptr>
    00228         __shared_count(_Ptr __p) : _M_pi(0)
    00229         {
    00230           try
    00231             {
    00232               _M_pi = new _Sp_counted_ptr<_Ptr, _Lp>(__p);
    00233             }
    00234           catch(...)
    00235             {
    00236               delete __p;
    00237               __throw_exception_again;
    00238             }
    00239         }
    00240
    00241       template<typename _Ptr, typename _Deleter>
    00242         __shared_count(_Ptr __p, _Deleter __d) : _M_pi(0)
    00243         {
    00244           // allocator's value_type doesn't matter, will rebind it anyway
    00245           typedef std::allocator<int> _Alloc;
    00246           typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
    00247           typedef std::allocator<_Sp_cd_type> _Alloc2;
    00248           _Alloc2 __a2;
    00249           try
    00250             {
    00251               _M_pi = __a2.allocate(1);
    00252               new(static_cast<void*>(_M_pi)) _Sp_cd_type(__p, __d);
    00253             }
    00254           catch(...)
    00255             {
    00256               __d(__p); // Call _Deleter on __p.
    00257               if (_M_pi)
    00258                 __a2.deallocate(static_cast<_Sp_cd_type*>(_M_pi), 1);
    00259               __throw_exception_again;
    00260             }
    00261         }
    00262
    00263       template<typename _Ptr, typename _Deleter, typename _Alloc>
    00264         __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
    00265         {
    00266           typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
    00267           typedef typename _Alloc::template rebind<_Sp_cd_type>::other _Alloc2;
    00268           _Alloc2 __a2(__a);
    00269           try
    00270             {
    00271               _M_pi = __a2.allocate(1);
    00272               new(static_cast<void*>(_M_pi)) _Sp_cd_type(__p, __d, __a);
    00273             }
    00274           catch(...)
    00275             {
    00276               __d(__p); // Call _Deleter on __p.
    00277               if (_M_pi)
    00278                 __a2.deallocate(static_cast<_Sp_cd_type*>(_M_pi), 1);
    00279               __throw_exception_again;
    00280             }
    00281         }
    00282
    00283       template<typename _Tp, typename _Alloc, typename... _Args>
    00284         __shared_count(_Sp_make_shared_tag, _Tp*, _Alloc __a, _Args&&... __args)
    00285         : _M_pi(0)
    00286         {
    00287           typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type;
    00288           typedef typename _Alloc::template rebind<_Sp_cp_type>::other _Alloc2;
    00289           _Alloc2 __a2(__a);
    00290           try
    00291             {
    00292               _M_pi = __a2.allocate(1);
    00293               new(static_cast<void*>(_M_pi)) _Sp_cp_type(__a,
    00294                   std::forward<_Args>(__args)...);
    00295             }
    00296           catch(...)
    00297             {
    00298               if (_M_pi)
    00299             __a2.deallocate(static_cast<_Sp_cp_type*>(_M_pi), 1);
    00300               __throw_exception_again;
    00301             }
    00302         }
    00303
    00304 #if _GLIBCXX_DEPRECATED
    00305       // Special case for auto_ptr<_Tp> to provide the strong guarantee.
    00306       template<typename _Tp>
    00307         explicit
    00308         __shared_count(std::auto_ptr<_Tp>& __r)
    00309         : _M_pi(new _Sp_counted_ptr<_Tp*, _Lp>(__r.get()))
    00310         { __r.release(); }
    00311 #endif
    00312
    00313       // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
    00314       explicit
    00315       __shared_count(const __weak_count<_Lp>& __r);
    00316
    00317       ~__shared_count() // nothrow
    00318       {
    00319         if (_M_pi != 0)
    00320           _M_pi->_M_release();
    00321       }
    00322
    00323       __shared_count(const __shared_count& __r)
    00324       : _M_pi(__r._M_pi) // nothrow
    00325       {
    00326         if (_M_pi != 0)
    00327           _M_pi->_M_add_ref_copy();
    00328       }
    00329
    00330       __shared_count&
    00331       operator=(const __shared_count& __r) // nothrow
    00332       {
    00333         _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
    00334         if (__tmp != _M_pi)
    00335           {
    00336             if (__tmp != 0)
    00337               __tmp->_M_add_ref_copy();
    00338             if (_M_pi != 0)
    00339               _M_pi->_M_release();
    00340             _M_pi = __tmp;
    00341           }
    00342         return *this;
    00343       }
    00344
    00345       void
    00346       _M_swap(__shared_count& __r) // nothrow
    00347       {
    00348         _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
    00349         __r._M_pi = _M_pi;
    00350         _M_pi = __tmp;
    00351       }
    00352
    00353       long
    00354       _M_get_use_count() const // nothrow
    00355       { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
    00356
    00357       bool
    00358       _M_unique() const // nothrow
    00359       { return this->_M_get_use_count() == 1; }
    00360
    00361       friend inline bool
    00362       operator==(const __shared_count& __a, const __shared_count& __b)
    00363       { return __a._M_pi == __b._M_pi; }
    00364
    00365       friend inline bool
    00366       operator<(const __shared_count& __a, const __shared_count& __b)
    00367       { return std::less<_Sp_counted_base<_Lp>*>()(__a._M_pi, __b._M_pi); }
    00368
    00369       void*
    00370       _M_get_deleter(const std::type_info& __ti) const
    00371       { return _M_pi ? _M_pi->_M_get_deleter(__ti) : 0; }
    00372
    00373     private:
    00374       friend class __weak_count<_Lp>;
    00375
    00376       _Sp_counted_base<_Lp>*  _M_pi;
    00377     };
    00378 }