columns.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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_COLUMNS_DEC_03_2009_0736AM)
  6. #define BOOST_SPIRIT_KARMA_COLUMNS_DEC_03_2009_0736AM
  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/delimit_out.hpp>
  14. #include <boost/spirit/home/karma/detail/default_width.hpp>
  15. #include <boost/spirit/home/karma/auxiliary/eol.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/fusion/include/at.hpp>
  24. #include <boost/fusion/include/vector.hpp>
  25. #include <boost/integer_traits.hpp>
  26. namespace boost { namespace spirit
  27. {
  28. ///////////////////////////////////////////////////////////////////////////
  29. // Enablers
  30. ///////////////////////////////////////////////////////////////////////////
  31. template <>
  32. struct use_directive<karma::domain, tag::columns> // enables columns[]
  33. : mpl::true_ {};
  34. // enables columns(c)[g], where c provides the number of require columns
  35. template <typename T>
  36. struct use_directive<karma::domain
  37. , terminal_ex<tag::columns, fusion::vector1<T> > >
  38. : mpl::true_ {};
  39. // enables *lazy* columns(c)[g]
  40. template <>
  41. struct use_lazy_directive<karma::domain, tag::columns, 1>
  42. : mpl::true_ {};
  43. // enables columns(c, d)[g], where c provides the number of require columns
  44. // and d is the custom column-delimiter (default is karma::endl)
  45. template <typename T1, typename T2>
  46. struct use_directive<karma::domain
  47. , terminal_ex<tag::columns, fusion::vector2<T1, T2> > >
  48. : boost::spirit::traits::matches<karma::domain, T2> {};
  49. // enables *lazy* columns(c, d)[g]
  50. template <>
  51. struct use_lazy_directive<karma::domain, tag::columns, 2>
  52. : mpl::true_ {};
  53. }}
  54. namespace boost { namespace spirit { namespace karma
  55. {
  56. #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  57. using spirit::columns;
  58. #endif
  59. using spirit::columns_type;
  60. namespace detail
  61. {
  62. template <typename Delimiter, typename ColumnDelimiter>
  63. struct columns_delimiter
  64. {
  65. columns_delimiter(Delimiter const& delim
  66. , ColumnDelimiter const& cdelim, unsigned int const numcols)
  67. : delimiter(delim), column_delimiter(cdelim)
  68. , numcolumns(numcols), count(0) {}
  69. template <typename OutputIterator, typename Context
  70. , typename Delimiter_, typename Attribute>
  71. bool generate(OutputIterator& sink, Context&, Delimiter_ const&
  72. , Attribute const&) const
  73. {
  74. // first invoke the embedded delimiter
  75. if (!karma::delimit_out(sink, delimiter))
  76. return false;
  77. // now we count the number of invocations and emit the column
  78. // delimiter if needed
  79. if ((++count % numcolumns) == 0)
  80. return karma::delimit_out(sink, column_delimiter);
  81. return true;
  82. }
  83. // generate a final column delimiter if the last invocation didn't
  84. // emit one
  85. template <typename OutputIterator>
  86. bool delimit_out(OutputIterator& sink) const
  87. {
  88. if (count % numcolumns)
  89. return karma::delimit_out(sink, column_delimiter);
  90. return true;
  91. }
  92. Delimiter const& delimiter;
  93. ColumnDelimiter const& column_delimiter;
  94. unsigned int const numcolumns;
  95. mutable unsigned int count;
  96. private:
  97. // silence MSVC warning C4512: assignment operator could not be generated
  98. columns_delimiter& operator= (columns_delimiter const&);
  99. };
  100. }
  101. ///////////////////////////////////////////////////////////////////////////
  102. // The columns_generator is used for columns(c, d)[...] directives.
  103. ///////////////////////////////////////////////////////////////////////////
  104. template <typename Subject, typename NumColumns, typename ColumnsDelimiter>
  105. struct columns_generator
  106. : unary_generator<columns_generator<Subject, NumColumns, ColumnsDelimiter> >
  107. {
  108. typedef Subject subject_type;
  109. typedef ColumnsDelimiter delimiter_type;
  110. typedef mpl::int_<
  111. subject_type::properties::value | delimiter_type::properties::value
  112. > properties;
  113. template <typename Context, typename Iterator>
  114. struct attribute
  115. : traits::attribute_of<subject_type, Context, Iterator>
  116. {};
  117. columns_generator(Subject const& subject, NumColumns const& cols
  118. , ColumnsDelimiter const& cdelimiter)
  119. : subject(subject), numcolumns(cols), column_delimiter(cdelimiter)
  120. {
  121. // having zero number of columns doesn't make any sense
  122. BOOST_ASSERT(numcolumns > 0);
  123. }
  124. template <typename OutputIterator, typename Context
  125. , typename Delimiter, typename Attribute>
  126. bool generate(OutputIterator& sink, Context& ctx
  127. , Delimiter const& delimiter, Attribute const& attr) const
  128. {
  129. // The columns generator dispatches to the embedded generator
  130. // while supplying a new delimiter to use, wrapping the outer
  131. // delimiter.
  132. typedef detail::columns_delimiter<
  133. Delimiter, ColumnsDelimiter
  134. > columns_delimiter_type;
  135. columns_delimiter_type d(delimiter, column_delimiter, numcolumns);
  136. return subject.generate(sink, ctx, d, attr) && d.delimit_out(sink);
  137. }
  138. template <typename Context>
  139. info what(Context& context) const
  140. {
  141. return info("columns", subject.what(context));
  142. }
  143. Subject subject;
  144. NumColumns numcolumns;
  145. ColumnsDelimiter column_delimiter;
  146. };
  147. ///////////////////////////////////////////////////////////////////////////
  148. // Generator generators: make_xxx function (objects)
  149. ///////////////////////////////////////////////////////////////////////////
  150. // creates columns[] directive
  151. template <typename Subject, typename Modifiers>
  152. struct make_directive<tag::columns, Subject, Modifiers>
  153. {
  154. typedef typename
  155. result_of::compile<karma::domain, eol_type, Modifiers>::type
  156. columns_delimiter_type;
  157. typedef columns_generator<
  158. Subject, detail::default_columns, columns_delimiter_type>
  159. result_type;
  160. result_type operator()(unused_type, Subject const& subject
  161. , unused_type) const
  162. {
  163. #if defined(BOOST_SPIRIT_NO_PREDEFINED_TERMINALS)
  164. eol_type const eol = eol_type();
  165. #endif
  166. return result_type(subject, detail::default_columns()
  167. , compile<karma::domain>(eol));
  168. }
  169. };
  170. // creates columns(c)[] directive generator (c is the number of columns)
  171. template <typename T, typename Subject, typename Modifiers>
  172. struct make_directive<
  173. terminal_ex<tag::columns, fusion::vector1<T> >
  174. , Subject, Modifiers
  175. , typename enable_if_c<integer_traits<T>::is_integral>::type>
  176. {
  177. typedef typename
  178. result_of::compile<karma::domain, eol_type, Modifiers>::type
  179. columns_delimiter_type;
  180. typedef columns_generator<
  181. Subject, T, columns_delimiter_type
  182. > result_type;
  183. template <typename Terminal>
  184. result_type operator()(Terminal const& term, Subject const& subject
  185. , unused_type) const
  186. {
  187. #if defined(BOOST_SPIRIT_NO_PREDEFINED_TERMINALS)
  188. eol_type const eol = eol_type();
  189. #endif
  190. return result_type(subject, fusion::at_c<0>(term.args)
  191. , compile<karma::domain>(eol));
  192. }
  193. };
  194. // creates columns(d)[] directive generator (d is the column delimiter)
  195. template <typename T, typename Subject, typename Modifiers>
  196. struct make_directive<
  197. terminal_ex<tag::columns, fusion::vector1<T> >
  198. , Subject, Modifiers
  199. , typename enable_if<
  200. mpl::and_<
  201. spirit::traits::matches<karma::domain, T>,
  202. mpl::not_<mpl::bool_<integer_traits<T>::is_integral> >
  203. >
  204. >::type>
  205. {
  206. typedef typename
  207. result_of::compile<karma::domain, T, Modifiers>::type
  208. columns_delimiter_type;
  209. typedef columns_generator<
  210. Subject, detail::default_columns, columns_delimiter_type
  211. > result_type;
  212. template <typename Terminal>
  213. result_type operator()(Terminal const& term, Subject const& subject
  214. , unused_type) const
  215. {
  216. return result_type(subject, detail::default_columns()
  217. , compile<karma::domain>(fusion::at_c<0>(term.args)));
  218. }
  219. };
  220. // creates columns(c, d)[] directive generator (c is the number of columns
  221. // and d is the column delimiter)
  222. template <typename T1, typename T2, typename Subject, typename Modifiers>
  223. struct make_directive<
  224. terminal_ex<tag::columns, fusion::vector2<T1, T2> >
  225. , Subject, Modifiers>
  226. {
  227. typedef typename
  228. result_of::compile<karma::domain, T2, Modifiers>::type
  229. columns_delimiter_type;
  230. typedef columns_generator<
  231. Subject, T1, columns_delimiter_type
  232. > result_type;
  233. template <typename Terminal>
  234. result_type operator()(Terminal const& term, Subject const& subject
  235. , unused_type) const
  236. {
  237. return result_type (subject, fusion::at_c<0>(term.args)
  238. , compile<karma::domain>(fusion::at_c<1>(term.args)));
  239. }
  240. };
  241. }}}
  242. namespace boost { namespace spirit { namespace traits
  243. {
  244. ///////////////////////////////////////////////////////////////////////////
  245. template <typename Subject, typename T1, typename T2>
  246. struct has_semantic_action<karma::columns_generator<Subject, T1, T2> >
  247. : unary_has_semantic_action<Subject> {};
  248. ///////////////////////////////////////////////////////////////////////////
  249. template <typename Subject, typename T1, typename T2, typename Attribute
  250. , typename Context, typename Iterator>
  251. struct handles_container<
  252. karma::columns_generator<Subject, T1, T2>, Attribute
  253. , Context, Iterator>
  254. : unary_handles_container<Subject, Attribute, Context, Iterator> {};
  255. }}}
  256. #endif