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