1 //-----------------------------------------------------------------------------
2 // boost variant/get.hpp header file
3 // See http://www.boost.org for updates, documentation, and revision history.
4 //-----------------------------------------------------------------------------
7 // Eric Friedman, Itay Maman
9 // Distributed under the Boost Software License, Version 1.0. (See
10 // accompanying file LICENSE_1_0.txt or copy at
11 // http://www.boost.org/LICENSE_1_0.txt)
13 #ifndef BOOST_VARIANT_GET_HPP
14 #define BOOST_VARIANT_GET_HPP
18 #include "boost/config.hpp"
19 #include "boost/detail/workaround.hpp"
20 #include "boost/utility/addressof.hpp"
21 #include "boost/variant/variant_fwd.hpp"
23 #include "boost/type_traits/add_reference.hpp"
24 #include "boost/type_traits/add_pointer.hpp"
26 #if BOOST_WORKAROUND(BOOST_MSVC, < 1300)
27 # include "boost/mpl/bool.hpp"
28 # include "boost/mpl/or.hpp"
29 # include "boost/type_traits/is_same.hpp"
34 //////////////////////////////////////////////////////////////////////////
37 // The exception thrown in the event of a failed get of a value.
40 : public std::exception
42 public: // std::exception implementation
44 virtual const char * what() const throw()
46 return "boost::bad_get: "
47 "failed value get using boost::get";
52 //////////////////////////////////////////////////////////////////////////
53 // function template get<T>
55 // Retrieves content of given variant object if content is of type T.
56 // Otherwise: pointer ver. returns 0; reference ver. throws bad_get.
59 namespace detail
{ namespace variant
{
61 // (detail) class template get_visitor
63 // Generic static visitor that: if the value is of the specified type,
64 // returns a pointer to the value it visits; else a null pointer.
69 private: // private typedefs
71 typedef typename add_pointer
<T
>::type pointer
;
72 typedef typename add_reference
<T
>::type reference
;
74 public: // visitor typedefs
76 typedef pointer result_type
;
78 public: // visitor interfaces
80 #if !BOOST_WORKAROUND(BOOST_MSVC, < 1300)
82 pointer
operator()(reference operand
) const
84 return boost::addressof(operand
);
88 pointer
operator()(const U
&) const
90 return static_cast<pointer
>(0);
95 private: // helpers, for visitor interfaces (below)
97 pointer
execute_impl(reference operand
, mpl::true_
) const
99 return boost::addressof(operand
);
102 template <typename U
>
103 pointer
execute_impl(const U
& operand
, mpl::false_
) const
105 return static_cast<pointer
>(0);
108 public: // visitor interfaces
110 template <typename U
>
111 pointer
operator()(U
& operand
) const
113 // MSVC6 finds normal implementation (above) ambiguous,
114 // so we must explicitly disambiguate
116 typedef typename
mpl::or_
<
118 , is_same
<const U
, T
>
121 return execute_impl(operand
, U_is_T());
124 #endif // MSVC6 workaround
128 }} // namespace detail::variant
130 #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0551))
131 # define BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(t) \
132 BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE(t)
134 # define BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(t) \
138 template <typename U
, BOOST_VARIANT_ENUM_PARAMS(typename T
) >
140 typename add_pointer
<U
>::type
142 boost::variant
< BOOST_VARIANT_ENUM_PARAMS(T
) >* operand
143 BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U
)
146 typedef typename add_pointer
<U
>::type U_ptr
;
147 if (!operand
) return static_cast<U_ptr
>(0);
149 detail::variant::get_visitor
<U
> v
;
150 return operand
->apply_visitor(v
);
153 template <typename U
, BOOST_VARIANT_ENUM_PARAMS(typename T
) >
155 typename add_pointer
<const U
>::type
157 const boost::variant
< BOOST_VARIANT_ENUM_PARAMS(T
) >* operand
158 BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U
)
161 typedef typename add_pointer
<const U
>::type U_ptr
;
162 if (!operand
) return static_cast<U_ptr
>(0);
164 detail::variant::get_visitor
<const U
> v
;
165 return operand
->apply_visitor(v
);
168 template <typename U
, BOOST_VARIANT_ENUM_PARAMS(typename T
) >
170 typename add_reference
<U
>::type
172 boost::variant
< BOOST_VARIANT_ENUM_PARAMS(T
) >& operand
173 BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U
)
176 typedef typename add_pointer
<U
>::type U_ptr
;
177 U_ptr result
= get
<U
>(&operand
);
184 template <typename U
, BOOST_VARIANT_ENUM_PARAMS(typename T
) >
186 typename add_reference
<const U
>::type
188 const boost::variant
< BOOST_VARIANT_ENUM_PARAMS(T
) >& operand
189 BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U
)
192 typedef typename add_pointer
<const U
>::type U_ptr
;
193 U_ptr result
= get
<const U
>(&operand
);
202 #endif // BOOST_VARIANT_GET_HPP