Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

3 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. // unique_ptr implementation -*- C++ -*-
  2. // Copyright (C) 2008-2020 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file bits/unique_ptr.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{memory}
  23. */
  24. #ifndef _UNIQUE_PTR_H
  25. #define _UNIQUE_PTR_H 1
  26. #include <bits/c++config.h>
  27. #include <debug/assertions.h>
  28. #include <type_traits>
  29. #include <utility>
  30. #include <tuple>
  31. #include <bits/stl_function.h>
  32. #include <bits/functional_hash.h>
  33. #if __cplusplus > 201703L
  34. # include <compare>
  35. # include <ostream>
  36. #endif
  37. namespace std _GLIBCXX_VISIBILITY(default)
  38. {
  39. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  40. /**
  41. * @addtogroup pointer_abstractions
  42. * @{
  43. */
  44. #if _GLIBCXX_USE_DEPRECATED
  45. #pragma GCC diagnostic push
  46. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  47. template<typename> class auto_ptr;
  48. #pragma GCC diagnostic pop
  49. #endif
  50. /// Primary template of default_delete, used by unique_ptr for single objects
  51. template<typename _Tp>
  52. struct default_delete
  53. {
  54. /// Default constructor
  55. constexpr default_delete() noexcept = default;
  56. /** @brief Converting constructor.
  57. *
  58. * Allows conversion from a deleter for objects of another type, `_Up`,
  59. * only if `_Up*` is convertible to `_Tp*`.
  60. */
  61. template<typename _Up,
  62. typename = _Require<is_convertible<_Up*, _Tp*>>>
  63. default_delete(const default_delete<_Up>&) noexcept { }
  64. /// Calls `delete __ptr`
  65. void
  66. operator()(_Tp* __ptr) const
  67. {
  68. static_assert(!is_void<_Tp>::value,
  69. "can't delete pointer to incomplete type");
  70. static_assert(sizeof(_Tp)>0,
  71. "can't delete pointer to incomplete type");
  72. delete __ptr;
  73. }
  74. };
  75. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  76. // DR 740 - omit specialization for array objects with a compile time length
  77. /// Specialization of default_delete for arrays, used by `unique_ptr<T[]>`
  78. template<typename _Tp>
  79. struct default_delete<_Tp[]>
  80. {
  81. public:
  82. /// Default constructor
  83. constexpr default_delete() noexcept = default;
  84. /** @brief Converting constructor.
  85. *
  86. * Allows conversion from a deleter for arrays of another type, such as
  87. * a const-qualified version of `_Tp`.
  88. *
  89. * Conversions from types derived from `_Tp` are not allowed because
  90. * it is undefined to `delete[]` an array of derived types through a
  91. * pointer to the base type.
  92. */
  93. template<typename _Up,
  94. typename = _Require<is_convertible<_Up(*)[], _Tp(*)[]>>>
  95. default_delete(const default_delete<_Up[]>&) noexcept { }
  96. /// Calls `delete[] __ptr`
  97. template<typename _Up>
  98. typename enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value>::type
  99. operator()(_Up* __ptr) const
  100. {
  101. static_assert(sizeof(_Tp)>0,
  102. "can't delete pointer to incomplete type");
  103. delete [] __ptr;
  104. }
  105. };
  106. /// @cond undocumented
  107. // Manages the pointer and deleter of a unique_ptr
  108. template <typename _Tp, typename _Dp>
  109. class __uniq_ptr_impl
  110. {
  111. template <typename _Up, typename _Ep, typename = void>
  112. struct _Ptr
  113. {
  114. using type = _Up*;
  115. };
  116. template <typename _Up, typename _Ep>
  117. struct
  118. _Ptr<_Up, _Ep, __void_t<typename remove_reference<_Ep>::type::pointer>>
  119. {
  120. using type = typename remove_reference<_Ep>::type::pointer;
  121. };
  122. public:
  123. using _DeleterConstraint = enable_if<
  124. __and_<__not_<is_pointer<_Dp>>,
  125. is_default_constructible<_Dp>>::value>;
  126. using pointer = typename _Ptr<_Tp, _Dp>::type;
  127. static_assert( !is_rvalue_reference<_Dp>::value,
  128. "unique_ptr's deleter type must be a function object type"
  129. " or an lvalue reference type" );
  130. __uniq_ptr_impl() = default;
  131. __uniq_ptr_impl(pointer __p) : _M_t() { _M_ptr() = __p; }
  132. template<typename _Del>
  133. __uniq_ptr_impl(pointer __p, _Del&& __d)
  134. : _M_t(__p, std::forward<_Del>(__d)) { }
  135. __uniq_ptr_impl(__uniq_ptr_impl&& __u) noexcept
  136. : _M_t(std::move(__u._M_t))
  137. { __u._M_ptr() = nullptr; }
  138. __uniq_ptr_impl& operator=(__uniq_ptr_impl&& __u) noexcept
  139. {
  140. reset(__u.release());
  141. _M_deleter() = std::forward<_Dp>(__u._M_deleter());
  142. return *this;
  143. }
  144. pointer& _M_ptr() { return std::get<0>(_M_t); }
  145. pointer _M_ptr() const { return std::get<0>(_M_t); }
  146. _Dp& _M_deleter() { return std::get<1>(_M_t); }
  147. const _Dp& _M_deleter() const { return std::get<1>(_M_t); }
  148. void reset(pointer __p) noexcept
  149. {
  150. const pointer __old_p = _M_ptr();
  151. _M_ptr() = __p;
  152. if (__old_p)
  153. _M_deleter()(__old_p);
  154. }
  155. pointer release() noexcept
  156. {
  157. pointer __p = _M_ptr();
  158. _M_ptr() = nullptr;
  159. return __p;
  160. }
  161. void
  162. swap(__uniq_ptr_impl& __rhs) noexcept
  163. {
  164. using std::swap;
  165. swap(this->_M_ptr(), __rhs._M_ptr());
  166. swap(this->_M_deleter(), __rhs._M_deleter());
  167. }
  168. private:
  169. tuple<pointer, _Dp> _M_t;
  170. };
  171. // Defines move construction + assignment as either defaulted or deleted.
  172. template <typename _Tp, typename _Dp,
  173. bool = is_move_constructible<_Dp>::value,
  174. bool = is_move_assignable<_Dp>::value>
  175. struct __uniq_ptr_data : __uniq_ptr_impl<_Tp, _Dp>
  176. {
  177. using __uniq_ptr_impl<_Tp, _Dp>::__uniq_ptr_impl;
  178. __uniq_ptr_data(__uniq_ptr_data&&) = default;
  179. __uniq_ptr_data& operator=(__uniq_ptr_data&&) = default;
  180. };
  181. template <typename _Tp, typename _Dp>
  182. struct __uniq_ptr_data<_Tp, _Dp, true, false> : __uniq_ptr_impl<_Tp, _Dp>
  183. {
  184. using __uniq_ptr_impl<_Tp, _Dp>::__uniq_ptr_impl;
  185. __uniq_ptr_data(__uniq_ptr_data&&) = default;
  186. __uniq_ptr_data& operator=(__uniq_ptr_data&&) = delete;
  187. };
  188. template <typename _Tp, typename _Dp>
  189. struct __uniq_ptr_data<_Tp, _Dp, false, true> : __uniq_ptr_impl<_Tp, _Dp>
  190. {
  191. using __uniq_ptr_impl<_Tp, _Dp>::__uniq_ptr_impl;
  192. __uniq_ptr_data(__uniq_ptr_data&&) = delete;
  193. __uniq_ptr_data& operator=(__uniq_ptr_data&&) = default;
  194. };
  195. template <typename _Tp, typename _Dp>
  196. struct __uniq_ptr_data<_Tp, _Dp, false, false> : __uniq_ptr_impl<_Tp, _Dp>
  197. {
  198. using __uniq_ptr_impl<_Tp, _Dp>::__uniq_ptr_impl;
  199. __uniq_ptr_data(__uniq_ptr_data&&) = delete;
  200. __uniq_ptr_data& operator=(__uniq_ptr_data&&) = delete;
  201. };
  202. /// @endcond
  203. /// 20.7.1.2 unique_ptr for single objects.
  204. template <typename _Tp, typename _Dp = default_delete<_Tp>>
  205. class unique_ptr
  206. {
  207. template <typename _Up>
  208. using _DeleterConstraint =
  209. typename __uniq_ptr_impl<_Tp, _Up>::_DeleterConstraint::type;
  210. __uniq_ptr_data<_Tp, _Dp> _M_t;
  211. public:
  212. using pointer = typename __uniq_ptr_impl<_Tp, _Dp>::pointer;
  213. using element_type = _Tp;
  214. using deleter_type = _Dp;
  215. private:
  216. // helper template for detecting a safe conversion from another
  217. // unique_ptr
  218. template<typename _Up, typename _Ep>
  219. using __safe_conversion_up = __and_<
  220. is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>,
  221. __not_<is_array<_Up>>
  222. >;
  223. public:
  224. // Constructors.
  225. /// Default constructor, creates a unique_ptr that owns nothing.
  226. template<typename _Del = _Dp, typename = _DeleterConstraint<_Del>>
  227. constexpr unique_ptr() noexcept
  228. : _M_t()
  229. { }
  230. /** Takes ownership of a pointer.
  231. *
  232. * @param __p A pointer to an object of @c element_type
  233. *
  234. * The deleter will be value-initialized.
  235. */
  236. template<typename _Del = _Dp, typename = _DeleterConstraint<_Del>>
  237. explicit
  238. unique_ptr(pointer __p) noexcept
  239. : _M_t(__p)
  240. { }
  241. /** Takes ownership of a pointer.
  242. *
  243. * @param __p A pointer to an object of @c element_type
  244. * @param __d A reference to a deleter.
  245. *
  246. * The deleter will be initialized with @p __d
  247. */
  248. template<typename _Del = deleter_type,
  249. typename = _Require<is_copy_constructible<_Del>>>
  250. unique_ptr(pointer __p, const deleter_type& __d) noexcept
  251. : _M_t(__p, __d) { }
  252. /** Takes ownership of a pointer.
  253. *
  254. * @param __p A pointer to an object of @c element_type
  255. * @param __d An rvalue reference to a (non-reference) deleter.
  256. *
  257. * The deleter will be initialized with @p std::move(__d)
  258. */
  259. template<typename _Del = deleter_type,
  260. typename = _Require<is_move_constructible<_Del>>>
  261. unique_ptr(pointer __p,
  262. __enable_if_t<!is_lvalue_reference<_Del>::value,
  263. _Del&&> __d) noexcept
  264. : _M_t(__p, std::move(__d))
  265. { }
  266. template<typename _Del = deleter_type,
  267. typename _DelUnref = typename remove_reference<_Del>::type>
  268. unique_ptr(pointer,
  269. __enable_if_t<is_lvalue_reference<_Del>::value,
  270. _DelUnref&&>) = delete;
  271. /// Creates a unique_ptr that owns nothing.
  272. template<typename _Del = _Dp, typename = _DeleterConstraint<_Del>>
  273. constexpr unique_ptr(nullptr_t) noexcept
  274. : _M_t()
  275. { }
  276. // Move constructors.
  277. /// Move constructor.
  278. unique_ptr(unique_ptr&&) = default;
  279. /** @brief Converting constructor from another type
  280. *
  281. * Requires that the pointer owned by @p __u is convertible to the
  282. * type of pointer owned by this object, @p __u does not own an array,
  283. * and @p __u has a compatible deleter type.
  284. */
  285. template<typename _Up, typename _Ep, typename = _Require<
  286. __safe_conversion_up<_Up, _Ep>,
  287. typename conditional<is_reference<_Dp>::value,
  288. is_same<_Ep, _Dp>,
  289. is_convertible<_Ep, _Dp>>::type>>
  290. unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
  291. : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter()))
  292. { }
  293. #if _GLIBCXX_USE_DEPRECATED
  294. #pragma GCC diagnostic push
  295. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  296. /// Converting constructor from @c auto_ptr
  297. template<typename _Up, typename = _Require<
  298. is_convertible<_Up*, _Tp*>, is_same<_Dp, default_delete<_Tp>>>>
  299. unique_ptr(auto_ptr<_Up>&& __u) noexcept;
  300. #pragma GCC diagnostic pop
  301. #endif
  302. /// Destructor, invokes the deleter if the stored pointer is not null.
  303. ~unique_ptr() noexcept
  304. {
  305. static_assert(__is_invocable<deleter_type&, pointer>::value,
  306. "unique_ptr's deleter must be invocable with a pointer");
  307. auto& __ptr = _M_t._M_ptr();
  308. if (__ptr != nullptr)
  309. get_deleter()(std::move(__ptr));
  310. __ptr = pointer();
  311. }
  312. // Assignment.
  313. /** @brief Move assignment operator.
  314. *
  315. * Invokes the deleter if this object owns a pointer.
  316. */
  317. unique_ptr& operator=(unique_ptr&&) = default;
  318. /** @brief Assignment from another type.
  319. *
  320. * @param __u The object to transfer ownership from, which owns a
  321. * convertible pointer to a non-array object.
  322. *
  323. * Invokes the deleter if this object owns a pointer.
  324. */
  325. template<typename _Up, typename _Ep>
  326. typename enable_if< __and_<
  327. __safe_conversion_up<_Up, _Ep>,
  328. is_assignable<deleter_type&, _Ep&&>
  329. >::value,
  330. unique_ptr&>::type
  331. operator=(unique_ptr<_Up, _Ep>&& __u) noexcept
  332. {
  333. reset(__u.release());
  334. get_deleter() = std::forward<_Ep>(__u.get_deleter());
  335. return *this;
  336. }
  337. /// Reset the %unique_ptr to empty, invoking the deleter if necessary.
  338. unique_ptr&
  339. operator=(nullptr_t) noexcept
  340. {
  341. reset();
  342. return *this;
  343. }
  344. // Observers.
  345. /// Dereference the stored pointer.
  346. typename add_lvalue_reference<element_type>::type
  347. operator*() const
  348. {
  349. __glibcxx_assert(get() != pointer());
  350. return *get();
  351. }
  352. /// Return the stored pointer.
  353. pointer
  354. operator->() const noexcept
  355. {
  356. _GLIBCXX_DEBUG_PEDASSERT(get() != pointer());
  357. return get();
  358. }
  359. /// Return the stored pointer.
  360. pointer
  361. get() const noexcept
  362. { return _M_t._M_ptr(); }
  363. /// Return a reference to the stored deleter.
  364. deleter_type&
  365. get_deleter() noexcept
  366. { return _M_t._M_deleter(); }
  367. /// Return a reference to the stored deleter.
  368. const deleter_type&
  369. get_deleter() const noexcept
  370. { return _M_t._M_deleter(); }
  371. /// Return @c true if the stored pointer is not null.
  372. explicit operator bool() const noexcept
  373. { return get() == pointer() ? false : true; }
  374. // Modifiers.
  375. /// Release ownership of any stored pointer.
  376. pointer
  377. release() noexcept
  378. { return _M_t.release(); }
  379. /** @brief Replace the stored pointer.
  380. *
  381. * @param __p The new pointer to store.
  382. *
  383. * The deleter will be invoked if a pointer is already owned.
  384. */
  385. void
  386. reset(pointer __p = pointer()) noexcept
  387. {
  388. static_assert(__is_invocable<deleter_type&, pointer>::value,
  389. "unique_ptr's deleter must be invocable with a pointer");
  390. _M_t.reset(std::move(__p));
  391. }
  392. /// Exchange the pointer and deleter with another object.
  393. void
  394. swap(unique_ptr& __u) noexcept
  395. {
  396. static_assert(__is_swappable<_Dp>::value, "deleter must be swappable");
  397. _M_t.swap(__u._M_t);
  398. }
  399. // Disable copy from lvalue.
  400. unique_ptr(const unique_ptr&) = delete;
  401. unique_ptr& operator=(const unique_ptr&) = delete;
  402. };
  403. /// 20.7.1.3 unique_ptr for array objects with a runtime length
  404. // [unique.ptr.runtime]
  405. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  406. // DR 740 - omit specialization for array objects with a compile time length
  407. template<typename _Tp, typename _Dp>
  408. class unique_ptr<_Tp[], _Dp>
  409. {
  410. template <typename _Up>
  411. using _DeleterConstraint =
  412. typename __uniq_ptr_impl<_Tp, _Up>::_DeleterConstraint::type;
  413. __uniq_ptr_data<_Tp, _Dp> _M_t;
  414. template<typename _Up>
  415. using __remove_cv = typename remove_cv<_Up>::type;
  416. // like is_base_of<_Tp, _Up> but false if unqualified types are the same
  417. template<typename _Up>
  418. using __is_derived_Tp
  419. = __and_< is_base_of<_Tp, _Up>,
  420. __not_<is_same<__remove_cv<_Tp>, __remove_cv<_Up>>> >;
  421. public:
  422. using pointer = typename __uniq_ptr_impl<_Tp, _Dp>::pointer;
  423. using element_type = _Tp;
  424. using deleter_type = _Dp;
  425. // helper template for detecting a safe conversion from another
  426. // unique_ptr
  427. template<typename _Up, typename _Ep,
  428. typename _UPtr = unique_ptr<_Up, _Ep>,
  429. typename _UP_pointer = typename _UPtr::pointer,
  430. typename _UP_element_type = typename _UPtr::element_type>
  431. using __safe_conversion_up = __and_<
  432. is_array<_Up>,
  433. is_same<pointer, element_type*>,
  434. is_same<_UP_pointer, _UP_element_type*>,
  435. is_convertible<_UP_element_type(*)[], element_type(*)[]>
  436. >;
  437. // helper template for detecting a safe conversion from a raw pointer
  438. template<typename _Up>
  439. using __safe_conversion_raw = __and_<
  440. __or_<__or_<is_same<_Up, pointer>,
  441. is_same<_Up, nullptr_t>>,
  442. __and_<is_pointer<_Up>,
  443. is_same<pointer, element_type*>,
  444. is_convertible<
  445. typename remove_pointer<_Up>::type(*)[],
  446. element_type(*)[]>
  447. >
  448. >
  449. >;
  450. // Constructors.
  451. /// Default constructor, creates a unique_ptr that owns nothing.
  452. template<typename _Del = _Dp, typename = _DeleterConstraint<_Del>>
  453. constexpr unique_ptr() noexcept
  454. : _M_t()
  455. { }
  456. /** Takes ownership of a pointer.
  457. *
  458. * @param __p A pointer to an array of a type safely convertible
  459. * to an array of @c element_type
  460. *
  461. * The deleter will be value-initialized.
  462. */
  463. template<typename _Up,
  464. typename _Vp = _Dp,
  465. typename = _DeleterConstraint<_Vp>,
  466. typename = typename enable_if<
  467. __safe_conversion_raw<_Up>::value, bool>::type>
  468. explicit
  469. unique_ptr(_Up __p) noexcept
  470. : _M_t(__p)
  471. { }
  472. /** Takes ownership of a pointer.
  473. *
  474. * @param __p A pointer to an array of a type safely convertible
  475. * to an array of @c element_type
  476. * @param __d A reference to a deleter.
  477. *
  478. * The deleter will be initialized with @p __d
  479. */
  480. template<typename _Up, typename _Del = deleter_type,
  481. typename = _Require<__safe_conversion_raw<_Up>,
  482. is_copy_constructible<_Del>>>
  483. unique_ptr(_Up __p, const deleter_type& __d) noexcept
  484. : _M_t(__p, __d) { }
  485. /** Takes ownership of a pointer.
  486. *
  487. * @param __p A pointer to an array of a type safely convertible
  488. * to an array of @c element_type
  489. * @param __d A reference to a deleter.
  490. *
  491. * The deleter will be initialized with @p std::move(__d)
  492. */
  493. template<typename _Up, typename _Del = deleter_type,
  494. typename = _Require<__safe_conversion_raw<_Up>,
  495. is_move_constructible<_Del>>>
  496. unique_ptr(_Up __p,
  497. __enable_if_t<!is_lvalue_reference<_Del>::value,
  498. _Del&&> __d) noexcept
  499. : _M_t(std::move(__p), std::move(__d))
  500. { }
  501. template<typename _Up, typename _Del = deleter_type,
  502. typename _DelUnref = typename remove_reference<_Del>::type,
  503. typename = _Require<__safe_conversion_raw<_Up>>>
  504. unique_ptr(_Up,
  505. __enable_if_t<is_lvalue_reference<_Del>::value,
  506. _DelUnref&&>) = delete;
  507. /// Move constructor.
  508. unique_ptr(unique_ptr&&) = default;
  509. /// Creates a unique_ptr that owns nothing.
  510. template<typename _Del = _Dp, typename = _DeleterConstraint<_Del>>
  511. constexpr unique_ptr(nullptr_t) noexcept
  512. : _M_t()
  513. { }
  514. template<typename _Up, typename _Ep, typename = _Require<
  515. __safe_conversion_up<_Up, _Ep>,
  516. typename conditional<is_reference<_Dp>::value,
  517. is_same<_Ep, _Dp>,
  518. is_convertible<_Ep, _Dp>>::type>>
  519. unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
  520. : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter()))
  521. { }
  522. /// Destructor, invokes the deleter if the stored pointer is not null.
  523. ~unique_ptr()
  524. {
  525. auto& __ptr = _M_t._M_ptr();
  526. if (__ptr != nullptr)
  527. get_deleter()(__ptr);
  528. __ptr = pointer();
  529. }
  530. // Assignment.
  531. /** @brief Move assignment operator.
  532. *
  533. * Invokes the deleter if this object owns a pointer.
  534. */
  535. unique_ptr&
  536. operator=(unique_ptr&&) = default;
  537. /** @brief Assignment from another type.
  538. *
  539. * @param __u The object to transfer ownership from, which owns a
  540. * convertible pointer to an array object.
  541. *
  542. * Invokes the deleter if this object owns a pointer.
  543. */
  544. template<typename _Up, typename _Ep>
  545. typename
  546. enable_if<__and_<__safe_conversion_up<_Up, _Ep>,
  547. is_assignable<deleter_type&, _Ep&&>
  548. >::value,
  549. unique_ptr&>::type
  550. operator=(unique_ptr<_Up, _Ep>&& __u) noexcept
  551. {
  552. reset(__u.release());
  553. get_deleter() = std::forward<_Ep>(__u.get_deleter());
  554. return *this;
  555. }
  556. /// Reset the %unique_ptr to empty, invoking the deleter if necessary.
  557. unique_ptr&
  558. operator=(nullptr_t) noexcept
  559. {
  560. reset();
  561. return *this;
  562. }
  563. // Observers.
  564. /// Access an element of owned array.
  565. typename std::add_lvalue_reference<element_type>::type
  566. operator[](size_t __i) const
  567. {
  568. __glibcxx_assert(get() != pointer());
  569. return get()[__i];
  570. }
  571. /// Return the stored pointer.
  572. pointer
  573. get() const noexcept
  574. { return _M_t._M_ptr(); }
  575. /// Return a reference to the stored deleter.
  576. deleter_type&
  577. get_deleter() noexcept
  578. { return _M_t._M_deleter(); }
  579. /// Return a reference to the stored deleter.
  580. const deleter_type&
  581. get_deleter() const noexcept
  582. { return _M_t._M_deleter(); }
  583. /// Return @c true if the stored pointer is not null.
  584. explicit operator bool() const noexcept
  585. { return get() == pointer() ? false : true; }
  586. // Modifiers.
  587. /// Release ownership of any stored pointer.
  588. pointer
  589. release() noexcept
  590. { return _M_t.release(); }
  591. /** @brief Replace the stored pointer.
  592. *
  593. * @param __p The new pointer to store.
  594. *
  595. * The deleter will be invoked if a pointer is already owned.
  596. */
  597. template <typename _Up,
  598. typename = _Require<
  599. __or_<is_same<_Up, pointer>,
  600. __and_<is_same<pointer, element_type*>,
  601. is_pointer<_Up>,
  602. is_convertible<
  603. typename remove_pointer<_Up>::type(*)[],
  604. element_type(*)[]
  605. >
  606. >
  607. >
  608. >>
  609. void
  610. reset(_Up __p) noexcept
  611. { _M_t.reset(std::move(__p)); }
  612. void reset(nullptr_t = nullptr) noexcept
  613. { reset(pointer()); }
  614. /// Exchange the pointer and deleter with another object.
  615. void
  616. swap(unique_ptr& __u) noexcept
  617. {
  618. static_assert(__is_swappable<_Dp>::value, "deleter must be swappable");
  619. _M_t.swap(__u._M_t);
  620. }
  621. // Disable copy from lvalue.
  622. unique_ptr(const unique_ptr&) = delete;
  623. unique_ptr& operator=(const unique_ptr&) = delete;
  624. };
  625. /// @relates unique_ptr @{
  626. /// Swap overload for unique_ptr
  627. template<typename _Tp, typename _Dp>
  628. inline
  629. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  630. // Constrained free swap overload, see p0185r1
  631. typename enable_if<__is_swappable<_Dp>::value>::type
  632. #else
  633. void
  634. #endif
  635. swap(unique_ptr<_Tp, _Dp>& __x,
  636. unique_ptr<_Tp, _Dp>& __y) noexcept
  637. { __x.swap(__y); }
  638. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  639. template<typename _Tp, typename _Dp>
  640. typename enable_if<!__is_swappable<_Dp>::value>::type
  641. swap(unique_ptr<_Tp, _Dp>&,
  642. unique_ptr<_Tp, _Dp>&) = delete;
  643. #endif
  644. /// Equality operator for unique_ptr objects, compares the owned pointers
  645. template<typename _Tp, typename _Dp,
  646. typename _Up, typename _Ep>
  647. _GLIBCXX_NODISCARD inline bool
  648. operator==(const unique_ptr<_Tp, _Dp>& __x,
  649. const unique_ptr<_Up, _Ep>& __y)
  650. { return __x.get() == __y.get(); }
  651. /// unique_ptr comparison with nullptr
  652. template<typename _Tp, typename _Dp>
  653. _GLIBCXX_NODISCARD inline bool
  654. operator==(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept
  655. { return !__x; }
  656. #ifndef __cpp_lib_three_way_comparison
  657. /// unique_ptr comparison with nullptr
  658. template<typename _Tp, typename _Dp>
  659. _GLIBCXX_NODISCARD inline bool
  660. operator==(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept
  661. { return !__x; }
  662. /// Inequality operator for unique_ptr objects, compares the owned pointers
  663. template<typename _Tp, typename _Dp,
  664. typename _Up, typename _Ep>
  665. _GLIBCXX_NODISCARD inline bool
  666. operator!=(const unique_ptr<_Tp, _Dp>& __x,
  667. const unique_ptr<_Up, _Ep>& __y)
  668. { return __x.get() != __y.get(); }
  669. /// unique_ptr comparison with nullptr
  670. template<typename _Tp, typename _Dp>
  671. _GLIBCXX_NODISCARD inline bool
  672. operator!=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept
  673. { return (bool)__x; }
  674. /// unique_ptr comparison with nullptr
  675. template<typename _Tp, typename _Dp>
  676. _GLIBCXX_NODISCARD inline bool
  677. operator!=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept
  678. { return (bool)__x; }
  679. #endif // three way comparison
  680. /// Relational operator for unique_ptr objects, compares the owned pointers
  681. template<typename _Tp, typename _Dp,
  682. typename _Up, typename _Ep>
  683. _GLIBCXX_NODISCARD inline bool
  684. operator<(const unique_ptr<_Tp, _Dp>& __x,
  685. const unique_ptr<_Up, _Ep>& __y)
  686. {
  687. typedef typename
  688. std::common_type<typename unique_ptr<_Tp, _Dp>::pointer,
  689. typename unique_ptr<_Up, _Ep>::pointer>::type _CT;
  690. return std::less<_CT>()(__x.get(), __y.get());
  691. }
  692. /// unique_ptr comparison with nullptr
  693. template<typename _Tp, typename _Dp>
  694. _GLIBCXX_NODISCARD inline bool
  695. operator<(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
  696. {
  697. return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(__x.get(),
  698. nullptr);
  699. }
  700. /// unique_ptr comparison with nullptr
  701. template<typename _Tp, typename _Dp>
  702. _GLIBCXX_NODISCARD inline bool
  703. operator<(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
  704. {
  705. return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(nullptr,
  706. __x.get());
  707. }
  708. /// Relational operator for unique_ptr objects, compares the owned pointers
  709. template<typename _Tp, typename _Dp,
  710. typename _Up, typename _Ep>
  711. _GLIBCXX_NODISCARD inline bool
  712. operator<=(const unique_ptr<_Tp, _Dp>& __x,
  713. const unique_ptr<_Up, _Ep>& __y)
  714. { return !(__y < __x); }
  715. /// unique_ptr comparison with nullptr
  716. template<typename _Tp, typename _Dp>
  717. _GLIBCXX_NODISCARD inline bool
  718. operator<=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
  719. { return !(nullptr < __x); }
  720. /// unique_ptr comparison with nullptr
  721. template<typename _Tp, typename _Dp>
  722. _GLIBCXX_NODISCARD inline bool
  723. operator<=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
  724. { return !(__x < nullptr); }
  725. /// Relational operator for unique_ptr objects, compares the owned pointers
  726. template<typename _Tp, typename _Dp,
  727. typename _Up, typename _Ep>
  728. _GLIBCXX_NODISCARD inline bool
  729. operator>(const unique_ptr<_Tp, _Dp>& __x,
  730. const unique_ptr<_Up, _Ep>& __y)
  731. { return (__y < __x); }
  732. /// unique_ptr comparison with nullptr
  733. template<typename _Tp, typename _Dp>
  734. _GLIBCXX_NODISCARD inline bool
  735. operator>(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
  736. {
  737. return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(nullptr,
  738. __x.get());
  739. }
  740. /// unique_ptr comparison with nullptr
  741. template<typename _Tp, typename _Dp>
  742. _GLIBCXX_NODISCARD inline bool
  743. operator>(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
  744. {
  745. return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(__x.get(),
  746. nullptr);
  747. }
  748. /// Relational operator for unique_ptr objects, compares the owned pointers
  749. template<typename _Tp, typename _Dp,
  750. typename _Up, typename _Ep>
  751. _GLIBCXX_NODISCARD inline bool
  752. operator>=(const unique_ptr<_Tp, _Dp>& __x,
  753. const unique_ptr<_Up, _Ep>& __y)
  754. { return !(__x < __y); }
  755. /// unique_ptr comparison with nullptr
  756. template<typename _Tp, typename _Dp>
  757. _GLIBCXX_NODISCARD inline bool
  758. operator>=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
  759. { return !(__x < nullptr); }
  760. /// unique_ptr comparison with nullptr
  761. template<typename _Tp, typename _Dp>
  762. _GLIBCXX_NODISCARD inline bool
  763. operator>=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
  764. { return !(nullptr < __x); }
  765. #ifdef __cpp_lib_three_way_comparison
  766. template<typename _Tp, typename _Dp, typename _Up, typename _Ep>
  767. requires three_way_comparable_with<typename unique_ptr<_Tp, _Dp>::pointer,
  768. typename unique_ptr<_Up, _Ep>::pointer>
  769. inline
  770. compare_three_way_result_t<typename unique_ptr<_Tp, _Dp>::pointer,
  771. typename unique_ptr<_Up, _Ep>::pointer>
  772. operator<=>(const unique_ptr<_Tp, _Dp>& __x,
  773. const unique_ptr<_Up, _Ep>& __y)
  774. { return compare_three_way()(__x.get(), __y.get()); }
  775. template<typename _Tp, typename _Dp>
  776. requires three_way_comparable<typename unique_ptr<_Tp, _Dp>::pointer>
  777. inline
  778. compare_three_way_result_t<typename unique_ptr<_Tp, _Dp>::pointer>
  779. operator<=>(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
  780. {
  781. using pointer = typename unique_ptr<_Tp, _Dp>::pointer;
  782. return compare_three_way()(__x.get(), static_cast<pointer>(nullptr));
  783. }
  784. #endif
  785. // @} relates unique_ptr
  786. /// @cond undocumented
  787. template<typename _Up, typename _Ptr = typename _Up::pointer,
  788. bool = __poison_hash<_Ptr>::__enable_hash_call>
  789. struct __uniq_ptr_hash
  790. #if ! _GLIBCXX_INLINE_VERSION
  791. : private __poison_hash<_Ptr>
  792. #endif
  793. {
  794. size_t
  795. operator()(const _Up& __u) const
  796. noexcept(noexcept(std::declval<hash<_Ptr>>()(std::declval<_Ptr>())))
  797. { return hash<_Ptr>()(__u.get()); }
  798. };
  799. template<typename _Up, typename _Ptr>
  800. struct __uniq_ptr_hash<_Up, _Ptr, false>
  801. : private __poison_hash<_Ptr>
  802. { };
  803. /// @endcond
  804. /// std::hash specialization for unique_ptr.
  805. template<typename _Tp, typename _Dp>
  806. struct hash<unique_ptr<_Tp, _Dp>>
  807. : public __hash_base<size_t, unique_ptr<_Tp, _Dp>>,
  808. public __uniq_ptr_hash<unique_ptr<_Tp, _Dp>>
  809. { };
  810. #if __cplusplus >= 201402L
  811. /// @relates unique_ptr @{
  812. #define __cpp_lib_make_unique 201304
  813. /// @cond undocumented
  814. template<typename _Tp>
  815. struct _MakeUniq
  816. { typedef unique_ptr<_Tp> __single_object; };
  817. template<typename _Tp>
  818. struct _MakeUniq<_Tp[]>
  819. { typedef unique_ptr<_Tp[]> __array; };
  820. template<typename _Tp, size_t _Bound>
  821. struct _MakeUniq<_Tp[_Bound]>
  822. { struct __invalid_type { }; };
  823. /// @endcond
  824. /// std::make_unique for single objects
  825. template<typename _Tp, typename... _Args>
  826. inline typename _MakeUniq<_Tp>::__single_object
  827. make_unique(_Args&&... __args)
  828. { return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...)); }
  829. /// std::make_unique for arrays of unknown bound
  830. template<typename _Tp>
  831. inline typename _MakeUniq<_Tp>::__array
  832. make_unique(size_t __num)
  833. { return unique_ptr<_Tp>(new remove_extent_t<_Tp>[__num]()); }
  834. /// Disable std::make_unique for arrays of known bound
  835. template<typename _Tp, typename... _Args>
  836. inline typename _MakeUniq<_Tp>::__invalid_type
  837. make_unique(_Args&&...) = delete;
  838. // @} relates unique_ptr
  839. #endif // C++14
  840. #if __cplusplus > 201703L && __cpp_concepts
  841. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  842. // 2948. unique_ptr does not define operator<< for stream output
  843. /// Stream output operator for unique_ptr
  844. template<typename _CharT, typename _Traits, typename _Tp, typename _Dp>
  845. inline basic_ostream<_CharT, _Traits>&
  846. operator<<(basic_ostream<_CharT, _Traits>& __os,
  847. const unique_ptr<_Tp, _Dp>& __p)
  848. requires requires { __os << __p.get(); }
  849. {
  850. __os << __p.get();
  851. return __os;
  852. }
  853. #endif // C++20
  854. // @} group pointer_abstractions
  855. #if __cplusplus >= 201703L
  856. namespace __detail::__variant
  857. {
  858. template<typename> struct _Never_valueless_alt; // see <variant>
  859. // Provide the strong exception-safety guarantee when emplacing a
  860. // unique_ptr into a variant.
  861. template<typename _Tp, typename _Del>
  862. struct _Never_valueless_alt<std::unique_ptr<_Tp, _Del>>
  863. : std::true_type
  864. { };
  865. } // namespace __detail::__variant
  866. #endif // C++17
  867. _GLIBCXX_END_NAMESPACE_VERSION
  868. } // namespace
  869. #endif /* _UNIQUE_PTR_H */