shared_ptr.h 20 KB

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