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