move.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // Distributed under the Boost Software License, Version 1.0. (See
  2. // accompanying file LICENSE_1_0.txt or copy at
  3. // http://www.boost.org/LICENSE_1_0.txt)
  4. // (C) Copyright 2007-8 Anthony Williams
  5. // (C) Copyright 2011-2012 Vicente J. Botet Escriba
  6. #ifndef BOOST_THREAD_MOVE_HPP
  7. #define BOOST_THREAD_MOVE_HPP
  8. #include <boost/thread/detail/config.hpp>
  9. #ifndef BOOST_NO_SFINAE
  10. #include <boost/core/enable_if.hpp>
  11. #include <boost/type_traits/is_convertible.hpp>
  12. #include <boost/type_traits/remove_reference.hpp>
  13. #include <boost/type_traits/remove_cv.hpp>
  14. #include <boost/type_traits/decay.hpp>
  15. #include <boost/type_traits/conditional.hpp>
  16. #include <boost/type_traits/remove_extent.hpp>
  17. #include <boost/type_traits/is_array.hpp>
  18. #include <boost/type_traits/is_function.hpp>
  19. #include <boost/type_traits/add_pointer.hpp>
  20. #endif
  21. #include <boost/thread/detail/delete.hpp>
  22. #include <boost/move/utility.hpp>
  23. #include <boost/move/traits.hpp>
  24. #include <boost/config/abi_prefix.hpp>
  25. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  26. #include <type_traits>
  27. #endif
  28. namespace boost
  29. {
  30. namespace detail
  31. {
  32. template <typename T>
  33. struct enable_move_utility_emulation_dummy_specialization;
  34. template<typename T>
  35. struct thread_move_t
  36. {
  37. T& t;
  38. explicit thread_move_t(T& t_):
  39. t(t_)
  40. {}
  41. T& operator*() const
  42. {
  43. return t;
  44. }
  45. T* operator->() const
  46. {
  47. return &t;
  48. }
  49. private:
  50. void operator=(thread_move_t&);
  51. };
  52. }
  53. #if !defined BOOST_THREAD_USES_MOVE
  54. #ifndef BOOST_NO_SFINAE
  55. template<typename T>
  56. typename enable_if<boost::is_convertible<T&,boost::detail::thread_move_t<T> >, boost::detail::thread_move_t<T> >::type move(T& t)
  57. {
  58. return boost::detail::thread_move_t<T>(t);
  59. }
  60. #endif
  61. template<typename T>
  62. boost::detail::thread_move_t<T> move(boost::detail::thread_move_t<T> t)
  63. {
  64. return t;
  65. }
  66. #endif //#if !defined BOOST_THREAD_USES_MOVE
  67. }
  68. #if ! defined BOOST_NO_CXX11_RVALUE_REFERENCES
  69. #define BOOST_THREAD_COPY_ASSIGN_REF(TYPE) BOOST_COPY_ASSIGN_REF(TYPE)
  70. #define BOOST_THREAD_RV_REF(TYPE) BOOST_RV_REF(TYPE)
  71. #define BOOST_THREAD_RV_REF_2_TEMPL_ARGS(TYPE) BOOST_RV_REF_2_TEMPL_ARGS(TYPE)
  72. #define BOOST_THREAD_RV_REF_BEG BOOST_RV_REF_BEG
  73. #define BOOST_THREAD_RV_REF_END BOOST_RV_REF_END
  74. #define BOOST_THREAD_RV(V) V
  75. #define BOOST_THREAD_MAKE_RV_REF(RVALUE) RVALUE
  76. #define BOOST_THREAD_FWD_REF(TYPE) BOOST_FWD_REF(TYPE)
  77. #define BOOST_THREAD_DCL_MOVABLE(TYPE)
  78. #define BOOST_THREAD_DCL_MOVABLE_BEG(T) \
  79. namespace detail { \
  80. template <typename T> \
  81. struct enable_move_utility_emulation_dummy_specialization<
  82. #define BOOST_THREAD_DCL_MOVABLE_BEG2(T1, T2) \
  83. namespace detail { \
  84. template <typename T1, typename T2> \
  85. struct enable_move_utility_emulation_dummy_specialization<
  86. #define BOOST_THREAD_DCL_MOVABLE_END > \
  87. : integral_constant<bool, false> \
  88. {}; \
  89. }
  90. #elif ! defined BOOST_NO_CXX11_RVALUE_REFERENCES && defined BOOST_MSVC
  91. #define BOOST_THREAD_COPY_ASSIGN_REF(TYPE) BOOST_COPY_ASSIGN_REF(TYPE)
  92. #define BOOST_THREAD_RV_REF(TYPE) BOOST_RV_REF(TYPE)
  93. #define BOOST_THREAD_RV_REF_2_TEMPL_ARGS(TYPE) BOOST_RV_REF_2_TEMPL_ARGS(TYPE)
  94. #define BOOST_THREAD_RV_REF_BEG BOOST_RV_REF_BEG
  95. #define BOOST_THREAD_RV_REF_END BOOST_RV_REF_END
  96. #define BOOST_THREAD_RV(V) V
  97. #define BOOST_THREAD_MAKE_RV_REF(RVALUE) RVALUE
  98. #define BOOST_THREAD_FWD_REF(TYPE) BOOST_FWD_REF(TYPE)
  99. #define BOOST_THREAD_DCL_MOVABLE(TYPE)
  100. #define BOOST_THREAD_DCL_MOVABLE_BEG(T) \
  101. namespace detail { \
  102. template <typename T> \
  103. struct enable_move_utility_emulation_dummy_specialization<
  104. #define BOOST_THREAD_DCL_MOVABLE_BEG2(T1, T2) \
  105. namespace detail { \
  106. template <typename T1, typename T2> \
  107. struct enable_move_utility_emulation_dummy_specialization<
  108. #define BOOST_THREAD_DCL_MOVABLE_END > \
  109. : integral_constant<bool, false> \
  110. {}; \
  111. }
  112. #else
  113. #if defined BOOST_THREAD_USES_MOVE
  114. #define BOOST_THREAD_COPY_ASSIGN_REF(TYPE) BOOST_COPY_ASSIGN_REF(TYPE)
  115. #define BOOST_THREAD_RV_REF(TYPE) BOOST_RV_REF(TYPE)
  116. #define BOOST_THREAD_RV_REF_2_TEMPL_ARGS(TYPE) BOOST_RV_REF_2_TEMPL_ARGS(TYPE)
  117. #define BOOST_THREAD_RV_REF_BEG BOOST_RV_REF_BEG
  118. #define BOOST_THREAD_RV_REF_END BOOST_RV_REF_END
  119. #define BOOST_THREAD_RV(V) V
  120. #define BOOST_THREAD_FWD_REF(TYPE) BOOST_FWD_REF(TYPE)
  121. #define BOOST_THREAD_DCL_MOVABLE(TYPE)
  122. #define BOOST_THREAD_DCL_MOVABLE_BEG(T) \
  123. namespace detail { \
  124. template <typename T> \
  125. struct enable_move_utility_emulation_dummy_specialization<
  126. #define BOOST_THREAD_DCL_MOVABLE_BEG2(T1, T2) \
  127. namespace detail { \
  128. template <typename T1, typename T2> \
  129. struct enable_move_utility_emulation_dummy_specialization<
  130. #define BOOST_THREAD_DCL_MOVABLE_END > \
  131. : integral_constant<bool, false> \
  132. {}; \
  133. }
  134. #else
  135. #define BOOST_THREAD_COPY_ASSIGN_REF(TYPE) const TYPE&
  136. #define BOOST_THREAD_RV_REF(TYPE) boost::detail::thread_move_t< TYPE >
  137. #define BOOST_THREAD_RV_REF_BEG boost::detail::thread_move_t<
  138. #define BOOST_THREAD_RV_REF_END >
  139. #define BOOST_THREAD_RV(V) (*V)
  140. #define BOOST_THREAD_FWD_REF(TYPE) BOOST_FWD_REF(TYPE)
  141. #define BOOST_THREAD_DCL_MOVABLE(TYPE) \
  142. template <> \
  143. struct enable_move_utility_emulation< TYPE > \
  144. { \
  145. static const bool value = false; \
  146. };
  147. #define BOOST_THREAD_DCL_MOVABLE_BEG(T) \
  148. template <typename T> \
  149. struct enable_move_utility_emulation<
  150. #define BOOST_THREAD_DCL_MOVABLE_BEG2(T1, T2) \
  151. template <typename T1, typename T2> \
  152. struct enable_move_utility_emulation<
  153. #define BOOST_THREAD_DCL_MOVABLE_END > \
  154. { \
  155. static const bool value = false; \
  156. };
  157. #endif
  158. namespace boost
  159. {
  160. namespace detail
  161. {
  162. template <typename T>
  163. BOOST_THREAD_RV_REF(typename ::boost::remove_cv<typename ::boost::remove_reference<T>::type>::type)
  164. make_rv_ref(T v) BOOST_NOEXCEPT
  165. {
  166. return (BOOST_THREAD_RV_REF(typename ::boost::remove_cv<typename ::boost::remove_reference<T>::type>::type))(v);
  167. }
  168. // template <typename T>
  169. // BOOST_THREAD_RV_REF(typename ::boost::remove_cv<typename ::boost::remove_reference<T>::type>::type)
  170. // make_rv_ref(T &v) BOOST_NOEXCEPT
  171. // {
  172. // return (BOOST_THREAD_RV_REF(typename ::boost::remove_cv<typename ::boost::remove_reference<T>::type>::type))(v);
  173. // }
  174. // template <typename T>
  175. // const BOOST_THREAD_RV_REF(typename ::boost::remove_cv<typename ::boost::remove_reference<T>::type>::type)
  176. // make_rv_ref(T const&v) BOOST_NOEXCEPT
  177. // {
  178. // return (const BOOST_THREAD_RV_REF(typename ::boost::remove_cv<typename ::boost::remove_reference<T>::type>::type))(v);
  179. // }
  180. }
  181. }
  182. #define BOOST_THREAD_MAKE_RV_REF(RVALUE) RVALUE.move()
  183. //#define BOOST_THREAD_MAKE_RV_REF(RVALUE) boost::detail::make_rv_ref(RVALUE)
  184. #endif
  185. #if ! defined BOOST_NO_CXX11_RVALUE_REFERENCES
  186. #define BOOST_THREAD_MOVABLE(TYPE)
  187. #define BOOST_THREAD_COPYABLE(TYPE)
  188. #else
  189. #if defined BOOST_THREAD_USES_MOVE
  190. #define BOOST_THREAD_MOVABLE(TYPE) \
  191. ::boost::rv<TYPE>& move() BOOST_NOEXCEPT \
  192. { \
  193. return *static_cast< ::boost::rv<TYPE>* >(this); \
  194. } \
  195. const ::boost::rv<TYPE>& move() const BOOST_NOEXCEPT \
  196. { \
  197. return *static_cast<const ::boost::rv<TYPE>* >(this); \
  198. } \
  199. operator ::boost::rv<TYPE>&() \
  200. { \
  201. return *static_cast< ::boost::rv<TYPE>* >(this); \
  202. } \
  203. operator const ::boost::rv<TYPE>&() const \
  204. { \
  205. return *static_cast<const ::boost::rv<TYPE>* >(this); \
  206. }\
  207. #define BOOST_THREAD_COPYABLE(TYPE) \
  208. TYPE& operator=(TYPE &t)\
  209. { this->operator=(static_cast<const ::boost::rv<TYPE> &>(const_cast<const TYPE &>(t))); return *this;}
  210. #else
  211. #define BOOST_THREAD_MOVABLE(TYPE) \
  212. operator ::boost::detail::thread_move_t<TYPE>() BOOST_NOEXCEPT \
  213. { \
  214. return move(); \
  215. } \
  216. ::boost::detail::thread_move_t<TYPE> move() BOOST_NOEXCEPT \
  217. { \
  218. ::boost::detail::thread_move_t<TYPE> x(*this); \
  219. return x; \
  220. } \
  221. #define BOOST_THREAD_COPYABLE(TYPE)
  222. #endif
  223. #endif
  224. #define BOOST_THREAD_MOVABLE_ONLY(TYPE) \
  225. BOOST_THREAD_NO_COPYABLE(TYPE) \
  226. BOOST_THREAD_MOVABLE(TYPE) \
  227. typedef int boost_move_no_copy_constructor_or_assign; \
  228. #define BOOST_THREAD_COPYABLE_AND_MOVABLE(TYPE) \
  229. BOOST_THREAD_COPYABLE(TYPE) \
  230. BOOST_THREAD_MOVABLE(TYPE) \
  231. namespace boost
  232. {
  233. namespace thread_detail
  234. {
  235. #if ! defined BOOST_NO_CXX11_RVALUE_REFERENCES
  236. #elif defined BOOST_THREAD_USES_MOVE
  237. template <class T>
  238. struct is_rv
  239. : ::boost::move_detail::is_rv<T>
  240. {};
  241. #else
  242. template <class T>
  243. struct is_rv
  244. : ::boost::integral_constant<bool, false>
  245. {};
  246. template <class T>
  247. struct is_rv< ::boost::detail::thread_move_t<T> >
  248. : ::boost::integral_constant<bool, true>
  249. {};
  250. template <class T>
  251. struct is_rv< const ::boost::detail::thread_move_t<T> >
  252. : ::boost::integral_constant<bool, true>
  253. {};
  254. #endif
  255. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  256. template <class Tp>
  257. struct remove_reference : boost::remove_reference<Tp> {};
  258. template <class Tp>
  259. struct decay : boost::decay<Tp> {};
  260. #else
  261. template <class Tp>
  262. struct remove_reference
  263. {
  264. typedef Tp type;
  265. };
  266. template <class Tp>
  267. struct remove_reference<Tp&>
  268. {
  269. typedef Tp type;
  270. };
  271. template <class Tp>
  272. struct remove_reference< rv<Tp> > {
  273. typedef Tp type;
  274. };
  275. template <class Tp>
  276. struct decay
  277. {
  278. private:
  279. typedef typename boost::move_detail::remove_rvalue_reference<Tp>::type Up0;
  280. typedef typename boost::remove_reference<Up0>::type Up;
  281. public:
  282. typedef typename conditional
  283. <
  284. is_array<Up>::value,
  285. typename remove_extent<Up>::type*,
  286. typename conditional
  287. <
  288. is_function<Up>::value,
  289. typename add_pointer<Up>::type,
  290. typename remove_cv<Up>::type
  291. >::type
  292. >::type type;
  293. };
  294. #endif
  295. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  296. template <class T>
  297. typename decay<T>::type
  298. decay_copy(T&& t)
  299. {
  300. return boost::forward<T>(t);
  301. }
  302. typedef void (*void_fct_ptr)();
  303. // inline void_fct_ptr
  304. // decay_copy(void (&t)())
  305. // {
  306. // return &t;
  307. // }
  308. #else
  309. template <class T>
  310. typename decay<T>::type
  311. decay_copy(BOOST_THREAD_FWD_REF(T) t)
  312. {
  313. return boost::forward<T>(t);
  314. }
  315. #endif
  316. }
  317. }
  318. #include <boost/config/abi_suffix.hpp>
  319. #endif