2323#ifndef __STR_CONVERT_H__
2424#define __STR_CONVERT_H__
2525
26- #include < string>
26+ #include < import/except.h>
27+ #include < cerrno>
2728#include < complex>
29+ #include < cstdlib>
30+ #include < iomanip>
31+ #include < iostream>
2832#include < limits>
2933#include < ostream>
3034#include < sstream>
31- #include < iomanip >
35+ #include < string >
3236#include < typeinfo>
33- #include < iostream>
34- #include < cstdlib>
35- #include < cerrno>
36- #include < limits>
37- #include < import/except.h>
3837
3938namespace str
4039{
40+ template <typename T>
41+ int getPrecision (const T& type);
4142
42- template <typename T> int getPrecision ( const T& type);
43- template < typename T> int getPrecision (const std::complex <T>& type);
43+ template <typename T>
44+ int getPrecision (const std::complex <T>& type);
4445
45- template <typename T> std::string toString (const T& value)
46+ template <typename T>
47+ std::string toString (const T& value)
4648{
4749 std::ostringstream buf;
4850 buf.precision (getPrecision (value));
4951 buf << std::boolalpha << value;
5052 return buf.str ();
5153}
5254
53- template <> std::string toString (const uint8_t & value);
55+ template <>
56+ std::string toString (const uint8_t & value);
5457
55- template <> std::string toString (const int8_t & value);
58+ template <>
59+ std::string toString (const int8_t & value);
5660
57- template <typename T> std::string toString (const T& real, const T& imag)
61+ template <typename T>
62+ std::string toString (const T& real, const T& imag)
5863{
5964 return toString (std::complex <T>(real, imag));
6065}
6166
62- template <typename T> T toType (const std::string& s)
67+ template <typename T>
68+ T toType (const std::string& s)
6369{
6470 if (s.empty ())
65- throw except::BadCastException (except::Context (__FILE__, __LINE__,
66- std::string (" " ), std::string (" " ), std::string (" Empty string" )));
71+ throw except::BadCastException (
72+ except::Context (__FILE__,
73+ __LINE__,
74+ std::string (" " ),
75+ std::string (" " ),
76+ std::string (" Empty string" )));
6777
6878 T value;
6979
@@ -73,26 +83,32 @@ template<typename T> T toType(const std::string& s)
7383
7484 if (buf.fail ())
7585 {
76- throw except::BadCastException (except::Context (__FILE__, __LINE__,
77- std::string (" " ), std::string (" " ),
78- std::string (" Conversion failed: '" )
79- + s + std::string (" ' -> " ) + typeid (T).name ()));
86+ throw except::BadCastException (
87+ except::Context (__FILE__,
88+ __LINE__,
89+ std::string (" " ),
90+ std::string (" " ),
91+ std::string (" Conversion failed: '" ) + s +
92+ std::string (" ' -> " ) +
93+ typeid (T).name ()));
8094 }
8195
8296 return value;
8397}
8498
85- template <> bool toType<bool > (const std::string& s);
86- template <> std::string toType<std::string> (const std::string& s);
99+ template <>
100+ bool toType<bool >(const std::string& s);
101+ template <>
102+ std::string toType<std::string>(const std::string& s);
87103
88104/* *
89105 * strtoll wrapper for msvc compatibility.
90106 */
91- long long strtoll (const char * str, char ** endptr, int base);
107+ long long strtoll (const char * str, char ** endptr, int base);
92108/* *
93109 * strtoull wrapper for msvc compatibility.
94110 */
95- unsigned long long strtoull (const char * str, char ** endptr, int base);
111+ unsigned long long strtoull (const char * str, char ** endptr, int base);
96112
97113/* *
98114 * Convert a string containing a number in any base to a numerical type.
@@ -102,7 +118,8 @@ unsigned long long strtoull(const char *str, char **endptr, int base);
102118 * @return a numberical representation of the number
103119 * @throw BadCastException thrown if cast cannot be performed.
104120 */
105- template <typename T> T toType (const std::string& s, int base)
121+ template <typename T>
122+ T toType (const std::string& s, int base)
106123{
107124 char * end;
108125 errno = 0 ;
@@ -113,8 +130,8 @@ template<typename T> T toType(const std::string& s, int base)
113130 if (std::numeric_limits<T>::is_signed)
114131 {
115132 const long long longRes = str::strtoll (str, &end, base);
116- if (longRes < std::numeric_limits<T>::min () ||
117- longRes > std::numeric_limits<T>::max ())
133+ if (longRes < static_cast < long long >( std::numeric_limits<T>::min () ) ||
134+ longRes > static_cast < long long >( std::numeric_limits<T>::max () ))
118135 {
119136 overflow = true ;
120137 }
@@ -123,25 +140,36 @@ template<typename T> T toType(const std::string& s, int base)
123140 else
124141 {
125142 const unsigned long long longRes = str::strtoull (str, &end, base);
126- if (longRes < std::numeric_limits<T>::min () ||
127- longRes > std::numeric_limits<T>::max ())
143+ if (longRes < static_cast <unsigned long long >(
144+ std::numeric_limits<T>::min ()) ||
145+ longRes > static_cast <unsigned long long >(
146+ std::numeric_limits<T>::max ()))
128147 {
129148 overflow = true ;
130149 }
131150 res = static_cast <T>(longRes);
132151 }
133152
134153 if (overflow || errno == ERANGE)
135- throw except::BadCastException (except::Context (__FILE__, __LINE__,
136- std::string (" " ), std::string (" " ),
137- std::string (" Overflow: '" )
138- + s + std::string (" ' -> " ) + typeid (T).name ()));
139- // If the end pointer is at the start of the string, we didn't convert anything.
154+ throw except::BadCastException (
155+ except::Context (__FILE__,
156+ __LINE__,
157+ std::string (" " ),
158+ std::string (" " ),
159+ std::string (" Overflow: '" ) + s +
160+ std::string (" ' -> " ) +
161+ typeid (T).name ()));
162+ // If the end pointer is at the start of the string, we didn't convert
163+ // anything.
140164 else if (end == str)
141- throw except::BadCastException (except::Context (__FILE__, __LINE__,
142- std::string (" " ), std::string (" " ),
143- std::string (" Conversion failed: '" )
144- + s + std::string (" ' -> " ) + typeid (T).name ()));
165+ throw except::BadCastException (
166+ except::Context (__FILE__,
167+ __LINE__,
168+ std::string (" " ),
169+ std::string (" " ),
170+ std::string (" Conversion failed: '" ) + s +
171+ std::string (" ' -> " ) +
172+ typeid (T).name ()));
145173
146174 return res;
147175}
@@ -153,18 +181,26 @@ template<typename T> T toType(const std::string& s, int base)
153181 * @return The integer argument required by ios::precision() to represent
154182 * this type.
155183 */
156- template <typename T> int getPrecision (const T& )
184+ template <typename T>
185+ int getPrecision (const T&)
157186{
158187 return 0 ;
159188}
160- template <typename T> int getPrecision (const std::complex <T>& type)
189+
190+ template <typename T>
191+ int getPrecision (const std::complex <T>& type)
161192{
162193 return getPrecision (type.real ());
163194}
164195
165- template <> int getPrecision (const float & type);
166- template <> int getPrecision (const double & type);
167- template <> int getPrecision (const long double & type);
196+ template <>
197+ int getPrecision (const float & type);
198+
199+ template <>
200+ int getPrecision (const double & type);
201+
202+ template <>
203+ int getPrecision (const long double & type);
168204
169205/* * Generic casting routine; used by explicitly overloaded
170206 conversion operators.
@@ -174,12 +210,11 @@ template<> int getPrecision(const long double& type);
174210 to the desired type, if possible.
175211 @throw BadCastException thrown if cast cannot be performed.
176212 */
177- template <typename T>
213+ template <typename T>
178214T generic_cast (const std::string& value)
179215{
180216 return str::toType<T>(value);
181217}
182-
183218}
184219
185220#endif
0 commit comments