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_RIGHT_ALIGNMENT_FEB_27_2007_1216PM)
|
Chris@16
|
7 #define BOOST_SPIRIT_KARMA_RIGHT_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/support/has_semantic_action.hpp>
|
Chris@16
|
23 #include <boost/spirit/home/support/handles_container.hpp>
|
Chris@16
|
24 #include <boost/spirit/home/karma/detail/attributes.hpp>
|
Chris@16
|
25 #include <boost/spirit/home/support/info.hpp>
|
Chris@16
|
26 #include <boost/spirit/home/support/unused.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 right_align[]
|
Chris@16
|
43 template <>
|
Chris@16
|
44 struct use_directive<karma::domain, tag::right_align>
|
Chris@16
|
45 : mpl::true_ {};
|
Chris@16
|
46
|
Chris@16
|
47 // enables right_align(d)[g] and right_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::right_align, fusion::vector1<T> > >
|
Chris@16
|
52 : mpl::true_ {};
|
Chris@16
|
53
|
Chris@16
|
54 // enables *lazy* right_align(d)[g], where d provides a generator
|
Chris@16
|
55 template <>
|
Chris@16
|
56 struct use_lazy_directive<karma::domain, tag::right_align, 1>
|
Chris@16
|
57 : mpl::true_ {};
|
Chris@16
|
58
|
Chris@16
|
59 // enables right_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::right_align, fusion::vector2<Width, Padding> > >
|
Chris@16
|
64 : spirit::traits::matches<karma::domain, Padding> {};
|
Chris@16
|
65
|
Chris@16
|
66 // enables *lazy* right_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::right_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::right_align;
|
Chris@16
|
79 #endif
|
Chris@16
|
80 using spirit::right_align_type;
|
Chris@16
|
81
|
Chris@16
|
82 namespace detail
|
Chris@16
|
83 {
|
Chris@16
|
84 ///////////////////////////////////////////////////////////////////////
|
Chris@16
|
85 // The right_align_generate template function is used for all the
|
Chris@16
|
86 // different flavors of the right_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 right_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 left padding
|
Chris@16
|
99 detail::enable_buffering<OutputIterator> buffering(sink, width);
|
Chris@16
|
100 bool r = false;
|
Chris@16
|
101
|
Chris@16
|
102 // first generate the embedded output
|
Chris@16
|
103 {
|
Chris@16
|
104 detail::disable_counting<OutputIterator> nocounting(sink);
|
Chris@16
|
105 r = e.generate(sink, ctx, d, attr);
|
Chris@16
|
106 } // re-enable counting
|
Chris@16
|
107
|
Chris@16
|
108 buffering.disable(); // do not perform buffering any more
|
Chris@16
|
109
|
Chris@16
|
110 // generate the left padding
|
Chris@16
|
111 detail::enable_counting<OutputIterator> counting(sink, buffering.buffer_size());
|
Chris@16
|
112 while(r && counting.count() < width)
|
Chris@16
|
113 r = p.generate(sink, ctx, unused, unused);
|
Chris@16
|
114
|
Chris@16
|
115 // copy the buffered output to the target output iterator
|
Chris@16
|
116 if (r)
|
Chris@16
|
117 buffering.buffer_copy();
|
Chris@16
|
118 return r;
|
Chris@16
|
119 }
|
Chris@16
|
120 }
|
Chris@16
|
121
|
Chris@16
|
122 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
123 // The simple left alignment directive is used for right_align[...]
|
Chris@16
|
124 // generators. It uses default values for the generated width (defined via
|
Chris@16
|
125 // the BOOST_KARMA_DEFAULT_FIELD_LENGTH constant) and for the padding
|
Chris@16
|
126 // generator (always spaces).
|
Chris@16
|
127 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
128 template <typename Subject, typename Width = detail::default_width>
|
Chris@16
|
129 struct simple_right_alignment
|
Chris@16
|
130 : unary_generator<simple_right_alignment<Subject, Width> >
|
Chris@16
|
131 {
|
Chris@16
|
132 typedef Subject subject_type;
|
Chris@16
|
133
|
Chris@16
|
134 typedef mpl::int_<
|
Chris@16
|
135 generator_properties::countingbuffer | subject_type::properties::value
|
Chris@16
|
136 > properties;
|
Chris@16
|
137
|
Chris@16
|
138 template <typename Context, typename Iterator>
|
Chris@16
|
139 struct attribute
|
Chris@16
|
140 : traits::attribute_of<subject_type, Context, Iterator>
|
Chris@16
|
141 {};
|
Chris@16
|
142
|
Chris@16
|
143 simple_right_alignment(Subject const& subject, Width width = Width())
|
Chris@16
|
144 : subject(subject), width(width) {}
|
Chris@16
|
145
|
Chris@16
|
146 template <typename OutputIterator, typename Context, typename Delimiter
|
Chris@16
|
147 , typename Attribute>
|
Chris@16
|
148 bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d
|
Chris@16
|
149 , Attribute const& attr) const
|
Chris@16
|
150 {
|
Chris@16
|
151 return detail::right_align_generate(sink, ctx, d, attr,
|
Chris@16
|
152 subject, width, compile<karma::domain>(' '));
|
Chris@16
|
153 }
|
Chris@16
|
154
|
Chris@16
|
155 template <typename Context>
|
Chris@16
|
156 info what(Context& context) const
|
Chris@16
|
157 {
|
Chris@16
|
158 return info("right_align", subject.what(context));
|
Chris@16
|
159 }
|
Chris@16
|
160
|
Chris@16
|
161 Subject subject;
|
Chris@16
|
162 Width width;
|
Chris@16
|
163 };
|
Chris@16
|
164
|
Chris@16
|
165 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
166 // The left alignment directive with padding, is used for generators like
|
Chris@16
|
167 // right_align(padding)[...], where padding is a arbitrary generator
|
Chris@16
|
168 // expression. It uses a default value for the generated width (defined
|
Chris@16
|
169 // via the BOOST_KARMA_DEFAULT_FIELD_LENGTH constant).
|
Chris@16
|
170 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
171 template <typename Subject, typename Padding
|
Chris@16
|
172 , typename Width = detail::default_width>
|
Chris@16
|
173 struct padding_right_alignment
|
Chris@16
|
174 : unary_generator<padding_right_alignment<Subject, Padding, Width> >
|
Chris@16
|
175 {
|
Chris@16
|
176 typedef Subject subject_type;
|
Chris@16
|
177 typedef Padding padding_type;
|
Chris@16
|
178
|
Chris@16
|
179 typedef mpl::int_<
|
Chris@16
|
180 generator_properties::countingbuffer |
|
Chris@16
|
181 subject_type::properties::value | padding_type::properties::value
|
Chris@16
|
182 > properties;
|
Chris@16
|
183
|
Chris@16
|
184 template <typename Context, typename Iterator>
|
Chris@16
|
185 struct attribute
|
Chris@16
|
186 : traits::attribute_of<subject_type, Context, Iterator>
|
Chris@16
|
187 {};
|
Chris@16
|
188
|
Chris@16
|
189 padding_right_alignment(Subject const& subject, Padding const& padding
|
Chris@16
|
190 , Width width = Width())
|
Chris@16
|
191 : subject(subject), padding(padding), width(width) {}
|
Chris@16
|
192
|
Chris@16
|
193 template <typename OutputIterator, typename Context, typename Delimiter
|
Chris@16
|
194 , typename Attribute>
|
Chris@16
|
195 bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d
|
Chris@16
|
196 , Attribute const& attr) const
|
Chris@16
|
197 {
|
Chris@16
|
198 return detail::right_align_generate(sink, ctx, d, attr,
|
Chris@16
|
199 subject, width, padding);
|
Chris@16
|
200 }
|
Chris@16
|
201
|
Chris@16
|
202 template <typename Context>
|
Chris@16
|
203 info what(Context& context) const
|
Chris@16
|
204 {
|
Chris@16
|
205 return info("right_align", subject.what(context));
|
Chris@16
|
206 }
|
Chris@16
|
207
|
Chris@16
|
208 Subject subject;
|
Chris@16
|
209 Padding padding;
|
Chris@16
|
210 Width width;
|
Chris@16
|
211 };
|
Chris@16
|
212
|
Chris@16
|
213 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
214 // Generator generators: make_xxx function (objects)
|
Chris@16
|
215 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
216
|
Chris@16
|
217 // creates right_align[] directive generator
|
Chris@16
|
218 template <typename Subject, typename Modifiers>
|
Chris@16
|
219 struct make_directive<tag::right_align, Subject, Modifiers>
|
Chris@16
|
220 {
|
Chris@16
|
221 typedef simple_right_alignment<Subject> result_type;
|
Chris@16
|
222 result_type operator()(unused_type, Subject const& subject
|
Chris@16
|
223 , unused_type) const
|
Chris@16
|
224 {
|
Chris@16
|
225 return result_type(subject);
|
Chris@16
|
226 }
|
Chris@16
|
227 };
|
Chris@16
|
228
|
Chris@16
|
229 // creates right_align(width)[] directive generator
|
Chris@16
|
230 template <typename Width, typename Subject, typename Modifiers>
|
Chris@16
|
231 struct make_directive<
|
Chris@16
|
232 terminal_ex<tag::right_align, fusion::vector1<Width> >
|
Chris@16
|
233 , Subject, Modifiers
|
Chris@16
|
234 , typename enable_if_c< integer_traits<Width>::is_integral >::type>
|
Chris@16
|
235 {
|
Chris@16
|
236 typedef simple_right_alignment<Subject, Width> result_type;
|
Chris@16
|
237
|
Chris@16
|
238 template <typename Terminal>
|
Chris@16
|
239 result_type operator()(Terminal const& term, Subject const& subject
|
Chris@16
|
240 , unused_type) const
|
Chris@16
|
241 {
|
Chris@16
|
242 return result_type(subject, fusion::at_c<0>(term.args));
|
Chris@16
|
243 }
|
Chris@16
|
244 };
|
Chris@16
|
245
|
Chris@16
|
246 // creates right_align(pad)[] directive generator
|
Chris@16
|
247 template <typename Padding, typename Subject, typename Modifiers>
|
Chris@16
|
248 struct make_directive<
|
Chris@16
|
249 terminal_ex<tag::right_align, fusion::vector1<Padding> >
|
Chris@16
|
250 , Subject, Modifiers
|
Chris@16
|
251 , typename enable_if<
|
Chris@16
|
252 mpl::and_<
|
Chris@16
|
253 spirit::traits::matches<karma::domain, Padding>,
|
Chris@16
|
254 mpl::not_<mpl::bool_<integer_traits<Padding>::is_integral> >
|
Chris@16
|
255 >
|
Chris@16
|
256 >::type>
|
Chris@16
|
257 {
|
Chris@16
|
258 typedef typename
|
Chris@16
|
259 result_of::compile<karma::domain, Padding, Modifiers>::type
|
Chris@16
|
260 padding_type;
|
Chris@16
|
261
|
Chris@16
|
262 typedef padding_right_alignment<Subject, padding_type> result_type;
|
Chris@16
|
263
|
Chris@16
|
264 template <typename Terminal>
|
Chris@16
|
265 result_type operator()(Terminal const& term, Subject const& subject
|
Chris@16
|
266 , Modifiers const& modifiers) const
|
Chris@16
|
267 {
|
Chris@16
|
268 return result_type(subject
|
Chris@16
|
269 , compile<karma::domain>(fusion::at_c<0>(term.args), modifiers));
|
Chris@16
|
270 }
|
Chris@16
|
271 };
|
Chris@16
|
272
|
Chris@16
|
273 // creates right_align(width, pad)[] directive generator
|
Chris@16
|
274 template <typename Width, typename Padding, typename Subject
|
Chris@16
|
275 , typename Modifiers>
|
Chris@16
|
276 struct make_directive<
|
Chris@16
|
277 terminal_ex<tag::right_align, fusion::vector2<Width, Padding> >
|
Chris@16
|
278 , Subject, Modifiers>
|
Chris@16
|
279 {
|
Chris@16
|
280 typedef typename
|
Chris@16
|
281 result_of::compile<karma::domain, Padding, Modifiers>::type
|
Chris@16
|
282 padding_type;
|
Chris@16
|
283
|
Chris@16
|
284 typedef padding_right_alignment<Subject, padding_type, Width> result_type;
|
Chris@16
|
285
|
Chris@16
|
286 template <typename Terminal>
|
Chris@16
|
287 result_type operator()(Terminal const& term, Subject const& subject
|
Chris@16
|
288 , Modifiers const& modifiers) const
|
Chris@16
|
289 {
|
Chris@16
|
290 return result_type(subject
|
Chris@16
|
291 , compile<karma::domain>(fusion::at_c<1>(term.args), modifiers)
|
Chris@16
|
292 , fusion::at_c<0>(term.args));
|
Chris@16
|
293 }
|
Chris@16
|
294 };
|
Chris@16
|
295
|
Chris@16
|
296 }}} // namespace boost::spirit::karma
|
Chris@16
|
297
|
Chris@16
|
298 namespace boost { namespace spirit { namespace traits
|
Chris@16
|
299 {
|
Chris@16
|
300 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
301 template <typename Subject, typename Width>
|
Chris@16
|
302 struct has_semantic_action<karma::simple_right_alignment<Subject, Width> >
|
Chris@16
|
303 : unary_has_semantic_action<Subject> {};
|
Chris@16
|
304
|
Chris@16
|
305 template <typename Subject, typename Padding, typename Width>
|
Chris@16
|
306 struct has_semantic_action<
|
Chris@16
|
307 karma::padding_right_alignment<Subject, Padding, Width> >
|
Chris@16
|
308 : unary_has_semantic_action<Subject> {};
|
Chris@16
|
309
|
Chris@16
|
310 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
311 template <typename Subject, typename Width, typename Attribute
|
Chris@16
|
312 , typename Context, typename Iterator>
|
Chris@16
|
313 struct handles_container<
|
Chris@16
|
314 karma::simple_right_alignment<Subject, Width>
|
Chris@16
|
315 , Attribute, Context, Iterator>
|
Chris@16
|
316 : unary_handles_container<Subject, Attribute, Context, Iterator> {};
|
Chris@16
|
317
|
Chris@16
|
318 template <typename Subject, typename Padding, typename Width
|
Chris@16
|
319 , typename Attribute, typename Context, typename Iterator>
|
Chris@16
|
320 struct handles_container<
|
Chris@16
|
321 karma::padding_right_alignment<Subject, Padding, Width>
|
Chris@16
|
322 , Attribute, Context, Iterator>
|
Chris@16
|
323 : unary_handles_container<Subject, Attribute, Context, Iterator> {};
|
Chris@16
|
324 }}}
|
Chris@16
|
325
|
Chris@16
|
326 #endif
|
Chris@16
|
327
|