]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/include/boost/spirit/home/karma/directive/columns.hpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / spirit / include / boost / spirit / home / karma / directive / columns.hpp
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_COLUMNS_DEC_03_2009_0736AM)
7 #define BOOST_SPIRIT_KARMA_COLUMNS_DEC_03_2009_0736AM
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/delimit_out.hpp>
17 #include <boost/spirit/home/karma/detail/default_width.hpp>
18 #include <boost/spirit/home/karma/auxiliary/eol.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/fusion/include/at.hpp>
27 #include <boost/fusion/include/vector.hpp>
28 #include <boost/integer_traits.hpp>
29
30 namespace boost { namespace spirit
31 {
32 ///////////////////////////////////////////////////////////////////////////
33 // Enablers
34 ///////////////////////////////////////////////////////////////////////////
35 template <>
36 struct use_directive<karma::domain, tag::columns> // enables columns[]
37 : mpl::true_ {};
38
39 // enables columns(c)[g], where c provides the number of require columns
40 template <typename T>
41 struct use_directive<karma::domain
42 , terminal_ex<tag::columns, fusion::vector1<T> > >
43 : mpl::true_ {};
44
45 // enables *lazy* columns(c)[g]
46 template <>
47 struct use_lazy_directive<karma::domain, tag::columns, 1>
48 : mpl::true_ {};
49
50 // enables columns(c, d)[g], where c provides the number of require columns
51 // and d is the custom column-delimiter (default is karma::endl)
52 template <typename T1, typename T2>
53 struct use_directive<karma::domain
54 , terminal_ex<tag::columns, fusion::vector2<T1, T2> > >
55 : boost::spirit::traits::matches<karma::domain, T2> {};
56
57 // enables *lazy* columns(c, d)[g]
58 template <>
59 struct use_lazy_directive<karma::domain, tag::columns, 2>
60 : mpl::true_ {};
61
62 }}
63
64 namespace boost { namespace spirit { namespace karma
65 {
66 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
67 using spirit::columns;
68 #endif
69 using spirit::columns_type;
70
71 namespace detail
72 {
73 template <typename Delimiter, typename ColumnDelimiter>
74 struct columns_delimiter
75 {
76 columns_delimiter(Delimiter const& delim
77 , ColumnDelimiter const& cdelim, unsigned int const numcols)
78 : delimiter(delim), column_delimiter(cdelim)
79 , numcolumns(numcols), count(0) {}
80
81 template <typename OutputIterator, typename Context
82 , typename Delimiter_, typename Attribute>
83 bool generate(OutputIterator& sink, Context&, Delimiter_ const&
84 , Attribute const&) const
85 {
86 // first invoke the embedded delimiter
87 if (!karma::delimit_out(sink, delimiter))
88 return false;
89
90 // now we count the number of invocations and emit the column
91 // delimiter if needed
92 if ((++count % numcolumns) == 0)
93 return karma::delimit_out(sink, column_delimiter);
94 return true;
95 }
96
97 // generate a final column delimiter if the last invocation didn't
98 // emit one
99 template <typename OutputIterator>
100 bool delimit_out(OutputIterator& sink) const
101 {
102 if (count % numcolumns)
103 return karma::delimit_out(sink, column_delimiter);
104 return true;
105 }
106
107 Delimiter const& delimiter;
108 ColumnDelimiter const& column_delimiter;
109 unsigned int const numcolumns;
110 mutable unsigned int count;
111
112 private:
113 // silence MSVC warning C4512: assignment operator could not be generated
114 columns_delimiter& operator= (columns_delimiter const&);
115 };
116 }
117
118 ///////////////////////////////////////////////////////////////////////////
119 // The columns_generator is used for columns(c, d)[...] directives.
120 ///////////////////////////////////////////////////////////////////////////
121 template <typename Subject, typename NumColumns, typename ColumnsDelimiter>
122 struct columns_generator
123 : unary_generator<columns_generator<Subject, NumColumns, ColumnsDelimiter> >
124 {
125 typedef Subject subject_type;
126 typedef ColumnsDelimiter delimiter_type;
127
128 typedef mpl::int_<
129 subject_type::properties::value | delimiter_type::properties::value
130 > properties;
131
132 template <typename Context, typename Iterator>
133 struct attribute
134 : traits::attribute_of<subject_type, Context, Iterator>
135 {};
136
137 columns_generator(Subject const& subject, NumColumns const& cols
138 , ColumnsDelimiter const& cdelimiter)
139 : subject(subject), numcolumns(cols), column_delimiter(cdelimiter)
140 {
141 // having zero number of columns doesn't make any sense
142 BOOST_ASSERT(numcolumns > 0);
143 }
144
145 template <typename OutputIterator, typename Context
146 , typename Delimiter, typename Attribute>
147 bool generate(OutputIterator& sink, Context& ctx
148 , Delimiter const& delimiter, Attribute const& attr) const
149 {
150 // The columns generator dispatches to the embedded generator
151 // while supplying a new delimiter to use, wrapping the outer
152 // delimiter.
153 typedef detail::columns_delimiter<
154 Delimiter, ColumnsDelimiter
155 > columns_delimiter_type;
156
157 columns_delimiter_type d(delimiter, column_delimiter, numcolumns);
158 return subject.generate(sink, ctx, d, attr) && d.delimit_out(sink);
159 }
160
161 template <typename Context>
162 info what(Context& context) const
163 {
164 return info("columns", subject.what(context));
165 }
166
167 Subject subject;
168 NumColumns numcolumns;
169 ColumnsDelimiter column_delimiter;
170 };
171
172 ///////////////////////////////////////////////////////////////////////////
173 // Generator generators: make_xxx function (objects)
174 ///////////////////////////////////////////////////////////////////////////
175
176 // creates columns[] directive
177 template <typename Subject, typename Modifiers>
178 struct make_directive<tag::columns, Subject, Modifiers>
179 {
180 typedef typename
181 result_of::compile<karma::domain, eol_type, Modifiers>::type
182 columns_delimiter_type;
183 typedef columns_generator<
184 Subject, detail::default_columns, columns_delimiter_type>
185 result_type;
186
187 result_type operator()(unused_type, Subject const& subject
188 , unused_type) const
189 {
190 #if defined(BOOST_SPIRIT_NO_PREDEFINED_TERMINALS)
191 eol_type const eol = eol_type();
192 #endif
193 return result_type(subject, detail::default_columns()
194 , compile<karma::domain>(eol));
195 }
196 };
197
198 // creates columns(c)[] directive generator (c is the number of columns)
199 template <typename T, typename Subject, typename Modifiers>
200 struct make_directive<
201 terminal_ex<tag::columns, fusion::vector1<T> >
202 , Subject, Modifiers
203 , typename enable_if_c<integer_traits<T>::is_integral>::type>
204 {
205 typedef typename
206 result_of::compile<karma::domain, eol_type, Modifiers>::type
207 columns_delimiter_type;
208 typedef columns_generator<
209 Subject, T, columns_delimiter_type
210 > result_type;
211
212 template <typename Terminal>
213 result_type operator()(Terminal const& term, Subject const& subject
214 , unused_type) const
215 {
216 #if defined(BOOST_SPIRIT_NO_PREDEFINED_TERMINALS)
217 eol_type const eol = eol_type();
218 #endif
219 return result_type(subject, fusion::at_c<0>(term.args)
220 , compile<karma::domain>(eol));
221 }
222 };
223
224 // creates columns(d)[] directive generator (d is the column delimiter)
225 template <typename T, typename Subject, typename Modifiers>
226 struct make_directive<
227 terminal_ex<tag::columns, fusion::vector1<T> >
228 , Subject, Modifiers
229 , typename enable_if<
230 mpl::and_<
231 spirit::traits::matches<karma::domain, T>,
232 mpl::not_<mpl::bool_<integer_traits<T>::is_integral> >
233 >
234 >::type>
235 {
236 typedef typename
237 result_of::compile<karma::domain, T, Modifiers>::type
238 columns_delimiter_type;
239 typedef columns_generator<
240 Subject, detail::default_columns, columns_delimiter_type
241 > result_type;
242
243 template <typename Terminal>
244 result_type operator()(Terminal const& term, Subject const& subject
245 , unused_type) const
246 {
247 return result_type(subject, detail::default_columns()
248 , compile<karma::domain>(fusion::at_c<0>(term.args)));
249 }
250 };
251
252 // creates columns(c, d)[] directive generator (c is the number of columns
253 // and d is the column delimiter)
254 template <typename T1, typename T2, typename Subject, typename Modifiers>
255 struct make_directive<
256 terminal_ex<tag::columns, fusion::vector2<T1, T2> >
257 , Subject, Modifiers>
258 {
259 typedef typename
260 result_of::compile<karma::domain, T2, Modifiers>::type
261 columns_delimiter_type;
262 typedef columns_generator<
263 Subject, T1, columns_delimiter_type
264 > result_type;
265
266 template <typename Terminal>
267 result_type operator()(Terminal const& term, Subject const& subject
268 , unused_type) const
269 {
270 return result_type (subject, fusion::at_c<0>(term.args)
271 , compile<karma::domain>(fusion::at_c<1>(term.args)));
272 }
273 };
274
275 }}}
276
277 namespace boost { namespace spirit { namespace traits
278 {
279 ///////////////////////////////////////////////////////////////////////////
280 template <typename Subject, typename T1, typename T2>
281 struct has_semantic_action<karma::columns_generator<Subject, T1, T2> >
282 : unary_has_semantic_action<Subject> {};
283
284 ///////////////////////////////////////////////////////////////////////////
285 template <typename Subject, typename T1, typename T2, typename Attribute
286 , typename Context, typename Iterator>
287 struct handles_container<
288 karma::columns_generator<Subject, T1, T2>, Attribute
289 , Context, Iterator>
290 : unary_handles_container<Subject, Attribute, Context, Iterator> {};
291 }}}
292
293 #endif