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_MAXWIDTH_MAR_18_2009_0827AM)
7 #define BOOST_SPIRIT_KARMA_MAXWIDTH_MAR_18_2009_0827AM
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>
32 ///////////////////////////////////////////////////////////////////////////////
33 namespace boost { namespace spirit
35 ///////////////////////////////////////////////////////////////////////////
37 ///////////////////////////////////////////////////////////////////////////
41 struct use_directive<karma::domain, tag::maxwidth>
44 // enables maxwidth(w)[g], where w provides a maxwidth
46 struct use_directive<karma::domain
47 , terminal_ex<tag::maxwidth, fusion::vector1<T> > >
50 // enables *lazy* maxwidth(w)[g], where w provides a maxwidth
52 struct use_lazy_directive<karma::domain, tag::maxwidth, 1>
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
58 template <typename T, typename RestIter>
59 struct use_directive<karma::domain
60 , terminal_ex<tag::maxwidth, fusion::vector2<T, RestIter> > >
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
67 struct use_lazy_directive<karma::domain, tag::maxwidth, 2>
72 ///////////////////////////////////////////////////////////////////////////////
73 namespace boost { namespace spirit { namespace karma
75 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
76 using spirit::maxwidth;
78 using spirit::maxwidth_type;
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)
87 return buff.buffer_copy_rest(dest, start_at);
90 template <typename OutputIterator>
91 bool buffer_copy_rest(detail::enable_buffering<OutputIterator>&
92 , std::size_t, unused_type)
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>
104 maxwidth_generate(OutputIterator& sink, Context& ctx,
105 Delimiter const& d, Attribute const& attr, Embedded const& e,
106 unsigned int const maxwidth, Rest& restdest)
108 #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
109 e; // suppresses warning: C4100: 'e' : unreferenced formal parameter
111 // wrap the given output iterator to allow buffering, but disable
113 detail::enable_buffering<OutputIterator> buffering(sink);
115 // generate the underlying output and copy the embedded
116 // output to the target output iterator applying the given
120 detail::disable_counting<OutputIterator> nocounting(sink);
121 r = e.generate(sink, ctx, d, attr);
122 } // re-enable counting
124 return r && buffering.buffer_copy(maxwidth) &&
125 buffer_copy_rest(buffering, maxwidth, restdest);
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).
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> >
142 typedef Subject subject_type;
145 generator_properties::countingbuffer | subject_type::properties::value
148 template <typename Context, typename Iterator>
150 : traits::attribute_of<subject_type, Context, Iterator>
153 maxwidth_width(Subject const& subject, Width const& w = Width()
154 , Rest const& r = Rest())
155 : subject(subject), width(w), rest(r) {}
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
162 return detail::maxwidth_generate(sink, ctx, d, attr, subject
166 template <typename Context>
167 info what(Context& context) const
169 return info("maxwidth", subject.what(context));
177 ///////////////////////////////////////////////////////////////////////////
178 // Generator generators: make_xxx function (objects)
179 ///////////////////////////////////////////////////////////////////////////
181 // creates maxwidth[] directive generator
182 template <typename Subject, typename Modifiers>
183 struct make_directive<tag::maxwidth, Subject, Modifiers>
185 typedef maxwidth_width<Subject> result_type;
186 result_type operator()(unused_type, Subject const& subject
189 return result_type(subject);
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>
199 typedef maxwidth_width<Subject, T> result_type;
201 template <typename Terminal>
202 result_type operator()(Terminal const& term, Subject const& subject
205 return result_type(subject, fusion::at_c<0>(term.args), unused);
209 // creates maxwidth(width, restiter)[] directive generator
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>
216 typedef maxwidth_width<Subject, T, RestIter> result_type;
218 template <typename Terminal>
219 result_type operator()(Terminal const& term, Subject const& subject
222 return result_type(subject, fusion::at_c<0>(term.args)
223 , fusion::at_c<1>(term.args));
227 }}} // namespace boost::spirit::karma
229 namespace boost { namespace spirit { namespace traits
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> {};
236 ///////////////////////////////////////////////////////////////////////////
237 template <typename Subject, typename Attribute, typename Context
239 struct handles_container<karma::maxwidth_width<Subject>, Attribute
241 : unary_handles_container<Subject, Attribute, Context, Iterator> {};