shared_ptr.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. // shared_ptr and weak_ptr implementation -*- C++ -*-
  2. // Copyright (C) 2007-2018 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. template<typename _Ch, typename _Tr, typename _Tp, _Lock_policy _Lp>
  49. inline std::basic_ostream<_Ch, _Tr>&
  50. operator<<(std::basic_ostream<_Ch, _Tr>& __os,
  51. const __shared_ptr<_Tp, _Lp>& __p)
  52. {
  53. __os << __p.get();
  54. return __os;
  55. }
  56. template<typename _Del, typename _Tp, _Lock_policy _Lp>
  57. inline _Del*
  58. get_deleter(const __shared_ptr<_Tp, _Lp>& __p) noexcept
  59. {
  60. #if __cpp_rtti
  61. return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del)));
  62. #else
  63. return 0;
  64. #endif
  65. }
  66. /// 20.7.2.2.10 shared_ptr get_deleter
  67. template<typename _Del, typename _Tp>
  68. inline _Del*
  69. get_deleter(const shared_ptr<_Tp>& __p) noexcept
  70. {
  71. #if __cpp_rtti
  72. return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del)));
  73. #else
  74. return 0;
  75. #endif
  76. }
  77. /**
  78. * @brief A smart pointer with reference-counted copy semantics.
  79. *
  80. * The object pointed to is deleted when the last shared_ptr pointing to
  81. * it is destroyed or reset.
  82. */
  83. template<typename _Tp>
  84. class shared_ptr : public __shared_ptr<_Tp>
  85. {
  86. template<typename... _Args>
  87. using _Constructible = typename enable_if<
  88. is_constructible<__shared_ptr<_Tp>, _Args...>::value
  89. >::type;
  90. template<typename _Arg>
  91. using _Assignable = typename enable_if<
  92. is_assignable<__shared_ptr<_Tp>&, _Arg>::value, shared_ptr&
  93. >::type;
  94. public:
  95. using element_type = typename __shared_ptr<_Tp>::element_type;
  96. #if __cplusplus > 201402L
  97. # define __cpp_lib_shared_ptr_weak_type 201606
  98. using weak_type = weak_ptr<_Tp>;
  99. #endif
  100. /**
  101. * @brief Construct an empty %shared_ptr.
  102. * @post use_count()==0 && get()==0
  103. */
  104. constexpr shared_ptr() noexcept : __shared_ptr<_Tp>() { }
  105. shared_ptr(const shared_ptr&) noexcept = default;
  106. /**
  107. * @brief Construct a %shared_ptr that owns the pointer @a __p.
  108. * @param __p A pointer that is convertible to element_type*.
  109. * @post use_count() == 1 && get() == __p
  110. * @throw std::bad_alloc, in which case @c delete @a __p is called.
  111. */
  112. template<typename _Yp, typename = _Constructible<_Yp*>>
  113. explicit
  114. shared_ptr(_Yp* __p) : __shared_ptr<_Tp>(__p) { }
  115. /**
  116. * @brief Construct a %shared_ptr that owns the pointer @a __p
  117. * and the deleter @a __d.
  118. * @param __p A pointer.
  119. * @param __d A deleter.
  120. * @post use_count() == 1 && get() == __p
  121. * @throw std::bad_alloc, in which case @a __d(__p) is called.
  122. *
  123. * Requirements: _Deleter's copy constructor and destructor must
  124. * not throw
  125. *
  126. * __shared_ptr will release __p by calling __d(__p)
  127. */
  128. template<typename _Yp, typename _Deleter,
  129. typename = _Constructible<_Yp*, _Deleter>>
  130. shared_ptr(_Yp* __p, _Deleter __d)
  131. : __shared_ptr<_Tp>(__p, std::move(__d)) { }
  132. /**
  133. * @brief Construct a %shared_ptr that owns a null pointer
  134. * and the deleter @a __d.
  135. * @param __p A null pointer constant.
  136. * @param __d A deleter.
  137. * @post use_count() == 1 && get() == __p
  138. * @throw std::bad_alloc, in which case @a __d(__p) is called.
  139. *
  140. * Requirements: _Deleter's copy constructor and destructor must
  141. * not throw
  142. *
  143. * The last owner will call __d(__p)
  144. */
  145. template<typename _Deleter>
  146. shared_ptr(nullptr_t __p, _Deleter __d)
  147. : __shared_ptr<_Tp>(__p, std::move(__d)) { }
  148. /**
  149. * @brief Construct a %shared_ptr that owns the pointer @a __p
  150. * and the deleter @a __d.
  151. * @param __p A pointer.
  152. * @param __d A deleter.
  153. * @param __a An allocator.
  154. * @post use_count() == 1 && get() == __p
  155. * @throw std::bad_alloc, in which case @a __d(__p) is called.
  156. *
  157. * Requirements: _Deleter's copy constructor and destructor must
  158. * not throw _Alloc's copy constructor and destructor must not
  159. * throw.
  160. *
  161. * __shared_ptr will release __p by calling __d(__p)
  162. */
  163. template<typename _Yp, typename _Deleter, typename _Alloc,
  164. typename = _Constructible<_Yp*, _Deleter, _Alloc>>
  165. shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a)
  166. : __shared_ptr<_Tp>(__p, std::move(__d), std::move(__a)) { }
  167. /**
  168. * @brief Construct a %shared_ptr that owns a null pointer
  169. * and the deleter @a __d.
  170. * @param __p A null pointer constant.
  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. * The last owner will call __d(__p)
  181. */
  182. template<typename _Deleter, typename _Alloc>
  183. shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
  184. : __shared_ptr<_Tp>(__p, std::move(__d), std::move(__a)) { }
  185. // Aliasing constructor
  186. /**
  187. * @brief Constructs a %shared_ptr instance that stores @a __p
  188. * and shares ownership with @a __r.
  189. * @param __r A %shared_ptr.
  190. * @param __p A pointer that will remain valid while @a *__r is valid.
  191. * @post get() == __p && use_count() == __r.use_count()
  192. *
  193. * This can be used to construct a @c shared_ptr to a sub-object
  194. * of an object managed by an existing @c shared_ptr.
  195. *
  196. * @code
  197. * shared_ptr< pair<int,int> > pii(new pair<int,int>());
  198. * shared_ptr<int> pi(pii, &pii->first);
  199. * assert(pii.use_count() == 2);
  200. * @endcode
  201. */
  202. template<typename _Yp>
  203. shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) noexcept
  204. : __shared_ptr<_Tp>(__r, __p) { }
  205. /**
  206. * @brief If @a __r is empty, constructs an empty %shared_ptr;
  207. * otherwise construct a %shared_ptr that shares ownership
  208. * with @a __r.
  209. * @param __r A %shared_ptr.
  210. * @post get() == __r.get() && use_count() == __r.use_count()
  211. */
  212. template<typename _Yp,
  213. typename = _Constructible<const shared_ptr<_Yp>&>>
  214. shared_ptr(const shared_ptr<_Yp>& __r) noexcept
  215. : __shared_ptr<_Tp>(__r) { }
  216. /**
  217. * @brief Move-constructs a %shared_ptr instance from @a __r.
  218. * @param __r A %shared_ptr rvalue.
  219. * @post *this contains the old value of @a __r, @a __r is empty.
  220. */
  221. shared_ptr(shared_ptr&& __r) noexcept
  222. : __shared_ptr<_Tp>(std::move(__r)) { }
  223. /**
  224. * @brief Move-constructs a %shared_ptr instance from @a __r.
  225. * @param __r A %shared_ptr rvalue.
  226. * @post *this contains the old value of @a __r, @a __r is empty.
  227. */
  228. template<typename _Yp, typename = _Constructible<shared_ptr<_Yp>>>
  229. shared_ptr(shared_ptr<_Yp>&& __r) noexcept
  230. : __shared_ptr<_Tp>(std::move(__r)) { }
  231. /**
  232. * @brief Constructs a %shared_ptr that shares ownership with @a __r
  233. * and stores a copy of the pointer stored in @a __r.
  234. * @param __r A weak_ptr.
  235. * @post use_count() == __r.use_count()
  236. * @throw bad_weak_ptr when __r.expired(),
  237. * in which case the constructor has no effect.
  238. */
  239. template<typename _Yp, typename = _Constructible<const weak_ptr<_Yp>&>>
  240. explicit shared_ptr(const weak_ptr<_Yp>& __r)
  241. : __shared_ptr<_Tp>(__r) { }
  242. #if _GLIBCXX_USE_DEPRECATED
  243. #pragma GCC diagnostic push
  244. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  245. template<typename _Yp, typename = _Constructible<auto_ptr<_Yp>>>
  246. shared_ptr(auto_ptr<_Yp>&& __r);
  247. #pragma GCC diagnostic pop
  248. #endif
  249. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  250. // 2399. shared_ptr's constructor from unique_ptr should be constrained
  251. template<typename _Yp, typename _Del,
  252. typename = _Constructible<unique_ptr<_Yp, _Del>>>
  253. shared_ptr(unique_ptr<_Yp, _Del>&& __r)
  254. : __shared_ptr<_Tp>(std::move(__r)) { }
  255. #if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED
  256. // This non-standard constructor exists to support conversions that
  257. // were possible in C++11 and C++14 but are ill-formed in C++17.
  258. // If an exception is thrown this constructor has no effect.
  259. template<typename _Yp, typename _Del,
  260. _Constructible<unique_ptr<_Yp, _Del>, __sp_array_delete>* = 0>
  261. shared_ptr(unique_ptr<_Yp, _Del>&& __r)
  262. : __shared_ptr<_Tp>(std::move(__r), __sp_array_delete()) { }
  263. #endif
  264. /**
  265. * @brief Construct an empty %shared_ptr.
  266. * @post use_count() == 0 && get() == nullptr
  267. */
  268. constexpr shared_ptr(nullptr_t) noexcept : shared_ptr() { }
  269. shared_ptr& operator=(const shared_ptr&) noexcept = default;
  270. template<typename _Yp>
  271. _Assignable<const shared_ptr<_Yp>&>
  272. operator=(const shared_ptr<_Yp>& __r) noexcept
  273. {
  274. this->__shared_ptr<_Tp>::operator=(__r);
  275. return *this;
  276. }
  277. #if _GLIBCXX_USE_DEPRECATED
  278. #pragma GCC diagnostic push
  279. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  280. template<typename _Yp>
  281. _Assignable<auto_ptr<_Yp>>
  282. operator=(auto_ptr<_Yp>&& __r)
  283. {
  284. this->__shared_ptr<_Tp>::operator=(std::move(__r));
  285. return *this;
  286. }
  287. #pragma GCC diagnostic pop
  288. #endif
  289. shared_ptr&
  290. operator=(shared_ptr&& __r) noexcept
  291. {
  292. this->__shared_ptr<_Tp>::operator=(std::move(__r));
  293. return *this;
  294. }
  295. template<class _Yp>
  296. _Assignable<shared_ptr<_Yp>>
  297. operator=(shared_ptr<_Yp>&& __r) noexcept
  298. {
  299. this->__shared_ptr<_Tp>::operator=(std::move(__r));
  300. return *this;
  301. }
  302. template<typename _Yp, typename _Del>
  303. _Assignable<unique_ptr<_Yp, _Del>>
  304. operator=(unique_ptr<_Yp, _Del>&& __r)
  305. {
  306. this->__shared_ptr<_Tp>::operator=(std::move(__r));
  307. return *this;
  308. }
  309. private:
  310. // This constructor is non-standard, it is used by allocate_shared.
  311. template<typename _Alloc, typename... _Args>
  312. shared_ptr(_Sp_alloc_shared_tag<_Alloc> __tag, _Args&&... __args)
  313. : __shared_ptr<_Tp>(__tag, std::forward<_Args>(__args)...)
  314. { }
  315. template<typename _Yp, typename _Alloc, typename... _Args>
  316. friend shared_ptr<_Yp>
  317. allocate_shared(const _Alloc& __a, _Args&&... __args);
  318. // This constructor is non-standard, it is used by weak_ptr::lock().
  319. shared_ptr(const weak_ptr<_Tp>& __r, std::nothrow_t)
  320. : __shared_ptr<_Tp>(__r, std::nothrow) { }
  321. friend class weak_ptr<_Tp>;
  322. };
  323. #if __cpp_deduction_guides >= 201606
  324. template<typename _Tp>
  325. shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
  326. template<typename _Tp, typename _Del>
  327. shared_ptr(unique_ptr<_Tp, _Del>) -> shared_ptr<_Tp>;
  328. #endif
  329. // 20.7.2.2.7 shared_ptr comparisons
  330. template<typename _Tp, typename _Up>
  331. inline bool
  332. operator==(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
  333. { return __a.get() == __b.get(); }
  334. template<typename _Tp>
  335. inline bool
  336. operator==(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
  337. { return !__a; }
  338. template<typename _Tp>
  339. inline bool
  340. operator==(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
  341. { return !__a; }
  342. template<typename _Tp, typename _Up>
  343. inline bool
  344. operator!=(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
  345. { return __a.get() != __b.get(); }
  346. template<typename _Tp>
  347. inline bool
  348. operator!=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
  349. { return (bool)__a; }
  350. template<typename _Tp>
  351. inline bool
  352. operator!=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
  353. { return (bool)__a; }
  354. template<typename _Tp, typename _Up>
  355. inline bool
  356. operator<(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
  357. {
  358. using _Tp_elt = typename shared_ptr<_Tp>::element_type;
  359. using _Up_elt = typename shared_ptr<_Up>::element_type;
  360. using _Vp = typename common_type<_Tp_elt*, _Up_elt*>::type;
  361. return less<_Vp>()(__a.get(), __b.get());
  362. }
  363. template<typename _Tp>
  364. inline bool
  365. operator<(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
  366. {
  367. using _Tp_elt = typename shared_ptr<_Tp>::element_type;
  368. return less<_Tp_elt*>()(__a.get(), nullptr);
  369. }
  370. template<typename _Tp>
  371. inline bool
  372. operator<(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
  373. {
  374. using _Tp_elt = typename shared_ptr<_Tp>::element_type;
  375. return less<_Tp_elt*>()(nullptr, __a.get());
  376. }
  377. template<typename _Tp, typename _Up>
  378. inline bool
  379. operator<=(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
  380. { return !(__b < __a); }
  381. template<typename _Tp>
  382. inline bool
  383. operator<=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
  384. { return !(nullptr < __a); }
  385. template<typename _Tp>
  386. inline bool
  387. operator<=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
  388. { return !(__a < nullptr); }
  389. template<typename _Tp, typename _Up>
  390. inline bool
  391. operator>(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
  392. { return (__b < __a); }
  393. template<typename _Tp>
  394. inline bool
  395. operator>(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
  396. { return nullptr < __a; }
  397. template<typename _Tp>
  398. inline bool
  399. operator>(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
  400. { return __a < nullptr; }
  401. template<typename _Tp, typename _Up>
  402. inline bool
  403. operator>=(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept
  404. { return !(__a < __b); }
  405. template<typename _Tp>
  406. inline bool
  407. operator>=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
  408. { return !(__a < nullptr); }
  409. template<typename _Tp>
  410. inline bool
  411. operator>=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
  412. { return !(nullptr < __a); }
  413. template<typename _Tp>
  414. struct less<shared_ptr<_Tp>> : public _Sp_less<shared_ptr<_Tp>>
  415. { };
  416. // 20.7.2.2.8 shared_ptr specialized algorithms.
  417. template<typename _Tp>
  418. inline void
  419. swap(shared_ptr<_Tp>& __a, shared_ptr<_Tp>& __b) noexcept
  420. { __a.swap(__b); }
  421. // 20.7.2.2.9 shared_ptr casts.
  422. template<typename _Tp, typename _Up>
  423. inline shared_ptr<_Tp>
  424. static_pointer_cast(const shared_ptr<_Up>& __r) noexcept
  425. {
  426. using _Sp = shared_ptr<_Tp>;
  427. return _Sp(__r, static_cast<typename _Sp::element_type*>(__r.get()));
  428. }
  429. template<typename _Tp, typename _Up>
  430. inline shared_ptr<_Tp>
  431. const_pointer_cast(const shared_ptr<_Up>& __r) noexcept
  432. {
  433. using _Sp = shared_ptr<_Tp>;
  434. return _Sp(__r, const_cast<typename _Sp::element_type*>(__r.get()));
  435. }
  436. template<typename _Tp, typename _Up>
  437. inline shared_ptr<_Tp>
  438. dynamic_pointer_cast(const shared_ptr<_Up>& __r) noexcept
  439. {
  440. using _Sp = shared_ptr<_Tp>;
  441. if (auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get()))
  442. return _Sp(__r, __p);
  443. return _Sp();
  444. }
  445. #if __cplusplus > 201402L
  446. template<typename _Tp, typename _Up>
  447. inline shared_ptr<_Tp>
  448. reinterpret_pointer_cast(const shared_ptr<_Up>& __r) noexcept
  449. {
  450. using _Sp = shared_ptr<_Tp>;
  451. return _Sp(__r, reinterpret_cast<typename _Sp::element_type*>(__r.get()));
  452. }
  453. #endif
  454. /**
  455. * @brief A smart pointer with weak semantics.
  456. *
  457. * With forwarding constructors and assignment operators.
  458. */
  459. template<typename _Tp>
  460. class weak_ptr : public __weak_ptr<_Tp>
  461. {
  462. template<typename _Arg>
  463. using _Constructible = typename enable_if<
  464. is_constructible<__weak_ptr<_Tp>, _Arg>::value
  465. >::type;
  466. template<typename _Arg>
  467. using _Assignable = typename enable_if<
  468. is_assignable<__weak_ptr<_Tp>&, _Arg>::value, weak_ptr&
  469. >::type;
  470. public:
  471. constexpr weak_ptr() noexcept = default;
  472. template<typename _Yp,
  473. typename = _Constructible<const shared_ptr<_Yp>&>>
  474. weak_ptr(const shared_ptr<_Yp>& __r) noexcept
  475. : __weak_ptr<_Tp>(__r) { }
  476. weak_ptr(const weak_ptr&) noexcept = default;
  477. template<typename _Yp, typename = _Constructible<const weak_ptr<_Yp>&>>
  478. weak_ptr(const weak_ptr<_Yp>& __r) noexcept
  479. : __weak_ptr<_Tp>(__r) { }
  480. weak_ptr(weak_ptr&&) noexcept = default;
  481. template<typename _Yp, typename = _Constructible<weak_ptr<_Yp>>>
  482. weak_ptr(weak_ptr<_Yp>&& __r) noexcept
  483. : __weak_ptr<_Tp>(std::move(__r)) { }
  484. weak_ptr&
  485. operator=(const weak_ptr& __r) noexcept = default;
  486. template<typename _Yp>
  487. _Assignable<const weak_ptr<_Yp>&>
  488. operator=(const weak_ptr<_Yp>& __r) noexcept
  489. {
  490. this->__weak_ptr<_Tp>::operator=(__r);
  491. return *this;
  492. }
  493. template<typename _Yp>
  494. _Assignable<const shared_ptr<_Yp>&>
  495. operator=(const shared_ptr<_Yp>& __r) noexcept
  496. {
  497. this->__weak_ptr<_Tp>::operator=(__r);
  498. return *this;
  499. }
  500. weak_ptr&
  501. operator=(weak_ptr&& __r) noexcept = default;
  502. template<typename _Yp>
  503. _Assignable<weak_ptr<_Yp>>
  504. operator=(weak_ptr<_Yp>&& __r) noexcept
  505. {
  506. this->__weak_ptr<_Tp>::operator=(std::move(__r));
  507. return *this;
  508. }
  509. shared_ptr<_Tp>
  510. lock() const noexcept
  511. { return shared_ptr<_Tp>(*this, std::nothrow); }
  512. };
  513. #if __cpp_deduction_guides >= 201606
  514. template<typename _Tp>
  515. weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
  516. #endif
  517. // 20.7.2.3.6 weak_ptr specialized algorithms.
  518. template<typename _Tp>
  519. inline void
  520. swap(weak_ptr<_Tp>& __a, weak_ptr<_Tp>& __b) noexcept
  521. { __a.swap(__b); }
  522. /// Primary template owner_less
  523. template<typename _Tp = void>
  524. struct owner_less;
  525. /// Void specialization of owner_less
  526. template<>
  527. struct owner_less<void> : _Sp_owner_less<void, void>
  528. { };
  529. /// Partial specialization of owner_less for shared_ptr.
  530. template<typename _Tp>
  531. struct owner_less<shared_ptr<_Tp>>
  532. : public _Sp_owner_less<shared_ptr<_Tp>, weak_ptr<_Tp>>
  533. { };
  534. /// Partial specialization of owner_less for weak_ptr.
  535. template<typename _Tp>
  536. struct owner_less<weak_ptr<_Tp>>
  537. : public _Sp_owner_less<weak_ptr<_Tp>, shared_ptr<_Tp>>
  538. { };
  539. /**
  540. * @brief Base class allowing use of member function shared_from_this.
  541. */
  542. template<typename _Tp>
  543. class enable_shared_from_this
  544. {
  545. protected:
  546. constexpr enable_shared_from_this() noexcept { }
  547. enable_shared_from_this(const enable_shared_from_this&) noexcept { }
  548. enable_shared_from_this&
  549. operator=(const enable_shared_from_this&) noexcept
  550. { return *this; }
  551. ~enable_shared_from_this() { }
  552. public:
  553. shared_ptr<_Tp>
  554. shared_from_this()
  555. { return shared_ptr<_Tp>(this->_M_weak_this); }
  556. shared_ptr<const _Tp>
  557. shared_from_this() const
  558. { return shared_ptr<const _Tp>(this->_M_weak_this); }
  559. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  560. #define __cpp_lib_enable_shared_from_this 201603
  561. weak_ptr<_Tp>
  562. weak_from_this() noexcept
  563. { return this->_M_weak_this; }
  564. weak_ptr<const _Tp>
  565. weak_from_this() const noexcept
  566. { return this->_M_weak_this; }
  567. #endif
  568. private:
  569. template<typename _Tp1>
  570. void
  571. _M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const noexcept
  572. { _M_weak_this._M_assign(__p, __n); }
  573. // Found by ADL when this is an associated class.
  574. friend const enable_shared_from_this*
  575. __enable_shared_from_this_base(const __shared_count<>&,
  576. const enable_shared_from_this* __p)
  577. { return __p; }
  578. template<typename, _Lock_policy>
  579. friend class __shared_ptr;
  580. mutable weak_ptr<_Tp> _M_weak_this;
  581. };
  582. /**
  583. * @brief Create an object that is owned by a shared_ptr.
  584. * @param __a An allocator.
  585. * @param __args Arguments for the @a _Tp object's constructor.
  586. * @return A shared_ptr that owns the newly created object.
  587. * @throw An exception thrown from @a _Alloc::allocate or from the
  588. * constructor of @a _Tp.
  589. *
  590. * A copy of @a __a will be used to allocate memory for the shared_ptr
  591. * and the new object.
  592. */
  593. template<typename _Tp, typename _Alloc, typename... _Args>
  594. inline shared_ptr<_Tp>
  595. allocate_shared(const _Alloc& __a, _Args&&... __args)
  596. {
  597. return shared_ptr<_Tp>(_Sp_alloc_shared_tag<_Alloc>{__a},
  598. std::forward<_Args>(__args)...);
  599. }
  600. /**
  601. * @brief Create an object that is owned by a shared_ptr.
  602. * @param __args Arguments for the @a _Tp object's constructor.
  603. * @return A shared_ptr that owns the newly created object.
  604. * @throw std::bad_alloc, or an exception thrown from the
  605. * constructor of @a _Tp.
  606. */
  607. template<typename _Tp, typename... _Args>
  608. inline shared_ptr<_Tp>
  609. make_shared(_Args&&... __args)
  610. {
  611. typedef typename std::remove_cv<_Tp>::type _Tp_nc;
  612. return std::allocate_shared<_Tp>(std::allocator<_Tp_nc>(),
  613. std::forward<_Args>(__args)...);
  614. }
  615. /// std::hash specialization for shared_ptr.
  616. template<typename _Tp>
  617. struct hash<shared_ptr<_Tp>>
  618. : public __hash_base<size_t, shared_ptr<_Tp>>
  619. {
  620. size_t
  621. operator()(const shared_ptr<_Tp>& __s) const noexcept
  622. {
  623. return std::hash<typename shared_ptr<_Tp>::element_type*>()(__s.get());
  624. }
  625. };
  626. // @} group pointer_abstractions
  627. _GLIBCXX_END_NAMESPACE_VERSION
  628. } // namespace
  629. #endif // _SHARED_PTR_H