make_unique.hpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. #if defined(_MSC_VER) && (_MSC_VER >= 1915)
  36. #pragma warning (push)
  37. #pragma warning (disable : 4643) // Forward declaring 'X' in namespace std is not permitted by the C++ Standard
  38. #endif
  39. namespace std { //no namespace versioning in clang+libc++
  40. struct nothrow_t;
  41. } //namespace std {
  42. #if defined(_MSC_VER) && (_MSC_VER >= 1915)
  43. #pragma warning (pop)
  44. #endif
  45. namespace boost{
  46. namespace move_upmu {
  47. //Compile time switch between
  48. //single element, unknown bound array
  49. //and known bound array
  50. template<class T>
  51. struct unique_ptr_if
  52. {
  53. typedef ::boost::movelib::unique_ptr<T> t_is_not_array;
  54. };
  55. template<class T>
  56. struct unique_ptr_if<T[]>
  57. {
  58. typedef ::boost::movelib::unique_ptr<T[]> t_is_array_of_unknown_bound;
  59. };
  60. template<class T, std::size_t N>
  61. struct unique_ptr_if<T[N]>
  62. {
  63. typedef void t_is_array_of_known_bound;
  64. };
  65. template <int Dummy = 0>
  66. struct nothrow_holder
  67. {
  68. static std::nothrow_t *pnothrow;
  69. };
  70. template <int Dummy>
  71. std::nothrow_t *nothrow_holder<Dummy>::pnothrow =
  72. reinterpret_cast<std::nothrow_t *>(0x1234); //Avoid reference to null errors in sanitizers
  73. } //namespace move_upmu {
  74. } //namespace boost{
  75. #endif //!defined(BOOST_MOVE_DOXYGEN_INVOKED)
  76. namespace boost{
  77. namespace movelib {
  78. #if defined(BOOST_MOVE_DOXYGEN_INVOKED) || !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  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::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(BOOST_FWD_REF(Args)... args)
  86. { return unique_ptr<T>(new T(::boost::forward<Args>(args)...)); }
  87. //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is not an array.
  88. //!
  89. //! <b>Returns</b>: <tt>unique_ptr<T>(new T(std::nothrow)(std::forward<Args>(args)...))</tt>.
  90. template<class T, class... Args>
  91. inline BOOST_MOVE_DOC1ST(unique_ptr<T>,
  92. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_not_array)
  93. make_unique_nothrow(BOOST_FWD_REF(Args)... args)
  94. { return unique_ptr<T>(new (*boost::move_upmu::nothrow_holder<>::pnothrow)T(::boost::forward<Args>(args)...)); }
  95. #else
  96. #define BOOST_MOVE_MAKE_UNIQUE_CODE(N)\
  97. template<class T BOOST_MOVE_I##N BOOST_MOVE_CLASS##N>\
  98. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_not_array\
  99. make_unique( BOOST_MOVE_UREF##N)\
  100. { return unique_ptr<T>( new T( BOOST_MOVE_FWD##N ) ); }\
  101. \
  102. template<class T BOOST_MOVE_I##N BOOST_MOVE_CLASS##N>\
  103. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_not_array\
  104. make_unique_nothrow( BOOST_MOVE_UREF##N)\
  105. { return unique_ptr<T>( new (*boost::move_upmu::nothrow_holder<>::pnothrow)T ( BOOST_MOVE_FWD##N ) ); }\
  106. //
  107. BOOST_MOVE_ITERATE_0TO9(BOOST_MOVE_MAKE_UNIQUE_CODE)
  108. #undef BOOST_MOVE_MAKE_UNIQUE_CODE
  109. #endif
  110. //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is not an array.
  111. //!
  112. //! <b>Returns</b>: <tt>unique_ptr<T>(new T)</tt> (default initialization)
  113. template<class T>
  114. inline BOOST_MOVE_DOC1ST(unique_ptr<T>,
  115. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_not_array)
  116. make_unique_definit()
  117. {
  118. return unique_ptr<T>(new T);
  119. }
  120. //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is not an array.
  121. //!
  122. //! <b>Returns</b>: <tt>unique_ptr<T>(new T(std::nothrow)</tt> (default initialization)
  123. template<class T>
  124. inline BOOST_MOVE_DOC1ST(unique_ptr<T>,
  125. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_not_array)
  126. make_unique_nothrow_definit()
  127. {
  128. return unique_ptr<T>(new (*boost::move_upmu::nothrow_holder<>::pnothrow)T);
  129. }
  130. //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is an array of
  131. //! unknown bound.
  132. //!
  133. //! <b>Returns</b>: <tt>unique_ptr<T>(new remove_extent_t<T>[n]())</tt> (value initialization)
  134. template<class T>
  135. inline BOOST_MOVE_DOC1ST(unique_ptr<T>,
  136. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_array_of_unknown_bound)
  137. make_unique(std::size_t n)
  138. {
  139. typedef typename ::boost::move_upmu::remove_extent<T>::type U;
  140. return unique_ptr<T>(new U[n]());
  141. }
  142. //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is an array of
  143. //! unknown bound.
  144. //!
  145. //! <b>Returns</b>: <tt>unique_ptr<T>(new (std::nothrow)remove_extent_t<T>[n]())</tt> (value initialization)
  146. template<class T>
  147. inline BOOST_MOVE_DOC1ST(unique_ptr<T>,
  148. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_array_of_unknown_bound)
  149. make_unique_nothrow(std::size_t n)
  150. {
  151. typedef typename ::boost::move_upmu::remove_extent<T>::type U;
  152. return unique_ptr<T>(new (*boost::move_upmu::nothrow_holder<>::pnothrow)U[n]());
  153. }
  154. //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is an array of
  155. //! unknown bound.
  156. //!
  157. //! <b>Returns</b>: <tt>unique_ptr<T>(new remove_extent_t<T>[n])</tt> (default initialization)
  158. template<class T>
  159. inline BOOST_MOVE_DOC1ST(unique_ptr<T>,
  160. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_array_of_unknown_bound)
  161. make_unique_definit(std::size_t n)
  162. {
  163. typedef typename ::boost::move_upmu::remove_extent<T>::type U;
  164. return unique_ptr<T>(new U[n]);
  165. }
  166. //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is an array of
  167. //! unknown bound.
  168. //!
  169. //! <b>Returns</b>: <tt>unique_ptr<T>(new (std::nothrow)remove_extent_t<T>[n])</tt> (default initialization)
  170. template<class T>
  171. inline BOOST_MOVE_DOC1ST(unique_ptr<T>,
  172. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_array_of_unknown_bound)
  173. make_unique_nothrow_definit(std::size_t n)
  174. {
  175. typedef typename ::boost::move_upmu::remove_extent<T>::type U;
  176. return unique_ptr<T>(new (*boost::move_upmu::nothrow_holder<>::pnothrow) U[n]);
  177. }
  178. #if !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS)
  179. //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is
  180. //! an array of known bound.
  181. template<class T, class... Args>
  182. inline BOOST_MOVE_DOC1ST(unspecified,
  183. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_array_of_known_bound)
  184. make_unique(BOOST_FWD_REF(Args) ...) = delete;
  185. //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is
  186. //! an array of known bound.
  187. template<class T, class... Args>
  188. inline BOOST_MOVE_DOC1ST(unspecified,
  189. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_array_of_known_bound)
  190. make_unique_definit(BOOST_FWD_REF(Args) ...) = delete;
  191. //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is
  192. //! an array of known bound.
  193. template<class T, class... Args>
  194. inline BOOST_MOVE_DOC1ST(unspecified,
  195. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_array_of_known_bound)
  196. make_unique_nothrow(BOOST_FWD_REF(Args) ...) = delete;
  197. //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is
  198. //! an array of known bound.
  199. template<class T, class... Args>
  200. inline BOOST_MOVE_DOC1ST(unspecified,
  201. typename ::boost::move_upmu::unique_ptr_if<T>::t_is_array_of_known_bound)
  202. make_unique_nothrow_definit(BOOST_FWD_REF(Args) ...) = delete;
  203. #endif
  204. } //namespace movelib {
  205. } //namespace boost{
  206. #include <boost/move/detail/config_end.hpp>
  207. #endif //#ifndef BOOST_MOVE_MAKE_UNIQUE_HPP_INCLUDED