]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*============================================================================= |
2 | Copyright (c) 2001-2011 Joel de Guzman | |
3 | Copyright (c) 2001-2011 Hartmut Kaiser | |
4 | ||
5 | Distributed under the Boost Software License, Version 1.0. (See accompanying | |
6 | file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
7 | =============================================================================*/ | |
8 | #if !defined(SPIRIT_PASS_CONTAINER_JANUARY_06_2009_0802PM) | |
9 | #define SPIRIT_PASS_CONTAINER_JANUARY_06_2009_0802PM | |
10 | ||
11 | #if defined(_MSC_VER) | |
12 | #pragma once | |
13 | #endif | |
14 | ||
15 | #include <boost/spirit/home/qi/detail/attributes.hpp> | |
16 | #include <boost/spirit/home/support/container.hpp> | |
17 | #include <boost/spirit/home/support/handles_container.hpp> | |
18 | #include <boost/type_traits/is_base_of.hpp> | |
19 | #include <boost/type_traits/is_convertible.hpp> | |
20 | #include <boost/mpl/bool.hpp> | |
21 | #include <boost/mpl/and.hpp> | |
22 | #include <boost/mpl/or.hpp> | |
23 | #include <boost/preprocessor/cat.hpp> | |
24 | #include <boost/preprocessor/repetition/repeat.hpp> | |
25 | ||
26 | namespace boost { namespace spirit { namespace qi { namespace detail | |
27 | { | |
28 | // Helper meta-function allowing to evaluate weak substitutability and | |
29 | // negate the result if the predicate (Sequence) is not true | |
30 | template <typename Sequence, typename Attribute, typename ValueType> | |
31 | struct negate_weak_substitute_if_not | |
32 | : mpl::if_< | |
33 | Sequence | |
34 | , typename traits::is_weak_substitute<Attribute, ValueType>::type | |
35 | , typename mpl::not_< | |
36 | traits::is_weak_substitute<Attribute, ValueType> | |
37 | >::type> | |
38 | {}; | |
39 | ||
40 | // pass_through_container: utility to check decide whether a provided | |
41 | // container attribute needs to be passed through to the current component | |
42 | // or of we need to split the container by passing along instances of its | |
43 | // value type | |
44 | ||
45 | // if the expected attribute of the current component is neither a Fusion | |
46 | // sequence nor a container, we will pass through the provided container | |
47 | // only if its value type is not compatible with the component | |
48 | template <typename Container, typename ValueType, typename Attribute | |
49 | , typename Sequence, typename Enable = void> | |
50 | struct pass_through_container_base | |
51 | : negate_weak_substitute_if_not<Sequence, Attribute, ValueType> | |
52 | {}; | |
53 | ||
54 | // Specialization for fusion sequences, in this case we check whether all | |
55 | // the types in the sequence are convertible to the lhs attribute. | |
56 | // | |
57 | // We return false if the rhs attribute itself is a fusion sequence, which | |
58 | // is compatible with the LHS sequence (we want to pass through this | |
59 | // attribute without it being split apart). | |
60 | template <typename Container, typename ValueType, typename Attribute | |
61 | , typename Sequence = mpl::true_> | |
62 | struct not_compatible_element | |
63 | : mpl::and_< | |
64 | negate_weak_substitute_if_not<Sequence, Attribute, Container> | |
65 | , negate_weak_substitute_if_not<Sequence, Attribute, ValueType> > | |
66 | {}; | |
67 | ||
68 | // If the value type of the container is not a Fusion sequence, we pass | |
69 | // through the container if each of the elements of the Attribute | |
70 | // sequence is compatible with either the container or its value type. | |
71 | template <typename Container, typename ValueType, typename Attribute | |
72 | , typename Sequence | |
73 | , bool IsSequence = fusion::traits::is_sequence<ValueType>::value> | |
74 | struct pass_through_container_fusion_sequence | |
75 | { | |
76 | typedef typename mpl::find_if< | |
77 | Attribute, not_compatible_element<Container, ValueType, mpl::_1> | |
78 | >::type iter; | |
79 | typedef typename mpl::end<Attribute>::type end; | |
80 | ||
81 | typedef typename is_same<iter, end>::type type; | |
82 | }; | |
83 | ||
84 | // If both, the Attribute and the value type of the provided container | |
85 | // are Fusion sequences, we pass the container only if the two | |
86 | // sequences are not compatible. | |
87 | template <typename Container, typename ValueType, typename Attribute | |
88 | , typename Sequence> | |
89 | struct pass_through_container_fusion_sequence< | |
90 | Container, ValueType, Attribute, Sequence, true> | |
91 | { | |
92 | typedef typename mpl::find_if< | |
93 | Attribute | |
94 | , not_compatible_element<Container, ValueType, mpl::_1, Sequence> | |
95 | >::type iter; | |
96 | typedef typename mpl::end<Attribute>::type end; | |
97 | ||
98 | typedef typename is_same<iter, end>::type type; | |
99 | }; | |
100 | ||
101 | template <typename Container, typename ValueType, typename Attribute | |
102 | , typename Sequence> | |
103 | struct pass_through_container_base<Container, ValueType, Attribute | |
104 | , Sequence | |
105 | , typename enable_if<fusion::traits::is_sequence<Attribute> >::type> | |
106 | : pass_through_container_fusion_sequence< | |
107 | Container, ValueType, Attribute, Sequence> | |
108 | {}; | |
109 | ||
110 | // Specialization for containers | |
111 | // | |
112 | // If the value type of the attribute of the current component is not | |
113 | // a Fusion sequence, we have to pass through the provided container if | |
114 | // both are compatible. | |
115 | template <typename Container, typename ValueType, typename Attribute | |
116 | , typename Sequence, typename AttributeValueType | |
117 | , bool IsSequence = fusion::traits::is_sequence<AttributeValueType>::value> | |
118 | struct pass_through_container_container | |
119 | : mpl::or_< | |
120 | traits::is_weak_substitute<Attribute, Container> | |
121 | , traits::is_weak_substitute<AttributeValueType, Container> > | |
122 | {}; | |
123 | ||
124 | // If the value type of the exposed container attribute is a Fusion | |
125 | // sequence, we use the already existing logic for those. | |
126 | template <typename Container, typename ValueType, typename Attribute | |
127 | , typename Sequence, typename AttributeValueType> | |
128 | struct pass_through_container_container< | |
129 | Container, ValueType, Attribute, Sequence, AttributeValueType, true> | |
130 | : pass_through_container_fusion_sequence< | |
131 | Container, ValueType, AttributeValueType, Sequence> | |
132 | {}; | |
133 | ||
134 | template <typename Container, typename ValueType, typename Attribute | |
135 | , typename Sequence> | |
136 | struct pass_through_container_base< | |
137 | Container, ValueType, Attribute, Sequence | |
138 | , typename enable_if<traits::is_container<Attribute> >::type> | |
139 | : detail::pass_through_container_container< | |
140 | Container, ValueType, Attribute, Sequence | |
141 | , typename traits::container_value<Attribute>::type> | |
142 | {}; | |
143 | ||
144 | // Specialization for exposed optional attributes | |
145 | // | |
146 | // If the type embedded in the exposed optional is not a Fusion | |
147 | // sequence we pass through the container attribute if it is compatible | |
148 | // either to the optionals embedded type or to the containers value | |
149 | // type. | |
150 | template <typename Container, typename ValueType, typename Attribute | |
151 | , typename Sequence | |
152 | , bool IsSequence = fusion::traits::is_sequence<Attribute>::value> | |
153 | struct pass_through_container_optional | |
154 | : mpl::or_< | |
155 | traits::is_weak_substitute<Attribute, Container> | |
156 | , traits::is_weak_substitute<Attribute, ValueType> > | |
157 | {}; | |
158 | ||
159 | // If the embedded type of the exposed optional attribute is a Fusion | |
160 | // sequence, we use the already existing logic for those. | |
161 | template <typename Container, typename ValueType, typename Attribute | |
162 | , typename Sequence> | |
163 | struct pass_through_container_optional< | |
164 | Container, ValueType, Attribute, Sequence, true> | |
165 | : pass_through_container_fusion_sequence< | |
166 | Container, ValueType, Attribute, Sequence> | |
167 | {}; | |
168 | ||
169 | /////////////////////////////////////////////////////////////////////////// | |
170 | template <typename Container, typename ValueType, typename Attribute | |
171 | , typename Sequence> | |
172 | struct pass_through_container | |
173 | : pass_through_container_base<Container, ValueType, Attribute, Sequence> | |
174 | {}; | |
175 | ||
176 | // Handle optional attributes | |
177 | template <typename Container, typename ValueType, typename Attribute | |
178 | , typename Sequence> | |
179 | struct pass_through_container< | |
180 | Container, ValueType, boost::optional<Attribute>, Sequence> | |
181 | : pass_through_container_optional< | |
182 | Container, ValueType, Attribute, Sequence> | |
183 | {}; | |
184 | ||
185 | // If both, the containers value type and the exposed attribute type are | |
186 | // optionals we are allowed to pass through the container only if the | |
187 | // embedded types of those optionals are not compatible. | |
188 | template <typename Container, typename ValueType, typename Attribute | |
189 | , typename Sequence> | |
190 | struct pass_through_container< | |
191 | Container, boost::optional<ValueType>, boost::optional<Attribute> | |
192 | , Sequence> | |
193 | : mpl::not_<traits::is_weak_substitute<Attribute, ValueType> > | |
194 | {}; | |
195 | ||
196 | // Specialization for exposed variant attributes | |
197 | // | |
198 | // We pass through the container attribute if at least one of the embedded | |
199 | // types in the variant requires to pass through the attribute | |
200 | ||
201 | #if !defined(BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES) | |
202 | template <typename Container, typename ValueType, typename Sequence | |
203 | , typename T> | |
204 | struct pass_through_container<Container, ValueType, boost::variant<T> | |
205 | , Sequence> | |
206 | : pass_through_container<Container, ValueType, T, Sequence> | |
207 | {}; | |
208 | ||
209 | template <typename Container, typename ValueType, typename Sequence | |
210 | , typename T0, typename ...TN> | |
211 | struct pass_through_container<Container, ValueType | |
212 | , boost::variant<T0, TN...>, Sequence> | |
213 | : mpl::bool_<pass_through_container< | |
214 | Container, ValueType, T0, Sequence | |
215 | >::type::value || pass_through_container< | |
216 | Container, ValueType, boost::variant<TN...>, Sequence | |
217 | >::type::value> | |
218 | {}; | |
219 | #else | |
220 | #define BOOST_SPIRIT_PASS_THROUGH_CONTAINER(z, N, _) \ | |
221 | pass_through_container<Container, ValueType, \ | |
222 | BOOST_PP_CAT(T, N), Sequence>::type::value || \ | |
223 | /***/ | |
224 | ||
225 | // make sure unused variant parameters do not affect the outcome | |
226 | template <typename Container, typename ValueType, typename Sequence> | |
227 | struct pass_through_container<Container, ValueType | |
228 | , boost::detail::variant::void_, Sequence> | |
229 | : mpl::false_ | |
230 | {}; | |
231 | ||
232 | template <typename Container, typename ValueType, typename Sequence | |
233 | , BOOST_VARIANT_ENUM_PARAMS(typename T)> | |
234 | struct pass_through_container<Container, ValueType | |
235 | , boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>, Sequence> | |
236 | : mpl::bool_<BOOST_PP_REPEAT(BOOST_VARIANT_LIMIT_TYPES | |
237 | , BOOST_SPIRIT_PASS_THROUGH_CONTAINER, _) false> | |
238 | {}; | |
239 | ||
240 | #undef BOOST_SPIRIT_PASS_THROUGH_CONTAINER | |
241 | #endif | |
242 | }}}} | |
243 | ||
244 | /////////////////////////////////////////////////////////////////////////////// | |
245 | namespace boost { namespace spirit { namespace traits | |
246 | { | |
247 | /////////////////////////////////////////////////////////////////////////// | |
248 | // forwarding customization point for domain qi::domain | |
249 | template <typename Container, typename ValueType, typename Attribute | |
250 | , typename Sequence> | |
251 | struct pass_through_container< | |
252 | Container, ValueType, Attribute, Sequence, qi::domain> | |
253 | : qi::detail::pass_through_container< | |
254 | Container, ValueType, Attribute, Sequence> | |
255 | {}; | |
256 | }}} | |
257 | ||
258 | namespace boost { namespace spirit { namespace qi { namespace detail | |
259 | { | |
260 | /////////////////////////////////////////////////////////////////////////// | |
261 | // This function handles the case where the attribute (Attr) given | |
262 | // the sequence is an STL container. This is a wrapper around F. | |
263 | // The function F does the actual parsing. | |
264 | template <typename F, typename Attr, typename Sequence> | |
265 | struct pass_container | |
266 | { | |
267 | typedef typename F::context_type context_type; | |
268 | typedef typename F::iterator_type iterator_type; | |
269 | ||
270 | pass_container(F const& f_, Attr& attr_) | |
271 | : f(f_), attr(attr_) {} | |
272 | ||
273 | // this is for the case when the current element exposes an attribute | |
274 | // which is pushed back onto the container | |
275 | template <typename Component> | |
276 | bool dispatch_container(Component const& component, mpl::false_) const | |
277 | { | |
278 | // synthesized attribute needs to be default constructed | |
279 | typename traits::container_value<Attr>::type val = | |
280 | typename traits::container_value<Attr>::type(); | |
281 | ||
282 | iterator_type save = f.first; | |
283 | bool r = f(component, val); | |
284 | if (!r) | |
285 | { | |
286 | // push the parsed value into our attribute | |
287 | r = !traits::push_back(attr, val); | |
288 | if (r) | |
289 | f.first = save; | |
290 | } | |
291 | return r; | |
292 | } | |
293 | ||
294 | // this is for the case when the current element is able to handle an | |
295 | // attribute which is a container itself, this element will push its | |
296 | // data directly into the attribute container | |
297 | template <typename Component> | |
298 | bool dispatch_container(Component const& component, mpl::true_) const | |
299 | { | |
300 | return f(component, attr); | |
301 | } | |
302 | ||
303 | /////////////////////////////////////////////////////////////////////// | |
304 | // this is for the case when the current element doesn't expect an | |
305 | // attribute | |
306 | template <typename Component> | |
307 | bool dispatch_attribute(Component const& component, mpl::false_) const | |
308 | { | |
309 | return f(component, unused); | |
310 | } | |
311 | ||
312 | // the current element expects an attribute | |
313 | template <typename Component> | |
314 | bool dispatch_attribute(Component const& component, mpl::true_) const | |
315 | { | |
316 | typedef typename traits::container_value<Attr>::type value_type; | |
317 | typedef typename traits::attribute_of< | |
318 | Component, context_type, iterator_type>::type | |
319 | rhs_attribute; | |
320 | ||
321 | // this predicate detects, whether the attribute of the current | |
322 | // element is a substitute for the value type of the container | |
323 | // attribute | |
324 | typedef mpl::and_< | |
325 | traits::handles_container< | |
326 | Component, Attr, context_type, iterator_type> | |
327 | , traits::pass_through_container< | |
328 | Attr, value_type, rhs_attribute, Sequence, qi::domain> | |
329 | > predicate; | |
330 | ||
331 | return dispatch_container(component, predicate()); | |
332 | } | |
333 | ||
334 | // Dispatches to dispatch_main depending on the attribute type | |
335 | // of the Component | |
336 | template <typename Component> | |
337 | bool operator()(Component const& component) const | |
338 | { | |
339 | // we need to dispatch depending on the type of the attribute | |
340 | // of the current element (component). If this is has no attribute | |
341 | // we shouldn't pass an attribute at all. | |
342 | typedef typename traits::not_is_unused< | |
343 | typename traits::attribute_of< | |
344 | Component, context_type, iterator_type | |
345 | >::type | |
346 | >::type predicate; | |
347 | ||
348 | // ensure the attribute is actually a container type | |
349 | traits::make_container(attr); | |
350 | ||
351 | return dispatch_attribute(component, predicate()); | |
352 | } | |
353 | ||
354 | F f; | |
355 | Attr& attr; | |
356 | ||
357 | private: | |
358 | // silence MSVC warning C4512: assignment operator could not be generated | |
359 | pass_container& operator= (pass_container const&); | |
360 | }; | |
361 | ||
362 | /////////////////////////////////////////////////////////////////////////// | |
363 | // Utility function to make a pass_container for container components | |
364 | // (kleene, list, plus, repeat) | |
365 | template <typename F, typename Attr> | |
366 | inline pass_container<F, Attr, mpl::false_> | |
367 | make_pass_container(F const& f, Attr& attr) | |
368 | { | |
369 | return pass_container<F, Attr, mpl::false_>(f, attr); | |
370 | } | |
371 | ||
372 | // Utility function to make a pass_container for sequences | |
373 | template <typename F, typename Attr> | |
374 | inline pass_container<F, Attr, mpl::true_> | |
375 | make_sequence_pass_container(F const& f, Attr& attr) | |
376 | { | |
377 | return pass_container<F, Attr, mpl::true_>(f, attr); | |
378 | } | |
379 | }}}} | |
380 | ||
381 | #endif | |
382 |