core.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2012-2012.
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // See http://www.boost.org/libs/move for documentation.
  9. //
  10. //////////////////////////////////////////////////////////////////////////////
  11. //! \file
  12. //! This header implements macros to define movable classes and
  13. //! move-aware functions
  14. #ifndef BOOST_MOVE_CORE_HPP
  15. #define BOOST_MOVE_CORE_HPP
  16. #ifndef BOOST_CONFIG_HPP
  17. # include <boost/config.hpp>
  18. #endif
  19. #
  20. #if defined(BOOST_HAS_PRAGMA_ONCE)
  21. # pragma once
  22. #endif
  23. #include <boost/move/detail/config_begin.hpp>
  24. #include <boost/move/detail/workaround.hpp>
  25. // @cond
  26. //boost_move_no_copy_constructor_or_assign typedef
  27. //used to detect noncopyable types for other Boost libraries.
  28. #if defined(BOOST_NO_CXX11_DELETED_FUNCTIONS) || defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  29. #define BOOST_MOVE_IMPL_NO_COPY_CTOR_OR_ASSIGN(TYPE) \
  30. private:\
  31. TYPE(TYPE &);\
  32. TYPE& operator=(TYPE &);\
  33. public:\
  34. typedef int boost_move_no_copy_constructor_or_assign; \
  35. private:\
  36. //
  37. #else
  38. #define BOOST_MOVE_IMPL_NO_COPY_CTOR_OR_ASSIGN(TYPE) \
  39. public:\
  40. TYPE(TYPE const &) = delete;\
  41. TYPE& operator=(TYPE const &) = delete;\
  42. public:\
  43. typedef int boost_move_no_copy_constructor_or_assign; \
  44. private:\
  45. //
  46. #endif //BOOST_NO_CXX11_DELETED_FUNCTIONS
  47. // @endcond
  48. #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_MOVE_DOXYGEN_INVOKED)
  49. #include <boost/move/detail/type_traits.hpp>
  50. #define BOOST_MOVE_TO_RV_CAST(RV_TYPE, ARG) reinterpret_cast<RV_TYPE>(ARG)
  51. //Move emulation rv breaks standard aliasing rules so add workarounds for some compilers
  52. #define BOOST_MOVE_ATTRIBUTE_MAY_ALIAS BOOST_MAY_ALIAS
  53. namespace boost {
  54. //////////////////////////////////////////////////////////////////////////////
  55. //
  56. // struct rv
  57. //
  58. //////////////////////////////////////////////////////////////////////////////
  59. template <class T>
  60. class rv
  61. : public ::boost::move_detail::if_c
  62. < ::boost::move_detail::is_class<T>::value
  63. , T
  64. , ::boost::move_detail::nat
  65. >::type
  66. {
  67. rv();
  68. ~rv() throw();
  69. rv(rv const&);
  70. void operator=(rv const&);
  71. } BOOST_MOVE_ATTRIBUTE_MAY_ALIAS;
  72. //////////////////////////////////////////////////////////////////////////////
  73. //
  74. // is_rv
  75. //
  76. //////////////////////////////////////////////////////////////////////////////
  77. namespace move_detail {
  78. template <class T>
  79. struct is_rv
  80. //Derive from integral constant because some Boost code assummes it has
  81. //a "type" internal typedef
  82. : integral_constant<bool, ::boost::move_detail::is_rv_impl<T>::value >
  83. {};
  84. template <class T>
  85. struct is_not_rv
  86. {
  87. static const bool value = !is_rv<T>::value;
  88. };
  89. } //namespace move_detail {
  90. //////////////////////////////////////////////////////////////////////////////
  91. //
  92. // has_move_emulation_enabled
  93. //
  94. //////////////////////////////////////////////////////////////////////////////
  95. template<class T>
  96. struct has_move_emulation_enabled
  97. : ::boost::move_detail::has_move_emulation_enabled_impl<T>
  98. {};
  99. template<class T>
  100. struct has_move_emulation_disabled
  101. {
  102. static const bool value = !::boost::move_detail::has_move_emulation_enabled_impl<T>::value;
  103. };
  104. } //namespace boost {
  105. #define BOOST_RV_REF(TYPE)\
  106. ::boost::rv< TYPE >& \
  107. //
  108. #define BOOST_RV_REF_2_TEMPL_ARGS(TYPE, ARG1, ARG2)\
  109. ::boost::rv< TYPE<ARG1, ARG2> >& \
  110. //
  111. #define BOOST_RV_REF_3_TEMPL_ARGS(TYPE, ARG1, ARG2, ARG3)\
  112. ::boost::rv< TYPE<ARG1, ARG2, ARG3> >& \
  113. //
  114. #define BOOST_RV_REF_BEG\
  115. ::boost::rv< \
  116. //
  117. #define BOOST_RV_REF_END\
  118. >& \
  119. //
  120. #define BOOST_RV_REF_BEG_IF_CXX11 \
  121. \
  122. //
  123. #define BOOST_RV_REF_END_IF_CXX11 \
  124. \
  125. //
  126. #define BOOST_FWD_REF(TYPE)\
  127. const TYPE & \
  128. //
  129. #define BOOST_COPY_ASSIGN_REF(TYPE)\
  130. const ::boost::rv< TYPE >& \
  131. //
  132. #define BOOST_COPY_ASSIGN_REF_BEG \
  133. const ::boost::rv< \
  134. //
  135. #define BOOST_COPY_ASSIGN_REF_END \
  136. >& \
  137. //
  138. #define BOOST_COPY_ASSIGN_REF_2_TEMPL_ARGS(TYPE, ARG1, ARG2)\
  139. const ::boost::rv< TYPE<ARG1, ARG2> >& \
  140. //
  141. #define BOOST_COPY_ASSIGN_REF_3_TEMPL_ARGS(TYPE, ARG1, ARG2, ARG3)\
  142. const ::boost::rv< TYPE<ARG1, ARG2, ARG3> >& \
  143. //
  144. #define BOOST_CATCH_CONST_RLVALUE(TYPE)\
  145. const ::boost::rv< TYPE >& \
  146. //
  147. namespace boost {
  148. namespace move_detail {
  149. template <class Ret, class T>
  150. BOOST_MOVE_FORCEINLINE typename ::boost::move_detail::enable_if_c
  151. < ::boost::move_detail::is_lvalue_reference<Ret>::value ||
  152. !::boost::has_move_emulation_enabled<T>::value
  153. , T&>::type
  154. move_return(T& x) BOOST_NOEXCEPT
  155. {
  156. return x;
  157. }
  158. template <class Ret, class T>
  159. BOOST_MOVE_FORCEINLINE typename ::boost::move_detail::enable_if_c
  160. < !::boost::move_detail::is_lvalue_reference<Ret>::value &&
  161. ::boost::has_move_emulation_enabled<T>::value
  162. , ::boost::rv<T>&>::type
  163. move_return(T& x) BOOST_NOEXCEPT
  164. {
  165. return *BOOST_MOVE_TO_RV_CAST(::boost::rv<T>*, ::boost::move_detail::addressof(x));
  166. }
  167. template <class Ret, class T>
  168. BOOST_MOVE_FORCEINLINE typename ::boost::move_detail::enable_if_c
  169. < !::boost::move_detail::is_lvalue_reference<Ret>::value &&
  170. ::boost::has_move_emulation_enabled<T>::value
  171. , ::boost::rv<T>&>::type
  172. move_return(::boost::rv<T>& x) BOOST_NOEXCEPT
  173. {
  174. return x;
  175. }
  176. } //namespace move_detail {
  177. } //namespace boost {
  178. #define BOOST_MOVE_RET(RET_TYPE, REF)\
  179. boost::move_detail::move_return< RET_TYPE >(REF)
  180. //
  181. #define BOOST_MOVE_BASE(BASE_TYPE, ARG) \
  182. ::boost::move((BASE_TYPE&)(ARG))
  183. //
  184. //////////////////////////////////////////////////////////////////////////////
  185. //
  186. // BOOST_MOVABLE_BUT_NOT_COPYABLE
  187. //
  188. //////////////////////////////////////////////////////////////////////////////
  189. #define BOOST_MOVABLE_BUT_NOT_COPYABLE(TYPE)\
  190. BOOST_MOVE_IMPL_NO_COPY_CTOR_OR_ASSIGN(TYPE)\
  191. public:\
  192. BOOST_MOVE_FORCEINLINE operator ::boost::rv<TYPE>&() \
  193. { return *BOOST_MOVE_TO_RV_CAST(::boost::rv<TYPE>*, this); }\
  194. BOOST_MOVE_FORCEINLINE operator const ::boost::rv<TYPE>&() const \
  195. { return *BOOST_MOVE_TO_RV_CAST(const ::boost::rv<TYPE>*, this); }\
  196. private:\
  197. //
  198. //////////////////////////////////////////////////////////////////////////////
  199. //
  200. // BOOST_COPYABLE_AND_MOVABLE
  201. //
  202. //////////////////////////////////////////////////////////////////////////////
  203. #define BOOST_COPYABLE_AND_MOVABLE(TYPE)\
  204. public:\
  205. BOOST_MOVE_FORCEINLINE TYPE& operator=(TYPE &t)\
  206. { this->operator=(const_cast<const TYPE&>(t)); return *this;}\
  207. public:\
  208. BOOST_MOVE_FORCEINLINE operator ::boost::rv<TYPE>&() \
  209. { return *BOOST_MOVE_TO_RV_CAST(::boost::rv<TYPE>*, this); }\
  210. BOOST_MOVE_FORCEINLINE operator const ::boost::rv<TYPE>&() const \
  211. { return *BOOST_MOVE_TO_RV_CAST(const ::boost::rv<TYPE>*, this); }\
  212. private:\
  213. //
  214. #define BOOST_COPYABLE_AND_MOVABLE_ALT(TYPE)\
  215. public:\
  216. BOOST_MOVE_FORCEINLINE operator ::boost::rv<TYPE>&() \
  217. { return *BOOST_MOVE_TO_RV_CAST(::boost::rv<TYPE>*, this); }\
  218. BOOST_MOVE_FORCEINLINE operator const ::boost::rv<TYPE>&() const \
  219. { return *BOOST_MOVE_TO_RV_CAST(const ::boost::rv<TYPE>*, this); }\
  220. private:\
  221. //
  222. namespace boost{
  223. namespace move_detail{
  224. template< class T>
  225. struct forward_type
  226. { typedef const T &type; };
  227. template< class T>
  228. struct forward_type< boost::rv<T> >
  229. { typedef T type; };
  230. }}
  231. #else //BOOST_NO_CXX11_RVALUE_REFERENCES
  232. //! This macro marks a type as movable but not copyable, disabling copy construction
  233. //! and assignment. The user will need to write a move constructor/assignment as explained
  234. //! in the documentation to fully write a movable but not copyable class.
  235. #define BOOST_MOVABLE_BUT_NOT_COPYABLE(TYPE)\
  236. BOOST_MOVE_IMPL_NO_COPY_CTOR_OR_ASSIGN(TYPE)\
  237. public:\
  238. typedef int boost_move_emulation_t;\
  239. private:\
  240. //
  241. //! This macro marks a type as copyable and movable.
  242. //! The user will need to write a move constructor/assignment and a copy assignment
  243. //! as explained in the documentation to fully write a copyable and movable class.
  244. #define BOOST_COPYABLE_AND_MOVABLE(TYPE)\
  245. //
  246. #if !defined(BOOST_MOVE_DOXYGEN_INVOKED)
  247. #define BOOST_COPYABLE_AND_MOVABLE_ALT(TYPE)\
  248. //
  249. #endif //#if !defined(BOOST_MOVE_DOXYGEN_INVOKED)
  250. namespace boost {
  251. //!This trait yields to a compile-time true boolean if T was marked as
  252. //!BOOST_MOVABLE_BUT_NOT_COPYABLE or BOOST_COPYABLE_AND_MOVABLE and
  253. //!rvalue references are not available on the platform. False otherwise.
  254. template<class T>
  255. struct has_move_emulation_enabled
  256. {
  257. static const bool value = false;
  258. };
  259. template<class T>
  260. struct has_move_emulation_disabled
  261. {
  262. static const bool value = true;
  263. };
  264. } //namespace boost{
  265. //!This macro is used to achieve portable syntax in move
  266. //!constructors and assignments for classes marked as
  267. //!BOOST_COPYABLE_AND_MOVABLE or BOOST_MOVABLE_BUT_NOT_COPYABLE
  268. #define BOOST_RV_REF(TYPE)\
  269. TYPE && \
  270. //
  271. //!This macro is used to achieve portable syntax in move
  272. //!constructors and assignments for template classes marked as
  273. //!BOOST_COPYABLE_AND_MOVABLE or BOOST_MOVABLE_BUT_NOT_COPYABLE.
  274. //!As macros have problems with comma-separated template arguments,
  275. //!the template argument must be preceded with BOOST_RV_REF_BEG
  276. //!and ended with BOOST_RV_REF_END
  277. #define BOOST_RV_REF_BEG\
  278. \
  279. //
  280. //!This macro is used to achieve portable syntax in move
  281. //!constructors and assignments for template classes marked as
  282. //!BOOST_COPYABLE_AND_MOVABLE or BOOST_MOVABLE_BUT_NOT_COPYABLE.
  283. //!As macros have problems with comma-separated template arguments,
  284. //!the template argument must be preceded with BOOST_RV_REF_BEG
  285. //!and ended with BOOST_RV_REF_END
  286. #define BOOST_RV_REF_END\
  287. && \
  288. //
  289. //!This macro expands to BOOST_RV_REF_BEG if BOOST_NO_CXX11_RVALUE_REFERENCES
  290. //!is not defined, empty otherwise
  291. #define BOOST_RV_REF_BEG_IF_CXX11 \
  292. BOOST_RV_REF_BEG \
  293. //
  294. //!This macro expands to BOOST_RV_REF_END if BOOST_NO_CXX11_RVALUE_REFERENCES
  295. //!is not defined, empty otherwise
  296. #define BOOST_RV_REF_END_IF_CXX11 \
  297. BOOST_RV_REF_END \
  298. //
  299. //!This macro is used to achieve portable syntax in copy
  300. //!assignment for classes marked as BOOST_COPYABLE_AND_MOVABLE.
  301. #define BOOST_COPY_ASSIGN_REF(TYPE)\
  302. const TYPE & \
  303. //
  304. //! This macro is used to implement portable perfect forwarding
  305. //! as explained in the documentation.
  306. #define BOOST_FWD_REF(TYPE)\
  307. TYPE && \
  308. //
  309. #if !defined(BOOST_MOVE_DOXYGEN_INVOKED)
  310. #define BOOST_RV_REF_2_TEMPL_ARGS(TYPE, ARG1, ARG2)\
  311. TYPE<ARG1, ARG2> && \
  312. //
  313. #define BOOST_RV_REF_3_TEMPL_ARGS(TYPE, ARG1, ARG2, ARG3)\
  314. TYPE<ARG1, ARG2, ARG3> && \
  315. //
  316. #define BOOST_COPY_ASSIGN_REF_BEG \
  317. const \
  318. //
  319. #define BOOST_COPY_ASSIGN_REF_END \
  320. & \
  321. //
  322. #define BOOST_COPY_ASSIGN_REF_2_TEMPL_ARGS(TYPE, ARG1, ARG2)\
  323. const TYPE<ARG1, ARG2> & \
  324. //
  325. #define BOOST_COPY_ASSIGN_REF_3_TEMPL_ARGS(TYPE, ARG1, ARG2, ARG3)\
  326. const TYPE<ARG1, ARG2, ARG3>& \
  327. //
  328. #define BOOST_CATCH_CONST_RLVALUE(TYPE)\
  329. const TYPE & \
  330. //
  331. #endif //#if !defined(BOOST_MOVE_DOXYGEN_INVOKED)
  332. #if !defined(BOOST_MOVE_MSVC_AUTO_MOVE_RETURN_BUG) || defined(BOOST_MOVE_DOXYGEN_INVOKED)
  333. //!This macro is used to achieve portable move return semantics.
  334. //!The C++11 Standard allows implicit move returns when the object to be returned
  335. //!is designated by a lvalue and:
  336. //! - The criteria for elision of a copy operation are met OR
  337. //! - The criteria would be met save for the fact that the source object is a function parameter
  338. //!
  339. //!For C++11 conforming compilers this macros only yields to REF:
  340. //! <code>return BOOST_MOVE_RET(RET_TYPE, REF);</code> -> <code>return REF;</code>
  341. //!
  342. //!For compilers without rvalue references
  343. //!this macro does an explicit move if the move emulation is activated
  344. //!and the return type (RET_TYPE) is not a reference.
  345. //!
  346. //!For non-conforming compilers with rvalue references like Visual 2010 & 2012,
  347. //!an explicit move is performed if RET_TYPE is not a reference.
  348. //!
  349. //! <b>Caution</b>: When using this macro in non-conforming or C++03
  350. //!compilers, a move will be performed even if the C++11 standard does not allow it
  351. //!(e.g. returning a static variable). The user is responsible for using this macro
  352. //!only to return local objects that met C++11 criteria.
  353. #define BOOST_MOVE_RET(RET_TYPE, REF)\
  354. REF
  355. //
  356. #else //!defined(BOOST_MOVE_MSVC_AUTO_MOVE_RETURN_BUG) || defined(BOOST_MOVE_DOXYGEN_INVOKED)
  357. #include <boost/move/detail/meta_utils.hpp>
  358. namespace boost {
  359. namespace move_detail {
  360. template <class Ret, class T>
  361. BOOST_MOVE_FORCEINLINE typename ::boost::move_detail::enable_if_c
  362. < ::boost::move_detail::is_lvalue_reference<Ret>::value
  363. , T&>::type
  364. move_return(T& x) BOOST_NOEXCEPT
  365. {
  366. return x;
  367. }
  368. template <class Ret, class T>
  369. BOOST_MOVE_FORCEINLINE typename ::boost::move_detail::enable_if_c
  370. < !::boost::move_detail::is_lvalue_reference<Ret>::value
  371. , Ret && >::type
  372. move_return(T&& t) BOOST_NOEXCEPT
  373. {
  374. return static_cast< Ret&& >(t);
  375. }
  376. } //namespace move_detail {
  377. } //namespace boost {
  378. #define BOOST_MOVE_RET(RET_TYPE, REF)\
  379. boost::move_detail::move_return< RET_TYPE >(REF)
  380. //
  381. #endif //!defined(BOOST_MOVE_MSVC_AUTO_MOVE_RETURN_BUG) || defined(BOOST_MOVE_DOXYGEN_INVOKED)
  382. //!This macro is used to achieve portable optimal move constructors.
  383. //!
  384. //!When implementing the move constructor, in C++03 compilers the moved-from argument must be
  385. //!cast to the base type before calling `::boost::move()` due to rvalue reference limitations.
  386. //!
  387. //!In C++11 compilers the cast from a rvalue reference of a derived type to a rvalue reference of
  388. //!a base type is implicit.
  389. #define BOOST_MOVE_BASE(BASE_TYPE, ARG) \
  390. ::boost::move((BASE_TYPE&)(ARG))
  391. //
  392. namespace boost {
  393. namespace move_detail {
  394. template< class T> struct forward_type { typedef T type; };
  395. }}
  396. #endif //BOOST_NO_CXX11_RVALUE_REFERENCES
  397. #include <boost/move/detail/config_end.hpp>
  398. #endif //#ifndef BOOST_MOVE_CORE_HPP