1 /*=============================================================================
2 Copyright (c) 2001-2011 Joel de Guzman
3 Copyright (c) 2001-2011 Hartmut Kaiser
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
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>
26 namespace boost { namespace spirit { namespace qi { namespace detail
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
34 , typename traits::is_weak_substitute<Attribute, ValueType>::type
36 traits::is_weak_substitute<Attribute, ValueType>
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
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>
54 // Specialization for fusion sequences, in this case we check whether all
55 // the types in the sequence are convertible to the lhs attribute.
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
64 negate_weak_substitute_if_not<Sequence, Attribute, Container>
65 , negate_weak_substitute_if_not<Sequence, Attribute, ValueType> >
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
73 , bool IsSequence = fusion::traits::is_sequence<ValueType>::value>
74 struct pass_through_container_fusion_sequence
76 typedef typename mpl::find_if<
77 Attribute, not_compatible_element<Container, ValueType, mpl::_1>
79 typedef typename mpl::end<Attribute>::type end;
81 typedef typename is_same<iter, end>::type type;
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
89 struct pass_through_container_fusion_sequence<
90 Container, ValueType, Attribute, Sequence, true>
92 typedef typename mpl::find_if<
94 , not_compatible_element<Container, ValueType, mpl::_1, Sequence>
96 typedef typename mpl::end<Attribute>::type end;
98 typedef typename is_same<iter, end>::type type;
101 template <typename Container, typename ValueType, typename Attribute
103 struct pass_through_container_base<Container, ValueType, Attribute
105 , typename enable_if<fusion::traits::is_sequence<Attribute> >::type>
106 : pass_through_container_fusion_sequence<
107 Container, ValueType, Attribute, Sequence>
110 // Specialization for containers
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
120 traits::is_weak_substitute<Attribute, Container>
121 , traits::is_weak_substitute<AttributeValueType, Container> >
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>
134 template <typename Container, typename ValueType, typename Attribute
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>
144 // Specialization for exposed optional attributes
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
150 template <typename Container, typename ValueType, typename Attribute
152 , bool IsSequence = fusion::traits::is_sequence<Attribute>::value>
153 struct pass_through_container_optional
155 traits::is_weak_substitute<Attribute, Container>
156 , traits::is_weak_substitute<Attribute, ValueType> >
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
163 struct pass_through_container_optional<
164 Container, ValueType, Attribute, Sequence, true>
165 : pass_through_container_fusion_sequence<
166 Container, ValueType, Attribute, Sequence>
169 ///////////////////////////////////////////////////////////////////////////
170 template <typename Container, typename ValueType, typename Attribute
172 struct pass_through_container
173 : pass_through_container_base<Container, ValueType, Attribute, Sequence>
176 // Handle optional attributes
177 template <typename Container, typename ValueType, typename Attribute
179 struct pass_through_container<
180 Container, ValueType, boost::optional<Attribute>, Sequence>
181 : pass_through_container_optional<
182 Container, ValueType, Attribute, Sequence>
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
190 struct pass_through_container<
191 Container, boost::optional<ValueType>, boost::optional<Attribute>
193 : mpl::not_<traits::is_weak_substitute<Attribute, ValueType> >
196 // Specialization for exposed variant attributes
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
201 #if !defined(BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES)
202 template <typename Container, typename ValueType, typename Sequence
204 struct pass_through_container<Container, ValueType, boost::variant<T>
206 : pass_through_container<Container, ValueType, T, Sequence>
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
220 #define BOOST_SPIRIT_PASS_THROUGH_CONTAINER(z, N, _) \
221 pass_through_container<Container, ValueType, \
222 BOOST_PP_CAT(T, N), Sequence>::type::value || \
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>
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>
240 #undef BOOST_SPIRIT_PASS_THROUGH_CONTAINER
244 ///////////////////////////////////////////////////////////////////////////////
245 namespace boost { namespace spirit { namespace traits
247 ///////////////////////////////////////////////////////////////////////////
248 // forwarding customization point for domain qi::domain
249 template <typename Container, typename ValueType, typename Attribute
251 struct pass_through_container<
252 Container, ValueType, Attribute, Sequence, qi::domain>
253 : qi::detail::pass_through_container<
254 Container, ValueType, Attribute, Sequence>
258 namespace boost { namespace spirit { namespace qi { namespace detail
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
267 typedef typename F::context_type context_type;
268 typedef typename F::iterator_type iterator_type;
270 pass_container(F const& f_, Attr& attr_)
271 : f(f_), attr(attr_) {}
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
278 // synthesized attribute needs to be default constructed
279 typename traits::container_value<Attr>::type val =
280 typename traits::container_value<Attr>::type();
282 iterator_type save = f.first;
283 bool r = f(component, val);
286 // push the parsed value into our attribute
287 r = !traits::push_back(attr, val);
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
300 return f(component, attr);
303 ///////////////////////////////////////////////////////////////////////
304 // this is for the case when the current element doesn't expect an
306 template <typename Component>
307 bool dispatch_attribute(Component const& component, mpl::false_) const
309 return f(component, unused);
312 // the current element expects an attribute
313 template <typename Component>
314 bool dispatch_attribute(Component const& component, mpl::true_) const
316 typedef typename traits::container_value<Attr>::type value_type;
317 typedef typename traits::attribute_of<
318 Component, context_type, iterator_type>::type
321 // this predicate detects, whether the attribute of the current
322 // element is a substitute for the value type of the container
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>
331 return dispatch_container(component, predicate());
334 // Dispatches to dispatch_main depending on the attribute type
336 template <typename Component>
337 bool operator()(Component const& component) const
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
348 // ensure the attribute is actually a container type
349 traits::make_container(attr);
351 return dispatch_attribute(component, predicate());
358 // silence MSVC warning C4512: assignment operator could not be generated
359 pass_container& operator= (pass_container const&);
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)
369 return pass_container<F, Attr, mpl::false_>(f, attr);
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)
377 return pass_container<F, Attr, mpl::true_>(f, attr);