Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

1039 rindas
36KB

  1. // -*- C++ -*-
  2. //===-------------------------- memory ------------------------------------===//
  3. //
  4. // The LLVM Compiler Infrastructure
  5. //
  6. // This file is dual licensed under the MIT and the University of Illinois Open
  7. // Source Licenses. See LICENSE.TXT for details.
  8. //
  9. //===----------------------------------------------------------------------===//
  10. // STL common functionality
  11. //
  12. // Some aspects of STL are core language concepts that should be used from all C++ code, regardless
  13. // of whether exceptions are enabled in the component. Common library code that expects to be used
  14. // from exception-free components want these concepts, but including STL headers directly introduces
  15. // friction as it requires components not using STL to declare their STL version. Doing so creates
  16. // ambiguity around whether STL use is safe in a particular component and implicitly brings in
  17. // a long list of headers (including <new>) which can create further ambiguity around throwing new
  18. // support (some routines pulled in may expect it). Secondarily, pulling in these headers also has
  19. // the potential to create naming conflicts or other implied dependencies.
  20. //
  21. // To promote the use of these core language concepts outside of STL-based binaries, this file is
  22. // selectively pulling those concepts *directly* from corresponding STL headers. The corresponding
  23. // "std::" namespace STL functions and types should be preferred over these in code that is bound to
  24. // STL. The implementation and naming of all functions are taken directly from STL, instead using
  25. // "wistd" (Windows Implementation std) as the namespace.
  26. //
  27. // Routines in this namespace should always be considered a reflection of the *current* STL implementation
  28. // of those routines. Updates from STL should be taken, but no "bugs" should be fixed here.
  29. //
  30. // New, exception-based code should not use this namespace, but instead should prefer the std:: implementation.
  31. // Only code that is not exception-based and libraries that expect to be utilized across both exception
  32. // and non-exception based code should utilize this functionality.
  33. #ifndef _WISTD_MEMORY_H_
  34. #define _WISTD_MEMORY_H_
  35. // DO NOT add *any* additional includes to this file -- there should be no dependencies from its usage
  36. #include "wistd_type_traits.h"
  37. #if !defined(__WI_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  38. #pragma GCC system_header
  39. #endif
  40. /// @cond
  41. namespace wistd // ("Windows Implementation" std)
  42. {
  43. // allocator_traits
  44. template <class _Tp, class = void>
  45. struct __has_pointer_type : false_type {};
  46. template <class _Tp>
  47. struct __has_pointer_type<_Tp,
  48. typename __void_t<typename _Tp::pointer>::type> : true_type {};
  49. namespace __pointer_type_imp
  50. {
  51. template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
  52. struct __pointer_type
  53. {
  54. typedef typename _Dp::pointer type;
  55. };
  56. template <class _Tp, class _Dp>
  57. struct __pointer_type<_Tp, _Dp, false>
  58. {
  59. typedef _Tp* type;
  60. };
  61. } // __pointer_type_imp
  62. template <class _Tp, class _Dp>
  63. struct __pointer_type
  64. {
  65. typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
  66. };
  67. template <class _Tp, int _Idx,
  68. bool _CanBeEmptyBase =
  69. is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
  70. struct __compressed_pair_elem {
  71. typedef _Tp _ParamT;
  72. typedef _Tp& reference;
  73. typedef const _Tp& const_reference;
  74. #ifndef __WI_LIBCPP_CXX03_LANG
  75. __WI_LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() : __value_() {}
  76. template <class _Up, class = typename enable_if<
  77. !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
  78. >::type>
  79. __WI_LIBCPP_INLINE_VISIBILITY
  80. constexpr explicit
  81. __compressed_pair_elem(_Up&& __u)
  82. : __value_(wistd::forward<_Up>(__u))
  83. {
  84. }
  85. // NOTE: Since we have not added 'tuple' to 'wistd', the 'piecewise' constructor has been removed
  86. #else
  87. __WI_LIBCPP_INLINE_VISIBILITY __compressed_pair_elem() : __value_() {}
  88. __WI_LIBCPP_INLINE_VISIBILITY
  89. __compressed_pair_elem(_ParamT __p) : __value_(wistd::forward<_ParamT>(__p)) {}
  90. #endif
  91. __WI_LIBCPP_INLINE_VISIBILITY reference __get() WI_NOEXCEPT { return __value_; }
  92. __WI_LIBCPP_INLINE_VISIBILITY
  93. const_reference __get() const WI_NOEXCEPT { return __value_; }
  94. private:
  95. _Tp __value_;
  96. };
  97. template <class _Tp, int _Idx>
  98. struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
  99. typedef _Tp _ParamT;
  100. typedef _Tp& reference;
  101. typedef const _Tp& const_reference;
  102. typedef _Tp __value_type;
  103. #ifndef __WI_LIBCPP_CXX03_LANG
  104. __WI_LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() = default;
  105. template <class _Up, class = typename enable_if<
  106. !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
  107. >::type>
  108. __WI_LIBCPP_INLINE_VISIBILITY
  109. constexpr explicit
  110. __compressed_pair_elem(_Up&& __u)
  111. : __value_type(wistd::forward<_Up>(__u))
  112. {}
  113. // NOTE: Since we have not added 'tuple' to 'wistd', the 'piecewise' constructor has been removed
  114. #else
  115. __WI_LIBCPP_INLINE_VISIBILITY __compressed_pair_elem() : __value_type() {}
  116. __WI_LIBCPP_INLINE_VISIBILITY
  117. __compressed_pair_elem(_ParamT __p)
  118. : __value_type(wistd::forward<_ParamT>(__p)) {}
  119. #endif
  120. __WI_LIBCPP_INLINE_VISIBILITY reference __get() WI_NOEXCEPT { return *this; }
  121. __WI_LIBCPP_INLINE_VISIBILITY
  122. const_reference __get() const WI_NOEXCEPT { return *this; }
  123. };
  124. // Tag used to construct the second element of the compressed pair.
  125. struct __second_tag {};
  126. template <class _T1, class _T2>
  127. class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
  128. private __compressed_pair_elem<_T2, 1> {
  129. typedef __compressed_pair_elem<_T1, 0> _Base1;
  130. typedef __compressed_pair_elem<_T2, 1> _Base2;
  131. // NOTE: This static assert should never fire because __compressed_pair
  132. // is *almost never* used in a scenario where it's possible for T1 == T2.
  133. // (The exception is wistd::function where it is possible that the function
  134. // object and the allocator have the same type).
  135. static_assert((!is_same<_T1, _T2>::value),
  136. "__compressed_pair cannot be instantated when T1 and T2 are the same type; "
  137. "The current implementation is NOT ABI-compatible with the previous "
  138. "implementation for this configuration");
  139. public:
  140. #ifndef __WI_LIBCPP_CXX03_LANG
  141. template <bool _Dummy = true,
  142. class = typename enable_if<
  143. __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
  144. __dependent_type<is_default_constructible<_T2>, _Dummy>::value
  145. >::type
  146. >
  147. __WI_LIBCPP_INLINE_VISIBILITY
  148. constexpr __compressed_pair() {}
  149. template <class _Tp, typename enable_if<!is_same<typename decay<_Tp>::type,
  150. __compressed_pair>::value,
  151. bool>::type = true>
  152. __WI_LIBCPP_INLINE_VISIBILITY constexpr explicit
  153. __compressed_pair(_Tp&& __t)
  154. : _Base1(wistd::forward<_Tp>(__t)), _Base2() {}
  155. template <class _Tp>
  156. __WI_LIBCPP_INLINE_VISIBILITY constexpr
  157. __compressed_pair(__second_tag, _Tp&& __t)
  158. : _Base1(), _Base2(wistd::forward<_Tp>(__t)) {}
  159. template <class _U1, class _U2>
  160. __WI_LIBCPP_INLINE_VISIBILITY constexpr
  161. __compressed_pair(_U1&& __t1, _U2&& __t2)
  162. : _Base1(wistd::forward<_U1>(__t1)), _Base2(wistd::forward<_U2>(__t2)) {}
  163. // NOTE: Since we have not added 'tuple' to 'wistd', the 'piecewise' constructor has been removed
  164. #else
  165. __WI_LIBCPP_INLINE_VISIBILITY
  166. __compressed_pair() {}
  167. __WI_LIBCPP_INLINE_VISIBILITY explicit
  168. __compressed_pair(_T1 __t1) : _Base1(wistd::forward<_T1>(__t1)) {}
  169. __WI_LIBCPP_INLINE_VISIBILITY
  170. __compressed_pair(__second_tag, _T2 __t2)
  171. : _Base1(), _Base2(wistd::forward<_T2>(__t2)) {}
  172. __WI_LIBCPP_INLINE_VISIBILITY
  173. __compressed_pair(_T1 __t1, _T2 __t2)
  174. : _Base1(wistd::forward<_T1>(__t1)), _Base2(wistd::forward<_T2>(__t2)) {}
  175. #endif
  176. __WI_LIBCPP_INLINE_VISIBILITY
  177. typename _Base1::reference first() WI_NOEXCEPT {
  178. return static_cast<_Base1&>(*this).__get();
  179. }
  180. __WI_LIBCPP_INLINE_VISIBILITY
  181. typename _Base1::const_reference first() const WI_NOEXCEPT {
  182. return static_cast<_Base1 const&>(*this).__get();
  183. }
  184. __WI_LIBCPP_INLINE_VISIBILITY
  185. typename _Base2::reference second() WI_NOEXCEPT {
  186. return static_cast<_Base2&>(*this).__get();
  187. }
  188. __WI_LIBCPP_INLINE_VISIBILITY
  189. typename _Base2::const_reference second() const WI_NOEXCEPT {
  190. return static_cast<_Base2 const&>(*this).__get();
  191. }
  192. __WI_LIBCPP_INLINE_VISIBILITY
  193. void swap(__compressed_pair& __x)
  194. __WI_NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
  195. __is_nothrow_swappable<_T2>::value)
  196. {
  197. using wistd::swap_wil;
  198. swap_wil(first(), __x.first());
  199. swap_wil(second(), __x.second());
  200. }
  201. };
  202. // Provide both 'swap_wil' and 'swap' since we now have two ADL scenarios that we need to work
  203. template <class _T1, class _T2>
  204. inline __WI_LIBCPP_INLINE_VISIBILITY
  205. void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
  206. __WI_NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
  207. __is_nothrow_swappable<_T2>::value) {
  208. __x.swap(__y);
  209. }
  210. template <class _T1, class _T2>
  211. inline __WI_LIBCPP_INLINE_VISIBILITY
  212. void swap_wil(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
  213. __WI_NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
  214. __is_nothrow_swappable<_T2>::value) {
  215. __x.swap(__y);
  216. }
  217. // default_delete
  218. template <class _Tp>
  219. struct __WI_LIBCPP_TEMPLATE_VIS default_delete {
  220. static_assert(!is_function<_Tp>::value,
  221. "default_delete cannot be instantiated for function types");
  222. #ifndef __WI_LIBCPP_CXX03_LANG
  223. __WI_LIBCPP_INLINE_VISIBILITY constexpr default_delete() WI_NOEXCEPT = default;
  224. #else
  225. __WI_LIBCPP_INLINE_VISIBILITY default_delete() {}
  226. #endif
  227. template <class _Up>
  228. __WI_LIBCPP_INLINE_VISIBILITY
  229. default_delete(const default_delete<_Up>&,
  230. typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
  231. 0) WI_NOEXCEPT {}
  232. __WI_LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const WI_NOEXCEPT {
  233. static_assert(sizeof(_Tp) > 0,
  234. "default_delete can not delete incomplete type");
  235. static_assert(!is_void<_Tp>::value,
  236. "default_delete can not delete incomplete type");
  237. delete __ptr;
  238. }
  239. };
  240. template <class _Tp>
  241. struct __WI_LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
  242. private:
  243. template <class _Up>
  244. struct _EnableIfConvertible
  245. : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
  246. public:
  247. #ifndef __WI_LIBCPP_CXX03_LANG
  248. __WI_LIBCPP_INLINE_VISIBILITY constexpr default_delete() WI_NOEXCEPT = default;
  249. #else
  250. __WI_LIBCPP_INLINE_VISIBILITY default_delete() {}
  251. #endif
  252. template <class _Up>
  253. __WI_LIBCPP_INLINE_VISIBILITY
  254. default_delete(const default_delete<_Up[]>&,
  255. typename _EnableIfConvertible<_Up>::type* = 0) WI_NOEXCEPT {}
  256. template <class _Up>
  257. __WI_LIBCPP_INLINE_VISIBILITY
  258. typename _EnableIfConvertible<_Up>::type
  259. operator()(_Up* __ptr) const WI_NOEXCEPT {
  260. static_assert(sizeof(_Tp) > 0,
  261. "default_delete can not delete incomplete type");
  262. static_assert(!is_void<_Tp>::value,
  263. "default_delete can not delete void type");
  264. delete[] __ptr;
  265. }
  266. };
  267. #ifndef __WI_LIBCPP_CXX03_LANG
  268. template <class _Deleter>
  269. struct __unique_ptr_deleter_sfinae {
  270. static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
  271. typedef const _Deleter& __lval_ref_type;
  272. typedef _Deleter&& __good_rval_ref_type;
  273. typedef true_type __enable_rval_overload;
  274. };
  275. template <class _Deleter>
  276. struct __unique_ptr_deleter_sfinae<_Deleter const&> {
  277. typedef const _Deleter& __lval_ref_type;
  278. typedef const _Deleter&& __bad_rval_ref_type;
  279. typedef false_type __enable_rval_overload;
  280. };
  281. template <class _Deleter>
  282. struct __unique_ptr_deleter_sfinae<_Deleter&> {
  283. typedef _Deleter& __lval_ref_type;
  284. typedef _Deleter&& __bad_rval_ref_type;
  285. typedef false_type __enable_rval_overload;
  286. };
  287. #endif // !defined(__WI_LIBCPP_CXX03_LANG)
  288. template <class _Tp, class _Dp = default_delete<_Tp> >
  289. class __WI_LIBCPP_TEMPLATE_VIS unique_ptr {
  290. public:
  291. typedef _Tp element_type;
  292. typedef _Dp deleter_type;
  293. typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
  294. static_assert(!is_rvalue_reference<deleter_type>::value,
  295. "the specified deleter type cannot be an rvalue reference");
  296. private:
  297. __compressed_pair<pointer, deleter_type> __ptr_;
  298. struct __nat { int __for_bool_; };
  299. #ifndef __WI_LIBCPP_CXX03_LANG
  300. typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
  301. template <bool _Dummy>
  302. using _LValRefType =
  303. typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
  304. template <bool _Dummy>
  305. using _GoodRValRefType =
  306. typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
  307. template <bool _Dummy>
  308. using _BadRValRefType =
  309. typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
  310. template <bool _Dummy, class _Deleter = typename __dependent_type<
  311. __identity<deleter_type>, _Dummy>::type>
  312. using _EnableIfDeleterDefaultConstructible =
  313. typename enable_if<is_default_constructible<_Deleter>::value &&
  314. !is_pointer<_Deleter>::value>::type;
  315. template <class _ArgType>
  316. using _EnableIfDeleterConstructible =
  317. typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
  318. template <class _UPtr, class _Up>
  319. using _EnableIfMoveConvertible = typename enable_if<
  320. is_convertible<typename _UPtr::pointer, pointer>::value &&
  321. !is_array<_Up>::value
  322. >::type;
  323. template <class _UDel>
  324. using _EnableIfDeleterConvertible = 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 = typename enable_if<
  330. is_assignable<_Dp&, _UDel&&>::value
  331. >::type;
  332. public:
  333. template <bool _Dummy = true,
  334. class = _EnableIfDeleterDefaultConstructible<_Dummy>>
  335. __WI_LIBCPP_INLINE_VISIBILITY
  336. constexpr unique_ptr() WI_NOEXCEPT : __ptr_(pointer()) {}
  337. template <bool _Dummy = true,
  338. class = _EnableIfDeleterDefaultConstructible<_Dummy>>
  339. __WI_LIBCPP_INLINE_VISIBILITY
  340. constexpr unique_ptr(nullptr_t) WI_NOEXCEPT : __ptr_(pointer()) {}
  341. template <bool _Dummy = true,
  342. class = _EnableIfDeleterDefaultConstructible<_Dummy>>
  343. __WI_LIBCPP_INLINE_VISIBILITY
  344. explicit unique_ptr(pointer __p) WI_NOEXCEPT : __ptr_(__p) {}
  345. template <bool _Dummy = true,
  346. class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>>
  347. __WI_LIBCPP_INLINE_VISIBILITY
  348. unique_ptr(pointer __p, _LValRefType<_Dummy> __d) WI_NOEXCEPT
  349. : __ptr_(__p, __d) {}
  350. template <bool _Dummy = true,
  351. class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>>
  352. __WI_LIBCPP_INLINE_VISIBILITY
  353. unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) WI_NOEXCEPT
  354. : __ptr_(__p, wistd::move(__d)) {
  355. static_assert(!is_reference<deleter_type>::value,
  356. "rvalue deleter bound to reference");
  357. }
  358. template <bool _Dummy = true,
  359. class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy>>>
  360. __WI_LIBCPP_INLINE_VISIBILITY
  361. unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
  362. __WI_LIBCPP_INLINE_VISIBILITY
  363. unique_ptr(unique_ptr&& __u) WI_NOEXCEPT
  364. : __ptr_(__u.release(), wistd::forward<deleter_type>(__u.get_deleter())) {
  365. }
  366. template <class _Up, class _Ep,
  367. class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
  368. class = _EnableIfDeleterConvertible<_Ep>
  369. >
  370. __WI_LIBCPP_INLINE_VISIBILITY
  371. unique_ptr(unique_ptr<_Up, _Ep>&& __u) WI_NOEXCEPT
  372. : __ptr_(__u.release(), wistd::forward<_Ep>(__u.get_deleter())) {}
  373. __WI_LIBCPP_INLINE_VISIBILITY
  374. unique_ptr& operator=(unique_ptr&& __u) WI_NOEXCEPT {
  375. reset(__u.release());
  376. __ptr_.second() = wistd::forward<deleter_type>(__u.get_deleter());
  377. return *this;
  378. }
  379. template <class _Up, class _Ep,
  380. class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
  381. class = _EnableIfDeleterAssignable<_Ep>
  382. >
  383. __WI_LIBCPP_INLINE_VISIBILITY
  384. unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) WI_NOEXCEPT {
  385. reset(__u.release());
  386. __ptr_.second() = wistd::forward<_Ep>(__u.get_deleter());
  387. return *this;
  388. }
  389. #else // __WI_LIBCPP_CXX03_LANG
  390. private:
  391. unique_ptr(unique_ptr&);
  392. template <class _Up, class _Ep> unique_ptr(unique_ptr<_Up, _Ep>&);
  393. unique_ptr& operator=(unique_ptr&);
  394. template <class _Up, class _Ep> unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
  395. public:
  396. __WI_LIBCPP_INLINE_VISIBILITY
  397. unique_ptr() : __ptr_(pointer())
  398. {
  399. static_assert(!is_pointer<deleter_type>::value,
  400. "unique_ptr constructed with null function pointer deleter");
  401. static_assert(is_default_constructible<deleter_type>::value,
  402. "unique_ptr::deleter_type is not default constructible");
  403. }
  404. __WI_LIBCPP_INLINE_VISIBILITY
  405. unique_ptr(nullptr_t) : __ptr_(pointer())
  406. {
  407. static_assert(!is_pointer<deleter_type>::value,
  408. "unique_ptr constructed with null function pointer deleter");
  409. }
  410. __WI_LIBCPP_INLINE_VISIBILITY
  411. explicit unique_ptr(pointer __p)
  412. : __ptr_(wistd::move(__p)) {
  413. static_assert(!is_pointer<deleter_type>::value,
  414. "unique_ptr constructed with null function pointer deleter");
  415. }
  416. __WI_LIBCPP_INLINE_VISIBILITY
  417. operator __rv<unique_ptr>() {
  418. return __rv<unique_ptr>(*this);
  419. }
  420. __WI_LIBCPP_INLINE_VISIBILITY
  421. unique_ptr(__rv<unique_ptr> __u)
  422. : __ptr_(__u->release(),
  423. wistd::forward<deleter_type>(__u->get_deleter())) {}
  424. template <class _Up, class _Ep>
  425. __WI_LIBCPP_INLINE_VISIBILITY
  426. typename enable_if<
  427. !is_array<_Up>::value &&
  428. is_convertible<typename unique_ptr<_Up, _Ep>::pointer,
  429. pointer>::value &&
  430. is_assignable<deleter_type&, _Ep&>::value,
  431. unique_ptr&>::type
  432. operator=(unique_ptr<_Up, _Ep> __u) {
  433. reset(__u.release());
  434. __ptr_.second() = wistd::forward<_Ep>(__u.get_deleter());
  435. return *this;
  436. }
  437. __WI_LIBCPP_INLINE_VISIBILITY
  438. unique_ptr(pointer __p, deleter_type __d)
  439. : __ptr_(wistd::move(__p), wistd::move(__d)) {}
  440. #endif // __WI_LIBCPP_CXX03_LANG
  441. __WI_LIBCPP_INLINE_VISIBILITY
  442. ~unique_ptr() { reset(); }
  443. __WI_LIBCPP_INLINE_VISIBILITY
  444. unique_ptr& operator=(nullptr_t) WI_NOEXCEPT {
  445. reset();
  446. return *this;
  447. }
  448. __WI_LIBCPP_INLINE_VISIBILITY
  449. typename add_lvalue_reference<_Tp>::type
  450. operator*() const {
  451. return *__ptr_.first();
  452. }
  453. __WI_LIBCPP_INLINE_VISIBILITY
  454. pointer operator->() const WI_NOEXCEPT {
  455. return __ptr_.first();
  456. }
  457. __WI_LIBCPP_INLINE_VISIBILITY
  458. pointer get() const WI_NOEXCEPT {
  459. return __ptr_.first();
  460. }
  461. __WI_LIBCPP_INLINE_VISIBILITY
  462. deleter_type& get_deleter() WI_NOEXCEPT {
  463. return __ptr_.second();
  464. }
  465. __WI_LIBCPP_INLINE_VISIBILITY
  466. const deleter_type& get_deleter() const WI_NOEXCEPT {
  467. return __ptr_.second();
  468. }
  469. __WI_LIBCPP_INLINE_VISIBILITY
  470. __WI_LIBCPP_EXPLICIT operator bool() const WI_NOEXCEPT {
  471. return __ptr_.first() != nullptr;
  472. }
  473. __WI_LIBCPP_INLINE_VISIBILITY
  474. pointer release() WI_NOEXCEPT {
  475. pointer __t = __ptr_.first();
  476. __ptr_.first() = pointer();
  477. return __t;
  478. }
  479. __WI_LIBCPP_INLINE_VISIBILITY
  480. void reset(pointer __p = pointer()) WI_NOEXCEPT {
  481. pointer __tmp = __ptr_.first();
  482. __ptr_.first() = __p;
  483. if (__tmp)
  484. __ptr_.second()(__tmp);
  485. }
  486. __WI_LIBCPP_INLINE_VISIBILITY
  487. void swap(unique_ptr& __u) WI_NOEXCEPT {
  488. __ptr_.swap(__u.__ptr_);
  489. }
  490. };
  491. template <class _Tp, class _Dp>
  492. class __WI_LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
  493. public:
  494. typedef _Tp element_type;
  495. typedef _Dp deleter_type;
  496. typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
  497. private:
  498. __compressed_pair<pointer, deleter_type> __ptr_;
  499. template <class _From>
  500. struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
  501. template <class _FromElem>
  502. struct _CheckArrayPointerConversion<_FromElem*>
  503. : integral_constant<bool,
  504. is_same<_FromElem*, pointer>::value ||
  505. (is_same<pointer, element_type*>::value &&
  506. is_convertible<_FromElem(*)[], element_type(*)[]>::value)
  507. >
  508. {};
  509. #ifndef __WI_LIBCPP_CXX03_LANG
  510. typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
  511. template <bool _Dummy>
  512. using _LValRefType =
  513. typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
  514. template <bool _Dummy>
  515. using _GoodRValRefType =
  516. typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
  517. template <bool _Dummy>
  518. using _BadRValRefType =
  519. typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
  520. template <bool _Dummy, class _Deleter = typename __dependent_type<
  521. __identity<deleter_type>, _Dummy>::type>
  522. using _EnableIfDeleterDefaultConstructible =
  523. typename enable_if<is_default_constructible<_Deleter>::value &&
  524. !is_pointer<_Deleter>::value>::type;
  525. template <class _ArgType>
  526. using _EnableIfDeleterConstructible =
  527. typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
  528. template <class _Pp>
  529. using _EnableIfPointerConvertible = typename enable_if<
  530. _CheckArrayPointerConversion<_Pp>::value
  531. >::type;
  532. template <class _UPtr, class _Up,
  533. class _ElemT = typename _UPtr::element_type>
  534. using _EnableIfMoveConvertible = typename enable_if<
  535. is_array<_Up>::value &&
  536. is_same<pointer, element_type*>::value &&
  537. is_same<typename _UPtr::pointer, _ElemT*>::value &&
  538. is_convertible<_ElemT(*)[], element_type(*)[]>::value
  539. >::type;
  540. template <class _UDel>
  541. using _EnableIfDeleterConvertible = typename enable_if<
  542. (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
  543. (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
  544. >::type;
  545. template <class _UDel>
  546. using _EnableIfDeleterAssignable = typename enable_if<
  547. is_assignable<_Dp&, _UDel&&>::value
  548. >::type;
  549. public:
  550. template <bool _Dummy = true,
  551. class = _EnableIfDeleterDefaultConstructible<_Dummy>>
  552. __WI_LIBCPP_INLINE_VISIBILITY
  553. constexpr unique_ptr() WI_NOEXCEPT : __ptr_(pointer()) {}
  554. template <bool _Dummy = true,
  555. class = _EnableIfDeleterDefaultConstructible<_Dummy>>
  556. __WI_LIBCPP_INLINE_VISIBILITY
  557. constexpr unique_ptr(nullptr_t) WI_NOEXCEPT : __ptr_(pointer()) {}
  558. template <class _Pp, bool _Dummy = true,
  559. class = _EnableIfDeleterDefaultConstructible<_Dummy>,
  560. class = _EnableIfPointerConvertible<_Pp>>
  561. __WI_LIBCPP_INLINE_VISIBILITY
  562. explicit unique_ptr(_Pp __p) WI_NOEXCEPT
  563. : __ptr_(__p) {}
  564. template <class _Pp, bool _Dummy = true,
  565. class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>,
  566. class = _EnableIfPointerConvertible<_Pp>>
  567. __WI_LIBCPP_INLINE_VISIBILITY
  568. unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) WI_NOEXCEPT
  569. : __ptr_(__p, __d) {}
  570. template <bool _Dummy = true,
  571. class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>>
  572. __WI_LIBCPP_INLINE_VISIBILITY
  573. unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) WI_NOEXCEPT
  574. : __ptr_(nullptr, __d) {}
  575. template <class _Pp, bool _Dummy = true,
  576. class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>,
  577. class = _EnableIfPointerConvertible<_Pp>>
  578. __WI_LIBCPP_INLINE_VISIBILITY
  579. unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) WI_NOEXCEPT
  580. : __ptr_(__p, wistd::move(__d)) {
  581. static_assert(!is_reference<deleter_type>::value,
  582. "rvalue deleter bound to reference");
  583. }
  584. template <bool _Dummy = true,
  585. class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>>
  586. __WI_LIBCPP_INLINE_VISIBILITY
  587. unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) WI_NOEXCEPT
  588. : __ptr_(nullptr, wistd::move(__d)) {
  589. static_assert(!is_reference<deleter_type>::value,
  590. "rvalue deleter bound to reference");
  591. }
  592. template <class _Pp, bool _Dummy = true,
  593. class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy>>,
  594. class = _EnableIfPointerConvertible<_Pp>>
  595. __WI_LIBCPP_INLINE_VISIBILITY
  596. unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
  597. __WI_LIBCPP_INLINE_VISIBILITY
  598. unique_ptr(unique_ptr&& __u) WI_NOEXCEPT
  599. : __ptr_(__u.release(), wistd::forward<deleter_type>(__u.get_deleter())) {
  600. }
  601. __WI_LIBCPP_INLINE_VISIBILITY
  602. unique_ptr& operator=(unique_ptr&& __u) WI_NOEXCEPT {
  603. reset(__u.release());
  604. __ptr_.second() = wistd::forward<deleter_type>(__u.get_deleter());
  605. return *this;
  606. }
  607. template <class _Up, class _Ep,
  608. class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
  609. class = _EnableIfDeleterConvertible<_Ep>
  610. >
  611. __WI_LIBCPP_INLINE_VISIBILITY
  612. unique_ptr(unique_ptr<_Up, _Ep>&& __u) WI_NOEXCEPT
  613. : __ptr_(__u.release(), wistd::forward<_Ep>(__u.get_deleter())) {
  614. }
  615. template <class _Up, class _Ep,
  616. class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
  617. class = _EnableIfDeleterAssignable<_Ep>
  618. >
  619. __WI_LIBCPP_INLINE_VISIBILITY
  620. unique_ptr&
  621. operator=(unique_ptr<_Up, _Ep>&& __u) WI_NOEXCEPT {
  622. reset(__u.release());
  623. __ptr_.second() = wistd::forward<_Ep>(__u.get_deleter());
  624. return *this;
  625. }
  626. #else // __WI_LIBCPP_CXX03_LANG
  627. private:
  628. template <class _Up> explicit unique_ptr(_Up);
  629. unique_ptr(unique_ptr&);
  630. template <class _Up> unique_ptr(unique_ptr<_Up>&);
  631. unique_ptr& operator=(unique_ptr&);
  632. template <class _Up> unique_ptr& operator=(unique_ptr<_Up>&);
  633. template <class _Up>
  634. unique_ptr(_Up __u,
  635. typename conditional<
  636. is_reference<deleter_type>::value, deleter_type,
  637. typename add_lvalue_reference<const deleter_type>::type>::type,
  638. typename enable_if<is_convertible<_Up, pointer>::value,
  639. __nat>::type = __nat());
  640. public:
  641. __WI_LIBCPP_INLINE_VISIBILITY
  642. unique_ptr() : __ptr_(pointer()) {
  643. static_assert(!is_pointer<deleter_type>::value,
  644. "unique_ptr constructed with null function pointer deleter");
  645. }
  646. __WI_LIBCPP_INLINE_VISIBILITY
  647. unique_ptr(nullptr_t) : __ptr_(pointer()) {
  648. static_assert(!is_pointer<deleter_type>::value,
  649. "unique_ptr constructed with null function pointer deleter");
  650. }
  651. __WI_LIBCPP_INLINE_VISIBILITY
  652. explicit unique_ptr(pointer __p) : __ptr_(__p) {
  653. static_assert(!is_pointer<deleter_type>::value,
  654. "unique_ptr constructed with null function pointer deleter");
  655. }
  656. __WI_LIBCPP_INLINE_VISIBILITY
  657. unique_ptr(pointer __p, deleter_type __d)
  658. : __ptr_(__p, wistd::forward<deleter_type>(__d)) {}
  659. __WI_LIBCPP_INLINE_VISIBILITY
  660. unique_ptr(nullptr_t, deleter_type __d)
  661. : __ptr_(pointer(), wistd::forward<deleter_type>(__d)) {}
  662. __WI_LIBCPP_INLINE_VISIBILITY
  663. operator __rv<unique_ptr>() {
  664. return __rv<unique_ptr>(*this);
  665. }
  666. __WI_LIBCPP_INLINE_VISIBILITY
  667. unique_ptr(__rv<unique_ptr> __u)
  668. : __ptr_(__u->release(),
  669. wistd::forward<deleter_type>(__u->get_deleter())) {}
  670. __WI_LIBCPP_INLINE_VISIBILITY
  671. unique_ptr& operator=(__rv<unique_ptr> __u) {
  672. reset(__u->release());
  673. __ptr_.second() = wistd::forward<deleter_type>(__u->get_deleter());
  674. return *this;
  675. }
  676. #endif // __WI_LIBCPP_CXX03_LANG
  677. public:
  678. __WI_LIBCPP_INLINE_VISIBILITY
  679. ~unique_ptr() { reset(); }
  680. __WI_LIBCPP_INLINE_VISIBILITY
  681. unique_ptr& operator=(nullptr_t) WI_NOEXCEPT {
  682. reset();
  683. return *this;
  684. }
  685. __WI_LIBCPP_INLINE_VISIBILITY
  686. typename add_lvalue_reference<_Tp>::type
  687. operator[](size_t __i) const {
  688. return __ptr_.first()[__i];
  689. }
  690. __WI_LIBCPP_INLINE_VISIBILITY
  691. pointer get() const WI_NOEXCEPT {
  692. return __ptr_.first();
  693. }
  694. __WI_LIBCPP_INLINE_VISIBILITY
  695. deleter_type& get_deleter() WI_NOEXCEPT {
  696. return __ptr_.second();
  697. }
  698. __WI_LIBCPP_INLINE_VISIBILITY
  699. const deleter_type& get_deleter() const WI_NOEXCEPT {
  700. return __ptr_.second();
  701. }
  702. __WI_LIBCPP_INLINE_VISIBILITY
  703. __WI_LIBCPP_EXPLICIT operator bool() const WI_NOEXCEPT {
  704. return __ptr_.first() != nullptr;
  705. }
  706. __WI_LIBCPP_INLINE_VISIBILITY
  707. pointer release() WI_NOEXCEPT {
  708. pointer __t = __ptr_.first();
  709. __ptr_.first() = pointer();
  710. return __t;
  711. }
  712. template <class _Pp>
  713. __WI_LIBCPP_INLINE_VISIBILITY
  714. typename enable_if<
  715. _CheckArrayPointerConversion<_Pp>::value
  716. >::type
  717. reset(_Pp __p) WI_NOEXCEPT {
  718. pointer __tmp = __ptr_.first();
  719. __ptr_.first() = __p;
  720. if (__tmp)
  721. __ptr_.second()(__tmp);
  722. }
  723. __WI_LIBCPP_INLINE_VISIBILITY
  724. void reset(nullptr_t = nullptr) WI_NOEXCEPT {
  725. pointer __tmp = __ptr_.first();
  726. __ptr_.first() = nullptr;
  727. if (__tmp)
  728. __ptr_.second()(__tmp);
  729. }
  730. __WI_LIBCPP_INLINE_VISIBILITY
  731. void swap(unique_ptr& __u) WI_NOEXCEPT {
  732. __ptr_.swap(__u.__ptr_);
  733. }
  734. };
  735. // Provide both 'swap_wil' and 'swap' since we now have two ADL scenarios that we need to work
  736. template <class _Tp, class _Dp>
  737. inline __WI_LIBCPP_INLINE_VISIBILITY
  738. typename enable_if<
  739. __is_swappable<_Dp>::value,
  740. void
  741. >::type
  742. swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) WI_NOEXCEPT {__x.swap(__y);}
  743. template <class _Tp, class _Dp>
  744. inline __WI_LIBCPP_INLINE_VISIBILITY
  745. typename enable_if<
  746. __is_swappable<_Dp>::value,
  747. void
  748. >::type
  749. swap_wil(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) WI_NOEXCEPT {__x.swap(__y);}
  750. template <class _T1, class _D1, class _T2, class _D2>
  751. inline __WI_LIBCPP_INLINE_VISIBILITY
  752. bool
  753. operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
  754. template <class _T1, class _D1, class _T2, class _D2>
  755. inline __WI_LIBCPP_INLINE_VISIBILITY
  756. bool
  757. operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
  758. template <class _T1, class _D1, class _T2, class _D2>
  759. inline __WI_LIBCPP_INLINE_VISIBILITY
  760. bool
  761. operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
  762. {
  763. typedef typename unique_ptr<_T1, _D1>::pointer _P1;
  764. typedef typename unique_ptr<_T2, _D2>::pointer _P2;
  765. typedef typename common_type<_P1, _P2>::type _Vp;
  766. return less<_Vp>()(__x.get(), __y.get());
  767. }
  768. template <class _T1, class _D1, class _T2, class _D2>
  769. inline __WI_LIBCPP_INLINE_VISIBILITY
  770. bool
  771. operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
  772. template <class _T1, class _D1, class _T2, class _D2>
  773. inline __WI_LIBCPP_INLINE_VISIBILITY
  774. bool
  775. operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
  776. template <class _T1, class _D1, class _T2, class _D2>
  777. inline __WI_LIBCPP_INLINE_VISIBILITY
  778. bool
  779. operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
  780. template <class _T1, class _D1>
  781. inline __WI_LIBCPP_INLINE_VISIBILITY
  782. bool
  783. operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) WI_NOEXCEPT
  784. {
  785. return !__x;
  786. }
  787. template <class _T1, class _D1>
  788. inline __WI_LIBCPP_INLINE_VISIBILITY
  789. bool
  790. operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) WI_NOEXCEPT
  791. {
  792. return !__x;
  793. }
  794. template <class _T1, class _D1>
  795. inline __WI_LIBCPP_INLINE_VISIBILITY
  796. bool
  797. operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) WI_NOEXCEPT
  798. {
  799. return static_cast<bool>(__x);
  800. }
  801. template <class _T1, class _D1>
  802. inline __WI_LIBCPP_INLINE_VISIBILITY
  803. bool
  804. operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) WI_NOEXCEPT
  805. {
  806. return static_cast<bool>(__x);
  807. }
  808. template <class _T1, class _D1>
  809. inline __WI_LIBCPP_INLINE_VISIBILITY
  810. bool
  811. operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
  812. {
  813. typedef typename unique_ptr<_T1, _D1>::pointer _P1;
  814. return less<_P1>()(__x.get(), nullptr);
  815. }
  816. template <class _T1, class _D1>
  817. inline __WI_LIBCPP_INLINE_VISIBILITY
  818. bool
  819. operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
  820. {
  821. typedef typename unique_ptr<_T1, _D1>::pointer _P1;
  822. return less<_P1>()(nullptr, __x.get());
  823. }
  824. template <class _T1, class _D1>
  825. inline __WI_LIBCPP_INLINE_VISIBILITY
  826. bool
  827. operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
  828. {
  829. return nullptr < __x;
  830. }
  831. template <class _T1, class _D1>
  832. inline __WI_LIBCPP_INLINE_VISIBILITY
  833. bool
  834. operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
  835. {
  836. return __x < nullptr;
  837. }
  838. template <class _T1, class _D1>
  839. inline __WI_LIBCPP_INLINE_VISIBILITY
  840. bool
  841. operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
  842. {
  843. return !(nullptr < __x);
  844. }
  845. template <class _T1, class _D1>
  846. inline __WI_LIBCPP_INLINE_VISIBILITY
  847. bool
  848. operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
  849. {
  850. return !(__x < nullptr);
  851. }
  852. template <class _T1, class _D1>
  853. inline __WI_LIBCPP_INLINE_VISIBILITY
  854. bool
  855. operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
  856. {
  857. return !(__x < nullptr);
  858. }
  859. template <class _T1, class _D1>
  860. inline __WI_LIBCPP_INLINE_VISIBILITY
  861. bool
  862. operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
  863. {
  864. return !(nullptr < __x);
  865. }
  866. #ifdef __WI_LIBCPP_HAS_NO_RVALUE_REFERENCES
  867. template <class _Tp, class _Dp>
  868. inline __WI_LIBCPP_INLINE_VISIBILITY
  869. unique_ptr<_Tp, _Dp>
  870. move(unique_ptr<_Tp, _Dp>& __t)
  871. {
  872. return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
  873. }
  874. #endif
  875. }
  876. /// @endcond
  877. #endif // _WISTD_MEMORY_H_