1 /*=============================================================================
2 Copyright (c) 2001-2011 Hartmut Kaiser
3 Copyright (c) 2001-2011 Joel de Guzman
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_MAR_15_2009_0114PM)
9 #define SPIRIT_PASS_CONTAINER_MAR_15_2009_0114PM
15 #include <boost/spirit/home/karma/detail/attributes.hpp>
16 #include <boost/spirit/home/support/container.hpp>
17 #include <boost/spirit/home/support/handles_container.hpp>
18 #include <boost/spirit/home/support/detail/hold_any.hpp>
19 #include <boost/type_traits/is_base_of.hpp>
20 #include <boost/type_traits/is_convertible.hpp>
21 #include <boost/mpl/bool.hpp>
22 #include <boost/mpl/and.hpp>
23 #include <boost/mpl/or.hpp>
24 #include <boost/preprocessor/cat.hpp>
25 #include <boost/preprocessor/repetition/repeat.hpp>
26 #include <boost/range/iterator_range.hpp>
27 #include <boost/fusion/include/deduce_sequence.hpp>
29 #include <boost/mpl/print.hpp>
31 namespace boost { namespace spirit { namespace karma { namespace detail
33 // Helper meta-function allowing to evaluate weak substitutability and
34 // negate the result if the predicate (Sequence) is not true
35 template <typename Sequence, typename Attribute, typename ValueType>
36 struct negate_weak_substitute_if_not
39 , typename traits::is_weak_substitute<Attribute, ValueType>::type
41 traits::is_weak_substitute<Attribute, ValueType>
45 // pass_through_container: utility to check decide whether a provided
46 // container attribute needs to be passed through to the current component
47 // or of we need to split the container by passing along instances of its
50 // if the expected attribute of the current component is neither a Fusion
51 // sequence nor a container, we will pass through the provided container
52 // only if its value type is not compatible with the component
53 template <typename Container, typename ValueType, typename Attribute
54 , typename Sequence, typename Enable = void>
55 struct pass_through_container_base
56 : negate_weak_substitute_if_not<Sequence, ValueType, Attribute>
59 // Specialization for fusion sequences, in this case we check whether all
60 // the types in the sequence are convertible to the lhs attribute.
62 // We return false if the rhs attribute itself is a fusion sequence, which
63 // is compatible with the LHS sequence (we want to pass through this
64 // attribute without it being split apart).
65 template <typename Container, typename ValueType, typename Attribute
66 , typename Sequence = mpl::true_>
67 struct not_compatible_element
69 negate_weak_substitute_if_not<Sequence, Container, Attribute>
70 , negate_weak_substitute_if_not<Sequence, ValueType, Attribute> >
73 // If the value type of the container is not a Fusion sequence, we pass
74 // through the container if each of the elements of the Attribute
75 // sequence is compatible with either the container or its value type.
76 template <typename Container, typename ValueType, typename Attribute
78 , bool IsSequence = fusion::traits::is_sequence<ValueType>::value>
79 struct pass_through_container_fusion_sequence
81 typedef typename mpl::find_if<
82 Attribute, not_compatible_element<Container, ValueType, mpl::_1>
84 typedef typename mpl::end<Attribute>::type end;
86 typedef typename is_same<iter, end>::type type;
89 // If both, the Attribute and the value type of the provided container
90 // are Fusion sequences, we pass the container only if the two
91 // sequences are not compatible.
92 template <typename Container, typename ValueType, typename Attribute
94 struct pass_through_container_fusion_sequence<
95 Container, ValueType, Attribute, Sequence, true>
97 typedef typename mpl::find_if<
99 , not_compatible_element<Container, ValueType, mpl::_1, Sequence>
101 typedef typename mpl::end<Attribute>::type end;
103 typedef typename is_same<iter, end>::type type;
106 template <typename Container, typename ValueType, typename Attribute
108 struct pass_through_container_base<Container, ValueType, Attribute
110 , typename enable_if<fusion::traits::is_sequence<Attribute> >::type>
111 : pass_through_container_fusion_sequence<
112 Container, ValueType, Attribute, Sequence>
115 // Specialization for containers
117 // If the value type of the attribute of the current component is not
118 // a Fusion sequence, we have to pass through the provided container if
119 // both are compatible.
120 template <typename Container, typename ValueType, typename Attribute
121 , typename Sequence, typename AttributeValueType
122 , bool IsSequence = fusion::traits::is_sequence<AttributeValueType>::value>
123 struct pass_through_container_container
125 traits::is_weak_substitute<Container, Attribute>
126 , traits::is_weak_substitute<Container, AttributeValueType> >
129 // If the value type of the exposed container attribute is a Fusion
130 // sequence, we use the already existing logic for those.
131 template <typename Container, typename ValueType, typename Attribute
132 , typename Sequence, typename AttributeValueType>
133 struct pass_through_container_container<
134 Container, ValueType, Attribute, Sequence, AttributeValueType, true>
135 : pass_through_container_fusion_sequence<
136 Container, ValueType, AttributeValueType, Sequence>
139 template <typename Container, typename ValueType, typename Attribute
141 struct pass_through_container_base<Container, ValueType, Attribute
143 , typename enable_if<traits::is_container<Attribute> >::type>
144 : detail::pass_through_container_container<
145 Container, ValueType, Attribute, Sequence
146 , typename traits::container_value<Attribute>::type>
149 // Specialization for exposed optional attributes
151 // If the type embedded in the exposed optional is not a Fusion
152 // sequence we pass through the container attribute if it is compatible
153 // either to the optionals embedded type or to the containers value
155 template <typename Container, typename ValueType, typename Attribute
157 , bool IsSequence = fusion::traits::is_sequence<Attribute>::value>
158 struct pass_through_container_optional
160 traits::is_weak_substitute<Container, Attribute>
161 , traits::is_weak_substitute<ValueType, Attribute> >
164 // If the embedded type of the exposed optional attribute is a Fusion
165 // sequence, we use the already existing logic for those.
166 template <typename Container, typename ValueType, typename Attribute
168 struct pass_through_container_optional<
169 Container, ValueType, Attribute, Sequence, true>
170 : pass_through_container_fusion_sequence<
171 Container, ValueType, Attribute, Sequence>
174 ///////////////////////////////////////////////////////////////////////////
175 template <typename Container, typename ValueType, typename Attribute
177 struct pass_through_container
178 : pass_through_container_base<Container, ValueType, Attribute, Sequence>
181 // Handle optional attributes
182 template <typename Container, typename ValueType, typename Attribute
184 struct pass_through_container<
185 Container, ValueType, boost::optional<Attribute>, Sequence>
186 : pass_through_container_optional<
187 Container, ValueType, Attribute, Sequence>
190 // If both, the containers value type and the exposed attribute type are
191 // optionals we are allowed to pass through the container only if the
192 // embedded types of those optionals are not compatible.
193 template <typename Container, typename ValueType, typename Attribute
195 struct pass_through_container<
196 Container, boost::optional<ValueType>, boost::optional<Attribute>
198 : mpl::not_<traits::is_weak_substitute<ValueType, Attribute> >
201 // Specialization for exposed variant attributes
203 // We pass through the container attribute if at least one of the embedded
204 // types in the variant requires to pass through the attribute
206 #if !defined(BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES)
207 template <typename Container, typename ValueType, typename Sequence
209 struct pass_through_container<Container, ValueType, boost::variant<T>
211 : pass_through_container<Container, ValueType, T, Sequence>
214 template <typename Container, typename ValueType, typename Sequence
215 , typename T0, typename ...TN>
216 struct pass_through_container<Container, ValueType
217 , boost::variant<T0, TN...>, Sequence>
218 : mpl::bool_<pass_through_container<
219 Container, ValueType, T0, Sequence
220 >::type::value || pass_through_container<
221 Container, ValueType, boost::variant<TN...>, Sequence
225 #define BOOST_SPIRIT_PASS_THROUGH_CONTAINER(z, N, _) \
226 pass_through_container<Container, ValueType, \
227 BOOST_PP_CAT(T, N), Sequence>::type::value || \
230 // make sure unused variant parameters do not affect the outcome
231 template <typename Container, typename ValueType, typename Sequence>
232 struct pass_through_container<Container, ValueType
233 , boost::detail::variant::void_, Sequence>
237 template <typename Container, typename ValueType, typename Sequence
238 , BOOST_VARIANT_ENUM_PARAMS(typename T)>
239 struct pass_through_container<Container, ValueType
240 , boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>, Sequence>
241 : mpl::bool_<BOOST_PP_REPEAT(BOOST_VARIANT_LIMIT_TYPES
242 , BOOST_SPIRIT_PASS_THROUGH_CONTAINER, _) false>
245 #undef BOOST_SPIRIT_PASS_THROUGH_CONTAINER
249 ///////////////////////////////////////////////////////////////////////////////
250 namespace boost { namespace spirit { namespace traits
252 ///////////////////////////////////////////////////////////////////////////
253 // forwarding customization point for domain karma::domain
254 template <typename Container, typename ValueType, typename Attribute
256 struct pass_through_container<
257 Container, ValueType, Attribute, Sequence, karma::domain>
258 : karma::detail::pass_through_container<
259 Container, ValueType, Attribute, Sequence>
263 namespace boost { namespace spirit { namespace karma { namespace detail
265 template <typename Iterator>
266 struct pass_container_base
268 pass_container_base(Iterator begin, Iterator end)
269 : iter(begin), end(end)
272 mutable Iterator iter;
273 mutable Iterator end;
276 template <typename Iterator>
277 struct pass_container_base<Iterator&>
279 pass_container_base(Iterator& begin, Iterator& end)
280 : iter(begin), end(end)
287 ///////////////////////////////////////////////////////////////////////////
288 // This function handles the case where the attribute (Attr) given
289 // to the sequence is an STL container. This is a wrapper around F.
290 // The function F does the actual generating.
291 template <typename F, typename Attr, typename Iterator, typename Sequence>
292 struct pass_container : pass_container_base<Iterator>
294 typedef pass_container_base<Iterator> base_type;
295 typedef typename F::context_type context_type;
297 pass_container(F const& f, Iterator begin, Iterator end)
298 : base_type(begin, end)
302 bool is_at_end() const
304 return traits::compare(this->iter, this->end);
309 traits::next(this->iter);
312 // this is for the case when the current element expects an attribute
313 // which is taken from the next entry in the container
314 template <typename Component>
315 bool dispatch_container(Component const& component, mpl::false_) const
317 // get the next value to generate from container
318 if (!is_at_end() && !f(component, traits::deref(this->iter)))
320 // needs to return false as long as everything is ok
321 traits::next(this->iter);
325 // either no elements available any more or generation failed
329 // this is for the case when the current element is able to handle an
330 // attribute which is a container itself, this element will push its
331 // data directly into the attribute container
332 template <typename Component>
333 bool dispatch_container(Component const& component, mpl::true_) const
335 return f(component, make_iterator_range(this->iter, this->end));
338 ///////////////////////////////////////////////////////////////////////
339 // this is for the case when the current element doesn't expect an
341 template <typename Component>
342 bool dispatch_attribute(Component const& component, mpl::false_) const
344 return f(component, unused);
347 // the current element expects an attribute
348 template <typename Component>
349 bool dispatch_attribute(Component const& component, mpl::true_) const
351 typedef typename traits::container_value<Attr>::type value_type;
353 traits::attribute_of<Component, context_type>::type
356 // this predicate detects, whether the value type of the container
357 // attribute is a substitute for the attribute of the current
360 traits::handles_container<Component, Attr, context_type>
361 , traits::pass_through_container<
362 Attr, value_type, lhs_attribute, Sequence, karma::domain>
365 return dispatch_container(component, predicate());
368 // Dispatches to dispatch_main depending on the attribute type
370 template <typename Component>
371 bool operator()(Component const& component) const
373 // we need to dispatch depending on the type of the attribute
374 // of the current element (component). If this is has no attribute
375 // we shouldn't use an element of the container but unused_type
377 typedef traits::not_is_unused<
378 typename traits::attribute_of<Component, context_type>::type
381 return dispatch_attribute(component, predicate());
387 // silence MSVC warning C4512: assignment operator could not be generated
388 pass_container& operator= (pass_container const&);