You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

919 lines
30KB

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