Changeset 2270
- Timestamp:
- Jun 14, 2010, 6:05:59 AM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/yat/utility/utility.h
r2248 r2270 8 8 Copyright (C) 2006 Jari Häkkinen 9 9 Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson 10 Copyright (C) 2009 Peter Johansson10 Copyright (C) 2009, 2010 Peter Johansson 11 11 12 12 This file is part of the yat library, http://dev.thep.lu.se/yat … … 183 183 // private namespace 184 184 namespace detail { 185 186 /** 187 \brief convert s to t 188 189 used in function is<T> and convert<T> 190 191 \return true if conversion was successful 192 193 \internal 194 */ 195 template<typename T> 196 bool convert(const std::string& s, T& t); 197 185 198 /** 186 199 Functor used in load function … … 249 262 T convert(const std::string& s) 250 263 { 251 if (is_nan(s)) 252 return std::numeric_limits<T>::quiet_NaN(); 253 if (is_equal(s, "inf")) 254 return std::numeric_limits<T>::infinity(); 255 if (is_equal(s, "-inf")) { 256 if (std::numeric_limits<T>::is_signed) 257 return -std::numeric_limits<T>::infinity(); 258 else 259 throw runtime_error(std::string("yat::utility::convert(\"")+s+ 260 std::string("\"): type is unsigned") ); 261 } 262 std::stringstream ss(s); 263 T a; 264 ss >> a; 265 bool ok = true; 266 if(ss.fail()) 267 ok = false; 268 // Check that nothing is left on stream 269 std::string b; 270 ss >> b; 271 if (!b.empty() || !ok) 264 T result; 265 if (!detail::convert(s, result)) 272 266 throw runtime_error(std::string("yat::utility::convert(\"")+s+ 273 267 std::string("\")")); 274 return a; 275 } 268 return result; 269 } 270 276 271 277 272 template<typename T> 278 273 bool is(const std::string& s) 279 274 { 280 if (is_nan(s)) 281 return std::numeric_limits<T>::has_quiet_NaN; 282 if (is_equal(s, "inf")) 283 return std::numeric_limits<T>::has_infinity; 284 if (is_equal(s, "-inf")) 285 return std::numeric_limits<T>::has_infinity && 286 std::numeric_limits<T>::is_signed; 287 std::stringstream ss(s); 288 T a; 289 ss >> a; 290 if(ss.fail()) 291 return false; 292 // Check that nothing is left on stream 293 std::string b; 294 ss >> b; 295 return b.empty(); 296 } 275 T tmp; 276 return detail::convert(s, tmp); 277 } 278 297 279 298 280 template<typename T> … … 347 329 } 348 330 331 332 namespace detail { 333 template<typename T> 334 bool convert(const std::string& s, T& result) 335 { 336 std::istringstream ss(s); 337 ss >> result; 338 if (ss.fail()) { 339 if (is_nan(s)) { 340 result = std::numeric_limits<T>::quiet_NaN(); 341 return true; 342 } 343 if (is_equal(s, "inf")) { 344 result = std::numeric_limits<T>::infinity(); 345 return true; 346 } 347 if (std::numeric_limits<T>::is_signed && is_equal(s, "-inf")) { 348 result = -std::numeric_limits<T>::infinity(); 349 return true; 350 } 351 return false; 352 } 353 // Check that nothing is left on stream 354 std::string b; 355 ss >> b; 356 return b.empty(); 357 } 358 } // of namespace detail 359 349 360 }}} // of namespace utility, yat, and theplu 350 361
Note: See TracChangeset
for help on using the changeset viewer.