1 // Copyright Kevlin Henney, 2000-2005.
2 // Copyright Alexander Nasonov, 2006-2010.
3 // Copyright Antony Polukhin, 2011-2014.
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 // what: lexical_cast custom keyword cast
10 // who: contributed by Kevlin Henney,
11 // enhanced with contributions from Terje Slettebo,
12 // with additional fixes and suggestions from Gennaro Prota,
13 // Beman Dawes, Dave Abrahams, Daryle Walker, Peter Dimov,
14 // Alexander Nasonov, Antony Polukhin, Justin Viiret, Michael Hofmann,
15 // Cheng Yang, Matthew Bradbury, David W. Birdsall, Pavel Korzh and other Boosters
16 // when: November 2000, March 2003, June 2005, June 2006, March 2011 - 2014
18 #ifndef BOOST_LEXICAL_CAST_LEXICAL_CAST_OLD_HPP
19 #define BOOST_LEXICAL_CAST_LEXICAL_CAST_OLD_HPP
21 #include <boost/config.hpp>
22 #ifdef BOOST_HAS_PRAGMA_ONCE
26 #if defined(BOOST_NO_STRINGSTREAM) || defined(BOOST_NO_STD_WSTRING)
27 #define BOOST_LCAST_NO_WCHAR_T
35 #include <boost/limits.hpp>
36 #include <boost/mpl/if.hpp>
37 #include <boost/type_traits/is_pointer.hpp>
38 #include <boost/static_assert.hpp>
39 #include <boost/detail/lcast_precision.hpp>
40 #include <boost/detail/workaround.hpp>
42 #ifdef BOOST_NO_STRINGSTREAM
48 #include <boost/lexical_cast/bad_lexical_cast.hpp>
49 #include <boost/lexical_cast/detail/widest_char.hpp>
55 // selectors for choosing stream character type
56 template<typename Type>
62 #ifndef BOOST_LCAST_NO_WCHAR_T
63 #ifndef BOOST_NO_INTRINSIC_WCHAR_T
65 struct stream_char<wchar_t>
72 struct stream_char<wchar_t *>
78 struct stream_char<const wchar_t *>
84 struct stream_char<std::wstring>
90 // stream wrapper for handling lexical conversions
91 template<typename Target, typename Source, typename Traits>
95 typedef typename widest_char<
96 typename stream_char<Target>::type,
97 typename stream_char<Source>::type>::type char_type;
99 typedef Traits traits_type;
102 lexical_stream(char_type* = 0, char_type* = 0)
104 stream.unsetf(std::ios::skipws);
105 lcast_set_precision(stream, static_cast<Source*>(0), static_cast<Target*>(0) );
109 #if defined(BOOST_NO_STRINGSTREAM)
110 stream.freeze(false);
113 bool operator<<(const Source &input)
115 return !(stream << input).fail();
117 template<typename InputStreamable>
118 bool operator>>(InputStreamable &output)
120 return !is_pointer<InputStreamable>::value &&
122 stream.get() == traits_type::eof();
125 bool operator>>(std::string &output)
127 #if defined(BOOST_NO_STRINGSTREAM)
130 stream.str().swap(output);
133 #ifndef BOOST_LCAST_NO_WCHAR_T
134 bool operator>>(std::wstring &output)
136 stream.str().swap(output);
142 #if defined(BOOST_NO_STRINGSTREAM)
143 std::strstream stream;
144 #elif defined(BOOST_NO_STD_LOCALE)
145 std::stringstream stream;
147 std::basic_stringstream<char_type,traits_type> stream;
152 // call-by-value fallback version (deprecated)
154 template<typename Target, typename Source>
155 Target lexical_cast(Source arg)
157 typedef typename detail::widest_char<
158 BOOST_DEDUCED_TYPENAME detail::stream_char<Target>::type
159 , BOOST_DEDUCED_TYPENAME detail::stream_char<Source>::type
162 typedef std::char_traits<char_type> traits;
163 detail::lexical_stream<Target, Source, traits> interpreter;
166 if(!(interpreter << arg && interpreter >> result))
167 boost::conversion::detail::throw_bad_cast<Source, Target>();
173 #undef BOOST_LCAST_NO_WCHAR_T
175 #endif // BOOST_LEXICAL_CAST_LEXICAL_CAST_OLD_HPP