123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- #ifndef CPPDB_NUMERIC_UTIL_H
- #define CPPDB_NUMERIC_UTIL_H
- #include <cppdb/errors.h>
- #include <string>
- #include <sstream>
- #include <limits>
- #include <iomanip>
- namespace cppdb {
-
-
-
-
-
-
- template<typename T>
- T parse_number(std::string const &s,std::istringstream &ss)
- {
- ss.clear();
- ss.str(s);
- if(s.find_first_of(".eEdD")!=std::string::npos) {
- long double v;
- ss >> v;
- if(ss.fail() || !std::ws(ss).eof())
- throw bad_value_cast();
- #ifdef __BORLANDC__
- #pragma warn -8008
- #pragma warn -8066
- #endif
- if(std::numeric_limits<T>::is_integer) {
- if(v > (std::numeric_limits<T>::max)() || v < (std::numeric_limits<T>::min)())
- throw bad_value_cast();
- }
- #ifdef __BORLANDC__
- #pragma warn .8008
- #pragma warn .8066
- #endif
- return static_cast<T>(v);
- }
- T v;
- ss >> v;
- if(ss.fail() || !std::ws(ss).eof())
- throw bad_value_cast();
- if( std::numeric_limits<T>::is_integer
- && !std::numeric_limits<T>::is_signed
- && s.find('-') != std::string::npos
- && v!=0)
- {
- throw bad_value_cast();
- }
- return v;
- }
- }
- #endif
|