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
00006
00007
00008
00009
00010
00011
00012
00013
00014
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>
00024 #endif
00025
00026 #include <algorithm>
00027 #include <functional>
00028 #include <new>
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
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)
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);
00097 px = r.release();
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
00115 {
00116 BOOST_ASSERT(px != 0);
00117 return *px;
00118 }
00119
00120 T * operator->() const
00121 {
00122 BOOST_ASSERT(px != 0);
00123 return px;
00124 }
00125
00126 T * get() const
00127 {
00128 return px;
00129 }
00130
00131 long use_count() const
00132 {
00133 return *pn;
00134 }
00135
00136 bool unique() const
00137 {
00138 return *pn == 1;
00139 }
00140
00141 void swap(shared_ptr<T> & other)
00142 {
00143 std::swap(px, other.px);
00144 std::swap(pn, other.pn);
00145 }
00146
00147 private:
00148
00149 T * px;
00150 count_type * pn;
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
00174
00175 template<class T> inline T * get_pointer(shared_ptr<T> const & p)
00176 {
00177 return p.get();
00178 }
00179
00180 }
00181
00182 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SHARED_PTR_NMT_HPP_INCLUDED