unique_ptr.hpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2014-2014. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/move for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_MOVE_UNIQUE_PTR_HPP_INCLUDED
  11. #define BOOST_MOVE_UNIQUE_PTR_HPP_INCLUDED
  12. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #
  16. #if defined(BOOST_HAS_PRAGMA_ONCE)
  17. # pragma once
  18. #endif
  19. #include <boost/move/detail/config_begin.hpp>
  20. #include <boost/move/detail/workaround.hpp> //forceinline
  21. #include <boost/move/detail/unique_ptr_meta_utils.hpp>
  22. #include <boost/move/default_delete.hpp>
  23. #include <boost/move/utility_core.hpp>
  24. #include <boost/move/adl_move_swap.hpp>
  25. #include <cassert>
  26. #include <cstddef> //For std::nullptr_t and std::size_t
  27. //!\file
  28. //! Describes the smart pointer unique_ptr, a drop-in replacement for std::unique_ptr,
  29. //! usable also from C++03 compilers.
  30. //!
  31. //! Main differences from std::unique_ptr to avoid heavy dependencies,
  32. //! specially in C++03 compilers:
  33. //! - <tt>operator < </tt> uses pointer <tt>operator < </tt>instead of <tt>std::less<common_type></tt>.
  34. //! This avoids dependencies on <tt>std::common_type</tt> and <tt>std::less</tt>
  35. //! (<tt><type_traits>/<functional></tt> headers). In C++03 this avoid pulling Boost.Typeof and other
  36. //! cascading dependencies. As in all Boost platforms <tt>operator <</tt> on raw pointers and
  37. //! other smart pointers provides strict weak ordering in practice this should not be a problem for users.
  38. //! - assignable from literal 0 for compilers without nullptr
  39. //! - <tt>unique_ptr<T[]></tt> is constructible and assignable from <tt>unique_ptr<U[]></tt> if
  40. //! cv-less T and cv-less U are the same type and T is more CV qualified than U.
  41. namespace boost{
  42. // @cond
  43. namespace move_upd {
  44. ////////////////////////////////////////////
  45. // deleter types
  46. ////////////////////////////////////////////
  47. #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  48. template <class T>
  49. class is_noncopyable
  50. {
  51. typedef char true_t;
  52. class false_t { char dummy[2]; };
  53. template<class U> static false_t dispatch(...);
  54. template<class U> static true_t dispatch(typename U::boost_move_no_copy_constructor_or_assign*);
  55. public:
  56. static const bool value = sizeof(dispatch<T>(0)) == sizeof(true_t);
  57. };
  58. #endif //defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  59. template <class D>
  60. struct deleter_types
  61. {
  62. typedef typename bmupmu::add_lvalue_reference<D>::type del_ref;
  63. typedef typename bmupmu::add_const_lvalue_reference<D>::type del_cref;
  64. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  65. typedef typename bmupmu::if_c
  66. < bmupmu::is_lvalue_reference<D>::value, D, del_cref >::type deleter_arg_type1;
  67. typedef typename bmupmu::remove_reference<D>::type && deleter_arg_type2;
  68. #else
  69. typedef typename bmupmu::if_c
  70. < is_noncopyable<D>::value, bmupmu::nat, del_cref>::type non_ref_deleter_arg1;
  71. typedef typename bmupmu::if_c< bmupmu::is_lvalue_reference<D>::value
  72. , D, non_ref_deleter_arg1 >::type deleter_arg_type1;
  73. typedef ::boost::rv<D> & deleter_arg_type2;
  74. #endif
  75. };
  76. ////////////////////////////////////////////
  77. // unique_ptr_data
  78. ////////////////////////////////////////////
  79. template <class P, class D, bool = bmupmu::is_unary_function<D>::value || bmupmu::is_reference<D>::value >
  80. struct unique_ptr_data
  81. {
  82. typedef typename deleter_types<D>::deleter_arg_type1 deleter_arg_type1;
  83. typedef typename deleter_types<D>::del_ref del_ref;
  84. typedef typename deleter_types<D>::del_cref del_cref;
  85. inline unique_ptr_data() BOOST_NOEXCEPT
  86. : m_p(), d()
  87. {}
  88. inline explicit unique_ptr_data(P p) BOOST_NOEXCEPT
  89. : m_p(p), d()
  90. {}
  91. inline unique_ptr_data(P p, deleter_arg_type1 d1) BOOST_NOEXCEPT
  92. : m_p(p), d(d1)
  93. {}
  94. template <class U>
  95. inline unique_ptr_data(P p, BOOST_FWD_REF(U) d1) BOOST_NOEXCEPT
  96. : m_p(p), d(::boost::forward<U>(d1))
  97. {}
  98. inline del_ref deleter() { return d; }
  99. inline del_cref deleter() const{ return d; }
  100. P m_p;
  101. D d;
  102. private:
  103. unique_ptr_data& operator=(const unique_ptr_data&);
  104. unique_ptr_data(const unique_ptr_data&);
  105. };
  106. template <class P, class D>
  107. struct unique_ptr_data<P, D, false>
  108. : private D
  109. {
  110. typedef typename deleter_types<D>::deleter_arg_type1 deleter_arg_type1;
  111. typedef typename deleter_types<D>::del_ref del_ref;
  112. typedef typename deleter_types<D>::del_cref del_cref;
  113. inline unique_ptr_data() BOOST_NOEXCEPT
  114. : D(), m_p()
  115. {}
  116. inline explicit unique_ptr_data(P p) BOOST_NOEXCEPT
  117. : D(), m_p(p)
  118. {}
  119. inline unique_ptr_data(P p, deleter_arg_type1 d1) BOOST_NOEXCEPT
  120. : D(d1), m_p(p)
  121. {}
  122. template <class U>
  123. inline unique_ptr_data(P p, BOOST_FWD_REF(U) d) BOOST_NOEXCEPT
  124. : D(::boost::forward<U>(d)), m_p(p)
  125. {}
  126. inline del_ref deleter() BOOST_NOEXCEPT { return static_cast<del_ref>(*this); }
  127. inline del_cref deleter() const BOOST_NOEXCEPT { return static_cast<del_cref>(*this); }
  128. P m_p;
  129. private:
  130. unique_ptr_data& operator=(const unique_ptr_data&);
  131. unique_ptr_data(const unique_ptr_data&);
  132. };
  133. ////////////////////////////////////////////
  134. // is_unique_ptr_convertible
  135. ////////////////////////////////////////////
  136. //Although non-standard, we avoid using pointer_traits
  137. //to avoid heavy dependencies
  138. template <typename T>
  139. struct get_element_type
  140. {
  141. struct DefaultWrap { typedef bmupmu::natify<T> element_type; };
  142. template <typename X> static char test(int, typename X::element_type*);
  143. template <typename X> static int test(...);
  144. static const bool value = (1 == sizeof(test<T>(0, 0)));
  145. typedef typename bmupmu::if_c<value, T, DefaultWrap>::type::element_type type;
  146. };
  147. template<class T>
  148. struct get_element_type<T*>
  149. {
  150. typedef T type;
  151. };
  152. template<class T>
  153. struct get_cvelement
  154. : bmupmu::remove_cv<typename get_element_type<T>::type>
  155. {};
  156. template <class P1, class P2>
  157. struct is_same_cvelement_and_convertible
  158. {
  159. typedef typename bmupmu::remove_reference<P1>::type arg1;
  160. typedef typename bmupmu::remove_reference<P2>::type arg2;
  161. static const bool same_cvless =
  162. bmupmu::is_same<typename get_cvelement<arg1>::type,typename get_cvelement<arg2>::type>::value;
  163. static const bool value = same_cvless && bmupmu::is_convertible<arg1, arg2>::value;
  164. };
  165. template<bool IsArray, class FromPointer, class ThisPointer>
  166. struct is_unique_ptr_convertible
  167. : is_same_cvelement_and_convertible<FromPointer, ThisPointer>
  168. {};
  169. template<class FromPointer, class ThisPointer>
  170. struct is_unique_ptr_convertible<false, FromPointer, ThisPointer>
  171. : bmupmu::is_convertible<FromPointer, ThisPointer>
  172. {};
  173. ////////////////////////////////////////
  174. //// enable_up_moveconv_assign
  175. ////////////////////////////////////////
  176. template<class T, class FromPointer, class ThisPointer, class Type = bmupmu::nat>
  177. struct enable_up_ptr
  178. : bmupmu::enable_if_c< is_unique_ptr_convertible
  179. < bmupmu::is_array<T>::value, FromPointer, ThisPointer>::value, Type>
  180. {};
  181. ////////////////////////////////////////
  182. //// enable_up_moveconv_assign
  183. ////////////////////////////////////////
  184. template<class T, class D, class U, class E>
  185. struct unique_moveconvert_assignable
  186. {
  187. static const bool t_is_array = bmupmu::is_array<T>::value;
  188. static const bool value =
  189. t_is_array == bmupmu::is_array<U>::value &&
  190. bmupmu::extent<T>::value == bmupmu::extent<U>::value &&
  191. is_unique_ptr_convertible
  192. < t_is_array
  193. , typename bmupmu::pointer_type<U, E>::type, typename bmupmu::pointer_type<T, D>::type
  194. >::value;
  195. };
  196. template<class T, class D, class U, class E, std::size_t N>
  197. struct unique_moveconvert_assignable<T[], D, U[N], E>
  198. : unique_moveconvert_assignable<T[], D, U[], E>
  199. {};
  200. template<class T, class D, class U, class E, class Type = bmupmu::nat>
  201. struct enable_up_moveconv_assign
  202. : bmupmu::enable_if_c<unique_moveconvert_assignable<T, D, U, E>::value, Type>
  203. {};
  204. ////////////////////////////////////////
  205. //// enable_up_moveconv_constr
  206. ////////////////////////////////////////
  207. template<class D, class E, bool IsReference = bmupmu::is_reference<D>::value>
  208. struct unique_deleter_is_initializable
  209. : bmupmu::is_same<D, E>
  210. {};
  211. template <class T, class U>
  212. class is_rvalue_convertible
  213. {
  214. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  215. typedef typename bmupmu::remove_reference<T>::type&& t_from;
  216. #else
  217. typedef typename bmupmu::if_c
  218. < ::boost::has_move_emulation_enabled<T>::value && !bmupmu::is_reference<T>::value
  219. , ::boost::rv<T>&
  220. , typename bmupmu::add_lvalue_reference<T>::type
  221. >::type t_from;
  222. #endif
  223. typedef char true_t;
  224. class false_t { char dummy[2]; };
  225. static false_t dispatch(...);
  226. static true_t dispatch(U);
  227. static t_from trigger();
  228. public:
  229. static const bool value = sizeof(dispatch(trigger())) == sizeof(true_t);
  230. };
  231. template<class D, class E>
  232. struct unique_deleter_is_initializable<D, E, false>
  233. {
  234. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  235. //Clang has some problems with is_rvalue_convertible with non-copyable types
  236. //so use intrinsic if available
  237. #if defined(BOOST_CLANG)
  238. #if __has_feature(is_convertible_to)
  239. static const bool value = __is_convertible_to(E, D);
  240. #else
  241. static const bool value = is_rvalue_convertible<E, D>::value;
  242. #endif
  243. #else
  244. static const bool value = is_rvalue_convertible<E, D>::value;
  245. #endif
  246. #else //!defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  247. //No hope for compilers with move emulation for now. In several compilers is_convertible
  248. // leads to errors, so just move the Deleter and see if the conversion works
  249. static const bool value = true; /*is_rvalue_convertible<E, D>::value*/
  250. #endif
  251. };
  252. template<class T, class D, class U, class E, class Type = bmupmu::nat>
  253. struct enable_up_moveconv_constr
  254. : bmupmu::enable_if_c
  255. < unique_moveconvert_assignable<T, D, U, E>::value && unique_deleter_is_initializable<D, E>::value
  256. , Type>
  257. {};
  258. } //namespace move_upd {
  259. // @endcond
  260. namespace movelib {
  261. //! A unique pointer is an object that owns another object and
  262. //! manages that other object through a pointer.
  263. //!
  264. //! More precisely, a unique pointer is an object u that stores a pointer to a second object p and will dispose
  265. //! of p when u is itself destroyed (e.g., when leaving block scope). In this context, u is said to own p.
  266. //!
  267. //! The mechanism by which u disposes of p is known as p's associated deleter, a function object whose correct
  268. //! invocation results in p's appropriate disposition (typically its deletion).
  269. //!
  270. //! Let the notation u.p denote the pointer stored by u, and let u.d denote the associated deleter. Upon request,
  271. //! u can reset (replace) u.p and u.d with another pointer and deleter, but must properly dispose of its owned
  272. //! object via the associated deleter before such replacement is considered completed.
  273. //!
  274. //! Additionally, u can, upon request, transfer ownership to another unique pointer u2. Upon completion of
  275. //! such a transfer, the following postconditions hold:
  276. //! - u2.p is equal to the pre-transfer u.p,
  277. //! - u.p is equal to nullptr, and
  278. //! - if the pre-transfer u.d maintained state, such state has been transferred to u2.d.
  279. //!
  280. //! As in the case of a reset, u2 must properly dispose of its pre-transfer owned object via the pre-transfer
  281. //! associated deleter before the ownership transfer is considered complete.
  282. //!
  283. //! Each object of a type U instantiated from the unique_ptr template specified in this subclause has the strict
  284. //! ownership semantics, specified above, of a unique pointer. In partial satisfaction of these semantics, each
  285. //! such U is MoveConstructible and MoveAssignable, but is not CopyConstructible nor CopyAssignable.
  286. //! The template parameter T of unique_ptr may be an incomplete type.
  287. //!
  288. //! The uses of unique_ptr include providing exception safety for dynamically allocated memory, passing
  289. //! ownership of dynamically allocated memory to a function, and returning dynamically allocated memory from
  290. //! a function.
  291. //!
  292. //! If T is an array type (e.g. unique_ptr<MyType[]>) the interface is slightly altered:
  293. //! - Pointers to types derived from T are rejected by the constructors, and by reset.
  294. //! - The observers <tt>operator*</tt> and <tt>operator-></tt> are not provided.
  295. //! - The indexing observer <tt>operator[]</tt> is provided.
  296. //!
  297. //! \tparam T Provides the type of the stored pointer.
  298. //! \tparam D The deleter type:
  299. //! - The default type for the template parameter D is default_delete. A client-supplied template argument
  300. //! D shall be a function object type, lvalue-reference to function, or lvalue-reference to function object type
  301. //! for which, given a value d of type D and a value ptr of type unique_ptr<T, D>::pointer, the expression
  302. //! d(ptr) is valid and has the effect of disposing of the pointer as appropriate for that deleter.
  303. //! - If the deleter's type D is not a reference type, D shall satisfy the requirements of Destructible.
  304. //! - If the type <tt>remove_reference<D>::type::pointer</tt> exists, it shall satisfy the requirements of NullablePointer.
  305. template <class T, class D = default_delete<T> >
  306. class unique_ptr
  307. {
  308. #if defined(BOOST_MOVE_DOXYGEN_INVOKED)
  309. public:
  310. unique_ptr(const unique_ptr&) = delete;
  311. unique_ptr& operator=(const unique_ptr&) = delete;
  312. private:
  313. #else
  314. BOOST_MOVABLE_BUT_NOT_COPYABLE(unique_ptr)
  315. typedef bmupmu::pointer_type<T, D > pointer_type_obtainer;
  316. typedef bmupd::unique_ptr_data
  317. <typename pointer_type_obtainer::type, D> data_type;
  318. typedef typename bmupd::deleter_types<D>::deleter_arg_type1 deleter_arg_type1;
  319. typedef typename bmupd::deleter_types<D>::deleter_arg_type2 deleter_arg_type2;
  320. data_type m_data;
  321. #endif
  322. public:
  323. //! If the type <tt>remove_reference<D>::type::pointer</tt> exists, then it shall be a
  324. //! synonym for <tt>remove_reference<D>::type::pointer</tt>. Otherwise it shall be a
  325. //! synonym for T*.
  326. typedef typename BOOST_MOVE_SEEDOC(pointer_type_obtainer::type) pointer;
  327. //! If T is an array type, then element_type is equal to T. Otherwise, if T is a type
  328. //! in the form U[], element_type is equal to U.
  329. typedef typename BOOST_MOVE_SEEDOC(bmupmu::remove_extent<T>::type) element_type;
  330. typedef D deleter_type;
  331. //! <b>Requires</b>: D shall satisfy the requirements of DefaultConstructible, and
  332. //! that construction shall not throw an exception.
  333. //!
  334. //! <b>Effects</b>: Constructs a unique_ptr object that owns nothing, value-initializing the
  335. //! stored pointer and the stored deleter.
  336. //!
  337. //! <b>Postconditions</b>: <tt>get() == nullptr</tt>. <tt>get_deleter()</tt> returns a reference to the stored deleter.
  338. //!
  339. //! <b>Remarks</b>: If this constructor is instantiated with a pointer type or reference type
  340. //! for the template argument D, the program is ill-formed.
  341. inline BOOST_CONSTEXPR unique_ptr() BOOST_NOEXCEPT
  342. : m_data()
  343. {
  344. //If this constructor is instantiated with a pointer type or reference type
  345. //for the template argument D, the program is ill-formed.
  346. BOOST_MOVE_STATIC_ASSERT(!bmupmu::is_pointer<D>::value);
  347. BOOST_MOVE_STATIC_ASSERT(!bmupmu::is_reference<D>::value);
  348. }
  349. //! <b>Effects</b>: Same as <tt>unique_ptr()</tt> (default constructor).
  350. //!
  351. inline BOOST_CONSTEXPR unique_ptr(BOOST_MOVE_DOC0PTR(bmupd::nullptr_type)) BOOST_NOEXCEPT
  352. : m_data()
  353. {
  354. //If this constructor is instantiated with a pointer type or reference type
  355. //for the template argument D, the program is ill-formed.
  356. BOOST_MOVE_STATIC_ASSERT(!bmupmu::is_pointer<D>::value);
  357. BOOST_MOVE_STATIC_ASSERT(!bmupmu::is_reference<D>::value);
  358. }
  359. //! <b>Requires</b>: D shall satisfy the requirements of DefaultConstructible, and
  360. //! that construction shall not throw an exception.
  361. //!
  362. //! <b>Effects</b>: Constructs a unique_ptr which owns p, initializing the stored pointer
  363. //! with p and value initializing the stored deleter.
  364. //!
  365. //! <b>Postconditions</b>: <tt>get() == p</tt>. <tt>get_deleter()</tt> returns a reference to the stored deleter.
  366. //!
  367. //! <b>Remarks</b>: If this constructor is instantiated with a pointer type or reference type
  368. //! for the template argument D, the program is ill-formed.
  369. //! This constructor shall not participate in overload resolution unless:
  370. //! - If T is not an array type and Pointer is implicitly convertible to pointer.
  371. //! - If T is an array type and Pointer is a more CV qualified pointer to element_type.
  372. template<class Pointer>
  373. inline explicit unique_ptr(Pointer p
  374. BOOST_MOVE_DOCIGN(BOOST_MOVE_I typename bmupd::enable_up_ptr<T BOOST_MOVE_I Pointer BOOST_MOVE_I pointer>::type* =0)
  375. ) BOOST_NOEXCEPT
  376. : m_data(p)
  377. {
  378. //If T is not an array type, element_type_t<Pointer> derives from T
  379. //it uses the default deleter and T has no virtual destructor, then you have a problem
  380. BOOST_MOVE_STATIC_ASSERT(( !bmupd::missing_virtual_destructor
  381. <D, typename bmupd::get_element_type<Pointer>::type>::value ));
  382. //If this constructor is instantiated with a pointer type or reference type
  383. //for the template argument D, the program is ill-formed.
  384. BOOST_MOVE_STATIC_ASSERT(!bmupmu::is_pointer<D>::value);
  385. BOOST_MOVE_STATIC_ASSERT(!bmupmu::is_reference<D>::value);
  386. }
  387. //!The signature of this constructor depends upon whether D is a reference type.
  388. //! - If D is non-reference type A, then the signature is <tt>unique_ptr(pointer p, const A& d)</tt>.
  389. //! - If D is an lvalue-reference type A&, then the signature is <tt>unique_ptr(pointer p, A& d)</tt>.
  390. //! - If D is an lvalue-reference type const A&, then the signature is <tt>unique_ptr(pointer p, const A& d)</tt>.
  391. //!
  392. //!
  393. //! <b>Requires</b>: Either
  394. //! - D is not an lvalue-reference type and d is an lvalue or const rvalue.
  395. //! D shall satisfy the requirements of CopyConstructible, and the copy constructor of D
  396. //! shall not throw an exception. This unique_ptr will hold a copy of d.
  397. //! - D is an lvalue-reference type and d is an lvalue. the type which D references need not be CopyConstructible nor
  398. //! MoveConstructible. This unique_ptr will hold a D which refers to the lvalue d.
  399. //!
  400. //! <b>Effects</b>: Constructs a unique_ptr object which owns p, initializing the stored pointer with p and
  401. //! initializing the deleter as described above.
  402. //!
  403. //! <b>Postconditions</b>: <tt>get() == p</tt>. <tt>get_deleter()</tt> returns a reference to the stored deleter. If D is a
  404. //! reference type then <tt>get_deleter()</tt> returns a reference to the lvalue d.
  405. //!
  406. //! <b>Remarks</b>: This constructor shall not participate in overload resolution unless:
  407. //! - If T is not an array type and Pointer is implicitly convertible to pointer.
  408. //! - If T is an array type and Pointer is a more CV qualified pointer to element_type.
  409. template<class Pointer>
  410. inline unique_ptr(Pointer p, BOOST_MOVE_SEEDOC(deleter_arg_type1) d1
  411. BOOST_MOVE_DOCIGN(BOOST_MOVE_I typename bmupd::enable_up_ptr<T BOOST_MOVE_I Pointer BOOST_MOVE_I pointer>::type* =0)
  412. ) BOOST_NOEXCEPT
  413. : m_data(p, d1)
  414. {
  415. //If T is not an array type, element_type_t<Pointer> derives from T
  416. //it uses the default deleter and T has no virtual destructor, then you have a problem
  417. BOOST_MOVE_STATIC_ASSERT(( !bmupd::missing_virtual_destructor
  418. <D, typename bmupd::get_element_type<Pointer>::type>::value ));
  419. }
  420. //! <b>Effects</b>: Same effects as <tt>template<class Pointer> unique_ptr(Pointer p, deleter_arg_type1 d1)</tt>
  421. //! and additionally <tt>get() == nullptr</tt>
  422. inline unique_ptr(BOOST_MOVE_DOC0PTR(bmupd::nullptr_type), BOOST_MOVE_SEEDOC(deleter_arg_type1) d1) BOOST_NOEXCEPT
  423. : m_data(pointer(), d1)
  424. {}
  425. //! The signature of this constructor depends upon whether D is a reference type.
  426. //! - If D is non-reference type A, then the signature is <tt>unique_ptr(pointer p, A&& d)</tt>.
  427. //! - If D is an lvalue-reference type A&, then the signature is <tt>unique_ptr(pointer p, A&& d)</tt>.
  428. //! - If D is an lvalue-reference type const A&, then the signature is <tt>unique_ptr(pointer p, const A&& d)</tt>.
  429. //!
  430. //! <b>Requires</b>: Either
  431. //! - D is not an lvalue-reference type and d is a non-const rvalue. D
  432. //! shall satisfy the requirements of MoveConstructible, and the move constructor
  433. //! of D shall not throw an exception. This unique_ptr will hold a value move constructed from d.
  434. //! - D is an lvalue-reference type and d is an rvalue, the program is ill-formed.
  435. //!
  436. //! <b>Effects</b>: Constructs a unique_ptr object which owns p, initializing the stored pointer with p and
  437. //! initializing the deleter as described above.
  438. //!
  439. //! <b>Postconditions</b>: <tt>get() == p</tt>. <tt>get_deleter()</tt> returns a reference to the stored deleter. If D is a
  440. //! reference type then <tt>get_deleter()</tt> returns a reference to the lvalue d.
  441. //!
  442. //! <b>Remarks</b>: This constructor shall not participate in overload resolution unless:
  443. //! - If T is not an array type and Pointer is implicitly convertible to pointer.
  444. //! - If T is an array type and Pointer is a more CV qualified pointer to element_type.
  445. template<class Pointer>
  446. inline unique_ptr(Pointer p, BOOST_MOVE_SEEDOC(deleter_arg_type2) d2
  447. BOOST_MOVE_DOCIGN(BOOST_MOVE_I typename bmupd::enable_up_ptr<T BOOST_MOVE_I Pointer BOOST_MOVE_I pointer>::type* =0)
  448. ) BOOST_NOEXCEPT
  449. : m_data(p, ::boost::move(d2))
  450. {
  451. //If T is not an array type, element_type_t<Pointer> derives from T
  452. //it uses the default deleter and T has no virtual destructor, then you have a problem
  453. BOOST_MOVE_STATIC_ASSERT(( !bmupd::missing_virtual_destructor
  454. <D, typename bmupd::get_element_type<Pointer>::type>::value ));
  455. }
  456. //! <b>Effects</b>: Same effects as <tt>template<class Pointer> unique_ptr(Pointer p, deleter_arg_type2 d2)</tt>
  457. //! and additionally <tt>get() == nullptr</tt>
  458. inline unique_ptr(BOOST_MOVE_DOC0PTR(bmupd::nullptr_type), BOOST_MOVE_SEEDOC(deleter_arg_type2) d2) BOOST_NOEXCEPT
  459. : m_data(pointer(), ::boost::move(d2))
  460. {}
  461. //! <b>Requires</b>: If D is not a reference type, D shall satisfy the requirements of MoveConstructible.
  462. //! Construction of the deleter from an rvalue of type D shall not throw an exception.
  463. //!
  464. //! <b>Effects</b>: Constructs a unique_ptr by transferring ownership from u to *this. If D is a reference type,
  465. //! this deleter is copy constructed from u's deleter; otherwise, this deleter is move constructed from u's
  466. //! deleter.
  467. //!
  468. //! <b>Postconditions</b>: <tt>get()</tt> yields the value u.get() yielded before the construction. <tt>get_deleter()</tt>
  469. //! returns a reference to the stored deleter that was constructed from u.get_deleter(). If D is a
  470. //! reference type then <tt>get_deleter()</tt> and <tt>u.get_deleter()</tt> both reference the same lvalue deleter.
  471. inline unique_ptr(BOOST_RV_REF(unique_ptr) u) BOOST_NOEXCEPT
  472. : m_data(u.release(), ::boost::move_if_not_lvalue_reference<D>(u.get_deleter()))
  473. {}
  474. //! <b>Requires</b>: If E is not a reference type, construction of the deleter from an rvalue of type E shall be
  475. //! well formed and shall not throw an exception. Otherwise, E is a reference type and construction of the
  476. //! deleter from an lvalue of type E shall be well formed and shall not throw an exception.
  477. //!
  478. //! <b>Remarks</b>: This constructor shall not participate in overload resolution unless:
  479. //! - <tt>unique_ptr<U, E>::pointer</tt> is implicitly convertible to pointer,
  480. //! - U is not an array type, and
  481. //! - either D is a reference type and E is the same type as D, or D is not a reference type and E is
  482. //! implicitly convertible to D.
  483. //!
  484. //! <b>Effects</b>: Constructs a unique_ptr by transferring ownership from u to *this. If E is a reference type,
  485. //! this deleter is copy constructed from u's deleter; otherwise, this deleter is move constructed from u's deleter.
  486. //!
  487. //! <b>Postconditions</b>: <tt>get()</tt> yields the value <tt>u.get()</tt> yielded before the construction. <tt>get_deleter()</tt>
  488. //! returns a reference to the stored deleter that was constructed from <tt>u.get_deleter()</tt>.
  489. template <class U, class E>
  490. inline unique_ptr( BOOST_RV_REF_BEG_IF_CXX11 unique_ptr<U, E> BOOST_RV_REF_END_IF_CXX11 u
  491. BOOST_MOVE_DOCIGN(BOOST_MOVE_I typename bmupd::enable_up_moveconv_constr<T BOOST_MOVE_I D BOOST_MOVE_I U BOOST_MOVE_I E>::type* =0)
  492. ) BOOST_NOEXCEPT
  493. : m_data(u.release(), ::boost::move_if_not_lvalue_reference<E>(u.get_deleter()))
  494. {
  495. //If T is not an array type, U derives from T
  496. //it uses the default deleter and T has no virtual destructor, then you have a problem
  497. BOOST_MOVE_STATIC_ASSERT(( !bmupd::missing_virtual_destructor
  498. <D, typename unique_ptr<U, E>::pointer>::value ));
  499. }
  500. //! <b>Requires</b>: The expression <tt>get_deleter()(get())</tt> shall be well formed, shall have well-defined behavior,
  501. //! and shall not throw exceptions.
  502. //!
  503. //! <b>Effects</b>: If <tt>get() == nullpt1r</tt> there are no effects. Otherwise <tt>get_deleter()(get())</tt>.
  504. //!
  505. //! <b>Note</b>: The use of default_delete requires T to be a complete type
  506. ~unique_ptr()
  507. { if(m_data.m_p) m_data.deleter()(m_data.m_p); }
  508. //! <b>Requires</b>: If D is not a reference type, D shall satisfy the requirements of MoveAssignable
  509. //! and assignment of the deleter from an rvalue of type D shall not throw an exception. Otherwise, D
  510. //! is a reference type; <tt>remove_reference<D>::type</tt> shall satisfy the CopyAssignable requirements and
  511. //! assignment of the deleter from an lvalue of type D shall not throw an exception.
  512. //!
  513. //! <b>Effects</b>: Transfers ownership from u to *this as if by calling <tt>reset(u.release())</tt> followed
  514. //! by <tt>get_deleter() = std::forward<D>(u.get_deleter())</tt>.
  515. //!
  516. //! <b>Returns</b>: *this.
  517. unique_ptr& operator=(BOOST_RV_REF(unique_ptr) u) BOOST_NOEXCEPT
  518. {
  519. this->reset(u.release());
  520. m_data.deleter() = ::boost::move_if_not_lvalue_reference<D>(u.get_deleter());
  521. return *this;
  522. }
  523. //! <b>Requires</b>: If E is not a reference type, assignment of the deleter from an rvalue of type E shall be
  524. //! well-formed and shall not throw an exception. Otherwise, E is a reference type and assignment of the
  525. //! deleter from an lvalue of type E shall be well-formed and shall not throw an exception.
  526. //!
  527. //! <b>Remarks</b>: This operator shall not participate in overload resolution unless:
  528. //! - <tt>unique_ptr<U, E>::pointer</tt> is implicitly convertible to pointer and
  529. //! - U is not an array type.
  530. //!
  531. //! <b>Effects</b>: Transfers ownership from u to *this as if by calling <tt>reset(u.release())</tt> followed by
  532. //! <tt>get_deleter() = std::forward<E>(u.get_deleter())</tt>.
  533. //!
  534. //! <b>Returns</b>: *this.
  535. template <class U, class E>
  536. BOOST_MOVE_DOC1ST(unique_ptr&, typename bmupd::enable_up_moveconv_assign
  537. <T BOOST_MOVE_I D BOOST_MOVE_I U BOOST_MOVE_I E BOOST_MOVE_I unique_ptr &>::type)
  538. operator=(BOOST_RV_REF_BEG unique_ptr<U, E> BOOST_RV_REF_END u) BOOST_NOEXCEPT
  539. {
  540. this->reset(u.release());
  541. m_data.deleter() = ::boost::move_if_not_lvalue_reference<E>(u.get_deleter());
  542. return *this;
  543. }
  544. //! <b>Effects</b>: <tt>reset()</tt>.
  545. //!
  546. //! <b>Postcondition</b>: <tt>get() == nullptr</tt>
  547. //!
  548. //! <b>Returns</b>: *this.
  549. unique_ptr& operator=(BOOST_MOVE_DOC0PTR(bmupd::nullptr_type)) BOOST_NOEXCEPT
  550. { this->reset(); return *this; }
  551. //! <b>Requires</b>: <tt>get() != nullptr</tt>.
  552. //!
  553. //! <b>Returns</b>: <tt>*get()</tt>.
  554. //!
  555. //! <b>Remarks</b: If T is an array type, the program is ill-formed.
  556. BOOST_MOVE_DOC1ST(element_type&, typename bmupmu::add_lvalue_reference<element_type>::type)
  557. operator*() const BOOST_NOEXCEPT
  558. {
  559. BOOST_MOVE_STATIC_ASSERT((!bmupmu::is_array<T>::value));
  560. return *m_data.m_p;
  561. }
  562. //! <b>Requires</b>: i < the number of elements in the array to which the stored pointer points.
  563. //!
  564. //! <b>Returns</b>: <tt>get()[i]</tt>.
  565. //!
  566. //! <b>Remarks</b: If T is not an array type, the program is ill-formed.
  567. inline BOOST_MOVE_DOC1ST(element_type&, typename bmupmu::add_lvalue_reference<element_type>::type)
  568. operator[](std::size_t i) const BOOST_NOEXCEPT
  569. {
  570. assert( bmupmu::extent<T>::value == 0 || i < bmupmu::extent<T>::value );
  571. assert(m_data.m_p);
  572. return m_data.m_p[i];
  573. }
  574. //! <b>Requires</b>: <tt>get() != nullptr</tt>.
  575. //!
  576. //! <b>Returns</b>: <tt>get()</tt>.
  577. //!
  578. //! <b>Note</b>: use typically requires that T be a complete type.
  579. //!
  580. //! <b>Remarks</b: If T is an array type, the program is ill-formed.
  581. inline pointer operator->() const BOOST_NOEXCEPT
  582. {
  583. BOOST_MOVE_STATIC_ASSERT((!bmupmu::is_array<T>::value));
  584. assert(m_data.m_p);
  585. return m_data.m_p;
  586. }
  587. //! <b>Returns</b>: The stored pointer.
  588. //!
  589. inline pointer get() const BOOST_NOEXCEPT
  590. { return m_data.m_p; }
  591. //! <b>Returns</b>: A reference to the stored deleter.
  592. //!
  593. inline BOOST_MOVE_DOC1ST(D&, typename bmupmu::add_lvalue_reference<D>::type)
  594. get_deleter() BOOST_NOEXCEPT
  595. { return m_data.deleter(); }
  596. //! <b>Returns</b>: A reference to the stored deleter.
  597. //!
  598. inline BOOST_MOVE_DOC1ST(const D&, typename bmupmu::add_const_lvalue_reference<D>::type)
  599. get_deleter() const BOOST_NOEXCEPT
  600. { return m_data.deleter(); }
  601. #ifdef BOOST_MOVE_DOXYGEN_INVOKED
  602. //! <b>Returns</b>: Returns: get() != nullptr.
  603. //!
  604. inline explicit operator bool
  605. #else
  606. inline operator bmupd::explicit_bool_arg
  607. #endif
  608. ()const BOOST_NOEXCEPT
  609. {
  610. return m_data.m_p
  611. ? &bmupd::bool_conversion::for_bool
  612. : bmupd::explicit_bool_arg(0);
  613. }
  614. //! <b>Postcondition</b>: <tt>get() == nullptr</tt>.
  615. //!
  616. //! <b>Returns</b>: The value <tt>get()</tt> had at the start of the call to release.
  617. inline pointer release() BOOST_NOEXCEPT
  618. {
  619. const pointer tmp = m_data.m_p;
  620. m_data.m_p = pointer();
  621. return tmp;
  622. }
  623. //! <b>Requires</b>: The expression <tt>get_deleter()(get())</tt> shall be well formed, shall have well-defined behavior,
  624. //! and shall not throw exceptions.
  625. //!
  626. //! <b>Effects</b>: assigns p to the stored pointer, and then if the old value of the stored pointer, old_p, was not
  627. //! equal to nullptr, calls <tt>get_deleter()(old_p)</tt>. Note: The order of these operations is significant
  628. //! because the call to <tt>get_deleter()</tt> may destroy *this.
  629. //!
  630. //! <b>Postconditions</b>: <tt>get() == p</tt>. Note: The postcondition does not hold if the call to <tt>get_deleter()</tt>
  631. //! destroys *this since <tt>this->get()</tt> is no longer a valid expression.
  632. //!
  633. //! <b>Remarks</b>: This constructor shall not participate in overload resolution unless:
  634. //! - If T is not an array type and Pointer is implicitly convertible to pointer.
  635. //! - If T is an array type and Pointer is a more CV qualified pointer to element_type.
  636. template<class Pointer>
  637. BOOST_MOVE_DOC1ST(void, typename bmupd::enable_up_ptr<T BOOST_MOVE_I Pointer BOOST_MOVE_I pointer BOOST_MOVE_I void>::type)
  638. reset(Pointer p) BOOST_NOEXCEPT
  639. {
  640. //If T is not an array type, element_type_t<Pointer> derives from T
  641. //it uses the default deleter and T has no virtual destructor, then you have a problem
  642. BOOST_MOVE_STATIC_ASSERT(( !bmupd::missing_virtual_destructor
  643. <D, typename bmupd::get_element_type<Pointer>::type>::value ));
  644. pointer tmp = m_data.m_p;
  645. m_data.m_p = p;
  646. if(tmp) m_data.deleter()(tmp);
  647. }
  648. //! <b>Requires</b>: The expression <tt>get_deleter()(get())</tt> shall be well formed, shall have well-defined behavior,
  649. //! and shall not throw exceptions.
  650. //!
  651. //! <b>Effects</b>: assigns nullptr to the stored pointer, and then if the old value of the stored pointer, old_p, was not
  652. //! equal to nullptr, calls <tt>get_deleter()(old_p)</tt>. Note: The order of these operations is significant
  653. //! because the call to <tt>get_deleter()</tt> may destroy *this.
  654. //!
  655. //! <b>Postconditions</b>: <tt>get() == p</tt>. Note: The postcondition does not hold if the call to <tt>get_deleter()</tt>
  656. //! destroys *this since <tt>this->get()</tt> is no longer a valid expression.
  657. void reset() BOOST_NOEXCEPT
  658. { this->reset(pointer()); }
  659. //! <b>Effects</b>: Same as <tt>reset()</tt>
  660. //!
  661. void reset(BOOST_MOVE_DOC0PTR(bmupd::nullptr_type)) BOOST_NOEXCEPT
  662. { this->reset(); }
  663. //! <b>Requires</b>: <tt>get_deleter()</tt> shall be swappable and shall not throw an exception under swap.
  664. //!
  665. //! <b>Effects</b>: Invokes swap on the stored pointers and on the stored deleters of *this and u.
  666. void swap(unique_ptr& u) BOOST_NOEXCEPT
  667. {
  668. ::boost::adl_move_swap(m_data.m_p, u.m_data.m_p);
  669. ::boost::adl_move_swap(m_data.deleter(), u.m_data.deleter());
  670. }
  671. };
  672. //! <b>Effects</b>: Calls <tt>x.swap(y)</tt>.
  673. //!
  674. template <class T, class D>
  675. inline void swap(unique_ptr<T, D> &x, unique_ptr<T, D> &y) BOOST_NOEXCEPT
  676. { x.swap(y); }
  677. //! <b>Returns</b>: <tt>x.get() == y.get()</tt>.
  678. //!
  679. template <class T1, class D1, class T2, class D2>
  680. inline bool operator==(const unique_ptr<T1, D1> &x, const unique_ptr<T2, D2> &y)
  681. { return x.get() == y.get(); }
  682. //! <b>Returns</b>: <tt>x.get() != y.get()</tt>.
  683. //!
  684. template <class T1, class D1, class T2, class D2>
  685. inline bool operator!=(const unique_ptr<T1, D1> &x, const unique_ptr<T2, D2> &y)
  686. { return x.get() != y.get(); }
  687. //! <b>Returns</b>: x.get() < y.get().
  688. //!
  689. //! <b>Remarks</b>: This comparison shall induce a
  690. //! strict weak ordering betwen pointers.
  691. template <class T1, class D1, class T2, class D2>
  692. inline bool operator<(const unique_ptr<T1, D1> &x, const unique_ptr<T2, D2> &y)
  693. { return x.get() < y.get(); }
  694. //! <b>Returns</b>: !(y < x).
  695. //!
  696. template <class T1, class D1, class T2, class D2>
  697. inline bool operator<=(const unique_ptr<T1, D1> &x, const unique_ptr<T2, D2> &y)
  698. { return !(y < x); }
  699. //! <b>Returns</b>: y < x.
  700. //!
  701. template <class T1, class D1, class T2, class D2>
  702. inline bool operator>(const unique_ptr<T1, D1> &x, const unique_ptr<T2, D2> &y)
  703. { return y < x; }
  704. //! <b>Returns</b>:!(x < y).
  705. //!
  706. template <class T1, class D1, class T2, class D2>
  707. inline bool operator>=(const unique_ptr<T1, D1> &x, const unique_ptr<T2, D2> &y)
  708. { return !(x < y); }
  709. //! <b>Returns</b>:!x.
  710. //!
  711. template <class T, class D>
  712. inline bool operator==(const unique_ptr<T, D> &x, BOOST_MOVE_DOC0PTR(bmupd::nullptr_type)) BOOST_NOEXCEPT
  713. { return !x; }
  714. //! <b>Returns</b>:!x.
  715. //!
  716. template <class T, class D>
  717. inline bool operator==(BOOST_MOVE_DOC0PTR(bmupd::nullptr_type), const unique_ptr<T, D> &x) BOOST_NOEXCEPT
  718. { return !x; }
  719. //! <b>Returns</b>: (bool)x.
  720. //!
  721. template <class T, class D>
  722. inline bool operator!=(const unique_ptr<T, D> &x, BOOST_MOVE_DOC0PTR(bmupd::nullptr_type)) BOOST_NOEXCEPT
  723. { return !!x; }
  724. //! <b>Returns</b>: (bool)x.
  725. //!
  726. template <class T, class D>
  727. inline bool operator!=(BOOST_MOVE_DOC0PTR(bmupd::nullptr_type), const unique_ptr<T, D> &x) BOOST_NOEXCEPT
  728. { return !!x; }
  729. //! <b>Requires</b>: <tt>operator </tt> shall induce a strict weak ordering on unique_ptr<T, D>::pointer values.
  730. //!
  731. //! <b>Returns</b>: Returns <tt>x.get() < pointer()</tt>.
  732. template <class T, class D>
  733. inline bool operator<(const unique_ptr<T, D> &x, BOOST_MOVE_DOC0PTR(bmupd::nullptr_type))
  734. { return x.get() < typename unique_ptr<T, D>::pointer(); }
  735. //! <b>Requires</b>: <tt>operator </tt> shall induce a strict weak ordering on unique_ptr<T, D>::pointer values.
  736. //!
  737. //! <b>Returns</b>: Returns <tt>pointer() < x.get()</tt>.
  738. template <class T, class D>
  739. inline bool operator<(BOOST_MOVE_DOC0PTR(bmupd::nullptr_type), const unique_ptr<T, D> &x)
  740. { return typename unique_ptr<T, D>::pointer() < x.get(); }
  741. //! <b>Returns</b>: <tt>nullptr < x</tt>.
  742. //!
  743. template <class T, class D>
  744. inline bool operator>(const unique_ptr<T, D> &x, BOOST_MOVE_DOC0PTR(bmupd::nullptr_type))
  745. { return x.get() > typename unique_ptr<T, D>::pointer(); }
  746. //! <b>Returns</b>: <tt>x < nullptr</tt>.
  747. //!
  748. template <class T, class D>
  749. inline bool operator>(BOOST_MOVE_DOC0PTR(bmupd::nullptr_type), const unique_ptr<T, D> &x)
  750. { return typename unique_ptr<T, D>::pointer() > x.get(); }
  751. //! <b>Returns</b>: <tt>!(nullptr < x)</tt>.
  752. //!
  753. template <class T, class D>
  754. inline bool operator<=(const unique_ptr<T, D> &x, BOOST_MOVE_DOC0PTR(bmupd::nullptr_type))
  755. { return !(bmupd::nullptr_type() < x); }
  756. //! <b>Returns</b>: <tt>!(x < nullptr)</tt>.
  757. //!
  758. template <class T, class D>
  759. inline bool operator<=(BOOST_MOVE_DOC0PTR(bmupd::nullptr_type), const unique_ptr<T, D> &x)
  760. { return !(x < bmupd::nullptr_type()); }
  761. //! <b>Returns</b>: <tt>!(x < nullptr)</tt>.
  762. //!
  763. template <class T, class D>
  764. inline bool operator>=(const unique_ptr<T, D> &x, BOOST_MOVE_DOC0PTR(bmupd::nullptr_type))
  765. { return !(x < bmupd::nullptr_type()); }
  766. //! <b>Returns</b>: <tt>!(nullptr < x)</tt>.
  767. //!
  768. template <class T, class D>
  769. inline bool operator>=(BOOST_MOVE_DOC0PTR(bmupd::nullptr_type), const unique_ptr<T, D> &x)
  770. { return !(bmupd::nullptr_type() < x); }
  771. } //namespace movelib {
  772. } //namespace boost{
  773. #include <boost/move/detail/config_end.hpp>
  774. #endif //#ifndef BOOST_MOVE_UNIQUE_PTR_HPP_INCLUDED