]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Copyright Kevlin Henney, 2000-2005. |
2 | // Copyright Alexander Nasonov, 2006-2010. | |
3 | // Copyright Antony Polukhin, 2011-2014. | |
4 | // | |
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) | |
8 | // | |
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 | |
17 | ||
18 | #ifndef BOOST_LEXICAL_CAST_LEXICAL_CAST_OLD_HPP | |
19 | #define BOOST_LEXICAL_CAST_LEXICAL_CAST_OLD_HPP | |
20 | ||
21 | #include <boost/config.hpp> | |
22 | #ifdef BOOST_HAS_PRAGMA_ONCE | |
23 | # pragma once | |
24 | #endif | |
25 | ||
26 | #if defined(BOOST_NO_STRINGSTREAM) || defined(BOOST_NO_STD_WSTRING) | |
27 | #define BOOST_LCAST_NO_WCHAR_T | |
28 | #endif | |
29 | ||
30 | #include <climits> | |
31 | #include <cstddef> | |
32 | #include <string> | |
33 | #include <cstring> | |
34 | #include <cstdio> | |
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> | |
41 | ||
42 | #ifdef BOOST_NO_STRINGSTREAM | |
43 | #include <strstream> | |
44 | #else | |
45 | #include <sstream> | |
46 | #endif | |
47 | ||
48 | #include <boost/lexical_cast/bad_lexical_cast.hpp> | |
49 | #include <boost/lexical_cast/detail/widest_char.hpp> | |
50 | ||
51 | namespace boost { | |
52 | namespace detail | |
53 | { | |
54 | ||
55 | // selectors for choosing stream character type | |
56 | template<typename Type> | |
57 | struct stream_char | |
58 | { | |
59 | typedef char type; | |
60 | }; | |
61 | ||
62 | #ifndef BOOST_LCAST_NO_WCHAR_T | |
63 | #ifndef BOOST_NO_INTRINSIC_WCHAR_T | |
64 | template<> | |
65 | struct stream_char<wchar_t> | |
66 | { | |
67 | typedef wchar_t type; | |
68 | }; | |
69 | #endif | |
70 | ||
71 | template<> | |
72 | struct stream_char<wchar_t *> | |
73 | { | |
74 | typedef wchar_t type; | |
75 | }; | |
76 | ||
77 | template<> | |
78 | struct stream_char<const wchar_t *> | |
79 | { | |
80 | typedef wchar_t type; | |
81 | }; | |
82 | ||
83 | template<> | |
84 | struct stream_char<std::wstring> | |
85 | { | |
86 | typedef wchar_t type; | |
87 | }; | |
88 | #endif | |
89 | ||
90 | // stream wrapper for handling lexical conversions | |
91 | template<typename Target, typename Source, typename Traits> | |
92 | class lexical_stream | |
93 | { | |
94 | private: | |
95 | typedef typename widest_char< | |
96 | typename stream_char<Target>::type, | |
97 | typename stream_char<Source>::type>::type char_type; | |
98 | ||
99 | typedef Traits traits_type; | |
100 | ||
101 | public: | |
102 | lexical_stream(char_type* = 0, char_type* = 0) | |
103 | { | |
104 | stream.unsetf(std::ios::skipws); | |
105 | lcast_set_precision(stream, static_cast<Source*>(0), static_cast<Target*>(0) ); | |
106 | } | |
107 | ~lexical_stream() | |
108 | { | |
109 | #if defined(BOOST_NO_STRINGSTREAM) | |
110 | stream.freeze(false); | |
111 | #endif | |
112 | } | |
113 | bool operator<<(const Source &input) | |
114 | { | |
115 | return !(stream << input).fail(); | |
116 | } | |
117 | template<typename InputStreamable> | |
118 | bool operator>>(InputStreamable &output) | |
119 | { | |
120 | return !is_pointer<InputStreamable>::value && | |
121 | stream >> output && | |
122 | stream.get() == traits_type::eof(); | |
123 | } | |
124 | ||
125 | bool operator>>(std::string &output) | |
126 | { | |
127 | #if defined(BOOST_NO_STRINGSTREAM) | |
128 | stream << '\0'; | |
129 | #endif | |
130 | stream.str().swap(output); | |
131 | return true; | |
132 | } | |
133 | #ifndef BOOST_LCAST_NO_WCHAR_T | |
134 | bool operator>>(std::wstring &output) | |
135 | { | |
136 | stream.str().swap(output); | |
137 | return true; | |
138 | } | |
139 | #endif | |
140 | ||
141 | private: | |
142 | #if defined(BOOST_NO_STRINGSTREAM) | |
143 | std::strstream stream; | |
144 | #elif defined(BOOST_NO_STD_LOCALE) | |
145 | std::stringstream stream; | |
146 | #else | |
147 | std::basic_stringstream<char_type,traits_type> stream; | |
148 | #endif | |
149 | }; | |
150 | } | |
151 | ||
152 | // call-by-value fallback version (deprecated) | |
153 | ||
154 | template<typename Target, typename Source> | |
155 | Target lexical_cast(Source arg) | |
156 | { | |
157 | typedef typename detail::widest_char< | |
158 | BOOST_DEDUCED_TYPENAME detail::stream_char<Target>::type | |
159 | , BOOST_DEDUCED_TYPENAME detail::stream_char<Source>::type | |
160 | >::type char_type; | |
161 | ||
162 | typedef std::char_traits<char_type> traits; | |
163 | detail::lexical_stream<Target, Source, traits> interpreter; | |
164 | Target result; | |
165 | ||
166 | if(!(interpreter << arg && interpreter >> result)) | |
167 | boost::conversion::detail::throw_bad_cast<Source, Target>(); | |
168 | return result; | |
169 | } | |
170 | ||
171 | } // namespace boost | |
172 | ||
173 | #undef BOOST_LCAST_NO_WCHAR_T | |
174 | ||
175 | #endif // BOOST_LEXICAL_CAST_LEXICAL_CAST_OLD_HPP | |
176 |