// boost lexical_cast.hpp header -------------------------------------------// // See http://www.boost.org for most recent version including documentation. #ifndef BOOST_LEXICAL_CAST_INCLUDED #define BOOST_LEXICAL_CAST_INCLUDED // what: lexical_cast custom keyword cast // who: contributed by Kevlin Henney, with alternative naming, behaviors // and fixes contributed by Dave Abrahams, Daryle Walker and other // Boosters on the list // when: November 2000 // where: tested with MSVC 6.0, BCC 5.5, and g++ 2.91 #include #include // Some sstream implementations are broken for the purposes of lexical cast. # if defined(BOOST_NO_STRINGSTREAM) # define BOOST_LEXICAL_CAST_USE_STRSTREAM # endif #ifdef BOOST_LEXICAL_CAST_USE_STRSTREAM # include #else # include #endif #include namespace boost { // exception used to indicate runtime lexical_cast failure class bad_lexical_cast : public std::bad_cast { public: // constructors, destructors, and assignment operator defaulted // function inlined for brevity and consistency with rest of library virtual const char * what() const throw() { return "bad lexical cast: " "source type value could not be interpreted as target"; } }; namespace detail { template inline # ifdef BOOST_LEXICAL_CAST_USE_STRSTREAM void lexical_cast_getvalue(std::strstream &stream, Target &result) # else void lexical_cast_getvalue(std::stringstream &stream, Target &result) # endif { stream.unsetf(std::ios::skipws); if(!(stream >> result) || !(stream >> std::ws).eof()) throw bad_lexical_cast(); } template<> inline # ifdef BOOST_LEXICAL_CAST_USE_STRSTREAM void lexical_cast_getvalue(std::strstream &stream, std::string &result) { result = std::string(stream.buf()); } # else void lexical_cast_getvalue(std::stringstream &stream, std::string &result) { result = stream.str(); } # endif } template Target lexical_cast(Source arg) { # ifdef BOOST_LEXICAL_CAST_USE_STRSTREAM std::strstream interpreter; // for out-of-the-box g++ 2.95.2 # else std::stringstream interpreter; # endif if (boost::is_same::value) { // this code will never be reached if casting is needed // the ugly casts are just for compilation return *reinterpret_cast( reinterpret_cast(&arg)); } Target result; if (!(interpreter << arg)) throw bad_lexical_cast(); detail::lexical_cast_getvalue(interpreter, result); return result; } } // Copyright Kevlin Henney, 2000, 2001, 2002. 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. #ifdef BOOST_LEXICAL_CAST_USE_STRSTREAM # undef BOOST_LEXICAL_CAST_USE_STRSTREAM #endif #endif