Fix moves of any when value isInBuffer

This was copying the pointer to the other's value in other.buffer.
This commit is contained in:
Ben Clayton 2020-06-10 13:51:21 +01:00
parent 2f607e077a
commit f0c28f93dd
2 changed files with 27 additions and 2 deletions

View File

@ -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.value = nullptr;
other.type = nullptr; other.type = nullptr;
} }
@ -110,8 +116,14 @@ any& any::operator=(const any& rhs) {
} }
any& any::operator=(any&& rhs) noexcept { any& any::operator=(any&& rhs) noexcept {
value = rhs.value; reset();
type = rhs.type; 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.value = nullptr;
rhs.type = nullptr; rhs.type = nullptr;
return *this; return *this;

View File

@ -40,6 +40,7 @@ TEST(Any, EmptyConstruct) {
ASSERT_FALSE(any.is<dap::integer>()); ASSERT_FALSE(any.is<dap::integer>());
ASSERT_FALSE(any.is<dap::number>()); ASSERT_FALSE(any.is<dap::number>());
ASSERT_FALSE(any.is<dap::object>()); ASSERT_FALSE(any.is<dap::object>());
ASSERT_FALSE(any.is<dap::string>());
ASSERT_FALSE(any.is<dap::array<dap::integer>>()); ASSERT_FALSE(any.is<dap::array<dap::integer>>());
ASSERT_FALSE(any.is<dap::AnyTestObject>()); ASSERT_FALSE(any.is<dap::AnyTestObject>());
} }
@ -62,6 +63,12 @@ TEST(Any, Number) {
ASSERT_EQ(any.get<dap::number>(), dap::number(123.0f)); ASSERT_EQ(any.get<dap::number>(), dap::number(123.0f));
} }
TEST(Any, String) {
dap::any any(dap::string("hello world"));
ASSERT_TRUE(any.is<dap::string>());
ASSERT_EQ(any.get<dap::string>(), dap::string("hello world"));
}
TEST(Any, Array) { TEST(Any, Array) {
using array = dap::array<dap::integer>; using array = dap::array<dap::integer>;
dap::any any(array({10, 20, 30})); dap::any any(array({10, 20, 30}));
@ -116,6 +123,12 @@ TEST(Any, Assign) {
ASSERT_TRUE(any.is<dap::AnyTestObject>()); ASSERT_TRUE(any.is<dap::AnyTestObject>());
ASSERT_EQ(any.get<dap::AnyTestObject>().i, 5); ASSERT_EQ(any.get<dap::AnyTestObject>().i, 5);
ASSERT_EQ(any.get<dap::AnyTestObject>().n, 3.0); ASSERT_EQ(any.get<dap::AnyTestObject>().n, 3.0);
any = dap::string("hello world");
ASSERT_FALSE(any.is<dap::integer>());
ASSERT_FALSE(any.is<dap::boolean>());
ASSERT_FALSE(any.is<dap::AnyTestObject>());
ASSERT_TRUE(any.is<dap::string>());
ASSERT_EQ(any.get<dap::string>(), dap::string("hello world"));
} }
TEST(Any, Reset) { TEST(Any, Reset) {