File indexing completed on 2025-01-30 09:44:54
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 #ifndef BOOST_LEXICAL_CAST_LEXICAL_CAST_OLD_HPP
0019 #define BOOST_LEXICAL_CAST_LEXICAL_CAST_OLD_HPP
0020
0021 #include <boost/config.hpp>
0022 #ifdef BOOST_HAS_PRAGMA_ONCE
0023 # pragma once
0024 #endif
0025
0026 #if defined(BOOST_NO_STRINGSTREAM) || defined(BOOST_NO_STD_WSTRING)
0027 #define BOOST_LCAST_NO_WCHAR_T
0028 #endif
0029
0030 #include <climits>
0031 #include <cstddef>
0032 #include <string>
0033 #include <cstring>
0034 #include <cstdio>
0035 #include <boost/limits.hpp>
0036 #include <boost/type_traits/is_pointer.hpp>
0037 #include <boost/detail/lcast_precision.hpp>
0038 #include <boost/detail/workaround.hpp>
0039
0040 #ifdef BOOST_NO_STRINGSTREAM
0041 #include <strstream>
0042 #else
0043 #include <sstream>
0044 #endif
0045
0046 #include <boost/lexical_cast/bad_lexical_cast.hpp>
0047 #include <boost/lexical_cast/detail/widest_char.hpp>
0048
0049 namespace boost {
0050 namespace detail
0051 {
0052
0053
0054 template<typename Type>
0055 struct stream_char
0056 {
0057 typedef char type;
0058 };
0059
0060 #ifndef BOOST_LCAST_NO_WCHAR_T
0061 #ifndef BOOST_NO_INTRINSIC_WCHAR_T
0062 template<>
0063 struct stream_char<wchar_t>
0064 {
0065 typedef wchar_t type;
0066 };
0067 #endif
0068
0069 template<>
0070 struct stream_char<wchar_t *>
0071 {
0072 typedef wchar_t type;
0073 };
0074
0075 template<>
0076 struct stream_char<const wchar_t *>
0077 {
0078 typedef wchar_t type;
0079 };
0080
0081 template<>
0082 struct stream_char<std::wstring>
0083 {
0084 typedef wchar_t type;
0085 };
0086 #endif
0087
0088
0089 template<typename Target, typename Source, typename Traits>
0090 class lexical_stream
0091 {
0092 private:
0093 typedef typename widest_char<
0094 typename stream_char<Target>::type,
0095 typename stream_char<Source>::type>::type char_type;
0096
0097 typedef Traits traits_type;
0098
0099 public:
0100 lexical_stream(char_type* = 0, char_type* = 0)
0101 {
0102 stream.unsetf(std::ios::skipws);
0103 lcast_set_precision(stream, static_cast<Source*>(0), static_cast<Target*>(0) );
0104 }
0105 ~lexical_stream()
0106 {
0107 #if defined(BOOST_NO_STRINGSTREAM)
0108 stream.freeze(false);
0109 #endif
0110 }
0111 bool operator<<(const Source &input)
0112 {
0113 return !(stream << input).fail();
0114 }
0115 template<typename InputStreamable>
0116 bool operator>>(InputStreamable &output)
0117 {
0118 return !is_pointer<InputStreamable>::value &&
0119 stream >> output &&
0120 stream.get() == traits_type::eof();
0121 }
0122
0123 bool operator>>(std::string &output)
0124 {
0125 #if defined(BOOST_NO_STRINGSTREAM)
0126 stream << '\0';
0127 #endif
0128 stream.str().swap(output);
0129 return true;
0130 }
0131 #ifndef BOOST_LCAST_NO_WCHAR_T
0132 bool operator>>(std::wstring &output)
0133 {
0134 stream.str().swap(output);
0135 return true;
0136 }
0137 #endif
0138
0139 private:
0140 #if defined(BOOST_NO_STRINGSTREAM)
0141 std::strstream stream;
0142 #elif defined(BOOST_NO_STD_LOCALE)
0143 std::stringstream stream;
0144 #else
0145 std::basic_stringstream<char_type,traits_type> stream;
0146 #endif
0147 };
0148 }
0149
0150
0151
0152 template<typename Target, typename Source>
0153 Target lexical_cast(Source arg)
0154 {
0155 typedef typename detail::widest_char<
0156 typename detail::stream_char<Target>::type
0157 , typename detail::stream_char<Source>::type
0158 >::type char_type;
0159
0160 typedef std::char_traits<char_type> traits;
0161 detail::lexical_stream<Target, Source, traits> interpreter;
0162 Target result;
0163
0164 if(!(interpreter << arg && interpreter >> result))
0165 boost::conversion::detail::throw_bad_cast<Source, Target>();
0166 return result;
0167 }
0168
0169 }
0170
0171 #undef BOOST_LCAST_NO_WCHAR_T
0172
0173 #endif
0174