core.hpp 15 KB

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