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_PADDING_MAY_06_2008_0436PM)
7 #define BOOST_SPIRIT_KARMA_PADDING_MAY_06_2008_0436PM
13 #include <boost/spirit/home/support/common_terminals.hpp>
14 #include <boost/spirit/home/support/info.hpp>
16 #include <boost/spirit/home/karma/domain.hpp>
17 #include <boost/spirit/home/karma/meta_compiler.hpp>
18 #include <boost/spirit/home/karma/delimit_out.hpp>
19 #include <boost/spirit/home/karma/auxiliary/lazy.hpp>
20 #include <boost/spirit/home/karma/detail/generate_to.hpp>
21 #include <boost/spirit/home/support/unused.hpp>
22 #include <boost/fusion/include/at.hpp>
23 #include <boost/fusion/include/vector.hpp>
25 ///////////////////////////////////////////////////////////////////////////////
26 namespace boost { namespace spirit
28 ///////////////////////////////////////////////////////////////////////////
30 ///////////////////////////////////////////////////////////////////////////
33 template <typename A0>
34 struct use_terminal<karma::domain
35 , terminal_ex<tag::pad, fusion::vector1<A0> > >
38 // enables lazy pad(...)
40 struct use_lazy_terminal<karma::domain, tag::pad, 1>
45 ///////////////////////////////////////////////////////////////////////////////
46 namespace boost { namespace spirit { namespace karma
48 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
49 using boost::spirit::pad;
51 using boost::spirit::pad_type;
53 struct binary_padding_generator
54 : primitive_generator<binary_padding_generator>
56 typedef mpl::int_<generator_properties::tracking> properties;
58 template <typename Context, typename Unused>
61 typedef unused_type type;
64 binary_padding_generator(int numpadbytes)
65 : numpadbytes_(numpadbytes)
69 typename OutputIterator, typename Context, typename Delimiter
71 bool generate(OutputIterator& sink, Context&, Delimiter const& d
72 , Attribute const& /*attr*/) const
74 std::size_t count = sink.get_out_count() % numpadbytes_;
76 count = numpadbytes_ - count;
79 while (result && count-- != 0)
80 result = detail::generate_to(sink, '\0');
83 result = karma::delimit_out(sink, d); // always do post-delimiting
87 template <typename Context>
88 static info what(Context const&)
96 ///////////////////////////////////////////////////////////////////////////
97 // Generator generators: make_xxx function (objects)
98 ///////////////////////////////////////////////////////////////////////////
99 template <typename Modifiers, typename A0>
100 struct make_primitive<
101 terminal_ex<tag::pad, fusion::vector1<A0> >
104 typedef binary_padding_generator result_type;
106 template <typename Terminal>
107 result_type operator()(Terminal const& term, unused_type) const
109 return result_type(fusion::at_c<0>(term.args));