/usr/include/boost/lexical_cast.hpp is in libboost1.46-dev 1.46.1-7ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 | #ifndef BOOST_LEXICAL_CAST_INCLUDED
#define BOOST_LEXICAL_CAST_INCLUDED
// Boost lexical_cast.hpp header -------------------------------------------//
//
// See http://www.boost.org/libs/conversion for documentation.
// See end of this header for rights and permissions.
//
// what: lexical_cast custom keyword cast
// who: contributed by Kevlin Henney,
// enhanced with contributions from Terje Slettebo,
// with additional fixes and suggestions from Gennaro Prota,
// Beman Dawes, Dave Abrahams, Daryle Walker, Peter Dimov,
// Alexander Nasonov and other Boosters
// when: November 2000, March 2003, June 2005, June 2006
#include <climits>
#include <cstddef>
#include <istream>
#include <string>
#include <typeinfo>
#include <exception>
#include <boost/config.hpp>
#include <boost/limits.hpp>
#include <boost/mpl/if.hpp>
#include <boost/throw_exception.hpp>
#include <boost/type_traits/is_pointer.hpp>
#include <boost/type_traits/make_unsigned.hpp>
#include <boost/call_traits.hpp>
#include <boost/static_assert.hpp>
#include <boost/detail/lcast_precision.hpp>
#include <boost/detail/workaround.hpp>
#ifndef BOOST_NO_STD_LOCALE
#include <locale>
#endif
#ifdef BOOST_NO_STRINGSTREAM
#include <strstream>
#else
#include <sstream>
#endif
#if defined(BOOST_NO_STRINGSTREAM) || defined(BOOST_NO_STD_WSTRING)
#define BOOST_LCAST_NO_WCHAR_T
#endif
#ifdef BOOST_NO_TYPEID
#define BOOST_LCAST_THROW_BAD_CAST(S, T) throw_exception(bad_lexical_cast())
#else
#define BOOST_LCAST_THROW_BAD_CAST(Source, Target) \
throw_exception(bad_lexical_cast(typeid(Source), typeid(Target)))
#endif
namespace boost
{
// exception used to indicate runtime lexical_cast failure
class bad_lexical_cast : public std::bad_cast
#if defined(__BORLANDC__) && BOOST_WORKAROUND( __BORLANDC__, < 0x560 )
// under bcc32 5.5.1 bad_cast doesn't derive from exception
, public std::exception
#endif
{
public:
bad_lexical_cast() :
#ifndef BOOST_NO_TYPEID
source(&typeid(void)), target(&typeid(void))
#else
source(0), target(0) // this breaks getters
#endif
{
}
bad_lexical_cast(
const std::type_info &source_type_arg,
const std::type_info &target_type_arg) :
source(&source_type_arg), target(&target_type_arg)
{
}
const std::type_info &source_type() const
{
return *source;
}
const std::type_info &target_type() const
{
return *target;
}
virtual const char *what() const throw()
{
return "bad lexical cast: "
"source type value could not be interpreted as target";
}
virtual ~bad_lexical_cast() throw()
{
}
private:
const std::type_info *source;
const std::type_info *target;
};
namespace detail // selectors for choosing stream character type
{
template<typename Type>
struct stream_char
{
typedef char type;
};
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
template<class CharT, class Traits, class Alloc>
struct stream_char< std::basic_string<CharT,Traits,Alloc> >
{
typedef CharT type;
};
#endif
#ifndef BOOST_LCAST_NO_WCHAR_T
#ifndef BOOST_NO_INTRINSIC_WCHAR_T
template<>
struct stream_char<wchar_t>
{
typedef wchar_t type;
};
#endif
template<>
struct stream_char<wchar_t *>
{
typedef wchar_t type;
};
template<>
struct stream_char<const wchar_t *>
{
typedef wchar_t type;
};
#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
template<>
struct stream_char<std::wstring>
{
typedef wchar_t type;
};
#endif
#endif
template<typename TargetChar, typename SourceChar>
struct widest_char
{
typedef TargetChar type;
};
template<>
struct widest_char<char, wchar_t>
{
typedef wchar_t type;
};
}
namespace detail // deduce_char_traits template
{
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
template<class CharT, class Target, class Source>
struct deduce_char_traits
{
typedef std::char_traits<CharT> type;
};
template<class CharT, class Traits, class Alloc, class Source>
struct deduce_char_traits< CharT
, std::basic_string<CharT,Traits,Alloc>
, Source
>
{
typedef Traits type;
};
template<class CharT, class Target, class Traits, class Alloc>
struct deduce_char_traits< CharT
, Target
, std::basic_string<CharT,Traits,Alloc>
>
{
typedef Traits type;
};
template<class CharT, class Traits, class Alloc1, class Alloc2>
struct deduce_char_traits< CharT
, std::basic_string<CharT,Traits,Alloc1>
, std::basic_string<CharT,Traits,Alloc2>
>
{
typedef Traits type;
};
#endif
}
namespace detail // lcast_src_length
{
// Return max. length of string representation of Source;
// 0 if unlimited (with exceptions for some types, see below).
// Values with limited string representation are placed to
// the buffer locally defined in lexical_cast function.
// 1 is returned for few types such as CharT const* or
// std::basic_string<CharT> that already have an internal
// buffer ready to be reused by lexical_stream_limited_src.
// Each specialization should have a correspondent operator<<
// defined in lexical_stream_limited_src.
template< class CharT // A result of widest_char transformation.
, class Source // Source type of lexical_cast.
>
struct lcast_src_length
{
BOOST_STATIC_CONSTANT(std::size_t, value = 0);
// To check coverage, build the test with
// bjam --v2 profile optimization=off
static void check_coverage() {}
};
template<>
struct lcast_src_length<char, bool>
{
BOOST_STATIC_CONSTANT(std::size_t, value = 1);
static void check_coverage() {}
};
template<>
struct lcast_src_length<char, char>
{
BOOST_STATIC_CONSTANT(std::size_t, value = 1);
static void check_coverage() {}
};
// No specializations for:
// lcast_src_length<char, signed char>
// lcast_src_length<char, unsigned char>
// lcast_src_length<char, signed char*>
// lcast_src_length<char, unsigned char*>
// lcast_src_length<char, signed char const*>
// lcast_src_length<char, unsigned char const*>
#ifndef BOOST_LCAST_NO_WCHAR_T
template<>
struct lcast_src_length<wchar_t, bool>
{
BOOST_STATIC_CONSTANT(std::size_t, value = 1);
static void check_coverage() {}
};
template<>
struct lcast_src_length<wchar_t, char>
{
BOOST_STATIC_CONSTANT(std::size_t, value = 1);
static void check_coverage() {}
};
#ifndef BOOST_NO_INTRINSIC_WCHAR_T
template<>
struct lcast_src_length<wchar_t, wchar_t>
{
BOOST_STATIC_CONSTANT(std::size_t, value = 1);
static void check_coverage() {}
};
#endif
#endif
template<>
struct lcast_src_length<char, char const*>
{
BOOST_STATIC_CONSTANT(std::size_t, value = 1);
static void check_coverage() {}
};
template<>
struct lcast_src_length<char, char*>
{
BOOST_STATIC_CONSTANT(std::size_t, value = 1);
static void check_coverage() {}
};
#ifndef BOOST_LCAST_NO_WCHAR_T
template<>
struct lcast_src_length<wchar_t, wchar_t const*>
{
BOOST_STATIC_CONSTANT(std::size_t, value = 1);
static void check_coverage() {}
};
template<>
struct lcast_src_length<wchar_t, wchar_t*>
{
BOOST_STATIC_CONSTANT(std::size_t, value = 1);
static void check_coverage() {}
};
#endif
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
template<class CharT, class Traits, class Alloc>
struct lcast_src_length< CharT, std::basic_string<CharT,Traits,Alloc> >
{
BOOST_STATIC_CONSTANT(std::size_t, value = 1);
static void check_coverage() {}
};
#else
template<>
struct lcast_src_length< char, std::basic_string<char> >
{
BOOST_STATIC_CONSTANT(std::size_t, value = 1);
static void check_coverage() {}
};
#ifndef BOOST_LCAST_NO_WCHAR_T
template<>
struct lcast_src_length< wchar_t, std::basic_string<wchar_t> >
{
BOOST_STATIC_CONSTANT(std::size_t, value = 1);
static void check_coverage() {}
};
#endif
#endif
// Helper for integral types.
// Notes on length calculation:
// Max length for 32bit int with grouping "\1" and thousands_sep ',':
// "-2,1,4,7,4,8,3,6,4,7"
// ^ - is_signed
// ^ - 1 digit not counted by digits10
// ^^^^^^^^^^^^^^^^^^ - digits10 * 2
//
// Constant is_specialized is used instead of constant 1
// to prevent buffer overflow in a rare case when
// <boost/limits.hpp> doesn't add missing specialization for
// numeric_limits<T> for some integral type T.
// When is_specialized is false, the whole expression is 0.
template<class Source>
struct lcast_src_length_integral
{
#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
BOOST_STATIC_CONSTANT(std::size_t, value =
std::numeric_limits<Source>::is_signed +
std::numeric_limits<Source>::is_specialized + // == 1
std::numeric_limits<Source>::digits10 * 2
);
#else
BOOST_STATIC_CONSTANT(std::size_t, value = 156);
BOOST_STATIC_ASSERT(sizeof(Source) * CHAR_BIT <= 256);
#endif
};
#define BOOST_LCAST_DEF1(CharT, T) \
template<> struct lcast_src_length<CharT, T> \
: lcast_src_length_integral<T> \
{ static void check_coverage() {} };
#ifdef BOOST_LCAST_NO_WCHAR_T
#define BOOST_LCAST_DEF(T) BOOST_LCAST_DEF1(char, T)
#else
#define BOOST_LCAST_DEF(T) \
BOOST_LCAST_DEF1(char, T) \
BOOST_LCAST_DEF1(wchar_t, T)
#endif
BOOST_LCAST_DEF(short)
BOOST_LCAST_DEF(unsigned short)
BOOST_LCAST_DEF(int)
BOOST_LCAST_DEF(unsigned int)
BOOST_LCAST_DEF(long)
BOOST_LCAST_DEF(unsigned long)
#if defined(BOOST_HAS_LONG_LONG)
BOOST_LCAST_DEF(boost::ulong_long_type)
BOOST_LCAST_DEF(boost::long_long_type )
#elif defined(BOOST_HAS_MS_INT64)
BOOST_LCAST_DEF(unsigned __int64)
BOOST_LCAST_DEF( __int64)
#endif
#undef BOOST_LCAST_DEF
#undef BOOST_LCAST_DEF1
#ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION
// Helper for floating point types.
// -1.23456789e-123456
// ^ sign
// ^ leading digit
// ^ decimal point
// ^^^^^^^^ lcast_precision<Source>::value
// ^ "e"
// ^ exponent sign
// ^^^^^^ exponent (assumed 6 or less digits)
// sign + leading digit + decimal point + "e" + exponent sign == 5
template<class Source>
struct lcast_src_length_floating
{
BOOST_STATIC_ASSERT(
std::numeric_limits<Source>::max_exponent10 <= 999999L &&
std::numeric_limits<Source>::min_exponent10 >= -999999L
);
BOOST_STATIC_CONSTANT(std::size_t, value =
5 + lcast_precision<Source>::value + 6
);
};
template<>
struct lcast_src_length<char,float>
: lcast_src_length_floating<float>
{
static void check_coverage() {}
};
template<>
struct lcast_src_length<char,double>
: lcast_src_length_floating<double>
{
static void check_coverage() {}
};
template<>
struct lcast_src_length<char,long double>
: lcast_src_length_floating<long double>
{
static void check_coverage() {}
};
#ifndef BOOST_LCAST_NO_WCHAR_T
template<>
struct lcast_src_length<wchar_t,float>
: lcast_src_length_floating<float>
{
static void check_coverage() {}
};
template<>
struct lcast_src_length<wchar_t,double>
: lcast_src_length_floating<double>
{
static void check_coverage() {}
};
template<>
struct lcast_src_length<wchar_t,long double>
: lcast_src_length_floating<long double>
{
static void check_coverage() {}
};
#endif // #ifndef BOOST_LCAST_NO_WCHAR_T
#endif // #ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION
}
namespace detail // '0' and '-' constants
{
template<typename CharT> struct lcast_char_constants;
template<>
struct lcast_char_constants<char>
{
BOOST_STATIC_CONSTANT(char, zero = '0');
BOOST_STATIC_CONSTANT(char, minus = '-');
};
#ifndef BOOST_LCAST_NO_WCHAR_T
template<>
struct lcast_char_constants<wchar_t>
{
BOOST_STATIC_CONSTANT(wchar_t, zero = L'0');
BOOST_STATIC_CONSTANT(wchar_t, minus = L'-');
};
#endif
}
namespace detail // lexical_streambuf_fake
{
struct lexical_streambuf_fake
{
};
}
namespace detail // lcast_to_unsigned
{
#if (defined _MSC_VER)
# pragma warning( push )
// C4146: unary minus operator applied to unsigned type, result still unsigned
# pragma warning( disable : 4146 )
#elif defined( __BORLANDC__ )
# pragma option push -w-8041
#endif
template<class T>
inline
BOOST_DEDUCED_TYPENAME make_unsigned<T>::type lcast_to_unsigned(T value)
{
typedef BOOST_DEDUCED_TYPENAME make_unsigned<T>::type result_type;
result_type uvalue = static_cast<result_type>(value);
return value < 0 ? -uvalue : uvalue;
}
#if (defined _MSC_VER)
# pragma warning( pop )
#elif defined( __BORLANDC__ )
# pragma option pop
#endif
}
namespace detail // lcast_put_unsigned
{
template<class Traits, class T, class CharT>
CharT* lcast_put_unsigned(T n, CharT* finish)
{
#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
BOOST_STATIC_ASSERT(!std::numeric_limits<T>::is_signed);
#endif
#ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
// TODO: use BOOST_NO_STD_LOCALE
std::locale loc;
typedef std::numpunct<CharT> numpunct;
numpunct const& np = BOOST_USE_FACET(numpunct, loc);
std::string const& grouping = np.grouping();
std::string::size_type const grouping_size = grouping.size();
CharT thousands_sep = grouping_size ? np.thousands_sep() : 0;
std::string::size_type group = 0; // current group number
char last_grp_size = grouping[0] <= 0 ? CHAR_MAX : grouping[0];
// a) Since grouping is const, grouping[grouping.size()] returns 0.
// b) It's safe to assume here and below that CHAR_MAX
// is equivalent to unlimited grouping:
#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
BOOST_STATIC_ASSERT(std::numeric_limits<T>::digits10 < CHAR_MAX);
#endif
char left = last_grp_size;
#endif
typedef typename Traits::int_type int_type;
CharT const czero = lcast_char_constants<CharT>::zero;
int_type const zero = Traits::to_int_type(czero);
do
{
#ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
if(left == 0)
{
++group;
if(group < grouping_size)
{
char const grp_size = grouping[group];
last_grp_size = grp_size <= 0 ? CHAR_MAX : grp_size;
}
left = last_grp_size;
--finish;
Traits::assign(*finish, thousands_sep);
}
--left;
#endif
--finish;
int_type const digit = static_cast<int_type>(n % 10U);
Traits::assign(*finish, Traits::to_char_type(zero + digit));
n /= 10;
} while(n);
return finish;
}
}
namespace detail // stream wrapper for handling lexical conversions
{
template<typename Target, typename Source, typename Traits>
class lexical_stream
{
private:
typedef typename widest_char<
typename stream_char<Target>::type,
typename stream_char<Source>::type>::type char_type;
typedef Traits traits_type;
public:
lexical_stream(char_type* = 0, char_type* = 0)
{
stream.unsetf(std::ios::skipws);
lcast_set_precision(stream, static_cast<Source*>(0), static_cast<Target*>(0) );
}
~lexical_stream()
{
#if defined(BOOST_NO_STRINGSTREAM)
stream.freeze(false);
#endif
}
bool operator<<(const Source &input)
{
return !(stream << input).fail();
}
template<typename InputStreamable>
bool operator>>(InputStreamable &output)
{
return !is_pointer<InputStreamable>::value &&
stream >> output &&
stream.get() ==
#if defined(__GNUC__) && (__GNUC__<3) && defined(BOOST_NO_STD_WSTRING)
// GCC 2.9x lacks std::char_traits<>::eof().
// We use BOOST_NO_STD_WSTRING to filter out STLport and libstdc++-v3
// configurations, which do provide std::char_traits<>::eof().
EOF;
#else
traits_type::eof();
#endif
}
#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
bool operator>>(std::string &output)
{
#if defined(BOOST_NO_STRINGSTREAM)
stream << '\0';
#endif
stream.str().swap(output);
return true;
}
#ifndef BOOST_LCAST_NO_WCHAR_T
bool operator>>(std::wstring &output)
{
stream.str().swap(output);
return true;
}
#endif
#else
bool operator>>(std::basic_string<char_type,traits_type>& output)
{
stream.str().swap(output);
return true;
}
template<class Alloc>
bool operator>>(std::basic_string<char_type,traits_type,Alloc>& out)
{
std::basic_string<char_type,traits_type> str(stream.str());
out.assign(str.begin(), str.end());
return true;
}
#endif
private:
#if defined(BOOST_NO_STRINGSTREAM)
std::strstream stream;
#elif defined(BOOST_NO_STD_LOCALE)
std::stringstream stream;
#else
std::basic_stringstream<char_type,traits_type> stream;
#endif
};
}
namespace detail // optimized stream wrapper
{
// String representation of Source has an upper limit.
template< class CharT // a result of widest_char transformation
, class Base // lexical_streambuf_fake or basic_streambuf<CharT>
, class Traits // usually char_traits<CharT>
>
class lexical_stream_limited_src : public Base
{
// A string representation of Source is written to [start, finish).
// Currently, it is assumed that [start, finish) is big enough
// to hold a string representation of any Source value.
CharT* start;
CharT* finish;
private:
static void widen_and_assign(char*p, char ch)
{
Traits::assign(*p, ch);
}
#ifndef BOOST_LCAST_NO_WCHAR_T
static void widen_and_assign(wchar_t* p, char ch)
{
// TODO: use BOOST_NO_STD_LOCALE
std::locale loc;
wchar_t w = BOOST_USE_FACET(std::ctype<wchar_t>, loc).widen(ch);
Traits::assign(*p, w);
}
static void widen_and_assign(wchar_t* p, wchar_t ch)
{
Traits::assign(*p, ch);
}
static void widen_and_assign(char*, wchar_t ch); // undefined
#endif
template<class OutputStreamable>
bool lcast_put(const OutputStreamable& input)
{
this->setp(start, finish);
std::basic_ostream<CharT> stream(static_cast<Base*>(this));
lcast_set_precision(stream, static_cast<OutputStreamable*>(0));
bool const result = !(stream << input).fail();
finish = this->pptr();
return result;
}
// Undefined:
lexical_stream_limited_src(lexical_stream_limited_src const&);
void operator=(lexical_stream_limited_src const&);
public:
lexical_stream_limited_src(CharT* sta, CharT* fin)
: start(sta)
, finish(fin)
{}
public: // output
template<class Alloc>
bool operator<<(std::basic_string<CharT,Traits,Alloc> const& str)
{
start = const_cast<CharT*>(str.data());
finish = start + str.length();
return true;
}
bool operator<<(bool);
bool operator<<(char);
#if !defined(BOOST_LCAST_NO_WCHAR_T) && !defined(BOOST_NO_INTRINSIC_WCHAR_T)
bool operator<<(wchar_t);
#endif
bool operator<<(CharT const*);
bool operator<<(short);
bool operator<<(int);
bool operator<<(long);
bool operator<<(unsigned short);
bool operator<<(unsigned int);
bool operator<<(unsigned long);
#if defined(BOOST_HAS_LONG_LONG)
bool operator<<(boost::ulong_long_type);
bool operator<<(boost::long_long_type );
#elif defined(BOOST_HAS_MS_INT64)
bool operator<<(unsigned __int64);
bool operator<<( __int64);
#endif
// These three operators use ostream and streambuf.
// lcast_streambuf_for_source<T>::value is true.
bool operator<<(float);
bool operator<<(double);
bool operator<<(long double);
public: // input
// Generic istream-based algorithm.
// lcast_streambuf_for_target<InputStreamable>::value is true.
template<typename InputStreamable>
bool operator>>(InputStreamable& output)
{
#if (defined _MSC_VER)
# pragma warning( push )
// conditional expression is constant
# pragma warning( disable : 4127 )
#endif
if(is_pointer<InputStreamable>::value)
return false;
this->setg(start, start, finish);
std::basic_istream<CharT> stream(static_cast<Base*>(this));
stream.unsetf(std::ios::skipws);
lcast_set_precision(stream, static_cast<InputStreamable*>(0));
#if (defined _MSC_VER)
# pragma warning( pop )
#endif
return stream >> output &&
stream.get() ==
#if defined(__GNUC__) && (__GNUC__<3) && defined(BOOST_NO_STD_WSTRING)
// GCC 2.9x lacks std::char_traits<>::eof().
// We use BOOST_NO_STD_WSTRING to filter out STLport and libstdc++-v3
// configurations, which do provide std::char_traits<>::eof().
EOF;
#else
Traits::eof();
#endif
}
bool operator>>(CharT&);
#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
// This #if is in sync with lcast_streambuf_for_target
bool operator>>(std::string&);
#ifndef BOOST_LCAST_NO_WCHAR_T
bool operator>>(std::wstring&);
#endif
#else
template<class Alloc>
bool operator>>(std::basic_string<CharT,Traits,Alloc>& str)
{
str.assign(start, finish);
return true;
}
#endif
};
template<typename CharT, class Base, class Traits>
inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator<<(
bool value)
{
typedef typename Traits::int_type int_type;
CharT const czero = lcast_char_constants<CharT>::zero;
int_type const zero = Traits::to_int_type(czero);
Traits::assign(*start, Traits::to_char_type(zero + value));
finish = start + 1;
return true;
}
template<typename CharT, class Base, class Traits>
inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator<<(
char ch)
{
widen_and_assign(start, ch);
finish = start + 1;
return true;
}
#if !defined(BOOST_LCAST_NO_WCHAR_T) && !defined(BOOST_NO_INTRINSIC_WCHAR_T)
template<typename CharT, class Base, class Traits>
inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator<<(
wchar_t ch)
{
widen_and_assign(start, ch);
finish = start + 1;
return true;
}
#endif
template<typename CharT, class Base, class Traits>
inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator<<(
short n)
{
start = lcast_put_unsigned<Traits>(lcast_to_unsigned(n), finish);
if(n < 0)
{
--start;
CharT const minus = lcast_char_constants<CharT>::minus;
Traits::assign(*start, minus);
}
return true;
}
template<typename CharT, class Base, class Traits>
inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator<<(
int n)
{
start = lcast_put_unsigned<Traits>(lcast_to_unsigned(n), finish);
if(n < 0)
{
--start;
CharT const minus = lcast_char_constants<CharT>::minus;
Traits::assign(*start, minus);
}
return true;
}
template<typename CharT, class Base, class Traits>
inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator<<(
long n)
{
start = lcast_put_unsigned<Traits>(lcast_to_unsigned(n), finish);
if(n < 0)
{
--start;
CharT const minus = lcast_char_constants<CharT>::minus;
Traits::assign(*start, minus);
}
return true;
}
#if defined(BOOST_HAS_LONG_LONG)
template<typename CharT, class Base, class Traits>
inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator<<(
boost::long_long_type n)
{
start = lcast_put_unsigned<Traits>(lcast_to_unsigned(n), finish);
if(n < 0)
{
--start;
CharT const minus = lcast_char_constants<CharT>::minus;
Traits::assign(*start, minus);
}
return true;
}
#elif defined(BOOST_HAS_MS_INT64)
template<typename CharT, class Base, class Traits>
inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator<<(
__int64 n)
{
start = lcast_put_unsigned<Traits>(lcast_to_unsigned(n), finish);
if(n < 0)
{
--start;
CharT const minus = lcast_char_constants<CharT>::minus;
Traits::assign(*start, minus);
}
return true;
}
#endif
template<typename CharT, class Base, class Traits>
inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator<<(
unsigned short n)
{
start = lcast_put_unsigned<Traits>(n, finish);
return true;
}
template<typename CharT, class Base, class Traits>
inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator<<(
unsigned int n)
{
start = lcast_put_unsigned<Traits>(n, finish);
return true;
}
template<typename CharT, class Base, class Traits>
inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator<<(
unsigned long n)
{
start = lcast_put_unsigned<Traits>(n, finish);
return true;
}
#if defined(BOOST_HAS_LONG_LONG)
template<typename CharT, class Base, class Traits>
inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator<<(
boost::ulong_long_type n)
{
start = lcast_put_unsigned<Traits>(n, finish);
return true;
}
#elif defined(BOOST_HAS_MS_INT64)
template<typename CharT, class Base, class Traits>
inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator<<(
unsigned __int64 n)
{
start = lcast_put_unsigned<Traits>(n, finish);
return true;
}
#endif
template<typename CharT, class Base, class Traits>
inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator<<(
float val)
{
return this->lcast_put(val);
}
template<typename CharT, class Base, class Traits>
inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator<<(
double val)
{
return this->lcast_put(val);
}
template<typename CharT, class Base, class Traits>
inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator<<(
long double val)
{
return this->lcast_put(val);
}
template<typename CharT, class Base, class Traits>
inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator<<(
CharT const* str)
{
start = const_cast<CharT*>(str);
finish = start + Traits::length(str);
return true;
}
template<typename CharT, class Base, class Traits>
inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator>>(
CharT& output)
{
bool const ok = (finish - start == 1);
if(ok)
Traits::assign(output, *start);
return ok;
}
#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
template<typename CharT, class Base, class Traits>
inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator>>(
std::string& str)
{
str.assign(start, finish);
return true;
}
#ifndef BOOST_LCAST_NO_WCHAR_T
template<typename CharT, class Base, class Traits>
inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator>>(
std::wstring& str)
{
str.assign(start, finish);
return true;
}
#endif
#endif
}
namespace detail // lcast_streambuf_for_source
{
// Returns true if optimized stream wrapper needs ostream for writing.
template<class Source>
struct lcast_streambuf_for_source
{
BOOST_STATIC_CONSTANT(bool, value = false);
};
template<>
struct lcast_streambuf_for_source<float>
{
BOOST_STATIC_CONSTANT(bool, value = true);
};
template<>
struct lcast_streambuf_for_source<double>
{
BOOST_STATIC_CONSTANT(bool, value = true);
};
template<>
struct lcast_streambuf_for_source<long double>
{
BOOST_STATIC_CONSTANT(bool, value = true);
};
}
namespace detail // lcast_streambuf_for_target
{
// Returns true if optimized stream wrapper needs istream for reading.
template<class Target>
struct lcast_streambuf_for_target
{
BOOST_STATIC_CONSTANT(bool, value = true);
};
template<>
struct lcast_streambuf_for_target<char>
{
BOOST_STATIC_CONSTANT(bool, value = false);
};
#if !defined(BOOST_LCAST_NO_WCHAR_T) && !defined(BOOST_NO_INTRINSIC_WCHAR_T)
template<>
struct lcast_streambuf_for_target<wchar_t>
{
BOOST_STATIC_CONSTANT(bool, value = false);
};
#endif
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
template<class Traits, class Alloc>
struct lcast_streambuf_for_target<
std::basic_string<char,Traits,Alloc> >
{
BOOST_STATIC_CONSTANT(bool, value = false);
};
#ifndef BOOST_LCAST_NO_WCHAR_T
template<class Traits, class Alloc>
struct lcast_streambuf_for_target<
std::basic_string<wchar_t,Traits,Alloc> >
{
BOOST_STATIC_CONSTANT(bool, value = false);
};
#endif
#else
template<>
struct lcast_streambuf_for_target<std::string>
{
BOOST_STATIC_CONSTANT(bool, value = false);
};
#ifndef BOOST_LCAST_NO_WCHAR_T
template<>
struct lcast_streambuf_for_target<std::wstring>
{
BOOST_STATIC_CONSTANT(bool, value = false);
};
#endif
#endif
}
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
// call-by-const reference version
namespace detail
{
template<class T>
struct array_to_pointer_decay
{
typedef T type;
};
template<class T, std::size_t N>
struct array_to_pointer_decay<T[N]>
{
typedef const T * type;
};
#if (defined _MSC_VER)
# pragma warning( push )
# pragma warning( disable : 4701 ) // possible use of ... before initialization
# pragma warning( disable : 4702 ) // unreachable code
#endif
template< typename Target
, typename Source
, bool Unlimited // string representation of Source is unlimited
, typename CharT
>
Target lexical_cast(
BOOST_DEDUCED_TYPENAME boost::call_traits<Source>::param_type arg,
CharT* buf, std::size_t src_len)
{
typedef BOOST_DEDUCED_TYPENAME
deduce_char_traits<CharT,Target,Source>::type traits;
typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_c<
lcast_streambuf_for_target<Target>::value ||
lcast_streambuf_for_source<Source>::value
, std::basic_streambuf<CharT>
, lexical_streambuf_fake
>::type base;
BOOST_DEDUCED_TYPENAME boost::mpl::if_c<
Unlimited
, detail::lexical_stream<Target,Source,traits>
, detail::lexical_stream_limited_src<CharT,base,traits>
>::type interpreter(buf, buf + src_len);
Target result;
if(!(interpreter << arg && interpreter >> result))
BOOST_LCAST_THROW_BAD_CAST(Source, Target);
return result;
}
#if (defined _MSC_VER)
# pragma warning( pop )
#endif
}
template<typename Target, typename Source>
inline Target lexical_cast(const Source &arg)
{
typedef typename detail::array_to_pointer_decay<Source>::type src;
typedef typename detail::widest_char<
typename detail::stream_char<Target>::type
, typename detail::stream_char<src>::type
>::type char_type;
typedef detail::lcast_src_length<char_type, src> lcast_src_length;
std::size_t const src_len = lcast_src_length::value;
char_type buf[src_len + 1];
lcast_src_length::check_coverage();
return detail::lexical_cast<Target, src, !src_len>(arg, buf, src_len);
}
#else
// call-by-value fallback version (deprecated)
template<typename Target, typename Source>
Target lexical_cast(Source arg)
{
typedef typename detail::widest_char<
BOOST_DEDUCED_TYPENAME detail::stream_char<Target>::type
, BOOST_DEDUCED_TYPENAME detail::stream_char<Source>::type
>::type char_type;
typedef std::char_traits<char_type> traits;
detail::lexical_stream<Target, Source, traits> interpreter;
Target result;
if(!(interpreter << arg && interpreter >> result))
#ifndef BOOST_NO_TYPEID
throw_exception(bad_lexical_cast(typeid(Source), typeid(Target)));
#else
throw_exception(bad_lexical_cast());
#endif
return result;
}
#endif
}
// Copyright Kevlin Henney, 2000-2005.
// Copyright Alexander Nasonov, 2006-2007.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#undef BOOST_LCAST_NO_WCHAR_T
#endif
|