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