]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Copyright (c) 2001-2011 Hartmut Kaiser |
2 | // Copyright (c) 2001-2011 Joel de Guzman | |
3 | // | |
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) | |
6 | ||
f67539c2 TL |
7 | #ifndef BOOST_SPIRIT_KARMA_OPERATOR_LIST_HPP |
8 | #define BOOST_SPIRIT_KARMA_OPERATOR_LIST_HPP | |
7c673cae FG |
9 | |
10 | #if defined(_MSC_VER) | |
11 | #pragma once | |
12 | #endif | |
13 | ||
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> | |
f67539c2 TL |
27 | #include <boost/proto/operators.hpp> |
28 | #include <boost/proto/tags.hpp> | |
7c673cae FG |
29 | |
30 | namespace boost { namespace spirit | |
31 | { | |
32 | /////////////////////////////////////////////////////////////////////////// | |
33 | // Enablers | |
34 | /////////////////////////////////////////////////////////////////////////// | |
35 | template <> | |
36 | struct use_operator<karma::domain, proto::tag::modulus> // enables g % d | |
37 | : mpl::true_ {}; | |
38 | }} | |
39 | ||
40 | /////////////////////////////////////////////////////////////////////////////// | |
41 | namespace boost { namespace spirit { namespace karma | |
42 | { | |
43 | template <typename Left, typename Right, typename Strict, typename Derived> | |
44 | struct base_list : binary_generator<Derived> | |
45 | { | |
46 | private: | |
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 | |
51 | { | |
52 | // Failing subject generators are just skipped. This allows to | |
53 | // selectively generate items in the provided attribute. | |
54 | while (!f.is_at_end()) | |
55 | { | |
56 | bool r = !f(left); | |
57 | if (r) | |
58 | return true; | |
59 | if (!f.is_at_end()) | |
60 | f.next(); | |
61 | } | |
62 | return false; | |
63 | } | |
64 | ||
65 | template <typename F, typename Attribute> | |
66 | bool generate_left(F f, Attribute const&, mpl::true_) const | |
67 | { | |
68 | return !f(left); | |
69 | } | |
70 | ||
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. | |
74 | template <typename F> | |
75 | bool generate_left(F f, unused_type, mpl::false_) const | |
76 | { | |
77 | return !f(left); | |
78 | } | |
79 | ||
80 | public: | |
81 | typedef Left left_type; | |
82 | typedef Right right_type; | |
83 | ||
84 | typedef mpl::int_< | |
85 | left_type::properties::value | |
86 | | right_type::properties::value | |
87 | | generator_properties::buffering | |
88 | | generator_properties::counting | |
89 | > properties; | |
90 | ||
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> | |
95 | struct attribute | |
96 | : traits::build_std_vector< | |
97 | typename traits::attribute_of<Left, Context, Iterator>::type> | |
98 | {}; | |
99 | ||
100 | base_list(Left const& left, Right const& right) | |
101 | : left(left), right(right) | |
102 | {} | |
103 | ||
104 | template < | |
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 | |
109 | { | |
110 | typedef detail::fail_function< | |
111 | OutputIterator, Context, Delimiter | |
112 | > fail_function; | |
113 | ||
114 | typedef typename traits::container_iterator< | |
115 | typename add_const<Attribute>::type | |
116 | >::type iterator_type; | |
117 | ||
118 | typedef | |
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_> | |
123 | pass_container; | |
124 | ||
125 | iterator_type it = traits::begin(attr); | |
126 | iterator_type end = traits::end(attr); | |
127 | ||
128 | pass_container pass(fail_function(sink, ctx, d), | |
129 | indirect_iterator_type(it), indirect_iterator_type(end)); | |
130 | ||
131 | if (generate_left(pass, attr, Strict())) | |
132 | { | |
133 | while (!pass.is_at_end()) | |
134 | { | |
135 | // wrap the given output iterator as generate_left might fail | |
136 | detail::enable_buffering<OutputIterator> buffering(sink); | |
137 | { | |
138 | detail::disable_counting<OutputIterator> nocounting(sink); | |
139 | ||
140 | if (!right.generate(sink, ctx, d, unused)) | |
141 | return false; // shouldn't happen | |
142 | ||
143 | if (!generate_left(pass, attr, Strict())) | |
144 | break; // return true as one item succeeded | |
145 | } | |
146 | buffering.buffer_copy(); | |
147 | } | |
148 | return detail::sink_is_good(sink); | |
149 | } | |
150 | return false; | |
151 | } | |
152 | ||
153 | template <typename Context> | |
154 | info what(Context& context) const | |
155 | { | |
156 | return info("list", | |
157 | std::make_pair(left.what(context), right.what(context))); | |
158 | } | |
159 | ||
160 | Left left; | |
161 | Right right; | |
162 | }; | |
163 | ||
164 | template <typename Left, typename Right> | |
165 | struct list | |
166 | : base_list<Left, Right, mpl::false_, list<Left, Right> > | |
167 | { | |
168 | typedef base_list<Left, Right, mpl::false_, list> base_list_; | |
169 | ||
170 | list(Left const& left, Right const& right) | |
171 | : base_list_(left, right) {} | |
172 | }; | |
173 | ||
174 | template <typename Left, typename Right> | |
175 | struct strict_list | |
176 | : base_list<Left, Right, mpl::true_, strict_list<Left, Right> > | |
177 | { | |
178 | typedef base_list<Left, Right, mpl::true_, strict_list> base_list_; | |
179 | ||
180 | strict_list (Left const& left, Right const& right) | |
181 | : base_list_(left, right) {} | |
182 | }; | |
183 | ||
184 | /////////////////////////////////////////////////////////////////////////// | |
185 | // Generator generators: make_xxx function (objects) | |
186 | /////////////////////////////////////////////////////////////////////////// | |
187 | namespace detail | |
188 | { | |
189 | template <typename Subject, bool strict_mode = false> | |
190 | struct make_list | |
191 | : make_binary_composite<Subject, list> | |
192 | {}; | |
193 | ||
194 | template <typename Subject> | |
195 | struct make_list<Subject, true> | |
196 | : make_binary_composite<Subject, strict_list> | |
197 | {}; | |
198 | } | |
199 | ||
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> | |
203 | {}; | |
204 | }}} | |
205 | ||
206 | namespace boost { namespace spirit { namespace traits | |
207 | { | |
208 | /////////////////////////////////////////////////////////////////////////// | |
209 | template <typename Left, typename Right> | |
210 | struct has_semantic_action<karma::list<Left, Right> > | |
211 | : binary_has_semantic_action<Left, Right> {}; | |
212 | ||
213 | template <typename Left, typename Right> | |
214 | struct has_semantic_action<karma::strict_list<Left, Right> > | |
215 | : binary_has_semantic_action<Left, Right> {}; | |
216 | ||
217 | /////////////////////////////////////////////////////////////////////////// | |
218 | template <typename Left, typename Right, typename Attribute | |
219 | , typename Context, typename Iterator> | |
220 | struct handles_container<karma::list<Left, Right>, Attribute | |
221 | , Context, Iterator> | |
222 | : mpl::true_ {}; | |
223 | ||
224 | template <typename Left, typename Right, typename Attribute | |
225 | , typename Context, typename Iterator> | |
226 | struct handles_container<karma::strict_list<Left, Right>, Attribute | |
227 | , Context, Iterator> | |
228 | : mpl::true_ {}; | |
229 | }}} | |
230 | ||
231 | #endif |