1 #ifndef BOOST_LEXICAL_CAST_INCLUDED
2 #define BOOST_LEXICAL_CAST_INCLUDED
4 // Boost lexical_cast.hpp header -------------------------------------------//
6 // See http://www.boost.org for most recent version including documentation.
7 // See end of this header for rights and permissions.
9 // what: lexical_cast custom keyword cast
10 // who: contributed by Kevlin Henney,
11 // enhanced with contributions from Terje Slettebø,
12 // with additional fixes and suggestions from Gennaro Prota,
13 // Beman Dawes, Dave Abrahams, Daryle Walker, Peter Dimov,
15 // when: November 2000, March 2003
19 #include <boost/config.hpp>
20 #include <boost/limits.hpp>
21 #include <boost/type_traits/is_pointer.hpp>
23 #ifdef BOOST_NO_STRINGSTREAM
29 #if defined(BOOST_NO_STRINGSTREAM) || \
30 defined(BOOST_NO_STD_WSTRING) || \
31 defined(BOOST_NO_STD_LOCALE) || \
32 defined(BOOST_NO_CWCHAR) || \
33 defined(BOOST_MSVC) && (BOOST_MSVC <= 1200)
34 #define DISABLE_WIDE_CHAR_SUPPORT
37 #ifdef BOOST_NO_INTRINSIC_WCHAR_T
43 // exception used to indicate runtime lexical_cast failure
44 class bad_lexical_cast : public std::bad_cast
47 virtual ~bad_lexical_cast() throw()
52 namespace detail // actual underlying concrete exception type
54 template<typename Target, typename Source>
55 class no_lexical_conversion : public bad_lexical_cast
58 no_lexical_conversion()
60 std::string() + "bad lexical cast: " +
61 "source type value could not be interpreted as target, Target=" +
62 typeid(Target).name() + ", Source=" + typeid(Source).name())
65 virtual ~no_lexical_conversion() throw()
68 virtual const char *what() const throw()
70 return description.c_str();
73 const std::string description; // static initialization fails on MSVC6
77 namespace detail // selectors for choosing stream character type
79 template<typename Type>
85 #ifndef DISABLE_WIDE_CHAR_SUPPORT
87 struct stream_char<wchar_t>
93 struct stream_char<wchar_t *>
99 struct stream_char<const wchar_t *>
101 typedef wchar_t type;
105 struct stream_char<std::wstring>
107 typedef wchar_t type;
111 template<typename TargetChar, typename SourceChar>
114 typedef TargetChar type;
118 struct widest_char<char, wchar_t>
120 typedef wchar_t type;
124 namespace detail // stream wrapper for handling lexical conversions
126 template<typename Target, typename Source>
132 stream.unsetf(std::ios::skipws);
134 if(std::numeric_limits<Target>::is_specialized)
135 stream.precision(std::numeric_limits<Target>::digits10 + 1);
136 else if(std::numeric_limits<Source>::is_specialized)
137 stream.precision(std::numeric_limits<Source>::digits10 + 1);
141 #if defined(BOOST_NO_STRINGSTREAM)
142 stream.freeze(false);
145 bool operator<<(const Source &input)
147 return stream << input;
149 template<typename InputStreamable>
150 bool operator>>(InputStreamable &output)
152 return !is_pointer<InputStreamable>::value &&
154 (stream >> std::ws).eof();
156 bool operator>>(std::string &output)
158 #if defined(BOOST_NO_STRINGSTREAM)
161 output = stream.str();
164 #ifndef DISABLE_WIDE_CHAR_SUPPORT
165 bool operator>>(std::wstring &output)
167 output = stream.str();
172 typedef typename widest_char<
173 typename stream_char<Target>::type,
174 typename stream_char<Source>::type>::type char_type;
176 #if defined(BOOST_NO_STRINGSTREAM)
177 std::strstream stream;
178 #elif defined(BOOST_NO_STD_LOCALE)
179 std::stringstream stream;
181 std::basic_stringstream<char_type> stream;
186 template<typename Target, typename Source>
187 Target lexical_cast(Source arg)
189 detail::lexical_stream<Target, Source> interpreter;
192 if(!(interpreter << arg && interpreter >> result))
193 throw detail::no_lexical_conversion<Target, Source>();
198 // Copyright Kevlin Henney, 2000-2003. All rights reserved.
200 // Permission to use, copy, modify, and distribute this software for any
201 // purpose is hereby granted without fee, provided that this copyright and
202 // permissions notice appear in all copies and derivatives.
204 // This software is provided "as is" without express or implied warranty.
206 #undef DISABLE_WIDE_CHAR_SUPPORT