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 #ifndef _SHARED_PTR_H
00050 #define _SHARED_PTR_H 1
00051
00052 #include <bits/shared_ptr_base.h>
00053
00054 _GLIBCXX_BEGIN_NAMESPACE(std)
00055
00056
00057
00058
00059
00060
00061
00062 template<typename _Ch, typename _Tr, typename _Tp, _Lock_policy _Lp>
00063 inline std::basic_ostream<_Ch, _Tr>&
00064 operator<<(std::basic_ostream<_Ch, _Tr>& __os,
00065 const __shared_ptr<_Tp, _Lp>& __p)
00066 {
00067 __os << __p.get();
00068 return __os;
00069 }
00070
00071
00072 template<typename _Del, typename _Tp, _Lock_policy _Lp>
00073 inline _Del*
00074 get_deleter(const __shared_ptr<_Tp, _Lp>& __p)
00075 {
00076 #ifdef __GXX_RTTI
00077 return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del)));
00078 #else
00079 return 0;
00080 #endif
00081 }
00082
00083
00084
00085
00086
00087
00088
00089
00090 template<typename _Tp>
00091 class shared_ptr : public __shared_ptr<_Tp>
00092 {
00093 public:
00094
00095
00096
00097
00098 shared_ptr() : __shared_ptr<_Tp>() { }
00099
00100
00101
00102
00103
00104
00105
00106 template<typename _Tp1>
00107 explicit shared_ptr(_Tp1* __p) : __shared_ptr<_Tp>(__p) { }
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122 template<typename _Tp1, typename _Deleter>
00123 shared_ptr(_Tp1* __p, _Deleter __d) : __shared_ptr<_Tp>(__p, __d) { }
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 template<typename _Deleter>
00139 shared_ptr(nullptr_t __p, _Deleter __d)
00140 : __shared_ptr<_Tp>(__p, __d) { }
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157 template<typename _Tp1, typename _Deleter, typename _Alloc>
00158 shared_ptr(_Tp1* __p, _Deleter __d, const _Alloc& __a)
00159 : __shared_ptr<_Tp>(__p, __d, __a) { }
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176 template<typename _Deleter, typename _Alloc>
00177 shared_ptr(nullptr_t __p, _Deleter __d, const _Alloc& __a)
00178 : __shared_ptr<_Tp>(__p, __d, __a) { }
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198 template<typename _Tp1>
00199 shared_ptr(const shared_ptr<_Tp1>& __r, _Tp* __p)
00200 : __shared_ptr<_Tp>(__r, __p) { }
00201
00202
00203
00204
00205
00206
00207
00208
00209 template<typename _Tp1>
00210 shared_ptr(const shared_ptr<_Tp1>& __r) : __shared_ptr<_Tp>(__r) { }
00211
00212
00213
00214
00215
00216
00217 shared_ptr(shared_ptr&& __r)
00218 : __shared_ptr<_Tp>(std::move(__r)) { }
00219
00220
00221
00222
00223
00224
00225 template<typename _Tp1>
00226 shared_ptr(shared_ptr<_Tp1>&& __r)
00227 : __shared_ptr<_Tp>(std::move(__r)) { }
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237 template<typename _Tp1>
00238 explicit shared_ptr(const weak_ptr<_Tp1>& __r)
00239 : __shared_ptr<_Tp>(__r) { }
00240
00241 #if _GLIBCXX_DEPRECATED
00242 template<typename _Tp1>
00243 shared_ptr(std::auto_ptr<_Tp1>&& __r)
00244 : __shared_ptr<_Tp>(std::move(__r)) { }
00245 #endif
00246
00247 template<typename _Tp1, typename _Del>
00248 shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r)
00249 : __shared_ptr<_Tp>(std::move(__r)) { }
00250
00251
00252
00253
00254
00255
00256 shared_ptr(nullptr_t __p) : __shared_ptr<_Tp>(__p) { }
00257
00258 template<typename _Tp1>
00259 shared_ptr&
00260 operator=(const shared_ptr<_Tp1>& __r)
00261 {
00262 this->__shared_ptr<_Tp>::operator=(__r);
00263 return *this;
00264 }
00265
00266 #if _GLIBCXX_DEPRECATED
00267 template<typename _Tp1>
00268 shared_ptr&
00269 operator=(std::auto_ptr<_Tp1>&& __r)
00270 {
00271 this->__shared_ptr<_Tp>::operator=(std::move(__r));
00272 return *this;
00273 }
00274 #endif
00275
00276 shared_ptr&
00277 operator=(shared_ptr&& __r)
00278 {
00279 this->__shared_ptr<_Tp>::operator=(std::move(__r));
00280 return *this;
00281 }
00282
00283 template<class _Tp1>
00284 shared_ptr&
00285 operator=(shared_ptr<_Tp1>&& __r)
00286 {
00287 this->__shared_ptr<_Tp>::operator=(std::move(__r));
00288 return *this;
00289 }
00290
00291 template<typename _Tp1, typename _Del>
00292 shared_ptr&
00293 operator=(std::unique_ptr<_Tp1, _Del>&& __r)
00294 {
00295 this->__shared_ptr<_Tp>::operator=(std::move(__r));
00296 return *this;
00297 }
00298
00299 private:
00300
00301 template<typename _Alloc, typename... _Args>
00302 shared_ptr(_Sp_make_shared_tag __tag, _Alloc __a, _Args&&... __args)
00303 : __shared_ptr<_Tp>(__tag, __a, std::forward<_Args>(__args)...)
00304 { }
00305
00306 template<typename _Tp1, typename _Alloc, typename... _Args>
00307 friend shared_ptr<_Tp1>
00308 allocate_shared(_Alloc __a, _Args&&... __args);
00309 };
00310
00311
00312 template<typename _Tp1, typename _Tp2>
00313 inline bool
00314 operator==(const shared_ptr<_Tp1>& __a, const shared_ptr<_Tp2>& __b)
00315 { return __a.get() == __b.get(); }
00316
00317 template<typename _Tp1, typename _Tp2>
00318 inline bool
00319 operator!=(const shared_ptr<_Tp1>& __a, const shared_ptr<_Tp2>& __b)
00320 { return __a.get() != __b.get(); }
00321
00322 template<typename _Tp1, typename _Tp2>
00323 inline bool
00324 operator<(const shared_ptr<_Tp1>& __a, const shared_ptr<_Tp2>& __b)
00325 { return __a.get() < __b.get(); }
00326
00327 template<typename _Tp>
00328 struct less<shared_ptr<_Tp>> : public _Sp_less<shared_ptr<_Tp>>
00329 { };
00330
00331
00332 template<typename _Tp>
00333 inline void
00334 swap(shared_ptr<_Tp>& __a, shared_ptr<_Tp>& __b)
00335 { __a.swap(__b); }
00336
00337
00338 template<typename _Tp, typename _Tp1>
00339 inline shared_ptr<_Tp>
00340 static_pointer_cast(const shared_ptr<_Tp1>& __r)
00341 { return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get())); }
00342
00343 template<typename _Tp, typename _Tp1>
00344 inline shared_ptr<_Tp>
00345 const_pointer_cast(const shared_ptr<_Tp1>& __r)
00346 { return shared_ptr<_Tp>(__r, const_cast<_Tp*>(__r.get())); }
00347
00348 template<typename _Tp, typename _Tp1>
00349 inline shared_ptr<_Tp>
00350 dynamic_pointer_cast(const shared_ptr<_Tp1>& __r)
00351 {
00352 if (_Tp* __p = dynamic_cast<_Tp*>(__r.get()))
00353 return shared_ptr<_Tp>(__r, __p);
00354 return shared_ptr<_Tp>();
00355 }
00356
00357
00358
00359
00360
00361
00362
00363 template<typename _Tp>
00364 class weak_ptr : public __weak_ptr<_Tp>
00365 {
00366 public:
00367 weak_ptr() : __weak_ptr<_Tp>() { }
00368
00369 template<typename _Tp1>
00370 weak_ptr(const weak_ptr<_Tp1>& __r)
00371 : __weak_ptr<_Tp>(__r) { }
00372
00373 template<typename _Tp1>
00374 weak_ptr(const shared_ptr<_Tp1>& __r)
00375 : __weak_ptr<_Tp>(__r) { }
00376
00377 template<typename _Tp1>
00378 weak_ptr&
00379 operator=(const weak_ptr<_Tp1>& __r)
00380 {
00381 this->__weak_ptr<_Tp>::operator=(__r);
00382 return *this;
00383 }
00384
00385 template<typename _Tp1>
00386 weak_ptr&
00387 operator=(const shared_ptr<_Tp1>& __r)
00388 {
00389 this->__weak_ptr<_Tp>::operator=(__r);
00390 return *this;
00391 }
00392
00393 shared_ptr<_Tp>
00394 lock() const
00395 {
00396 #ifdef __GTHREADS
00397 if (this->expired())
00398 return shared_ptr<_Tp>();
00399
00400 __try
00401 {
00402 return shared_ptr<_Tp>(*this);
00403 }
00404 __catch(const bad_weak_ptr&)
00405 {
00406 return shared_ptr<_Tp>();
00407 }
00408 #else
00409 return this->expired() ? shared_ptr<_Tp>() : shared_ptr<_Tp>(*this);
00410 #endif
00411 }
00412 };
00413
00414
00415 template<typename _Tp>
00416 inline void
00417 swap(weak_ptr<_Tp>& __a, weak_ptr<_Tp>& __b)
00418 { __a.swap(__b); }
00419
00420
00421
00422 template<typename _Tp>
00423 struct owner_less;
00424
00425
00426 template<typename _Tp>
00427 struct owner_less<shared_ptr<_Tp>>
00428 : public _Sp_owner_less<shared_ptr<_Tp>, weak_ptr<_Tp>>
00429 { };
00430
00431
00432 template<typename _Tp>
00433 struct owner_less<weak_ptr<_Tp>>
00434 : public _Sp_owner_less<weak_ptr<_Tp>, shared_ptr<_Tp>>
00435 { };
00436
00437
00438
00439
00440 template<typename _Tp>
00441 class enable_shared_from_this
00442 {
00443 protected:
00444 enable_shared_from_this() { }
00445
00446 enable_shared_from_this(const enable_shared_from_this&) { }
00447
00448 enable_shared_from_this&
00449 operator=(const enable_shared_from_this&)
00450 { return *this; }
00451
00452 ~enable_shared_from_this() { }
00453
00454 public:
00455 shared_ptr<_Tp>
00456 shared_from_this()
00457 { return shared_ptr<_Tp>(this->_M_weak_this); }
00458
00459 shared_ptr<const _Tp>
00460 shared_from_this() const
00461 { return shared_ptr<const _Tp>(this->_M_weak_this); }
00462
00463 private:
00464 template<typename _Tp1>
00465 void
00466 _M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const
00467 { _M_weak_this._M_assign(__p, __n); }
00468
00469 template<typename _Tp1>
00470 friend void
00471 __enable_shared_from_this_helper(const __shared_count<>& __pn,
00472 const enable_shared_from_this* __pe,
00473 const _Tp1* __px)
00474 {
00475 if (__pe != 0)
00476 __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
00477 }
00478
00479 mutable weak_ptr<_Tp> _M_weak_this;
00480 };
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493 template<typename _Tp, typename _Alloc, typename... _Args>
00494 inline shared_ptr<_Tp>
00495 allocate_shared(_Alloc __a, _Args&&... __args)
00496 {
00497 return shared_ptr<_Tp>(_Sp_make_shared_tag(), std::forward<_Alloc>(__a),
00498 std::forward<_Args>(__args)...);
00499 }
00500
00501
00502
00503
00504
00505
00506
00507
00508 template<typename _Tp, typename... _Args>
00509 inline shared_ptr<_Tp>
00510 make_shared(_Args&&... __args)
00511 {
00512 typedef typename std::remove_const<_Tp>::type _Tp_nc;
00513 return allocate_shared<_Tp>(std::allocator<_Tp_nc>(),
00514 std::forward<_Args>(__args)...);
00515 }
00516
00517
00518 template<typename _Tp>
00519 struct hash<shared_ptr<_Tp>>
00520 : public std::unary_function<shared_ptr<_Tp>, size_t>
00521 {
00522 size_t
00523 operator()(const shared_ptr<_Tp>& __s) const
00524 { return std::hash<_Tp*>()(__s.get()); }
00525 };
00526
00527
00528
00529 _GLIBCXX_END_NAMESPACE
00530
00531 #endif // _SHARED_PTR_H