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 #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
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()
00075 { delete _M_ptr; }
00076
00077 virtual void
00078 _M_destroy()
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;
00091 };
00092
00093
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
00102
00103
00104 struct _My_Deleter
00105 : public _My_alloc_type
00106 {
00107 _Deleter _M_del;
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
00118
00119
00120 _Sp_counted_deleter(_Ptr __p, _Deleter __d)
00121 : _Base_type(__p), _M_del(__d, _Alloc()) { }
00122
00123
00124
00125
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()
00132 { _M_del._M_del(_Base_type::_M_ptr); }
00133
00134 virtual void
00135 _M_destroy()
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;
00152 };
00153
00154
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();
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)...);
00188 _Base_type::_Base_type::_M_ptr = static_cast<_Tp*>(__p);
00189 }
00190
00191
00192 virtual void
00193 _M_destroy()
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
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)
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
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);
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);
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
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
00314 explicit
00315 __shared_count(const __weak_count<_Lp>& __r);
00316
00317 ~__shared_count()
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)
00325 {
00326 if (_M_pi != 0)
00327 _M_pi->_M_add_ref_copy();
00328 }
00329
00330 __shared_count&
00331 operator=(const __shared_count& __r)
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)
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
00355 { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
00356
00357 bool
00358 _M_unique() const
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 }