bool Value::as_bool() const { try { switch(m_storedAs) { case valueType_bool: return any_cast<bool>(m_value); case valueType_int16: case valueType_uint32: case valueType_uint16: case valueType_uint8: case valueType_int32: return as_uint32() != 0; case valueType_float: case valueType_double: return as_double() != 0.0; case valueType_string: default: return as_uint32() != 0; } } catch(...) { throw Error_BadDataType(); } }
std::string Value::as_string() const { //if the data is stored as a string if(m_storedAs == valueType_string) { try { return any_cast<std::string>(m_value); } catch(std::bad_cast&) { throw Error_BadDataType(); } } //build the string depending on how the value is stored switch(m_storedAs) { case valueType_uint8: return Utils::toStr(static_cast<uint16>(as_uint8())); case valueType_uint16: return Utils::toStr(as_uint16()); case valueType_uint32: return Utils::toStr(as_uint32()); case valueType_int16: return Utils::toStr(as_int16()); case valueType_int32: return Utils::toStr(as_int32()); case valueType_float: return Utils::toStr(as_float()); case valueType_double: return Utils::toStr(as_double()); case valueType_bool: return Utils::toStr(as_bool()); default: throw Error_BadDataType(); } }
bool Value::isSameValue(const Value& other) const { switch(m_storedAs) { case valueType_float: return as_float() == other.as_float(); case valueType_double: return as_double() == other.as_double(); case valueType_uint16: return as_uint16() == other.as_uint16(); case valueType_uint32: return as_uint32() == other.as_uint32(); case valueType_int16: return as_int16() == other.as_int16(); case valueType_int32: return as_int32() == other.as_int32(); case valueType_uint8: return as_uint8() == other.as_uint8(); case valueType_bool: return as_bool() == other.as_bool(); case valueType_string: return as_string() == other.as_string(); default: return false; } }
TEST_F(CtypeOpsFromStringTest, uint32_max) { EXPECT_EQ(0, parse("uint32", "4294967295")); EXPECT_EQ(0xFFFFFFFF, as_uint32()); }
TEST_F(CtypeOpsFromStringTest, uint32_min) { EXPECT_EQ(0, parse("uint32", "0")); EXPECT_EQ(0U, as_uint32()); }
uint32_t as_uint32_suffix() const { return as_uint32(true); }
operator uint32_t() const { return as_uint32(); }