shared_ptr.h 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  1. // <tr1/shared_ptr.h> -*- C++ -*-
  2. // Copyright (C) 2007-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. // shared_count.hpp
  21. // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
  22. // shared_ptr.hpp
  23. // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
  24. // Copyright (C) 2001, 2002, 2003 Peter Dimov
  25. // weak_ptr.hpp
  26. // Copyright (C) 2001, 2002, 2003 Peter Dimov
  27. // enable_shared_from_this.hpp
  28. // Copyright (C) 2002 Peter Dimov
  29. // Distributed under the Boost Software License, Version 1.0. (See
  30. // accompanying file LICENSE_1_0.txt or copy at
  31. // http://www.boost.org/LICENSE_1_0.txt)
  32. // GCC Note: based on version 1.32.0 of the Boost library.
  33. /** @file tr1/shared_ptr.h
  34. * This is an internal header file, included by other library headers.
  35. * Do not attempt to use it directly. @headername{tr1/memory}
  36. */
  37. #ifndef _TR1_SHARED_PTR_H
  38. #define _TR1_SHARED_PTR_H 1
  39. namespace std _GLIBCXX_VISIBILITY(default)
  40. {
  41. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  42. namespace tr1
  43. {
  44. /**
  45. * @brief Exception possibly thrown by @c shared_ptr.
  46. * @ingroup exceptions
  47. */
  48. class bad_weak_ptr : public std::exception
  49. {
  50. public:
  51. virtual char const*
  52. what() const throw()
  53. { return "tr1::bad_weak_ptr"; }
  54. };
  55. // Substitute for bad_weak_ptr object in the case of -fno-exceptions.
  56. inline void
  57. __throw_bad_weak_ptr()
  58. { _GLIBCXX_THROW_OR_ABORT(bad_weak_ptr()); }
  59. using __gnu_cxx::_Lock_policy;
  60. using __gnu_cxx::__default_lock_policy;
  61. using __gnu_cxx::_S_single;
  62. using __gnu_cxx::_S_mutex;
  63. using __gnu_cxx::_S_atomic;
  64. // Empty helper class except when the template argument is _S_mutex.
  65. template<_Lock_policy _Lp>
  66. class _Mutex_base
  67. {
  68. protected:
  69. // The atomic policy uses fully-fenced builtins, single doesn't care.
  70. enum { _S_need_barriers = 0 };
  71. };
  72. template<>
  73. class _Mutex_base<_S_mutex>
  74. : public __gnu_cxx::__mutex
  75. {
  76. protected:
  77. // This policy is used when atomic builtins are not available.
  78. // The replacement atomic operations might not have the necessary
  79. // memory barriers.
  80. enum { _S_need_barriers = 1 };
  81. };
  82. template<_Lock_policy _Lp = __default_lock_policy>
  83. class _Sp_counted_base
  84. : public _Mutex_base<_Lp>
  85. {
  86. public:
  87. _Sp_counted_base()
  88. : _M_use_count(1), _M_weak_count(1) { }
  89. virtual
  90. ~_Sp_counted_base() // nothrow
  91. { }
  92. // Called when _M_use_count drops to zero, to release the resources
  93. // managed by *this.
  94. virtual void
  95. _M_dispose() = 0; // nothrow
  96. // Called when _M_weak_count drops to zero.
  97. virtual void
  98. _M_destroy() // nothrow
  99. { delete this; }
  100. virtual void*
  101. _M_get_deleter(const std::type_info&) = 0;
  102. void
  103. _M_add_ref_copy()
  104. { __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); }
  105. void
  106. _M_add_ref_lock();
  107. void
  108. _M_release() // nothrow
  109. {
  110. // Be race-detector-friendly. For more info see bits/c++config.
  111. _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count);
  112. if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
  113. {
  114. _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count);
  115. _M_dispose();
  116. // There must be a memory barrier between dispose() and destroy()
  117. // to ensure that the effects of dispose() are observed in the
  118. // thread that runs destroy().
  119. // See http://gcc.gnu.org/ml/libstdc++/2005-11/msg00136.html
  120. if (_Mutex_base<_Lp>::_S_need_barriers)
  121. {
  122. __atomic_thread_fence (__ATOMIC_ACQ_REL);
  123. }
  124. // Be race-detector-friendly. For more info see bits/c++config.
  125. _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
  126. if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count,
  127. -1) == 1)
  128. {
  129. _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
  130. _M_destroy();
  131. }
  132. }
  133. }
  134. void
  135. _M_weak_add_ref() // nothrow
  136. { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); }
  137. void
  138. _M_weak_release() // nothrow
  139. {
  140. // Be race-detector-friendly. For more info see bits/c++config.
  141. _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
  142. if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1)
  143. {
  144. _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
  145. if (_Mutex_base<_Lp>::_S_need_barriers)
  146. {
  147. // See _M_release(),
  148. // destroy() must observe results of dispose()
  149. __atomic_thread_fence (__ATOMIC_ACQ_REL);
  150. }
  151. _M_destroy();
  152. }
  153. }
  154. long
  155. _M_get_use_count() const // nothrow
  156. {
  157. // No memory barrier is used here so there is no synchronization
  158. // with other threads.
  159. return const_cast<const volatile _Atomic_word&>(_M_use_count);
  160. }
  161. private:
  162. _Sp_counted_base(_Sp_counted_base const&);
  163. _Sp_counted_base& operator=(_Sp_counted_base const&);
  164. _Atomic_word _M_use_count; // #shared
  165. _Atomic_word _M_weak_count; // #weak + (#shared != 0)
  166. };
  167. template<>
  168. inline void
  169. _Sp_counted_base<_S_single>::
  170. _M_add_ref_lock()
  171. {
  172. if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
  173. {
  174. _M_use_count = 0;
  175. __throw_bad_weak_ptr();
  176. }
  177. }
  178. template<>
  179. inline void
  180. _Sp_counted_base<_S_mutex>::
  181. _M_add_ref_lock()
  182. {
  183. __gnu_cxx::__scoped_lock sentry(*this);
  184. if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
  185. {
  186. _M_use_count = 0;
  187. __throw_bad_weak_ptr();
  188. }
  189. }
  190. template<>
  191. inline void
  192. _Sp_counted_base<_S_atomic>::
  193. _M_add_ref_lock()
  194. {
  195. // Perform lock-free add-if-not-zero operation.
  196. _Atomic_word __count = _M_use_count;
  197. do
  198. {
  199. if (__count == 0)
  200. __throw_bad_weak_ptr();
  201. // Replace the current counter value with the old value + 1, as
  202. // long as it's not changed meanwhile.
  203. }
  204. while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1,
  205. true, __ATOMIC_ACQ_REL,
  206. __ATOMIC_RELAXED));
  207. }
  208. template<typename _Ptr, typename _Deleter, _Lock_policy _Lp>
  209. class _Sp_counted_base_impl
  210. : public _Sp_counted_base<_Lp>
  211. {
  212. public:
  213. // Precondition: __d(__p) must not throw.
  214. _Sp_counted_base_impl(_Ptr __p, _Deleter __d)
  215. : _M_ptr(__p), _M_del(__d) { }
  216. virtual void
  217. _M_dispose() // nothrow
  218. { _M_del(_M_ptr); }
  219. virtual void*
  220. _M_get_deleter(const std::type_info& __ti)
  221. {
  222. #if __cpp_rtti
  223. return __ti == typeid(_Deleter) ? &_M_del : 0;
  224. #else
  225. return 0;
  226. #endif
  227. }
  228. private:
  229. _Sp_counted_base_impl(const _Sp_counted_base_impl&);
  230. _Sp_counted_base_impl& operator=(const _Sp_counted_base_impl&);
  231. _Ptr _M_ptr; // copy constructor must not throw
  232. _Deleter _M_del; // copy constructor must not throw
  233. };
  234. template<_Lock_policy _Lp = __default_lock_policy>
  235. class __weak_count;
  236. template<typename _Tp>
  237. struct _Sp_deleter
  238. {
  239. typedef void result_type;
  240. typedef _Tp* argument_type;
  241. void operator()(_Tp* __p) const { delete __p; }
  242. };
  243. template<_Lock_policy _Lp = __default_lock_policy>
  244. class __shared_count
  245. {
  246. public:
  247. __shared_count()
  248. : _M_pi(0) // nothrow
  249. { }
  250. template<typename _Ptr>
  251. __shared_count(_Ptr __p) : _M_pi(0)
  252. {
  253. __try
  254. {
  255. typedef typename std::tr1::remove_pointer<_Ptr>::type _Tp;
  256. _M_pi = new _Sp_counted_base_impl<_Ptr, _Sp_deleter<_Tp>, _Lp>(
  257. __p, _Sp_deleter<_Tp>());
  258. }
  259. __catch(...)
  260. {
  261. delete __p;
  262. __throw_exception_again;
  263. }
  264. }
  265. template<typename _Ptr, typename _Deleter>
  266. __shared_count(_Ptr __p, _Deleter __d) : _M_pi(0)
  267. {
  268. __try
  269. {
  270. _M_pi = new _Sp_counted_base_impl<_Ptr, _Deleter, _Lp>(__p, __d);
  271. }
  272. __catch(...)
  273. {
  274. __d(__p); // Call _Deleter on __p.
  275. __throw_exception_again;
  276. }
  277. }
  278. #if (__cplusplus < 201103L) || _GLIBCXX_USE_DEPRECATED
  279. #pragma GCC diagnostic push
  280. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  281. // Special case for auto_ptr<_Tp> to provide the strong guarantee.
  282. template<typename _Tp>
  283. explicit
  284. __shared_count(std::auto_ptr<_Tp>& __r)
  285. : _M_pi(new _Sp_counted_base_impl<_Tp*,
  286. _Sp_deleter<_Tp>, _Lp >(__r.get(), _Sp_deleter<_Tp>()))
  287. { __r.release(); }
  288. #pragma GCC diagnostic pop
  289. #endif
  290. // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
  291. explicit
  292. __shared_count(const __weak_count<_Lp>& __r);
  293. ~__shared_count() // nothrow
  294. {
  295. if (_M_pi != 0)
  296. _M_pi->_M_release();
  297. }
  298. __shared_count(const __shared_count& __r)
  299. : _M_pi(__r._M_pi) // nothrow
  300. {
  301. if (_M_pi != 0)
  302. _M_pi->_M_add_ref_copy();
  303. }
  304. __shared_count&
  305. operator=(const __shared_count& __r) // nothrow
  306. {
  307. _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
  308. if (__tmp != _M_pi)
  309. {
  310. if (__tmp != 0)
  311. __tmp->_M_add_ref_copy();
  312. if (_M_pi != 0)
  313. _M_pi->_M_release();
  314. _M_pi = __tmp;
  315. }
  316. return *this;
  317. }
  318. void
  319. _M_swap(__shared_count& __r) // nothrow
  320. {
  321. _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
  322. __r._M_pi = _M_pi;
  323. _M_pi = __tmp;
  324. }
  325. long
  326. _M_get_use_count() const // nothrow
  327. { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
  328. bool
  329. _M_unique() const // nothrow
  330. { return this->_M_get_use_count() == 1; }
  331. friend inline bool
  332. operator==(const __shared_count& __a, const __shared_count& __b)
  333. { return __a._M_pi == __b._M_pi; }
  334. friend inline bool
  335. operator<(const __shared_count& __a, const __shared_count& __b)
  336. { return std::less<_Sp_counted_base<_Lp>*>()(__a._M_pi, __b._M_pi); }
  337. void*
  338. _M_get_deleter(const std::type_info& __ti) const
  339. { return _M_pi ? _M_pi->_M_get_deleter(__ti) : 0; }
  340. private:
  341. friend class __weak_count<_Lp>;
  342. _Sp_counted_base<_Lp>* _M_pi;
  343. };
  344. template<_Lock_policy _Lp>
  345. class __weak_count
  346. {
  347. public:
  348. __weak_count()
  349. : _M_pi(0) // nothrow
  350. { }
  351. __weak_count(const __shared_count<_Lp>& __r)
  352. : _M_pi(__r._M_pi) // nothrow
  353. {
  354. if (_M_pi != 0)
  355. _M_pi->_M_weak_add_ref();
  356. }
  357. __weak_count(const __weak_count<_Lp>& __r)
  358. : _M_pi(__r._M_pi) // nothrow
  359. {
  360. if (_M_pi != 0)
  361. _M_pi->_M_weak_add_ref();
  362. }
  363. ~__weak_count() // nothrow
  364. {
  365. if (_M_pi != 0)
  366. _M_pi->_M_weak_release();
  367. }
  368. __weak_count<_Lp>&
  369. operator=(const __shared_count<_Lp>& __r) // nothrow
  370. {
  371. _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
  372. if (__tmp != 0)
  373. __tmp->_M_weak_add_ref();
  374. if (_M_pi != 0)
  375. _M_pi->_M_weak_release();
  376. _M_pi = __tmp;
  377. return *this;
  378. }
  379. __weak_count<_Lp>&
  380. operator=(const __weak_count<_Lp>& __r) // nothrow
  381. {
  382. _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
  383. if (__tmp != 0)
  384. __tmp->_M_weak_add_ref();
  385. if (_M_pi != 0)
  386. _M_pi->_M_weak_release();
  387. _M_pi = __tmp;
  388. return *this;
  389. }
  390. void
  391. _M_swap(__weak_count<_Lp>& __r) // nothrow
  392. {
  393. _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
  394. __r._M_pi = _M_pi;
  395. _M_pi = __tmp;
  396. }
  397. long
  398. _M_get_use_count() const // nothrow
  399. { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
  400. friend inline bool
  401. operator==(const __weak_count<_Lp>& __a, const __weak_count<_Lp>& __b)
  402. { return __a._M_pi == __b._M_pi; }
  403. friend inline bool
  404. operator<(const __weak_count<_Lp>& __a, const __weak_count<_Lp>& __b)
  405. { return std::less<_Sp_counted_base<_Lp>*>()(__a._M_pi, __b._M_pi); }
  406. private:
  407. friend class __shared_count<_Lp>;
  408. _Sp_counted_base<_Lp>* _M_pi;
  409. };
  410. // now that __weak_count is defined we can define this constructor:
  411. template<_Lock_policy _Lp>
  412. inline
  413. __shared_count<_Lp>::
  414. __shared_count(const __weak_count<_Lp>& __r)
  415. : _M_pi(__r._M_pi)
  416. {
  417. if (_M_pi != 0)
  418. _M_pi->_M_add_ref_lock();
  419. else
  420. __throw_bad_weak_ptr();
  421. }
  422. // Forward declarations.
  423. template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
  424. class __shared_ptr;
  425. template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
  426. class __weak_ptr;
  427. template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
  428. class __enable_shared_from_this;
  429. template<typename _Tp>
  430. class shared_ptr;
  431. template<typename _Tp>
  432. class weak_ptr;
  433. template<typename _Tp>
  434. class enable_shared_from_this;
  435. // Support for enable_shared_from_this.
  436. // Friend of __enable_shared_from_this.
  437. template<_Lock_policy _Lp, typename _Tp1, typename _Tp2>
  438. void
  439. __enable_shared_from_this_helper(const __shared_count<_Lp>&,
  440. const __enable_shared_from_this<_Tp1,
  441. _Lp>*, const _Tp2*);
  442. // Friend of enable_shared_from_this.
  443. template<typename _Tp1, typename _Tp2>
  444. void
  445. __enable_shared_from_this_helper(const __shared_count<>&,
  446. const enable_shared_from_this<_Tp1>*,
  447. const _Tp2*);
  448. template<_Lock_policy _Lp>
  449. inline void
  450. __enable_shared_from_this_helper(const __shared_count<_Lp>&, ...)
  451. { }
  452. struct __static_cast_tag { };
  453. struct __const_cast_tag { };
  454. struct __dynamic_cast_tag { };
  455. // A smart pointer with reference-counted copy semantics. The
  456. // object pointed to is deleted when the last shared_ptr pointing to
  457. // it is destroyed or reset.
  458. template<typename _Tp, _Lock_policy _Lp>
  459. class __shared_ptr
  460. {
  461. public:
  462. typedef _Tp element_type;
  463. __shared_ptr()
  464. : _M_ptr(0), _M_refcount() // never throws
  465. { }
  466. template<typename _Tp1>
  467. explicit
  468. __shared_ptr(_Tp1* __p)
  469. : _M_ptr(__p), _M_refcount(__p)
  470. {
  471. __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
  472. typedef int _IsComplete[sizeof(_Tp1)];
  473. __enable_shared_from_this_helper(_M_refcount, __p, __p);
  474. }
  475. template<typename _Tp1, typename _Deleter>
  476. __shared_ptr(_Tp1* __p, _Deleter __d)
  477. : _M_ptr(__p), _M_refcount(__p, __d)
  478. {
  479. __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
  480. // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
  481. __enable_shared_from_this_helper(_M_refcount, __p, __p);
  482. }
  483. // generated copy constructor, assignment, destructor are fine.
  484. template<typename _Tp1>
  485. __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r)
  486. : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
  487. { __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) }
  488. template<typename _Tp1>
  489. explicit
  490. __shared_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
  491. : _M_refcount(__r._M_refcount) // may throw
  492. {
  493. __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
  494. // It is now safe to copy __r._M_ptr, as _M_refcount(__r._M_refcount)
  495. // did not throw.
  496. _M_ptr = __r._M_ptr;
  497. }
  498. #if (__cplusplus < 201103L) || _GLIBCXX_USE_DEPRECATED
  499. #pragma GCC diagnostic push
  500. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  501. // Postcondition: use_count() == 1 and __r.get() == 0
  502. template<typename _Tp1>
  503. explicit
  504. __shared_ptr(std::auto_ptr<_Tp1>& __r)
  505. : _M_ptr(__r.get()), _M_refcount()
  506. { // TODO requries delete __r.release() well-formed
  507. __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
  508. typedef int _IsComplete[sizeof(_Tp1)];
  509. _Tp1* __tmp = __r.get();
  510. _M_refcount = __shared_count<_Lp>(__r);
  511. __enable_shared_from_this_helper(_M_refcount, __tmp, __tmp);
  512. }
  513. #pragma GCC diagnostic pop
  514. #endif
  515. template<typename _Tp1>
  516. __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r, __static_cast_tag)
  517. : _M_ptr(static_cast<element_type*>(__r._M_ptr)),
  518. _M_refcount(__r._M_refcount)
  519. { }
  520. template<typename _Tp1>
  521. __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r, __const_cast_tag)
  522. : _M_ptr(const_cast<element_type*>(__r._M_ptr)),
  523. _M_refcount(__r._M_refcount)
  524. { }
  525. template<typename _Tp1>
  526. __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r, __dynamic_cast_tag)
  527. : _M_ptr(dynamic_cast<element_type*>(__r._M_ptr)),
  528. _M_refcount(__r._M_refcount)
  529. {
  530. if (_M_ptr == 0) // need to allocate new counter -- the cast failed
  531. _M_refcount = __shared_count<_Lp>();
  532. }
  533. template<typename _Tp1>
  534. __shared_ptr&
  535. operator=(const __shared_ptr<_Tp1, _Lp>& __r) // never throws
  536. {
  537. _M_ptr = __r._M_ptr;
  538. _M_refcount = __r._M_refcount; // __shared_count::op= doesn't throw
  539. return *this;
  540. }
  541. #if (__cplusplus < 201103L) || _GLIBCXX_USE_DEPRECATED
  542. #pragma GCC diagnostic push
  543. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  544. template<typename _Tp1>
  545. __shared_ptr&
  546. operator=(std::auto_ptr<_Tp1>& __r)
  547. {
  548. __shared_ptr(__r).swap(*this);
  549. return *this;
  550. }
  551. #pragma GCC diagnostic pop
  552. #endif
  553. void
  554. reset() // never throws
  555. { __shared_ptr().swap(*this); }
  556. template<typename _Tp1>
  557. void
  558. reset(_Tp1* __p) // _Tp1 must be complete.
  559. {
  560. // Catch self-reset errors.
  561. _GLIBCXX_DEBUG_ASSERT(__p == 0 || __p != _M_ptr);
  562. __shared_ptr(__p).swap(*this);
  563. }
  564. template<typename _Tp1, typename _Deleter>
  565. void
  566. reset(_Tp1* __p, _Deleter __d)
  567. { __shared_ptr(__p, __d).swap(*this); }
  568. // Allow class instantiation when _Tp is [cv-qual] void.
  569. typename std::tr1::add_reference<_Tp>::type
  570. operator*() const // never throws
  571. {
  572. _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
  573. return *_M_ptr;
  574. }
  575. _Tp*
  576. operator->() const // never throws
  577. {
  578. _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
  579. return _M_ptr;
  580. }
  581. _Tp*
  582. get() const // never throws
  583. { return _M_ptr; }
  584. // Implicit conversion to "bool"
  585. private:
  586. typedef _Tp* __shared_ptr::*__unspecified_bool_type;
  587. public:
  588. operator __unspecified_bool_type() const // never throws
  589. { return _M_ptr == 0 ? 0 : &__shared_ptr::_M_ptr; }
  590. bool
  591. unique() const // never throws
  592. { return _M_refcount._M_unique(); }
  593. long
  594. use_count() const // never throws
  595. { return _M_refcount._M_get_use_count(); }
  596. void
  597. swap(__shared_ptr<_Tp, _Lp>& __other) // never throws
  598. {
  599. std::swap(_M_ptr, __other._M_ptr);
  600. _M_refcount._M_swap(__other._M_refcount);
  601. }
  602. private:
  603. void*
  604. _M_get_deleter(const std::type_info& __ti) const
  605. { return _M_refcount._M_get_deleter(__ti); }
  606. template<typename _Tp1, _Lock_policy _Lp1>
  607. bool
  608. _M_less(const __shared_ptr<_Tp1, _Lp1>& __rhs) const
  609. { return _M_refcount < __rhs._M_refcount; }
  610. template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
  611. template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
  612. template<typename _Del, typename _Tp1, _Lock_policy _Lp1>
  613. friend _Del* get_deleter(const __shared_ptr<_Tp1, _Lp1>&);
  614. // Friends injected into enclosing namespace and found by ADL:
  615. template<typename _Tp1>
  616. friend inline bool
  617. operator==(const __shared_ptr& __a, const __shared_ptr<_Tp1, _Lp>& __b)
  618. { return __a.get() == __b.get(); }
  619. template<typename _Tp1>
  620. friend inline bool
  621. operator!=(const __shared_ptr& __a, const __shared_ptr<_Tp1, _Lp>& __b)
  622. { return __a.get() != __b.get(); }
  623. template<typename _Tp1>
  624. friend inline bool
  625. operator<(const __shared_ptr& __a, const __shared_ptr<_Tp1, _Lp>& __b)
  626. { return __a._M_less(__b); }
  627. _Tp* _M_ptr; // Contained pointer.
  628. __shared_count<_Lp> _M_refcount; // Reference counter.
  629. };
  630. // 2.2.3.8 shared_ptr specialized algorithms.
  631. template<typename _Tp, _Lock_policy _Lp>
  632. inline void
  633. swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b)
  634. { __a.swap(__b); }
  635. // 2.2.3.9 shared_ptr casts
  636. /* The seemingly equivalent
  637. * shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))
  638. * will eventually result in undefined behaviour,
  639. * attempting to delete the same object twice.
  640. */
  641. template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
  642. inline __shared_ptr<_Tp, _Lp>
  643. static_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r)
  644. { return __shared_ptr<_Tp, _Lp>(__r, __static_cast_tag()); }
  645. /* The seemingly equivalent
  646. * shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))
  647. * will eventually result in undefined behaviour,
  648. * attempting to delete the same object twice.
  649. */
  650. template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
  651. inline __shared_ptr<_Tp, _Lp>
  652. const_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r)
  653. { return __shared_ptr<_Tp, _Lp>(__r, __const_cast_tag()); }
  654. /* The seemingly equivalent
  655. * shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))
  656. * will eventually result in undefined behaviour,
  657. * attempting to delete the same object twice.
  658. */
  659. template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
  660. inline __shared_ptr<_Tp, _Lp>
  661. dynamic_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r)
  662. { return __shared_ptr<_Tp, _Lp>(__r, __dynamic_cast_tag()); }
  663. // 2.2.3.7 shared_ptr I/O
  664. template<typename _Ch, typename _Tr, typename _Tp, _Lock_policy _Lp>
  665. std::basic_ostream<_Ch, _Tr>&
  666. operator<<(std::basic_ostream<_Ch, _Tr>& __os,
  667. const __shared_ptr<_Tp, _Lp>& __p)
  668. {
  669. __os << __p.get();
  670. return __os;
  671. }
  672. // 2.2.3.10 shared_ptr get_deleter (experimental)
  673. template<typename _Del, typename _Tp, _Lock_policy _Lp>
  674. inline _Del*
  675. get_deleter(const __shared_ptr<_Tp, _Lp>& __p)
  676. {
  677. #if __cpp_rtti
  678. return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del)));
  679. #else
  680. return 0;
  681. #endif
  682. }
  683. template<typename _Tp, _Lock_policy _Lp>
  684. class __weak_ptr
  685. {
  686. public:
  687. typedef _Tp element_type;
  688. __weak_ptr()
  689. : _M_ptr(0), _M_refcount() // never throws
  690. { }
  691. // Generated copy constructor, assignment, destructor are fine.
  692. // The "obvious" converting constructor implementation:
  693. //
  694. // template<typename _Tp1>
  695. // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
  696. // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
  697. // { }
  698. //
  699. // has a serious problem.
  700. //
  701. // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
  702. // conversion may require access to *__r._M_ptr (virtual inheritance).
  703. //
  704. // It is not possible to avoid spurious access violations since
  705. // in multithreaded programs __r._M_ptr may be invalidated at any point.
  706. template<typename _Tp1>
  707. __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
  708. : _M_refcount(__r._M_refcount) // never throws
  709. {
  710. __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
  711. _M_ptr = __r.lock().get();
  712. }
  713. template<typename _Tp1>
  714. __weak_ptr(const __shared_ptr<_Tp1, _Lp>& __r)
  715. : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
  716. { __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) }
  717. template<typename _Tp1>
  718. __weak_ptr&
  719. operator=(const __weak_ptr<_Tp1, _Lp>& __r) // never throws
  720. {
  721. _M_ptr = __r.lock().get();
  722. _M_refcount = __r._M_refcount;
  723. return *this;
  724. }
  725. template<typename _Tp1>
  726. __weak_ptr&
  727. operator=(const __shared_ptr<_Tp1, _Lp>& __r) // never throws
  728. {
  729. _M_ptr = __r._M_ptr;
  730. _M_refcount = __r._M_refcount;
  731. return *this;
  732. }
  733. __shared_ptr<_Tp, _Lp>
  734. lock() const // never throws
  735. {
  736. #ifdef __GTHREADS
  737. // Optimization: avoid throw overhead.
  738. if (expired())
  739. return __shared_ptr<element_type, _Lp>();
  740. __try
  741. {
  742. return __shared_ptr<element_type, _Lp>(*this);
  743. }
  744. __catch(const bad_weak_ptr&)
  745. {
  746. // Q: How can we get here?
  747. // A: Another thread may have invalidated r after the
  748. // use_count test above.
  749. return __shared_ptr<element_type, _Lp>();
  750. }
  751. #else
  752. // Optimization: avoid try/catch overhead when single threaded.
  753. return expired() ? __shared_ptr<element_type, _Lp>()
  754. : __shared_ptr<element_type, _Lp>(*this);
  755. #endif
  756. } // XXX MT
  757. long
  758. use_count() const // never throws
  759. { return _M_refcount._M_get_use_count(); }
  760. bool
  761. expired() const // never throws
  762. { return _M_refcount._M_get_use_count() == 0; }
  763. void
  764. reset() // never throws
  765. { __weak_ptr().swap(*this); }
  766. void
  767. swap(__weak_ptr& __s) // never throws
  768. {
  769. std::swap(_M_ptr, __s._M_ptr);
  770. _M_refcount._M_swap(__s._M_refcount);
  771. }
  772. private:
  773. // Used by __enable_shared_from_this.
  774. void
  775. _M_assign(_Tp* __ptr, const __shared_count<_Lp>& __refcount)
  776. {
  777. _M_ptr = __ptr;
  778. _M_refcount = __refcount;
  779. }
  780. template<typename _Tp1>
  781. bool
  782. _M_less(const __weak_ptr<_Tp1, _Lp>& __rhs) const
  783. { return _M_refcount < __rhs._M_refcount; }
  784. template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
  785. template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
  786. friend class __enable_shared_from_this<_Tp, _Lp>;
  787. friend class enable_shared_from_this<_Tp>;
  788. // Friend injected into namespace and found by ADL.
  789. template<typename _Tp1>
  790. friend inline bool
  791. operator<(const __weak_ptr& __lhs, const __weak_ptr<_Tp1, _Lp>& __rhs)
  792. { return __lhs._M_less(__rhs); }
  793. _Tp* _M_ptr; // Contained pointer.
  794. __weak_count<_Lp> _M_refcount; // Reference counter.
  795. };
  796. // 2.2.4.7 weak_ptr specialized algorithms.
  797. template<typename _Tp, _Lock_policy _Lp>
  798. inline void
  799. swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b)
  800. { __a.swap(__b); }
  801. template<typename _Tp, _Lock_policy _Lp>
  802. class __enable_shared_from_this
  803. {
  804. protected:
  805. __enable_shared_from_this() { }
  806. __enable_shared_from_this(const __enable_shared_from_this&) { }
  807. __enable_shared_from_this&
  808. operator=(const __enable_shared_from_this&)
  809. { return *this; }
  810. ~__enable_shared_from_this() { }
  811. public:
  812. __shared_ptr<_Tp, _Lp>
  813. shared_from_this()
  814. { return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
  815. __shared_ptr<const _Tp, _Lp>
  816. shared_from_this() const
  817. { return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
  818. private:
  819. template<typename _Tp1>
  820. void
  821. _M_weak_assign(_Tp1* __p, const __shared_count<_Lp>& __n) const
  822. { _M_weak_this._M_assign(__p, __n); }
  823. template<typename _Tp1>
  824. friend void
  825. __enable_shared_from_this_helper(const __shared_count<_Lp>& __pn,
  826. const __enable_shared_from_this* __pe,
  827. const _Tp1* __px)
  828. {
  829. if (__pe != 0)
  830. __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
  831. }
  832. mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
  833. };
  834. // The actual shared_ptr, with forwarding constructors and
  835. // assignment operators.
  836. template<typename _Tp>
  837. class shared_ptr
  838. : public __shared_ptr<_Tp>
  839. {
  840. public:
  841. shared_ptr()
  842. : __shared_ptr<_Tp>() { }
  843. template<typename _Tp1>
  844. explicit
  845. shared_ptr(_Tp1* __p)
  846. : __shared_ptr<_Tp>(__p) { }
  847. template<typename _Tp1, typename _Deleter>
  848. shared_ptr(_Tp1* __p, _Deleter __d)
  849. : __shared_ptr<_Tp>(__p, __d) { }
  850. template<typename _Tp1>
  851. shared_ptr(const shared_ptr<_Tp1>& __r)
  852. : __shared_ptr<_Tp>(__r) { }
  853. template<typename _Tp1>
  854. explicit
  855. shared_ptr(const weak_ptr<_Tp1>& __r)
  856. : __shared_ptr<_Tp>(__r) { }
  857. #if (__cplusplus < 201103L) || _GLIBCXX_USE_DEPRECATED
  858. #pragma GCC diagnostic push
  859. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  860. template<typename _Tp1>
  861. explicit
  862. shared_ptr(std::auto_ptr<_Tp1>& __r)
  863. : __shared_ptr<_Tp>(__r) { }
  864. #pragma GCC diagnostic pop
  865. #endif
  866. template<typename _Tp1>
  867. shared_ptr(const shared_ptr<_Tp1>& __r, __static_cast_tag)
  868. : __shared_ptr<_Tp>(__r, __static_cast_tag()) { }
  869. template<typename _Tp1>
  870. shared_ptr(const shared_ptr<_Tp1>& __r, __const_cast_tag)
  871. : __shared_ptr<_Tp>(__r, __const_cast_tag()) { }
  872. template<typename _Tp1>
  873. shared_ptr(const shared_ptr<_Tp1>& __r, __dynamic_cast_tag)
  874. : __shared_ptr<_Tp>(__r, __dynamic_cast_tag()) { }
  875. template<typename _Tp1>
  876. shared_ptr&
  877. operator=(const shared_ptr<_Tp1>& __r) // never throws
  878. {
  879. this->__shared_ptr<_Tp>::operator=(__r);
  880. return *this;
  881. }
  882. #if (__cplusplus < 201103L) || _GLIBCXX_USE_DEPRECATED
  883. #pragma GCC diagnostic push
  884. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  885. template<typename _Tp1>
  886. shared_ptr&
  887. operator=(std::auto_ptr<_Tp1>& __r)
  888. {
  889. this->__shared_ptr<_Tp>::operator=(__r);
  890. return *this;
  891. }
  892. #pragma GCC diagnostic pop
  893. #endif
  894. };
  895. // 2.2.3.8 shared_ptr specialized algorithms.
  896. template<typename _Tp>
  897. inline void
  898. swap(__shared_ptr<_Tp>& __a, __shared_ptr<_Tp>& __b)
  899. { __a.swap(__b); }
  900. template<typename _Tp, typename _Tp1>
  901. inline shared_ptr<_Tp>
  902. static_pointer_cast(const shared_ptr<_Tp1>& __r)
  903. { return shared_ptr<_Tp>(__r, __static_cast_tag()); }
  904. template<typename _Tp, typename _Tp1>
  905. inline shared_ptr<_Tp>
  906. const_pointer_cast(const shared_ptr<_Tp1>& __r)
  907. { return shared_ptr<_Tp>(__r, __const_cast_tag()); }
  908. template<typename _Tp, typename _Tp1>
  909. inline shared_ptr<_Tp>
  910. dynamic_pointer_cast(const shared_ptr<_Tp1>& __r)
  911. { return shared_ptr<_Tp>(__r, __dynamic_cast_tag()); }
  912. // The actual weak_ptr, with forwarding constructors and
  913. // assignment operators.
  914. template<typename _Tp>
  915. class weak_ptr
  916. : public __weak_ptr<_Tp>
  917. {
  918. public:
  919. weak_ptr()
  920. : __weak_ptr<_Tp>() { }
  921. template<typename _Tp1>
  922. weak_ptr(const weak_ptr<_Tp1>& __r)
  923. : __weak_ptr<_Tp>(__r) { }
  924. template<typename _Tp1>
  925. weak_ptr(const shared_ptr<_Tp1>& __r)
  926. : __weak_ptr<_Tp>(__r) { }
  927. template<typename _Tp1>
  928. weak_ptr&
  929. operator=(const weak_ptr<_Tp1>& __r) // never throws
  930. {
  931. this->__weak_ptr<_Tp>::operator=(__r);
  932. return *this;
  933. }
  934. template<typename _Tp1>
  935. weak_ptr&
  936. operator=(const shared_ptr<_Tp1>& __r) // never throws
  937. {
  938. this->__weak_ptr<_Tp>::operator=(__r);
  939. return *this;
  940. }
  941. shared_ptr<_Tp>
  942. lock() const // never throws
  943. {
  944. #ifdef __GTHREADS
  945. if (this->expired())
  946. return shared_ptr<_Tp>();
  947. __try
  948. {
  949. return shared_ptr<_Tp>(*this);
  950. }
  951. __catch(const bad_weak_ptr&)
  952. {
  953. return shared_ptr<_Tp>();
  954. }
  955. #else
  956. return this->expired() ? shared_ptr<_Tp>()
  957. : shared_ptr<_Tp>(*this);
  958. #endif
  959. }
  960. };
  961. template<typename _Tp>
  962. class enable_shared_from_this
  963. {
  964. protected:
  965. enable_shared_from_this() { }
  966. enable_shared_from_this(const enable_shared_from_this&) { }
  967. enable_shared_from_this&
  968. operator=(const enable_shared_from_this&)
  969. { return *this; }
  970. ~enable_shared_from_this() { }
  971. public:
  972. shared_ptr<_Tp>
  973. shared_from_this()
  974. { return shared_ptr<_Tp>(this->_M_weak_this); }
  975. shared_ptr<const _Tp>
  976. shared_from_this() const
  977. { return shared_ptr<const _Tp>(this->_M_weak_this); }
  978. private:
  979. template<typename _Tp1>
  980. void
  981. _M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const
  982. { _M_weak_this._M_assign(__p, __n); }
  983. template<typename _Tp1>
  984. friend void
  985. __enable_shared_from_this_helper(const __shared_count<>& __pn,
  986. const enable_shared_from_this* __pe,
  987. const _Tp1* __px)
  988. {
  989. if (__pe != 0)
  990. __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
  991. }
  992. mutable weak_ptr<_Tp> _M_weak_this;
  993. };
  994. }
  995. _GLIBCXX_END_NAMESPACE_VERSION
  996. }
  997. #endif // _TR1_SHARED_PTR_H