core.hpp 16 KB

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