]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/include/boost/spirit/home/karma/directive/maxwidth.hpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / spirit / include / boost / spirit / home / karma / directive / maxwidth.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_MAXWIDTH_MAR_18_2009_0827AM)
7 #define BOOST_SPIRIT_KARMA_MAXWIDTH_MAR_18_2009_0827AM
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>
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/lexical_cast.hpp>
30 #include <boost/detail/workaround.hpp>
31
32 ///////////////////////////////////////////////////////////////////////////////
33 namespace boost { namespace spirit
34 {
35 ///////////////////////////////////////////////////////////////////////////
36 // Enablers
37 ///////////////////////////////////////////////////////////////////////////
38
39 // enables maxwidth[]
40 template <>
41 struct use_directive<karma::domain, tag::maxwidth>
42 : mpl::true_ {};
43
44 // enables maxwidth(w)[g], where w provides a maxwidth
45 template <typename T>
46 struct use_directive<karma::domain
47 , terminal_ex<tag::maxwidth, fusion::vector1<T> > >
48 : mpl::true_ {};
49
50 // enables *lazy* maxwidth(w)[g], where w provides a maxwidth
51 template <>
52 struct use_lazy_directive<karma::domain, tag::maxwidth, 1>
53 : mpl::true_ {};
54
55 // enables maxwidth(w, r)[g], where w provides a maxwidth and r is an output
56 // iterator used to receive the rest of the output not fitting into the
57 // maxwidth limit
58 template <typename T, typename RestIter>
59 struct use_directive<karma::domain
60 , terminal_ex<tag::maxwidth, fusion::vector2<T, RestIter> > >
61 : mpl::true_ {};
62
63 // enables *lazy* maxwidth(w, r)[g], where w provides a maxwidth and r is
64 // an output iterator used to receive the rest of the output not fitting
65 // into the maxwidth limit
66 template <>
67 struct use_lazy_directive<karma::domain, tag::maxwidth, 2>
68 : mpl::true_ {};
69
70 }}
71
72 ///////////////////////////////////////////////////////////////////////////////
73 namespace boost { namespace spirit { namespace karma
74 {
75 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
76 using spirit::maxwidth;
77 #endif
78 using spirit::maxwidth_type;
79
80 namespace detail
81 {
82 ///////////////////////////////////////////////////////////////////////
83 template <typename OutputIterator, typename RestIterator>
84 bool buffer_copy_rest(detail::enable_buffering<OutputIterator>& buff
85 , std::size_t start_at, RestIterator& dest)
86 {
87 return buff.buffer_copy_rest(dest, start_at);
88 }
89
90 template <typename OutputIterator>
91 bool buffer_copy_rest(detail::enable_buffering<OutputIterator>&
92 , std::size_t, unused_type)
93 {
94 return true;
95 }
96
97 ///////////////////////////////////////////////////////////////////////
98 // The maxwidth_generate template function is used for all the
99 // different flavors of the maxwidth[] directive.
100 ///////////////////////////////////////////////////////////////////////
101 template <typename OutputIterator, typename Context, typename Delimiter,
102 typename Attribute, typename Embedded, typename Rest>
103 inline static bool
104 maxwidth_generate(OutputIterator& sink, Context& ctx,
105 Delimiter const& d, Attribute const& attr, Embedded const& e,
106 unsigned int const maxwidth, Rest& restdest)
107 {
108 #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
109 e; // suppresses warning: C4100: 'e' : unreferenced formal parameter
110 #endif
111 // wrap the given output iterator to allow buffering, but disable
112 // counting
113 detail::enable_buffering<OutputIterator> buffering(sink);
114
115 // generate the underlying output and copy the embedded
116 // output to the target output iterator applying the given
117 // maxwidth
118 bool r = false;
119 {
120 detail::disable_counting<OutputIterator> nocounting(sink);
121 r = e.generate(sink, ctx, d, attr);
122 } // re-enable counting
123
124 return r && buffering.buffer_copy(maxwidth) &&
125 buffer_copy_rest(buffering, maxwidth, restdest);
126 }
127 }
128
129 ///////////////////////////////////////////////////////////////////////////
130 // The maxwidth directive is used for maxwidth[...]
131 // generators. It uses default values for the generated width (defined via
132 // the BOOST_KARMA_DEFAULT_FIELD_MAXWIDTH constant).
133 //
134 // The maxwidth with width directive, is used for generators
135 // like maxwidth(width)[...].
136 ///////////////////////////////////////////////////////////////////////////
137 template <typename Subject, typename Width = detail::default_max_width
138 , typename Rest = unused_type>
139 struct maxwidth_width
140 : unary_generator<maxwidth_width<Subject, Width, Rest> >
141 {
142 typedef Subject subject_type;
143
144 typedef mpl::int_<
145 generator_properties::countingbuffer | subject_type::properties::value
146 > properties;
147
148 template <typename Context, typename Iterator>
149 struct attribute
150 : traits::attribute_of<subject_type, Context, Iterator>
151 {};
152
153 maxwidth_width(Subject const& subject, Width const& w = Width()
154 , Rest const& r = Rest())
155 : subject(subject), width(w), rest(r) {}
156
157 template <typename OutputIterator, typename Context, typename Delimiter
158 , typename Attribute>
159 bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d
160 , Attribute const& attr) const
161 {
162 return detail::maxwidth_generate(sink, ctx, d, attr, subject
163 , width, rest);
164 }
165
166 template <typename Context>
167 info what(Context& context) const
168 {
169 return info("maxwidth", subject.what(context));
170 }
171
172 Subject subject;
173 Width width;
174 Rest rest;
175 };
176
177 ///////////////////////////////////////////////////////////////////////////
178 // Generator generators: make_xxx function (objects)
179 ///////////////////////////////////////////////////////////////////////////
180
181 // creates maxwidth[] directive generator
182 template <typename Subject, typename Modifiers>
183 struct make_directive<tag::maxwidth, Subject, Modifiers>
184 {
185 typedef maxwidth_width<Subject> result_type;
186 result_type operator()(unused_type, Subject const& subject
187 , unused_type) const
188 {
189 return result_type(subject);
190 }
191 };
192
193 // creates maxwidth(width)[] directive generator
194 template <typename T, typename Subject, typename Modifiers>
195 struct make_directive<
196 terminal_ex<tag::maxwidth, fusion::vector1<T> >
197 , Subject, Modifiers>
198 {
199 typedef maxwidth_width<Subject, T> result_type;
200
201 template <typename Terminal>
202 result_type operator()(Terminal const& term, Subject const& subject
203 , unused_type) const
204 {
205 return result_type(subject, fusion::at_c<0>(term.args), unused);
206 }
207 };
208
209 // creates maxwidth(width, restiter)[] directive generator
210 template <
211 typename T, typename RestIter, typename Subject, typename Modifiers>
212 struct make_directive<
213 terminal_ex<tag::maxwidth, fusion::vector2<T, RestIter> >
214 , Subject, Modifiers>
215 {
216 typedef maxwidth_width<Subject, T, RestIter> result_type;
217
218 template <typename Terminal>
219 result_type operator()(Terminal const& term, Subject const& subject
220 , unused_type) const
221 {
222 return result_type(subject, fusion::at_c<0>(term.args)
223 , fusion::at_c<1>(term.args));
224 }
225 };
226
227 }}} // namespace boost::spirit::karma
228
229 namespace boost { namespace spirit { namespace traits
230 {
231 ///////////////////////////////////////////////////////////////////////////
232 template <typename Subject, typename Width, typename Rest>
233 struct has_semantic_action<karma::maxwidth_width<Subject, Width, Rest> >
234 : unary_has_semantic_action<Subject> {};
235
236 ///////////////////////////////////////////////////////////////////////////
237 template <typename Subject, typename Attribute, typename Context
238 , typename Iterator>
239 struct handles_container<karma::maxwidth_width<Subject>, Attribute
240 , Context, Iterator>
241 : unary_handles_container<Subject, Attribute, Context, Iterator> {};
242 }}}
243
244 #endif
245
246