#ifndef BOOST_LEXICAL_CAST_INCLUDED #define BOOST_LEXICAL_CAST_INCLUDED // Boost lexical_cast.hpp header -------------------------------------------// // // See http://www.boost.org for most recent version including 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 Slettebų, // with additional fixes and suggestions from Gennaro Prota, // Beman Dawes, Dave Abrahams, Daryle Walker, Peter Dimov, // and other Boosters // when: November 2000, March 2003 // Changed number of significant digits, Paul Bristow 9 Aug 2003 // To enable clean compile MSVC 7.1 strict & warnings level 4. #ifdef _MSC_VER #pragma warning (disable : 4701) // variable may be returned without have being initialised. #pragma warning (disable : 4511) // copy constructor could not be generated #pragma warning (disable : 4512) // assignment operator could not be generated #pragma warning (disable : 4127) // conditional expression is constant (stream precision) #endif #include #include #include #include #include #include #ifdef BOOST_NO_STRINGSTREAM #include #else #include #endif #if defined(BOOST_NO_STRINGSTREAM) || \ defined(BOOST_NO_STD_WSTRING) || \ defined(BOOST_NO_STD_LOCALE) || \ defined(BOOST_NO_INTRINSIC_WCHAR_T) #define DISABLE_WIDE_CHAR_SUPPORT #endif namespace boost { // exception used to indicate runtime lexical_cast failure. class bad_lexical_cast : public std::bad_cast { public: bad_lexical_cast() : source(&typeid(void)), target(&typeid(void)) { } bad_lexical_cast( const std::type_info &s, const std::type_info &t) : source(&s), target(&t) { } 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 struct stream_char { typedef char type; }; #ifndef DISABLE_WIDE_CHAR_SUPPORT template<> struct stream_char { typedef wchar_t type; }; template<> struct stream_char { typedef wchar_t type; }; template<> struct stream_char { typedef wchar_t type; }; template<> struct stream_char { typedef wchar_t type; }; #endif template struct widest_char { typedef TargetChar type; }; template<> struct widest_char { typedef wchar_t type; }; } namespace detail // stream wrapper for handling lexical conversions. { template class lexical_stream { public: lexical_stream() { stream.unsetf(std::ios::skipws); // was: /*if(std::numeric_limits::is_specialized) stream.precision(std::numeric_limits::digits10 +1 ); else if(std::numeric_limits::is_specialized) stream.precision(std::numeric_limits::digits10 +1 );*/ // New calculation to give more accurate number of significant decimal digits: // 32 significand bits digits10 = 6 significant_digits10 = 9 // 53 significand bits digits10 = 15 significant_digits10 = 17 // 64 significand bits digits10 = 18 significant_digits10 = 21 // 106 significand bits digits10 = 31 significant_digits10 = 33 // 113 significand bits digits10 = 33 significant_digits10 = 36 // 128 significand bits digits10 = 38 significant_digits10 = 40 if(std::numeric_limits::is_specialized) stream.precision(2 + std::numeric_limits::digits * 301/1000); else if(std::numeric_limits::is_specialized) stream.precision(2 + std::numeric_limits::digits * 301/1000); } ~lexical_stream() { #if defined(BOOST_NO_STRINGSTREAM) stream.freeze(false); #endif } bool operator<<(const Source &input) { return !(stream << input).fail(); } template bool operator>>(InputStreamable &output) { return !is_pointer::value && stream >> output && (stream >> std::ws).eof(); } bool operator>>(std::string &output) { #if defined(BOOST_NO_STRINGSTREAM) stream << '\0'; #endif output = stream.str(); return true; } #ifndef DISABLE_WIDE_CHAR_SUPPORT bool operator>>(std::wstring &output) { output = stream.str(); return true; } #endif private: typedef typename widest_char< typename stream_char::type, typename stream_char::type>::type char_type; #if defined(BOOST_NO_STRINGSTREAM) std::strstream stream; #elif defined(BOOST_NO_STD_LOCALE) std::stringstream stream; #else std::basic_stringstream stream; #endif }; } template Target lexical_cast(Source arg) { detail::lexical_stream interpreter; Target result; if(!((interpreter << arg) && (interpreter >> result))) { throw_exception(bad_lexical_cast(typeid(Target), typeid(Source))); } return result; // warning 4701 may not be initialised - OK to ignore. } } // Copyright Kevlin Henney, 2000-2003. All rights reserved. // // Permission to use, copy, modify, and distribute this software for any // purpose is hereby granted without fee, provided that this copyright and // permissions notice appear in all copies and derivatives. // // This software is provided "as is" without express or implied warranty. #undef DISABLE_WIDE_CHAR_SUPPORT #endif