//----------------------------------------------------------------------------- // boost variant/get.hpp header file // See http://www.boost.org for updates, documentation, and revision history. //----------------------------------------------------------------------------- // // Copyright (c) 2003 Eric Friedman, Itay Maman // Copyright (c) 2014-2021 Antony Polukhin // // 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) #ifndef ACTIV_BOOST_VARIANT_GET_HPP #define ACTIV_BOOST_VARIANT_GET_HPP #include #include #include #include #include #include #include #include #include #include #include #include namespace Boost { #if defined(ACTIV_BOOST_CLANG) # pragma clang diagnostic push # pragma clang diagnostic ignored "-Wweak-vtables" #endif ////////////////////////////////////////////////////////////////////////// // class bad_get // // The exception thrown in the event of a failed get of a value. // class ACTIV_BOOST_SYMBOL_VISIBLE bad_get : public std::exception { public: // std::exception implementation const char * what() const ACTIV_BOOST_NOEXCEPT_OR_NOTHROW ACTIV_BOOST_OVERRIDE { return "Boost::bad_get: " "failed value get using Boost::get"; } }; #if defined(ACTIV_BOOST_CLANG) # pragma clang diagnostic pop #endif ////////////////////////////////////////////////////////////////////////// // function template get // // Retrieves content of given variant object if content is of type T. // Otherwise: pointer ver. returns 0; reference ver. throws bad_get. // namespace detail { namespace variant { // (detail) class template get_visitor // // Generic static visitor that: if the value is of the specified type, // returns a pointer to the value it visits; else a null pointer. // template struct get_visitor { private: // private typedefs typedef typename add_pointer::type pointer; typedef typename add_reference::type reference; public: // visitor typedefs typedef pointer result_type; public: // visitor interfaces pointer operator()(reference operand) const ACTIV_BOOST_NOEXCEPT { return Boost::addressof(operand); } template pointer operator()(const U&) const ACTIV_BOOST_NOEXCEPT { return static_cast(0); } }; }} // namespace detail::variant #ifndef ACTIV_BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE # if !ACTIV_BOOST_WORKAROUND(ACTIV_BOOST_BORLANDC, ACTIV_BOOST_TESTED_AT(0x0551)) # define ACTIV_BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(t) # else # if defined(ACTIV_BOOST_NO_CXX11_NULLPTR) # define ACTIV_BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(t) \ , t* = 0 # else # define ACTIV_BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(t) \ , t* = nullptr # endif # endif #endif ///////////////////////////////////////////////////////////////////////////////////////////////////////////// // relaxed_get(variant) methods // template inline typename add_pointer::type relaxed_get( Boost::variant< ACTIV_BOOST_VARIANT_ENUM_PARAMS(T) >* operand ACTIV_BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U) ) ACTIV_BOOST_NOEXCEPT { typedef typename add_pointer::type U_ptr; if (!operand) return static_cast(0); detail::variant::get_visitor v; return operand->apply_visitor(v); } template inline typename add_pointer::type relaxed_get( const Boost::variant< ACTIV_BOOST_VARIANT_ENUM_PARAMS(T) >* operand ACTIV_BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U) ) ACTIV_BOOST_NOEXCEPT { typedef typename add_pointer::type U_ptr; if (!operand) return static_cast(0); detail::variant::get_visitor v; return operand->apply_visitor(v); } template inline typename add_reference::type relaxed_get( Boost::variant< ACTIV_BOOST_VARIANT_ENUM_PARAMS(T) >& operand ACTIV_BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U) ) { typedef typename add_pointer::type U_ptr; U_ptr result = relaxed_get(Boost::addressof(operand)); if (!result) Boost::throw_exception(bad_get()); return *result; } template inline typename add_reference::type relaxed_get( const Boost::variant< ACTIV_BOOST_VARIANT_ENUM_PARAMS(T) >& operand ACTIV_BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U) ) { typedef typename add_pointer::type U_ptr; U_ptr result = relaxed_get(Boost::addressof(operand)); if (!result) Boost::throw_exception(bad_get()); return *result; } #ifndef ACTIV_BOOST_NO_CXX11_RVALUE_REFERENCES #if defined(ACTIV_BOOST_MSVC) && (_MSC_VER < 1900) // MSVC-2014 has fixed the incorrect diagnostics. # pragma warning(push) # pragma warning(disable: 4172) // returning address of local variable or temporary #endif template inline U&& relaxed_get( Boost::variant< ACTIV_BOOST_VARIANT_ENUM_PARAMS(T) >&& operand ACTIV_BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U) ) { typedef typename add_pointer::type U_ptr; U_ptr result = relaxed_get(Boost::addressof(operand)); if (!result) Boost::throw_exception(bad_get()); return static_cast(*result); } #if defined(ACTIV_BOOST_MSVC) && (_MSC_VER < 1900) # pragma warning(pop) #endif #endif ///////////////////////////////////////////////////////////////////////////////////////////////////////////// // strict_get(variant) methods // template inline typename add_pointer::type strict_get( Boost::variant< ACTIV_BOOST_VARIANT_ENUM_PARAMS(T) >* operand ACTIV_BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U) ) ACTIV_BOOST_NOEXCEPT { ACTIV_BOOST_STATIC_ASSERT_MSG( (Boost::detail::variant::holds_element, U >::value), "Boost::variant does not contain specified type U, " "call to Boost::get(Boost::variant*) will always return NULL" ); return relaxed_get(operand); } template inline typename add_pointer::type strict_get( const Boost::variant< ACTIV_BOOST_VARIANT_ENUM_PARAMS(T) >* operand ACTIV_BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U) ) ACTIV_BOOST_NOEXCEPT { ACTIV_BOOST_STATIC_ASSERT_MSG( (Boost::detail::variant::holds_element, const U >::value), "Boost::variant does not contain specified type U, " "call to Boost::get(const Boost::variant*) will always return NULL" ); return relaxed_get(operand); } template inline typename add_reference::type strict_get( Boost::variant< ACTIV_BOOST_VARIANT_ENUM_PARAMS(T) >& operand ACTIV_BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U) ) { ACTIV_BOOST_STATIC_ASSERT_MSG( (Boost::detail::variant::holds_element, U >::value), "Boost::variant does not contain specified type U, " "call to Boost::get(Boost::variant&) will always throw Boost::bad_get exception" ); return relaxed_get(operand); } template inline typename add_reference::type strict_get( const Boost::variant< ACTIV_BOOST_VARIANT_ENUM_PARAMS(T) >& operand ACTIV_BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U) ) { ACTIV_BOOST_STATIC_ASSERT_MSG( (Boost::detail::variant::holds_element, const U >::value), "Boost::variant does not contain specified type U, " "call to Boost::get(const Boost::variant&) will always throw Boost::bad_get exception" ); return relaxed_get(operand); } #ifndef ACTIV_BOOST_NO_CXX11_RVALUE_REFERENCES template inline U&& strict_get( Boost::variant< ACTIV_BOOST_VARIANT_ENUM_PARAMS(T) >&& operand ACTIV_BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U) ) { ACTIV_BOOST_STATIC_ASSERT_MSG( (!Boost::is_lvalue_reference::value), "remove ampersand '&' from template type U in Boost::get(Boost::variant&&) " ); ACTIV_BOOST_STATIC_ASSERT_MSG( (Boost::detail::variant::holds_element, U >::value), "Boost::variant does not contain specified type U, " "call to Boost::get(const Boost::variant&) will always throw Boost::bad_get exception" ); return relaxed_get(detail::variant::move(operand)); } #endif ///////////////////////////////////////////////////////////////////////////////////////////////////////////// // get(variant) methods // template inline typename add_pointer::type get( Boost::variant< ACTIV_BOOST_VARIANT_ENUM_PARAMS(T) >* operand ACTIV_BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U) ) ACTIV_BOOST_NOEXCEPT { #ifdef ACTIV_BOOST_VARIANT_USE_RELAXED_GET_BY_DEFAULT return relaxed_get(operand); #else return strict_get(operand); #endif } template inline typename add_pointer::type get( const Boost::variant< ACTIV_BOOST_VARIANT_ENUM_PARAMS(T) >* operand ACTIV_BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U) ) ACTIV_BOOST_NOEXCEPT { #ifdef ACTIV_BOOST_VARIANT_USE_RELAXED_GET_BY_DEFAULT return relaxed_get(operand); #else return strict_get(operand); #endif } template inline typename add_reference::type get( Boost::variant< ACTIV_BOOST_VARIANT_ENUM_PARAMS(T) >& operand ACTIV_BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U) ) { #ifdef ACTIV_BOOST_VARIANT_USE_RELAXED_GET_BY_DEFAULT return relaxed_get(operand); #else return strict_get(operand); #endif } template inline typename add_reference::type get( const Boost::variant< ACTIV_BOOST_VARIANT_ENUM_PARAMS(T) >& operand ACTIV_BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U) ) { #ifdef ACTIV_BOOST_VARIANT_USE_RELAXED_GET_BY_DEFAULT return relaxed_get(operand); #else return strict_get(operand); #endif } #ifndef ACTIV_BOOST_NO_CXX11_RVALUE_REFERENCES template inline U&& get( Boost::variant< ACTIV_BOOST_VARIANT_ENUM_PARAMS(T) >&& operand ACTIV_BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U) ) { #ifdef ACTIV_BOOST_VARIANT_USE_RELAXED_GET_BY_DEFAULT return relaxed_get(detail::variant::move(operand)); #else return strict_get(detail::variant::move(operand)); #endif } #endif } // namespace Boost #endif // ACTIV_BOOST_VARIANT_GET_HPP