unique_ptr.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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_UNIQUE_PTR_H
  10. #define _LIBCPP___MEMORY_UNIQUE_PTR_H
  11. #include <__config>
  12. #include <__functional/hash.h>
  13. #include <__functional/operations.h>
  14. #include <__memory/allocator_traits.h> // __pointer
  15. #include <__memory/compressed_pair.h>
  16. #include <__utility/forward.h>
  17. #include <__utility/move.h>
  18. #include <cstddef>
  19. #include <stlfwd>
  20. #include <type_traits>
  21. #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
  22. # include <__memory/auto_ptr.h>
  23. #endif
  24. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  25. # pragma GCC system_header
  26. #endif
  27. _LIBCPP_BEGIN_NAMESPACE_STD
  28. template <class _Tp>
  29. struct _LIBCPP_TEMPLATE_VIS default_delete {
  30. static_assert(!is_function<_Tp>::value,
  31. "default_delete cannot be instantiated for function types");
  32. #ifndef _LIBCPP_CXX03_LANG
  33. _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
  34. #else
  35. _LIBCPP_INLINE_VISIBILITY default_delete() {}
  36. #endif
  37. template <class _Up>
  38. _LIBCPP_INLINE_VISIBILITY
  39. default_delete(const default_delete<_Up>&,
  40. typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
  41. 0) _NOEXCEPT {}
  42. _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
  43. static_assert(sizeof(_Tp) > 0,
  44. "default_delete can not delete incomplete type");
  45. static_assert(!is_void<_Tp>::value,
  46. "default_delete can not delete incomplete type");
  47. delete __ptr;
  48. }
  49. };
  50. template <class _Tp>
  51. struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
  52. private:
  53. template <class _Up>
  54. struct _EnableIfConvertible
  55. : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
  56. public:
  57. #ifndef _LIBCPP_CXX03_LANG
  58. _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
  59. #else
  60. _LIBCPP_INLINE_VISIBILITY default_delete() {}
  61. #endif
  62. template <class _Up>
  63. _LIBCPP_INLINE_VISIBILITY
  64. default_delete(const default_delete<_Up[]>&,
  65. typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
  66. template <class _Up>
  67. _LIBCPP_INLINE_VISIBILITY
  68. typename _EnableIfConvertible<_Up>::type
  69. operator()(_Up* __ptr) const _NOEXCEPT {
  70. static_assert(sizeof(_Tp) > 0,
  71. "default_delete can not delete incomplete type");
  72. static_assert(!is_void<_Tp>::value,
  73. "default_delete can not delete void type");
  74. delete[] __ptr;
  75. }
  76. };
  77. template <class _Deleter>
  78. struct __unique_ptr_deleter_sfinae {
  79. static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
  80. typedef const _Deleter& __lval_ref_type;
  81. typedef _Deleter&& __good_rval_ref_type;
  82. typedef true_type __enable_rval_overload;
  83. };
  84. template <class _Deleter>
  85. struct __unique_ptr_deleter_sfinae<_Deleter const&> {
  86. typedef const _Deleter& __lval_ref_type;
  87. typedef const _Deleter&& __bad_rval_ref_type;
  88. typedef false_type __enable_rval_overload;
  89. };
  90. template <class _Deleter>
  91. struct __unique_ptr_deleter_sfinae<_Deleter&> {
  92. typedef _Deleter& __lval_ref_type;
  93. typedef _Deleter&& __bad_rval_ref_type;
  94. typedef false_type __enable_rval_overload;
  95. };
  96. #if defined(_LIBCPP_ABI_ENABLE_UNIQUE_PTR_TRIVIAL_ABI)
  97. # define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
  98. #else
  99. # define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI
  100. #endif
  101. template <class _Tp, class _Dp>
  102. class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr {
  103. public:
  104. typedef _Tp element_type;
  105. typedef _Dp deleter_type;
  106. typedef _LIBCPP_NODEBUG typename __pointer<_Tp, deleter_type>::type pointer;
  107. static_assert(!is_rvalue_reference<deleter_type>::value,
  108. "the specified deleter type cannot be an rvalue reference");
  109. private:
  110. __compressed_pair<pointer, deleter_type> __ptr_;
  111. struct __nat { int __for_bool_; };
  112. typedef _LIBCPP_NODEBUG __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
  113. template <bool _Dummy>
  114. using _LValRefType _LIBCPP_NODEBUG =
  115. typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
  116. template <bool _Dummy>
  117. using _GoodRValRefType _LIBCPP_NODEBUG =
  118. typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
  119. template <bool _Dummy>
  120. using _BadRValRefType _LIBCPP_NODEBUG =
  121. typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
  122. template <bool _Dummy, class _Deleter = typename __dependent_type<
  123. __identity<deleter_type>, _Dummy>::type>
  124. using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG =
  125. typename enable_if<is_default_constructible<_Deleter>::value &&
  126. !is_pointer<_Deleter>::value>::type;
  127. template <class _ArgType>
  128. using _EnableIfDeleterConstructible _LIBCPP_NODEBUG =
  129. typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
  130. template <class _UPtr, class _Up>
  131. using _EnableIfMoveConvertible _LIBCPP_NODEBUG = typename enable_if<
  132. is_convertible<typename _UPtr::pointer, pointer>::value &&
  133. !is_array<_Up>::value
  134. >::type;
  135. template <class _UDel>
  136. using _EnableIfDeleterConvertible _LIBCPP_NODEBUG = typename enable_if<
  137. (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
  138. (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
  139. >::type;
  140. template <class _UDel>
  141. using _EnableIfDeleterAssignable = typename enable_if<
  142. is_assignable<_Dp&, _UDel&&>::value
  143. >::type;
  144. public:
  145. template <bool _Dummy = true,
  146. class = _EnableIfDeleterDefaultConstructible<_Dummy> >
  147. _LIBCPP_INLINE_VISIBILITY
  148. _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(__value_init_tag(), __value_init_tag()) {}
  149. template <bool _Dummy = true,
  150. class = _EnableIfDeleterDefaultConstructible<_Dummy> >
  151. _LIBCPP_INLINE_VISIBILITY
  152. _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(__value_init_tag(), __value_init_tag()) {}
  153. template <bool _Dummy = true,
  154. class = _EnableIfDeleterDefaultConstructible<_Dummy> >
  155. _LIBCPP_INLINE_VISIBILITY
  156. explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p, __value_init_tag()) {}
  157. template <bool _Dummy = true,
  158. class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
  159. _LIBCPP_INLINE_VISIBILITY
  160. unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
  161. : __ptr_(__p, __d) {}
  162. template <bool _Dummy = true,
  163. class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
  164. _LIBCPP_INLINE_VISIBILITY
  165. unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
  166. : __ptr_(__p, _VSTD::move(__d)) {
  167. static_assert(!is_reference<deleter_type>::value,
  168. "rvalue deleter bound to reference");
  169. }
  170. template <bool _Dummy = true,
  171. class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
  172. _LIBCPP_INLINE_VISIBILITY
  173. unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
  174. _LIBCPP_INLINE_VISIBILITY
  175. unique_ptr(unique_ptr&& __u) _NOEXCEPT
  176. : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
  177. }
  178. template <class _Up, class _Ep,
  179. class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
  180. class = _EnableIfDeleterConvertible<_Ep>
  181. >
  182. _LIBCPP_INLINE_VISIBILITY
  183. unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
  184. : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
  185. #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
  186. template <class _Up>
  187. _LIBCPP_INLINE_VISIBILITY
  188. unique_ptr(auto_ptr<_Up>&& __p,
  189. typename enable_if<is_convertible<_Up*, _Tp*>::value &&
  190. is_same<_Dp, default_delete<_Tp> >::value,
  191. __nat>::type = __nat()) _NOEXCEPT
  192. : __ptr_(__p.release(), __value_init_tag()) {}
  193. #endif
  194. _LIBCPP_INLINE_VISIBILITY
  195. unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
  196. reset(__u.release());
  197. __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
  198. return *this;
  199. }
  200. template <class _Up, class _Ep,
  201. class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
  202. class = _EnableIfDeleterAssignable<_Ep>
  203. >
  204. _LIBCPP_INLINE_VISIBILITY
  205. unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
  206. reset(__u.release());
  207. __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
  208. return *this;
  209. }
  210. #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
  211. template <class _Up>
  212. _LIBCPP_INLINE_VISIBILITY
  213. typename enable_if<is_convertible<_Up*, _Tp*>::value &&
  214. is_same<_Dp, default_delete<_Tp> >::value,
  215. unique_ptr&>::type
  216. operator=(auto_ptr<_Up> __p) {
  217. reset(__p.release());
  218. return *this;
  219. }
  220. #endif
  221. #ifdef _LIBCPP_CXX03_LANG
  222. unique_ptr(unique_ptr const&) = delete;
  223. unique_ptr& operator=(unique_ptr const&) = delete;
  224. #endif
  225. _LIBCPP_INLINE_VISIBILITY
  226. ~unique_ptr() { reset(); }
  227. _LIBCPP_INLINE_VISIBILITY
  228. unique_ptr& operator=(nullptr_t) _NOEXCEPT {
  229. reset();
  230. return *this;
  231. }
  232. _LIBCPP_INLINE_VISIBILITY
  233. typename add_lvalue_reference<_Tp>::type
  234. operator*() const {
  235. return *__ptr_.first();
  236. }
  237. _LIBCPP_INLINE_VISIBILITY
  238. pointer operator->() const _NOEXCEPT {
  239. return __ptr_.first();
  240. }
  241. _LIBCPP_INLINE_VISIBILITY
  242. pointer get() const _NOEXCEPT {
  243. return __ptr_.first();
  244. }
  245. _LIBCPP_INLINE_VISIBILITY
  246. deleter_type& get_deleter() _NOEXCEPT {
  247. return __ptr_.second();
  248. }
  249. _LIBCPP_INLINE_VISIBILITY
  250. const deleter_type& get_deleter() const _NOEXCEPT {
  251. return __ptr_.second();
  252. }
  253. _LIBCPP_INLINE_VISIBILITY
  254. explicit operator bool() const _NOEXCEPT {
  255. return __ptr_.first() != nullptr;
  256. }
  257. _LIBCPP_INLINE_VISIBILITY
  258. pointer release() _NOEXCEPT {
  259. pointer __t = __ptr_.first();
  260. __ptr_.first() = pointer();
  261. return __t;
  262. }
  263. _LIBCPP_INLINE_VISIBILITY
  264. void reset(pointer __p = pointer()) _NOEXCEPT {
  265. pointer __tmp = __ptr_.first();
  266. __ptr_.first() = __p;
  267. if (__tmp)
  268. __ptr_.second()(__tmp);
  269. }
  270. _LIBCPP_INLINE_VISIBILITY
  271. void swap(unique_ptr& __u) _NOEXCEPT {
  272. __ptr_.swap(__u.__ptr_);
  273. }
  274. };
  275. template <class _Tp, class _Dp>
  276. class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
  277. public:
  278. typedef _Tp element_type;
  279. typedef _Dp deleter_type;
  280. typedef typename __pointer<_Tp, deleter_type>::type pointer;
  281. private:
  282. __compressed_pair<pointer, deleter_type> __ptr_;
  283. template <class _From>
  284. struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
  285. template <class _FromElem>
  286. struct _CheckArrayPointerConversion<_FromElem*>
  287. : integral_constant<bool,
  288. is_same<_FromElem*, pointer>::value ||
  289. (is_same<pointer, element_type*>::value &&
  290. is_convertible<_FromElem(*)[], element_type(*)[]>::value)
  291. >
  292. {};
  293. typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
  294. template <bool _Dummy>
  295. using _LValRefType _LIBCPP_NODEBUG =
  296. typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
  297. template <bool _Dummy>
  298. using _GoodRValRefType _LIBCPP_NODEBUG =
  299. typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
  300. template <bool _Dummy>
  301. using _BadRValRefType _LIBCPP_NODEBUG =
  302. typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
  303. template <bool _Dummy, class _Deleter = typename __dependent_type<
  304. __identity<deleter_type>, _Dummy>::type>
  305. using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG =
  306. typename enable_if<is_default_constructible<_Deleter>::value &&
  307. !is_pointer<_Deleter>::value>::type;
  308. template <class _ArgType>
  309. using _EnableIfDeleterConstructible _LIBCPP_NODEBUG =
  310. typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
  311. template <class _Pp>
  312. using _EnableIfPointerConvertible _LIBCPP_NODEBUG = typename enable_if<
  313. _CheckArrayPointerConversion<_Pp>::value
  314. >::type;
  315. template <class _UPtr, class _Up,
  316. class _ElemT = typename _UPtr::element_type>
  317. using _EnableIfMoveConvertible _LIBCPP_NODEBUG = typename enable_if<
  318. is_array<_Up>::value &&
  319. is_same<pointer, element_type*>::value &&
  320. is_same<typename _UPtr::pointer, _ElemT*>::value &&
  321. is_convertible<_ElemT(*)[], element_type(*)[]>::value
  322. >::type;
  323. template <class _UDel>
  324. using _EnableIfDeleterConvertible _LIBCPP_NODEBUG = typename enable_if<
  325. (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
  326. (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
  327. >::type;
  328. template <class _UDel>
  329. using _EnableIfDeleterAssignable _LIBCPP_NODEBUG = typename enable_if<
  330. is_assignable<_Dp&, _UDel&&>::value
  331. >::type;
  332. public:
  333. template <bool _Dummy = true,
  334. class = _EnableIfDeleterDefaultConstructible<_Dummy> >
  335. _LIBCPP_INLINE_VISIBILITY
  336. _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(__value_init_tag(), __value_init_tag()) {}
  337. template <bool _Dummy = true,
  338. class = _EnableIfDeleterDefaultConstructible<_Dummy> >
  339. _LIBCPP_INLINE_VISIBILITY
  340. _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(__value_init_tag(), __value_init_tag()) {}
  341. template <class _Pp, bool _Dummy = true,
  342. class = _EnableIfDeleterDefaultConstructible<_Dummy>,
  343. class = _EnableIfPointerConvertible<_Pp> >
  344. _LIBCPP_INLINE_VISIBILITY
  345. explicit unique_ptr(_Pp __p) _NOEXCEPT
  346. : __ptr_(__p, __value_init_tag()) {}
  347. template <class _Pp, bool _Dummy = true,
  348. class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
  349. class = _EnableIfPointerConvertible<_Pp> >
  350. _LIBCPP_INLINE_VISIBILITY
  351. unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
  352. : __ptr_(__p, __d) {}
  353. template <bool _Dummy = true,
  354. class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
  355. _LIBCPP_INLINE_VISIBILITY
  356. unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
  357. : __ptr_(nullptr, __d) {}
  358. template <class _Pp, bool _Dummy = true,
  359. class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
  360. class = _EnableIfPointerConvertible<_Pp> >
  361. _LIBCPP_INLINE_VISIBILITY
  362. unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
  363. : __ptr_(__p, _VSTD::move(__d)) {
  364. static_assert(!is_reference<deleter_type>::value,
  365. "rvalue deleter bound to reference");
  366. }
  367. template <bool _Dummy = true,
  368. class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
  369. _LIBCPP_INLINE_VISIBILITY
  370. unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
  371. : __ptr_(nullptr, _VSTD::move(__d)) {
  372. static_assert(!is_reference<deleter_type>::value,
  373. "rvalue deleter bound to reference");
  374. }
  375. template <class _Pp, bool _Dummy = true,
  376. class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
  377. class = _EnableIfPointerConvertible<_Pp> >
  378. _LIBCPP_INLINE_VISIBILITY
  379. unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
  380. _LIBCPP_INLINE_VISIBILITY
  381. unique_ptr(unique_ptr&& __u) _NOEXCEPT
  382. : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
  383. }
  384. _LIBCPP_INLINE_VISIBILITY
  385. unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
  386. reset(__u.release());
  387. __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
  388. return *this;
  389. }
  390. template <class _Up, class _Ep,
  391. class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
  392. class = _EnableIfDeleterConvertible<_Ep>
  393. >
  394. _LIBCPP_INLINE_VISIBILITY
  395. unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
  396. : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
  397. }
  398. template <class _Up, class _Ep,
  399. class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
  400. class = _EnableIfDeleterAssignable<_Ep>
  401. >
  402. _LIBCPP_INLINE_VISIBILITY
  403. unique_ptr&
  404. operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
  405. reset(__u.release());
  406. __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
  407. return *this;
  408. }
  409. #ifdef _LIBCPP_CXX03_LANG
  410. unique_ptr(unique_ptr const&) = delete;
  411. unique_ptr& operator=(unique_ptr const&) = delete;
  412. #endif
  413. public:
  414. _LIBCPP_INLINE_VISIBILITY
  415. ~unique_ptr() { reset(); }
  416. _LIBCPP_INLINE_VISIBILITY
  417. unique_ptr& operator=(nullptr_t) _NOEXCEPT {
  418. reset();
  419. return *this;
  420. }
  421. _LIBCPP_INLINE_VISIBILITY
  422. typename add_lvalue_reference<_Tp>::type
  423. operator[](size_t __i) const {
  424. return __ptr_.first()[__i];
  425. }
  426. _LIBCPP_INLINE_VISIBILITY
  427. pointer get() const _NOEXCEPT {
  428. return __ptr_.first();
  429. }
  430. _LIBCPP_INLINE_VISIBILITY
  431. deleter_type& get_deleter() _NOEXCEPT {
  432. return __ptr_.second();
  433. }
  434. _LIBCPP_INLINE_VISIBILITY
  435. const deleter_type& get_deleter() const _NOEXCEPT {
  436. return __ptr_.second();
  437. }
  438. _LIBCPP_INLINE_VISIBILITY
  439. explicit operator bool() const _NOEXCEPT {
  440. return __ptr_.first() != nullptr;
  441. }
  442. _LIBCPP_INLINE_VISIBILITY
  443. pointer release() _NOEXCEPT {
  444. pointer __t = __ptr_.first();
  445. __ptr_.first() = pointer();
  446. return __t;
  447. }
  448. template <class _Pp>
  449. _LIBCPP_INLINE_VISIBILITY
  450. typename enable_if<
  451. _CheckArrayPointerConversion<_Pp>::value
  452. >::type
  453. reset(_Pp __p) _NOEXCEPT {
  454. pointer __tmp = __ptr_.first();
  455. __ptr_.first() = __p;
  456. if (__tmp)
  457. __ptr_.second()(__tmp);
  458. }
  459. _LIBCPP_INLINE_VISIBILITY
  460. void reset(nullptr_t = nullptr) _NOEXCEPT {
  461. pointer __tmp = __ptr_.first();
  462. __ptr_.first() = nullptr;
  463. if (__tmp)
  464. __ptr_.second()(__tmp);
  465. }
  466. _LIBCPP_INLINE_VISIBILITY
  467. void swap(unique_ptr& __u) _NOEXCEPT {
  468. __ptr_.swap(__u.__ptr_);
  469. }
  470. };
  471. template <class _Tp, class _Dp>
  472. inline _LIBCPP_INLINE_VISIBILITY
  473. typename enable_if<
  474. __is_swappable<_Dp>::value,
  475. void
  476. >::type
  477. swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
  478. template <class _T1, class _D1, class _T2, class _D2>
  479. inline _LIBCPP_INLINE_VISIBILITY
  480. bool
  481. operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
  482. template <class _T1, class _D1, class _T2, class _D2>
  483. inline _LIBCPP_INLINE_VISIBILITY
  484. bool
  485. operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
  486. template <class _T1, class _D1, class _T2, class _D2>
  487. inline _LIBCPP_INLINE_VISIBILITY
  488. bool
  489. operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
  490. {
  491. typedef typename unique_ptr<_T1, _D1>::pointer _P1;
  492. typedef typename unique_ptr<_T2, _D2>::pointer _P2;
  493. typedef typename common_type<_P1, _P2>::type _Vp;
  494. return less<_Vp>()(__x.get(), __y.get());
  495. }
  496. template <class _T1, class _D1, class _T2, class _D2>
  497. inline _LIBCPP_INLINE_VISIBILITY
  498. bool
  499. operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
  500. template <class _T1, class _D1, class _T2, class _D2>
  501. inline _LIBCPP_INLINE_VISIBILITY
  502. bool
  503. operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
  504. template <class _T1, class _D1, class _T2, class _D2>
  505. inline _LIBCPP_INLINE_VISIBILITY
  506. bool
  507. operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
  508. template <class _T1, class _D1>
  509. inline _LIBCPP_INLINE_VISIBILITY
  510. bool
  511. operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
  512. {
  513. return !__x;
  514. }
  515. template <class _T1, class _D1>
  516. inline _LIBCPP_INLINE_VISIBILITY
  517. bool
  518. operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
  519. {
  520. return !__x;
  521. }
  522. template <class _T1, class _D1>
  523. inline _LIBCPP_INLINE_VISIBILITY
  524. bool
  525. operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
  526. {
  527. return static_cast<bool>(__x);
  528. }
  529. template <class _T1, class _D1>
  530. inline _LIBCPP_INLINE_VISIBILITY
  531. bool
  532. operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
  533. {
  534. return static_cast<bool>(__x);
  535. }
  536. template <class _T1, class _D1>
  537. inline _LIBCPP_INLINE_VISIBILITY
  538. bool
  539. operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
  540. {
  541. typedef typename unique_ptr<_T1, _D1>::pointer _P1;
  542. return less<_P1>()(__x.get(), nullptr);
  543. }
  544. template <class _T1, class _D1>
  545. inline _LIBCPP_INLINE_VISIBILITY
  546. bool
  547. operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
  548. {
  549. typedef typename unique_ptr<_T1, _D1>::pointer _P1;
  550. return less<_P1>()(nullptr, __x.get());
  551. }
  552. template <class _T1, class _D1>
  553. inline _LIBCPP_INLINE_VISIBILITY
  554. bool
  555. operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
  556. {
  557. return nullptr < __x;
  558. }
  559. template <class _T1, class _D1>
  560. inline _LIBCPP_INLINE_VISIBILITY
  561. bool
  562. operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
  563. {
  564. return __x < nullptr;
  565. }
  566. template <class _T1, class _D1>
  567. inline _LIBCPP_INLINE_VISIBILITY
  568. bool
  569. operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
  570. {
  571. return !(nullptr < __x);
  572. }
  573. template <class _T1, class _D1>
  574. inline _LIBCPP_INLINE_VISIBILITY
  575. bool
  576. operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
  577. {
  578. return !(__x < nullptr);
  579. }
  580. template <class _T1, class _D1>
  581. inline _LIBCPP_INLINE_VISIBILITY
  582. bool
  583. operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
  584. {
  585. return !(__x < nullptr);
  586. }
  587. template <class _T1, class _D1>
  588. inline _LIBCPP_INLINE_VISIBILITY
  589. bool
  590. operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
  591. {
  592. return !(nullptr < __x);
  593. }
  594. #if _LIBCPP_STD_VER > 11
  595. template<class _Tp>
  596. struct __unique_if
  597. {
  598. typedef unique_ptr<_Tp> __unique_single;
  599. };
  600. template<class _Tp>
  601. struct __unique_if<_Tp[]>
  602. {
  603. typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
  604. };
  605. template<class _Tp, size_t _Np>
  606. struct __unique_if<_Tp[_Np]>
  607. {
  608. typedef void __unique_array_known_bound;
  609. };
  610. template<class _Tp, class... _Args>
  611. inline _LIBCPP_INLINE_VISIBILITY
  612. typename __unique_if<_Tp>::__unique_single
  613. make_unique(_Args&&... __args)
  614. {
  615. return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
  616. }
  617. template<class _Tp>
  618. inline _LIBCPP_INLINE_VISIBILITY
  619. typename __unique_if<_Tp>::__unique_array_unknown_bound
  620. make_unique(size_t __n)
  621. {
  622. typedef typename remove_extent<_Tp>::type _Up;
  623. return unique_ptr<_Tp>(new _Up[__n]());
  624. }
  625. template<class _Tp, class... _Args>
  626. typename __unique_if<_Tp>::__unique_array_known_bound
  627. make_unique(_Args&&...) = delete;
  628. #endif // _LIBCPP_STD_VER > 11
  629. template <class _Tp> struct _LIBCPP_TEMPLATE_VIS hash;
  630. template <class _Tp, class _Dp>
  631. #ifdef _LIBCPP_CXX03_LANG
  632. struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
  633. #else
  634. struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
  635. unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
  636. #endif
  637. {
  638. #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
  639. _LIBCPP_DEPRECATED_IN_CXX17 typedef unique_ptr<_Tp, _Dp> argument_type;
  640. _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
  641. #endif
  642. _LIBCPP_INLINE_VISIBILITY
  643. size_t operator()(const unique_ptr<_Tp, _Dp>& __ptr) const
  644. {
  645. typedef typename unique_ptr<_Tp, _Dp>::pointer pointer;
  646. return hash<pointer>()(__ptr.get());
  647. }
  648. };
  649. _LIBCPP_END_NAMESPACE_STD
  650. #endif // _LIBCPP___MEMORY_UNIQUE_PTR_H