Chris@16
|
1 // Copyright (c) 2001-2011 Hartmut Kaiser
|
Chris@16
|
2 //
|
Chris@16
|
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
5
|
Chris@16
|
6 #if !defined(BOOST_SPIRIT_KARMA_LEFT_ALIGNMENT_FEB_27_2007_1216PM)
|
Chris@16
|
7 #define BOOST_SPIRIT_KARMA_LEFT_ALIGNMENT_FEB_27_2007_1216PM
|
Chris@16
|
8
|
Chris@16
|
9 #if defined(_MSC_VER)
|
Chris@16
|
10 #pragma once
|
Chris@16
|
11 #endif
|
Chris@16
|
12
|
Chris@16
|
13 #include <boost/spirit/home/karma/meta_compiler.hpp>
|
Chris@16
|
14 #include <boost/spirit/home/karma/generator.hpp>
|
Chris@16
|
15 #include <boost/spirit/home/karma/domain.hpp>
|
Chris@16
|
16 #include <boost/spirit/home/karma/detail/output_iterator.hpp>
|
Chris@16
|
17 #include <boost/spirit/home/karma/detail/default_width.hpp>
|
Chris@16
|
18 #include <boost/spirit/home/karma/delimit_out.hpp>
|
Chris@16
|
19 #include <boost/spirit/home/karma/auxiliary/lazy.hpp>
|
Chris@16
|
20 #include <boost/spirit/home/support/unused.hpp>
|
Chris@16
|
21 #include <boost/spirit/home/support/common_terminals.hpp>
|
Chris@16
|
22 #include <boost/spirit/home/karma/detail/attributes.hpp>
|
Chris@16
|
23 #include <boost/spirit/home/support/info.hpp>
|
Chris@16
|
24 #include <boost/spirit/home/support/unused.hpp>
|
Chris@16
|
25 #include <boost/spirit/home/support/has_semantic_action.hpp>
|
Chris@16
|
26 #include <boost/spirit/home/support/handles_container.hpp>
|
Chris@16
|
27 #include <boost/fusion/include/at.hpp>
|
Chris@16
|
28 #include <boost/fusion/include/vector.hpp>
|
Chris@16
|
29 #include <boost/lexical_cast.hpp>
|
Chris@16
|
30 #include <boost/integer_traits.hpp>
|
Chris@16
|
31 #include <boost/mpl/bool.hpp>
|
Chris@16
|
32 #include <boost/utility/enable_if.hpp>
|
Chris@16
|
33 #include <boost/detail/workaround.hpp>
|
Chris@16
|
34
|
Chris@16
|
35 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
36 namespace boost { namespace spirit
|
Chris@16
|
37 {
|
Chris@16
|
38 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
39 // Enablers
|
Chris@16
|
40 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
41
|
Chris@16
|
42 // enables left_align[]
|
Chris@16
|
43 template <>
|
Chris@16
|
44 struct use_directive<karma::domain, tag::left_align>
|
Chris@16
|
45 : mpl::true_ {};
|
Chris@16
|
46
|
Chris@16
|
47 // enables left_align(d)[g] and left_align(w)[g], where d is a generator
|
Chris@16
|
48 // and w is a maximum width
|
Chris@16
|
49 template <typename T>
|
Chris@16
|
50 struct use_directive<karma::domain
|
Chris@16
|
51 , terminal_ex<tag::left_align, fusion::vector1<T> > >
|
Chris@16
|
52 : mpl::true_ {};
|
Chris@16
|
53
|
Chris@16
|
54 // enables *lazy* left_align(d)[g], where d provides a generator
|
Chris@16
|
55 template <>
|
Chris@16
|
56 struct use_lazy_directive<karma::domain, tag::left_align, 1>
|
Chris@16
|
57 : mpl::true_ {};
|
Chris@16
|
58
|
Chris@16
|
59 // enables left_align(w, d)[g], where d is a generator and w is a maximum
|
Chris@16
|
60 // width
|
Chris@16
|
61 template <typename Width, typename Padding>
|
Chris@16
|
62 struct use_directive<karma::domain
|
Chris@16
|
63 , terminal_ex<tag::left_align, fusion::vector2<Width, Padding> > >
|
Chris@16
|
64 : spirit::traits::matches<karma::domain, Padding> {};
|
Chris@16
|
65
|
Chris@16
|
66 // enables *lazy* left_align(w, d)[g], where d provides a generator and w
|
Chris@16
|
67 // is a maximum width
|
Chris@16
|
68 template <>
|
Chris@16
|
69 struct use_lazy_directive<karma::domain, tag::left_align, 2>
|
Chris@16
|
70 : mpl::true_ {};
|
Chris@16
|
71
|
Chris@16
|
72 }}
|
Chris@16
|
73
|
Chris@16
|
74 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
75 namespace boost { namespace spirit { namespace karma
|
Chris@16
|
76 {
|
Chris@16
|
77 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
|
Chris@16
|
78 using spirit::left_align;
|
Chris@16
|
79 #endif
|
Chris@16
|
80 using spirit::left_align_type;
|
Chris@16
|
81
|
Chris@16
|
82 namespace detail
|
Chris@16
|
83 {
|
Chris@16
|
84 ///////////////////////////////////////////////////////////////////////
|
Chris@16
|
85 // The left_align_generate template function is used for all the
|
Chris@16
|
86 // different flavors of the left_align[] directive.
|
Chris@16
|
87 ///////////////////////////////////////////////////////////////////////
|
Chris@16
|
88 template <typename OutputIterator, typename Context, typename Delimiter,
|
Chris@16
|
89 typename Attribute, typename Embedded, typename Padding>
|
Chris@16
|
90 inline static bool
|
Chris@16
|
91 left_align_generate(OutputIterator& sink, Context& ctx,
|
Chris@16
|
92 Delimiter const& d, Attribute const& attr, Embedded const& e,
|
Chris@16
|
93 unsigned int const width, Padding const& p)
|
Chris@16
|
94 {
|
Chris@16
|
95 #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
|
Chris@16
|
96 e; // suppresses warning: C4100: 'e' : unreferenced formal parameter
|
Chris@16
|
97 #endif
|
Chris@16
|
98 // wrap the given output iterator to allow counting
|
Chris@16
|
99 detail::enable_counting<OutputIterator> counting(sink);
|
Chris@16
|
100
|
Chris@16
|
101 // first generate the underlying output
|
Chris@16
|
102 bool r = e.generate(sink, ctx, d, attr);
|
Chris@16
|
103
|
Chris@16
|
104 // pad the output until the max width is reached
|
Chris@16
|
105 while(r && counting.count() < width)
|
Chris@16
|
106 r = p.generate(sink, ctx, unused, unused);
|
Chris@16
|
107
|
Chris@16
|
108 return r;
|
Chris@16
|
109 }
|
Chris@16
|
110 }
|
Chris@16
|
111
|
Chris@16
|
112 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
113 // The simple left alignment directive is used for left_align[...]
|
Chris@16
|
114 // generators. It uses default values for the generated width (defined via
|
Chris@16
|
115 // the BOOST_KARMA_DEFAULT_FIELD_LENGTH constant) and for the padding
|
Chris@16
|
116 // generator (always spaces).
|
Chris@16
|
117 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
118 template <typename Subject, typename Width = detail::default_width>
|
Chris@16
|
119 struct simple_left_alignment
|
Chris@16
|
120 : unary_generator<simple_left_alignment<Subject, Width> >
|
Chris@16
|
121 {
|
Chris@16
|
122 typedef Subject subject_type;
|
Chris@16
|
123
|
Chris@16
|
124 typedef mpl::int_<
|
Chris@16
|
125 generator_properties::counting | subject_type::properties::value
|
Chris@16
|
126 > properties;
|
Chris@16
|
127
|
Chris@16
|
128 template <typename Context, typename Iterator>
|
Chris@16
|
129 struct attribute
|
Chris@16
|
130 : traits::attribute_of<subject_type, Context, Iterator>
|
Chris@16
|
131 {};
|
Chris@16
|
132
|
Chris@16
|
133 simple_left_alignment(Subject const& subject, Width width = Width())
|
Chris@16
|
134 : subject(subject), width(width) {}
|
Chris@16
|
135
|
Chris@16
|
136 template <typename OutputIterator, typename Context, typename Delimiter
|
Chris@16
|
137 , typename Attribute>
|
Chris@16
|
138 bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d
|
Chris@16
|
139 , Attribute const& attr) const
|
Chris@16
|
140 {
|
Chris@16
|
141 return detail::left_align_generate(sink, ctx, d, attr,
|
Chris@16
|
142 subject, width, compile<karma::domain>(' '));
|
Chris@16
|
143 }
|
Chris@16
|
144
|
Chris@16
|
145 template <typename Context>
|
Chris@16
|
146 info what(Context& context) const
|
Chris@16
|
147 {
|
Chris@16
|
148 return info("left_align", subject.what(context));
|
Chris@16
|
149 }
|
Chris@16
|
150
|
Chris@16
|
151 Subject subject;
|
Chris@16
|
152 Width width;
|
Chris@16
|
153 };
|
Chris@16
|
154
|
Chris@16
|
155 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
156 // The left alignment directive with padding, is used for generators like
|
Chris@16
|
157 // left_align(padding)[...], where padding is a arbitrary generator
|
Chris@16
|
158 // expression. It uses a default value for the generated width (defined
|
Chris@16
|
159 // via the BOOST_KARMA_DEFAULT_FIELD_LENGTH constant).
|
Chris@16
|
160 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
161 template <typename Subject, typename Padding
|
Chris@16
|
162 , typename Width = detail::default_width>
|
Chris@16
|
163 struct padding_left_alignment
|
Chris@16
|
164 : unary_generator<padding_left_alignment<Subject, Padding, Width> >
|
Chris@16
|
165 {
|
Chris@16
|
166 typedef Subject subject_type;
|
Chris@16
|
167 typedef Padding padding_type;
|
Chris@16
|
168
|
Chris@16
|
169 typedef mpl::int_<
|
Chris@16
|
170 generator_properties::counting |
|
Chris@16
|
171 subject_type::properties::value | padding_type::properties::value
|
Chris@16
|
172 > properties;
|
Chris@16
|
173
|
Chris@16
|
174 template <typename Context, typename Iterator>
|
Chris@16
|
175 struct attribute
|
Chris@16
|
176 : traits::attribute_of<subject_type, Context, Iterator>
|
Chris@16
|
177 {};
|
Chris@16
|
178
|
Chris@16
|
179 padding_left_alignment(Subject const& subject, Padding const& padding
|
Chris@16
|
180 , Width width = Width())
|
Chris@16
|
181 : subject(subject), padding(padding), width(width) {}
|
Chris@16
|
182
|
Chris@16
|
183 template <typename OutputIterator, typename Context, typename Delimiter
|
Chris@16
|
184 , typename Attribute>
|
Chris@16
|
185 bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d
|
Chris@16
|
186 , Attribute const& attr) const
|
Chris@16
|
187 {
|
Chris@16
|
188 return detail::left_align_generate(sink, ctx, d, attr,
|
Chris@16
|
189 subject, width, padding);
|
Chris@16
|
190 }
|
Chris@16
|
191
|
Chris@16
|
192 template <typename Context>
|
Chris@16
|
193 info what(Context& context) const
|
Chris@16
|
194 {
|
Chris@16
|
195 return info("left_align", subject.what(context));
|
Chris@16
|
196 }
|
Chris@16
|
197
|
Chris@16
|
198 Subject subject;
|
Chris@16
|
199 Padding padding;
|
Chris@16
|
200 Width width;
|
Chris@16
|
201 };
|
Chris@16
|
202
|
Chris@16
|
203 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
204 // Generator generators: make_xxx function (objects)
|
Chris@16
|
205 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
206
|
Chris@16
|
207 // creates left_align[] directive generator
|
Chris@16
|
208 template <typename Subject, typename Modifiers>
|
Chris@16
|
209 struct make_directive<tag::left_align, Subject, Modifiers>
|
Chris@16
|
210 {
|
Chris@16
|
211 typedef simple_left_alignment<Subject> result_type;
|
Chris@16
|
212 result_type operator()(unused_type, Subject const& subject
|
Chris@16
|
213 , unused_type) const
|
Chris@16
|
214 {
|
Chris@16
|
215 return result_type(subject);
|
Chris@16
|
216 }
|
Chris@16
|
217 };
|
Chris@16
|
218
|
Chris@16
|
219 // creates left_align(width)[] directive generator
|
Chris@16
|
220 template <typename Width, typename Subject, typename Modifiers>
|
Chris@16
|
221 struct make_directive<
|
Chris@16
|
222 terminal_ex<tag::left_align, fusion::vector1<Width> >
|
Chris@16
|
223 , Subject, Modifiers
|
Chris@16
|
224 , typename enable_if_c< integer_traits<Width>::is_integral >::type>
|
Chris@16
|
225 {
|
Chris@16
|
226 typedef simple_left_alignment<Subject, Width> result_type;
|
Chris@16
|
227
|
Chris@16
|
228 template <typename Terminal>
|
Chris@16
|
229 result_type operator()(Terminal const& term, Subject const& subject
|
Chris@16
|
230 , unused_type) const
|
Chris@16
|
231 {
|
Chris@16
|
232 return result_type(subject, fusion::at_c<0>(term.args));
|
Chris@16
|
233 }
|
Chris@16
|
234 };
|
Chris@16
|
235
|
Chris@16
|
236 // creates left_align(pad)[] directive generator
|
Chris@16
|
237 template <typename Padding, typename Subject, typename Modifiers>
|
Chris@16
|
238 struct make_directive<
|
Chris@16
|
239 terminal_ex<tag::left_align, fusion::vector1<Padding> >
|
Chris@16
|
240 , Subject, Modifiers
|
Chris@16
|
241 , typename enable_if<
|
Chris@16
|
242 mpl::and_<
|
Chris@16
|
243 spirit::traits::matches<karma::domain, Padding>,
|
Chris@16
|
244 mpl::not_<mpl::bool_<integer_traits<Padding>::is_integral> >
|
Chris@16
|
245 >
|
Chris@16
|
246 >::type>
|
Chris@16
|
247 {
|
Chris@16
|
248 typedef typename
|
Chris@16
|
249 result_of::compile<karma::domain, Padding, Modifiers>::type
|
Chris@16
|
250 padding_type;
|
Chris@16
|
251
|
Chris@16
|
252 typedef padding_left_alignment<Subject, padding_type> result_type;
|
Chris@16
|
253
|
Chris@16
|
254 template <typename Terminal>
|
Chris@16
|
255 result_type operator()(Terminal const& term, Subject const& subject
|
Chris@16
|
256 , Modifiers const& modifiers) const
|
Chris@16
|
257 {
|
Chris@16
|
258 return result_type(subject
|
Chris@16
|
259 , compile<karma::domain>(fusion::at_c<0>(term.args), modifiers));
|
Chris@16
|
260 }
|
Chris@16
|
261 };
|
Chris@16
|
262
|
Chris@16
|
263 // creates left_align(width, pad)[] directive generator
|
Chris@16
|
264 template <typename Width, typename Padding, typename Subject
|
Chris@16
|
265 , typename Modifiers>
|
Chris@16
|
266 struct make_directive<
|
Chris@16
|
267 terminal_ex<tag::left_align, fusion::vector2<Width, Padding> >
|
Chris@16
|
268 , Subject, Modifiers>
|
Chris@16
|
269 {
|
Chris@16
|
270 typedef typename
|
Chris@16
|
271 result_of::compile<karma::domain, Padding, Modifiers>::type
|
Chris@16
|
272 padding_type;
|
Chris@16
|
273
|
Chris@16
|
274 typedef padding_left_alignment<Subject, padding_type, Width> result_type;
|
Chris@16
|
275
|
Chris@16
|
276 template <typename Terminal>
|
Chris@16
|
277 result_type operator()(Terminal const& term, Subject const& subject
|
Chris@16
|
278 , unused_type) const
|
Chris@16
|
279 {
|
Chris@16
|
280 return result_type(subject
|
Chris@16
|
281 , compile<karma::domain>(fusion::at_c<1>(term.args))
|
Chris@16
|
282 , fusion::at_c<0>(term.args));
|
Chris@16
|
283 }
|
Chris@16
|
284 };
|
Chris@16
|
285
|
Chris@16
|
286 }}} // namespace boost::spirit::karma
|
Chris@16
|
287
|
Chris@16
|
288 namespace boost { namespace spirit { namespace traits
|
Chris@16
|
289 {
|
Chris@16
|
290 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
291 template <typename Subject, typename Width>
|
Chris@16
|
292 struct has_semantic_action<karma::simple_left_alignment<Subject, Width> >
|
Chris@16
|
293 : unary_has_semantic_action<Subject> {};
|
Chris@16
|
294
|
Chris@16
|
295 template <typename Subject, typename Padding, typename Width>
|
Chris@16
|
296 struct has_semantic_action<
|
Chris@16
|
297 karma::padding_left_alignment<Subject, Padding, Width> >
|
Chris@16
|
298 : unary_has_semantic_action<Subject> {};
|
Chris@16
|
299
|
Chris@16
|
300 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
301 template <typename Subject, typename Width, typename Attribute
|
Chris@16
|
302 , typename Context, typename Iterator>
|
Chris@16
|
303 struct handles_container<
|
Chris@16
|
304 karma::simple_left_alignment<Subject, Width>, Attribute
|
Chris@16
|
305 , Context, Iterator>
|
Chris@16
|
306 : unary_handles_container<Subject, Attribute, Context, Iterator> {};
|
Chris@16
|
307
|
Chris@16
|
308 template <typename Subject, typename Padding, typename Width
|
Chris@16
|
309 , typename Attribute, typename Context, typename Iterator>
|
Chris@16
|
310 struct handles_container<
|
Chris@16
|
311 karma::padding_left_alignment<Subject, Padding, Width>
|
Chris@16
|
312 , Attribute, Context, Iterator>
|
Chris@16
|
313 : unary_handles_container<Subject, Attribute, Context, Iterator> {};
|
Chris@16
|
314 }}}
|
Chris@16
|
315
|
Chris@16
|
316 #endif
|
Chris@16
|
317
|
Chris@16
|
318
|