padding.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. #if !defined(BOOST_SPIRIT_KARMA_PADDING_MAY_06_2008_0436PM)
  6. #define BOOST_SPIRIT_KARMA_PADDING_MAY_06_2008_0436PM
  7. #if defined(_MSC_VER)
  8. #pragma once
  9. #endif
  10. #include <boost/spirit/home/support/common_terminals.hpp>
  11. #include <boost/spirit/home/support/info.hpp>
  12. #include <boost/spirit/home/karma/domain.hpp>
  13. #include <boost/spirit/home/karma/meta_compiler.hpp>
  14. #include <boost/spirit/home/karma/delimit_out.hpp>
  15. #include <boost/spirit/home/karma/auxiliary/lazy.hpp>
  16. #include <boost/spirit/home/karma/detail/generate_to.hpp>
  17. #include <boost/spirit/home/support/unused.hpp>
  18. #include <boost/fusion/include/at.hpp>
  19. #include <boost/fusion/include/vector.hpp>
  20. ///////////////////////////////////////////////////////////////////////////////
  21. namespace boost { namespace spirit
  22. {
  23. ///////////////////////////////////////////////////////////////////////////
  24. // Enablers
  25. ///////////////////////////////////////////////////////////////////////////
  26. // enables pad(...)
  27. template <typename A0>
  28. struct use_terminal<karma::domain
  29. , terminal_ex<tag::pad, fusion::vector1<A0> > >
  30. : mpl::true_ {};
  31. // enables lazy pad(...)
  32. template <>
  33. struct use_lazy_terminal<karma::domain, tag::pad, 1>
  34. : mpl::true_ {};
  35. }}
  36. ///////////////////////////////////////////////////////////////////////////////
  37. namespace boost { namespace spirit { namespace karma
  38. {
  39. #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  40. using boost::spirit::pad;
  41. #endif
  42. using boost::spirit::pad_type;
  43. struct binary_padding_generator
  44. : primitive_generator<binary_padding_generator>
  45. {
  46. typedef mpl::int_<generator_properties::tracking> properties;
  47. template <typename Context, typename Unused>
  48. struct attribute
  49. {
  50. typedef unused_type type;
  51. };
  52. binary_padding_generator(int numpadbytes)
  53. : numpadbytes_(numpadbytes)
  54. {}
  55. template <
  56. typename OutputIterator, typename Context, typename Delimiter
  57. , typename Attribute>
  58. bool generate(OutputIterator& sink, Context&, Delimiter const& d
  59. , Attribute const& /*attr*/) const
  60. {
  61. std::size_t count = sink.get_out_count() % numpadbytes_;
  62. if (count)
  63. count = numpadbytes_ - count;
  64. bool result = true;
  65. while (result && count-- != 0)
  66. result = detail::generate_to(sink, '\0');
  67. if (result)
  68. result = karma::delimit_out(sink, d); // always do post-delimiting
  69. return result;
  70. }
  71. template <typename Context>
  72. static info what(Context const&)
  73. {
  74. return info("pad");
  75. }
  76. int numpadbytes_;
  77. };
  78. ///////////////////////////////////////////////////////////////////////////
  79. // Generator generators: make_xxx function (objects)
  80. ///////////////////////////////////////////////////////////////////////////
  81. template <typename Modifiers, typename A0>
  82. struct make_primitive<
  83. terminal_ex<tag::pad, fusion::vector1<A0> >
  84. , Modifiers>
  85. {
  86. typedef binary_padding_generator result_type;
  87. template <typename Terminal>
  88. result_type operator()(Terminal const& term, unused_type) const
  89. {
  90. return result_type(fusion::at_c<0>(term.args));
  91. }
  92. };
  93. }}}
  94. #endif