| 1 |
#pragma once |
| 2 |
|
| 3 |
#include <string> |
| 4 |
#include <sstream> |
| 5 |
#include <limits> |
| 6 |
#include <cstdlib> |
| 7 |
#include <cerrno> |
| 8 |
|
| 9 |
#include "typeconv_exception.hpp" |
| 10 |
#include "../base_types.hpp" |
| 11 |
|
| 12 |
|
| 13 |
namespace A_LIB_NAMESPACE{ |
| 14 |
|
| 15 |
namespace{ |
| 16 |
template<typename Unsigned, typename Signed> |
| 17 |
void check_sign(Unsigned val, const std::string & orig_string){ |
| 18 |
if( val > static_cast<Unsigned>(std::numeric_limits<Signed>::max()) && orig_string.find('-') != std::string::npos){ |
| 19 |
throw TypeConvException("Negative value to unsigned value"); |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
template<typename SourceType, typename DestType> |
| 24 |
DestType numerical_cast_safe(SourceType val){ |
| 25 |
if(val < static_cast<SourceType>(std::numeric_limits<DestType>::min()) |
| 26 |
|| val > static_cast<SourceType>(std::numeric_limits<DestType>::max())){ |
| 27 |
throw TypeConvException("Overflow in numerical conversion"); |
| 28 |
} |
| 29 |
return static_cast<DestType>(val); |
| 30 |
} |
| 31 |
|
| 32 |
template<typename Integral> |
| 33 |
inline void check_overflow_integral(Integral val, int errcode){ |
| 34 |
if( (val == std::numeric_limits<Integral>::min() || val ==std::numeric_limits<Integral>::max()) |
| 35 |
&& ( errcode == ERANGE || errcode == EINVAL)){ |
| 36 |
errno =0; |
| 37 |
throw TypeConvException("Overflow during type conversion from string to integral value"); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
inline void check_strto_error(char * end_str, const std::string & str){ |
| 42 |
if( str.size() ==0 || *end_str != '\0'){ |
| 43 |
errno =0; |
| 44 |
throw TypeConvException("Invalid type conversion from string to integral value"); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
template<typename T, typename S> |
| 53 |
struct toType{ |
| 54 |
T operator()(const S & val){ |
| 55 |
(void) val; |
| 56 |
throw TypeConvException("Invalid type conversion: no specialization"); |
| 57 |
} |
| 58 |
|
| 59 |
}; |
| 60 |
|
| 61 |
|
| 62 |
template <> |
| 63 |
struct toType<unsigned long long, std::string>{ |
| 64 |
unsigned long long operator()(const std::string & str){ |
| 65 |
char* end_str = NULL; |
| 66 |
errno =0; |
| 67 |
const unsigned long long ret = strtoull(str.c_str(), &end_str, 10); |
| 68 |
check_overflow_integral(ret, errno); |
| 69 |
check_strto_error(end_str,str); |
| 70 |
check_sign<unsigned long long, long long>(ret, str); |
| 71 |
return ret; |
| 72 |
} |
| 73 |
}; |
| 74 |
|
| 75 |
|
| 76 |
template <> |
| 77 |
struct toType<long long, std::string>{ |
| 78 |
long long operator()(const std::string & str){ |
| 79 |
char* end_str = NULL; |
| 80 |
errno =0; |
| 81 |
const long long ret = strtoll(str.c_str(), &end_str, 10); |
| 82 |
check_overflow_integral(ret, errno); |
| 83 |
check_strto_error(end_str,str); |
| 84 |
return ret; |
| 85 |
} |
| 86 |
}; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
template <> |
| 93 |
struct toType<unsigned long, std::string>{ |
| 94 |
unsigned long operator()(const std::string & str){ |
| 95 |
char* end_str = NULL; |
| 96 |
errno =0; |
| 97 |
const unsigned long ret = strtoul(str.c_str(), &end_str, 10); |
| 98 |
check_overflow_integral(ret, errno); |
| 99 |
check_strto_error(end_str,str); |
| 100 |
check_sign<unsigned long, long>(ret, str); |
| 101 |
return ret; |
| 102 |
} |
| 103 |
}; |
| 104 |
|
| 105 |
|
| 106 |
template <> |
| 107 |
struct toType<long, std::string>{ |
| 108 |
unsigned long operator()(const std::string & str){ |
| 109 |
char* end_str = NULL; |
| 110 |
errno =0; |
| 111 |
const long ret = strtol(str.c_str(), &end_str, 10); |
| 112 |
check_overflow_integral(ret, errno); |
| 113 |
check_strto_error(end_str,str); |
| 114 |
return ret; |
| 115 |
} |
| 116 |
}; |
| 117 |
|
| 118 |
|
| 119 |
template <> |
| 120 |
struct toType<unsigned int, std::string>{ |
| 121 |
unsigned int operator()(const std::string & str){ |
| 122 |
char* end_str = NULL; |
| 123 |
errno =0; |
| 124 |
const unsigned long ret = strtoul(str.c_str(), &end_str, 10); |
| 125 |
check_overflow_integral(ret, errno); |
| 126 |
check_strto_error(end_str,str); |
| 127 |
check_sign<unsigned long, long>(ret, str); |
| 128 |
return numerical_cast_safe<unsigned long, unsigned int>(ret); |
| 129 |
} |
| 130 |
}; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
template <> |
| 135 |
struct toType<int, std::string>{ |
| 136 |
int operator()(const std::string & str){ |
| 137 |
char* end_str = NULL; |
| 138 |
errno =0; |
| 139 |
const long ret = strtol(str.c_str(), &end_str, 10); |
| 140 |
check_overflow_integral(ret, errno); |
| 141 |
check_strto_error(end_str,str); |
| 142 |
return numerical_cast_safe<long, int>(ret); |
| 143 |
} |
| 144 |
}; |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
template<typename S> |
| 149 |
struct toType<std::string, S>{ |
| 150 |
std::string operator()(const S & val){ |
| 151 |
S ret; |
| 152 |
ss.str(""); |
| 153 |
ss << val; |
| 154 |
if(ss.fail()){ |
| 155 |
throw TypeConvException("Invalid type conversion from string to "); |
| 156 |
} |
| 157 |
return ret.str(); |
| 158 |
} |
| 159 |
|
| 160 |
std::ostringstream ss; |
| 161 |
}; |
| 162 |
|
| 163 |
} |