| 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_NUMERIC_UTILS_FEB_23_2007_0841PM) |
| 7 | #define BOOST_SPIRIT_KARMA_NUMERIC_UTILS_FEB_23_2007_0841PM |
| 8 | |
| 9 | #if defined(_MSC_VER) |
| 10 | #pragma once |
| 11 | #endif |
| 12 | |
| 13 | #include <boost/config.hpp> |
| 14 | #include <boost/config/no_tr1/cmath.hpp> |
| 15 | #include <boost/limits.hpp> |
| 16 | |
| 17 | #include <boost/type_traits/is_integral.hpp> |
| 18 | #include <boost/spirit/home/support/char_class.hpp> |
| 19 | #include <boost/spirit/home/support/unused.hpp> |
| 20 | #include <boost/spirit/home/support/numeric_traits.hpp> |
| 21 | #include <boost/spirit/home/support/detail/pow10.hpp> |
| 22 | #include <boost/spirit/home/karma/detail/generate_to.hpp> |
| 23 | #include <boost/spirit/home/karma/detail/string_generate.hpp> |
| 24 | |
| 25 | #include <boost/core/cmath.hpp> |
| 26 | |
| 27 | /////////////////////////////////////////////////////////////////////////////// |
| 28 | // |
| 29 | // The value BOOST_KARMA_NUMERICS_LOOP_UNROLL specifies, how to unroll the |
| 30 | // integer string generation loop (see below). |
| 31 | // |
| 32 | // Set the value to some integer in between 0 (no unrolling) and the |
| 33 | // largest expected generated integer string length (complete unrolling). |
| 34 | // If not specified, this value defaults to 6. |
| 35 | // |
| 36 | /////////////////////////////////////////////////////////////////////////////// |
| 37 | #if !defined(BOOST_KARMA_NUMERICS_LOOP_UNROLL) |
| 38 | #define BOOST_KARMA_NUMERICS_LOOP_UNROLL 6 |
| 39 | #endif |
| 40 | |
| 41 | #if BOOST_KARMA_NUMERICS_LOOP_UNROLL < 0 |
| 42 | #error "Please set the BOOST_KARMA_NUMERICS_LOOP_UNROLL to a non-negative value!" |
| 43 | #endif |
| 44 | |
| 45 | namespace boost { namespace spirit { namespace traits |
| 46 | { |
| 47 | /////////////////////////////////////////////////////////////////////// |
| 48 | // |
| 49 | // return the absolute value from a given number, avoiding over- and |
| 50 | // underflow |
| 51 | // |
| 52 | /////////////////////////////////////////////////////////////////////// |
| 53 | template <typename T, typename Enable/* = void*/> |
| 54 | struct absolute_value |
| 55 | { |
| 56 | typedef T type; |
| 57 | static T call (T n) |
| 58 | { |
| 59 | // allow for ADL to find the correct overloads for fabs |
| 60 | using namespace std; |
| 61 | return fabs(n); |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | #define BOOST_SPIRIT_ABSOLUTE_VALUE(signedtype, unsignedtype) \ |
| 66 | template <> \ |
| 67 | struct absolute_value<signedtype> \ |
| 68 | { \ |
| 69 | typedef unsignedtype type; \ |
| 70 | static type call(signedtype n) \ |
| 71 | { \ |
| 72 | /* implementation is well-defined for one's complement, */ \ |
| 73 | /* two's complement, and signed magnitude architectures */ \ |
| 74 | /* by the C++ Standard. [conv.integral] [expr.unary.op] */ \ |
| 75 | return (n >= 0) ? static_cast<type>(n) \ |
| 76 | : -static_cast<type>(n); \ |
| 77 | } \ |
| 78 | } \ |
| 79 | /**/ |
| 80 | #define BOOST_SPIRIT_ABSOLUTE_VALUE_UNSIGNED(unsignedtype) \ |
| 81 | template <> \ |
| 82 | struct absolute_value<unsignedtype> \ |
| 83 | { \ |
| 84 | typedef unsignedtype type; \ |
| 85 | static type call(unsignedtype n) \ |
| 86 | { \ |
| 87 | return n; \ |
| 88 | } \ |
| 89 | } \ |
| 90 | /**/ |
| 91 | |
| 92 | #if defined(BOOST_MSVC) |
| 93 | # pragma warning(push) |
| 94 | // unary minus operator applied to unsigned type, result still unsigned |
| 95 | # pragma warning(disable: 4146) |
| 96 | #endif |
| 97 | BOOST_SPIRIT_ABSOLUTE_VALUE(signed char, unsigned char); |
| 98 | BOOST_SPIRIT_ABSOLUTE_VALUE(char, unsigned char); |
| 99 | BOOST_SPIRIT_ABSOLUTE_VALUE(short, unsigned short); |
| 100 | BOOST_SPIRIT_ABSOLUTE_VALUE(int, unsigned int); |
| 101 | BOOST_SPIRIT_ABSOLUTE_VALUE(long, unsigned long); |
| 102 | BOOST_SPIRIT_ABSOLUTE_VALUE_UNSIGNED(unsigned char); |
| 103 | BOOST_SPIRIT_ABSOLUTE_VALUE_UNSIGNED(unsigned short); |
| 104 | BOOST_SPIRIT_ABSOLUTE_VALUE_UNSIGNED(unsigned int); |
| 105 | BOOST_SPIRIT_ABSOLUTE_VALUE_UNSIGNED(unsigned long); |
| 106 | #ifdef BOOST_HAS_LONG_LONG |
| 107 | BOOST_SPIRIT_ABSOLUTE_VALUE(boost::long_long_type, boost::ulong_long_type); |
| 108 | BOOST_SPIRIT_ABSOLUTE_VALUE_UNSIGNED(boost::ulong_long_type); |
| 109 | #endif |
| 110 | #if defined(BOOST_MSVC) |
| 111 | # pragma warning(pop) |
| 112 | #endif |
| 113 | |
| 114 | #undef BOOST_SPIRIT_ABSOLUTE_VALUE |
| 115 | #undef BOOST_SPIRIT_ABSOLUTE_VALUE_UNSIGNED |
| 116 | |
| 117 | template <> |
| 118 | struct absolute_value<float> |
| 119 | { |
| 120 | typedef float type; |
| 121 | static type call(float n) |
| 122 | { |
| 123 | return (std::fabs)(x: n); |
| 124 | } |
| 125 | }; |
| 126 | |
| 127 | template <> |
| 128 | struct absolute_value<double> |
| 129 | { |
| 130 | typedef double type; |
| 131 | static type call(double n) |
| 132 | { |
| 133 | return (std::fabs)(x: n); |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | template <> |
| 138 | struct absolute_value<long double> |
| 139 | { |
| 140 | typedef long double type; |
| 141 | static type call(long double n) |
| 142 | { |
| 143 | return (std::fabs)(x: n); |
| 144 | } |
| 145 | }; |
| 146 | |
| 147 | // specialization for pointers |
| 148 | template <typename T> |
| 149 | struct absolute_value<T*> |
| 150 | { |
| 151 | typedef std::size_t type; |
| 152 | static type call (T* p) |
| 153 | { |
| 154 | return std::size_t(p); |
| 155 | } |
| 156 | }; |
| 157 | |
| 158 | template <typename T> |
| 159 | inline typename absolute_value<T>::type |
| 160 | get_absolute_value(T n) |
| 161 | { |
| 162 | return absolute_value<T>::call(n); |
| 163 | } |
| 164 | |
| 165 | /////////////////////////////////////////////////////////////////////// |
| 166 | template <typename T, typename Enable/* = void*/> |
| 167 | struct is_negative |
| 168 | { |
| 169 | static bool call(T n) |
| 170 | { |
| 171 | return (n < 0) ? true : false; |
| 172 | } |
| 173 | }; |
| 174 | |
| 175 | template <> |
| 176 | struct is_negative<float> |
| 177 | { |
| 178 | static bool call(float n) |
| 179 | { |
| 180 | return (core::signbit)(x: n) ? true : false; |
| 181 | } |
| 182 | }; |
| 183 | |
| 184 | template <> |
| 185 | struct is_negative<double> |
| 186 | { |
| 187 | static bool call(double n) |
| 188 | { |
| 189 | return (core::signbit)(x: n) ? true : false; |
| 190 | } |
| 191 | }; |
| 192 | |
| 193 | template <> |
| 194 | struct is_negative<long double> |
| 195 | { |
| 196 | static bool call(long double n) |
| 197 | { |
| 198 | return (core::signbit)(x: n) ? true : false; |
| 199 | } |
| 200 | }; |
| 201 | |
| 202 | template <typename T> |
| 203 | inline bool test_negative(T n) |
| 204 | { |
| 205 | return is_negative<T>::call(n); |
| 206 | } |
| 207 | |
| 208 | /////////////////////////////////////////////////////////////////////// |
| 209 | template <typename T, typename Enable/* = void*/> |
| 210 | struct is_zero |
| 211 | { |
| 212 | static bool call(T n) |
| 213 | { |
| 214 | return (n == 0) ? true : false; |
| 215 | } |
| 216 | }; |
| 217 | |
| 218 | template <> |
| 219 | struct is_zero<float> |
| 220 | { |
| 221 | static bool call(float n) |
| 222 | { |
| 223 | return (core::fpclassify)(x: n) == core::fp_zero; |
| 224 | } |
| 225 | }; |
| 226 | |
| 227 | template <> |
| 228 | struct is_zero<double> |
| 229 | { |
| 230 | static bool call(double n) |
| 231 | { |
| 232 | return (core::fpclassify)(x: n) == core::fp_zero; |
| 233 | } |
| 234 | }; |
| 235 | |
| 236 | template <> |
| 237 | struct is_zero<long double> |
| 238 | { |
| 239 | static bool call(long double n) |
| 240 | { |
| 241 | return (core::fpclassify)(x: n) == core::fp_zero; |
| 242 | } |
| 243 | }; |
| 244 | |
| 245 | template <typename T> |
| 246 | inline bool test_zero(T n) |
| 247 | { |
| 248 | return is_zero<T>::call(n); |
| 249 | } |
| 250 | |
| 251 | /////////////////////////////////////////////////////////////////////// |
| 252 | template <typename T, typename Enable/* = void*/> |
| 253 | struct is_nan |
| 254 | { |
| 255 | static bool call(T n) |
| 256 | { |
| 257 | // NaN numbers are not equal to anything |
| 258 | return (n != n) ? true : false; |
| 259 | } |
| 260 | }; |
| 261 | |
| 262 | template <> |
| 263 | struct is_nan<float> |
| 264 | { |
| 265 | static bool call(float n) |
| 266 | { |
| 267 | return (core::fpclassify)(x: n) == core::fp_nan; |
| 268 | } |
| 269 | }; |
| 270 | |
| 271 | template <> |
| 272 | struct is_nan<double> |
| 273 | { |
| 274 | static bool call(double n) |
| 275 | { |
| 276 | return (core::fpclassify)(x: n) == core::fp_nan; |
| 277 | } |
| 278 | }; |
| 279 | |
| 280 | template <> |
| 281 | struct is_nan<long double> |
| 282 | { |
| 283 | static bool call(long double n) |
| 284 | { |
| 285 | return (core::fpclassify)(x: n) == core::fp_nan; |
| 286 | } |
| 287 | }; |
| 288 | |
| 289 | template <typename T> |
| 290 | inline bool test_nan(T n) |
| 291 | { |
| 292 | return is_nan<T>::call(n); |
| 293 | } |
| 294 | |
| 295 | /////////////////////////////////////////////////////////////////////// |
| 296 | template <typename T, typename Enable/* = void*/> |
| 297 | struct is_infinite |
| 298 | { |
| 299 | static bool call(T n) |
| 300 | { |
| 301 | return std::numeric_limits<T>::has_infinity |
| 302 | && n == std::numeric_limits<T>::infinity(); |
| 303 | } |
| 304 | }; |
| 305 | |
| 306 | template <> |
| 307 | struct is_infinite<float> |
| 308 | { |
| 309 | static bool call(float n) |
| 310 | { |
| 311 | return (core::fpclassify)(x: n) == core::fp_infinite; |
| 312 | } |
| 313 | }; |
| 314 | |
| 315 | template <> |
| 316 | struct is_infinite<double> |
| 317 | { |
| 318 | static bool call(double n) |
| 319 | { |
| 320 | return (core::fpclassify)(x: n) == core::fp_infinite; |
| 321 | } |
| 322 | }; |
| 323 | |
| 324 | template <> |
| 325 | struct is_infinite<long double> |
| 326 | { |
| 327 | static bool call(long double n) |
| 328 | { |
| 329 | return (core::fpclassify)(x: n) == core::fp_infinite; |
| 330 | } |
| 331 | }; |
| 332 | |
| 333 | template <typename T> |
| 334 | inline bool test_infinite(T n) |
| 335 | { |
| 336 | return is_infinite<T>::call(n); |
| 337 | } |
| 338 | |
| 339 | /////////////////////////////////////////////////////////////////////// |
| 340 | struct cast_to_long |
| 341 | { |
| 342 | static long call(float n, mpl::false_) |
| 343 | { |
| 344 | return static_cast<long>(std::floor(x: n)); |
| 345 | } |
| 346 | |
| 347 | static long call(double n, mpl::false_) |
| 348 | { |
| 349 | return static_cast<long>(std::floor(x: n)); |
| 350 | } |
| 351 | |
| 352 | static long call(long double n, mpl::false_) |
| 353 | { |
| 354 | return static_cast<long>(std::floor(x: n)); |
| 355 | } |
| 356 | |
| 357 | template <typename T> |
| 358 | static long call(T n, mpl::false_) |
| 359 | { |
| 360 | // allow for ADL to find the correct overload for floor and |
| 361 | // lround |
| 362 | using namespace std; |
| 363 | return lround(floor(n)); |
| 364 | } |
| 365 | |
| 366 | template <typename T> |
| 367 | static long call(T n, mpl::true_) |
| 368 | { |
| 369 | return static_cast<long>(n); |
| 370 | } |
| 371 | |
| 372 | template <typename T> |
| 373 | static long call(T n) |
| 374 | { |
| 375 | return call(n, mpl::bool_<is_integral<T>::value>()); |
| 376 | } |
| 377 | }; |
| 378 | |
| 379 | /////////////////////////////////////////////////////////////////////// |
| 380 | struct truncate_to_long |
| 381 | { |
| 382 | static long call(float n, mpl::false_) |
| 383 | { |
| 384 | return test_negative(n) ? static_cast<long>(std::ceil(x: n)) : |
| 385 | static_cast<long>(std::floor(x: n)); |
| 386 | } |
| 387 | |
| 388 | static long call(double n, mpl::false_) |
| 389 | { |
| 390 | return test_negative(n) ? static_cast<long>(std::ceil(x: n)) : |
| 391 | static_cast<long>(std::floor(x: n)); |
| 392 | } |
| 393 | |
| 394 | static long call(long double n, mpl::false_) |
| 395 | { |
| 396 | return test_negative(n) ? static_cast<long>(std::ceil(x: n)) : |
| 397 | static_cast<long>(std::floor(x: n)); |
| 398 | } |
| 399 | |
| 400 | template <typename T> |
| 401 | static long call(T n, mpl::false_) |
| 402 | { |
| 403 | // allow for ADL to find the correct overloads for ltrunc |
| 404 | using namespace std; |
| 405 | return ltrunc(n); |
| 406 | } |
| 407 | |
| 408 | template <typename T> |
| 409 | static long call(T n, mpl::true_) |
| 410 | { |
| 411 | return static_cast<long>(n); |
| 412 | } |
| 413 | |
| 414 | template <typename T> |
| 415 | static long call(T n) |
| 416 | { |
| 417 | return call(n, mpl::bool_<is_integral<T>::value>()); |
| 418 | } |
| 419 | }; |
| 420 | |
| 421 | /////////////////////////////////////////////////////////////////////// |
| 422 | // |
| 423 | // Traits class for radix specific number conversion |
| 424 | // |
| 425 | // Convert a digit from binary representation to character |
| 426 | // representation: |
| 427 | // |
| 428 | // static int call(unsigned n); |
| 429 | // |
| 430 | /////////////////////////////////////////////////////////////////////// |
| 431 | namespace detail |
| 432 | { |
| 433 | template <typename CharEncoding, typename Tag, bool radix_less_than_10> |
| 434 | struct convert_digit |
| 435 | { |
| 436 | static int call(unsigned n) |
| 437 | { |
| 438 | if (n <= 9) |
| 439 | return n + '0'; |
| 440 | |
| 441 | using spirit::char_class::convert; |
| 442 | return convert<CharEncoding>::to(Tag(), n - 10 + 'a'); |
| 443 | } |
| 444 | }; |
| 445 | |
| 446 | template <> |
| 447 | struct convert_digit<unused_type, unused_type, false> |
| 448 | { |
| 449 | static int call(unsigned n) |
| 450 | { |
| 451 | if (n <= 9) |
| 452 | return n + '0'; |
| 453 | return n - 10 + 'a'; |
| 454 | } |
| 455 | }; |
| 456 | |
| 457 | template <typename CharEncoding, typename Tag> |
| 458 | struct convert_digit<CharEncoding, Tag, true> |
| 459 | { |
| 460 | static int call(unsigned n) |
| 461 | { |
| 462 | return n + '0'; |
| 463 | } |
| 464 | }; |
| 465 | } |
| 466 | |
| 467 | template <unsigned Radix, typename CharEncoding, typename Tag> |
| 468 | struct convert_digit |
| 469 | : detail::convert_digit<CharEncoding, Tag, (Radix <= 10) ? true : false> |
| 470 | {}; |
| 471 | |
| 472 | /////////////////////////////////////////////////////////////////////// |
| 473 | template <unsigned Radix> |
| 474 | struct divide |
| 475 | { |
| 476 | template <typename T> |
| 477 | static T call(T& n, mpl::true_) |
| 478 | { |
| 479 | return n / Radix; |
| 480 | } |
| 481 | |
| 482 | template <typename T> |
| 483 | static T call(T& n, mpl::false_) |
| 484 | { |
| 485 | // Allow ADL to find the correct overload for floor |
| 486 | using namespace std; |
| 487 | return floor(n / Radix); |
| 488 | } |
| 489 | |
| 490 | template <typename T> |
| 491 | static T call(T& n, T const&, int) |
| 492 | { |
| 493 | return call(n, mpl::bool_<is_integral<T>::value>()); |
| 494 | } |
| 495 | |
| 496 | template <typename T> |
| 497 | static T call(T& n) |
| 498 | { |
| 499 | return call(n, mpl::bool_<is_integral<T>::value>()); |
| 500 | } |
| 501 | }; |
| 502 | |
| 503 | // specialization for division by 10 |
| 504 | template <> |
| 505 | struct divide<10> |
| 506 | { |
| 507 | template <typename T> |
| 508 | static T call(T& n, T, int, mpl::true_) |
| 509 | { |
| 510 | return n / 10; |
| 511 | } |
| 512 | |
| 513 | template <typename T> |
| 514 | static T call(T, T& num, int exp, mpl::false_) |
| 515 | { |
| 516 | // Allow ADL to find the correct overload for floor |
| 517 | using namespace std; |
| 518 | return floor(num / spirit::traits::pow10<T>(exp)); |
| 519 | } |
| 520 | |
| 521 | template <typename T> |
| 522 | static T call(T& n, T& num, int exp) |
| 523 | { |
| 524 | return call(n, num, exp, mpl::bool_<is_integral<T>::value>()); |
| 525 | } |
| 526 | |
| 527 | template <typename T> |
| 528 | static T call(T& n) |
| 529 | { |
| 530 | return call(n, n, 1, mpl::bool_<is_integral<T>::value>()); |
| 531 | } |
| 532 | }; |
| 533 | |
| 534 | /////////////////////////////////////////////////////////////////////// |
| 535 | template <unsigned Radix> |
| 536 | struct remainder |
| 537 | { |
| 538 | template <typename T> |
| 539 | static long call(T n, mpl::true_) |
| 540 | { |
| 541 | // this cast is safe since we know the result is not larger |
| 542 | // than Radix |
| 543 | return static_cast<long>(n % Radix); |
| 544 | } |
| 545 | |
| 546 | template <typename T> |
| 547 | static long call(T n, mpl::false_) |
| 548 | { |
| 549 | // Allow ADL to find the correct overload for fmod |
| 550 | using namespace std; |
| 551 | return cast_to_long::call(fmod(n, T(Radix))); |
| 552 | } |
| 553 | |
| 554 | template <typename T> |
| 555 | static long call(T n) |
| 556 | { |
| 557 | return call(n, mpl::bool_<is_integral<T>::value>()); |
| 558 | } |
| 559 | }; |
| 560 | }}} |
| 561 | |
| 562 | namespace boost { namespace spirit { namespace karma |
| 563 | { |
| 564 | /////////////////////////////////////////////////////////////////////////// |
| 565 | // |
| 566 | // The int_inserter template takes care of the integer to string |
| 567 | // conversion. If specified, the loop is unrolled for better performance. |
| 568 | // |
| 569 | // Set the value BOOST_KARMA_NUMERICS_LOOP_UNROLL to some integer in |
| 570 | // between 0 (no unrolling) and the largest expected generated integer |
| 571 | // string length (complete unrolling). |
| 572 | // If not specified, this value defaults to 6. |
| 573 | // |
| 574 | /////////////////////////////////////////////////////////////////////////// |
| 575 | #define BOOST_KARMA_NUMERICS_INNER_LOOP_PREFIX(z, x, data) \ |
| 576 | if (!traits::test_zero(n)) { \ |
| 577 | int ch_##x = radix_type::call(remainder_type::call(n)); \ |
| 578 | n = divide_type::call(n, num, ++exp); \ |
| 579 | /**/ |
| 580 | |
| 581 | #define BOOST_KARMA_NUMERICS_INNER_LOOP_SUFFIX(z, x, n_rolls_sub1) \ |
| 582 | *sink = char(BOOST_PP_CAT(ch_, BOOST_PP_SUB(n_rolls_sub1, x))); \ |
| 583 | ++sink; \ |
| 584 | } \ |
| 585 | /**/ |
| 586 | |
| 587 | template < |
| 588 | unsigned Radix, typename CharEncoding = unused_type |
| 589 | , typename Tag = unused_type> |
| 590 | struct int_inserter |
| 591 | { |
| 592 | typedef traits::convert_digit<Radix, CharEncoding, Tag> radix_type; |
| 593 | typedef traits::divide<Radix> divide_type; |
| 594 | typedef traits::remainder<Radix> remainder_type; |
| 595 | |
| 596 | template <typename OutputIterator, typename T> |
| 597 | static bool |
| 598 | call(OutputIterator& sink, T n, T& num, int exp) |
| 599 | { |
| 600 | // remainder_type::call returns n % Radix |
| 601 | int ch = radix_type::call(remainder_type::call(n)); |
| 602 | n = divide_type::call(n, num, ++exp); |
| 603 | |
| 604 | BOOST_PP_REPEAT( |
| 605 | BOOST_KARMA_NUMERICS_LOOP_UNROLL, |
| 606 | BOOST_KARMA_NUMERICS_INNER_LOOP_PREFIX, _); |
| 607 | |
| 608 | if (!traits::test_zero(n)) |
| 609 | call(sink, n, num, exp); |
| 610 | |
| 611 | BOOST_PP_REPEAT( |
| 612 | BOOST_KARMA_NUMERICS_LOOP_UNROLL, |
| 613 | BOOST_KARMA_NUMERICS_INNER_LOOP_SUFFIX, |
| 614 | BOOST_PP_DEC(BOOST_KARMA_NUMERICS_LOOP_UNROLL)); |
| 615 | |
| 616 | *sink = char(ch); |
| 617 | ++sink; |
| 618 | return true; |
| 619 | } |
| 620 | |
| 621 | // Common code for integer string representations |
| 622 | template <typename OutputIterator, typename T> |
| 623 | static bool |
| 624 | call(OutputIterator& sink, T n) |
| 625 | { |
| 626 | return call(sink, n, n, 0); |
| 627 | } |
| 628 | |
| 629 | private: |
| 630 | // helper function returning the biggest number representable either in |
| 631 | // a boost::long_long_type (if this does exist) or in a plain long |
| 632 | // otherwise |
| 633 | #if defined(BOOST_HAS_LONG_LONG) |
| 634 | typedef boost::long_long_type biggest_long_type; |
| 635 | #else |
| 636 | typedef long biggest_long_type; |
| 637 | #endif |
| 638 | |
| 639 | static biggest_long_type max_long() |
| 640 | { |
| 641 | return (std::numeric_limits<biggest_long_type>::max)(); |
| 642 | } |
| 643 | |
| 644 | public: |
| 645 | // Specialization for doubles and floats, falling back to long integers |
| 646 | // for representable values. These specializations speed up formatting |
| 647 | // of floating point numbers considerably as all the required |
| 648 | // arithmetics will be executed using integral data types. |
| 649 | template <typename OutputIterator> |
| 650 | static bool |
| 651 | call(OutputIterator& sink, long double n) |
| 652 | { |
| 653 | if (std::fabs(x: n) < max_long()) |
| 654 | { |
| 655 | biggest_long_type l((biggest_long_type)n); |
| 656 | return call(sink, l, l, 0); |
| 657 | } |
| 658 | return call(sink, n, n, 0); |
| 659 | } |
| 660 | template <typename OutputIterator> |
| 661 | static bool |
| 662 | call(OutputIterator& sink, double n) |
| 663 | { |
| 664 | if (std::fabs(x: n) < max_long()) |
| 665 | { |
| 666 | biggest_long_type l((biggest_long_type)n); |
| 667 | return call(sink, l, l, 0); |
| 668 | } |
| 669 | return call(sink, n, n, 0); |
| 670 | } |
| 671 | template <typename OutputIterator> |
| 672 | static bool |
| 673 | call(OutputIterator& sink, float n) |
| 674 | { |
| 675 | if (std::fabs(x: n) < max_long()) |
| 676 | { |
| 677 | biggest_long_type l((biggest_long_type)n); |
| 678 | return call(sink, l, l, 0); |
| 679 | } |
| 680 | return call(sink, n, n, 0); |
| 681 | } |
| 682 | }; |
| 683 | |
| 684 | #undef BOOST_KARMA_NUMERICS_INNER_LOOP_PREFIX |
| 685 | #undef BOOST_KARMA_NUMERICS_INNER_LOOP_SUFFIX |
| 686 | |
| 687 | /////////////////////////////////////////////////////////////////////////// |
| 688 | // |
| 689 | // The uint_inserter template takes care of the conversion of any integer |
| 690 | // to a string, while interpreting the number as an unsigned type. |
| 691 | // |
| 692 | /////////////////////////////////////////////////////////////////////////// |
| 693 | template < |
| 694 | unsigned Radix, typename CharEncoding = unused_type |
| 695 | , typename Tag = unused_type> |
| 696 | struct uint_inserter : int_inserter<Radix, CharEncoding, Tag> |
| 697 | { |
| 698 | typedef int_inserter<Radix, CharEncoding, Tag> base_type; |
| 699 | |
| 700 | // Common code for integer string representations |
| 701 | template <typename OutputIterator, typename T> |
| 702 | static bool |
| 703 | call(OutputIterator& sink, T const& n) |
| 704 | { |
| 705 | typedef typename traits::absolute_value<T>::type type; |
| 706 | type un = type(n); |
| 707 | return base_type::call(sink, un, un, 0); |
| 708 | } |
| 709 | }; |
| 710 | |
| 711 | /////////////////////////////////////////////////////////////////////////// |
| 712 | // |
| 713 | // The sign_inserter template generates a sign for a given numeric value. |
| 714 | // |
| 715 | // The parameter forcesign allows to generate a sign even for positive |
| 716 | // numbers. |
| 717 | // |
| 718 | /////////////////////////////////////////////////////////////////////////// |
| 719 | struct sign_inserter |
| 720 | { |
| 721 | template <typename OutputIterator> |
| 722 | static bool |
| 723 | call_noforce(OutputIterator& sink, bool is_zero, bool is_negative, |
| 724 | bool sign_if_zero) |
| 725 | { |
| 726 | // generate a sign for negative numbers only |
| 727 | if (is_negative || (is_zero && sign_if_zero)) { |
| 728 | *sink = '-'; |
| 729 | ++sink; |
| 730 | } |
| 731 | return true; |
| 732 | } |
| 733 | |
| 734 | template <typename OutputIterator> |
| 735 | static bool |
| 736 | call_force(OutputIterator& sink, bool is_zero, bool is_negative, |
| 737 | bool sign_if_zero) |
| 738 | { |
| 739 | // generate a sign for all numbers except zero |
| 740 | if (!is_zero || sign_if_zero) |
| 741 | *sink = is_negative ? '-' : '+'; |
| 742 | else |
| 743 | *sink = ' '; |
| 744 | |
| 745 | ++sink; |
| 746 | return true; |
| 747 | } |
| 748 | |
| 749 | template <typename OutputIterator> |
| 750 | static bool |
| 751 | call(OutputIterator& sink, bool is_zero, bool is_negative |
| 752 | , bool forcesign, bool sign_if_zero = false) |
| 753 | { |
| 754 | return forcesign ? |
| 755 | call_force(sink, is_zero, is_negative, sign_if_zero) : |
| 756 | call_noforce(sink, is_zero, is_negative, sign_if_zero); |
| 757 | } |
| 758 | }; |
| 759 | |
| 760 | /////////////////////////////////////////////////////////////////////////// |
| 761 | // These are helper functions for the real policies allowing to generate |
| 762 | // a single character and a string |
| 763 | /////////////////////////////////////////////////////////////////////////// |
| 764 | template <typename CharEncoding = unused_type, typename Tag = unused_type> |
| 765 | struct char_inserter |
| 766 | { |
| 767 | template <typename OutputIterator, typename Char> |
| 768 | static bool call(OutputIterator& sink, Char c) |
| 769 | { |
| 770 | return detail::generate_to(sink, c, CharEncoding(), Tag()); |
| 771 | } |
| 772 | }; |
| 773 | |
| 774 | template <typename CharEncoding = unused_type, typename Tag = unused_type> |
| 775 | struct string_inserter |
| 776 | { |
| 777 | template <typename OutputIterator, typename String> |
| 778 | static bool call(OutputIterator& sink, String str) |
| 779 | { |
| 780 | return detail::string_generate(sink, str, CharEncoding(), Tag()); |
| 781 | } |
| 782 | }; |
| 783 | |
| 784 | }}} |
| 785 | |
| 786 | #endif |
| 787 | |