//----------------------------------------------------------------------------- // boost variant/polymorphic_get.hpp header file // See http://www.boost.org for updates, documentation, and revision history. //----------------------------------------------------------------------------- // // Copyright (c) 2013-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_POLYMORPHIC_GET_HPP #define ACTIV_BOOST_VARIANT_POLYMORPHIC_GET_HPP #include #include #include #include #include #include #include #include #include #include #include #include namespace Boost { ////////////////////////////////////////////////////////////////////////// // class bad_polymorphic_get // // The exception thrown in the event of a failed get of a value. // class ACTIV_BOOST_SYMBOL_VISIBLE bad_polymorphic_get : public bad_get { public: // std::exception implementation virtual const char * what() const ACTIV_BOOST_NOEXCEPT_OR_NOTHROW { return "Boost::bad_polymorphic_get: " "failed value get using Boost::polymorphic_get"; } }; ////////////////////////////////////////////////////////////////////////// // 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 { /////////////////////////////////////////////////////////////////////////////////////////////////// // polymorphic metafunctions to detect index of a value // template struct element_polymorphic_iterator_impl : Boost::mpl::find_if< Types, Boost::mpl::or_< variant_element_functor, variant_element_functor::type >, Boost::is_base_of > > {}; template struct holds_element_polymorphic : Boost::mpl::not_< Boost::is_same< typename Boost::mpl::end::type, typename element_polymorphic_iterator_impl::type >::type > > {}; // (detail) class template get_polymorphic_visitor // // Generic static visitor that: if the value is of the specified // type or of a type derived from specified, returns a pointer // to the value it visits; else a null pointer. // template struct get_polymorphic_visitor { private: // private typedefs typedef get_polymorphic_visitor this_type; typedef typename add_pointer::type pointer; typedef typename add_reference::type reference; pointer get(reference operand, Boost::true_type) const ACTIV_BOOST_NOEXCEPT { return Boost::addressof(operand); } template pointer get(T&, Boost::false_type) const ACTIV_BOOST_NOEXCEPT { return static_cast(0); } public: // visitor interfaces typedef pointer result_type; template pointer operator()(U& operand) const ACTIV_BOOST_NOEXCEPT { typedef typename Boost::remove_reference::type base_t; typedef Boost::integral_constant< bool, ( Boost::is_base_of::value && (Boost::is_const::value || !Boost::is_const::value) ) || Boost::is_same::value || Boost::is_same::type, U >::value > tag_t; return this_type::get(operand, tag_t()); } }; }} // 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 ////////////////////////////////////////////////////////////////////////////////////////////////////////// // polymorphic_relaxed_get // template inline typename add_pointer::type polymorphic_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_polymorphic_visitor v; return operand->apply_visitor(v); } template inline typename add_pointer::type polymorphic_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_polymorphic_visitor v; return operand->apply_visitor(v); } template inline typename add_reference::type polymorphic_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 = polymorphic_relaxed_get(&operand); if (!result) Boost::throw_exception(bad_polymorphic_get()); return *result; } template inline typename add_reference::type polymorphic_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 = polymorphic_relaxed_get(&operand); if (!result) Boost::throw_exception(bad_polymorphic_get()); return *result; } ////////////////////////////////////////////////////////////////////////////////////////////////////////// // polymorphic_strict_get // template inline typename add_pointer::type polymorphic_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_polymorphic, U >::value), "Boost::variant does not contain specified type U, " "call to Boost::polymorphic_get(Boost::variant*) will always return NULL" ); return polymorphic_relaxed_get(operand); } template inline typename add_pointer::type polymorphic_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_polymorphic, U >::value), "Boost::variant does not contain specified type U, " "call to Boost::polymorphic_get(const Boost::variant*) will always return NULL" ); return polymorphic_relaxed_get(operand); } template inline typename add_reference::type polymorphic_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_polymorphic, U >::value), "Boost::variant does not contain specified type U, " "call to Boost::polymorphic_get(Boost::variant&) will always throw Boost::bad_polymorphic_get exception" ); return polymorphic_relaxed_get(operand); } template inline typename add_reference::type polymorphic_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_polymorphic, U >::value), "Boost::variant does not contain specified type U, " "call to Boost::polymorphic_get(const Boost::variant&) will always throw Boost::bad_polymorphic_get exception" ); return polymorphic_relaxed_get(operand); } ///////////////////////////////////////////////////////////////////////////////////////////////////////////// // polymorphic_get(variant) methods // template inline typename add_pointer::type polymorphic_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 polymorphic_relaxed_get(operand); #else return polymorphic_strict_get(operand); #endif } template inline typename add_pointer::type polymorphic_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 polymorphic_relaxed_get(operand); #else return polymorphic_strict_get(operand); #endif } template inline typename add_reference::type polymorphic_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 polymorphic_relaxed_get(operand); #else return polymorphic_strict_get(operand); #endif } template inline typename add_reference::type polymorphic_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 polymorphic_relaxed_get(operand); #else return polymorphic_strict_get(operand); #endif } } // namespace Boost #endif // ACTIV_BOOST_VARIANT_POLYMORPHIC_GET_HPP