right_alignment.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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_RIGHT_ALIGNMENT_FEB_27_2007_1216PM)
  6. #define BOOST_SPIRIT_KARMA_RIGHT_ALIGNMENT_FEB_27_2007_1216PM
  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/integer_traits.hpp>
  27. #include <boost/mpl/bool.hpp>
  28. #include <boost/utility/enable_if.hpp>
  29. #include <boost/detail/workaround.hpp>
  30. ///////////////////////////////////////////////////////////////////////////////
  31. namespace boost { namespace spirit
  32. {
  33. ///////////////////////////////////////////////////////////////////////////
  34. // Enablers
  35. ///////////////////////////////////////////////////////////////////////////
  36. // enables right_align[]
  37. template <>
  38. struct use_directive<karma::domain, tag::right_align>
  39. : mpl::true_ {};
  40. // enables right_align(d)[g] and right_align(w)[g], where d is a generator
  41. // and w is a maximum width
  42. template <typename T>
  43. struct use_directive<karma::domain
  44. , terminal_ex<tag::right_align, fusion::vector1<T> > >
  45. : mpl::true_ {};
  46. // enables *lazy* right_align(d)[g], where d provides a generator
  47. template <>
  48. struct use_lazy_directive<karma::domain, tag::right_align, 1>
  49. : mpl::true_ {};
  50. // enables right_align(w, d)[g], where d is a generator and w is a maximum
  51. // width
  52. template <typename Width, typename Padding>
  53. struct use_directive<karma::domain
  54. , terminal_ex<tag::right_align, fusion::vector2<Width, Padding> > >
  55. : spirit::traits::matches<karma::domain, Padding> {};
  56. // enables *lazy* right_align(w, d)[g], where d provides a generator and w
  57. // is a maximum width
  58. template <>
  59. struct use_lazy_directive<karma::domain, tag::right_align, 2>
  60. : mpl::true_ {};
  61. }}
  62. ///////////////////////////////////////////////////////////////////////////////
  63. namespace boost { namespace spirit { namespace karma
  64. {
  65. #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  66. using spirit::right_align;
  67. #endif
  68. using spirit::right_align_type;
  69. namespace detail
  70. {
  71. ///////////////////////////////////////////////////////////////////////
  72. // The right_align_generate template function is used for all the
  73. // different flavors of the right_align[] directive.
  74. ///////////////////////////////////////////////////////////////////////
  75. template <typename OutputIterator, typename Context, typename Delimiter,
  76. typename Attribute, typename Embedded, typename Padding>
  77. inline static bool
  78. right_align_generate(OutputIterator& sink, Context& ctx,
  79. Delimiter const& d, Attribute const& attr, Embedded const& e,
  80. unsigned int const width, Padding const& p)
  81. {
  82. #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
  83. e; // suppresses warning: C4100: 'e' : unreferenced formal parameter
  84. #endif
  85. // wrap the given output iterator to allow left padding
  86. detail::enable_buffering<OutputIterator> buffering(sink, width);
  87. bool r = false;
  88. // first generate the embedded output
  89. {
  90. detail::disable_counting<OutputIterator> nocounting(sink);
  91. r = e.generate(sink, ctx, d, attr);
  92. } // re-enable counting
  93. buffering.disable(); // do not perform buffering any more
  94. // generate the left padding
  95. detail::enable_counting<OutputIterator> counting(sink, buffering.buffer_size());
  96. while(r && counting.count() < width)
  97. r = p.generate(sink, ctx, unused, unused);
  98. // copy the buffered output to the target output iterator
  99. if (r)
  100. buffering.buffer_copy();
  101. return r;
  102. }
  103. }
  104. ///////////////////////////////////////////////////////////////////////////
  105. // The simple left alignment directive is used for right_align[...]
  106. // generators. It uses default values for the generated width (defined via
  107. // the BOOST_KARMA_DEFAULT_FIELD_LENGTH constant) and for the padding
  108. // generator (always spaces).
  109. ///////////////////////////////////////////////////////////////////////////
  110. template <typename Subject, typename Width = detail::default_width>
  111. struct simple_right_alignment
  112. : unary_generator<simple_right_alignment<Subject, Width> >
  113. {
  114. typedef Subject subject_type;
  115. typedef mpl::int_<
  116. generator_properties::countingbuffer | subject_type::properties::value
  117. > properties;
  118. template <typename Context, typename Iterator>
  119. struct attribute
  120. : traits::attribute_of<subject_type, Context, Iterator>
  121. {};
  122. simple_right_alignment(Subject const& subject, Width width = Width())
  123. : subject(subject), width(width) {}
  124. template <typename OutputIterator, typename Context, typename Delimiter
  125. , typename Attribute>
  126. bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d
  127. , Attribute const& attr) const
  128. {
  129. return detail::right_align_generate(sink, ctx, d, attr,
  130. subject, width, compile<karma::domain>(' '));
  131. }
  132. template <typename Context>
  133. info what(Context& context) const
  134. {
  135. return info("right_align", subject.what(context));
  136. }
  137. Subject subject;
  138. Width width;
  139. };
  140. ///////////////////////////////////////////////////////////////////////////
  141. // The left alignment directive with padding, is used for generators like
  142. // right_align(padding)[...], where padding is a arbitrary generator
  143. // expression. It uses a default value for the generated width (defined
  144. // via the BOOST_KARMA_DEFAULT_FIELD_LENGTH constant).
  145. ///////////////////////////////////////////////////////////////////////////
  146. template <typename Subject, typename Padding
  147. , typename Width = detail::default_width>
  148. struct padding_right_alignment
  149. : unary_generator<padding_right_alignment<Subject, Padding, Width> >
  150. {
  151. typedef Subject subject_type;
  152. typedef Padding padding_type;
  153. typedef mpl::int_<
  154. generator_properties::countingbuffer |
  155. subject_type::properties::value | padding_type::properties::value
  156. > properties;
  157. template <typename Context, typename Iterator>
  158. struct attribute
  159. : traits::attribute_of<subject_type, Context, Iterator>
  160. {};
  161. padding_right_alignment(Subject const& subject, Padding const& padding
  162. , Width width = Width())
  163. : subject(subject), padding(padding), width(width) {}
  164. template <typename OutputIterator, typename Context, typename Delimiter
  165. , typename Attribute>
  166. bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d
  167. , Attribute const& attr) const
  168. {
  169. return detail::right_align_generate(sink, ctx, d, attr,
  170. subject, width, padding);
  171. }
  172. template <typename Context>
  173. info what(Context& context) const
  174. {
  175. return info("right_align", subject.what(context));
  176. }
  177. Subject subject;
  178. Padding padding;
  179. Width width;
  180. };
  181. ///////////////////////////////////////////////////////////////////////////
  182. // Generator generators: make_xxx function (objects)
  183. ///////////////////////////////////////////////////////////////////////////
  184. // creates right_align[] directive generator
  185. template <typename Subject, typename Modifiers>
  186. struct make_directive<tag::right_align, Subject, Modifiers>
  187. {
  188. typedef simple_right_alignment<Subject> result_type;
  189. result_type operator()(unused_type, Subject const& subject
  190. , unused_type) const
  191. {
  192. return result_type(subject);
  193. }
  194. };
  195. // creates right_align(width)[] directive generator
  196. template <typename Width, typename Subject, typename Modifiers>
  197. struct make_directive<
  198. terminal_ex<tag::right_align, fusion::vector1<Width> >
  199. , Subject, Modifiers
  200. , typename enable_if_c< integer_traits<Width>::is_integral >::type>
  201. {
  202. typedef simple_right_alignment<Subject, Width> result_type;
  203. template <typename Terminal>
  204. result_type operator()(Terminal const& term, Subject const& subject
  205. , unused_type) const
  206. {
  207. return result_type(subject, fusion::at_c<0>(term.args));
  208. }
  209. };
  210. // creates right_align(pad)[] directive generator
  211. template <typename Padding, typename Subject, typename Modifiers>
  212. struct make_directive<
  213. terminal_ex<tag::right_align, fusion::vector1<Padding> >
  214. , Subject, Modifiers
  215. , typename enable_if<
  216. mpl::and_<
  217. spirit::traits::matches<karma::domain, Padding>,
  218. mpl::not_<mpl::bool_<integer_traits<Padding>::is_integral> >
  219. >
  220. >::type>
  221. {
  222. typedef typename
  223. result_of::compile<karma::domain, Padding, Modifiers>::type
  224. padding_type;
  225. typedef padding_right_alignment<Subject, padding_type> result_type;
  226. template <typename Terminal>
  227. result_type operator()(Terminal const& term, Subject const& subject
  228. , Modifiers const& modifiers) const
  229. {
  230. return result_type(subject
  231. , compile<karma::domain>(fusion::at_c<0>(term.args), modifiers));
  232. }
  233. };
  234. // creates right_align(width, pad)[] directive generator
  235. template <typename Width, typename Padding, typename Subject
  236. , typename Modifiers>
  237. struct make_directive<
  238. terminal_ex<tag::right_align, fusion::vector2<Width, Padding> >
  239. , Subject, Modifiers>
  240. {
  241. typedef typename
  242. result_of::compile<karma::domain, Padding, Modifiers>::type
  243. padding_type;
  244. typedef padding_right_alignment<Subject, padding_type, Width> result_type;
  245. template <typename Terminal>
  246. result_type operator()(Terminal const& term, Subject const& subject
  247. , Modifiers const& modifiers) const
  248. {
  249. return result_type(subject
  250. , compile<karma::domain>(fusion::at_c<1>(term.args), modifiers)
  251. , fusion::at_c<0>(term.args));
  252. }
  253. };
  254. }}} // namespace boost::spirit::karma
  255. namespace boost { namespace spirit { namespace traits
  256. {
  257. ///////////////////////////////////////////////////////////////////////////
  258. template <typename Subject, typename Width>
  259. struct has_semantic_action<karma::simple_right_alignment<Subject, Width> >
  260. : unary_has_semantic_action<Subject> {};
  261. template <typename Subject, typename Padding, typename Width>
  262. struct has_semantic_action<
  263. karma::padding_right_alignment<Subject, Padding, Width> >
  264. : unary_has_semantic_action<Subject> {};
  265. ///////////////////////////////////////////////////////////////////////////
  266. template <typename Subject, typename Width, typename Attribute
  267. , typename Context, typename Iterator>
  268. struct handles_container<
  269. karma::simple_right_alignment<Subject, Width>
  270. , Attribute, Context, Iterator>
  271. : unary_handles_container<Subject, Attribute, Context, Iterator> {};
  272. template <typename Subject, typename Padding, typename Width
  273. , typename Attribute, typename Context, typename Iterator>
  274. struct handles_container<
  275. karma::padding_right_alignment<Subject, Padding, Width>
  276. , Attribute, Context, Iterator>
  277. : unary_handles_container<Subject, Attribute, Context, Iterator> {};
  278. }}}
  279. #endif