Commit 08b11682 authored by jacobgladish's avatar jacobgladish

Fixed string array in Variant.

parent dd4342db
...@@ -14,6 +14,34 @@ namespace ...@@ -14,6 +14,34 @@ namespace
buff << static_cast<TOut>(typed_array[i]); buff << static_cast<TOut>(typed_array[i]);
} }
} }
template<class T>
void releaseArray(void* p)
{
T* arr = reinterpret_cast<T *>(p);
delete [] arr;
}
void freeArray2(void* p, common::Variant::DataType t)
{
typedef common::Variant::DataType dt;
switch (t)
{
case dt::BooleanArray: releaseArray<bool>(p); break;
case dt::UInt8Array: releaseArray<uint8_t>(p); break;
case dt::Int16Array: releaseArray<int16_t>(p); break;
case dt::UInt16Array: releaseArray<uint16_t>(p); break;
case dt::Int32Array: releaseArray<int32_t>(p); break;
case dt::UInt32Array: releaseArray<uint32_t>(p); break;
case dt::Int64Array: releaseArray<int64_t>(p); break;
case dt::UInt64Array: releaseArray<uint64_t>(p); break;
case dt::DoubleArray: releaseArray<double>(p); break;
case dt::StringArray: releaseArray<std::string>(p); break;
default:
assert(false);
break;
}
}
} }
common::Variant::Data::Data() common::Variant::Data::Data()
...@@ -200,7 +228,7 @@ common::Variant::~Variant() ...@@ -200,7 +228,7 @@ common::Variant::~Variant()
delete m_data.Item.v_string; delete m_data.Item.v_string;
if (IsArray() && m_data.Item.v_arr) if (IsArray() && m_data.Item.v_arr)
free(m_data.Item.v_arr); freeArray2(m_data.Item.v_arr, m_data.Type);
} }
common::Variant& common::Variant&
......
...@@ -117,6 +117,20 @@ namespace common ...@@ -117,6 +117,20 @@ namespace common
std::string ToString(bool* ok = NULL) const; std::string ToString(bool* ok = NULL) const;
private: private:
template<class T>
static void* allocArray(int n)
{
T* p = new T[n];
return reinterpret_cast<void *>(p);
}
template<class T>
static void freeArray(void* p)
{
T* arr = reinterpret_cast<T *>(p);
delete [] arr;
}
bool CanConvert(DataType t) const; bool CanConvert(DataType t) const;
template<class T> template<class T>
...@@ -204,12 +218,12 @@ namespace common ...@@ -204,12 +218,12 @@ namespace common
void AssignFromArray(Data& to, Data const& from) void AssignFromArray(Data& to, Data const& from)
{ {
if (IsArray() && to.Item.v_arr) if (IsArray() && to.Item.v_arr)
free(to.Item.v_arr); freeArray<T>(to.Item.v_arr);
memset(&to.Item, 0, sizeof(to.Item)); memset(&to.Item, 0, sizeof(to.Item));
to.Item.v_arr = malloc(sizeof(T) * from.Size);
to.Size = from.Size; to.Size = from.Size;
to.Type = from.Type; to.Type = from.Type;
to.Item.v_arr = allocArray<T>(from.Size);
T* toVect = reinterpret_cast<T *>(to.Item.v_arr); T* toVect = reinterpret_cast<T *>(to.Item.v_arr);
T* fromVect = reinterpret_cast<T *>(from.Item.v_arr); T* fromVect = reinterpret_cast<T *>(from.Item.v_arr);
...@@ -225,10 +239,10 @@ namespace common ...@@ -225,10 +239,10 @@ namespace common
if (d.Size > 0) if (d.Size > 0)
{ {
if (IsArray() && d.Item.v_arr) if (IsArray() && d.Item.v_arr)
free(d.Item.v_arr); freeArray<T>(d.Item.v_arr);
memset(&d.Item, 0, sizeof(d.Item)); memset(&d.Item, 0, sizeof(d.Item));
d.Item.v_arr = malloc(sizeof(T) * d.Size); d.Item.v_arr = allocArray<T>(d.Size);
T* arr = reinterpret_cast<T *>(d.Item.v_arr); T* arr = reinterpret_cast<T *>(d.Item.v_arr);
for (int i = 0; i < d.Size; ++i) for (int i = 0; i < d.Size; ++i)
......
...@@ -69,13 +69,25 @@ TEST(Variant, UInt8Array) { ...@@ -69,13 +69,25 @@ TEST(Variant, UInt8Array) {
EXPECT_EQ(std::string("[10,20]"), v1.ToString()); EXPECT_EQ(std::string("[10,20]"), v1.ToString());
} }
TEST(Variant, StringArray)
{
std::vector<std::string> a1;
a1.push_back("a");
a1.push_back("b");
common::Variant v1(a1);
common::Variant v2 = v1;
EXPECT_TRUE(v1.IsArray());
EXPECT_EQ(v1.Length(), 2);
}
TEST(VariantTest, String) { TEST(VariantTest, String) {
EXPECT_EQ(std::string("hello world"), TestVariant_StdString("hello world")); EXPECT_EQ(std::string("hello world"), TestVariant_StdString("hello world"));
EXPECT_EQ(std::string(""), TestVariant_StdString("")); EXPECT_EQ(std::string(""), TestVariant_StdString(""));
EXPECT_EQ(std::string(), TestVariant_StdString("")); EXPECT_EQ(std::string(), TestVariant_StdString(""));
} }
#if 0
TEST(VariantTest, StringCopyAssign) { TEST(VariantTest, StringCopyAssign) {
common::Variant v1; common::Variant v1;
common::Variant v2; common::Variant v2;
...@@ -90,7 +102,6 @@ TEST(VariantTest, StringCopyAssign) { ...@@ -90,7 +102,6 @@ TEST(VariantTest, StringCopyAssign) {
v1 = v2; v1 = v2;
EXPECT_EQ(v1.ToString(), v2.ToString()); EXPECT_EQ(v1.ToString(), v2.ToString());
} }
#endif
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
......
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