1 //////////////////////////////////////////////////////////////////////////////
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)
8 // See http://www.boost.org/libs/move for documentation.
10 //////////////////////////////////////////////////////////////////////////////
13 //! This header implements macros to define movable classes and
14 //! move-aware functions
16 #ifndef BOOST_MOVE_CORE_HPP
17 #define BOOST_MOVE_CORE_HPP
19 #ifndef BOOST_CONFIG_HPP
20 # include <boost/config.hpp>
23 #if defined(BOOST_HAS_PRAGMA_ONCE)
27 #include <boost/move/detail/config_begin.hpp>
28 #include <boost/move/detail/workaround.hpp>
32 //boost_move_no_copy_constructor_or_assign typedef
33 //used to detect noncopyable types for other Boost libraries.
34 #if defined(BOOST_NO_CXX11_DELETED_FUNCTIONS) || defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
35 #define BOOST_MOVE_IMPL_NO_COPY_CTOR_OR_ASSIGN(TYPE) \
38 TYPE& operator=(TYPE &);\
40 typedef int boost_move_no_copy_constructor_or_assign; \
44 #define BOOST_MOVE_IMPL_NO_COPY_CTOR_OR_ASSIGN(TYPE) \
46 TYPE(TYPE const &) = delete;\
47 TYPE& operator=(TYPE const &) = delete;\
49 typedef int boost_move_no_copy_constructor_or_assign; \
52 #endif //BOOST_NO_CXX11_DELETED_FUNCTIONS
56 #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_MOVE_DOXYGEN_INVOKED)
58 #include <boost/move/detail/type_traits.hpp>
60 #if defined(BOOST_MOVE_ADDRESS_SANITIZER_ON)
61 #define BOOST_MOVE_TO_RV_CAST(RV_TYPE, ARG) reinterpret_cast<RV_TYPE>(ARG)
63 #define BOOST_MOVE_TO_RV_CAST(RV_TYPE, ARG) static_cast<RV_TYPE>(ARG)
66 //Move emulation rv breaks standard aliasing rules so add workarounds for some compilers
67 #define BOOST_MOVE_ATTRIBUTE_MAY_ALIAS BOOST_MAY_ALIAS
71 //////////////////////////////////////////////////////////////////////////////
75 //////////////////////////////////////////////////////////////////////////////
78 : public ::boost::move_detail::if_c
79 < ::boost::move_detail::is_class<T>::value
81 , ::boost::move_detail::nat
87 void operator=(rv const&);
88 } BOOST_MOVE_ATTRIBUTE_MAY_ALIAS;
91 //////////////////////////////////////////////////////////////////////////////
95 //////////////////////////////////////////////////////////////////////////////
97 namespace move_detail {
101 //Derive from integral constant because some Boost code assummes it has
102 //a "type" internal typedef
103 : integral_constant<bool, ::boost::move_detail::is_rv_impl<T>::value >
109 static const bool value = !is_rv<T>::value;
112 } //namespace move_detail {
114 //////////////////////////////////////////////////////////////////////////////
116 // has_move_emulation_enabled
118 //////////////////////////////////////////////////////////////////////////////
120 struct has_move_emulation_enabled
121 : ::boost::move_detail::has_move_emulation_enabled_impl<T>
125 struct has_move_emulation_disabled
127 static const bool value = !::boost::move_detail::has_move_emulation_enabled_impl<T>::value;
130 } //namespace boost {
132 #define BOOST_RV_REF(TYPE)\
133 ::boost::rv< TYPE >& \
136 #define BOOST_RV_REF_2_TEMPL_ARGS(TYPE, ARG1, ARG2)\
137 ::boost::rv< TYPE<ARG1, ARG2> >& \
140 #define BOOST_RV_REF_3_TEMPL_ARGS(TYPE, ARG1, ARG2, ARG3)\
141 ::boost::rv< TYPE<ARG1, ARG2, ARG3> >& \
144 #define BOOST_RV_REF_BEG\
148 #define BOOST_RV_REF_END\
152 #define BOOST_RV_REF_BEG_IF_CXX11 \
156 #define BOOST_RV_REF_END_IF_CXX11 \
160 #define BOOST_FWD_REF(TYPE)\
164 #define BOOST_COPY_ASSIGN_REF(TYPE)\
165 const ::boost::rv< TYPE >& \
168 #define BOOST_COPY_ASSIGN_REF_BEG \
172 #define BOOST_COPY_ASSIGN_REF_END \
176 #define BOOST_COPY_ASSIGN_REF_2_TEMPL_ARGS(TYPE, ARG1, ARG2)\
177 const ::boost::rv< TYPE<ARG1, ARG2> >& \
180 #define BOOST_COPY_ASSIGN_REF_3_TEMPL_ARGS(TYPE, ARG1, ARG2, ARG3)\
181 const ::boost::rv< TYPE<ARG1, ARG2, ARG3> >& \
184 #define BOOST_CATCH_CONST_RLVALUE(TYPE)\
185 const ::boost::rv< TYPE >& \
189 namespace move_detail {
191 template <class Ret, class T>
192 BOOST_MOVE_FORCEINLINE typename ::boost::move_detail::enable_if_c
193 < ::boost::move_detail::is_lvalue_reference<Ret>::value ||
194 !::boost::has_move_emulation_enabled<T>::value
196 move_return(T& x) BOOST_NOEXCEPT
201 template <class Ret, class T>
202 BOOST_MOVE_FORCEINLINE typename ::boost::move_detail::enable_if_c
203 < !::boost::move_detail::is_lvalue_reference<Ret>::value &&
204 ::boost::has_move_emulation_enabled<T>::value
205 , ::boost::rv<T>&>::type
206 move_return(T& x) BOOST_NOEXCEPT
208 return *BOOST_MOVE_TO_RV_CAST(::boost::rv<T>*, ::boost::move_detail::addressof(x));
211 template <class Ret, class T>
212 BOOST_MOVE_FORCEINLINE typename ::boost::move_detail::enable_if_c
213 < !::boost::move_detail::is_lvalue_reference<Ret>::value &&
214 ::boost::has_move_emulation_enabled<T>::value
215 , ::boost::rv<T>&>::type
216 move_return(::boost::rv<T>& x) BOOST_NOEXCEPT
221 } //namespace move_detail {
222 } //namespace boost {
224 #define BOOST_MOVE_RET(RET_TYPE, REF)\
225 boost::move_detail::move_return< RET_TYPE >(REF)
228 #define BOOST_MOVE_BASE(BASE_TYPE, ARG) \
229 ::boost::move((BASE_TYPE&)(ARG))
232 //////////////////////////////////////////////////////////////////////////////
234 // BOOST_MOVABLE_BUT_NOT_COPYABLE
236 //////////////////////////////////////////////////////////////////////////////
237 #define BOOST_MOVABLE_BUT_NOT_COPYABLE(TYPE)\
238 BOOST_MOVE_IMPL_NO_COPY_CTOR_OR_ASSIGN(TYPE)\
240 BOOST_MOVE_FORCEINLINE operator ::boost::rv<TYPE>&() \
241 { return *BOOST_MOVE_TO_RV_CAST(::boost::rv<TYPE>*, this); }\
242 BOOST_MOVE_FORCEINLINE operator const ::boost::rv<TYPE>&() const \
243 { return *BOOST_MOVE_TO_RV_CAST(const ::boost::rv<TYPE>*, this); }\
247 //////////////////////////////////////////////////////////////////////////////
249 // BOOST_COPYABLE_AND_MOVABLE
251 //////////////////////////////////////////////////////////////////////////////
253 #define BOOST_COPYABLE_AND_MOVABLE(TYPE)\
255 BOOST_MOVE_FORCEINLINE TYPE& operator=(TYPE &t)\
256 { this->operator=(const_cast<const TYPE&>(t)); return *this;}\
258 BOOST_MOVE_FORCEINLINE operator ::boost::rv<TYPE>&() \
259 { return *BOOST_MOVE_TO_RV_CAST(::boost::rv<TYPE>*, this); }\
260 BOOST_MOVE_FORCEINLINE operator const ::boost::rv<TYPE>&() const \
261 { return *BOOST_MOVE_TO_RV_CAST(const ::boost::rv<TYPE>*, this); }\
265 #define BOOST_COPYABLE_AND_MOVABLE_ALT(TYPE)\
267 BOOST_MOVE_FORCEINLINE operator ::boost::rv<TYPE>&() \
268 { return *BOOST_MOVE_TO_RV_CAST(::boost::rv<TYPE>*, this); }\
269 BOOST_MOVE_FORCEINLINE operator const ::boost::rv<TYPE>&() const \
270 { return *BOOST_MOVE_TO_RV_CAST(const ::boost::rv<TYPE>*, this); }\
275 namespace move_detail{
279 { typedef const T &type; };
282 struct forward_type< boost::rv<T> >
287 #else //BOOST_NO_CXX11_RVALUE_REFERENCES
289 //! This macro marks a type as movable but not copyable, disabling copy construction
290 //! and assignment. The user will need to write a move constructor/assignment as explained
291 //! in the documentation to fully write a movable but not copyable class.
292 #define BOOST_MOVABLE_BUT_NOT_COPYABLE(TYPE)\
293 BOOST_MOVE_IMPL_NO_COPY_CTOR_OR_ASSIGN(TYPE)\
295 typedef int boost_move_emulation_t;\
299 //! This macro marks a type as copyable and movable.
300 //! The user will need to write a move constructor/assignment and a copy assignment
301 //! as explained in the documentation to fully write a copyable and movable class.
302 #define BOOST_COPYABLE_AND_MOVABLE(TYPE)\
305 #if !defined(BOOST_MOVE_DOXYGEN_INVOKED)
306 #define BOOST_COPYABLE_AND_MOVABLE_ALT(TYPE)\
308 #endif //#if !defined(BOOST_MOVE_DOXYGEN_INVOKED)
312 //!This trait yields to a compile-time true boolean if T was marked as
313 //!BOOST_MOVABLE_BUT_NOT_COPYABLE or BOOST_COPYABLE_AND_MOVABLE and
314 //!rvalue references are not available on the platform. False otherwise.
316 struct has_move_emulation_enabled
318 static const bool value = false;
322 struct has_move_emulation_disabled
324 static const bool value = true;
329 //!This macro is used to achieve portable syntax in move
330 //!constructors and assignments for classes marked as
331 //!BOOST_COPYABLE_AND_MOVABLE or BOOST_MOVABLE_BUT_NOT_COPYABLE
332 #define BOOST_RV_REF(TYPE)\
336 //!This macro is used to achieve portable syntax in move
337 //!constructors and assignments for template classes marked as
338 //!BOOST_COPYABLE_AND_MOVABLE or BOOST_MOVABLE_BUT_NOT_COPYABLE.
339 //!As macros have problems with comma-separated template arguments,
340 //!the template argument must be preceded with BOOST_RV_REF_BEG
341 //!and ended with BOOST_RV_REF_END
342 #define BOOST_RV_REF_BEG\
346 //!This macro is used to achieve portable syntax in move
347 //!constructors and assignments for template classes marked as
348 //!BOOST_COPYABLE_AND_MOVABLE or BOOST_MOVABLE_BUT_NOT_COPYABLE.
349 //!As macros have problems with comma-separated template arguments,
350 //!the template argument must be preceded with BOOST_RV_REF_BEG
351 //!and ended with BOOST_RV_REF_END
352 #define BOOST_RV_REF_END\
356 //!This macro expands to BOOST_RV_REF_BEG if BOOST_NO_CXX11_RVALUE_REFERENCES
357 //!is not defined, empty otherwise
358 #define BOOST_RV_REF_BEG_IF_CXX11 \
362 //!This macro expands to BOOST_RV_REF_END if BOOST_NO_CXX11_RVALUE_REFERENCES
363 //!is not defined, empty otherwise
364 #define BOOST_RV_REF_END_IF_CXX11 \
368 //!This macro is used to achieve portable syntax in copy
369 //!assignment for classes marked as BOOST_COPYABLE_AND_MOVABLE.
370 #define BOOST_COPY_ASSIGN_REF(TYPE)\
374 //! This macro is used to implement portable perfect forwarding
375 //! as explained in the documentation.
376 #define BOOST_FWD_REF(TYPE)\
380 #if !defined(BOOST_MOVE_DOXYGEN_INVOKED)
382 #define BOOST_RV_REF_2_TEMPL_ARGS(TYPE, ARG1, ARG2)\
383 TYPE<ARG1, ARG2> && \
386 #define BOOST_RV_REF_3_TEMPL_ARGS(TYPE, ARG1, ARG2, ARG3)\
387 TYPE<ARG1, ARG2, ARG3> && \
390 #define BOOST_COPY_ASSIGN_REF_BEG \
394 #define BOOST_COPY_ASSIGN_REF_END \
398 #define BOOST_COPY_ASSIGN_REF_2_TEMPL_ARGS(TYPE, ARG1, ARG2)\
399 const TYPE<ARG1, ARG2> & \
402 #define BOOST_COPY_ASSIGN_REF_3_TEMPL_ARGS(TYPE, ARG1, ARG2, ARG3)\
403 const TYPE<ARG1, ARG2, ARG3>& \
406 #define BOOST_CATCH_CONST_RLVALUE(TYPE)\
410 #endif //#if !defined(BOOST_MOVE_DOXYGEN_INVOKED)
412 #if !defined(BOOST_MOVE_MSVC_AUTO_MOVE_RETURN_BUG) || defined(BOOST_MOVE_DOXYGEN_INVOKED)
414 //!This macro is used to achieve portable move return semantics.
415 //!The C++11 Standard allows implicit move returns when the object to be returned
416 //!is designated by a lvalue and:
417 //! - The criteria for elision of a copy operation are met OR
418 //! - The criteria would be met save for the fact that the source object is a function parameter
420 //!For C++11 conforming compilers this macros only yields to REF:
421 //! <code>return BOOST_MOVE_RET(RET_TYPE, REF);</code> -> <code>return REF;</code>
423 //!For compilers without rvalue references
424 //!this macro does an explicit move if the move emulation is activated
425 //!and the return type (RET_TYPE) is not a reference.
427 //!For non-conforming compilers with rvalue references like Visual 2010 & 2012,
428 //!an explicit move is performed if RET_TYPE is not a reference.
430 //! <b>Caution</b>: When using this macro in non-conforming or C++03
431 //!compilers, a move will be performed even if the C++11 standard does not allow it
432 //!(e.g. returning a static variable). The user is responsible for using this macro
433 //!only to return local objects that met C++11 criteria.
434 #define BOOST_MOVE_RET(RET_TYPE, REF)\
438 #else //!defined(BOOST_MOVE_MSVC_AUTO_MOVE_RETURN_BUG) || defined(BOOST_MOVE_DOXYGEN_INVOKED)
440 #include <boost/move/detail/meta_utils.hpp>
443 namespace move_detail {
445 template <class Ret, class T>
446 BOOST_MOVE_FORCEINLINE typename ::boost::move_detail::enable_if_c
447 < ::boost::move_detail::is_lvalue_reference<Ret>::value
449 move_return(T& x) BOOST_NOEXCEPT
454 template <class Ret, class T>
455 BOOST_MOVE_FORCEINLINE typename ::boost::move_detail::enable_if_c
456 < !::boost::move_detail::is_lvalue_reference<Ret>::value
458 move_return(T&& t) BOOST_NOEXCEPT
460 return static_cast< Ret&& >(t);
463 } //namespace move_detail {
464 } //namespace boost {
466 #define BOOST_MOVE_RET(RET_TYPE, REF)\
467 boost::move_detail::move_return< RET_TYPE >(REF)
470 #endif //!defined(BOOST_MOVE_MSVC_AUTO_MOVE_RETURN_BUG) || defined(BOOST_MOVE_DOXYGEN_INVOKED)
472 //!This macro is used to achieve portable optimal move constructors.
474 //!When implementing the move constructor, in C++03 compilers the moved-from argument must be
475 //!cast to the base type before calling `::boost::move()` due to rvalue reference limitations.
477 //!In C++11 compilers the cast from a rvalue reference of a derived type to a rvalue reference of
478 //!a base type is implicit.
479 #define BOOST_MOVE_BASE(BASE_TYPE, ARG) \
480 ::boost::move((BASE_TYPE&)(ARG))
484 namespace move_detail {
486 template< class T> struct forward_type { typedef T type; };
490 #endif //BOOST_NO_CXX11_RVALUE_REFERENCES
492 #include <boost/move/detail/config_end.hpp>
494 #endif //#ifndef BOOST_MOVE_CORE_HPP