left_alignment.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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_LEFT_ALIGNMENT_FEB_27_2007_1216PM)
  6. #define BOOST_SPIRIT_KARMA_LEFT_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/karma/detail/attributes.hpp>
  20. #include <boost/spirit/home/support/info.hpp>
  21. #include <boost/spirit/home/support/unused.hpp>
  22. #include <boost/spirit/home/support/has_semantic_action.hpp>
  23. #include <boost/spirit/home/support/handles_container.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 left_align[]
  37. template <>
  38. struct use_directive<karma::domain, tag::left_align>
  39. : mpl::true_ {};
  40. // enables left_align(d)[g] and left_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::left_align, fusion::vector1<T> > >
  45. : mpl::true_ {};
  46. // enables *lazy* left_align(d)[g], where d provides a generator
  47. template <>
  48. struct use_lazy_directive<karma::domain, tag::left_align, 1>
  49. : mpl::true_ {};
  50. // enables left_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::left_align, fusion::vector2<Width, Padding> > >
  55. : spirit::traits::matches<karma::domain, Padding> {};
  56. // enables *lazy* left_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::left_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::left_align;
  67. #endif
  68. using spirit::left_align_type;
  69. namespace detail
  70. {
  71. ///////////////////////////////////////////////////////////////////////
  72. // The left_align_generate template function is used for all the
  73. // different flavors of the left_align[] directive.
  74. ///////////////////////////////////////////////////////////////////////
  75. template <typename OutputIterator, typename Context, typename Delimiter,
  76. typename Attribute, typename Embedded, typename Padding>
  77. inline static bool
  78. left_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 counting
  86. detail::enable_counting<OutputIterator> counting(sink);
  87. // first generate the underlying output
  88. bool r = e.generate(sink, ctx, d, attr);
  89. // pad the output until the max width is reached
  90. while(r && counting.count() < width)
  91. r = p.generate(sink, ctx, unused, unused);
  92. return r;
  93. }
  94. }
  95. ///////////////////////////////////////////////////////////////////////////
  96. // The simple left alignment directive is used for left_align[...]
  97. // generators. It uses default values for the generated width (defined via
  98. // the BOOST_KARMA_DEFAULT_FIELD_LENGTH constant) and for the padding
  99. // generator (always spaces).
  100. ///////////////////////////////////////////////////////////////////////////
  101. template <typename Subject, typename Width = detail::default_width>
  102. struct simple_left_alignment
  103. : unary_generator<simple_left_alignment<Subject, Width> >
  104. {
  105. typedef Subject subject_type;
  106. typedef mpl::int_<
  107. generator_properties::counting | subject_type::properties::value
  108. > properties;
  109. template <typename Context, typename Iterator>
  110. struct attribute
  111. : traits::attribute_of<subject_type, Context, Iterator>
  112. {};
  113. simple_left_alignment(Subject const& subject, Width width = Width())
  114. : subject(subject), width(width) {}
  115. template <typename OutputIterator, typename Context, typename Delimiter
  116. , typename Attribute>
  117. bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d
  118. , Attribute const& attr) const
  119. {
  120. return detail::left_align_generate(sink, ctx, d, attr,
  121. subject, width, compile<karma::domain>(' '));
  122. }
  123. template <typename Context>
  124. info what(Context& context) const
  125. {
  126. return info("left_align", subject.what(context));
  127. }
  128. Subject subject;
  129. Width width;
  130. };
  131. ///////////////////////////////////////////////////////////////////////////
  132. // The left alignment directive with padding, is used for generators like
  133. // left_align(padding)[...], where padding is a arbitrary generator
  134. // expression. It uses a default value for the generated width (defined
  135. // via the BOOST_KARMA_DEFAULT_FIELD_LENGTH constant).
  136. ///////////////////////////////////////////////////////////////////////////
  137. template <typename Subject, typename Padding
  138. , typename Width = detail::default_width>
  139. struct padding_left_alignment
  140. : unary_generator<padding_left_alignment<Subject, Padding, Width> >
  141. {
  142. typedef Subject subject_type;
  143. typedef Padding padding_type;
  144. typedef mpl::int_<
  145. generator_properties::counting |
  146. subject_type::properties::value | padding_type::properties::value
  147. > properties;
  148. template <typename Context, typename Iterator>
  149. struct attribute
  150. : traits::attribute_of<subject_type, Context, Iterator>
  151. {};
  152. padding_left_alignment(Subject const& subject, Padding const& padding
  153. , Width width = Width())
  154. : subject(subject), padding(padding), width(width) {}
  155. template <typename OutputIterator, typename Context, typename Delimiter
  156. , typename Attribute>
  157. bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d
  158. , Attribute const& attr) const
  159. {
  160. return detail::left_align_generate(sink, ctx, d, attr,
  161. subject, width, padding);
  162. }
  163. template <typename Context>
  164. info what(Context& context) const
  165. {
  166. return info("left_align", subject.what(context));
  167. }
  168. Subject subject;
  169. Padding padding;
  170. Width width;
  171. };
  172. ///////////////////////////////////////////////////////////////////////////
  173. // Generator generators: make_xxx function (objects)
  174. ///////////////////////////////////////////////////////////////////////////
  175. // creates left_align[] directive generator
  176. template <typename Subject, typename Modifiers>
  177. struct make_directive<tag::left_align, Subject, Modifiers>
  178. {
  179. typedef simple_left_alignment<Subject> result_type;
  180. result_type operator()(unused_type, Subject const& subject
  181. , unused_type) const
  182. {
  183. return result_type(subject);
  184. }
  185. };
  186. // creates left_align(width)[] directive generator
  187. template <typename Width, typename Subject, typename Modifiers>
  188. struct make_directive<
  189. terminal_ex<tag::left_align, fusion::vector1<Width> >
  190. , Subject, Modifiers
  191. , typename enable_if_c< integer_traits<Width>::is_integral >::type>
  192. {
  193. typedef simple_left_alignment<Subject, Width> result_type;
  194. template <typename Terminal>
  195. result_type operator()(Terminal const& term, Subject const& subject
  196. , unused_type) const
  197. {
  198. return result_type(subject, fusion::at_c<0>(term.args));
  199. }
  200. };
  201. // creates left_align(pad)[] directive generator
  202. template <typename Padding, typename Subject, typename Modifiers>
  203. struct make_directive<
  204. terminal_ex<tag::left_align, fusion::vector1<Padding> >
  205. , Subject, Modifiers
  206. , typename enable_if<
  207. mpl::and_<
  208. spirit::traits::matches<karma::domain, Padding>,
  209. mpl::not_<mpl::bool_<integer_traits<Padding>::is_integral> >
  210. >
  211. >::type>
  212. {
  213. typedef typename
  214. result_of::compile<karma::domain, Padding, Modifiers>::type
  215. padding_type;
  216. typedef padding_left_alignment<Subject, padding_type> result_type;
  217. template <typename Terminal>
  218. result_type operator()(Terminal const& term, Subject const& subject
  219. , Modifiers const& modifiers) const
  220. {
  221. return result_type(subject
  222. , compile<karma::domain>(fusion::at_c<0>(term.args), modifiers));
  223. }
  224. };
  225. // creates left_align(width, pad)[] directive generator
  226. template <typename Width, typename Padding, typename Subject
  227. , typename Modifiers>
  228. struct make_directive<
  229. terminal_ex<tag::left_align, fusion::vector2<Width, Padding> >
  230. , Subject, Modifiers>
  231. {
  232. typedef typename
  233. result_of::compile<karma::domain, Padding, Modifiers>::type
  234. padding_type;
  235. typedef padding_left_alignment<Subject, padding_type, Width> result_type;
  236. template <typename Terminal>
  237. result_type operator()(Terminal const& term, Subject const& subject
  238. , Modifiers const& modifiers) const
  239. {
  240. return result_type(subject
  241. , compile<karma::domain>(fusion::at_c<1>(term.args), modifiers)
  242. , fusion::at_c<0>(term.args));
  243. }
  244. };
  245. }}} // namespace boost::spirit::karma
  246. namespace boost { namespace spirit { namespace traits
  247. {
  248. ///////////////////////////////////////////////////////////////////////////
  249. template <typename Subject, typename Width>
  250. struct has_semantic_action<karma::simple_left_alignment<Subject, Width> >
  251. : unary_has_semantic_action<Subject> {};
  252. template <typename Subject, typename Padding, typename Width>
  253. struct has_semantic_action<
  254. karma::padding_left_alignment<Subject, Padding, Width> >
  255. : unary_has_semantic_action<Subject> {};
  256. ///////////////////////////////////////////////////////////////////////////
  257. template <typename Subject, typename Width, typename Attribute
  258. , typename Context, typename Iterator>
  259. struct handles_container<
  260. karma::simple_left_alignment<Subject, Width>, Attribute
  261. , Context, Iterator>
  262. : unary_handles_container<Subject, Attribute, Context, Iterator> {};
  263. template <typename Subject, typename Padding, typename Width
  264. , typename Attribute, typename Context, typename Iterator>
  265. struct handles_container<
  266. karma::padding_left_alignment<Subject, Padding, Width>
  267. , Attribute, Context, Iterator>
  268. : unary_handles_container<Subject, Attribute, Context, Iterator> {};
  269. }}}
  270. #endif