Mercurial > code
changeset 321:0eb59106f700
Json: move operators now makes the other value JsonType::Null to be still usable.
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 27 Feb 2015 08:48:40 +0100 |
parents | b67b2da45bb0 |
children | 0e80e4589533 |
files | C++/Json.h C++/Tests/Json/main.cpp |
diffstat | 2 files changed, 36 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/C++/Json.h Thu Feb 26 14:21:29 2015 +0100 +++ b/C++/Json.h Fri Feb 27 08:48:40 2015 +0100 @@ -203,18 +203,28 @@ } /** - * Default move constructor. + * Move constructor, the other value is left empty (JsonType::Null). * * @param other the other value */ - JsonValue(JsonValue &&other) = default; + inline JsonValue(JsonValue &&other) + : m_handle(std::move(other.m_handle)) + { + other.m_handle = Handle(json_null(), json_decref); + } /** - * Default move assignment. + * Move assignment, the other value is left empty (JsonType::Null). * * @param other the other value */ - JsonValue &operator=(JsonValue &&) = default; + inline JsonValue &operator=(JsonValue &&other) + { + m_handle = std::move(other.m_handle); + other.m_handle = Handle(json_null(), json_decref); + + return *this; + } /** * Create a JsonValue from a native Jansson type. This function
--- a/C++/Tests/Json/main.cpp Thu Feb 26 14:21:29 2015 +0100 +++ b/C++/Tests/Json/main.cpp Fri Feb 27 08:48:40 2015 +0100 @@ -59,6 +59,28 @@ ASSERT_TRUE(object["true"].isTrue()); } +TEST(Misc, move) +{ + JsonObject object(123); + JsonObject object2(std::move(object)); + + ASSERT_TRUE(object.isNull()); + ASSERT_TRUE(object2.isInteger()); + ASSERT_EQ(123, object2.toInteger()); +} + +TEST(Misc, moveAssign) +{ + JsonObject object(123); + JsonObject object2; + + object2 = std::move(object); + + ASSERT_TRUE(object.isNull()); + ASSERT_TRUE(object2.isInteger()); + ASSERT_EQ(123, object2.toInteger()); +} + /* -------------------------------------------------------- * JsonValue constructors * -------------------------------------------------------- */