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