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@16
|
191 // optionals we are allowed to pass through the 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@16
|
206 #define BOOST_SPIRIT_PASS_THROUGH_CONTAINER(z, N, _) \
|
Chris@16
|
207 pass_through_container<Container, ValueType, \
|
Chris@16
|
208 BOOST_PP_CAT(T, N), Sequence>::type::value || \
|
Chris@16
|
209 /***/
|
Chris@16
|
210
|
Chris@16
|
211 // make sure unused variant parameters do not affect the outcome
|
Chris@16
|
212 template <typename Container, typename ValueType, typename Sequence>
|
Chris@16
|
213 struct pass_through_container<Container, ValueType
|
Chris@16
|
214 , boost::detail::variant::void_, Sequence>
|
Chris@16
|
215 : mpl::false_
|
Chris@16
|
216 {};
|
Chris@16
|
217
|
Chris@16
|
218 template <typename Container, typename ValueType, typename Sequence
|
Chris@16
|
219 , BOOST_VARIANT_ENUM_PARAMS(typename T)>
|
Chris@16
|
220 struct pass_through_container<Container, ValueType
|
Chris@16
|
221 , boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>, Sequence>
|
Chris@16
|
222 : mpl::bool_<BOOST_PP_REPEAT(BOOST_VARIANT_LIMIT_TYPES
|
Chris@16
|
223 , BOOST_SPIRIT_PASS_THROUGH_CONTAINER, _) false>
|
Chris@16
|
224 {};
|
Chris@16
|
225
|
Chris@16
|
226 #undef BOOST_SPIRIT_PASS_THROUGH_CONTAINER
|
Chris@16
|
227 }}}}
|
Chris@16
|
228
|
Chris@16
|
229 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
230 namespace boost { namespace spirit { namespace traits
|
Chris@16
|
231 {
|
Chris@16
|
232 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
233 // forwarding customization point for domain karma::domain
|
Chris@16
|
234 template <typename Container, typename ValueType, typename Attribute
|
Chris@16
|
235 , typename Sequence>
|
Chris@16
|
236 struct pass_through_container<
|
Chris@16
|
237 Container, ValueType, Attribute, Sequence, karma::domain>
|
Chris@16
|
238 : karma::detail::pass_through_container<
|
Chris@16
|
239 Container, ValueType, Attribute, Sequence>
|
Chris@16
|
240 {};
|
Chris@16
|
241 }}}
|
Chris@16
|
242
|
Chris@16
|
243 namespace boost { namespace spirit { namespace karma { namespace detail
|
Chris@16
|
244 {
|
Chris@16
|
245 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
246 // This function handles the case where the attribute (Attr) given
|
Chris@16
|
247 // to the sequence is an STL container. This is a wrapper around F.
|
Chris@16
|
248 // The function F does the actual generating.
|
Chris@16
|
249 template <typename F, typename Attr, typename Iterator, typename Sequence>
|
Chris@16
|
250 struct pass_container
|
Chris@16
|
251 {
|
Chris@16
|
252 typedef typename F::context_type context_type;
|
Chris@16
|
253
|
Chris@16
|
254 pass_container(F const& f, Iterator begin, Iterator end)
|
Chris@16
|
255 : f(f), iter(begin), end(end)
|
Chris@16
|
256 {}
|
Chris@16
|
257
|
Chris@16
|
258 bool is_at_end() const
|
Chris@16
|
259 {
|
Chris@16
|
260 return traits::compare(iter, end);
|
Chris@16
|
261 }
|
Chris@16
|
262
|
Chris@16
|
263 void next()
|
Chris@16
|
264 {
|
Chris@16
|
265 traits::next(iter);
|
Chris@16
|
266 }
|
Chris@16
|
267
|
Chris@16
|
268 // this is for the case when the current element expects an attribute
|
Chris@16
|
269 // which is taken from the next entry in the container
|
Chris@16
|
270 template <typename Component>
|
Chris@16
|
271 bool dispatch_container(Component const& component, mpl::false_) const
|
Chris@16
|
272 {
|
Chris@16
|
273 // get the next value to generate from container
|
Chris@16
|
274 if (!is_at_end() && !f(component, traits::deref(iter)))
|
Chris@16
|
275 {
|
Chris@16
|
276 // needs to return false as long as everything is ok
|
Chris@16
|
277 traits::next(iter);
|
Chris@16
|
278 return false;
|
Chris@16
|
279 }
|
Chris@16
|
280
|
Chris@16
|
281 // either no elements available any more or generation failed
|
Chris@16
|
282 return true;
|
Chris@16
|
283 }
|
Chris@16
|
284
|
Chris@16
|
285 // this is for the case when the current element is able to handle an
|
Chris@16
|
286 // attribute which is a container itself, this element will push its
|
Chris@16
|
287 // data directly into the attribute container
|
Chris@16
|
288 template <typename Component>
|
Chris@16
|
289 bool dispatch_container(Component const& component, mpl::true_) const
|
Chris@16
|
290 {
|
Chris@16
|
291 return f(component, make_iterator_range(iter, end));
|
Chris@16
|
292 }
|
Chris@16
|
293
|
Chris@16
|
294 ///////////////////////////////////////////////////////////////////////
|
Chris@16
|
295 // this is for the case when the current element doesn't expect an
|
Chris@16
|
296 // attribute
|
Chris@16
|
297 template <typename Component>
|
Chris@16
|
298 bool dispatch_attribute(Component const& component, mpl::false_) const
|
Chris@16
|
299 {
|
Chris@16
|
300 return f(component, unused);
|
Chris@16
|
301 }
|
Chris@16
|
302
|
Chris@16
|
303 // the current element expects an attribute
|
Chris@16
|
304 template <typename Component>
|
Chris@16
|
305 bool dispatch_attribute(Component const& component, mpl::true_) const
|
Chris@16
|
306 {
|
Chris@16
|
307 typedef typename traits::container_value<Attr>::type value_type;
|
Chris@16
|
308 typedef typename
|
Chris@16
|
309 traits::attribute_of<Component, context_type>::type
|
Chris@16
|
310 lhs_attribute;
|
Chris@16
|
311
|
Chris@16
|
312 // this predicate detects, whether the value type of the container
|
Chris@16
|
313 // attribute is a substitute for the attribute of the current
|
Chris@16
|
314 // element
|
Chris@16
|
315 typedef mpl::and_<
|
Chris@16
|
316 traits::handles_container<Component, Attr, context_type>
|
Chris@16
|
317 , traits::pass_through_container<
|
Chris@16
|
318 Attr, value_type, lhs_attribute, Sequence, karma::domain>
|
Chris@16
|
319 > predicate;
|
Chris@16
|
320
|
Chris@16
|
321 return dispatch_container(component, predicate());
|
Chris@16
|
322 }
|
Chris@16
|
323
|
Chris@16
|
324 // Dispatches to dispatch_main depending on the attribute type
|
Chris@16
|
325 // of the Component
|
Chris@16
|
326 template <typename Component>
|
Chris@16
|
327 bool operator()(Component const& component) const
|
Chris@16
|
328 {
|
Chris@16
|
329 // we need to dispatch depending on the type of the attribute
|
Chris@16
|
330 // of the current element (component). If this is has no attribute
|
Chris@16
|
331 // we shouldn't use an element of the container but unused_type
|
Chris@16
|
332 // instead
|
Chris@16
|
333 typedef traits::not_is_unused<
|
Chris@16
|
334 typename traits::attribute_of<Component, context_type>::type
|
Chris@16
|
335 > predicate;
|
Chris@16
|
336
|
Chris@16
|
337 return dispatch_attribute(component, predicate());
|
Chris@16
|
338 }
|
Chris@16
|
339
|
Chris@16
|
340 F f;
|
Chris@16
|
341 mutable Iterator iter;
|
Chris@16
|
342 mutable Iterator end;
|
Chris@16
|
343
|
Chris@16
|
344 private:
|
Chris@16
|
345 // silence MSVC warning C4512: assignment operator could not be generated
|
Chris@16
|
346 pass_container& operator= (pass_container const&);
|
Chris@16
|
347 };
|
Chris@16
|
348 }}}}
|
Chris@16
|
349
|
Chris@16
|
350 #endif
|