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/throw_exception.hpp>
22 #include <boost/type_traits/is_pointer.hpp>
24 #ifdef BOOST_NO_STRINGSTREAM
30 #if defined(BOOST_NO_STRINGSTREAM) || \
31 defined(BOOST_NO_STD_WSTRING) || \
32 defined(BOOST_NO_STD_LOCALE) || \
33 defined(BOOST_NO_INTRINSIC_WCHAR_T)
34 #define DISABLE_WIDE_CHAR_SUPPORT
39 // exception used to indicate runtime lexical_cast failure
40 class bad_lexical_cast : public std::bad_cast
44 source(&typeid(void)), target(&typeid(void))
48 const std::type_info &s,
49 const std::type_info &t) :
50 source(&s), target(&t)
53 const std::type_info &source_type() const
57 const std::type_info &target_type() const
61 virtual const char *what() const throw()
63 return "bad lexical cast: "
64 "source type value could not be interpreted as target";
66 virtual ~bad_lexical_cast() throw()
70 const std::type_info *source;
71 const std::type_info *target;
74 namespace detail // selectors for choosing stream character type
76 template<typename Type>
82 #ifndef DISABLE_WIDE_CHAR_SUPPORT
84 struct stream_char<wchar_t>
90 struct stream_char<wchar_t *>
96 struct stream_char<const wchar_t *>
102 struct stream_char<std::wstring>
104 typedef wchar_t type;
108 template<typename TargetChar, typename SourceChar>
111 typedef TargetChar type;
115 struct widest_char<char, wchar_t>
117 typedef wchar_t type;
121 namespace detail // stream wrapper for handling lexical conversions
123 template<typename Target, typename Source>
129 stream.unsetf(std::ios::skipws);
131 if(std::numeric_limits<Target>::is_specialized)
132 stream.precision(std::numeric_limits<Target>::digits10 + 1);
133 else if(std::numeric_limits<Source>::is_specialized)
134 stream.precision(std::numeric_limits<Source>::digits10 + 1);
138 #if defined(BOOST_NO_STRINGSTREAM)
139 stream.freeze(false);
142 bool operator<<(const Source &input)
144 return !(stream << input).fail();
146 template<typename InputStreamable>
147 bool operator>>(InputStreamable &output)
149 return !is_pointer<InputStreamable>::value &&
151 (stream >> std::ws).eof();
153 bool operator>>(std::string &output)
155 #if defined(BOOST_NO_STRINGSTREAM)
158 output = stream.str();
161 #ifndef DISABLE_WIDE_CHAR_SUPPORT
162 bool operator>>(std::wstring &output)
164 output = stream.str();
169 typedef typename widest_char<
170 typename stream_char<Target>::type,
171 typename stream_char<Source>::type>::type char_type;
173 #if defined(BOOST_NO_STRINGSTREAM)
174 std::strstream stream;
175 #elif defined(BOOST_NO_STD_LOCALE)
176 std::stringstream stream;
178 std::basic_stringstream<char_type> stream;
183 template<typename Target, typename Source>
184 Target lexical_cast(Source arg)
186 detail::lexical_stream<Target, Source> interpreter;
189 if(!(interpreter << arg && interpreter >> result))
190 throw_exception(bad_lexical_cast(typeid(Target), typeid(Source)));
195 // Copyright Kevlin Henney, 2000-2003. All rights reserved.
197 // Distributed under the Boost Software License, Version 1.0. (See
198 // accompanying file LICENSE_1_0.txt or copy at
199 // http://www.boost.org/LICENSE_1_0.txt)
201 #undef DISABLE_WIDE_CHAR_SUPPORT