center_alignment.hpp 12 KB

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