move ctors

* Add move constructor to Value::CZString
* Add unit test for Value move constructor
* Allow includer to specify in advance the value for
JSON_HAS_RVALUE_REFERENCES
This commit is contained in:
Motti
2015-04-20 17:44:47 +03:00
committed by Christopher Dunn
parent a4ce2829dc
commit 2b00891a86
4 changed files with 82 additions and 13 deletions

View File

@@ -237,6 +237,15 @@ Value::CZString::CZString(const CZString& other)
storage_.length_ = other.storage_.length_;
}
#if JSON_HAS_RVALUE_REFERENCES
Value::CZString::CZString(CZString&& other)
: cstr_(other.cstr_),
index_(other.index_)
{
other.cstr_ = 0;
}
#endif
Value::CZString::~CZString() {
if (cstr_ && storage_.policy_ == duplicate)
releaseStringValue(const_cast<char*>(cstr_));
@@ -425,6 +434,15 @@ Value::Value(Value const& other)
}
}
#if JSON_HAS_RVALUE_REFERENCES
// Move constructor
Value::Value(Value&& other)
{
initBasic(nullValue);
swap(other);
}
#endif
Value::~Value() {
switch (type_) {
case nullValue:

View File

@@ -2497,6 +2497,19 @@ JSONTEST_FIXTURE(IteratorTest, const) {
JSONTEST_ASSERT_STRING_EQUAL(expected, out.str());
}
struct RValueTest : JsonTest::TestCase {};
JSONTEST_FIXTURE(RValueTest, moveConstruction) {
#if JSON_HAS_RVALUE_REFERENCES
Json::Value json;
json["key"] = "value";
Json::Value moved = std::move(json);
JSONTEST_ASSERT(moved != json); // Possibly not nullValue; definitely not equal.
JSONTEST_ASSERT_EQUAL(Json::objectValue, moved.type());
JSONTEST_ASSERT_EQUAL(Json::stringValue, moved["key"].type());
#endif
}
int main(int argc, const char* argv[]) {
JsonTest::Runner runner;
JSONTEST_REGISTER_FIXTURE(runner, ValueTest, checkNormalizeFloatingPointStr);
@@ -2570,5 +2583,7 @@ int main(int argc, const char* argv[]) {
JSONTEST_REGISTER_FIXTURE(runner, IteratorTest, indexes);
JSONTEST_REGISTER_FIXTURE(runner, IteratorTest, const);
JSONTEST_REGISTER_FIXTURE(runner, RValueTest, moveConstruction);
return runner.runCommandLine(argc, argv);
}