1/*=============================================================================
2 Copyright (c) 2001-2011 Hartmut Kaiser
3
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
9
10#if defined(_MSC_VER)
11#pragma once
12#endif
13
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>
20
21namespace boost { namespace spirit { namespace qi { namespace detail
22{
23 template <typename Variant, typename T>
24 struct find_substitute
25 {
26 // Get the type from the Variant that can be a substitute for T.
27 // If none is found, just return T
28
29 typedef Variant variant_type;
30 typedef typename variant_type::types types;
31 typedef typename mpl::end<types>::type end;
32
33 typedef typename mpl::find<types, T>::type iter_1;
34
35 typedef typename
36 mpl::eval_if<
37 is_same<iter_1, end>,
38 mpl::find_if<types, traits::is_substitute<T, mpl::_1> >,
39 mpl::identity<iter_1>
40 >::type
41 iter;
42
43 typedef typename
44 mpl::eval_if<
45 is_same<iter, end>,
46 mpl::identity<T>,
47 mpl::deref<iter>
48 >::type
49 type;
50 };
51
52#ifdef _MSC_VER
53# pragma warning(push)
54# pragma warning(disable: 4512) // assignment operator could not be generated.
55#endif
56 template <typename Iterator, typename Context, typename Skipper,
57 typename Attribute>
58 struct alternative_function
59 {
60 alternative_function(
61 Iterator& first_, Iterator const& last_, Context& context_,
62 Skipper const& skipper_, Attribute& attr_)
63 : first(first_), last(last_), context(context_), skipper(skipper_),
64 attr(attr_)
65 {
66 }
67
68 template <typename Component>
69 bool call(Component const& component, mpl::true_) const
70 {
71 // if Attribute is not a variant, then pass it as-is
72 return component.parse(first, last, context, skipper, attr);
73 }
74
75 template <typename Component>
76 bool call_optional_or_variant(Component const& component, mpl::true_) const
77 {
78 // If Attribute is an optional, then create an attribute for the Component
79 // with the type optional::value_type. If the expected attribute is unused type,
80 // use it instead.
81 typedef typename
82 traits::attribute_of<Component, Context, Iterator>::type
83 expected_type;
84
85 typename mpl::if_<
86 is_same<expected_type, unused_type>,
87 unused_type,
88 typename Attribute::value_type>::type
89 val;
90
91 if (component.parse(first, last, context, skipper, val))
92 {
93 traits::assign_to(val, attr);
94 return true;
95 }
96 return false;
97 }
98
99 template <typename Component>
100 bool call_variant(Component const& component, mpl::false_) const
101 {
102 // If Attribute is a variant, then search the variant types for a
103 // suitable substitute type.
104
105 typename
106 find_substitute<Attribute,
107 typename traits::attribute_of<Component, Context, Iterator>::type
108 >::type
109 val;
110
111 if (component.parse(first, last, context, skipper, val))
112 {
113 traits::assign_to(val, attr);
114 return true;
115 }
116 return false;
117 }
118
119 template <typename Component>
120 bool call_variant(Component const& component, mpl::true_) const
121 {
122 // If Attribute is a variant and the expected attribute is
123 // the same type (pass the variant as-is).
124
125 return component.parse(first, last, context, skipper, attr);
126 }
127
128 template <typename Component>
129 bool call_optional_or_variant(Component const& component, mpl::false_) const
130 {
131 // Attribute is a variant...
132
133 typedef typename
134 traits::attribute_of<Component, Context, Iterator>::type
135 expected;
136 return call_variant(component,
137 is_same<Attribute, expected>());
138 }
139
140 template <typename Component>
141 bool call(Component const& component, mpl::false_) const
142 {
143 return call_optional_or_variant(
144 component, spirit::traits::not_is_variant<Attribute, qi::domain>());
145 }
146
147 template <typename Component>
148 bool call_unused(Component const& component, mpl::true_) const
149 {
150 // return true if the parser succeeds
151 return call(component,
152 mpl::and_<
153 spirit::traits::not_is_variant<Attribute, qi::domain>,
154 spirit::traits::not_is_optional<Attribute, qi::domain>
155 >());
156 }
157
158 template <typename Component>
159 bool call_unused(Component const& component, mpl::false_) const
160 {
161 return component.parse(first, last, context, skipper, unused);
162 }
163
164 template <typename Component>
165 bool operator()(Component const& component) const
166 {
167 // return true if the parser succeeds
168 typedef typename traits::not_is_unused<
169 typename traits::attribute_of<Component, Context, Iterator>::type
170 >::type predicate;
171
172 return call_unused(component, predicate());
173 }
174
175 Iterator& first;
176 Iterator const& last;
177 Context& context;
178 Skipper const& skipper;
179 Attribute& attr;
180 };
181
182 template <typename Iterator, typename Context, typename Skipper>
183 struct alternative_function<Iterator, Context, Skipper, unused_type const>
184 {
185 alternative_function(
186 Iterator& first_, Iterator const& last_, Context& context_,
187 Skipper const& skipper_, unused_type)
188 : first(first_), last(last_), context(context_), skipper(skipper_)
189 {
190 }
191
192 template <typename Component>
193 bool operator()(Component const& component) const
194 {
195 // return true if the parser succeeds
196 return component.parse(first, last, context, skipper,
197 unused);
198 }
199
200 Iterator& first;
201 Iterator const& last;
202 Context& context;
203 Skipper const& skipper;
204 };
205#ifdef _MSC_VER
206# pragma warning(pop)
207#endif
208
209}}}}
210
211#endif
212

source code of boost/libs/spirit/include/boost/spirit/home/qi/detail/alternative_function.hpp