Commit 83e1372f authored by jacobgladish's avatar jacobgladish

Fixed up Variant::ToString()

parent 08b11682
......@@ -391,7 +391,7 @@ common::Variant::CanConvert(DataType t) const
}
std::string
common::Variant::ToString(bool* ok) const
common::Variant::ToString() const
{
if (IsArray())
{
......@@ -418,17 +418,28 @@ common::Variant::ToString(bool* ok) const
return buff.str();
}
// string handled special to allow for other data types
// to be converted later.
if (CanConvert(DataType::String))
if (m_data.Type == DataType::String)
{
if (ok)
*ok = true;
if (m_data.Item.v_string)
return *m_data.Item.v_string;
return std::string();
return (m_data.Item.v_string != NULL)
? *m_data.Item.v_string
: std::string();
}
if (ok)
*ok = false;
return std::string();
std::stringstream buff;
switch (m_data.Type)
{
case DataType::Boolean: buff << m_data.Item.v_bool; break;
case DataType::UInt8: buff << (int) m_data.Item.v_uint8; break;
case DataType::Int16: buff << m_data.Item.v_int16; break;
case DataType::UInt16: buff << m_data.Item.v_uint16; break;
case DataType::Int32: buff << m_data.Item.v_int32; break;
case DataType::UInt32: buff << m_data.Item.v_uint32; break;
case DataType::Int64: buff << m_data.Item.v_int64; break;
case DataType::UInt64: buff << m_data.Item.v_uint64; break;
case DataType::Double: buff << m_data.Item.v_double; break;
default:
assert(false);
break;
}
return buff.str();
}
......@@ -114,7 +114,7 @@ namespace common
std::vector<double> ToDoubleArray(bool* ok = NULL) const;
std::vector<std::string> ToStringArray(bool* ok = NULL) const;
std::string ToString(bool* ok = NULL) const;
std::string ToString() const;
private:
template<class T>
......@@ -159,12 +159,10 @@ namespace common
{
if (CanConvert(t))
{
if (ok)
*ok = true;
if (ok) *ok = true;
return Convert<T>(t);
}
if (ok)
*ok = false;
if (ok) *ok = false;
return T();
}
......
......@@ -88,6 +88,19 @@ TEST(VariantTest, String) {
EXPECT_EQ(std::string(), TestVariant_StdString(""));
}
TEST(VariantTest, ToString)
{
common::Variant v1;
v1 = (uint8_t) 234;
EXPECT_EQ("234", v1.ToString());
v1 = (int) 1234567;
EXPECT_EQ("1234567", v1.ToString());
v1 = (uint64_t) 18446744073709551615ul;;
EXPECT_EQ("18446744073709551615", v1.ToString());
}
TEST(VariantTest, StringCopyAssign) {
common::Variant v1;
common::Variant v2;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment