shared_ptr.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. // Experimental shared_ptr with array support -*- C++ -*-
  2. // Copyright (C) 2015-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. /** @file experimental/bits/shared_ptr.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{experimental/memory}
  23. */
  24. #ifndef _GLIBCXX_EXPERIMENTAL_SHARED_PTR_H
  25. #define _GLIBCXX_EXPERIMENTAL_SHARED_PTR_H 1
  26. #pragma GCC system_header
  27. #if __cplusplus >= 201402L
  28. #include <memory>
  29. #include <experimental/type_traits>
  30. namespace std _GLIBCXX_VISIBILITY(default)
  31. {
  32. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  33. namespace experimental
  34. {
  35. inline namespace fundamentals_v2
  36. {
  37. // 8.2.1
  38. template<typename _Tp> class shared_ptr;
  39. template<typename _Tp> class weak_ptr;
  40. template<typename _Tp> class enable_shared_from_this;
  41. template<typename _Yp, typename _Tp>
  42. constexpr bool __sp_compatible_v
  43. = std::__sp_compatible_with<_Yp*, _Tp*>::value;
  44. template<typename _Tp, typename _Yp>
  45. constexpr bool __sp_is_constructible_v
  46. = std::__sp_is_constructible<_Tp, _Yp>::value;
  47. template<typename _Tp>
  48. class shared_ptr : public __shared_ptr<_Tp>
  49. {
  50. using _Base_type = __shared_ptr<_Tp>;
  51. public:
  52. using element_type = typename _Base_type::element_type;
  53. private:
  54. // Constraint for construction from a pointer of type _Yp*:
  55. template<typename _Yp>
  56. using _SafeConv = enable_if_t<__sp_is_constructible_v<_Tp, _Yp>>;
  57. template<typename _Tp1, typename _Res = void>
  58. using _Compatible
  59. = enable_if_t<__sp_compatible_v<_Tp1, _Tp>, _Res>;
  60. template<typename _Tp1, typename _Del,
  61. typename _Ptr = typename unique_ptr<_Tp1, _Del>::pointer,
  62. typename _Res = void>
  63. using _UniqCompatible = enable_if_t<
  64. __sp_compatible_v<_Tp1, _Tp>
  65. && experimental::is_convertible_v<_Ptr, element_type*>,
  66. _Res>;
  67. public:
  68. // 8.2.1.1, shared_ptr constructors
  69. constexpr shared_ptr() noexcept = default;
  70. template<typename _Tp1, typename = _SafeConv<_Tp1>>
  71. explicit
  72. shared_ptr(_Tp1* __p) : _Base_type(__p)
  73. { _M_enable_shared_from_this_with(__p); }
  74. template<typename _Tp1, typename _Deleter, typename = _SafeConv<_Tp1>>
  75. shared_ptr(_Tp1* __p, _Deleter __d)
  76. : _Base_type(__p, __d)
  77. { _M_enable_shared_from_this_with(__p); }
  78. template<typename _Tp1, typename _Deleter, typename _Alloc,
  79. typename = _SafeConv<_Tp1>>
  80. shared_ptr(_Tp1* __p, _Deleter __d, _Alloc __a)
  81. : _Base_type(__p, __d, __a)
  82. { _M_enable_shared_from_this_with(__p); }
  83. template<typename _Deleter>
  84. shared_ptr(nullptr_t __p, _Deleter __d)
  85. : _Base_type(__p, __d) { }
  86. template<typename _Deleter, typename _Alloc>
  87. shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
  88. : _Base_type(__p, __d, __a) { }
  89. template<typename _Tp1>
  90. shared_ptr(const shared_ptr<_Tp1>& __r, element_type* __p) noexcept
  91. : _Base_type(__r, __p) { }
  92. shared_ptr(const shared_ptr& __r) noexcept
  93. : _Base_type(__r) { }
  94. template<typename _Tp1, typename = _Compatible<_Tp1>>
  95. shared_ptr(const shared_ptr<_Tp1>& __r) noexcept
  96. : _Base_type(__r) { }
  97. shared_ptr(shared_ptr&& __r) noexcept
  98. : _Base_type(std::move(__r)) { }
  99. template<typename _Tp1, typename = _Compatible<_Tp1>>
  100. shared_ptr(shared_ptr<_Tp1>&& __r) noexcept
  101. : _Base_type(std::move(__r)) { }
  102. template<typename _Tp1, typename = _Compatible<_Tp1>>
  103. explicit
  104. shared_ptr(const weak_ptr<_Tp1>& __r)
  105. : _Base_type(__r) { }
  106. #if _GLIBCXX_USE_DEPRECATED
  107. #pragma GCC diagnostic push
  108. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  109. template<typename _Tp1, typename = _Compatible<_Tp1>>
  110. shared_ptr(std::auto_ptr<_Tp1>&& __r)
  111. : _Base_type(std::move(__r))
  112. { _M_enable_shared_from_this_with(static_cast<_Tp1*>(this->get())); }
  113. #pragma GCC diagnostic pop
  114. #endif
  115. template<typename _Tp1, typename _Del,
  116. typename = _UniqCompatible<_Tp1, _Del>>
  117. shared_ptr(unique_ptr<_Tp1, _Del>&& __r)
  118. : _Base_type(std::move(__r))
  119. {
  120. // XXX assume conversion from __r.get() to this->get() to __elem_t*
  121. // is a round trip, which might not be true in all cases.
  122. using __elem_t = typename unique_ptr<_Tp1, _Del>::element_type;
  123. _M_enable_shared_from_this_with(static_cast<__elem_t*>(this->get()));
  124. }
  125. constexpr shared_ptr(nullptr_t __p)
  126. : _Base_type(__p) { }
  127. // C++14 20.8.2.2
  128. ~shared_ptr() = default;
  129. // C++14 20.8.2.3
  130. shared_ptr& operator=(const shared_ptr&) noexcept = default;
  131. template <typename _Tp1>
  132. _Compatible<_Tp1, shared_ptr&>
  133. operator=(const shared_ptr<_Tp1>& __r) noexcept
  134. {
  135. _Base_type::operator=(__r);
  136. return *this;
  137. }
  138. shared_ptr&
  139. operator=(shared_ptr&& __r) noexcept
  140. {
  141. _Base_type::operator=(std::move(__r));
  142. return *this;
  143. }
  144. template <typename _Tp1>
  145. _Compatible<_Tp1, shared_ptr&>
  146. operator=(shared_ptr<_Tp1>&& __r) noexcept
  147. {
  148. _Base_type::operator=(std::move(__r));
  149. return *this;
  150. }
  151. #if _GLIBCXX_USE_DEPRECATED
  152. #pragma GCC diagnostic push
  153. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  154. template<typename _Tp1>
  155. _Compatible<_Tp1, shared_ptr&>
  156. operator=(std::auto_ptr<_Tp1>&& __r)
  157. {
  158. __shared_ptr<_Tp>::operator=(std::move(__r));
  159. return *this;
  160. }
  161. #pragma GCC diagnostic pop
  162. #endif
  163. template <typename _Tp1, typename _Del>
  164. _UniqCompatible<_Tp1, _Del, shared_ptr&>
  165. operator=(unique_ptr<_Tp1, _Del>&& __r)
  166. {
  167. _Base_type::operator=(std::move(__r));
  168. return *this;
  169. }
  170. // C++14 20.8.2.2.4
  171. // swap & reset
  172. // 8.2.1.2 shared_ptr observers
  173. // in __shared_ptr
  174. private:
  175. template<typename _Alloc, typename... _Args>
  176. shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a,
  177. _Args&&... __args)
  178. : _Base_type(__tag, __a, std::forward<_Args>(__args)...)
  179. { _M_enable_shared_from_this_with(this->get()); }
  180. template<typename _Tp1, typename _Alloc, typename... _Args>
  181. friend shared_ptr<_Tp1>
  182. allocate_shared(const _Alloc& __a, _Args&&... __args);
  183. shared_ptr(const weak_ptr<_Tp>& __r, std::nothrow_t)
  184. : _Base_type(__r, std::nothrow) { }
  185. friend class weak_ptr<_Tp>;
  186. template<typename _Yp>
  187. using __esft_base_t =
  188. decltype(__expt_enable_shared_from_this_base(std::declval<_Yp*>()));
  189. // Detect an accessible and unambiguous enable_shared_from_this base.
  190. template<typename _Yp, typename = void>
  191. struct __has_esft_base
  192. : false_type { };
  193. template<typename _Yp>
  194. struct __has_esft_base<_Yp, __void_t<__esft_base_t<_Yp>>>
  195. : __bool_constant<!is_array_v<_Tp>> { }; // ignore base for arrays
  196. template<typename _Yp>
  197. typename enable_if<__has_esft_base<_Yp>::value>::type
  198. _M_enable_shared_from_this_with(const _Yp* __p) noexcept
  199. {
  200. if (auto __base = __expt_enable_shared_from_this_base(__p))
  201. {
  202. __base->_M_weak_this
  203. = shared_ptr<_Yp>(*this, const_cast<_Yp*>(__p));
  204. }
  205. }
  206. template<typename _Yp>
  207. typename enable_if<!__has_esft_base<_Yp>::value>::type
  208. _M_enable_shared_from_this_with(const _Yp*) noexcept
  209. { }
  210. };
  211. // C++14 20.8.2.2.7
  212. template<typename _Tp1, typename _Tp2>
  213. bool operator==(const shared_ptr<_Tp1>& __a,
  214. const shared_ptr<_Tp2>& __b) noexcept
  215. { return __a.get() == __b.get(); }
  216. template<typename _Tp>
  217. inline bool
  218. operator==(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
  219. { return !__a; }
  220. template<typename _Tp>
  221. inline bool
  222. operator==(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
  223. { return !__a; }
  224. template<typename _Tp1, typename _Tp2>
  225. inline bool
  226. operator!=(const shared_ptr<_Tp1>& __a,
  227. const shared_ptr<_Tp2>& __b) noexcept
  228. { return __a.get() != __b.get(); }
  229. template<typename _Tp>
  230. inline bool
  231. operator!=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
  232. { return (bool)__a; }
  233. template<typename _Tp>
  234. inline bool
  235. operator!=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
  236. { return (bool)__a; }
  237. template<typename _Tp1, typename _Tp2>
  238. inline bool
  239. operator<(const shared_ptr<_Tp1>& __a,
  240. const shared_ptr<_Tp2>& __b) noexcept
  241. {
  242. using __elem_t1 = typename shared_ptr<_Tp1>::element_type;
  243. using __elem_t2 = typename shared_ptr<_Tp2>::element_type;
  244. using _CT = common_type_t<__elem_t1*, __elem_t2*>;
  245. return std::less<_CT>()(__a.get(), __b.get());
  246. }
  247. template<typename _Tp>
  248. inline bool
  249. operator<(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
  250. {
  251. using __elem_t = typename shared_ptr<_Tp>::element_type;
  252. return std::less<__elem_t*>()(__a.get(), nullptr);
  253. }
  254. template<typename _Tp>
  255. inline bool
  256. operator<(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
  257. {
  258. using __elem_t = typename shared_ptr<_Tp>::element_type;
  259. return std::less<__elem_t*>()(nullptr, __a.get());
  260. }
  261. template<typename _Tp1, typename _Tp2>
  262. inline bool
  263. operator<=(const shared_ptr<_Tp1>& __a,
  264. const shared_ptr<_Tp2>& __b) noexcept
  265. { return !(__b < __a); }
  266. template<typename _Tp>
  267. inline bool
  268. operator<=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
  269. { return !(nullptr < __a); }
  270. template<typename _Tp>
  271. inline bool
  272. operator<=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
  273. { return !(__a < nullptr); }
  274. template<typename _Tp1, typename _Tp2>
  275. inline bool
  276. operator>(const shared_ptr<_Tp1>& __a,
  277. const shared_ptr<_Tp2>& __b) noexcept
  278. { return (__b < __a); }
  279. template<typename _Tp>
  280. inline bool
  281. operator>(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
  282. {
  283. using __elem_t = typename shared_ptr<_Tp>::element_type;
  284. return std::less<__elem_t*>()(nullptr, __a.get());
  285. }
  286. template<typename _Tp>
  287. inline bool
  288. operator>(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
  289. {
  290. using __elem_t = typename shared_ptr<_Tp>::element_type;
  291. return std::less<__elem_t*>()(__a.get(), nullptr);
  292. }
  293. template<typename _Tp1, typename _Tp2>
  294. inline bool
  295. operator>=(const shared_ptr<_Tp1>& __a,
  296. const shared_ptr<_Tp2>& __b) noexcept
  297. { return !(__a < __b); }
  298. template<typename _Tp>
  299. inline bool
  300. operator>=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
  301. { return !(__a < nullptr); }
  302. template<typename _Tp>
  303. inline bool
  304. operator>=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
  305. { return !(nullptr < __a); }
  306. // C++14 20.8.2.2.8
  307. template<typename _Tp>
  308. inline void
  309. swap(shared_ptr<_Tp>& __a, shared_ptr<_Tp>& __b) noexcept
  310. { __a.swap(__b); }
  311. // 8.2.1.3, shared_ptr casts
  312. template<typename _Tp, typename _Tp1>
  313. inline shared_ptr<_Tp>
  314. static_pointer_cast(const shared_ptr<_Tp1>& __r) noexcept
  315. {
  316. using __elem_t = typename shared_ptr<_Tp>::element_type;
  317. return shared_ptr<_Tp>(__r, static_cast<__elem_t*>(__r.get()));
  318. }
  319. template<typename _Tp, typename _Tp1>
  320. inline shared_ptr<_Tp>
  321. dynamic_pointer_cast(const shared_ptr<_Tp1>& __r) noexcept
  322. {
  323. using __elem_t = typename shared_ptr<_Tp>::element_type;
  324. if (_Tp* __p = dynamic_cast<__elem_t*>(__r.get()))
  325. return shared_ptr<_Tp>(__r, __p);
  326. return shared_ptr<_Tp>();
  327. }
  328. template<typename _Tp, typename _Tp1>
  329. inline shared_ptr<_Tp>
  330. const_pointer_cast(const shared_ptr<_Tp1>& __r) noexcept
  331. {
  332. using __elem_t = typename shared_ptr<_Tp>::element_type;
  333. return shared_ptr<_Tp>(__r, const_cast<__elem_t*>(__r.get()));
  334. }
  335. template<typename _Tp, typename _Tp1>
  336. inline shared_ptr<_Tp>
  337. reinterpret_pointer_cast(const shared_ptr<_Tp1>& __r) noexcept
  338. {
  339. using __elem_t = typename shared_ptr<_Tp>::element_type;
  340. return shared_ptr<_Tp>(__r, reinterpret_cast<__elem_t*>(__r.get()));
  341. }
  342. // C++14 20.8.2.3
  343. template<typename _Tp>
  344. class weak_ptr : public __weak_ptr<_Tp>
  345. {
  346. template<typename _Tp1, typename _Res = void>
  347. using _Compatible = enable_if_t<__sp_compatible_v<_Tp1, _Tp>, _Res>;
  348. using _Base_type = __weak_ptr<_Tp>;
  349. public:
  350. constexpr weak_ptr() noexcept = default;
  351. template<typename _Tp1, typename = _Compatible<_Tp1>>
  352. weak_ptr(const shared_ptr<_Tp1>& __r) noexcept
  353. : _Base_type(__r) { }
  354. weak_ptr(const weak_ptr&) noexcept = default;
  355. template<typename _Tp1, typename = _Compatible<_Tp1>>
  356. weak_ptr(const weak_ptr<_Tp1>& __r) noexcept
  357. : _Base_type(__r) { }
  358. weak_ptr(weak_ptr&&) noexcept = default;
  359. template<typename _Tp1, typename = _Compatible<_Tp1>>
  360. weak_ptr(weak_ptr<_Tp1>&& __r) noexcept
  361. : _Base_type(std::move(__r)) { }
  362. weak_ptr&
  363. operator=(const weak_ptr& __r) noexcept = default;
  364. template<typename _Tp1>
  365. _Compatible<_Tp1, weak_ptr&>
  366. operator=(const weak_ptr<_Tp1>& __r) noexcept
  367. {
  368. this->_Base_type::operator=(__r);
  369. return *this;
  370. }
  371. template<typename _Tp1>
  372. _Compatible<_Tp1, weak_ptr&>
  373. operator=(const shared_ptr<_Tp1>& __r) noexcept
  374. {
  375. this->_Base_type::operator=(__r);
  376. return *this;
  377. }
  378. weak_ptr&
  379. operator=(weak_ptr&& __r) noexcept = default;
  380. template<typename _Tp1>
  381. _Compatible<_Tp1, weak_ptr&>
  382. operator=(weak_ptr<_Tp1>&& __r) noexcept
  383. {
  384. this->_Base_type::operator=(std::move(__r));
  385. return *this;
  386. }
  387. shared_ptr<_Tp>
  388. lock() const noexcept
  389. { return shared_ptr<_Tp>(*this, std::nothrow); }
  390. friend class enable_shared_from_this<_Tp>;
  391. };
  392. // C++14 20.8.2.3.6
  393. template<typename _Tp>
  394. inline void
  395. swap(weak_ptr<_Tp>& __a, weak_ptr<_Tp>& __b) noexcept
  396. { __a.swap(__b); }
  397. /// C++14 20.8.2.2.10
  398. template<typename _Del, typename _Tp>
  399. inline _Del*
  400. get_deleter(const shared_ptr<_Tp>& __p) noexcept
  401. { return std::get_deleter<_Del>(__p); }
  402. // C++14 20.8.2.2.11
  403. template<typename _Ch, typename _Tr, typename _Tp>
  404. inline std::basic_ostream<_Ch, _Tr>&
  405. operator<<(std::basic_ostream<_Ch, _Tr>& __os, const shared_ptr<_Tp>& __p)
  406. {
  407. __os << __p.get();
  408. return __os;
  409. }
  410. // C++14 20.8.2.4
  411. template<typename _Tp = void> class owner_less;
  412. /// Partial specialization of owner_less for shared_ptr.
  413. template<typename _Tp>
  414. struct owner_less<shared_ptr<_Tp>>
  415. : public _Sp_owner_less<shared_ptr<_Tp>, weak_ptr<_Tp>>
  416. { };
  417. /// Partial specialization of owner_less for weak_ptr.
  418. template<typename _Tp>
  419. struct owner_less<weak_ptr<_Tp>>
  420. : public _Sp_owner_less<weak_ptr<_Tp>, shared_ptr<_Tp>>
  421. { };
  422. template<>
  423. class owner_less<void>
  424. {
  425. template<typename _Tp, typename _Up>
  426. bool
  427. operator()(shared_ptr<_Tp> const& __lhs,
  428. shared_ptr<_Up> const& __rhs) const
  429. { return __lhs.owner_before(__rhs); }
  430. template<typename _Tp, typename _Up>
  431. bool
  432. operator()(shared_ptr<_Tp> const& __lhs,
  433. weak_ptr<_Up> const& __rhs) const
  434. { return __lhs.owner_before(__rhs); }
  435. template<typename _Tp, typename _Up>
  436. bool
  437. operator()(weak_ptr<_Tp> const& __lhs,
  438. shared_ptr<_Up> const& __rhs) const
  439. { return __lhs.owner_before(__rhs); }
  440. template<typename _Tp, typename _Up>
  441. bool
  442. operator()(weak_ptr<_Tp> const& __lhs,
  443. weak_ptr<_Up> const& __rhs) const
  444. { return __lhs.owner_before(__rhs); }
  445. typedef void is_transparent;
  446. };
  447. // C++14 20.8.2.6
  448. template<typename _Tp>
  449. inline bool
  450. atomic_is_lock_free(const shared_ptr<_Tp>* __p)
  451. { return std::atomic_is_lock_free<_Tp, __default_lock_policy>(__p); }
  452. template<typename _Tp>
  453. shared_ptr<_Tp> atomic_load(const shared_ptr<_Tp>* __p)
  454. { return std::atomic_load<_Tp>(__p); }
  455. template<typename _Tp>
  456. shared_ptr<_Tp>
  457. atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order __mo)
  458. { return std::atomic_load_explicit<_Tp>(__p, __mo); }
  459. template<typename _Tp>
  460. void atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
  461. { return std::atomic_store<_Tp>(__p, __r); }
  462. template<typename _Tp>
  463. shared_ptr<_Tp>
  464. atomic_store_explicit(const shared_ptr<_Tp>* __p,
  465. shared_ptr<_Tp> __r,
  466. memory_order __mo)
  467. { return std::atomic_store_explicit<_Tp>(__p, __r, __mo); }
  468. template<typename _Tp>
  469. void atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
  470. { return std::atomic_exchange<_Tp>(__p, __r); }
  471. template<typename _Tp>
  472. shared_ptr<_Tp>
  473. atomic_exchange_explicit(const shared_ptr<_Tp>* __p,
  474. shared_ptr<_Tp> __r,
  475. memory_order __mo)
  476. { return std::atomic_exchange_explicit<_Tp>(__p, __r, __mo); }
  477. template<typename _Tp>
  478. bool atomic_compare_exchange_weak(shared_ptr<_Tp>* __p,
  479. shared_ptr<_Tp>* __v,
  480. shared_ptr<_Tp> __w)
  481. { return std::atomic_compare_exchange_weak<_Tp>(__p, __v, __w); }
  482. template<typename _Tp>
  483. bool atomic_compare_exchange_strong(shared_ptr<_Tp>* __p,
  484. shared_ptr<_Tp>* __v,
  485. shared_ptr<_Tp> __w)
  486. { return std::atomic_compare_exchange_strong<_Tp>(__p, __v, __w); }
  487. template<typename _Tp>
  488. bool atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p,
  489. shared_ptr<_Tp>* __v,
  490. shared_ptr<_Tp> __w,
  491. memory_order __success,
  492. memory_order __failure)
  493. { return std::atomic_compare_exchange_weak_explicit<_Tp>(__p, __v, __w,
  494. __success,
  495. __failure); }
  496. template<typename _Tp>
  497. bool atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p,
  498. shared_ptr<_Tp>* __v,
  499. shared_ptr<_Tp> __w,
  500. memory_order __success,
  501. memory_order __failure)
  502. { return std::atomic_compare_exchange_strong_explicit<_Tp>(__p, __v, __w,
  503. __success,
  504. __failure); }
  505. //enable_shared_from_this
  506. template<typename _Tp>
  507. class enable_shared_from_this
  508. {
  509. protected:
  510. constexpr enable_shared_from_this() noexcept { }
  511. enable_shared_from_this(const enable_shared_from_this&) noexcept { }
  512. enable_shared_from_this&
  513. operator=(const enable_shared_from_this&) noexcept
  514. { return *this; }
  515. ~enable_shared_from_this() { }
  516. public:
  517. shared_ptr<_Tp>
  518. shared_from_this()
  519. { return shared_ptr<_Tp>(this->_M_weak_this); }
  520. shared_ptr<const _Tp>
  521. shared_from_this() const
  522. { return shared_ptr<const _Tp>(this->_M_weak_this); }
  523. weak_ptr<_Tp>
  524. weak_from_this() noexcept
  525. { return _M_weak_this; }
  526. weak_ptr<const _Tp>
  527. weak_from_this() const noexcept
  528. { return _M_weak_this; }
  529. private:
  530. template<typename _Tp1>
  531. void
  532. _M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const noexcept
  533. { _M_weak_this._M_assign(__p, __n); }
  534. // Found by ADL when this is an associated class.
  535. friend const enable_shared_from_this*
  536. __expt_enable_shared_from_this_base(const enable_shared_from_this* __p)
  537. { return __p; }
  538. template<typename>
  539. friend class shared_ptr;
  540. mutable weak_ptr<_Tp> _M_weak_this;
  541. };
  542. } // namespace fundamentals_v2
  543. } // namespace experimental
  544. /// std::hash specialization for shared_ptr.
  545. template<typename _Tp>
  546. struct hash<experimental::shared_ptr<_Tp>>
  547. : public __hash_base<size_t, experimental::shared_ptr<_Tp>>
  548. {
  549. size_t
  550. operator()(const experimental::shared_ptr<_Tp>& __s) const noexcept
  551. { return std::hash<_Tp*>()(__s.get()); }
  552. };
  553. _GLIBCXX_END_NAMESPACE_VERSION
  554. } // namespace std
  555. #endif // __cplusplus <= 201103L
  556. #endif // _GLIBCXX_EXPERIMENTAL_SHARED_PTR_H