1 /*=============================================================================
2 Copyright (c) 2001-2011 Hartmut Kaiser
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 =============================================================================*/
7 #ifndef BOOST_SPIRIT_QI_DETAIL_ALTERNATIVE_FUNCTION_HPP
8 #define BOOST_SPIRIT_QI_DETAIL_ALTERNATIVE_FUNCTION_HPP
14 #include <boost/spirit/home/qi/domain.hpp>
15 #include <boost/spirit/home/qi/detail/assign_to.hpp>
16 #include <boost/spirit/home/support/unused.hpp>
17 #include <boost/spirit/home/qi/detail/attributes.hpp>
18 #include <boost/variant.hpp>
19 #include <boost/mpl/bool.hpp>
21 namespace boost { namespace spirit { namespace qi { namespace detail
23 template <typename Variant, typename T>
24 struct find_substitute
26 // Get the type from the Variant that can be a substitute for T.
27 // If none is found, just return T
29 typedef Variant variant_type;
30 typedef typename variant_type::types types;
31 typedef typename mpl::end<types>::type end;
33 typedef typename mpl::find<types, T>::type iter_1;
38 mpl::find_if<types, traits::is_substitute<T, mpl::_1> >,
52 template <typename Iterator, typename Context, typename Skipper,
54 struct alternative_function
57 Iterator& first_, Iterator const& last_, Context& context_,
58 Skipper const& skipper_, Attribute& attr_)
59 : first(first_), last(last_), context(context_), skipper(skipper_),
64 template <typename Component>
65 bool call(Component const& component, mpl::true_) const
67 // if Attribute is not a variant, then pass it as-is
68 return component.parse(first, last, context, skipper, attr);
71 template <typename Component>
72 bool call_optional_or_variant(Component const& component, mpl::true_) const
74 // If Attribute is an optional, then create an attribute for the Component
75 // with the type optional::value_type. If the expected attribute is unused type,
78 traits::attribute_of<Component, Context, Iterator>::type
82 is_same<expected_type, unused_type>,
84 typename Attribute::value_type>::type
87 if (component.parse(first, last, context, skipper, val))
89 traits::assign_to(val, attr);
95 template <typename Component>
96 bool call_variant(Component const& component, mpl::false_) const
98 // If Attribute is a variant, then search the variant types for a
99 // suitable substitute type.
102 find_substitute<Attribute,
103 typename traits::attribute_of<Component, Context, Iterator>::type
107 if (component.parse(first, last, context, skipper, val))
109 traits::assign_to(val, attr);
115 template <typename Component>
116 bool call_variant(Component const& component, mpl::true_) const
118 // If Attribute is a variant and the expected attribute is
119 // the same type (pass the variant as-is).
121 return component.parse(first, last, context, skipper, attr);
124 template <typename Component>
125 bool call_optional_or_variant(Component const& component, mpl::false_) const
127 // Attribute is a variant...
130 traits::attribute_of<Component, Context, Iterator>::type
132 return call_variant(component,
133 is_same<Attribute, expected>());
136 template <typename Component>
137 bool call(Component const& component, mpl::false_) const
139 return call_optional_or_variant(
140 component, spirit::traits::not_is_variant<Attribute, qi::domain>());
143 template <typename Component>
144 bool call_unused(Component const& component, mpl::true_) const
146 // return true if the parser succeeds
147 return call(component,
149 spirit::traits::not_is_variant<Attribute, qi::domain>,
150 spirit::traits::not_is_optional<Attribute, qi::domain>
154 template <typename Component>
155 bool call_unused(Component const& component, mpl::false_) const
157 return component.parse(first, last, context, skipper, unused);
160 template <typename Component>
161 bool operator()(Component const& component) const
163 // return true if the parser succeeds
164 typedef typename traits::not_is_unused<
165 typename traits::attribute_of<Component, Context, Iterator>::type
168 return call_unused(component, predicate());
172 Iterator const& last;
174 Skipper const& skipper;
177 // silence MSVC warning C4512: assignment operator could not be generated
178 BOOST_DELETED_FUNCTION(alternative_function& operator= (alternative_function const&))
181 template <typename Iterator, typename Context, typename Skipper>
182 struct alternative_function<Iterator, Context, Skipper, unused_type const>
184 alternative_function(
185 Iterator& first_, Iterator const& last_, Context& context_,
186 Skipper const& skipper_, unused_type)
187 : first(first_), last(last_), context(context_), skipper(skipper_)
191 template <typename Component>
192 bool operator()(Component const& component) const
194 // return true if the parser succeeds
195 return component.parse(first, last, context, skipper,
200 Iterator const& last;
202 Skipper const& skipper;
204 // silence MSVC warning C4512: assignment operator could not be generated
205 BOOST_DELETED_FUNCTION(alternative_function& operator= (alternative_function const&))