| 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_RIGHT_ALIGNMENT_FEB_27_2007_1216PM) |
| 7 | #define BOOST_SPIRIT_KARMA_RIGHT_ALIGNMENT_FEB_27_2007_1216PM |
| 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/detail/output_iterator.hpp> |
| 17 | #include <boost/spirit/home/karma/detail/default_width.hpp> |
| 18 | #include <boost/spirit/home/karma/delimit_out.hpp> |
| 19 | #include <boost/spirit/home/karma/auxiliary/lazy.hpp> |
| 20 | #include <boost/spirit/home/karma/char/char.hpp> |
| 21 | #include <boost/spirit/home/support/unused.hpp> |
| 22 | #include <boost/spirit/home/support/common_terminals.hpp> |
| 23 | #include <boost/spirit/home/support/has_semantic_action.hpp> |
| 24 | #include <boost/spirit/home/support/handles_container.hpp> |
| 25 | #include <boost/spirit/home/karma/detail/attributes.hpp> |
| 26 | #include <boost/spirit/home/support/info.hpp> |
| 27 | #include <boost/spirit/home/support/unused.hpp> |
| 28 | #include <boost/fusion/include/at.hpp> |
| 29 | #include <boost/fusion/include/vector.hpp> |
| 30 | #include <boost/integer_traits.hpp> |
| 31 | #include <boost/mpl/bool.hpp> |
| 32 | #include <boost/utility/enable_if.hpp> |
| 33 | #include <boost/detail/workaround.hpp> |
| 34 | |
| 35 | /////////////////////////////////////////////////////////////////////////////// |
| 36 | namespace boost { namespace spirit |
| 37 | { |
| 38 | /////////////////////////////////////////////////////////////////////////// |
| 39 | // Enablers |
| 40 | /////////////////////////////////////////////////////////////////////////// |
| 41 | |
| 42 | // enables right_align[] |
| 43 | template <> |
| 44 | struct use_directive<karma::domain, tag::right_align> |
| 45 | : mpl::true_ {}; |
| 46 | |
| 47 | // enables right_align(d)[g] and right_align(w)[g], where d is a generator |
| 48 | // and w is a maximum width |
| 49 | template <typename T> |
| 50 | struct use_directive<karma::domain |
| 51 | , terminal_ex<tag::right_align, fusion::vector1<T> > > |
| 52 | : mpl::true_ {}; |
| 53 | |
| 54 | // enables *lazy* right_align(d)[g], where d provides a generator |
| 55 | template <> |
| 56 | struct use_lazy_directive<karma::domain, tag::right_align, 1> |
| 57 | : mpl::true_ {}; |
| 58 | |
| 59 | // enables right_align(w, d)[g], where d is a generator and w is a maximum |
| 60 | // width |
| 61 | template <typename Width, typename Padding> |
| 62 | struct use_directive<karma::domain |
| 63 | , terminal_ex<tag::right_align, fusion::vector2<Width, Padding> > > |
| 64 | : spirit::traits::matches<karma::domain, Padding> {}; |
| 65 | |
| 66 | // enables *lazy* right_align(w, d)[g], where d provides a generator and w |
| 67 | // is a maximum width |
| 68 | template <> |
| 69 | struct use_lazy_directive<karma::domain, tag::right_align, 2> |
| 70 | : mpl::true_ {}; |
| 71 | |
| 72 | }} |
| 73 | |
| 74 | /////////////////////////////////////////////////////////////////////////////// |
| 75 | namespace boost { namespace spirit { namespace karma |
| 76 | { |
| 77 | #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS |
| 78 | using spirit::right_align; |
| 79 | #endif |
| 80 | using spirit::right_align_type; |
| 81 | |
| 82 | namespace detail |
| 83 | { |
| 84 | /////////////////////////////////////////////////////////////////////// |
| 85 | // The right_align_generate template function is used for all the |
| 86 | // different flavors of the right_align[] directive. |
| 87 | /////////////////////////////////////////////////////////////////////// |
| 88 | template <typename OutputIterator, typename Context, typename Delimiter, |
| 89 | typename Attribute, typename Embedded, typename Padding> |
| 90 | inline static bool |
| 91 | right_align_generate(OutputIterator& sink, Context& ctx, |
| 92 | Delimiter const& d, Attribute const& attr, Embedded const& e, |
| 93 | unsigned int const width, Padding const& p) |
| 94 | { |
| 95 | #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600)) |
| 96 | (void)e; // suppresses warning: C4100: 'e' : unreferenced formal parameter |
| 97 | #endif |
| 98 | // wrap the given output iterator to allow left padding |
| 99 | detail::enable_buffering<OutputIterator> buffering(sink, width); |
| 100 | bool r = false; |
| 101 | |
| 102 | // first generate the embedded output |
| 103 | { |
| 104 | detail::disable_counting<OutputIterator> nocounting(sink); |
| 105 | r = e.generate(sink, ctx, d, attr); |
| 106 | } // re-enable counting |
| 107 | |
| 108 | buffering.disable(); // do not perform buffering any more |
| 109 | |
| 110 | // generate the left padding |
| 111 | detail::enable_counting<OutputIterator> counting(sink, buffering.buffer_size()); |
| 112 | while(r && counting.count() < width) |
| 113 | r = p.generate(sink, ctx, unused, unused); |
| 114 | |
| 115 | // copy the buffered output to the target output iterator |
| 116 | if (r) |
| 117 | buffering.buffer_copy(); |
| 118 | return r; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /////////////////////////////////////////////////////////////////////////// |
| 123 | // The simple left alignment directive is used for right_align[...] |
| 124 | // generators. It uses default values for the generated width (defined via |
| 125 | // the BOOST_KARMA_DEFAULT_FIELD_LENGTH constant) and for the padding |
| 126 | // generator (always spaces). |
| 127 | /////////////////////////////////////////////////////////////////////////// |
| 128 | template <typename Subject, typename Width = detail::default_width> |
| 129 | struct simple_right_alignment |
| 130 | : unary_generator<simple_right_alignment<Subject, Width> > |
| 131 | { |
| 132 | typedef Subject subject_type; |
| 133 | |
| 134 | typedef mpl::int_< |
| 135 | generator_properties::countingbuffer | subject_type::properties::value |
| 136 | > properties; |
| 137 | |
| 138 | template <typename Context, typename Iterator> |
| 139 | struct attribute |
| 140 | : traits::attribute_of<subject_type, Context, Iterator> |
| 141 | {}; |
| 142 | |
| 143 | simple_right_alignment(Subject const& subject, Width width = Width()) |
| 144 | : subject(subject), width(width) {} |
| 145 | |
| 146 | template <typename OutputIterator, typename Context, typename Delimiter |
| 147 | , typename Attribute> |
| 148 | bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d |
| 149 | , Attribute const& attr) const |
| 150 | { |
| 151 | return detail::right_align_generate(sink, ctx, d, attr, |
| 152 | subject, width, compile<karma::domain>(expr: ' ')); |
| 153 | } |
| 154 | |
| 155 | template <typename Context> |
| 156 | info what(Context& context) const |
| 157 | { |
| 158 | return info("right_align" , subject.what(context)); |
| 159 | } |
| 160 | |
| 161 | Subject subject; |
| 162 | Width width; |
| 163 | }; |
| 164 | |
| 165 | /////////////////////////////////////////////////////////////////////////// |
| 166 | // The left alignment directive with padding, is used for generators like |
| 167 | // right_align(padding)[...], where padding is a arbitrary generator |
| 168 | // expression. It uses a default value for the generated width (defined |
| 169 | // via the BOOST_KARMA_DEFAULT_FIELD_LENGTH constant). |
| 170 | /////////////////////////////////////////////////////////////////////////// |
| 171 | template <typename Subject, typename Padding |
| 172 | , typename Width = detail::default_width> |
| 173 | struct padding_right_alignment |
| 174 | : unary_generator<padding_right_alignment<Subject, Padding, Width> > |
| 175 | { |
| 176 | typedef Subject subject_type; |
| 177 | typedef Padding padding_type; |
| 178 | |
| 179 | typedef mpl::int_< |
| 180 | generator_properties::countingbuffer | |
| 181 | subject_type::properties::value | padding_type::properties::value |
| 182 | > properties; |
| 183 | |
| 184 | template <typename Context, typename Iterator> |
| 185 | struct attribute |
| 186 | : traits::attribute_of<subject_type, Context, Iterator> |
| 187 | {}; |
| 188 | |
| 189 | padding_right_alignment(Subject const& subject, Padding const& padding |
| 190 | , Width width = Width()) |
| 191 | : subject(subject), padding(padding), width(width) {} |
| 192 | |
| 193 | template <typename OutputIterator, typename Context, typename Delimiter |
| 194 | , typename Attribute> |
| 195 | bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d |
| 196 | , Attribute const& attr) const |
| 197 | { |
| 198 | return detail::right_align_generate(sink, ctx, d, attr, |
| 199 | subject, width, padding); |
| 200 | } |
| 201 | |
| 202 | template <typename Context> |
| 203 | info what(Context& context) const |
| 204 | { |
| 205 | return info("right_align" , subject.what(context)); |
| 206 | } |
| 207 | |
| 208 | Subject subject; |
| 209 | Padding padding; |
| 210 | Width width; |
| 211 | }; |
| 212 | |
| 213 | /////////////////////////////////////////////////////////////////////////// |
| 214 | // Generator generators: make_xxx function (objects) |
| 215 | /////////////////////////////////////////////////////////////////////////// |
| 216 | |
| 217 | // creates right_align[] directive generator |
| 218 | template <typename Subject, typename Modifiers> |
| 219 | struct make_directive<tag::right_align, Subject, Modifiers> |
| 220 | { |
| 221 | typedef simple_right_alignment<Subject> result_type; |
| 222 | result_type operator()(unused_type, Subject const& subject |
| 223 | , unused_type) const |
| 224 | { |
| 225 | return result_type(subject); |
| 226 | } |
| 227 | }; |
| 228 | |
| 229 | // creates right_align(width)[] directive generator |
| 230 | template <typename Width, typename Subject, typename Modifiers> |
| 231 | struct make_directive< |
| 232 | terminal_ex<tag::right_align, fusion::vector1<Width> > |
| 233 | , Subject, Modifiers |
| 234 | , typename enable_if_c< integer_traits<Width>::is_integral >::type> |
| 235 | { |
| 236 | typedef simple_right_alignment<Subject, Width> result_type; |
| 237 | |
| 238 | template <typename Terminal> |
| 239 | result_type operator()(Terminal const& term, Subject const& subject |
| 240 | , unused_type) const |
| 241 | { |
| 242 | return result_type(subject, fusion::at_c<0>(term.args)); |
| 243 | } |
| 244 | }; |
| 245 | |
| 246 | // creates right_align(pad)[] directive generator |
| 247 | template <typename Padding, typename Subject, typename Modifiers> |
| 248 | struct make_directive< |
| 249 | terminal_ex<tag::right_align, fusion::vector1<Padding> > |
| 250 | , Subject, Modifiers |
| 251 | , typename enable_if< |
| 252 | mpl::and_< |
| 253 | spirit::traits::matches<karma::domain, Padding>, |
| 254 | mpl::not_<mpl::bool_<integer_traits<Padding>::is_integral> > |
| 255 | > |
| 256 | >::type> |
| 257 | { |
| 258 | typedef typename |
| 259 | result_of::compile<karma::domain, Padding, Modifiers>::type |
| 260 | padding_type; |
| 261 | |
| 262 | typedef padding_right_alignment<Subject, padding_type> result_type; |
| 263 | |
| 264 | template <typename Terminal> |
| 265 | result_type operator()(Terminal const& term, Subject const& subject |
| 266 | , Modifiers const& modifiers) const |
| 267 | { |
| 268 | return result_type(subject |
| 269 | , compile<karma::domain>(fusion::at_c<0>(term.args), modifiers)); |
| 270 | } |
| 271 | }; |
| 272 | |
| 273 | // creates right_align(width, pad)[] directive generator |
| 274 | template <typename Width, typename Padding, typename Subject |
| 275 | , typename Modifiers> |
| 276 | struct make_directive< |
| 277 | terminal_ex<tag::right_align, fusion::vector2<Width, Padding> > |
| 278 | , Subject, Modifiers> |
| 279 | { |
| 280 | typedef typename |
| 281 | result_of::compile<karma::domain, Padding, Modifiers>::type |
| 282 | padding_type; |
| 283 | |
| 284 | typedef padding_right_alignment<Subject, padding_type, Width> result_type; |
| 285 | |
| 286 | template <typename Terminal> |
| 287 | result_type operator()(Terminal const& term, Subject const& subject |
| 288 | , Modifiers const& modifiers) const |
| 289 | { |
| 290 | return result_type(subject |
| 291 | , compile<karma::domain>(fusion::at_c<1>(term.args), modifiers) |
| 292 | , fusion::at_c<0>(term.args)); |
| 293 | } |
| 294 | }; |
| 295 | |
| 296 | }}} // namespace boost::spirit::karma |
| 297 | |
| 298 | namespace boost { namespace spirit { namespace traits |
| 299 | { |
| 300 | /////////////////////////////////////////////////////////////////////////// |
| 301 | template <typename Subject, typename Width> |
| 302 | struct has_semantic_action<karma::simple_right_alignment<Subject, Width> > |
| 303 | : unary_has_semantic_action<Subject> {}; |
| 304 | |
| 305 | template <typename Subject, typename Padding, typename Width> |
| 306 | struct has_semantic_action< |
| 307 | karma::padding_right_alignment<Subject, Padding, Width> > |
| 308 | : unary_has_semantic_action<Subject> {}; |
| 309 | |
| 310 | /////////////////////////////////////////////////////////////////////////// |
| 311 | template <typename Subject, typename Width, typename Attribute |
| 312 | , typename Context, typename Iterator> |
| 313 | struct handles_container< |
| 314 | karma::simple_right_alignment<Subject, Width> |
| 315 | , Attribute, Context, Iterator> |
| 316 | : unary_handles_container<Subject, Attribute, Context, Iterator> {}; |
| 317 | |
| 318 | template <typename Subject, typename Padding, typename Width |
| 319 | , typename Attribute, typename Context, typename Iterator> |
| 320 | struct handles_container< |
| 321 | karma::padding_right_alignment<Subject, Padding, Width> |
| 322 | , Attribute, Context, Iterator> |
| 323 | : unary_handles_container<Subject, Attribute, Context, Iterator> {}; |
| 324 | }}} |
| 325 | |
| 326 | #endif |
| 327 | |
| 328 | |