Improved error checking in Value + more to array conversion control.

This commit is contained in:
eelke 2017-12-17 09:06:18 +01:00
parent 1fe7d3c56d
commit d9854d81fa
3 changed files with 153 additions and 18 deletions

View file

@ -45,24 +45,44 @@ Value::operator std::string() const
return m_val;
}
Value::operator short() const
Value::operator int16_t() const
{
return (short)std::atoi(m_val);
int32_t r = operator int32_t();
if (r <= std::numeric_limits<int16_t>::max()
&& r >= std::numeric_limits<int16_t>::min())
return int16_t(r);
throw std::runtime_error("Value conversion to int16_t failed (out of range)");
}
Value::operator int() const
Value::operator int32_t() const
{
return std::atoi(m_val);
const int len = std::strlen(m_val);
if (len > 0) {
char *endptr = nullptr;
long result = std::strtol(m_val, &endptr, 10);
if (endptr == m_val + len)
return result;
}
throw std::runtime_error("Value conversion to int32_t failed");
}
Value::operator Oid() const
{
return operator int();
return operator int32_t();
}
Value::operator long long() const
Value::operator int64_t() const
{
return std::strtoull(m_val, nullptr, 10);
const int len = std::strlen(m_val);
if (len > 0) {
char *endptr = nullptr;
int64_t result = std::strtoll(m_val, &endptr, 10);
if (endptr == m_val + len)
return result;
}
throw std::runtime_error("Value conversion to int64_t failed");
// return std::strtoll(m_val, nullptr, 10);
}
Value::operator bool() const