make_unique.hpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. reinterpret_cast<std::nothrow_t *>(0x1234); //Avoid reference to null errors in sanitizers
  66. } //namespace move_upmu {
  67. } //namespace boost{
  68. #endif //!defined(BOOST_MOVE_DOXYGEN_INVOKED)
  69. namespace boost{
  70. namespace movelib {
  71. #if defined(BOOST_MOVE_DOXYGEN_INVOKED) || !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  72. //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is not an array.
  73. //!
  74. //! <b>Returns</b>: <tt>unique_ptr<T>(new T(std::forward<Args>(args)...))</tt>.
  75. template<class T, class... Args>
  76. inline BOOST_MOVE_DOC1ST(unique_ptr<T>,
  77. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_not_array)
  78. make_unique(BOOST_FWD_REF(Args)... args)
  79. { return unique_ptr<T>(new T(::boost::forward<Args>(args)...)); }
  80. //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is not an array.
  81. //!
  82. //! <b>Returns</b>: <tt>unique_ptr<T>(new T(std::nothrow)(std::forward<Args>(args)...))</tt>.
  83. template<class T, class... Args>
  84. inline BOOST_MOVE_DOC1ST(unique_ptr<T>,
  85. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_not_array)
  86. make_unique_nothrow(BOOST_FWD_REF(Args)... args)
  87. { return unique_ptr<T>(new (*boost::move_upmu::nothrow_holder<>::pnothrow)T(::boost::forward<Args>(args)...)); }
  88. #else
  89. #define BOOST_MOVE_MAKE_UNIQUE_CODE(N)\
  90. template<class T BOOST_MOVE_I##N BOOST_MOVE_CLASS##N>\
  91. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_not_array\
  92. make_unique( BOOST_MOVE_UREF##N)\
  93. { return unique_ptr<T>( new T( BOOST_MOVE_FWD##N ) ); }\
  94. \
  95. template<class T BOOST_MOVE_I##N BOOST_MOVE_CLASS##N>\
  96. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_not_array\
  97. make_unique_nothrow( BOOST_MOVE_UREF##N)\
  98. { return unique_ptr<T>( new (*boost::move_upmu::nothrow_holder<>::pnothrow)T ( BOOST_MOVE_FWD##N ) ); }\
  99. //
  100. BOOST_MOVE_ITERATE_0TO9(BOOST_MOVE_MAKE_UNIQUE_CODE)
  101. #undef BOOST_MOVE_MAKE_UNIQUE_CODE
  102. #endif
  103. //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is not an array.
  104. //!
  105. //! <b>Returns</b>: <tt>unique_ptr<T>(new T)</tt> (default initialization)
  106. template<class T>
  107. inline BOOST_MOVE_DOC1ST(unique_ptr<T>,
  108. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_not_array)
  109. make_unique_definit()
  110. {
  111. return unique_ptr<T>(new T);
  112. }
  113. //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is not an array.
  114. //!
  115. //! <b>Returns</b>: <tt>unique_ptr<T>(new T(std::nothrow)</tt> (default initialization)
  116. template<class T>
  117. inline BOOST_MOVE_DOC1ST(unique_ptr<T>,
  118. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_not_array)
  119. make_unique_nothrow_definit()
  120. {
  121. return unique_ptr<T>(new (*boost::move_upmu::nothrow_holder<>::pnothrow)T);
  122. }
  123. //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is an array of
  124. //! unknown bound.
  125. //!
  126. //! <b>Returns</b>: <tt>unique_ptr<T>(new remove_extent_t<T>[n]())</tt> (value initialization)
  127. template<class T>
  128. inline BOOST_MOVE_DOC1ST(unique_ptr<T>,
  129. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_array_of_unknown_bound)
  130. make_unique(std::size_t n)
  131. {
  132. typedef typename ::boost::move_upmu::remove_extent<T>::type U;
  133. return unique_ptr<T>(new U[n]());
  134. }
  135. //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is an array of
  136. //! unknown bound.
  137. //!
  138. //! <b>Returns</b>: <tt>unique_ptr<T>(new (std::nothrow)remove_extent_t<T>[n]())</tt> (value initialization)
  139. template<class T>
  140. inline BOOST_MOVE_DOC1ST(unique_ptr<T>,
  141. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_array_of_unknown_bound)
  142. make_unique_nothrow(std::size_t n)
  143. {
  144. typedef typename ::boost::move_upmu::remove_extent<T>::type U;
  145. return unique_ptr<T>(new (*boost::move_upmu::nothrow_holder<>::pnothrow)U[n]());
  146. }
  147. //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is an array of
  148. //! unknown bound.
  149. //!
  150. //! <b>Returns</b>: <tt>unique_ptr<T>(new remove_extent_t<T>[n])</tt> (default initialization)
  151. template<class T>
  152. inline BOOST_MOVE_DOC1ST(unique_ptr<T>,
  153. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_array_of_unknown_bound)
  154. make_unique_definit(std::size_t n)
  155. {
  156. typedef typename ::boost::move_upmu::remove_extent<T>::type U;
  157. return unique_ptr<T>(new U[n]);
  158. }
  159. //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is an array of
  160. //! unknown bound.
  161. //!
  162. //! <b>Returns</b>: <tt>unique_ptr<T>(new (std::nothrow)remove_extent_t<T>[n])</tt> (default initialization)
  163. template<class T>
  164. inline BOOST_MOVE_DOC1ST(unique_ptr<T>,
  165. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_array_of_unknown_bound)
  166. make_unique_nothrow_definit(std::size_t n)
  167. {
  168. typedef typename ::boost::move_upmu::remove_extent<T>::type U;
  169. return unique_ptr<T>(new (*boost::move_upmu::nothrow_holder<>::pnothrow) U[n]);
  170. }
  171. #if !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS)
  172. //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is
  173. //! an array of known bound.
  174. template<class T, class... Args>
  175. inline BOOST_MOVE_DOC1ST(unspecified,
  176. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_array_of_known_bound)
  177. make_unique(BOOST_FWD_REF(Args) ...) = delete;
  178. //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is
  179. //! an array of known bound.
  180. template<class T, class... Args>
  181. inline BOOST_MOVE_DOC1ST(unspecified,
  182. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_array_of_known_bound)
  183. make_unique_definit(BOOST_FWD_REF(Args) ...) = delete;
  184. //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is
  185. //! an array of known bound.
  186. template<class T, class... Args>
  187. inline BOOST_MOVE_DOC1ST(unspecified,
  188. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_array_of_known_bound)
  189. make_unique_nothrow(BOOST_FWD_REF(Args) ...) = delete;
  190. //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is
  191. //! an array of known bound.
  192. template<class T, class... Args>
  193. inline BOOST_MOVE_DOC1ST(unspecified,
  194. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_array_of_known_bound)
  195. make_unique_nothrow_definit(BOOST_FWD_REF(Args) ...) = delete;
  196. #endif
  197. } //namespace movelib {
  198. } //namespace boost{
  199. #include <boost/move/detail/config_end.hpp>
  200. #endif //#ifndef BOOST_MOVE_MAKE_UNIQUE_HPP_INCLUDED