shared_ptr.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. // shared_ptr and weak_ptr implementation -*- C++ -*-
  2. // Copyright (C) 2007-2021 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. // GCC Note: Based on files from version 1.32.0 of the Boost library.
  21. // shared_count.hpp
  22. // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
  23. // shared_ptr.hpp
  24. // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
  25. // Copyright (C) 2001, 2002, 2003 Peter Dimov
  26. // weak_ptr.hpp
  27. // Copyright (C) 2001, 2002, 2003 Peter Dimov
  28. // enable_shared_from_this.hpp
  29. // Copyright (C) 2002 Peter Dimov
  30. // Distributed under the Boost Software License, Version 1.0. (See
  31. // accompanying file LICENSE_1_0.txt or copy at
  32. // http://www.boost.org/LICENSE_1_0.txt)
  33. /** @file
  34. * This is an internal header file, included by other library headers.
  35. * Do not attempt to use it directly. @headername{memory}
  36. */
  37. #ifndef _SHARED_PTR_H
  38. #define _SHARED_PTR_H 1
  39. #include <iosfwd> // std::basic_ostream
  40. #include <bits/shared_ptr_base.h>
  41. namespace std _GLIBCXX_VISIBILITY(default)
  42. {
  43. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  44. /**
  45. * @addtogroup pointer_abstractions
  46. * @{
  47. */
  48. // 20.7.2.2.11 shared_ptr I/O
  49. /// Write the stored pointer to an ostream.
  50. /// @relates shared_ptr
  51. template<typename _Ch, typename _Tr, typename _Tp, _Lock_policy _Lp>
  52. inline std::basic_ostream<_Ch, _Tr>&
  53. operator<<(std::basic_ostream<_Ch, _Tr>& __os,
  54. const __shared_ptr<_Tp, _Lp>& __p)
  55. {
  56. __os << __p.get();
  57. return __os;
  58. }
  59. template<typename _Del, typename _Tp, _Lock_policy _Lp>
  60. inline _Del*
  61. get_deleter(const __shared_ptr<_Tp, _Lp>& __p) noexcept
  62. {
  63. #if __cpp_rtti
  64. return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del)));
  65. #else
  66. return 0;
  67. #endif
  68. }
  69. /// 20.7.2.2.10 shared_ptr get_deleter
  70. /// If `__p` has a deleter of type `_Del`, return a pointer to it.
  71. /// @relates shared_ptr
  72. template<typename _Del, typename _Tp>
  73. inline _Del*
  74. get_deleter(const shared_ptr<_Tp>& __p) noexcept
  75. {
  76. #if __cpp_rtti
  77. return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del)));
  78. #else
  79. return 0;
  80. #endif
  81. }
  82. /**
  83. * @brief A smart pointer with reference-counted copy semantics.
  84. *
  85. * A `shared_ptr` object is either empty or _owns_ a pointer passed
  86. * to the constructor. Copies of a `shared_ptr` share ownership of
  87. * the same pointer. When the last `shared_ptr` that owns the pointer
  88. * is destroyed or reset, the owned pointer is freed (either by `delete`
  89. * or by invoking a custom deleter that was passed to the constructor).
  90. *
  91. * A `shared_ptr` also stores another pointer, which is usually
  92. * (but not always) the same pointer as it owns. The stored pointer
  93. * can be retrieved by calling the `get()` member function.
  94. *
  95. * The equality and relational operators for `shared_ptr` only compare
  96. * the stored pointer returned by `get()`, not the owned pointer.
  97. * To test whether two `shared_ptr` objects share ownership of the same
  98. * pointer see `std::shared_ptr::owner_before` and `std::owner_less`.
  99. */
  100. template<typename _Tp>
  101. class shared_ptr : public __shared_ptr<_Tp>
  102. {
  103. template<typename... _Args>
  104. using _Constructible = typename enable_if<
  105. is_constructible<__shared_ptr<_Tp>, _Args...>::value
  106. >::type;
  107. template<typename _Arg>
  108. using _Assignable = typename enable_if<
  109. is_assignable<__shared_ptr<_Tp>&, _Arg>::value, shared_ptr&
  110. >::type;
  111. public:
  112. /// The type pointed to by the stored pointer, remove_extent_t<_Tp>
  113. using element_type = typename __shared_ptr<_Tp>::element_type;
  114. #if __cplusplus >= 201703L
  115. # define __cpp_lib_shared_ptr_weak_type 201606
  116. /// The corresponding weak_ptr type for this shared_ptr
  117. using weak_type = weak_ptr<_Tp>;
  118. #endif
  119. /**
  120. * @brief Construct an empty %shared_ptr.
  121. * @post use_count()==0 && get()==0
  122. */
  123. constexpr shared_ptr() noexcept : __shared_ptr<_Tp>() { }
  124. shared_ptr(const shared_ptr&) noexcept = default; ///< Copy constructor
  125. /**
  126. * @brief Construct a %shared_ptr that owns the pointer @a __p.
  127. * @param __p A pointer that is convertible to element_type*.
  128. * @post use_count() == 1 && get() == __p
  129. * @throw std::bad_alloc, in which case @c delete @a __p is called.
  130. */
  131. template<typename _Yp, typename = _Constructible<_Yp*>>
  132. explicit
  133. shared_ptr(_Yp* __p) : __shared_ptr<_Tp>(__p) { }
  134. /**
  135. * @brief Construct a %shared_ptr that owns the pointer @a __p
  136. * and the deleter @a __d.
  137. * @param __p A pointer.
  138. * @param __d A deleter.
  139. * @post use_count() == 1 && get() == __p
  140. * @throw std::bad_alloc, in which case @a __d(__p) is called.
  141. *
  142. * Requirements: _Deleter's copy constructor and destructor must
  143. * not throw
  144. *
  145. * __shared_ptr will release __p by calling __d(__p)
  146. */
  147. template<typename _Yp, typename _Deleter,
  148. typename = _Constructible<_Yp*, _Deleter>>
  149. shared_ptr(_Yp* __p, _Deleter __d)
  150. : __shared_ptr<_Tp>(__p, std::move(__d)) { }
  151. /**
  152. * @brief Construct a %shared_ptr that owns a null pointer
  153. * and the deleter @a __d.
  154. * @param __p A null pointer constant.
  155. * @param __d A deleter.
  156. * @post use_count() == 1 && get() == __p
  157. * @throw std::bad_alloc, in which case @a __d(__p) is called.
  158. *
  159. * Requirements: _Deleter's copy constructor and destructor must
  160. * not throw
  161. *
  162. * The last owner will call __d(__p)
  163. */
  164. template<typename _Deleter>
  165. shared_ptr(nullptr_t __p, _Deleter __d)
  166. : __shared_ptr<_Tp>(__p, std::move(__d)) { }
  167. /**
  168. * @brief Construct a %shared_ptr that owns the pointer @a __p
  169. * and the deleter @a __d.
  170. * @param __p A pointer.
  171. * @param __d A deleter.
  172. * @param __a An allocator.
  173. * @post use_count() == 1 && get() == __p
  174. * @throw std::bad_alloc, in which case @a __d(__p) is called.
  175. *
  176. * Requirements: _Deleter's copy constructor and destructor must
  177. * not throw _Alloc's copy constructor and destructor must not
  178. * throw.
  179. *
  180. * __shared_ptr will release __p by calling __d(__p)
  181. */
  182. template<typename _Yp, typename _Deleter, typename _Alloc,
  183. typename = _Constructible<_Yp*, _Deleter, _Alloc>>
  184. shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a)
  185. : __shared_ptr<_Tp>(__p, std::move(__d), std::move(__a)) { }
  186. /**
  187. * @brief Construct a %shared_ptr that owns a null pointer
  188. * and the deleter @a __d.
  189. * @param __p A null pointer constant.
  190. * @param __d A deleter.
  191. * @param __a An allocator.
  192. * @post use_count() == 1 && get() == __p
  193. * @throw std::bad_alloc, in which case @a __d(__p) is called.
  194. *
  195. * Requirements: _Deleter's copy constructor and destructor must
  196. * not throw _Alloc's copy constructor and destructor must not
  197. * throw.
  198. *
  199. * The last owner will call __d(__p)
  200. */
  201. template<typename _Deleter, typename _Alloc>
  202. shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
  203. : __shared_ptr<_Tp>(__p, std::move(__d), std::move(__a)) { }
  204. // Aliasing constructor
  205. /**
  206. * @brief Constructs a `shared_ptr` instance that stores `__p`
  207. * and shares ownership with `__r`.
  208. * @param __r A `shared_ptr`.
  209. * @param __p A pointer that will remain valid while `*__r` is valid.
  210. * @post `get() == __p && use_count() == __r.use_count()`
  211. *
  212. * This can be used to construct a `shared_ptr` to a sub-object
  213. * of an object managed by an existing `shared_ptr`. The complete
  214. * object will remain valid while any `shared_ptr` owns it, even
  215. * if they don't store a pointer to the complete object.
  216. *
  217. * @code
  218. * shared_ptr<pair<int,int>> pii(new pair<int,int>());
  219. * shared_ptr<int> pi(pii, &pii->first);
  220. * assert(pii.use_count() == 2);
  221. * @endcode
  222. */
  223. template<typename _Yp>
  224. shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) noexcept
  225. : __shared_ptr<_Tp>(__r, __p) { }
  226. #if __cplusplus > 201703L
  227. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  228. // 2996. Missing rvalue overloads for shared_ptr operations
  229. /**
  230. * @brief Constructs a `shared_ptr` instance that stores `__p`
  231. * and shares ownership with `__r`.
  232. * @param __r A `shared_ptr`.
  233. * @param __p A pointer that will remain valid while `*__r` is valid.
  234. * @post `get() == __p && !__r.use_count() && !__r.get()`
  235. *
  236. * This can be used to construct a `shared_ptr` to a sub-object
  237. * of an object managed by an existing `shared_ptr`. The complete
  238. * object will remain valid while any `shared_ptr` owns it, even
  239. * if they don't store a pointer to the complete object.
  240. *
  241. * @code
  242. * shared_ptr<pair<int,int>> pii(new pair<int,int>());
  243. * shared_ptr<int> pi1(pii, &pii->first);
  244. * assert(pii.use_count() == 2);
  245. * shared_ptr<int> pi2(std::move(pii), &pii->second);
  246. * assert(pii.use_count() == 0);
  247. * @endcode
  248. */
  249. template<typename _Yp>
  250. shared_ptr(shared_ptr<_Yp>&& __r, element_type* __p) noexcept
  251. : __shared_ptr<_Tp>(std::move(__r), __p) { }
  252. #endif
  253. /**
  254. * @brief If @a __r is empty, constructs an empty %shared_ptr;
  255. * otherwise construct a %shared_ptr that shares ownership
  256. * with @a __r.
  257. * @param __r A %shared_ptr.
  258. * @post get() == __r.get() && use_count() == __r.use_count()
  259. */
  260. template<typename _Yp,
  261. typename = _Constructible<const shared_ptr<_Yp>&>>
  262. shared_ptr(const shared_ptr<_Yp>& __r) noexcept
  263. : __shared_ptr<_Tp>(__r) { }
  264. /**
  265. * @brief Move-constructs a %shared_ptr instance from @a __r.
  266. * @param __r A %shared_ptr rvalue.
  267. * @post *this contains the old value of @a __r, @a __r is empty.
  268. */
  269. shared_ptr(shared_ptr&& __r) noexcept
  270. : __shared_ptr<_Tp>(std::move(__r)) { }
  271. /**
  272. * @brief Move-constructs a %shared_ptr instance from @a __r.
  273. * @param __r A %shared_ptr rvalue.
  274. * @post *this contains the old value of @a __r, @a __r is empty.
  275. */
  276. template<typename _Yp, typename = _Constructible<shared_ptr<_Yp>>>
  277. shared_ptr(shared_ptr<_Yp>&& __r) noexcept
  278. : __shared_ptr<_Tp>(std::move(__r)) { }
  279. /**
  280. * @brief Constructs a %shared_ptr that shares ownership with @a __r
  281. * and stores a copy of the pointer stored in @a __r.
  282. * @param __r A weak_ptr.
  283. * @post use_count() == __r.use_count()
  284. * @throw bad_weak_ptr when __r.expired(),
  285. * in which case the constructor has no effect.
  286. */
  287. template<typename _Yp, typename = _Constructible<const weak_ptr<_Yp>&>>
  288. explicit shared_ptr(const weak_ptr<_Yp>& __r)
  289. : __shared_ptr<_Tp>(__r) { }
  290. #if _GLIBCXX_USE_DEPRECATED
  291. #pragma GCC diagnostic push
  292. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  293. template<typename _Yp, typename = _Constructible<auto_ptr<_Yp>>>
  294. shared_ptr(auto_ptr<_Yp>&& __r);
  295. #pragma GCC diagnostic pop
  296. #endif
  297. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  298. // 2399. shared_ptr's constructor from unique_ptr should be constrained
  299. template<typename _Yp, typename _Del,
  300. typename = _Constructible<unique_ptr<_Yp, _Del>>>
  301. shared_ptr(unique_ptr<_Yp, _Del>&& __r)
  302. : __shared_ptr<_Tp>(std::move(__r)) { }
  303. #if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED
  304. // This non-standard constructor exists to support conversions that
  305. // were possible in C++11 and C++14 but are ill-formed in C++17.
  306. // If an exception is thrown this constructor has no effect.
  307. template<typename _Yp, typename _Del,
  308. _Constructible<unique_ptr<_Yp, _Del>, __sp_array_delete>* = 0>
  309. shared_ptr(unique_ptr<_Yp, _Del>&& __r)
  310. : __shared_ptr<_Tp>(std::move(__r), __sp_array_delete()) { }
  311. #endif
  312. /**
  313. * @brief Construct an empty %shared_ptr.
  314. * @post use_count() == 0 && get() == nullptr
  315. */
  316. constexpr shared_ptr(nullptr_t) noexcept : shared_ptr() { }
  317. shared_ptr& operator=(const shared_ptr&) noexcept = default;
  318. template<typename _Yp>
  319. _Assignable<const shared_ptr<_Yp>&>
  320. operator=(const shared_ptr<_Yp>& __r) noexcept
  321. {
  322. this->__shared_ptr<_Tp>::operator=(__r);
  323. return *this;
  324. }
  325. #if _GLIBCXX_USE_DEPRECATED
  326. #pragma GCC diagnostic push
  327. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  328. template<typename _Yp>
  329. _Assignable<auto_ptr<_Yp>>
  330. operator=(auto_ptr<_Yp>&& __r)
  331. {
  332. this->__shared_ptr<_Tp>::operator=(std::move(__r));
  333. return *this;
  334. }
  335. #pragma GCC diagnostic pop
  336. #endif
  337. shared_ptr&
  338. operator=(shared_ptr&& __r) noexcept
  339. {
  340. this->__shared_ptr<_Tp>::operator=(std::move(__r));
  341. return *this;
  342. }
  343. template<class _Yp>
  344. _Assignable<shared_ptr<_Yp>>
  345. operator=(shared_ptr<_Yp>&& __r) noexcept
  346. {
  347. this->__shared_ptr<_Tp>::operator=(std::move(__r));
  348. return *this;
  349. }
  350. template<typename _Yp, typename _Del>
  351. _Assignable<unique_ptr<_Yp, _Del>>
  352. operator=(unique_ptr<_Yp, _Del>&& __r)
  353. {
  354. this->__shared_ptr<_Tp>::operator=(std::move(__r));
  355. return *this;
  356. }
  357. private:
  358. // This constructor is non-standard, it is used by allocate_shared.
  359. template<typename _Alloc, typename... _Args>
  360. shared_ptr(_Sp_alloc_shared_tag<_Alloc> __tag, _Args&&... __args)
  361. : __shared_ptr<_Tp>(__tag, std::forward<_Args>(__args)...)
  362. { }
  363. template<typename _Yp, typename _Alloc, typename... _Args>
  364. friend shared_ptr<_Yp>
  365. allocate_shared(const _Alloc& __a, _Args&&... __args);
  366. // This constructor is non-standard, it is used by weak_ptr::lock().
  367. shared_ptr(const weak_ptr<_Tp>& __r, std::nothrow_t) noexcept
  368. : __shared_ptr<_Tp>(__r, std::nothrow) { }
  369. friend class weak_ptr<_Tp>;
  370. };
  371. #if __cpp_deduction_guides >= 201606
  372. template<typename _Tp>
  373. shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
  374. template<typename _Tp, typename _Del>
  375. shared_ptr(unique_ptr<_Tp, _Del>) -> shared_ptr<_Tp>;
  376. #endif
  377. // 20.7.2.2.7 shared_ptr comparisons
  378. /// @relates shared_ptr @{
  379. /// Equality operator for shared_ptr objects, compares the stored pointers
  380. template<typename _Tp, typename _Up>
  381. _GLIBCXX_NODISCARD inline bool
  382. operator==(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
  383. { return __a.get() == __b.get(); }
  384. /// shared_ptr comparison with nullptr
  385. template<typename _Tp>
  386. _GLIBCXX_NODISCARD inline bool
  387. operator==(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
  388. { return !__a; }
  389. #ifdef __cpp_lib_three_way_comparison
  390. template<typename _Tp, typename _Up>
  391. inline strong_ordering
  392. operator<=>(const shared_ptr<_Tp>& __a,
  393. const shared_ptr<_Up>& __b) noexcept
  394. { return compare_three_way()(__a.get(), __b.get()); }
  395. template<typename _Tp>
  396. inline strong_ordering
  397. operator<=>(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
  398. {
  399. using pointer = typename shared_ptr<_Tp>::element_type*;
  400. return compare_three_way()(__a.get(), static_cast<pointer>(nullptr));
  401. }
  402. #else
  403. /// shared_ptr comparison with nullptr
  404. template<typename _Tp>
  405. _GLIBCXX_NODISCARD inline bool
  406. operator==(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
  407. { return !__a; }
  408. /// Inequality operator for shared_ptr objects, compares the stored pointers
  409. template<typename _Tp, typename _Up>
  410. _GLIBCXX_NODISCARD inline bool
  411. operator!=(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
  412. { return __a.get() != __b.get(); }
  413. /// shared_ptr comparison with nullptr
  414. template<typename _Tp>
  415. _GLIBCXX_NODISCARD inline bool
  416. operator!=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
  417. { return (bool)__a; }
  418. /// shared_ptr comparison with nullptr
  419. template<typename _Tp>
  420. _GLIBCXX_NODISCARD inline bool
  421. operator!=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
  422. { return (bool)__a; }
  423. /// Relational operator for shared_ptr objects, compares the stored pointers
  424. template<typename _Tp, typename _Up>
  425. _GLIBCXX_NODISCARD inline bool
  426. operator<(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
  427. {
  428. using _Tp_elt = typename shared_ptr<_Tp>::element_type;
  429. using _Up_elt = typename shared_ptr<_Up>::element_type;
  430. using _Vp = typename common_type<_Tp_elt*, _Up_elt*>::type;
  431. return less<_Vp>()(__a.get(), __b.get());
  432. }
  433. /// shared_ptr comparison with nullptr
  434. template<typename _Tp>
  435. _GLIBCXX_NODISCARD inline bool
  436. operator<(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
  437. {
  438. using _Tp_elt = typename shared_ptr<_Tp>::element_type;
  439. return less<_Tp_elt*>()(__a.get(), nullptr);
  440. }
  441. /// shared_ptr comparison with nullptr
  442. template<typename _Tp>
  443. _GLIBCXX_NODISCARD inline bool
  444. operator<(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
  445. {
  446. using _Tp_elt = typename shared_ptr<_Tp>::element_type;
  447. return less<_Tp_elt*>()(nullptr, __a.get());
  448. }
  449. /// Relational operator for shared_ptr objects, compares the stored pointers
  450. template<typename _Tp, typename _Up>
  451. _GLIBCXX_NODISCARD inline bool
  452. operator<=(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
  453. { return !(__b < __a); }
  454. /// shared_ptr comparison with nullptr
  455. template<typename _Tp>
  456. _GLIBCXX_NODISCARD inline bool
  457. operator<=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
  458. { return !(nullptr < __a); }
  459. /// shared_ptr comparison with nullptr
  460. template<typename _Tp>
  461. _GLIBCXX_NODISCARD inline bool
  462. operator<=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
  463. { return !(__a < nullptr); }
  464. /// Relational operator for shared_ptr objects, compares the stored pointers
  465. template<typename _Tp, typename _Up>
  466. _GLIBCXX_NODISCARD inline bool
  467. operator>(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
  468. { return (__b < __a); }
  469. /// shared_ptr comparison with nullptr
  470. template<typename _Tp>
  471. _GLIBCXX_NODISCARD inline bool
  472. operator>(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
  473. { return nullptr < __a; }
  474. /// shared_ptr comparison with nullptr
  475. template<typename _Tp>
  476. _GLIBCXX_NODISCARD inline bool
  477. operator>(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
  478. { return __a < nullptr; }
  479. /// Relational operator for shared_ptr objects, compares the stored pointers
  480. template<typename _Tp, typename _Up>
  481. _GLIBCXX_NODISCARD inline bool
  482. operator>=(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
  483. { return !(__a < __b); }
  484. /// shared_ptr comparison with nullptr
  485. template<typename _Tp>
  486. _GLIBCXX_NODISCARD inline bool
  487. operator>=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
  488. { return !(__a < nullptr); }
  489. /// shared_ptr comparison with nullptr
  490. template<typename _Tp>
  491. _GLIBCXX_NODISCARD inline bool
  492. operator>=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
  493. { return !(nullptr < __a); }
  494. #endif
  495. // 20.7.2.2.8 shared_ptr specialized algorithms.
  496. /// Swap overload for shared_ptr
  497. template<typename _Tp>
  498. inline void
  499. swap(shared_ptr<_Tp>& __a, shared_ptr<_Tp>& __b) noexcept
  500. { __a.swap(__b); }
  501. // 20.7.2.2.9 shared_ptr casts.
  502. /// Convert type of `shared_ptr`, via `static_cast`
  503. template<typename _Tp, typename _Up>
  504. inline shared_ptr<_Tp>
  505. static_pointer_cast(const shared_ptr<_Up>& __r) noexcept
  506. {
  507. using _Sp = shared_ptr<_Tp>;
  508. return _Sp(__r, static_cast<typename _Sp::element_type*>(__r.get()));
  509. }
  510. /// Convert type of `shared_ptr`, via `const_cast`
  511. template<typename _Tp, typename _Up>
  512. inline shared_ptr<_Tp>
  513. const_pointer_cast(const shared_ptr<_Up>& __r) noexcept
  514. {
  515. using _Sp = shared_ptr<_Tp>;
  516. return _Sp(__r, const_cast<typename _Sp::element_type*>(__r.get()));
  517. }
  518. /// Convert type of `shared_ptr`, via `dynamic_cast`
  519. template<typename _Tp, typename _Up>
  520. inline shared_ptr<_Tp>
  521. dynamic_pointer_cast(const shared_ptr<_Up>& __r) noexcept
  522. {
  523. using _Sp = shared_ptr<_Tp>;
  524. if (auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get()))
  525. return _Sp(__r, __p);
  526. return _Sp();
  527. }
  528. #if __cplusplus >= 201703L
  529. /// Convert type of `shared_ptr`, via `reinterpret_cast`
  530. template<typename _Tp, typename _Up>
  531. inline shared_ptr<_Tp>
  532. reinterpret_pointer_cast(const shared_ptr<_Up>& __r) noexcept
  533. {
  534. using _Sp = shared_ptr<_Tp>;
  535. return _Sp(__r, reinterpret_cast<typename _Sp::element_type*>(__r.get()));
  536. }
  537. #if __cplusplus > 201703L
  538. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  539. // 2996. Missing rvalue overloads for shared_ptr operations
  540. /// Convert type of `shared_ptr` rvalue, via `static_cast`
  541. template<typename _Tp, typename _Up>
  542. inline shared_ptr<_Tp>
  543. static_pointer_cast(shared_ptr<_Up>&& __r) noexcept
  544. {
  545. using _Sp = shared_ptr<_Tp>;
  546. return _Sp(std::move(__r),
  547. static_cast<typename _Sp::element_type*>(__r.get()));
  548. }
  549. /// Convert type of `shared_ptr` rvalue, via `const_cast`
  550. template<typename _Tp, typename _Up>
  551. inline shared_ptr<_Tp>
  552. const_pointer_cast(shared_ptr<_Up>&& __r) noexcept
  553. {
  554. using _Sp = shared_ptr<_Tp>;
  555. return _Sp(std::move(__r),
  556. const_cast<typename _Sp::element_type*>(__r.get()));
  557. }
  558. /// Convert type of `shared_ptr` rvalue, via `dynamic_cast`
  559. template<typename _Tp, typename _Up>
  560. inline shared_ptr<_Tp>
  561. dynamic_pointer_cast(shared_ptr<_Up>&& __r) noexcept
  562. {
  563. using _Sp = shared_ptr<_Tp>;
  564. if (auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get()))
  565. return _Sp(std::move(__r), __p);
  566. return _Sp();
  567. }
  568. /// Convert type of `shared_ptr` rvalue, via `reinterpret_cast`
  569. template<typename _Tp, typename _Up>
  570. inline shared_ptr<_Tp>
  571. reinterpret_pointer_cast(shared_ptr<_Up>&& __r) noexcept
  572. {
  573. using _Sp = shared_ptr<_Tp>;
  574. return _Sp(std::move(__r),
  575. reinterpret_cast<typename _Sp::element_type*>(__r.get()));
  576. }
  577. #endif // C++20
  578. #endif // C++17
  579. /// @}
  580. /**
  581. * @brief A non-owning observer for a pointer owned by a shared_ptr
  582. *
  583. * A weak_ptr provides a safe alternative to a raw pointer when you want
  584. * a non-owning reference to an object that is managed by a shared_ptr.
  585. *
  586. * Unlike a raw pointer, a weak_ptr can be converted to a new shared_ptr
  587. * that shares ownership with every other shared_ptr that already owns
  588. * the pointer. In other words you can upgrade from a non-owning "weak"
  589. * reference to an owning shared_ptr, without having access to any of
  590. * the existing shared_ptr objects.
  591. *
  592. * Also unlike a raw pointer, a weak_ptr does not become "dangling" after
  593. * the object it points to has been destroyed. Instead, a weak_ptr
  594. * becomes _expired_ and can no longer be converted to a shared_ptr that
  595. * owns the freed pointer, so you cannot accidentally access the pointed-to
  596. * object after it has been destroyed.
  597. */
  598. template<typename _Tp>
  599. class weak_ptr : public __weak_ptr<_Tp>
  600. {
  601. template<typename _Arg>
  602. using _Constructible = typename enable_if<
  603. is_constructible<__weak_ptr<_Tp>, _Arg>::value
  604. >::type;
  605. template<typename _Arg>
  606. using _Assignable = typename enable_if<
  607. is_assignable<__weak_ptr<_Tp>&, _Arg>::value, weak_ptr&
  608. >::type;
  609. public:
  610. constexpr weak_ptr() noexcept = default;
  611. template<typename _Yp,
  612. typename = _Constructible<const shared_ptr<_Yp>&>>
  613. weak_ptr(const shared_ptr<_Yp>& __r) noexcept
  614. : __weak_ptr<_Tp>(__r) { }
  615. weak_ptr(const weak_ptr&) noexcept = default;
  616. template<typename _Yp, typename = _Constructible<const weak_ptr<_Yp>&>>
  617. weak_ptr(const weak_ptr<_Yp>& __r) noexcept
  618. : __weak_ptr<_Tp>(__r) { }
  619. weak_ptr(weak_ptr&&) noexcept = default;
  620. template<typename _Yp, typename = _Constructible<weak_ptr<_Yp>>>
  621. weak_ptr(weak_ptr<_Yp>&& __r) noexcept
  622. : __weak_ptr<_Tp>(std::move(__r)) { }
  623. weak_ptr&
  624. operator=(const weak_ptr& __r) noexcept = default;
  625. template<typename _Yp>
  626. _Assignable<const weak_ptr<_Yp>&>
  627. operator=(const weak_ptr<_Yp>& __r) noexcept
  628. {
  629. this->__weak_ptr<_Tp>::operator=(__r);
  630. return *this;
  631. }
  632. template<typename _Yp>
  633. _Assignable<const shared_ptr<_Yp>&>
  634. operator=(const shared_ptr<_Yp>& __r) noexcept
  635. {
  636. this->__weak_ptr<_Tp>::operator=(__r);
  637. return *this;
  638. }
  639. weak_ptr&
  640. operator=(weak_ptr&& __r) noexcept = default;
  641. template<typename _Yp>
  642. _Assignable<weak_ptr<_Yp>>
  643. operator=(weak_ptr<_Yp>&& __r) noexcept
  644. {
  645. this->__weak_ptr<_Tp>::operator=(std::move(__r));
  646. return *this;
  647. }
  648. shared_ptr<_Tp>
  649. lock() const noexcept
  650. { return shared_ptr<_Tp>(*this, std::nothrow); }
  651. };
  652. #if __cpp_deduction_guides >= 201606
  653. template<typename _Tp>
  654. weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
  655. #endif
  656. // 20.7.2.3.6 weak_ptr specialized algorithms.
  657. /// Swap overload for weak_ptr
  658. /// @relates weak_ptr
  659. template<typename _Tp>
  660. inline void
  661. swap(weak_ptr<_Tp>& __a, weak_ptr<_Tp>& __b) noexcept
  662. { __a.swap(__b); }
  663. /// Primary template owner_less
  664. template<typename _Tp = void>
  665. struct owner_less;
  666. /// Void specialization of owner_less compares either shared_ptr or weak_ptr
  667. template<>
  668. struct owner_less<void> : _Sp_owner_less<void, void>
  669. { };
  670. /// Partial specialization of owner_less for shared_ptr.
  671. template<typename _Tp>
  672. struct owner_less<shared_ptr<_Tp>>
  673. : public _Sp_owner_less<shared_ptr<_Tp>, weak_ptr<_Tp>>
  674. { };
  675. /// Partial specialization of owner_less for weak_ptr.
  676. template<typename _Tp>
  677. struct owner_less<weak_ptr<_Tp>>
  678. : public _Sp_owner_less<weak_ptr<_Tp>, shared_ptr<_Tp>>
  679. { };
  680. /**
  681. * @brief Base class allowing use of member function shared_from_this.
  682. */
  683. template<typename _Tp>
  684. class enable_shared_from_this
  685. {
  686. protected:
  687. constexpr enable_shared_from_this() noexcept { }
  688. enable_shared_from_this(const enable_shared_from_this&) noexcept { }
  689. enable_shared_from_this&
  690. operator=(const enable_shared_from_this&) noexcept
  691. { return *this; }
  692. ~enable_shared_from_this() { }
  693. public:
  694. shared_ptr<_Tp>
  695. shared_from_this()
  696. { return shared_ptr<_Tp>(this->_M_weak_this); }
  697. shared_ptr<const _Tp>
  698. shared_from_this() const
  699. { return shared_ptr<const _Tp>(this->_M_weak_this); }
  700. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  701. #define __cpp_lib_enable_shared_from_this 201603
  702. weak_ptr<_Tp>
  703. weak_from_this() noexcept
  704. { return this->_M_weak_this; }
  705. weak_ptr<const _Tp>
  706. weak_from_this() const noexcept
  707. { return this->_M_weak_this; }
  708. #endif
  709. private:
  710. template<typename _Tp1>
  711. void
  712. _M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const noexcept
  713. { _M_weak_this._M_assign(__p, __n); }
  714. // Found by ADL when this is an associated class.
  715. friend const enable_shared_from_this*
  716. __enable_shared_from_this_base(const __shared_count<>&,
  717. const enable_shared_from_this* __p)
  718. { return __p; }
  719. template<typename, _Lock_policy>
  720. friend class __shared_ptr;
  721. mutable weak_ptr<_Tp> _M_weak_this;
  722. };
  723. /// @relates shared_ptr @{
  724. /**
  725. * @brief Create an object that is owned by a shared_ptr.
  726. * @param __a An allocator.
  727. * @param __args Arguments for the @a _Tp object's constructor.
  728. * @return A shared_ptr that owns the newly created object.
  729. * @throw An exception thrown from @a _Alloc::allocate or from the
  730. * constructor of @a _Tp.
  731. *
  732. * A copy of @a __a will be used to allocate memory for the shared_ptr
  733. * and the new object.
  734. */
  735. template<typename _Tp, typename _Alloc, typename... _Args>
  736. inline shared_ptr<_Tp>
  737. allocate_shared(const _Alloc& __a, _Args&&... __args)
  738. {
  739. return shared_ptr<_Tp>(_Sp_alloc_shared_tag<_Alloc>{__a},
  740. std::forward<_Args>(__args)...);
  741. }
  742. /**
  743. * @brief Create an object that is owned by a shared_ptr.
  744. * @param __args Arguments for the @a _Tp object's constructor.
  745. * @return A shared_ptr that owns the newly created object.
  746. * @throw std::bad_alloc, or an exception thrown from the
  747. * constructor of @a _Tp.
  748. */
  749. template<typename _Tp, typename... _Args>
  750. inline shared_ptr<_Tp>
  751. make_shared(_Args&&... __args)
  752. {
  753. typedef typename std::remove_cv<_Tp>::type _Tp_nc;
  754. return std::allocate_shared<_Tp>(std::allocator<_Tp_nc>(),
  755. std::forward<_Args>(__args)...);
  756. }
  757. /// std::hash specialization for shared_ptr.
  758. template<typename _Tp>
  759. struct hash<shared_ptr<_Tp>>
  760. : public __hash_base<size_t, shared_ptr<_Tp>>
  761. {
  762. size_t
  763. operator()(const shared_ptr<_Tp>& __s) const noexcept
  764. {
  765. return std::hash<typename shared_ptr<_Tp>::element_type*>()(__s.get());
  766. }
  767. };
  768. /// @} relates shared_ptr
  769. /// @} group pointer_abstractions
  770. #if __cplusplus >= 201703L
  771. namespace __detail::__variant
  772. {
  773. template<typename> struct _Never_valueless_alt; // see <variant>
  774. // Provide the strong exception-safety guarantee when emplacing a
  775. // shared_ptr into a variant.
  776. template<typename _Tp>
  777. struct _Never_valueless_alt<std::shared_ptr<_Tp>>
  778. : std::true_type
  779. { };
  780. // Provide the strong exception-safety guarantee when emplacing a
  781. // weak_ptr into a variant.
  782. template<typename _Tp>
  783. struct _Never_valueless_alt<std::weak_ptr<_Tp>>
  784. : std::true_type
  785. { };
  786. } // namespace __detail::__variant
  787. #endif // C++17
  788. _GLIBCXX_END_NAMESPACE_VERSION
  789. } // namespace
  790. #endif // _SHARED_PTR_H