shared_ptr_nmt.hpp
Go to the documentation of this file.
00001 #ifndef BOOST_SMART_PTR_DETAIL_SHARED_PTR_NMT_HPP_INCLUDED
00002 #define BOOST_SMART_PTR_DETAIL_SHARED_PTR_NMT_HPP_INCLUDED
00003 
00004 //
00005 //  detail/shared_ptr_nmt.hpp - shared_ptr.hpp without member templates
00006 //
00007 //  (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
00008 //  Copyright (c) 2001, 2002 Peter Dimov
00009 //
00010 //  Distributed under the Boost Software License, Version 1.0. (See
00011 //  accompanying file LICENSE_1_0.txt or copy at
00012 //  http://www.boost.org/LICENSE_1_0.txt)
00013 //
00014 //  See http://www.boost.org/libs/smart_ptr/shared_ptr.htm for documentation.
00015 //
00016 
00017 #include <boost/assert.hpp>
00018 #include <boost/checked_delete.hpp>
00019 #include <boost/throw_exception.hpp>
00020 #include <boost/smart_ptr/detail/atomic_count.hpp>
00021 
00022 #ifndef BOOST_NO_AUTO_PTR
00023 # include <memory>          // for std::auto_ptr
00024 #endif
00025 
00026 #include <algorithm>        // for std::swap
00027 #include <functional>       // for std::less
00028 #include <new>              // for std::bad_alloc
00029 
00030 namespace boost
00031 {
00032 
00033 template<class T> class shared_ptr
00034 {
00035 private:
00036 
00037     typedef detail::atomic_count count_type;
00038 
00039 public:
00040 
00041     typedef T element_type;
00042     typedef T value_type;
00043 
00044     explicit shared_ptr(T * p = 0): px(p)
00045     {
00046 #ifndef BOOST_NO_EXCEPTIONS
00047 
00048         try  // prevent leak if new throws
00049         {
00050             pn = new count_type(1);
00051         }
00052         catch(...)
00053         {
00054             boost::checked_delete(p);
00055             throw;
00056         }
00057 
00058 #else
00059 
00060         pn = new count_type(1);
00061 
00062         if(pn == 0)
00063         {
00064             boost::checked_delete(p);
00065             boost::throw_exception(std::bad_alloc());
00066         }
00067 
00068 #endif
00069     }
00070 
00071     ~shared_ptr()
00072     {
00073         if(--*pn == 0)
00074         {
00075             boost::checked_delete(px);
00076             delete pn;
00077         }
00078     }
00079 
00080     shared_ptr(shared_ptr const & r): px(r.px)  // never throws
00081     {
00082         pn = r.pn;
00083         ++*pn;
00084     }
00085 
00086     shared_ptr & operator=(shared_ptr const & r)
00087     {
00088         shared_ptr(r).swap(*this);
00089         return *this;
00090     }
00091 
00092 #ifndef BOOST_NO_AUTO_PTR
00093 
00094     explicit shared_ptr(std::auto_ptr<T> & r)
00095     { 
00096         pn = new count_type(1); // may throw
00097         px = r.release(); // fix: moved here to stop leak if new throws
00098     } 
00099 
00100     shared_ptr & operator=(std::auto_ptr<T> & r)
00101     {
00102         shared_ptr(r).swap(*this);
00103         return *this;
00104     }
00105 
00106 #endif
00107 
00108     void reset(T * p = 0)
00109     {
00110         BOOST_ASSERT(p == 0 || p != px);
00111         shared_ptr(p).swap(*this);
00112     }
00113 
00114     T & operator*() const  // never throws
00115     {
00116         BOOST_ASSERT(px != 0);
00117         return *px;
00118     }
00119 
00120     T * operator->() const  // never throws
00121     {
00122         BOOST_ASSERT(px != 0);
00123         return px;
00124     }
00125 
00126     T * get() const  // never throws
00127     {
00128         return px;
00129     }
00130 
00131     long use_count() const  // never throws
00132     {
00133         return *pn;
00134     }
00135 
00136     bool unique() const  // never throws
00137     {
00138         return *pn == 1;
00139     }
00140     
00141     void swap(shared_ptr<T> & other)  // never throws
00142     {
00143         std::swap(px, other.px);
00144         std::swap(pn, other.pn);
00145     }
00146 
00147 private:
00148 
00149     T * px;            // contained pointer
00150     count_type * pn;   // ptr to reference counter
00151 };
00152 
00153 template<class T, class U> inline bool operator==(shared_ptr<T> const & a, shared_ptr<U> const & b)
00154 {
00155     return a.get() == b.get();
00156 }
00157 
00158 template<class T, class U> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<U> const & b)
00159 {
00160     return a.get() != b.get();
00161 }
00162 
00163 template<class T> inline bool operator<(shared_ptr<T> const & a, shared_ptr<T> const & b)
00164 {
00165     return std::less<T*>()(a.get(), b.get());
00166 }
00167 
00168 template<class T> void swap(shared_ptr<T> & a, shared_ptr<T> & b)
00169 {
00170     a.swap(b);
00171 }
00172 
00173 // get_pointer() enables boost::mem_fn to recognize shared_ptr
00174 
00175 template<class T> inline T * get_pointer(shared_ptr<T> const & p)
00176 {
00177     return p.get();
00178 }
00179 
00180 } // namespace boost
00181 
00182 #endif  // #ifndef BOOST_SMART_PTR_DETAIL_SHARED_PTR_NMT_HPP_INCLUDED


appl
Author(s): petercai
autogenerated on Tue Jan 7 2014 11:02:29