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, typename = typename
00210 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
00211 shared_ptr(const shared_ptr<_Tp1>& __r)
00212 : __shared_ptr<_Tp>(__r) { }
00213
00214
00215
00216
00217
00218
00219 shared_ptr(shared_ptr&& __r)
00220 : __shared_ptr<_Tp>(std::move(__r)) { }
00221
00222
00223
00224
00225
00226
00227 template<typename _Tp1, typename = typename
00228 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
00229 shared_ptr(shared_ptr<_Tp1>&& __r)
00230 : __shared_ptr<_Tp>(std::move(__r)) { }
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240 template<typename _Tp1>
00241 explicit shared_ptr(const weak_ptr<_Tp1>& __r)
00242 : __shared_ptr<_Tp>(__r) { }
00243
00244 #if _GLIBCXX_DEPRECATED
00245 template<typename _Tp1>
00246 shared_ptr(std::auto_ptr<_Tp1>&& __r)
00247 : __shared_ptr<_Tp>(std::move(__r)) { }
00248 #endif
00249
00250 template<typename _Tp1, typename _Del>
00251 shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r)
00252 : __shared_ptr<_Tp>(std::move(__r)) { }
00253
00254
00255
00256
00257
00258
00259 shared_ptr(nullptr_t __p) : __shared_ptr<_Tp>(__p) { }
00260
00261 template<typename _Tp1>
00262 shared_ptr&
00263 operator=(const shared_ptr<_Tp1>& __r)
00264 {
00265 this->__shared_ptr<_Tp>::operator=(__r);
00266 return *this;
00267 }
00268
00269 #if _GLIBCXX_DEPRECATED
00270 template<typename _Tp1>
00271 shared_ptr&
00272 operator=(std::auto_ptr<_Tp1>&& __r)
00273 {
00274 this->__shared_ptr<_Tp>::operator=(std::move(__r));
00275 return *this;
00276 }
00277 #endif
00278
00279 shared_ptr&
00280 operator=(shared_ptr&& __r)
00281 {
00282 this->__shared_ptr<_Tp>::operator=(std::move(__r));
00283 return *this;
00284 }
00285
00286 template<class _Tp1>
00287 shared_ptr&
00288 operator=(shared_ptr<_Tp1>&& __r)
00289 {
00290 this->__shared_ptr<_Tp>::operator=(std::move(__r));
00291 return *this;
00292 }
00293
00294 template<typename _Tp1, typename _Del>
00295 shared_ptr&
00296 operator=(std::unique_ptr<_Tp1, _Del>&& __r)
00297 {
00298 this->__shared_ptr<_Tp>::operator=(std::move(__r));
00299 return *this;
00300 }
00301
00302 private:
00303
00304 template<typename _Alloc, typename... _Args>
00305 shared_ptr(_Sp_make_shared_tag __tag, _Alloc __a, _Args&&... __args)
00306 : __shared_ptr<_Tp>(__tag, __a, std::forward<_Args>(__args)...)
00307 { }
00308
00309 template<typename _Tp1, typename _Alloc, typename... _Args>
00310 friend shared_ptr<_Tp1>
00311 allocate_shared(_Alloc __a, _Args&&... __args);
00312 };
00313
00314
00315 template<typename _Tp1, typename _Tp2>
00316 inline bool
00317 operator==(const shared_ptr<_Tp1>& __a, const shared_ptr<_Tp2>& __b)
00318 { return __a.get() == __b.get(); }
00319
00320 template<typename _Tp>
00321 inline bool
00322 operator==(const shared_ptr<_Tp>& __a, nullptr_t)
00323 { return __a.get() == nullptr; }
00324
00325 template<typename _Tp>
00326 inline bool
00327 operator==(nullptr_t, const shared_ptr<_Tp>& __b)
00328 { return nullptr == __b.get(); }
00329
00330 template<typename _Tp1, typename _Tp2>
00331 inline bool
00332 operator!=(const shared_ptr<_Tp1>& __a, const shared_ptr<_Tp2>& __b)
00333 { return __a.get() != __b.get(); }
00334
00335 template<typename _Tp>
00336 inline bool
00337 operator!=(const shared_ptr<_Tp>& __a, nullptr_t)
00338 { return __a.get() != nullptr; }
00339
00340 template<typename _Tp>
00341 inline bool
00342 operator!=(nullptr_t, const shared_ptr<_Tp>& __b)
00343 { return nullptr != __b.get(); }
00344
00345 template<typename _Tp1, typename _Tp2>
00346 inline bool
00347 operator<(const shared_ptr<_Tp1>& __a, const shared_ptr<_Tp2>& __b)
00348 { return __a.get() < __b.get(); }
00349
00350 template<typename _Tp>
00351 struct less<shared_ptr<_Tp>> : public _Sp_less<shared_ptr<_Tp>>
00352 { };
00353
00354
00355 template<typename _Tp>
00356 inline void
00357 swap(shared_ptr<_Tp>& __a, shared_ptr<_Tp>& __b)
00358 { __a.swap(__b); }
00359
00360
00361 template<typename _Tp, typename _Tp1>
00362 inline shared_ptr<_Tp>
00363 static_pointer_cast(const shared_ptr<_Tp1>& __r)
00364 { return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get())); }
00365
00366 template<typename _Tp, typename _Tp1>
00367 inline shared_ptr<_Tp>
00368 const_pointer_cast(const shared_ptr<_Tp1>& __r)
00369 { return shared_ptr<_Tp>(__r, const_cast<_Tp*>(__r.get())); }
00370
00371 template<typename _Tp, typename _Tp1>
00372 inline shared_ptr<_Tp>
00373 dynamic_pointer_cast(const shared_ptr<_Tp1>& __r)
00374 {
00375 if (_Tp* __p = dynamic_cast<_Tp*>(__r.get()))
00376 return shared_ptr<_Tp>(__r, __p);
00377 return shared_ptr<_Tp>();
00378 }
00379
00380
00381
00382
00383
00384
00385
00386 template<typename _Tp>
00387 class weak_ptr : public __weak_ptr<_Tp>
00388 {
00389 public:
00390 weak_ptr() : __weak_ptr<_Tp>() { }
00391
00392 template<typename _Tp1, typename = typename
00393 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
00394 weak_ptr(const weak_ptr<_Tp1>& __r)
00395 : __weak_ptr<_Tp>(__r) { }
00396
00397 template<typename _Tp1, typename = typename
00398 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
00399 weak_ptr(const shared_ptr<_Tp1>& __r)
00400 : __weak_ptr<_Tp>(__r) { }
00401
00402 template<typename _Tp1>
00403 weak_ptr&
00404 operator=(const weak_ptr<_Tp1>& __r)
00405 {
00406 this->__weak_ptr<_Tp>::operator=(__r);
00407 return *this;
00408 }
00409
00410 template<typename _Tp1>
00411 weak_ptr&
00412 operator=(const shared_ptr<_Tp1>& __r)
00413 {
00414 this->__weak_ptr<_Tp>::operator=(__r);
00415 return *this;
00416 }
00417
00418 shared_ptr<_Tp>
00419 lock() const
00420 {
00421 #ifdef __GTHREADS
00422 if (this->expired())
00423 return shared_ptr<_Tp>();
00424
00425 __try
00426 {
00427 return shared_ptr<_Tp>(*this);
00428 }
00429 __catch(const bad_weak_ptr&)
00430 {
00431 return shared_ptr<_Tp>();
00432 }
00433 #else
00434 return this->expired() ? shared_ptr<_Tp>() : shared_ptr<_Tp>(*this);
00435 #endif
00436 }
00437 };
00438
00439
00440 template<typename _Tp>
00441 inline void
00442 swap(weak_ptr<_Tp>& __a, weak_ptr<_Tp>& __b)
00443 { __a.swap(__b); }
00444
00445
00446
00447 template<typename _Tp>
00448 struct owner_less;
00449
00450
00451 template<typename _Tp>
00452 struct owner_less<shared_ptr<_Tp>>
00453 : public _Sp_owner_less<shared_ptr<_Tp>, weak_ptr<_Tp>>
00454 { };
00455
00456
00457 template<typename _Tp>
00458 struct owner_less<weak_ptr<_Tp>>
00459 : public _Sp_owner_less<weak_ptr<_Tp>, shared_ptr<_Tp>>
00460 { };
00461
00462
00463
00464
00465 template<typename _Tp>
00466 class enable_shared_from_this
00467 {
00468 protected:
00469 enable_shared_from_this() { }
00470
00471 enable_shared_from_this(const enable_shared_from_this&) { }
00472
00473 enable_shared_from_this&
00474 operator=(const enable_shared_from_this&)
00475 { return *this; }
00476
00477 ~enable_shared_from_this() { }
00478
00479 public:
00480 shared_ptr<_Tp>
00481 shared_from_this()
00482 { return shared_ptr<_Tp>(this->_M_weak_this); }
00483
00484 shared_ptr<const _Tp>
00485 shared_from_this() const
00486 { return shared_ptr<const _Tp>(this->_M_weak_this); }
00487
00488 private:
00489 template<typename _Tp1>
00490 void
00491 _M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const
00492 { _M_weak_this._M_assign(__p, __n); }
00493
00494 template<typename _Tp1>
00495 friend void
00496 __enable_shared_from_this_helper(const __shared_count<>& __pn,
00497 const enable_shared_from_this* __pe,
00498 const _Tp1* __px)
00499 {
00500 if (__pe != 0)
00501 __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
00502 }
00503
00504 mutable weak_ptr<_Tp> _M_weak_this;
00505 };
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518 template<typename _Tp, typename _Alloc, typename... _Args>
00519 inline shared_ptr<_Tp>
00520 allocate_shared(_Alloc __a, _Args&&... __args)
00521 {
00522 return shared_ptr<_Tp>(_Sp_make_shared_tag(), std::forward<_Alloc>(__a),
00523 std::forward<_Args>(__args)...);
00524 }
00525
00526
00527
00528
00529
00530
00531
00532
00533 template<typename _Tp, typename... _Args>
00534 inline shared_ptr<_Tp>
00535 make_shared(_Args&&... __args)
00536 {
00537 typedef typename std::remove_const<_Tp>::type _Tp_nc;
00538 return allocate_shared<_Tp>(std::allocator<_Tp_nc>(),
00539 std::forward<_Args>(__args)...);
00540 }
00541
00542
00543 template<typename _Tp>
00544 struct hash<shared_ptr<_Tp>>
00545 : public std::unary_function<shared_ptr<_Tp>, size_t>
00546 {
00547 size_t
00548 operator()(const shared_ptr<_Tp>& __s) const
00549 { return std::hash<_Tp*>()(__s.get()); }
00550 };
00551
00552
00553
00554 _GLIBCXX_END_NAMESPACE
00555
00556 #endif // _SHARED_PTR_H