annotate DEPENDENCIES/generic/include/boost/spirit/home/karma/directive/columns.hpp @ 63:307619fa5546

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