diff --git a/include/dap/any.h b/include/dap/any.h index 0d2242f..187ca5d 100644 --- a/include/dap/any.h +++ b/include/dap/any.h @@ -85,7 +85,13 @@ any::any(const any& other) noexcept : type(other.type) { } } -any::any(any&& other) noexcept : value(other.value), type(other.type) { +any::any(any&& other) noexcept : type(other.type) { + if (other.isInBuffer(other.value)) { + alloc(type->size(), type->alignment()); + type->copyConstruct(value, other.value); + } else { + value = other.value; + } other.value = nullptr; other.type = nullptr; } @@ -110,8 +116,14 @@ any& any::operator=(const any& rhs) { } any& any::operator=(any&& rhs) noexcept { - value = rhs.value; + reset(); type = rhs.type; + if (rhs.isInBuffer(rhs.value)) { + alloc(type->size(), type->alignment()); + type->copyConstruct(value, rhs.value); + } else { + value = rhs.value; + } rhs.value = nullptr; rhs.type = nullptr; return *this; diff --git a/src/any_test.cpp b/src/any_test.cpp index d52ff05..2028d8c 100644 --- a/src/any_test.cpp +++ b/src/any_test.cpp @@ -40,6 +40,7 @@ TEST(Any, EmptyConstruct) { ASSERT_FALSE(any.is()); ASSERT_FALSE(any.is()); ASSERT_FALSE(any.is()); + ASSERT_FALSE(any.is()); ASSERT_FALSE(any.is>()); ASSERT_FALSE(any.is()); } @@ -62,6 +63,12 @@ TEST(Any, Number) { ASSERT_EQ(any.get(), dap::number(123.0f)); } +TEST(Any, String) { + dap::any any(dap::string("hello world")); + ASSERT_TRUE(any.is()); + ASSERT_EQ(any.get(), dap::string("hello world")); +} + TEST(Any, Array) { using array = dap::array; dap::any any(array({10, 20, 30})); @@ -116,6 +123,12 @@ TEST(Any, Assign) { ASSERT_TRUE(any.is()); ASSERT_EQ(any.get().i, 5); ASSERT_EQ(any.get().n, 3.0); + any = dap::string("hello world"); + ASSERT_FALSE(any.is()); + ASSERT_FALSE(any.is()); + ASSERT_FALSE(any.is()); + ASSERT_TRUE(any.is()); + ASSERT_EQ(any.get(), dap::string("hello world")); } TEST(Any, Reset) {