1 // Copyright (c) 2001-2011 Hartmut Kaiser
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #if !defined(BOOST_SPIRIT_KARMA_RIGHT_ALIGNMENT_FEB_27_2007_1216PM)
7 #define BOOST_SPIRIT_KARMA_RIGHT_ALIGNMENT_FEB_27_2007_1216PM
13 #include <boost/spirit/home/karma/meta_compiler.hpp>
14 #include <boost/spirit/home/karma/generator.hpp>
15 #include <boost/spirit/home/karma/domain.hpp>
16 #include <boost/spirit/home/karma/detail/output_iterator.hpp>
17 #include <boost/spirit/home/karma/detail/default_width.hpp>
18 #include <boost/spirit/home/karma/delimit_out.hpp>
19 #include <boost/spirit/home/karma/auxiliary/lazy.hpp>
20 #include <boost/spirit/home/support/unused.hpp>
21 #include <boost/spirit/home/support/common_terminals.hpp>
22 #include <boost/spirit/home/support/has_semantic_action.hpp>
23 #include <boost/spirit/home/support/handles_container.hpp>
24 #include <boost/spirit/home/karma/detail/attributes.hpp>
25 #include <boost/spirit/home/support/info.hpp>
26 #include <boost/spirit/home/support/unused.hpp>
27 #include <boost/fusion/include/at.hpp>
28 #include <boost/fusion/include/vector.hpp>
29 #include <boost/integer_traits.hpp>
30 #include <boost/mpl/bool.hpp>
31 #include <boost/utility/enable_if.hpp>
32 #include <boost/detail/workaround.hpp>
34 ///////////////////////////////////////////////////////////////////////////////
35 namespace boost { namespace spirit
37 ///////////////////////////////////////////////////////////////////////////
39 ///////////////////////////////////////////////////////////////////////////
41 // enables right_align[]
43 struct use_directive<karma::domain, tag::right_align>
46 // enables right_align(d)[g] and right_align(w)[g], where d is a generator
47 // and w is a maximum width
49 struct use_directive<karma::domain
50 , terminal_ex<tag::right_align, fusion::vector1<T> > >
53 // enables *lazy* right_align(d)[g], where d provides a generator
55 struct use_lazy_directive<karma::domain, tag::right_align, 1>
58 // enables right_align(w, d)[g], where d is a generator and w is a maximum
60 template <typename Width, typename Padding>
61 struct use_directive<karma::domain
62 , terminal_ex<tag::right_align, fusion::vector2<Width, Padding> > >
63 : spirit::traits::matches<karma::domain, Padding> {};
65 // enables *lazy* right_align(w, d)[g], where d provides a generator and w
68 struct use_lazy_directive<karma::domain, tag::right_align, 2>
73 ///////////////////////////////////////////////////////////////////////////////
74 namespace boost { namespace spirit { namespace karma
76 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
77 using spirit::right_align;
79 using spirit::right_align_type;
83 ///////////////////////////////////////////////////////////////////////
84 // The right_align_generate template function is used for all the
85 // different flavors of the right_align[] directive.
86 ///////////////////////////////////////////////////////////////////////
87 template <typename OutputIterator, typename Context, typename Delimiter,
88 typename Attribute, typename Embedded, typename Padding>
90 right_align_generate(OutputIterator& sink, Context& ctx,
91 Delimiter const& d, Attribute const& attr, Embedded const& e,
92 unsigned int const width, Padding const& p)
94 #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
95 e; // suppresses warning: C4100: 'e' : unreferenced formal parameter
97 // wrap the given output iterator to allow left padding
98 detail::enable_buffering<OutputIterator> buffering(sink, width);
101 // first generate the embedded output
103 detail::disable_counting<OutputIterator> nocounting(sink);
104 r = e.generate(sink, ctx, d, attr);
105 } // re-enable counting
107 buffering.disable(); // do not perform buffering any more
109 // generate the left padding
110 detail::enable_counting<OutputIterator> counting(sink, buffering.buffer_size());
111 while(r && counting.count() < width)
112 r = p.generate(sink, ctx, unused, unused);
114 // copy the buffered output to the target output iterator
116 buffering.buffer_copy();
121 ///////////////////////////////////////////////////////////////////////////
122 // The simple left alignment directive is used for right_align[...]
123 // generators. It uses default values for the generated width (defined via
124 // the BOOST_KARMA_DEFAULT_FIELD_LENGTH constant) and for the padding
125 // generator (always spaces).
126 ///////////////////////////////////////////////////////////////////////////
127 template <typename Subject, typename Width = detail::default_width>
128 struct simple_right_alignment
129 : unary_generator<simple_right_alignment<Subject, Width> >
131 typedef Subject subject_type;
134 generator_properties::countingbuffer | subject_type::properties::value
137 template <typename Context, typename Iterator>
139 : traits::attribute_of<subject_type, Context, Iterator>
142 simple_right_alignment(Subject const& subject, Width width = Width())
143 : subject(subject), width(width) {}
145 template <typename OutputIterator, typename Context, typename Delimiter
146 , typename Attribute>
147 bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d
148 , Attribute const& attr) const
150 return detail::right_align_generate(sink, ctx, d, attr,
151 subject, width, compile<karma::domain>(' '));
154 template <typename Context>
155 info what(Context& context) const
157 return info("right_align", subject.what(context));
164 ///////////////////////////////////////////////////////////////////////////
165 // The left alignment directive with padding, is used for generators like
166 // right_align(padding)[...], where padding is a arbitrary generator
167 // expression. It uses a default value for the generated width (defined
168 // via the BOOST_KARMA_DEFAULT_FIELD_LENGTH constant).
169 ///////////////////////////////////////////////////////////////////////////
170 template <typename Subject, typename Padding
171 , typename Width = detail::default_width>
172 struct padding_right_alignment
173 : unary_generator<padding_right_alignment<Subject, Padding, Width> >
175 typedef Subject subject_type;
176 typedef Padding padding_type;
179 generator_properties::countingbuffer |
180 subject_type::properties::value | padding_type::properties::value
183 template <typename Context, typename Iterator>
185 : traits::attribute_of<subject_type, Context, Iterator>
188 padding_right_alignment(Subject const& subject, Padding const& padding
189 , Width width = Width())
190 : subject(subject), padding(padding), width(width) {}
192 template <typename OutputIterator, typename Context, typename Delimiter
193 , typename Attribute>
194 bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d
195 , Attribute const& attr) const
197 return detail::right_align_generate(sink, ctx, d, attr,
198 subject, width, padding);
201 template <typename Context>
202 info what(Context& context) const
204 return info("right_align", subject.what(context));
212 ///////////////////////////////////////////////////////////////////////////
213 // Generator generators: make_xxx function (objects)
214 ///////////////////////////////////////////////////////////////////////////
216 // creates right_align[] directive generator
217 template <typename Subject, typename Modifiers>
218 struct make_directive<tag::right_align, Subject, Modifiers>
220 typedef simple_right_alignment<Subject> result_type;
221 result_type operator()(unused_type, Subject const& subject
224 return result_type(subject);
228 // creates right_align(width)[] directive generator
229 template <typename Width, typename Subject, typename Modifiers>
230 struct make_directive<
231 terminal_ex<tag::right_align, fusion::vector1<Width> >
233 , typename enable_if_c< integer_traits<Width>::is_integral >::type>
235 typedef simple_right_alignment<Subject, Width> result_type;
237 template <typename Terminal>
238 result_type operator()(Terminal const& term, Subject const& subject
241 return result_type(subject, fusion::at_c<0>(term.args));
245 // creates right_align(pad)[] directive generator
246 template <typename Padding, typename Subject, typename Modifiers>
247 struct make_directive<
248 terminal_ex<tag::right_align, fusion::vector1<Padding> >
250 , typename enable_if<
252 spirit::traits::matches<karma::domain, Padding>,
253 mpl::not_<mpl::bool_<integer_traits<Padding>::is_integral> >
258 result_of::compile<karma::domain, Padding, Modifiers>::type
261 typedef padding_right_alignment<Subject, padding_type> result_type;
263 template <typename Terminal>
264 result_type operator()(Terminal const& term, Subject const& subject
265 , Modifiers const& modifiers) const
267 return result_type(subject
268 , compile<karma::domain>(fusion::at_c<0>(term.args), modifiers));
272 // creates right_align(width, pad)[] directive generator
273 template <typename Width, typename Padding, typename Subject
274 , typename Modifiers>
275 struct make_directive<
276 terminal_ex<tag::right_align, fusion::vector2<Width, Padding> >
277 , Subject, Modifiers>
280 result_of::compile<karma::domain, Padding, Modifiers>::type
283 typedef padding_right_alignment<Subject, padding_type, Width> result_type;
285 template <typename Terminal>
286 result_type operator()(Terminal const& term, Subject const& subject
287 , Modifiers const& modifiers) const
289 return result_type(subject
290 , compile<karma::domain>(fusion::at_c<1>(term.args), modifiers)
291 , fusion::at_c<0>(term.args));
295 }}} // namespace boost::spirit::karma
297 namespace boost { namespace spirit { namespace traits
299 ///////////////////////////////////////////////////////////////////////////
300 template <typename Subject, typename Width>
301 struct has_semantic_action<karma::simple_right_alignment<Subject, Width> >
302 : unary_has_semantic_action<Subject> {};
304 template <typename Subject, typename Padding, typename Width>
305 struct has_semantic_action<
306 karma::padding_right_alignment<Subject, Padding, Width> >
307 : unary_has_semantic_action<Subject> {};
309 ///////////////////////////////////////////////////////////////////////////
310 template <typename Subject, typename Width, typename Attribute
311 , typename Context, typename Iterator>
312 struct handles_container<
313 karma::simple_right_alignment<Subject, Width>
314 , Attribute, Context, Iterator>
315 : unary_handles_container<Subject, Attribute, Context, Iterator> {};
317 template <typename Subject, typename Padding, typename Width
318 , typename Attribute, typename Context, typename Iterator>
319 struct handles_container<
320 karma::padding_right_alignment<Subject, Padding, Width>
321 , Attribute, Context, Iterator>
322 : unary_handles_container<Subject, Attribute, Context, Iterator> {};