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