File indexing completed on 2025-01-30 09:59:44
0001 #ifndef BOOST_DETAIL_SHARED_PTR_NMT_132_HPP_INCLUDED
0002 #define BOOST_DETAIL_SHARED_PTR_NMT_132_HPP_INCLUDED
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #include <boost/assert.hpp>
0018 #include <boost/checked_delete.hpp>
0019 #include <boost/serialization/throw_exception.hpp>
0020 #include <boost/detail/atomic_count.hpp>
0021
0022 #ifndef BOOST_NO_AUTO_PTR
0023 # include <memory> // for std::auto_ptr
0024 #endif
0025
0026 #include <algorithm> // for std::swap
0027 #include <functional> // for std::less
0028 #include <new> // for std::bad_alloc
0029
0030 namespace boost
0031 {
0032
0033 template<class T> class shared_ptr
0034 {
0035 private:
0036
0037 typedef detail::atomic_count count_type;
0038
0039 public:
0040
0041 typedef T element_type;
0042 typedef T value_type;
0043
0044 explicit shared_ptr(T * p = 0): px(p)
0045 {
0046 #ifndef BOOST_NO_EXCEPTIONS
0047
0048 try
0049 {
0050 pn = new count_type(1);
0051 }
0052 catch(...)
0053 {
0054 boost::checked_delete(p);
0055 throw;
0056 }
0057
0058 #else
0059
0060 pn = new count_type(1);
0061
0062 if(pn == 0)
0063 {
0064 boost::checked_delete(p);
0065 boost::serialization::throw_exception(std::bad_alloc());
0066 }
0067
0068 #endif
0069 }
0070
0071 ~shared_ptr()
0072 {
0073 if(--*pn == 0)
0074 {
0075 boost::checked_delete(px);
0076 delete pn;
0077 }
0078 }
0079
0080 shared_ptr(shared_ptr const & r): px(r.px)
0081 {
0082 pn = r.pn;
0083 ++*pn;
0084 }
0085
0086 shared_ptr & operator=(shared_ptr const & r)
0087 {
0088 shared_ptr(r).swap(*this);
0089 return *this;
0090 }
0091
0092 #ifndef BOOST_NO_AUTO_PTR
0093
0094 explicit shared_ptr(std::auto_ptr< T > & r)
0095 {
0096 pn = new count_type(1);
0097 px = r.release();
0098 }
0099
0100 shared_ptr & operator=(std::auto_ptr< T > & r)
0101 {
0102 shared_ptr(r).swap(*this);
0103 return *this;
0104 }
0105
0106 #endif
0107
0108 void reset(T * p = 0)
0109 {
0110 BOOST_ASSERT(p == 0 || p != px);
0111 shared_ptr(p).swap(*this);
0112 }
0113
0114 T & operator*() const
0115 {
0116 BOOST_ASSERT(px != 0);
0117 return *px;
0118 }
0119
0120 T * operator->() const
0121 {
0122 BOOST_ASSERT(px != 0);
0123 return px;
0124 }
0125
0126 T * get() const
0127 {
0128 return px;
0129 }
0130
0131 long use_count() const
0132 {
0133 return *pn;
0134 }
0135
0136 bool unique() const
0137 {
0138 return *pn == 1;
0139 }
0140
0141 void swap(shared_ptr< T > & other)
0142 {
0143 std::swap(px, other.px);
0144 std::swap(pn, other.pn);
0145 }
0146
0147 private:
0148
0149 T * px;
0150 count_type * pn;
0151 };
0152
0153 template<class T, class U> inline bool operator==(shared_ptr< T > const & a, shared_ptr<U> const & b)
0154 {
0155 return a.get() == b.get();
0156 }
0157
0158 template<class T, class U> inline bool operator!=(shared_ptr< T > const & a, shared_ptr<U> const & b)
0159 {
0160 return a.get() != b.get();
0161 }
0162
0163 template<class T> inline bool operator<(shared_ptr< T > const & a, shared_ptr< T > const & b)
0164 {
0165 return std::less<T*>()(a.get(), b.get());
0166 }
0167
0168 template<class T> void swap(shared_ptr< T > & a, shared_ptr< T > & b)
0169 {
0170 a.swap(b);
0171 }
0172
0173
0174
0175 template<class T> inline T * get_pointer(shared_ptr< T > const & p)
0176 {
0177 return p.get();
0178 }
0179
0180 }
0181
0182 #endif