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