9
3

maxwidth.hpp 9.1 KB

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