tr1/boost_sp_shared_count.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 #if defined(_GLIBCXX_INCLUDE_AS_CXX0X)
00055 # error TR1 header cannot be included from C++0x header
00056 #endif
00057
00058 namespace std
00059 {
00060 namespace tr1
00061 {
00062
00063 template<typename _Ptr, typename _Deleter, _Lock_policy _Lp>
00064 class _Sp_counted_base_impl
00065 : public _Sp_counted_base<_Lp>
00066 {
00067 public:
00068
00069
00070
00071
00072 _Sp_counted_base_impl(_Ptr __p, _Deleter __d)
00073 : _M_ptr(__p), _M_del(__d) { }
00074
00075 virtual void
00076 _M_dispose()
00077 { _M_del(_M_ptr); }
00078
00079 virtual void*
00080 _M_get_deleter(const std::type_info& __ti)
00081 { return __ti == typeid(_Deleter) ? &_M_del : 0; }
00082
00083 private:
00084 _Sp_counted_base_impl(const _Sp_counted_base_impl&);
00085 _Sp_counted_base_impl& operator=(const _Sp_counted_base_impl&);
00086
00087 _Ptr _M_ptr;
00088 _Deleter _M_del;
00089 };
00090
00091 template<_Lock_policy _Lp = __default_lock_policy>
00092 class __weak_count;
00093
00094 template<typename _Tp>
00095 struct _Sp_deleter
00096 {
00097 typedef void result_type;
00098 typedef _Tp* argument_type;
00099 void operator()(_Tp* __p) const { delete __p; }
00100 };
00101
00102 template<_Lock_policy _Lp = __default_lock_policy>
00103 class __shared_count
00104 {
00105 public:
00106 __shared_count()
00107 : _M_pi(0)
00108 { }
00109
00110 template<typename _Ptr>
00111 __shared_count(_Ptr __p) : _M_pi(0)
00112 {
00113 try
00114 {
00115 typedef typename std::tr1::remove_pointer<_Ptr>::type _Tp;
00116 _M_pi = new _Sp_counted_base_impl<_Ptr, _Sp_deleter<_Tp>, _Lp>(
00117 __p, _Sp_deleter<_Tp>());
00118 }
00119 catch(...)
00120 {
00121 delete __p;
00122 __throw_exception_again;
00123 }
00124 }
00125
00126 template<typename _Ptr, typename _Deleter>
00127 __shared_count(_Ptr __p, _Deleter __d) : _M_pi(0)
00128 {
00129 try
00130 {
00131 _M_pi = new _Sp_counted_base_impl<_Ptr, _Deleter, _Lp>(__p, __d);
00132 }
00133 catch(...)
00134 {
00135 __d(__p);
00136 __throw_exception_again;
00137 }
00138 }
00139
00140
00141 template<typename _Tp>
00142 explicit
00143 __shared_count(std::auto_ptr<_Tp>& __r)
00144 : _M_pi(new _Sp_counted_base_impl<_Tp*,
00145 _Sp_deleter<_Tp>, _Lp >(__r.get(), _Sp_deleter<_Tp>()))
00146 { __r.release(); }
00147
00148
00149 explicit
00150 __shared_count(const __weak_count<_Lp>& __r);
00151
00152 ~__shared_count()
00153 {
00154 if (_M_pi != 0)
00155 _M_pi->_M_release();
00156 }
00157
00158 __shared_count(const __shared_count& __r)
00159 : _M_pi(__r._M_pi)
00160 {
00161 if (_M_pi != 0)
00162 _M_pi->_M_add_ref_copy();
00163 }
00164
00165 __shared_count&
00166 operator=(const __shared_count& __r)
00167 {
00168 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
00169 if (__tmp != _M_pi)
00170 {
00171 if (__tmp != 0)
00172 __tmp->_M_add_ref_copy();
00173 if (_M_pi != 0)
00174 _M_pi->_M_release();
00175 _M_pi = __tmp;
00176 }
00177 return *this;
00178 }
00179
00180 void
00181 _M_swap(__shared_count& __r)
00182 {
00183 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
00184 __r._M_pi = _M_pi;
00185 _M_pi = __tmp;
00186 }
00187
00188 long
00189 _M_get_use_count() const
00190 { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
00191
00192 bool
00193 _M_unique() const
00194 { return this->_M_get_use_count() == 1; }
00195
00196 friend inline bool
00197 operator==(const __shared_count& __a, const __shared_count& __b)
00198 { return __a._M_pi == __b._M_pi; }
00199
00200 friend inline bool
00201 operator<(const __shared_count& __a, const __shared_count& __b)
00202 { return std::less<_Sp_counted_base<_Lp>*>()(__a._M_pi, __b._M_pi); }
00203
00204 void*
00205 _M_get_deleter(const std::type_info& __ti) const
00206 { return _M_pi ? _M_pi->_M_get_deleter(__ti) : 0; }
00207
00208 private:
00209 friend class __weak_count<_Lp>;
00210
00211 _Sp_counted_base<_Lp>* _M_pi;
00212 };
00213 }
00214 }