////////////////////////////////////////////////////////////////////////////// // // (C) Copyright Ion Gaztanaga 2012-2012. // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // // See http://www.boost.org/libs/move for documentation. // ////////////////////////////////////////////////////////////////////////////// //! \file //! This header implements macros to define movable classes and //! move-aware functions #ifndef ACTIV_BOOST_MOVE_CORE_HPP #define ACTIV_BOOST_MOVE_CORE_HPP #ifndef ACTIV_BOOST_CONFIG_HPP # include #endif # #if defined(ACTIV_BOOST_HAS_PRAGMA_ONCE) # pragma once #endif #include #include // @cond //boost_move_no_copy_constructor_or_assign typedef //used to detect noncopyable types for other Boost libraries. #if defined(ACTIV_BOOST_NO_CXX11_DELETED_FUNCTIONS) || defined(ACTIV_BOOST_NO_CXX11_RVALUE_REFERENCES) #define ACTIV_BOOST_MOVE_IMPL_NO_COPY_CTOR_OR_ASSIGN(TYPE) \ private:\ TYPE(TYPE &);\ TYPE& operator=(TYPE &);\ public:\ typedef int boost_move_no_copy_constructor_or_assign; \ private:\ // #else #define ACTIV_BOOST_MOVE_IMPL_NO_COPY_CTOR_OR_ASSIGN(TYPE) \ public:\ TYPE(TYPE const &) = delete;\ TYPE& operator=(TYPE const &) = delete;\ public:\ typedef int boost_move_no_copy_constructor_or_assign; \ private:\ // #endif //ACTIV_BOOST_NO_CXX11_DELETED_FUNCTIONS // @endcond #if defined(ACTIV_BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(ACTIV_BOOST_MOVE_DOXYGEN_INVOKED) #include #define ACTIV_BOOST_MOVE_TO_RV_CAST(RV_TYPE, ARG) reinterpret_cast(ARG) //Move emulation rv breaks standard aliasing rules so add workarounds for some compilers #if defined(ACTIV_BOOST_GCC) && (ACTIV_BOOST_GCC >= 40400) && (ACTIV_BOOST_GCC < 40500) #define ACTIV_BOOST_RV_ATTRIBUTE_MAY_ALIAS ACTIV_BOOST_MAY_ALIAS #else #define ACTIV_BOOST_RV_ATTRIBUTE_MAY_ALIAS #endif namespace Boost { ////////////////////////////////////////////////////////////////////////////// // // struct rv // ////////////////////////////////////////////////////////////////////////////// template class ACTIV_BOOST_RV_ATTRIBUTE_MAY_ALIAS rv : public ::Boost::move_detail::if_c < ::Boost::move_detail::is_class::value , T , ::Boost::move_detail::nat >::type { rv(); ~rv() throw(); rv(rv const&); void operator=(rv const&); }; ////////////////////////////////////////////////////////////////////////////// // // is_rv // ////////////////////////////////////////////////////////////////////////////// namespace move_detail { template struct is_rv //Derive from integral constant because some Boost code assummes it has //a "type" internal typedef : integral_constant::value > {}; template struct is_not_rv { static const bool value = !is_rv::value; }; } //namespace move_detail { ////////////////////////////////////////////////////////////////////////////// // // has_move_emulation_enabled // ////////////////////////////////////////////////////////////////////////////// template struct has_move_emulation_enabled : ::Boost::move_detail::has_move_emulation_enabled_impl {}; template struct has_move_emulation_disabled { static const bool value = !::Boost::move_detail::has_move_emulation_enabled_impl::value; }; } //namespace Boost { #define ACTIV_BOOST_RV_REF(TYPE)\ ::Boost::rv< TYPE >& \ // #define ACTIV_BOOST_RV_REF_2_TEMPL_ARGS(TYPE, ARG1, ARG2)\ ::Boost::rv< TYPE >& \ // #define ACTIV_BOOST_RV_REF_3_TEMPL_ARGS(TYPE, ARG1, ARG2, ARG3)\ ::Boost::rv< TYPE >& \ // #define ACTIV_BOOST_RV_REF_BEG\ ::Boost::rv< \ // #define ACTIV_BOOST_RV_REF_END\ >& \ // #define ACTIV_BOOST_RV_REF_BEG_IF_CXX11 \ \ // #define ACTIV_BOOST_RV_REF_END_IF_CXX11 \ \ // #define ACTIV_BOOST_FWD_REF(TYPE)\ const TYPE & \ // #define ACTIV_BOOST_COPY_ASSIGN_REF(TYPE)\ const ::Boost::rv< TYPE >& \ // #define ACTIV_BOOST_COPY_ASSIGN_REF_BEG \ const ::Boost::rv< \ // #define ACTIV_BOOST_COPY_ASSIGN_REF_END \ >& \ // #define ACTIV_BOOST_COPY_ASSIGN_REF_2_TEMPL_ARGS(TYPE, ARG1, ARG2)\ const ::Boost::rv< TYPE >& \ // #define ACTIV_BOOST_COPY_ASSIGN_REF_3_TEMPL_ARGS(TYPE, ARG1, ARG2, ARG3)\ const ::Boost::rv< TYPE >& \ // #define ACTIV_BOOST_CATCH_CONST_RLVALUE(TYPE)\ const ::Boost::rv< TYPE >& \ // namespace Boost { namespace move_detail { template ACTIV_BOOST_MOVE_FORCEINLINE typename ::Boost::move_detail::enable_if_c < ::Boost::move_detail::is_lvalue_reference::value || !::Boost::has_move_emulation_enabled::value , T&>::type move_return(T& x) ACTIV_BOOST_NOEXCEPT { return x; } template ACTIV_BOOST_MOVE_FORCEINLINE typename ::Boost::move_detail::enable_if_c < !::Boost::move_detail::is_lvalue_reference::value && ::Boost::has_move_emulation_enabled::value , ::Boost::rv&>::type move_return(T& x) ACTIV_BOOST_NOEXCEPT { return *ACTIV_BOOST_MOVE_TO_RV_CAST(::Boost::rv*, ::Boost::move_detail::addressof(x)); } template ACTIV_BOOST_MOVE_FORCEINLINE typename ::Boost::move_detail::enable_if_c < !::Boost::move_detail::is_lvalue_reference::value && ::Boost::has_move_emulation_enabled::value , ::Boost::rv&>::type move_return(::Boost::rv& x) ACTIV_BOOST_NOEXCEPT { return x; } } //namespace move_detail { } //namespace Boost { #define ACTIV_BOOST_MOVE_RET(RET_TYPE, REF)\ Boost::move_detail::move_return< RET_TYPE >(REF) // #define ACTIV_BOOST_MOVE_BASE(BASE_TYPE, ARG) \ ::Boost::move((BASE_TYPE&)(ARG)) // ////////////////////////////////////////////////////////////////////////////// // // ACTIV_BOOST_MOVABLE_BUT_NOT_COPYABLE // ////////////////////////////////////////////////////////////////////////////// #define ACTIV_BOOST_MOVABLE_BUT_NOT_COPYABLE(TYPE)\ ACTIV_BOOST_MOVE_IMPL_NO_COPY_CTOR_OR_ASSIGN(TYPE)\ public:\ ACTIV_BOOST_MOVE_FORCEINLINE operator ::Boost::rv&() \ { return *ACTIV_BOOST_MOVE_TO_RV_CAST(::Boost::rv*, this); }\ ACTIV_BOOST_MOVE_FORCEINLINE operator const ::Boost::rv&() const \ { return *ACTIV_BOOST_MOVE_TO_RV_CAST(const ::Boost::rv*, this); }\ private:\ // ////////////////////////////////////////////////////////////////////////////// // // ACTIV_BOOST_COPYABLE_AND_MOVABLE // ////////////////////////////////////////////////////////////////////////////// #define ACTIV_BOOST_COPYABLE_AND_MOVABLE(TYPE)\ public:\ ACTIV_BOOST_MOVE_FORCEINLINE TYPE& operator=(TYPE &t)\ { this->operator=(const_cast(t)); return *this;}\ public:\ ACTIV_BOOST_MOVE_FORCEINLINE operator ::Boost::rv&() \ { return *ACTIV_BOOST_MOVE_TO_RV_CAST(::Boost::rv*, this); }\ ACTIV_BOOST_MOVE_FORCEINLINE operator const ::Boost::rv&() const \ { return *ACTIV_BOOST_MOVE_TO_RV_CAST(const ::Boost::rv*, this); }\ private:\ // #define ACTIV_BOOST_COPYABLE_AND_MOVABLE_ALT(TYPE)\ public:\ ACTIV_BOOST_MOVE_FORCEINLINE operator ::Boost::rv&() \ { return *ACTIV_BOOST_MOVE_TO_RV_CAST(::Boost::rv*, this); }\ ACTIV_BOOST_MOVE_FORCEINLINE operator const ::Boost::rv&() const \ { return *ACTIV_BOOST_MOVE_TO_RV_CAST(const ::Boost::rv*, this); }\ private:\ // namespace Boost{ namespace move_detail{ template< class T> struct forward_type { typedef const T &type; }; template< class T> struct forward_type< Boost::rv > { typedef T type; }; }} #else //ACTIV_BOOST_NO_CXX11_RVALUE_REFERENCES //! This macro marks a type as movable but not copyable, disabling copy construction //! and assignment. The user will need to write a move constructor/assignment as explained //! in the documentation to fully write a movable but not copyable class. #define ACTIV_BOOST_MOVABLE_BUT_NOT_COPYABLE(TYPE)\ ACTIV_BOOST_MOVE_IMPL_NO_COPY_CTOR_OR_ASSIGN(TYPE)\ public:\ typedef int boost_move_emulation_t;\ private:\ // //! This macro marks a type as copyable and movable. //! The user will need to write a move constructor/assignment and a copy assignment //! as explained in the documentation to fully write a copyable and movable class. #define ACTIV_BOOST_COPYABLE_AND_MOVABLE(TYPE)\ // #if !defined(ACTIV_BOOST_MOVE_DOXYGEN_INVOKED) #define ACTIV_BOOST_COPYABLE_AND_MOVABLE_ALT(TYPE)\ // #endif //#if !defined(ACTIV_BOOST_MOVE_DOXYGEN_INVOKED) namespace Boost { //!This trait yields to a compile-time true boolean if T was marked as //!ACTIV_BOOST_MOVABLE_BUT_NOT_COPYABLE or ACTIV_BOOST_COPYABLE_AND_MOVABLE and //!rvalue references are not available on the platform. False otherwise. template struct has_move_emulation_enabled { static const bool value = false; }; template struct has_move_emulation_disabled { static const bool value = true; }; } //namespace Boost{ //!This macro is used to achieve portable syntax in move //!constructors and assignments for classes marked as //!ACTIV_BOOST_COPYABLE_AND_MOVABLE or ACTIV_BOOST_MOVABLE_BUT_NOT_COPYABLE #define ACTIV_BOOST_RV_REF(TYPE)\ TYPE && \ // //!This macro is used to achieve portable syntax in move //!constructors and assignments for template classes marked as //!ACTIV_BOOST_COPYABLE_AND_MOVABLE or ACTIV_BOOST_MOVABLE_BUT_NOT_COPYABLE. //!As macros have problems with comma-separated template arguments, //!the template argument must be preceded with ACTIV_BOOST_RV_REF_BEG //!and ended with ACTIV_BOOST_RV_REF_END #define ACTIV_BOOST_RV_REF_BEG\ \ // //!This macro is used to achieve portable syntax in move //!constructors and assignments for template classes marked as //!ACTIV_BOOST_COPYABLE_AND_MOVABLE or ACTIV_BOOST_MOVABLE_BUT_NOT_COPYABLE. //!As macros have problems with comma-separated template arguments, //!the template argument must be preceded with ACTIV_BOOST_RV_REF_BEG //!and ended with ACTIV_BOOST_RV_REF_END #define ACTIV_BOOST_RV_REF_END\ && \ // //!This macro expands to ACTIV_BOOST_RV_REF_BEG if ACTIV_BOOST_NO_CXX11_RVALUE_REFERENCES //!is not defined, empty otherwise #define ACTIV_BOOST_RV_REF_BEG_IF_CXX11 \ ACTIV_BOOST_RV_REF_BEG \ // //!This macro expands to ACTIV_BOOST_RV_REF_END if ACTIV_BOOST_NO_CXX11_RVALUE_REFERENCES //!is not defined, empty otherwise #define ACTIV_BOOST_RV_REF_END_IF_CXX11 \ ACTIV_BOOST_RV_REF_END \ // //!This macro is used to achieve portable syntax in copy //!assignment for classes marked as ACTIV_BOOST_COPYABLE_AND_MOVABLE. #define ACTIV_BOOST_COPY_ASSIGN_REF(TYPE)\ const TYPE & \ // //! This macro is used to implement portable perfect forwarding //! as explained in the documentation. #define ACTIV_BOOST_FWD_REF(TYPE)\ TYPE && \ // #if !defined(ACTIV_BOOST_MOVE_DOXYGEN_INVOKED) #define ACTIV_BOOST_RV_REF_2_TEMPL_ARGS(TYPE, ARG1, ARG2)\ TYPE && \ // #define ACTIV_BOOST_RV_REF_3_TEMPL_ARGS(TYPE, ARG1, ARG2, ARG3)\ TYPE && \ // #define ACTIV_BOOST_COPY_ASSIGN_REF_BEG \ const \ // #define ACTIV_BOOST_COPY_ASSIGN_REF_END \ & \ // #define ACTIV_BOOST_COPY_ASSIGN_REF_2_TEMPL_ARGS(TYPE, ARG1, ARG2)\ const TYPE & \ // #define ACTIV_BOOST_COPY_ASSIGN_REF_3_TEMPL_ARGS(TYPE, ARG1, ARG2, ARG3)\ const TYPE& \ // #define ACTIV_BOOST_CATCH_CONST_RLVALUE(TYPE)\ const TYPE & \ // #endif //#if !defined(ACTIV_BOOST_MOVE_DOXYGEN_INVOKED) #if !defined(ACTIV_BOOST_MOVE_MSVC_AUTO_MOVE_RETURN_BUG) || defined(ACTIV_BOOST_MOVE_DOXYGEN_INVOKED) //!This macro is used to achieve portable move return semantics. //!The C++11 Standard allows implicit move returns when the object to be returned //!is designated by a lvalue and: //! - The criteria for elision of a copy operation are met OR //! - The criteria would be met save for the fact that the source object is a function parameter //! //!For C++11 conforming compilers this macros only yields to REF: //! return ACTIV_BOOST_MOVE_RET(RET_TYPE, REF); -> return REF; //! //!For compilers without rvalue references //!this macro does an explicit move if the move emulation is activated //!and the return type (RET_TYPE) is not a reference. //! //!For non-conforming compilers with rvalue references like Visual 2010 & 2012, //!an explicit move is performed if RET_TYPE is not a reference. //! //! Caution: When using this macro in non-conforming or C++03 //!compilers, a move will be performed even if the C++11 standard does not allow it //!(e.g. returning a static variable). The user is responsible for using this macro //!only to return local objects that met C++11 criteria. #define ACTIV_BOOST_MOVE_RET(RET_TYPE, REF)\ REF // #else //!defined(ACTIV_BOOST_MOVE_MSVC_AUTO_MOVE_RETURN_BUG) || defined(ACTIV_BOOST_MOVE_DOXYGEN_INVOKED) #include namespace Boost { namespace move_detail { template ACTIV_BOOST_MOVE_FORCEINLINE typename ::Boost::move_detail::enable_if_c < ::Boost::move_detail::is_lvalue_reference::value , T&>::type move_return(T& x) ACTIV_BOOST_NOEXCEPT { return x; } template ACTIV_BOOST_MOVE_FORCEINLINE typename ::Boost::move_detail::enable_if_c < !::Boost::move_detail::is_lvalue_reference::value , Ret && >::type move_return(T&& t) ACTIV_BOOST_NOEXCEPT { return static_cast< Ret&& >(t); } } //namespace move_detail { } //namespace Boost { #define ACTIV_BOOST_MOVE_RET(RET_TYPE, REF)\ Boost::move_detail::move_return< RET_TYPE >(REF) // #endif //!defined(ACTIV_BOOST_MOVE_MSVC_AUTO_MOVE_RETURN_BUG) || defined(ACTIV_BOOST_MOVE_DOXYGEN_INVOKED) //!This macro is used to achieve portable optimal move constructors. //! //!When implementing the move constructor, in C++03 compilers the moved-from argument must be //!cast to the base type before calling `::Boost::move()` due to rvalue reference limitations. //! //!In C++11 compilers the cast from a rvalue reference of a derived type to a rvalue reference of //!a base type is implicit. #define ACTIV_BOOST_MOVE_BASE(BASE_TYPE, ARG) \ ::Boost::move((BASE_TYPE&)(ARG)) // namespace Boost { namespace move_detail { template< class T> struct forward_type { typedef T type; }; }} #endif //ACTIV_BOOST_NO_CXX11_RVALUE_REFERENCES #include #endif //#ifndef ACTIV_BOOST_MOVE_CORE_HPP