1 //-----------------------------------------------------------------------------
2 // boost variant/polymorphic_get.hpp header file
3 // See http://www.boost.org for updates, documentation, and revision history.
4 //-----------------------------------------------------------------------------
6 // Copyright (c) 2013-2017 Antony Polukhin
8 // Distributed under the Boost Software License, Version 1.0. (See
9 // accompanying file LICENSE_1_0.txt or copy at
10 // http://www.boost.org/LICENSE_1_0.txt)
12 #ifndef BOOST_VARIANT_POLYMORPHIC_GET_HPP
13 #define BOOST_VARIANT_POLYMORPHIC_GET_HPP
17 #include <boost/config.hpp>
18 #include <boost/detail/workaround.hpp>
19 #include <boost/static_assert.hpp>
20 #include <boost/throw_exception.hpp>
21 #include <boost/utility/addressof.hpp>
22 #include <boost/variant/variant_fwd.hpp>
23 #include <boost/variant/get.hpp>
25 #include <boost/type_traits/add_reference.hpp>
26 #include <boost/type_traits/add_pointer.hpp>
27 #include <boost/type_traits/is_base_of.hpp>
28 #include <boost/type_traits/is_const.hpp>
32 //////////////////////////////////////////////////////////////////////////
33 // class bad_polymorphic_get
35 // The exception thrown in the event of a failed get of a value.
37 class BOOST_SYMBOL_VISIBLE bad_polymorphic_get
40 public: // std::exception implementation
42 virtual const char * what() const BOOST_NOEXCEPT_OR_NOTHROW
44 return "boost::bad_polymorphic_get: "
45 "failed value get using boost::polymorphic_get";
50 //////////////////////////////////////////////////////////////////////////
51 // function template get<T>
53 // Retrieves content of given variant object if content is of type T.
54 // Otherwise: pointer ver. returns 0; reference ver. throws bad_get.
57 namespace detail { namespace variant {
60 ///////////////////////////////////////////////////////////////////////////////////////////////////
61 // polymorphic metafunctions to detect index of a value
64 template <class Types, class T>
65 struct element_polymorphic_iterator_impl :
69 variant_element_functor<boost::mpl::_1, T>,
70 variant_element_functor<boost::mpl::_1, typename boost::remove_cv<T>::type >,
71 boost::is_base_of<T, boost::mpl::_1>
76 template <class Variant, class T>
77 struct holds_element_polymorphic :
80 typename boost::mpl::end<typename Variant::types>::type,
81 typename element_polymorphic_iterator_impl<typename Variant::types, typename boost::remove_reference<T>::type >::type
86 // (detail) class template get_polymorphic_visitor
88 // Generic static visitor that: if the value is of the specified
89 // type or of a type derived from specified, returns a pointer
90 // to the value it visits; else a null pointer.
92 template <typename Base>
93 struct get_polymorphic_visitor
95 private: // private typedefs
96 typedef get_polymorphic_visitor<Base> this_type;
97 typedef typename add_pointer<Base>::type pointer;
98 typedef typename add_reference<Base>::type reference;
100 pointer get(reference operand, boost::true_type) const BOOST_NOEXCEPT
102 return boost::addressof(operand);
106 pointer get(T&, boost::false_type) const BOOST_NOEXCEPT
108 return static_cast<pointer>(0);
111 public: // visitor interfaces
112 typedef pointer result_type;
114 template <typename U>
115 pointer operator()(U& operand) const BOOST_NOEXCEPT
117 typedef typename boost::remove_reference<Base>::type base_t;
118 typedef boost::integral_constant<
121 boost::is_base_of<base_t, U>::value &&
122 (boost::is_const<base_t>::value || !boost::is_const<U>::value)
124 || boost::is_same<base_t, U>::value
125 || boost::is_same<typename boost::remove_cv<base_t>::type, U >::value
128 return this_type::get(operand, tag_t());
132 }} // namespace detail::variant
134 #ifndef BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE
135 # if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0551))
136 # define BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(t)
138 # define BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(t) \
143 //////////////////////////////////////////////////////////////////////////////////////////////////////////
144 // polymorphic_relaxed_get
147 template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
149 typename add_pointer<U>::type
150 polymorphic_relaxed_get(
151 boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >* operand
152 BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
155 typedef typename add_pointer<U>::type U_ptr;
156 if (!operand) return static_cast<U_ptr>(0);
158 detail::variant::get_polymorphic_visitor<U> v;
159 return operand->apply_visitor(v);
162 template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
164 typename add_pointer<const U>::type
165 polymorphic_relaxed_get(
166 const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >* operand
167 BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
170 typedef typename add_pointer<const U>::type U_ptr;
171 if (!operand) return static_cast<U_ptr>(0);
173 detail::variant::get_polymorphic_visitor<const U> v;
174 return operand->apply_visitor(v);
177 template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
179 typename add_reference<U>::type
180 polymorphic_relaxed_get(
181 boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& operand
182 BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
185 typedef typename add_pointer<U>::type U_ptr;
186 U_ptr result = polymorphic_relaxed_get<U>(&operand);
189 boost::throw_exception(bad_polymorphic_get());
193 template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
195 typename add_reference<const U>::type
196 polymorphic_relaxed_get(
197 const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& operand
198 BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
201 typedef typename add_pointer<const U>::type U_ptr;
202 U_ptr result = polymorphic_relaxed_get<const U>(&operand);
205 boost::throw_exception(bad_polymorphic_get());
209 //////////////////////////////////////////////////////////////////////////////////////////////////////////
210 // polymorphic_strict_get
213 template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
215 typename add_pointer<U>::type
216 polymorphic_strict_get(
217 boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >* operand
218 BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
221 BOOST_STATIC_ASSERT_MSG(
222 (boost::detail::variant::holds_element_polymorphic<boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >, U >::value),
223 "boost::variant does not contain specified type U, "
224 "call to boost::polymorphic_get<U>(boost::variant<T...>*) will always return NULL"
227 return polymorphic_relaxed_get<U>(operand);
230 template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
232 typename add_pointer<const U>::type
233 polymorphic_strict_get(
234 const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >* operand
235 BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
238 BOOST_STATIC_ASSERT_MSG(
239 (boost::detail::variant::holds_element_polymorphic<boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >, U >::value),
240 "boost::variant does not contain specified type U, "
241 "call to boost::polymorphic_get<U>(const boost::variant<T...>*) will always return NULL"
244 return polymorphic_relaxed_get<U>(operand);
247 template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
249 typename add_reference<U>::type
250 polymorphic_strict_get(
251 boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& operand
252 BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
255 BOOST_STATIC_ASSERT_MSG(
256 (boost::detail::variant::holds_element_polymorphic<boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >, U >::value),
257 "boost::variant does not contain specified type U, "
258 "call to boost::polymorphic_get<U>(boost::variant<T...>&) will always throw boost::bad_polymorphic_get exception"
261 return polymorphic_relaxed_get<U>(operand);
264 template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
266 typename add_reference<const U>::type
267 polymorphic_strict_get(
268 const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& operand
269 BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
272 BOOST_STATIC_ASSERT_MSG(
273 (boost::detail::variant::holds_element_polymorphic<boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >, U >::value),
274 "boost::variant does not contain specified type U, "
275 "call to boost::polymorphic_get<U>(const boost::variant<T...>&) will always throw boost::bad_polymorphic_get exception"
278 return polymorphic_relaxed_get<U>(operand);
281 /////////////////////////////////////////////////////////////////////////////////////////////////////////////
282 // polymorphic_get<U>(variant) methods
285 template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
287 typename add_pointer<U>::type
289 boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >* operand
290 BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
293 #ifdef BOOST_VARIANT_USE_RELAXED_GET_BY_DEFAULT
294 return polymorphic_relaxed_get<U>(operand);
296 return polymorphic_strict_get<U>(operand);
301 template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
303 typename add_pointer<const U>::type
305 const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >* operand
306 BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
309 #ifdef BOOST_VARIANT_USE_RELAXED_GET_BY_DEFAULT
310 return polymorphic_relaxed_get<U>(operand);
312 return polymorphic_strict_get<U>(operand);
316 template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
318 typename add_reference<U>::type
320 boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& operand
321 BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
324 #ifdef BOOST_VARIANT_USE_RELAXED_GET_BY_DEFAULT
325 return polymorphic_relaxed_get<U>(operand);
327 return polymorphic_strict_get<U>(operand);
331 template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
333 typename add_reference<const U>::type
335 const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& operand
336 BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
339 #ifdef BOOST_VARIANT_USE_RELAXED_GET_BY_DEFAULT
340 return polymorphic_relaxed_get<U>(operand);
342 return polymorphic_strict_get<U>(operand);
347 #endif // BOOST_VARIANT_POLYMORPHIC_GET_HPP