unique_ptr.h 25 KB

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