Chris@16: //----------------------------------------------------------------------------- Chris@16: // boost variant/get.hpp header file Chris@16: // See http://www.boost.org for updates, documentation, and revision history. Chris@16: //----------------------------------------------------------------------------- Chris@16: // Chris@101: // Copyright (c) 2003 Eric Friedman, Itay Maman Chris@101: // Copyright (c) 2014 Antony Polukhin Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: #ifndef BOOST_VARIANT_GET_HPP Chris@16: #define BOOST_VARIANT_GET_HPP Chris@16: Chris@16: #include Chris@16: Chris@16: #include "boost/config.hpp" Chris@16: #include "boost/detail/workaround.hpp" Chris@101: #include "boost/static_assert.hpp" Chris@16: #include "boost/throw_exception.hpp" Chris@16: #include "boost/utility/addressof.hpp" Chris@16: #include "boost/variant/variant_fwd.hpp" Chris@101: #include "boost/variant/detail/element_index.hpp" Chris@16: Chris@16: #include "boost/type_traits/add_reference.hpp" Chris@16: #include "boost/type_traits/add_pointer.hpp" Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: ////////////////////////////////////////////////////////////////////////// Chris@16: // class bad_get Chris@16: // Chris@16: // The exception thrown in the event of a failed get of a value. Chris@16: // Chris@101: class BOOST_SYMBOL_VISIBLE bad_get Chris@16: : public std::exception Chris@16: { Chris@16: public: // std::exception implementation Chris@16: Chris@16: virtual const char * what() const BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { Chris@16: return "boost::bad_get: " Chris@16: "failed value get using boost::get"; Chris@16: } Chris@16: Chris@16: }; Chris@16: Chris@16: ////////////////////////////////////////////////////////////////////////// Chris@16: // function template get Chris@16: // Chris@16: // Retrieves content of given variant object if content is of type T. Chris@16: // Otherwise: pointer ver. returns 0; reference ver. throws bad_get. Chris@16: // Chris@16: Chris@16: namespace detail { namespace variant { Chris@16: Chris@16: // (detail) class template get_visitor Chris@16: // Chris@16: // Generic static visitor that: if the value is of the specified type, Chris@16: // returns a pointer to the value it visits; else a null pointer. Chris@16: // Chris@16: template Chris@16: struct get_visitor Chris@16: { Chris@16: private: // private typedefs Chris@16: Chris@16: typedef typename add_pointer::type pointer; Chris@16: typedef typename add_reference::type reference; Chris@16: Chris@16: public: // visitor typedefs Chris@16: Chris@16: typedef pointer result_type; Chris@16: Chris@16: public: // visitor interfaces Chris@16: Chris@101: pointer operator()(reference operand) const BOOST_NOEXCEPT Chris@16: { Chris@16: return boost::addressof(operand); Chris@16: } Chris@16: Chris@16: template Chris@101: pointer operator()(const U&) const BOOST_NOEXCEPT Chris@16: { Chris@16: return static_cast(0); Chris@16: } Chris@16: }; Chris@16: Chris@16: }} // namespace detail::variant Chris@16: Chris@101: #ifndef BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE Chris@101: # if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0551)) Chris@101: # define BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(t) Chris@101: # else Chris@101: # define BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(t) \ Chris@101: , t* = 0 Chris@101: # endif Chris@16: #endif Chris@16: Chris@101: ///////////////////////////////////////////////////////////////////////////////////////////////////////////// Chris@101: // relaxed_get(variant) methods Chris@101: // Chris@16: template Chris@16: inline Chris@16: typename add_pointer::type Chris@101: relaxed_get( Chris@16: boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >* operand Chris@16: BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U) Chris@101: ) BOOST_NOEXCEPT Chris@16: { Chris@16: typedef typename add_pointer::type U_ptr; Chris@16: if (!operand) return static_cast(0); Chris@16: Chris@16: detail::variant::get_visitor v; Chris@16: return operand->apply_visitor(v); Chris@16: } Chris@16: Chris@16: template Chris@16: inline Chris@16: typename add_pointer::type Chris@101: relaxed_get( Chris@16: const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >* operand Chris@16: BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U) Chris@101: ) BOOST_NOEXCEPT Chris@16: { Chris@16: typedef typename add_pointer::type U_ptr; Chris@16: if (!operand) return static_cast(0); Chris@16: Chris@16: detail::variant::get_visitor v; Chris@16: return operand->apply_visitor(v); Chris@16: } Chris@16: Chris@16: template Chris@16: inline Chris@16: typename add_reference::type Chris@101: relaxed_get( Chris@101: boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& operand Chris@101: BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U) Chris@101: ) Chris@101: { Chris@101: typedef typename add_pointer::type U_ptr; Chris@101: U_ptr result = relaxed_get(&operand); Chris@101: Chris@101: if (!result) Chris@101: boost::throw_exception(bad_get()); Chris@101: return *result; Chris@101: } Chris@101: Chris@101: template Chris@101: inline Chris@101: typename add_reference::type Chris@101: relaxed_get( Chris@101: const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& operand Chris@101: BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U) Chris@101: ) Chris@101: { Chris@101: typedef typename add_pointer::type U_ptr; Chris@101: U_ptr result = relaxed_get(&operand); Chris@101: Chris@101: if (!result) Chris@101: boost::throw_exception(bad_get()); Chris@101: return *result; Chris@101: } Chris@101: Chris@101: Chris@101: Chris@101: ///////////////////////////////////////////////////////////////////////////////////////////////////////////// Chris@101: // strict_get(variant) methods Chris@101: // Chris@101: template Chris@101: inline Chris@101: typename add_pointer::type Chris@101: strict_get( Chris@101: boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >* operand Chris@101: BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U) Chris@101: ) BOOST_NOEXCEPT Chris@101: { Chris@101: BOOST_STATIC_ASSERT_MSG( Chris@101: (boost::detail::variant::holds_element, U >::value), Chris@101: "boost::variant does not contain specified type U, " Chris@101: "call to boost::get(boost::variant*) will always return NULL" Chris@101: ); Chris@101: Chris@101: return relaxed_get(operand); Chris@101: } Chris@101: Chris@101: template Chris@101: inline Chris@101: typename add_pointer::type Chris@101: strict_get( Chris@101: const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >* operand Chris@101: BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U) Chris@101: ) BOOST_NOEXCEPT Chris@101: { Chris@101: BOOST_STATIC_ASSERT_MSG( Chris@101: (boost::detail::variant::holds_element, const U >::value), Chris@101: "boost::variant does not contain specified type U, " Chris@101: "call to boost::get(const boost::variant*) will always return NULL" Chris@101: ); Chris@101: Chris@101: return relaxed_get(operand); Chris@101: } Chris@101: Chris@101: template Chris@101: inline Chris@101: typename add_reference::type Chris@101: strict_get( Chris@101: boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& operand Chris@101: BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U) Chris@101: ) Chris@101: { Chris@101: BOOST_STATIC_ASSERT_MSG( Chris@101: (boost::detail::variant::holds_element, U >::value), Chris@101: "boost::variant does not contain specified type U, " Chris@101: "call to boost::get(boost::variant&) will always throw boost::bad_get exception" Chris@101: ); Chris@101: Chris@101: return relaxed_get(operand); Chris@101: } Chris@101: Chris@101: template Chris@101: inline Chris@101: typename add_reference::type Chris@101: strict_get( Chris@101: const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& operand Chris@101: BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U) Chris@101: ) Chris@101: { Chris@101: BOOST_STATIC_ASSERT_MSG( Chris@101: (boost::detail::variant::holds_element, const U >::value), Chris@101: "boost::variant does not contain specified type U, " Chris@101: "call to boost::get(const boost::variant&) will always throw boost::bad_get exception" Chris@101: ); Chris@101: Chris@101: return relaxed_get(operand); Chris@101: } Chris@101: Chris@101: ///////////////////////////////////////////////////////////////////////////////////////////////////////////// Chris@101: // get(variant) methods Chris@101: // Chris@101: Chris@101: template Chris@101: inline Chris@101: typename add_pointer::type Chris@101: get( Chris@101: boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >* operand Chris@101: BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U) Chris@101: ) BOOST_NOEXCEPT Chris@101: { Chris@101: #ifdef BOOST_VARIANT_USE_RELAXED_GET_BY_DEFAULT Chris@101: return relaxed_get(operand); Chris@101: #else Chris@101: return strict_get(operand); Chris@101: #endif Chris@101: Chris@101: } Chris@101: Chris@101: template Chris@101: inline Chris@101: typename add_pointer::type Chris@101: get( Chris@101: const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >* operand Chris@101: BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U) Chris@101: ) BOOST_NOEXCEPT Chris@101: { Chris@101: #ifdef BOOST_VARIANT_USE_RELAXED_GET_BY_DEFAULT Chris@101: return relaxed_get(operand); Chris@101: #else Chris@101: return strict_get(operand); Chris@101: #endif Chris@101: } Chris@101: Chris@101: template Chris@101: inline Chris@101: typename add_reference::type Chris@16: get( Chris@16: boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& operand Chris@16: BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U) Chris@16: ) Chris@16: { Chris@101: #ifdef BOOST_VARIANT_USE_RELAXED_GET_BY_DEFAULT Chris@101: return relaxed_get(operand); Chris@101: #else Chris@101: return strict_get(operand); Chris@101: #endif Chris@16: } Chris@16: Chris@16: template Chris@16: inline Chris@16: typename add_reference::type Chris@16: get( Chris@16: const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& operand Chris@16: BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U) Chris@16: ) Chris@16: { Chris@101: #ifdef BOOST_VARIANT_USE_RELAXED_GET_BY_DEFAULT Chris@101: return relaxed_get(operand); Chris@101: #else Chris@101: return strict_get(operand); Chris@101: #endif Chris@16: } Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #endif // BOOST_VARIANT_GET_HPP