shared_ptr.h 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___MEMORY_SHARED_PTR_H
  10. #define _LIBCPP___MEMORY_SHARED_PTR_H
  11. #include <__availability>
  12. #include <__config>
  13. #include <__functional/binary_function.h>
  14. #include <__functional/operations.h>
  15. #include <__functional/reference_wrapper.h>
  16. #include <__memory/addressof.h>
  17. #include <__memory/allocation_guard.h>
  18. #include <__memory/allocator.h>
  19. #include <__memory/allocator_traits.h>
  20. #include <__memory/compressed_pair.h>
  21. #include <__memory/pointer_traits.h>
  22. #include <__memory/unique_ptr.h>
  23. #include <__utility/forward.h>
  24. #include <__utility/move.h>
  25. #include <cstddef>
  26. #include <cstdlib> // abort
  27. #include <iosfwd>
  28. #include <stdexcept>
  29. #include <type_traits>
  30. #include <typeinfo>
  31. #if !defined(_LIBCPP_HAS_NO_THREADS) // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
  32. # include <atomic>
  33. #endif
  34. #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
  35. # include <__memory/auto_ptr.h>
  36. #endif
  37. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  38. # pragma GCC system_header
  39. #endif
  40. _LIBCPP_BEGIN_NAMESPACE_STD
  41. template <class _Alloc>
  42. class __allocator_destructor
  43. {
  44. typedef _LIBCPP_NODEBUG allocator_traits<_Alloc> __alloc_traits;
  45. public:
  46. typedef _LIBCPP_NODEBUG typename __alloc_traits::pointer pointer;
  47. typedef _LIBCPP_NODEBUG typename __alloc_traits::size_type size_type;
  48. private:
  49. _Alloc& __alloc_;
  50. size_type __s_;
  51. public:
  52. _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
  53. _NOEXCEPT
  54. : __alloc_(__a), __s_(__s) {}
  55. _LIBCPP_INLINE_VISIBILITY
  56. void operator()(pointer __p) _NOEXCEPT
  57. {__alloc_traits::deallocate(__alloc_, __p, __s_);}
  58. };
  59. // NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
  60. // should be sufficient for thread safety.
  61. // See https://llvm.org/PR22803
  62. #if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
  63. && defined(__ATOMIC_RELAXED) \
  64. && defined(__ATOMIC_ACQ_REL)
  65. # define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
  66. #elif defined(_LIBCPP_COMPILER_GCC)
  67. # define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
  68. #endif
  69. template <class _ValueType>
  70. inline _LIBCPP_INLINE_VISIBILITY
  71. _ValueType __libcpp_relaxed_load(_ValueType const* __value) {
  72. #if !defined(_LIBCPP_HAS_NO_THREADS) && \
  73. defined(__ATOMIC_RELAXED) && \
  74. (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
  75. return __atomic_load_n(__value, __ATOMIC_RELAXED);
  76. #else
  77. return *__value;
  78. #endif
  79. }
  80. template <class _ValueType>
  81. inline _LIBCPP_INLINE_VISIBILITY
  82. _ValueType __libcpp_acquire_load(_ValueType const* __value) {
  83. #if !defined(_LIBCPP_HAS_NO_THREADS) && \
  84. defined(__ATOMIC_ACQUIRE) && \
  85. (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
  86. return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
  87. #else
  88. return *__value;
  89. #endif
  90. }
  91. #ifndef _LIBCPP_COMPILER_MSVC
  92. template <class _Tp>
  93. inline _LIBCPP_INLINE_VISIBILITY _Tp
  94. __libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
  95. {
  96. #if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
  97. return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
  98. #else
  99. return __t += 1;
  100. #endif
  101. }
  102. template <class _Tp>
  103. inline _LIBCPP_INLINE_VISIBILITY _Tp
  104. __libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
  105. {
  106. #if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
  107. return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
  108. #else
  109. return __t -= 1;
  110. #endif
  111. }
  112. #endif
  113. class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
  114. : public std::exception
  115. {
  116. public:
  117. bad_weak_ptr() _NOEXCEPT = default;
  118. bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default;
  119. virtual ~bad_weak_ptr() _NOEXCEPT;
  120. virtual const char* what() const _NOEXCEPT;
  121. };
  122. _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
  123. void __throw_bad_weak_ptr()
  124. {
  125. #ifndef _LIBCPP_NO_EXCEPTIONS
  126. throw bad_weak_ptr();
  127. #else
  128. _VSTD::abort();
  129. #endif
  130. }
  131. template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
  132. class _LIBCPP_TYPE_VIS __shared_count
  133. {
  134. __shared_count(const __shared_count&);
  135. __shared_count& operator=(const __shared_count&);
  136. protected:
  137. #ifdef _LIBCPP_HAS_NO_THREADS
  138. typedef long __atomic_count;
  139. #else
  140. typedef atomic<long> __atomic_count;
  141. #endif
  142. __atomic_count __shared_owners_;
  143. virtual ~__shared_count();
  144. private:
  145. virtual void __on_zero_shared() _NOEXCEPT = 0;
  146. public:
  147. _LIBCPP_INLINE_VISIBILITY
  148. explicit __shared_count(long __refs = 0) _NOEXCEPT
  149. : __shared_owners_(__refs) {}
  150. #if defined(_LIBCPP_BUILDING_LIBRARY) && \
  151. defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
  152. void __add_shared() _NOEXCEPT;
  153. bool __release_shared() _NOEXCEPT;
  154. #else
  155. _LIBCPP_INLINE_VISIBILITY
  156. void __add_shared() _NOEXCEPT {
  157. #ifdef _LIBCPP_HAS_NO_THREADS
  158. __libcpp_atomic_refcount_increment(__shared_owners_);
  159. #else
  160. __shared_owners_++;
  161. #endif
  162. }
  163. _LIBCPP_INLINE_VISIBILITY
  164. bool __release_shared() _NOEXCEPT {
  165. #ifdef _LIBCPP_HAS_NO_THREADS
  166. if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
  167. #else
  168. if (--__shared_owners_ == -1) {
  169. #endif
  170. __on_zero_shared();
  171. return true;
  172. }
  173. return false;
  174. }
  175. #endif
  176. _LIBCPP_INLINE_VISIBILITY
  177. long use_count() const _NOEXCEPT {
  178. #ifdef _LIBCPP_HAS_NO_THREADS
  179. return __libcpp_relaxed_load(&__shared_owners_) + 1;
  180. #else
  181. return __shared_owners_.load(memory_order_relaxed) + 1;
  182. #endif
  183. }
  184. };
  185. class _LIBCPP_TYPE_VIS __shared_weak_count
  186. : private __shared_count
  187. {
  188. #ifdef _LIBCPP_HAS_NO_THREADS
  189. typedef long __atomic_count;
  190. #else
  191. typedef atomic<long> __atomic_count;
  192. #endif
  193. __atomic_count __shared_weak_owners_;
  194. public:
  195. _LIBCPP_INLINE_VISIBILITY
  196. explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
  197. : __shared_count(__refs),
  198. __shared_weak_owners_(__refs) {}
  199. protected:
  200. virtual ~__shared_weak_count();
  201. public:
  202. #if defined(_LIBCPP_BUILDING_LIBRARY) && \
  203. defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
  204. void __add_shared() _NOEXCEPT;
  205. void __add_weak() _NOEXCEPT;
  206. void __release_shared() _NOEXCEPT;
  207. #else
  208. _LIBCPP_INLINE_VISIBILITY
  209. void __add_shared() _NOEXCEPT {
  210. __shared_count::__add_shared();
  211. }
  212. _LIBCPP_INLINE_VISIBILITY
  213. void __add_weak() _NOEXCEPT {
  214. #ifdef _LIBCPP_HAS_NO_THREADS
  215. __libcpp_atomic_refcount_increment(__shared_weak_owners_);
  216. #else
  217. __shared_weak_owners_++;
  218. #endif
  219. }
  220. _LIBCPP_INLINE_VISIBILITY
  221. void __release_shared() _NOEXCEPT {
  222. if (__shared_count::__release_shared())
  223. __release_weak();
  224. }
  225. #endif
  226. void __release_weak() _NOEXCEPT;
  227. _LIBCPP_INLINE_VISIBILITY
  228. long use_count() const _NOEXCEPT {return __shared_count::use_count();}
  229. __shared_weak_count* lock() _NOEXCEPT;
  230. virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
  231. private:
  232. virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
  233. };
  234. template <class _Tp, class _Dp, class _Alloc>
  235. class __shared_ptr_pointer
  236. : public __shared_weak_count
  237. {
  238. __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
  239. public:
  240. _LIBCPP_INLINE_VISIBILITY
  241. __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
  242. : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
  243. #ifndef _LIBCPP_NO_RTTI
  244. virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
  245. #endif
  246. private:
  247. virtual void __on_zero_shared() _NOEXCEPT;
  248. virtual void __on_zero_shared_weak() _NOEXCEPT;
  249. };
  250. #ifndef _LIBCPP_NO_RTTI
  251. template <class _Tp, class _Dp, class _Alloc>
  252. const void*
  253. __shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
  254. {
  255. return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
  256. }
  257. #endif // _LIBCPP_NO_RTTI
  258. template <class _Tp, class _Dp, class _Alloc>
  259. void
  260. __shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
  261. {
  262. __data_.first().second()(__data_.first().first());
  263. __data_.first().second().~_Dp();
  264. }
  265. template <class _Tp, class _Dp, class _Alloc>
  266. void
  267. __shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
  268. {
  269. typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
  270. typedef allocator_traits<_Al> _ATraits;
  271. typedef pointer_traits<typename _ATraits::pointer> _PTraits;
  272. _Al __a(__data_.second());
  273. __data_.second().~_Alloc();
  274. __a.deallocate(_PTraits::pointer_to(*this), 1);
  275. }
  276. template <class _Tp, class _Alloc>
  277. struct __shared_ptr_emplace
  278. : __shared_weak_count
  279. {
  280. template<class ..._Args>
  281. _LIBCPP_HIDE_FROM_ABI
  282. explicit __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
  283. : __storage_(_VSTD::move(__a))
  284. {
  285. #if _LIBCPP_STD_VER > 17
  286. using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
  287. _TpAlloc __tmp(*__get_alloc());
  288. allocator_traits<_TpAlloc>::construct(__tmp, __get_elem(), _VSTD::forward<_Args>(__args)...);
  289. #else
  290. ::new ((void*)__get_elem()) _Tp(_VSTD::forward<_Args>(__args)...);
  291. #endif
  292. }
  293. _LIBCPP_HIDE_FROM_ABI
  294. _Alloc* __get_alloc() _NOEXCEPT { return __storage_.__get_alloc(); }
  295. _LIBCPP_HIDE_FROM_ABI
  296. _Tp* __get_elem() _NOEXCEPT { return __storage_.__get_elem(); }
  297. private:
  298. virtual void __on_zero_shared() _NOEXCEPT {
  299. #if _LIBCPP_STD_VER > 17
  300. using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
  301. _TpAlloc __tmp(*__get_alloc());
  302. allocator_traits<_TpAlloc>::destroy(__tmp, __get_elem());
  303. #else
  304. __get_elem()->~_Tp();
  305. #endif
  306. }
  307. virtual void __on_zero_shared_weak() _NOEXCEPT {
  308. using _ControlBlockAlloc = typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type;
  309. using _ControlBlockPointer = typename allocator_traits<_ControlBlockAlloc>::pointer;
  310. _ControlBlockAlloc __tmp(*__get_alloc());
  311. __storage_.~_Storage();
  312. allocator_traits<_ControlBlockAlloc>::deallocate(__tmp,
  313. pointer_traits<_ControlBlockPointer>::pointer_to(*this), 1);
  314. }
  315. // This class implements the control block for non-array shared pointers created
  316. // through `std::allocate_shared` and `std::make_shared`.
  317. //
  318. // In previous versions of the library, we used a compressed pair to store
  319. // both the _Alloc and the _Tp. This implies using EBO, which is incompatible
  320. // with Allocator construction for _Tp. To allow implementing P0674 in C++20,
  321. // we now use a properly aligned char buffer while making sure that we maintain
  322. // the same layout that we had when we used a compressed pair.
  323. using _CompressedPair = __compressed_pair<_Alloc, _Tp>;
  324. struct _ALIGNAS_TYPE(_CompressedPair) _Storage {
  325. char __blob_[sizeof(_CompressedPair)];
  326. _LIBCPP_HIDE_FROM_ABI explicit _Storage(_Alloc&& __a) {
  327. ::new ((void*)__get_alloc()) _Alloc(_VSTD::move(__a));
  328. }
  329. _LIBCPP_HIDE_FROM_ABI ~_Storage() {
  330. __get_alloc()->~_Alloc();
  331. }
  332. _Alloc* __get_alloc() _NOEXCEPT {
  333. _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
  334. typename _CompressedPair::_Base1* __first = _CompressedPair::__get_first_base(__as_pair);
  335. _Alloc *__alloc = reinterpret_cast<_Alloc*>(__first);
  336. return __alloc;
  337. }
  338. _LIBCPP_NO_CFI _Tp* __get_elem() _NOEXCEPT {
  339. _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
  340. typename _CompressedPair::_Base2* __second = _CompressedPair::__get_second_base(__as_pair);
  341. _Tp *__elem = reinterpret_cast<_Tp*>(__second);
  342. return __elem;
  343. }
  344. };
  345. static_assert(_LIBCPP_ALIGNOF(_Storage) == _LIBCPP_ALIGNOF(_CompressedPair), "");
  346. static_assert(sizeof(_Storage) == sizeof(_CompressedPair), "");
  347. _Storage __storage_;
  348. };
  349. struct __shared_ptr_dummy_rebind_allocator_type;
  350. template <>
  351. class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
  352. {
  353. public:
  354. template <class _Other>
  355. struct rebind
  356. {
  357. typedef allocator<_Other> other;
  358. };
  359. };
  360. template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
  361. template<class _Tp, class _Up>
  362. struct __compatible_with
  363. #if _LIBCPP_STD_VER > 14
  364. : is_convertible<remove_extent_t<_Tp>*, remove_extent_t<_Up>*> {};
  365. #else
  366. : is_convertible<_Tp*, _Up*> {};
  367. #endif // _LIBCPP_STD_VER > 14
  368. template <class _Ptr, class = void>
  369. struct __is_deletable : false_type { };
  370. template <class _Ptr>
  371. struct __is_deletable<_Ptr, decltype(delete declval<_Ptr>())> : true_type { };
  372. template <class _Ptr, class = void>
  373. struct __is_array_deletable : false_type { };
  374. template <class _Ptr>
  375. struct __is_array_deletable<_Ptr, decltype(delete[] declval<_Ptr>())> : true_type { };
  376. template <class _Dp, class _Pt,
  377. class = decltype(declval<_Dp>()(declval<_Pt>()))>
  378. static true_type __well_formed_deleter_test(int);
  379. template <class, class>
  380. static false_type __well_formed_deleter_test(...);
  381. template <class _Dp, class _Pt>
  382. struct __well_formed_deleter : decltype(__well_formed_deleter_test<_Dp, _Pt>(0)) {};
  383. template<class _Dp, class _Tp, class _Yp>
  384. struct __shared_ptr_deleter_ctor_reqs
  385. {
  386. static const bool value = __compatible_with<_Tp, _Yp>::value &&
  387. is_move_constructible<_Dp>::value &&
  388. __well_formed_deleter<_Dp, _Tp*>::value;
  389. };
  390. #if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI)
  391. # define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
  392. #else
  393. # define _LIBCPP_SHARED_PTR_TRIVIAL_ABI
  394. #endif
  395. template<class _Tp>
  396. class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr
  397. {
  398. public:
  399. #if _LIBCPP_STD_VER > 14
  400. typedef weak_ptr<_Tp> weak_type;
  401. typedef remove_extent_t<_Tp> element_type;
  402. #else
  403. typedef _Tp element_type;
  404. #endif
  405. private:
  406. element_type* __ptr_;
  407. __shared_weak_count* __cntrl_;
  408. public:
  409. _LIBCPP_HIDE_FROM_ABI
  410. _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT
  411. : __ptr_(nullptr),
  412. __cntrl_(nullptr)
  413. { }
  414. _LIBCPP_HIDE_FROM_ABI
  415. _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT
  416. : __ptr_(nullptr),
  417. __cntrl_(nullptr)
  418. { }
  419. template<class _Yp, class = __enable_if_t<
  420. _And<
  421. __compatible_with<_Yp, _Tp>
  422. // In C++03 we get errors when trying to do SFINAE with the
  423. // delete operator, so we always pretend that it's deletable.
  424. // The same happens on GCC.
  425. #if !defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_COMPILER_GCC)
  426. , _If<is_array<_Tp>::value, __is_array_deletable<_Yp*>, __is_deletable<_Yp*> >
  427. #endif
  428. >::value
  429. > >
  430. explicit shared_ptr(_Yp* __p) : __ptr_(__p) {
  431. unique_ptr<_Yp> __hold(__p);
  432. typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
  433. typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT > _CntrlBlk;
  434. __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT());
  435. __hold.release();
  436. __enable_weak_this(__p, __p);
  437. }
  438. template<class _Yp, class _Dp, class = __enable_if_t<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value> >
  439. _LIBCPP_HIDE_FROM_ABI
  440. shared_ptr(_Yp* __p, _Dp __d)
  441. : __ptr_(__p)
  442. {
  443. #ifndef _LIBCPP_NO_EXCEPTIONS
  444. try
  445. {
  446. #endif // _LIBCPP_NO_EXCEPTIONS
  447. typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
  448. typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
  449. #ifndef _LIBCPP_CXX03_LANG
  450. __cntrl_ = new _CntrlBlk(__p, _VSTD::move(__d), _AllocT());
  451. #else
  452. __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
  453. #endif // not _LIBCPP_CXX03_LANG
  454. __enable_weak_this(__p, __p);
  455. #ifndef _LIBCPP_NO_EXCEPTIONS
  456. }
  457. catch (...)
  458. {
  459. __d(__p);
  460. throw;
  461. }
  462. #endif // _LIBCPP_NO_EXCEPTIONS
  463. }
  464. template<class _Yp, class _Dp, class _Alloc, class = __enable_if_t<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value> >
  465. _LIBCPP_HIDE_FROM_ABI
  466. shared_ptr(_Yp* __p, _Dp __d, _Alloc __a)
  467. : __ptr_(__p)
  468. {
  469. #ifndef _LIBCPP_NO_EXCEPTIONS
  470. try
  471. {
  472. #endif // _LIBCPP_NO_EXCEPTIONS
  473. typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
  474. typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
  475. typedef __allocator_destructor<_A2> _D2;
  476. _A2 __a2(__a);
  477. unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
  478. ::new ((void*)_VSTD::addressof(*__hold2.get()))
  479. #ifndef _LIBCPP_CXX03_LANG
  480. _CntrlBlk(__p, _VSTD::move(__d), __a);
  481. #else
  482. _CntrlBlk(__p, __d, __a);
  483. #endif // not _LIBCPP_CXX03_LANG
  484. __cntrl_ = _VSTD::addressof(*__hold2.release());
  485. __enable_weak_this(__p, __p);
  486. #ifndef _LIBCPP_NO_EXCEPTIONS
  487. }
  488. catch (...)
  489. {
  490. __d(__p);
  491. throw;
  492. }
  493. #endif // _LIBCPP_NO_EXCEPTIONS
  494. }
  495. template<class _Dp>
  496. _LIBCPP_HIDE_FROM_ABI
  497. shared_ptr(nullptr_t __p, _Dp __d)
  498. : __ptr_(nullptr)
  499. {
  500. #ifndef _LIBCPP_NO_EXCEPTIONS
  501. try
  502. {
  503. #endif // _LIBCPP_NO_EXCEPTIONS
  504. typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
  505. typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
  506. #ifndef _LIBCPP_CXX03_LANG
  507. __cntrl_ = new _CntrlBlk(__p, _VSTD::move(__d), _AllocT());
  508. #else
  509. __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
  510. #endif // not _LIBCPP_CXX03_LANG
  511. #ifndef _LIBCPP_NO_EXCEPTIONS
  512. }
  513. catch (...)
  514. {
  515. __d(__p);
  516. throw;
  517. }
  518. #endif // _LIBCPP_NO_EXCEPTIONS
  519. }
  520. template<class _Dp, class _Alloc>
  521. _LIBCPP_HIDE_FROM_ABI
  522. shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
  523. : __ptr_(nullptr)
  524. {
  525. #ifndef _LIBCPP_NO_EXCEPTIONS
  526. try
  527. {
  528. #endif // _LIBCPP_NO_EXCEPTIONS
  529. typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
  530. typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
  531. typedef __allocator_destructor<_A2> _D2;
  532. _A2 __a2(__a);
  533. unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
  534. ::new ((void*)_VSTD::addressof(*__hold2.get()))
  535. #ifndef _LIBCPP_CXX03_LANG
  536. _CntrlBlk(__p, _VSTD::move(__d), __a);
  537. #else
  538. _CntrlBlk(__p, __d, __a);
  539. #endif // not _LIBCPP_CXX03_LANG
  540. __cntrl_ = _VSTD::addressof(*__hold2.release());
  541. #ifndef _LIBCPP_NO_EXCEPTIONS
  542. }
  543. catch (...)
  544. {
  545. __d(__p);
  546. throw;
  547. }
  548. #endif // _LIBCPP_NO_EXCEPTIONS
  549. }
  550. template<class _Yp>
  551. _LIBCPP_HIDE_FROM_ABI
  552. shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
  553. : __ptr_(__p),
  554. __cntrl_(__r.__cntrl_)
  555. {
  556. if (__cntrl_)
  557. __cntrl_->__add_shared();
  558. }
  559. _LIBCPP_HIDE_FROM_ABI
  560. shared_ptr(const shared_ptr& __r) _NOEXCEPT
  561. : __ptr_(__r.__ptr_),
  562. __cntrl_(__r.__cntrl_)
  563. {
  564. if (__cntrl_)
  565. __cntrl_->__add_shared();
  566. }
  567. template<class _Yp, class = __enable_if_t<__compatible_with<_Yp, _Tp>::value> >
  568. _LIBCPP_HIDE_FROM_ABI
  569. shared_ptr(const shared_ptr<_Yp>& __r) _NOEXCEPT
  570. : __ptr_(__r.__ptr_),
  571. __cntrl_(__r.__cntrl_)
  572. {
  573. if (__cntrl_)
  574. __cntrl_->__add_shared();
  575. }
  576. _LIBCPP_HIDE_FROM_ABI
  577. shared_ptr(shared_ptr&& __r) _NOEXCEPT
  578. : __ptr_(__r.__ptr_),
  579. __cntrl_(__r.__cntrl_)
  580. {
  581. __r.__ptr_ = nullptr;
  582. __r.__cntrl_ = nullptr;
  583. }
  584. template<class _Yp, class = __enable_if_t<__compatible_with<_Yp, _Tp>::value> >
  585. _LIBCPP_HIDE_FROM_ABI
  586. shared_ptr(shared_ptr<_Yp>&& __r) _NOEXCEPT
  587. : __ptr_(__r.__ptr_),
  588. __cntrl_(__r.__cntrl_)
  589. {
  590. __r.__ptr_ = nullptr;
  591. __r.__cntrl_ = nullptr;
  592. }
  593. template<class _Yp, class = __enable_if_t<__compatible_with<_Yp, _Tp>::value> >
  594. _LIBCPP_HIDE_FROM_ABI
  595. explicit shared_ptr(const weak_ptr<_Yp>& __r)
  596. : __ptr_(__r.__ptr_),
  597. __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
  598. {
  599. if (__cntrl_ == nullptr)
  600. __throw_bad_weak_ptr();
  601. }
  602. #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
  603. template<class _Yp, class = __enable_if_t<is_convertible<_Yp*, element_type*>::value> >
  604. _LIBCPP_HIDE_FROM_ABI
  605. shared_ptr(auto_ptr<_Yp>&& __r)
  606. : __ptr_(__r.get())
  607. {
  608. typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
  609. __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
  610. __enable_weak_this(__r.get(), __r.get());
  611. __r.release();
  612. }
  613. #endif
  614. template <class _Yp, class _Dp, class = __enable_if_t<
  615. !is_lvalue_reference<_Dp>::value &&
  616. is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
  617. > >
  618. _LIBCPP_HIDE_FROM_ABI
  619. shared_ptr(unique_ptr<_Yp, _Dp>&& __r)
  620. : __ptr_(__r.get())
  621. {
  622. #if _LIBCPP_STD_VER > 11
  623. if (__ptr_ == nullptr)
  624. __cntrl_ = nullptr;
  625. else
  626. #endif
  627. {
  628. typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
  629. typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer, _Dp, _AllocT > _CntrlBlk;
  630. __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
  631. __enable_weak_this(__r.get(), __r.get());
  632. }
  633. __r.release();
  634. }
  635. template <class _Yp, class _Dp, class = void, class = __enable_if_t<
  636. is_lvalue_reference<_Dp>::value &&
  637. is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
  638. > >
  639. _LIBCPP_HIDE_FROM_ABI
  640. shared_ptr(unique_ptr<_Yp, _Dp>&& __r)
  641. : __ptr_(__r.get())
  642. {
  643. #if _LIBCPP_STD_VER > 11
  644. if (__ptr_ == nullptr)
  645. __cntrl_ = nullptr;
  646. else
  647. #endif
  648. {
  649. typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
  650. typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer,
  651. reference_wrapper<typename remove_reference<_Dp>::type>,
  652. _AllocT > _CntrlBlk;
  653. __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
  654. __enable_weak_this(__r.get(), __r.get());
  655. }
  656. __r.release();
  657. }
  658. _LIBCPP_HIDE_FROM_ABI
  659. ~shared_ptr()
  660. {
  661. if (__cntrl_)
  662. __cntrl_->__release_shared();
  663. }
  664. _LIBCPP_HIDE_FROM_ABI
  665. shared_ptr<_Tp>& operator=(const shared_ptr& __r) _NOEXCEPT
  666. {
  667. shared_ptr(__r).swap(*this);
  668. return *this;
  669. }
  670. template<class _Yp, class = __enable_if_t<__compatible_with<_Yp, _Tp>::value> >
  671. _LIBCPP_HIDE_FROM_ABI
  672. shared_ptr<_Tp>& operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
  673. {
  674. shared_ptr(__r).swap(*this);
  675. return *this;
  676. }
  677. _LIBCPP_HIDE_FROM_ABI
  678. shared_ptr<_Tp>& operator=(shared_ptr&& __r) _NOEXCEPT
  679. {
  680. shared_ptr(_VSTD::move(__r)).swap(*this);
  681. return *this;
  682. }
  683. template<class _Yp, class = __enable_if_t<__compatible_with<_Yp, _Tp>::value> >
  684. _LIBCPP_HIDE_FROM_ABI
  685. shared_ptr<_Tp>& operator=(shared_ptr<_Yp>&& __r)
  686. {
  687. shared_ptr(_VSTD::move(__r)).swap(*this);
  688. return *this;
  689. }
  690. #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
  691. template<class _Yp, class = __enable_if_t<
  692. !is_array<_Yp>::value &&
  693. is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value
  694. > >
  695. _LIBCPP_HIDE_FROM_ABI
  696. shared_ptr<_Tp>& operator=(auto_ptr<_Yp>&& __r)
  697. {
  698. shared_ptr(_VSTD::move(__r)).swap(*this);
  699. return *this;
  700. }
  701. #endif
  702. template <class _Yp, class _Dp, class = __enable_if_t<
  703. is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
  704. > >
  705. _LIBCPP_HIDE_FROM_ABI
  706. shared_ptr<_Tp>& operator=(unique_ptr<_Yp, _Dp>&& __r)
  707. {
  708. shared_ptr(_VSTD::move(__r)).swap(*this);
  709. return *this;
  710. }
  711. _LIBCPP_HIDE_FROM_ABI
  712. void swap(shared_ptr& __r) _NOEXCEPT
  713. {
  714. _VSTD::swap(__ptr_, __r.__ptr_);
  715. _VSTD::swap(__cntrl_, __r.__cntrl_);
  716. }
  717. _LIBCPP_HIDE_FROM_ABI
  718. void reset() _NOEXCEPT
  719. {
  720. shared_ptr().swap(*this);
  721. }
  722. template<class _Yp, class = __enable_if_t<
  723. __compatible_with<_Yp, _Tp>::value
  724. > >
  725. _LIBCPP_HIDE_FROM_ABI
  726. void reset(_Yp* __p)
  727. {
  728. shared_ptr(__p).swap(*this);
  729. }
  730. template<class _Yp, class _Dp, class = __enable_if_t<
  731. __compatible_with<_Yp, _Tp>::value
  732. > >
  733. _LIBCPP_HIDE_FROM_ABI
  734. void reset(_Yp* __p, _Dp __d)
  735. {
  736. shared_ptr(__p, __d).swap(*this);
  737. }
  738. template<class _Yp, class _Dp, class _Alloc, class = __enable_if_t<
  739. __compatible_with<_Yp, _Tp>::value
  740. > >
  741. _LIBCPP_HIDE_FROM_ABI
  742. void reset(_Yp* __p, _Dp __d, _Alloc __a)
  743. {
  744. shared_ptr(__p, __d, __a).swap(*this);
  745. }
  746. _LIBCPP_HIDE_FROM_ABI
  747. element_type* get() const _NOEXCEPT
  748. {
  749. return __ptr_;
  750. }
  751. _LIBCPP_HIDE_FROM_ABI
  752. typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
  753. {
  754. return *__ptr_;
  755. }
  756. _LIBCPP_HIDE_FROM_ABI
  757. element_type* operator->() const _NOEXCEPT
  758. {
  759. static_assert(!is_array<_Tp>::value,
  760. "std::shared_ptr<T>::operator-> is only valid when T is not an array type.");
  761. return __ptr_;
  762. }
  763. _LIBCPP_HIDE_FROM_ABI
  764. long use_count() const _NOEXCEPT
  765. {
  766. return __cntrl_ ? __cntrl_->use_count() : 0;
  767. }
  768. _LIBCPP_HIDE_FROM_ABI
  769. bool unique() const _NOEXCEPT
  770. {
  771. return use_count() == 1;
  772. }
  773. _LIBCPP_HIDE_FROM_ABI
  774. explicit operator bool() const _NOEXCEPT
  775. {
  776. return get() != nullptr;
  777. }
  778. template <class _Up>
  779. _LIBCPP_HIDE_FROM_ABI
  780. bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
  781. {
  782. return __cntrl_ < __p.__cntrl_;
  783. }
  784. template <class _Up>
  785. _LIBCPP_HIDE_FROM_ABI
  786. bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
  787. {
  788. return __cntrl_ < __p.__cntrl_;
  789. }
  790. _LIBCPP_HIDE_FROM_ABI
  791. bool __owner_equivalent(const shared_ptr& __p) const
  792. {
  793. return __cntrl_ == __p.__cntrl_;
  794. }
  795. #if _LIBCPP_STD_VER > 14
  796. _LIBCPP_HIDE_FROM_ABI
  797. typename add_lvalue_reference<element_type>::type operator[](ptrdiff_t __i) const
  798. {
  799. static_assert(is_array<_Tp>::value,
  800. "std::shared_ptr<T>::operator[] is only valid when T is an array type.");
  801. return __ptr_[__i];
  802. }
  803. #endif
  804. #ifndef _LIBCPP_NO_RTTI
  805. template <class _Dp>
  806. _LIBCPP_HIDE_FROM_ABI
  807. _Dp* __get_deleter() const _NOEXCEPT
  808. {
  809. return static_cast<_Dp*>(__cntrl_
  810. ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
  811. : nullptr);
  812. }
  813. #endif // _LIBCPP_NO_RTTI
  814. template<class _Yp, class _CntrlBlk>
  815. _LIBCPP_HIDE_FROM_ABI
  816. static shared_ptr<_Tp> __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT
  817. {
  818. shared_ptr<_Tp> __r;
  819. __r.__ptr_ = __p;
  820. __r.__cntrl_ = __cntrl;
  821. __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
  822. return __r;
  823. }
  824. private:
  825. template <class _Yp, bool = is_function<_Yp>::value>
  826. struct __shared_ptr_default_allocator
  827. {
  828. typedef allocator<_Yp> type;
  829. };
  830. template <class _Yp>
  831. struct __shared_ptr_default_allocator<_Yp, true>
  832. {
  833. typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
  834. };
  835. template <class _Yp, class _OrigPtr, class = __enable_if_t<
  836. is_convertible<_OrigPtr*, const enable_shared_from_this<_Yp>*>::value
  837. > >
  838. _LIBCPP_HIDE_FROM_ABI
  839. void __enable_weak_this(const enable_shared_from_this<_Yp>* __e, _OrigPtr* __ptr) _NOEXCEPT
  840. {
  841. typedef typename remove_cv<_Yp>::type _RawYp;
  842. if (__e && __e->__weak_this_.expired())
  843. {
  844. __e->__weak_this_ = shared_ptr<_RawYp>(*this,
  845. const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
  846. }
  847. }
  848. _LIBCPP_HIDE_FROM_ABI void __enable_weak_this(...) _NOEXCEPT { }
  849. template <class, class _Yp>
  850. struct __shared_ptr_default_delete
  851. : default_delete<_Yp>
  852. { };
  853. template <class _Yp, class _Un, size_t _Sz>
  854. struct __shared_ptr_default_delete<_Yp[_Sz], _Un>
  855. : default_delete<_Yp[]>
  856. { };
  857. template <class _Yp, class _Un>
  858. struct __shared_ptr_default_delete<_Yp[], _Un>
  859. : default_delete<_Yp[]>
  860. { };
  861. template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
  862. template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
  863. };
  864. #if _LIBCPP_STD_VER > 14
  865. template<class _Tp>
  866. shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
  867. template<class _Tp, class _Dp>
  868. shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
  869. #endif
  870. //
  871. // std::allocate_shared and std::make_shared
  872. //
  873. template<class _Tp, class _Alloc, class ..._Args, class = __enable_if_t<!is_array<_Tp>::value> >
  874. _LIBCPP_HIDE_FROM_ABI
  875. shared_ptr<_Tp> allocate_shared(const _Alloc& __a, _Args&& ...__args)
  876. {
  877. using _ControlBlock = __shared_ptr_emplace<_Tp, _Alloc>;
  878. using _ControlBlockAllocator = typename __allocator_traits_rebind<_Alloc, _ControlBlock>::type;
  879. __allocation_guard<_ControlBlockAllocator> __guard(__a, 1);
  880. ::new ((void*)_VSTD::addressof(*__guard.__get())) _ControlBlock(__a, _VSTD::forward<_Args>(__args)...);
  881. auto __control_block = __guard.__release_ptr();
  882. return shared_ptr<_Tp>::__create_with_control_block((*__control_block).__get_elem(), _VSTD::addressof(*__control_block));
  883. }
  884. template<class _Tp, class ..._Args, class = __enable_if_t<!is_array<_Tp>::value> >
  885. _LIBCPP_HIDE_FROM_ABI
  886. shared_ptr<_Tp> make_shared(_Args&& ...__args)
  887. {
  888. return _VSTD::allocate_shared<_Tp>(allocator<_Tp>(), _VSTD::forward<_Args>(__args)...);
  889. }
  890. template<class _Tp, class _Up>
  891. inline _LIBCPP_INLINE_VISIBILITY
  892. bool
  893. operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
  894. {
  895. return __x.get() == __y.get();
  896. }
  897. template<class _Tp, class _Up>
  898. inline _LIBCPP_INLINE_VISIBILITY
  899. bool
  900. operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
  901. {
  902. return !(__x == __y);
  903. }
  904. template<class _Tp, class _Up>
  905. inline _LIBCPP_INLINE_VISIBILITY
  906. bool
  907. operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
  908. {
  909. #if _LIBCPP_STD_VER <= 11
  910. typedef typename common_type<_Tp*, _Up*>::type _Vp;
  911. return less<_Vp>()(__x.get(), __y.get());
  912. #else
  913. return less<>()(__x.get(), __y.get());
  914. #endif
  915. }
  916. template<class _Tp, class _Up>
  917. inline _LIBCPP_INLINE_VISIBILITY
  918. bool
  919. operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
  920. {
  921. return __y < __x;
  922. }
  923. template<class _Tp, class _Up>
  924. inline _LIBCPP_INLINE_VISIBILITY
  925. bool
  926. operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
  927. {
  928. return !(__y < __x);
  929. }
  930. template<class _Tp, class _Up>
  931. inline _LIBCPP_INLINE_VISIBILITY
  932. bool
  933. operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
  934. {
  935. return !(__x < __y);
  936. }
  937. template<class _Tp>
  938. inline _LIBCPP_INLINE_VISIBILITY
  939. bool
  940. operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
  941. {
  942. return !__x;
  943. }
  944. template<class _Tp>
  945. inline _LIBCPP_INLINE_VISIBILITY
  946. bool
  947. operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
  948. {
  949. return !__x;
  950. }
  951. template<class _Tp>
  952. inline _LIBCPP_INLINE_VISIBILITY
  953. bool
  954. operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
  955. {
  956. return static_cast<bool>(__x);
  957. }
  958. template<class _Tp>
  959. inline _LIBCPP_INLINE_VISIBILITY
  960. bool
  961. operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
  962. {
  963. return static_cast<bool>(__x);
  964. }
  965. template<class _Tp>
  966. inline _LIBCPP_INLINE_VISIBILITY
  967. bool
  968. operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
  969. {
  970. return less<_Tp*>()(__x.get(), nullptr);
  971. }
  972. template<class _Tp>
  973. inline _LIBCPP_INLINE_VISIBILITY
  974. bool
  975. operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
  976. {
  977. return less<_Tp*>()(nullptr, __x.get());
  978. }
  979. template<class _Tp>
  980. inline _LIBCPP_INLINE_VISIBILITY
  981. bool
  982. operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
  983. {
  984. return nullptr < __x;
  985. }
  986. template<class _Tp>
  987. inline _LIBCPP_INLINE_VISIBILITY
  988. bool
  989. operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
  990. {
  991. return __x < nullptr;
  992. }
  993. template<class _Tp>
  994. inline _LIBCPP_INLINE_VISIBILITY
  995. bool
  996. operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
  997. {
  998. return !(nullptr < __x);
  999. }
  1000. template<class _Tp>
  1001. inline _LIBCPP_INLINE_VISIBILITY
  1002. bool
  1003. operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
  1004. {
  1005. return !(__x < nullptr);
  1006. }
  1007. template<class _Tp>
  1008. inline _LIBCPP_INLINE_VISIBILITY
  1009. bool
  1010. operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
  1011. {
  1012. return !(__x < nullptr);
  1013. }
  1014. template<class _Tp>
  1015. inline _LIBCPP_INLINE_VISIBILITY
  1016. bool
  1017. operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
  1018. {
  1019. return !(nullptr < __x);
  1020. }
  1021. template<class _Tp>
  1022. inline _LIBCPP_INLINE_VISIBILITY
  1023. void
  1024. swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
  1025. {
  1026. __x.swap(__y);
  1027. }
  1028. template<class _Tp, class _Up>
  1029. inline _LIBCPP_INLINE_VISIBILITY
  1030. shared_ptr<_Tp>
  1031. static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
  1032. {
  1033. return shared_ptr<_Tp>(__r,
  1034. static_cast<
  1035. typename shared_ptr<_Tp>::element_type*>(__r.get()));
  1036. }
  1037. template<class _Tp, class _Up>
  1038. inline _LIBCPP_INLINE_VISIBILITY
  1039. shared_ptr<_Tp>
  1040. dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
  1041. {
  1042. typedef typename shared_ptr<_Tp>::element_type _ET;
  1043. _ET* __p = dynamic_cast<_ET*>(__r.get());
  1044. return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
  1045. }
  1046. template<class _Tp, class _Up>
  1047. shared_ptr<_Tp>
  1048. const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
  1049. {
  1050. typedef typename shared_ptr<_Tp>::element_type _RTp;
  1051. return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
  1052. }
  1053. template<class _Tp, class _Up>
  1054. shared_ptr<_Tp>
  1055. reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
  1056. {
  1057. return shared_ptr<_Tp>(__r,
  1058. reinterpret_cast<
  1059. typename shared_ptr<_Tp>::element_type*>(__r.get()));
  1060. }
  1061. #ifndef _LIBCPP_NO_RTTI
  1062. template<class _Dp, class _Tp>
  1063. inline _LIBCPP_INLINE_VISIBILITY
  1064. _Dp*
  1065. get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
  1066. {
  1067. return __p.template __get_deleter<_Dp>();
  1068. }
  1069. #endif // _LIBCPP_NO_RTTI
  1070. template<class _Tp>
  1071. class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr
  1072. {
  1073. public:
  1074. #if _LIBCPP_STD_VER > 14
  1075. typedef remove_extent_t<_Tp> element_type;
  1076. #else
  1077. typedef _Tp element_type;
  1078. #endif
  1079. private:
  1080. element_type* __ptr_;
  1081. __shared_weak_count* __cntrl_;
  1082. public:
  1083. _LIBCPP_INLINE_VISIBILITY
  1084. _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
  1085. template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
  1086. typename enable_if<__compatible_with<_Yp, _Tp>::value, __nat*>::type = 0)
  1087. _NOEXCEPT;
  1088. _LIBCPP_INLINE_VISIBILITY
  1089. weak_ptr(weak_ptr const& __r) _NOEXCEPT;
  1090. template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
  1091. typename enable_if<__compatible_with<_Yp, _Tp>::value, __nat*>::type = 0)
  1092. _NOEXCEPT;
  1093. _LIBCPP_INLINE_VISIBILITY
  1094. weak_ptr(weak_ptr&& __r) _NOEXCEPT;
  1095. template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
  1096. typename enable_if<__compatible_with<_Yp, _Tp>::value, __nat*>::type = 0)
  1097. _NOEXCEPT;
  1098. ~weak_ptr();
  1099. _LIBCPP_INLINE_VISIBILITY
  1100. weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
  1101. template<class _Yp>
  1102. typename enable_if
  1103. <
  1104. __compatible_with<_Yp, _Tp>::value,
  1105. weak_ptr&
  1106. >::type
  1107. _LIBCPP_INLINE_VISIBILITY
  1108. operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
  1109. _LIBCPP_INLINE_VISIBILITY
  1110. weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
  1111. template<class _Yp>
  1112. typename enable_if
  1113. <
  1114. __compatible_with<_Yp, _Tp>::value,
  1115. weak_ptr&
  1116. >::type
  1117. _LIBCPP_INLINE_VISIBILITY
  1118. operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
  1119. template<class _Yp>
  1120. typename enable_if
  1121. <
  1122. __compatible_with<_Yp, _Tp>::value,
  1123. weak_ptr&
  1124. >::type
  1125. _LIBCPP_INLINE_VISIBILITY
  1126. operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
  1127. _LIBCPP_INLINE_VISIBILITY
  1128. void swap(weak_ptr& __r) _NOEXCEPT;
  1129. _LIBCPP_INLINE_VISIBILITY
  1130. void reset() _NOEXCEPT;
  1131. _LIBCPP_INLINE_VISIBILITY
  1132. long use_count() const _NOEXCEPT
  1133. {return __cntrl_ ? __cntrl_->use_count() : 0;}
  1134. _LIBCPP_INLINE_VISIBILITY
  1135. bool expired() const _NOEXCEPT
  1136. {return __cntrl_ == nullptr || __cntrl_->use_count() == 0;}
  1137. shared_ptr<_Tp> lock() const _NOEXCEPT;
  1138. template<class _Up>
  1139. _LIBCPP_INLINE_VISIBILITY
  1140. bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
  1141. {return __cntrl_ < __r.__cntrl_;}
  1142. template<class _Up>
  1143. _LIBCPP_INLINE_VISIBILITY
  1144. bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
  1145. {return __cntrl_ < __r.__cntrl_;}
  1146. template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
  1147. template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
  1148. };
  1149. #if _LIBCPP_STD_VER > 14
  1150. template<class _Tp>
  1151. weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
  1152. #endif
  1153. template<class _Tp>
  1154. inline
  1155. _LIBCPP_CONSTEXPR
  1156. weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
  1157. : __ptr_(nullptr),
  1158. __cntrl_(nullptr)
  1159. {
  1160. }
  1161. template<class _Tp>
  1162. inline
  1163. weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
  1164. : __ptr_(__r.__ptr_),
  1165. __cntrl_(__r.__cntrl_)
  1166. {
  1167. if (__cntrl_)
  1168. __cntrl_->__add_weak();
  1169. }
  1170. template<class _Tp>
  1171. template<class _Yp>
  1172. inline
  1173. weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
  1174. typename enable_if<__compatible_with<_Yp, _Tp>::value, __nat*>::type)
  1175. _NOEXCEPT
  1176. : __ptr_(__r.__ptr_),
  1177. __cntrl_(__r.__cntrl_)
  1178. {
  1179. if (__cntrl_)
  1180. __cntrl_->__add_weak();
  1181. }
  1182. template<class _Tp>
  1183. template<class _Yp>
  1184. inline
  1185. weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
  1186. typename enable_if<__compatible_with<_Yp, _Tp>::value, __nat*>::type)
  1187. _NOEXCEPT
  1188. : __ptr_(__r.__ptr_),
  1189. __cntrl_(__r.__cntrl_)
  1190. {
  1191. if (__cntrl_)
  1192. __cntrl_->__add_weak();
  1193. }
  1194. template<class _Tp>
  1195. inline
  1196. weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
  1197. : __ptr_(__r.__ptr_),
  1198. __cntrl_(__r.__cntrl_)
  1199. {
  1200. __r.__ptr_ = nullptr;
  1201. __r.__cntrl_ = nullptr;
  1202. }
  1203. template<class _Tp>
  1204. template<class _Yp>
  1205. inline
  1206. weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
  1207. typename enable_if<__compatible_with<_Yp, _Tp>::value, __nat*>::type)
  1208. _NOEXCEPT
  1209. : __ptr_(__r.__ptr_),
  1210. __cntrl_(__r.__cntrl_)
  1211. {
  1212. __r.__ptr_ = nullptr;
  1213. __r.__cntrl_ = nullptr;
  1214. }
  1215. template<class _Tp>
  1216. weak_ptr<_Tp>::~weak_ptr()
  1217. {
  1218. if (__cntrl_)
  1219. __cntrl_->__release_weak();
  1220. }
  1221. template<class _Tp>
  1222. inline
  1223. weak_ptr<_Tp>&
  1224. weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
  1225. {
  1226. weak_ptr(__r).swap(*this);
  1227. return *this;
  1228. }
  1229. template<class _Tp>
  1230. template<class _Yp>
  1231. inline
  1232. typename enable_if
  1233. <
  1234. __compatible_with<_Yp, _Tp>::value,
  1235. weak_ptr<_Tp>&
  1236. >::type
  1237. weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
  1238. {
  1239. weak_ptr(__r).swap(*this);
  1240. return *this;
  1241. }
  1242. template<class _Tp>
  1243. inline
  1244. weak_ptr<_Tp>&
  1245. weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
  1246. {
  1247. weak_ptr(_VSTD::move(__r)).swap(*this);
  1248. return *this;
  1249. }
  1250. template<class _Tp>
  1251. template<class _Yp>
  1252. inline
  1253. typename enable_if
  1254. <
  1255. __compatible_with<_Yp, _Tp>::value,
  1256. weak_ptr<_Tp>&
  1257. >::type
  1258. weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
  1259. {
  1260. weak_ptr(_VSTD::move(__r)).swap(*this);
  1261. return *this;
  1262. }
  1263. template<class _Tp>
  1264. template<class _Yp>
  1265. inline
  1266. typename enable_if
  1267. <
  1268. __compatible_with<_Yp, _Tp>::value,
  1269. weak_ptr<_Tp>&
  1270. >::type
  1271. weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
  1272. {
  1273. weak_ptr(__r).swap(*this);
  1274. return *this;
  1275. }
  1276. template<class _Tp>
  1277. inline
  1278. void
  1279. weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
  1280. {
  1281. _VSTD::swap(__ptr_, __r.__ptr_);
  1282. _VSTD::swap(__cntrl_, __r.__cntrl_);
  1283. }
  1284. template<class _Tp>
  1285. inline _LIBCPP_INLINE_VISIBILITY
  1286. void
  1287. swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
  1288. {
  1289. __x.swap(__y);
  1290. }
  1291. template<class _Tp>
  1292. inline
  1293. void
  1294. weak_ptr<_Tp>::reset() _NOEXCEPT
  1295. {
  1296. weak_ptr().swap(*this);
  1297. }
  1298. template<class _Tp>
  1299. shared_ptr<_Tp>
  1300. weak_ptr<_Tp>::lock() const _NOEXCEPT
  1301. {
  1302. shared_ptr<_Tp> __r;
  1303. __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
  1304. if (__r.__cntrl_)
  1305. __r.__ptr_ = __ptr_;
  1306. return __r;
  1307. }
  1308. #if _LIBCPP_STD_VER > 14
  1309. template <class _Tp = void> struct owner_less;
  1310. #else
  1311. template <class _Tp> struct owner_less;
  1312. #endif
  1313. _LIBCPP_SUPPRESS_DEPRECATED_PUSH
  1314. template <class _Tp>
  1315. struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
  1316. #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
  1317. : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
  1318. #endif
  1319. {
  1320. _LIBCPP_SUPPRESS_DEPRECATED_POP
  1321. #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
  1322. _LIBCPP_DEPRECATED_IN_CXX17 typedef bool result_type;
  1323. _LIBCPP_DEPRECATED_IN_CXX17 typedef shared_ptr<_Tp> first_argument_type;
  1324. _LIBCPP_DEPRECATED_IN_CXX17 typedef shared_ptr<_Tp> second_argument_type;
  1325. #endif
  1326. _LIBCPP_INLINE_VISIBILITY
  1327. bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
  1328. {return __x.owner_before(__y);}
  1329. _LIBCPP_INLINE_VISIBILITY
  1330. bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
  1331. {return __x.owner_before(__y);}
  1332. _LIBCPP_INLINE_VISIBILITY
  1333. bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
  1334. {return __x.owner_before(__y);}
  1335. };
  1336. _LIBCPP_SUPPRESS_DEPRECATED_PUSH
  1337. template <class _Tp>
  1338. struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
  1339. #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
  1340. : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
  1341. #endif
  1342. {
  1343. _LIBCPP_SUPPRESS_DEPRECATED_POP
  1344. #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
  1345. _LIBCPP_DEPRECATED_IN_CXX17 typedef bool result_type;
  1346. _LIBCPP_DEPRECATED_IN_CXX17 typedef weak_ptr<_Tp> first_argument_type;
  1347. _LIBCPP_DEPRECATED_IN_CXX17 typedef weak_ptr<_Tp> second_argument_type;
  1348. #endif
  1349. _LIBCPP_INLINE_VISIBILITY
  1350. bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
  1351. {return __x.owner_before(__y);}
  1352. _LIBCPP_INLINE_VISIBILITY
  1353. bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
  1354. {return __x.owner_before(__y);}
  1355. _LIBCPP_INLINE_VISIBILITY
  1356. bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
  1357. {return __x.owner_before(__y);}
  1358. };
  1359. #if _LIBCPP_STD_VER > 14
  1360. template <>
  1361. struct _LIBCPP_TEMPLATE_VIS owner_less<void>
  1362. {
  1363. template <class _Tp, class _Up>
  1364. _LIBCPP_INLINE_VISIBILITY
  1365. bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
  1366. {return __x.owner_before(__y);}
  1367. template <class _Tp, class _Up>
  1368. _LIBCPP_INLINE_VISIBILITY
  1369. bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
  1370. {return __x.owner_before(__y);}
  1371. template <class _Tp, class _Up>
  1372. _LIBCPP_INLINE_VISIBILITY
  1373. bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
  1374. {return __x.owner_before(__y);}
  1375. template <class _Tp, class _Up>
  1376. _LIBCPP_INLINE_VISIBILITY
  1377. bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
  1378. {return __x.owner_before(__y);}
  1379. typedef void is_transparent;
  1380. };
  1381. #endif
  1382. template<class _Tp>
  1383. class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
  1384. {
  1385. mutable weak_ptr<_Tp> __weak_this_;
  1386. protected:
  1387. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  1388. enable_shared_from_this() _NOEXCEPT {}
  1389. _LIBCPP_INLINE_VISIBILITY
  1390. enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
  1391. _LIBCPP_INLINE_VISIBILITY
  1392. enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
  1393. {return *this;}
  1394. _LIBCPP_INLINE_VISIBILITY
  1395. ~enable_shared_from_this() {}
  1396. public:
  1397. _LIBCPP_INLINE_VISIBILITY
  1398. shared_ptr<_Tp> shared_from_this()
  1399. {return shared_ptr<_Tp>(__weak_this_);}
  1400. _LIBCPP_INLINE_VISIBILITY
  1401. shared_ptr<_Tp const> shared_from_this() const
  1402. {return shared_ptr<const _Tp>(__weak_this_);}
  1403. #if _LIBCPP_STD_VER > 14
  1404. _LIBCPP_INLINE_VISIBILITY
  1405. weak_ptr<_Tp> weak_from_this() _NOEXCEPT
  1406. { return __weak_this_; }
  1407. _LIBCPP_INLINE_VISIBILITY
  1408. weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
  1409. { return __weak_this_; }
  1410. #endif // _LIBCPP_STD_VER > 14
  1411. template <class _Up> friend class shared_ptr;
  1412. };
  1413. template <class _Tp> struct _LIBCPP_TEMPLATE_VIS hash;
  1414. template <class _Tp>
  1415. struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
  1416. {
  1417. #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
  1418. _LIBCPP_DEPRECATED_IN_CXX17 typedef shared_ptr<_Tp> argument_type;
  1419. _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
  1420. #endif
  1421. _LIBCPP_INLINE_VISIBILITY
  1422. size_t operator()(const shared_ptr<_Tp>& __ptr) const _NOEXCEPT
  1423. {
  1424. return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get());
  1425. }
  1426. };
  1427. template<class _CharT, class _Traits, class _Yp>
  1428. inline _LIBCPP_INLINE_VISIBILITY
  1429. basic_ostream<_CharT, _Traits>&
  1430. operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
  1431. #if !defined(_LIBCPP_HAS_NO_THREADS)
  1432. class _LIBCPP_TYPE_VIS __sp_mut
  1433. {
  1434. void* __lx;
  1435. public:
  1436. void lock() _NOEXCEPT;
  1437. void unlock() _NOEXCEPT;
  1438. private:
  1439. _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
  1440. __sp_mut(const __sp_mut&);
  1441. __sp_mut& operator=(const __sp_mut&);
  1442. friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
  1443. };
  1444. _LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
  1445. __sp_mut& __get_sp_mut(const void*);
  1446. template <class _Tp>
  1447. inline _LIBCPP_INLINE_VISIBILITY
  1448. bool
  1449. atomic_is_lock_free(const shared_ptr<_Tp>*)
  1450. {
  1451. return false;
  1452. }
  1453. template <class _Tp>
  1454. _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
  1455. shared_ptr<_Tp>
  1456. atomic_load(const shared_ptr<_Tp>* __p)
  1457. {
  1458. __sp_mut& __m = __get_sp_mut(__p);
  1459. __m.lock();
  1460. shared_ptr<_Tp> __q = *__p;
  1461. __m.unlock();
  1462. return __q;
  1463. }
  1464. template <class _Tp>
  1465. inline _LIBCPP_INLINE_VISIBILITY
  1466. _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
  1467. shared_ptr<_Tp>
  1468. atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
  1469. {
  1470. return atomic_load(__p);
  1471. }
  1472. template <class _Tp>
  1473. _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
  1474. void
  1475. atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
  1476. {
  1477. __sp_mut& __m = __get_sp_mut(__p);
  1478. __m.lock();
  1479. __p->swap(__r);
  1480. __m.unlock();
  1481. }
  1482. template <class _Tp>
  1483. inline _LIBCPP_INLINE_VISIBILITY
  1484. _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
  1485. void
  1486. atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
  1487. {
  1488. atomic_store(__p, __r);
  1489. }
  1490. template <class _Tp>
  1491. _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
  1492. shared_ptr<_Tp>
  1493. atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
  1494. {
  1495. __sp_mut& __m = __get_sp_mut(__p);
  1496. __m.lock();
  1497. __p->swap(__r);
  1498. __m.unlock();
  1499. return __r;
  1500. }
  1501. template <class _Tp>
  1502. inline _LIBCPP_INLINE_VISIBILITY
  1503. _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
  1504. shared_ptr<_Tp>
  1505. atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
  1506. {
  1507. return atomic_exchange(__p, __r);
  1508. }
  1509. template <class _Tp>
  1510. _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
  1511. bool
  1512. atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
  1513. {
  1514. shared_ptr<_Tp> __temp;
  1515. __sp_mut& __m = __get_sp_mut(__p);
  1516. __m.lock();
  1517. if (__p->__owner_equivalent(*__v))
  1518. {
  1519. _VSTD::swap(__temp, *__p);
  1520. *__p = __w;
  1521. __m.unlock();
  1522. return true;
  1523. }
  1524. _VSTD::swap(__temp, *__v);
  1525. *__v = *__p;
  1526. __m.unlock();
  1527. return false;
  1528. }
  1529. template <class _Tp>
  1530. inline _LIBCPP_INLINE_VISIBILITY
  1531. _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
  1532. bool
  1533. atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
  1534. {
  1535. return atomic_compare_exchange_strong(__p, __v, __w);
  1536. }
  1537. template <class _Tp>
  1538. inline _LIBCPP_INLINE_VISIBILITY
  1539. _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
  1540. bool
  1541. atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
  1542. shared_ptr<_Tp> __w, memory_order, memory_order)
  1543. {
  1544. return atomic_compare_exchange_strong(__p, __v, __w);
  1545. }
  1546. template <class _Tp>
  1547. inline _LIBCPP_INLINE_VISIBILITY
  1548. _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
  1549. bool
  1550. atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
  1551. shared_ptr<_Tp> __w, memory_order, memory_order)
  1552. {
  1553. return atomic_compare_exchange_weak(__p, __v, __w);
  1554. }
  1555. #endif // !defined(_LIBCPP_HAS_NO_THREADS)
  1556. _LIBCPP_END_NAMESPACE_STD
  1557. #endif // _LIBCPP___MEMORY_SHARED_PTR_H