1 // Copyright (c) 2001-2011 Hartmut Kaiser
2 // Copyright (c) 2001-2011 Joel de Guzman
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 #ifndef BOOST_SPIRIT_KARMA_OPERATOR_LIST_HPP
8 #define BOOST_SPIRIT_KARMA_OPERATOR_LIST_HPP
14 #include <boost/spirit/home/karma/domain.hpp>
15 #include <boost/spirit/home/karma/generator.hpp>
16 #include <boost/spirit/home/karma/meta_compiler.hpp>
17 #include <boost/spirit/home/karma/detail/output_iterator.hpp>
18 #include <boost/spirit/home/karma/detail/indirect_iterator.hpp>
19 #include <boost/spirit/home/karma/detail/get_stricttag.hpp>
20 #include <boost/spirit/home/karma/detail/pass_container.hpp>
21 #include <boost/spirit/home/karma/detail/fail_function.hpp>
22 #include <boost/spirit/home/support/info.hpp>
23 #include <boost/spirit/home/support/unused.hpp>
24 #include <boost/spirit/home/support/container.hpp>
25 #include <boost/spirit/home/support/handles_container.hpp>
26 #include <boost/spirit/home/karma/detail/attributes.hpp>
27 #include <boost/proto/operators.hpp>
28 #include <boost/proto/tags.hpp>
30 namespace boost { namespace spirit
32 ///////////////////////////////////////////////////////////////////////////
34 ///////////////////////////////////////////////////////////////////////////
36 struct use_operator<karma::domain, proto::tag::modulus> // enables g % d
40 ///////////////////////////////////////////////////////////////////////////////
41 namespace boost { namespace spirit { namespace karma
43 template <typename Left, typename Right, typename Strict, typename Derived>
44 struct base_list : binary_generator<Derived>
47 // iterate over the given container until its exhausted or the embedded
48 // (left) generator succeeds
49 template <typename F, typename Attribute>
50 bool generate_left(F f, Attribute const&, mpl::false_) const
52 // Failing subject generators are just skipped. This allows to
53 // selectively generate items in the provided attribute.
54 while (!f.is_at_end())
65 template <typename F, typename Attribute>
66 bool generate_left(F f, Attribute const&, mpl::true_) const
71 // There is no way to distinguish a failed generator from a
72 // generator to be skipped. We assume the user takes responsibility
73 // for ending the loop if no attribute is specified.
75 bool generate_left(F f, unused_type, mpl::false_) const
81 typedef Left left_type;
82 typedef Right right_type;
85 left_type::properties::value
86 | right_type::properties::value
87 | generator_properties::buffering
88 | generator_properties::counting
91 // Build a std::vector from the LHS's attribute. Note
92 // that build_std_vector may return unused_type if the
93 // subject's attribute is an unused_type.
94 template <typename Context, typename Iterator>
96 : traits::build_std_vector<
97 typename traits::attribute_of<Left, Context, Iterator>::type>
100 base_list(Left const& left, Right const& right)
101 : left(left), right(right)
105 typename OutputIterator, typename Context, typename Delimiter
106 , typename Attribute>
107 bool generate(OutputIterator& sink, Context& ctx
108 , Delimiter const& d, Attribute const& attr) const
110 typedef detail::fail_function<
111 OutputIterator, Context, Delimiter
114 typedef typename traits::container_iterator<
115 typename add_const<Attribute>::type
116 >::type iterator_type;
119 typename traits::make_indirect_iterator<iterator_type>::type
120 indirect_iterator_type;
121 typedef detail::pass_container<
122 fail_function, Attribute, indirect_iterator_type, mpl::false_>
125 iterator_type it = traits::begin(attr);
126 iterator_type end = traits::end(attr);
128 pass_container pass(fail_function(sink, ctx, d),
129 indirect_iterator_type(it), indirect_iterator_type(end));
131 if (generate_left(pass, attr, Strict()))
133 while (!pass.is_at_end())
135 // wrap the given output iterator as generate_left might fail
136 detail::enable_buffering<OutputIterator> buffering(sink);
138 detail::disable_counting<OutputIterator> nocounting(sink);
140 if (!right.generate(sink, ctx, d, unused))
141 return false; // shouldn't happen
143 if (!generate_left(pass, attr, Strict()))
144 break; // return true as one item succeeded
146 buffering.buffer_copy();
148 return detail::sink_is_good(sink);
153 template <typename Context>
154 info what(Context& context) const
157 std::make_pair(left.what(context), right.what(context)));
164 template <typename Left, typename Right>
166 : base_list<Left, Right, mpl::false_, list<Left, Right> >
168 typedef base_list<Left, Right, mpl::false_, list> base_list_;
170 list(Left const& left, Right const& right)
171 : base_list_(left, right) {}
174 template <typename Left, typename Right>
176 : base_list<Left, Right, mpl::true_, strict_list<Left, Right> >
178 typedef base_list<Left, Right, mpl::true_, strict_list> base_list_;
180 strict_list (Left const& left, Right const& right)
181 : base_list_(left, right) {}
184 ///////////////////////////////////////////////////////////////////////////
185 // Generator generators: make_xxx function (objects)
186 ///////////////////////////////////////////////////////////////////////////
189 template <typename Subject, bool strict_mode = false>
191 : make_binary_composite<Subject, list>
194 template <typename Subject>
195 struct make_list<Subject, true>
196 : make_binary_composite<Subject, strict_list>
200 template <typename Subject, typename Modifiers>
201 struct make_composite<proto::tag::modulus, Subject, Modifiers>
202 : detail::make_list<Subject, detail::get_stricttag<Modifiers>::value>
206 namespace boost { namespace spirit { namespace traits
208 ///////////////////////////////////////////////////////////////////////////
209 template <typename Left, typename Right>
210 struct has_semantic_action<karma::list<Left, Right> >
211 : binary_has_semantic_action<Left, Right> {};
213 template <typename Left, typename Right>
214 struct has_semantic_action<karma::strict_list<Left, Right> >
215 : binary_has_semantic_action<Left, Right> {};
217 ///////////////////////////////////////////////////////////////////////////
218 template <typename Left, typename Right, typename Attribute
219 , typename Context, typename Iterator>
220 struct handles_container<karma::list<Left, Right>, Attribute
224 template <typename Left, typename Right, typename Attribute
225 , typename Context, typename Iterator>
226 struct handles_container<karma::strict_list<Left, Right>, Attribute