make_unique.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2006-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_MAKE_UNIQUE_HPP_INCLUDED
  11. #define BOOST_MOVE_MAKE_UNIQUE_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>
  21. #include <boost/move/utility_core.hpp>
  22. #include <boost/move/unique_ptr.hpp>
  23. #include <cstddef> //for std::size_t
  24. #include <boost/move/detail/unique_ptr_meta_utils.hpp>
  25. #ifdef BOOST_NO_CXX11_VARIADIC_TEMPLATES
  26. # include <boost/move/detail/fwd_macros.hpp>
  27. #endif
  28. //!\file
  29. //! Defines "make_unique" functions, which are factories to create instances
  30. //! of unique_ptr depending on the passed arguments.
  31. //!
  32. //! This header can be a bit heavyweight in C++03 compilers due to the use of the
  33. //! preprocessor library, that's why it's a a separate header from <tt>unique_ptr.hpp</tt>
  34. #if !defined(BOOST_MOVE_DOXYGEN_INVOKED)
  35. namespace std { //no namespace versioning in clang+libc++
  36. struct nothrow_t;
  37. } //namespace std {
  38. namespace boost{
  39. namespace move_upmu {
  40. //Compile time switch between
  41. //single element, unknown bound array
  42. //and known bound array
  43. template<class T>
  44. struct unique_ptr_if
  45. {
  46. typedef ::boost::movelib::unique_ptr<T> t_is_not_array;
  47. };
  48. template<class T>
  49. struct unique_ptr_if<T[]>
  50. {
  51. typedef ::boost::movelib::unique_ptr<T[]> t_is_array_of_unknown_bound;
  52. };
  53. template<class T, std::size_t N>
  54. struct unique_ptr_if<T[N]>
  55. {
  56. typedef void t_is_array_of_known_bound;
  57. };
  58. template <int Dummy = 0>
  59. struct nothrow_holder
  60. {
  61. static std::nothrow_t *pnothrow;
  62. };
  63. template <int Dummy>
  64. std::nothrow_t *nothrow_holder<Dummy>::pnothrow;
  65. } //namespace move_upmu {
  66. } //namespace boost{
  67. #endif //!defined(BOOST_MOVE_DOXYGEN_INVOKED)
  68. namespace boost{
  69. namespace movelib {
  70. #if defined(BOOST_MOVE_DOXYGEN_INVOKED) || !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  71. //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is not an array.
  72. //!
  73. //! <b>Returns</b>: <tt>unique_ptr<T>(new T(std::forward<Args>(args)...))</tt>.
  74. template<class T, class... Args>
  75. inline BOOST_MOVE_DOC1ST(unique_ptr<T>,
  76. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_not_array)
  77. make_unique(BOOST_FWD_REF(Args)... args)
  78. { return unique_ptr<T>(new T(::boost::forward<Args>(args)...)); }
  79. //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is not an array.
  80. //!
  81. //! <b>Returns</b>: <tt>unique_ptr<T>(new T(std::nothrow)(std::forward<Args>(args)...))</tt>.
  82. template<class T, class... Args>
  83. inline BOOST_MOVE_DOC1ST(unique_ptr<T>,
  84. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_not_array)
  85. make_unique_nothrow(BOOST_FWD_REF(Args)... args)
  86. { return unique_ptr<T>(new (*boost::move_upmu::nothrow_holder<>::pnothrow)T(::boost::forward<Args>(args)...)); }
  87. #else
  88. #define BOOST_MOVE_MAKE_UNIQUE_CODE(N)\
  89. template<class T BOOST_MOVE_I##N BOOST_MOVE_CLASS##N>\
  90. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_not_array\
  91. make_unique( BOOST_MOVE_UREF##N)\
  92. { return unique_ptr<T>( new T( BOOST_MOVE_FWD##N ) ); }\
  93. \
  94. template<class T BOOST_MOVE_I##N BOOST_MOVE_CLASS##N>\
  95. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_not_array\
  96. make_unique_nothrow( BOOST_MOVE_UREF##N)\
  97. { return unique_ptr<T>( new (*boost::move_upmu::nothrow_holder<>::pnothrow)T ( BOOST_MOVE_FWD##N ) ); }\
  98. //
  99. BOOST_MOVE_ITERATE_0TO9(BOOST_MOVE_MAKE_UNIQUE_CODE)
  100. #undef BOOST_MOVE_MAKE_UNIQUE_CODE
  101. #endif
  102. //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is not an array.
  103. //!
  104. //! <b>Returns</b>: <tt>unique_ptr<T>(new T)</tt> (default initialization)
  105. template<class T>
  106. inline BOOST_MOVE_DOC1ST(unique_ptr<T>,
  107. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_not_array)
  108. make_unique_definit()
  109. {
  110. return unique_ptr<T>(new T);
  111. }
  112. //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is not an array.
  113. //!
  114. //! <b>Returns</b>: <tt>unique_ptr<T>(new T(std::nothrow)</tt> (default initialization)
  115. template<class T>
  116. inline BOOST_MOVE_DOC1ST(unique_ptr<T>,
  117. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_not_array)
  118. make_unique_nothrow_definit()
  119. {
  120. return unique_ptr<T>(new (*boost::move_upmu::nothrow_holder<>::pnothrow)T);
  121. }
  122. //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is an array of
  123. //! unknown bound.
  124. //!
  125. //! <b>Returns</b>: <tt>unique_ptr<T>(new remove_extent_t<T>[n]())</tt> (value initialization)
  126. template<class T>
  127. inline BOOST_MOVE_DOC1ST(unique_ptr<T>,
  128. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_array_of_unknown_bound)
  129. make_unique(std::size_t n)
  130. {
  131. typedef typename ::boost::move_upmu::remove_extent<T>::type U;
  132. return unique_ptr<T>(new U[n]());
  133. }
  134. //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is an array of
  135. //! unknown bound.
  136. //!
  137. //! <b>Returns</b>: <tt>unique_ptr<T>(new (std::nothrow)remove_extent_t<T>[n]())</tt> (value initialization)
  138. template<class T>
  139. inline BOOST_MOVE_DOC1ST(unique_ptr<T>,
  140. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_array_of_unknown_bound)
  141. make_unique_nothrow(std::size_t n)
  142. {
  143. typedef typename ::boost::move_upmu::remove_extent<T>::type U;
  144. return unique_ptr<T>(new (*boost::move_upmu::nothrow_holder<>::pnothrow)U[n]());
  145. }
  146. //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is an array of
  147. //! unknown bound.
  148. //!
  149. //! <b>Returns</b>: <tt>unique_ptr<T>(new remove_extent_t<T>[n])</tt> (default initialization)
  150. template<class T>
  151. inline BOOST_MOVE_DOC1ST(unique_ptr<T>,
  152. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_array_of_unknown_bound)
  153. make_unique_definit(std::size_t n)
  154. {
  155. typedef typename ::boost::move_upmu::remove_extent<T>::type U;
  156. return unique_ptr<T>(new U[n]);
  157. }
  158. //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is an array of
  159. //! unknown bound.
  160. //!
  161. //! <b>Returns</b>: <tt>unique_ptr<T>(new (std::nothrow)remove_extent_t<T>[n])</tt> (default initialization)
  162. template<class T>
  163. inline BOOST_MOVE_DOC1ST(unique_ptr<T>,
  164. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_array_of_unknown_bound)
  165. make_unique_nothrow_definit(std::size_t n)
  166. {
  167. typedef typename ::boost::move_upmu::remove_extent<T>::type U;
  168. return unique_ptr<T>(new (*boost::move_upmu::nothrow_holder<>::pnothrow) U[n]);
  169. }
  170. #if !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS)
  171. //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is
  172. //! an array of known bound.
  173. template<class T, class... Args>
  174. inline BOOST_MOVE_DOC1ST(unspecified,
  175. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_array_of_known_bound)
  176. make_unique(BOOST_FWD_REF(Args) ...) = delete;
  177. //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is
  178. //! an array of known bound.
  179. template<class T, class... Args>
  180. inline BOOST_MOVE_DOC1ST(unspecified,
  181. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_array_of_known_bound)
  182. make_unique_definit(BOOST_FWD_REF(Args) ...) = delete;
  183. //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is
  184. //! an array of known bound.
  185. template<class T, class... Args>
  186. inline BOOST_MOVE_DOC1ST(unspecified,
  187. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_array_of_known_bound)
  188. make_unique_nothrow(BOOST_FWD_REF(Args) ...) = delete;
  189. //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is
  190. //! an array of known bound.
  191. template<class T, class... Args>
  192. inline BOOST_MOVE_DOC1ST(unspecified,
  193. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_array_of_known_bound)
  194. make_unique_nothrow_definit(BOOST_FWD_REF(Args) ...) = delete;
  195. #endif
  196. } //namespace movelib {
  197. } //namespace boost{
  198. #include <boost/move/detail/config_end.hpp>
  199. #endif //#ifndef BOOST_MOVE_MAKE_UNIQUE_HPP_INCLUDED