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