Improve Json::Value special members (fixes RIPD-215):

* Add move special members
* Fix Json::Value::swap
This commit is contained in:
Howard Hinnant
2014-04-18 13:39:40 -04:00
committed by Vinnie Falco
parent 73c5a867c6
commit b5348980e2
3 changed files with 87 additions and 1 deletions

View File

@@ -18,6 +18,7 @@
//==============================================================================
#include "../../../beast/beast/unit_test/suite.h"
#include "../../../beast/beast/utility/type_name.h"
namespace ripple {
@@ -37,9 +38,57 @@ public:
pass ();
}
void
test_copy ()
{
Json::Value v1{2.5};
expect (v1.isDouble ());
expect (v1.asDouble () == 2.5);
Json::Value v2 = v1;
expect (v1.isDouble ());
expect (v1.asDouble () == 2.5);
expect (v2.isDouble ());
expect (v2.asDouble () == 2.5);
expect (v1 == v2);
v1 = v2;
expect (v1.isDouble ());
expect (v1.asDouble () == 2.5);
expect (v2.isDouble ());
expect (v2.asDouble () == 2.5);
expect (v1 == v2);
pass ();
}
void
test_move ()
{
Json::Value v1{2.5};
expect (v1.isDouble ());
expect (v1.asDouble () == 2.5);
Json::Value v2 = std::move(v1);
expect (v1.isNull ());
expect (v2.isDouble ());
expect (v2.asDouble () == 2.5);
expect (v1 != v2);
v1 = std::move(v2);
expect (v1.isDouble ());
expect (v1.asDouble () == 2.5);
expect (v2.isNull ());
expect (v1 != v2);
pass ();
}
void run ()
{
testBadJson ();
test_copy ();
test_move ();
}
};

View File

@@ -554,16 +554,49 @@ Value::operator= ( const Value& other )
return *this;
}
Value::Value ( Value&& other )
: value_ ( other.value_ )
, type_ ( other.type_ )
, allocated_ ( other.allocated_ )
# ifdef JSON_VALUE_USE_INTERNAL_MAP
, itemIsUsed_ ( other.itemIsUsed_ )
, memberNameIsStatic_ ( other.memberNameIsStatic_ )
#endif // JSON_VALUE_USE_INTERNAL_MAP
, comments_ ( other.comments_ )
{
std::memset( &other, 0, sizeof(Value) );
}
Value&
Value::operator= ( Value&& other )
{
swap ( other );
return *this;
}
void
Value::swap ( Value& other )
{
std::swap ( value_, other.value_ );
ValueType temp = type_;
type_ = other.type_;
other.type_ = temp;
std::swap ( value_, other.value_ );
int temp2 = allocated_;
allocated_ = other.allocated_;
other.allocated_ = temp2;
# ifdef JSON_VALUE_USE_INTERNAL_MAP
unsigned temp3 = itemIsUsed_;
itemIsUsed_ = other.itemIsUsed_;
other.itemIsUsed_ = itemIsUsed_;
temp2 = memberNameIsStatic_;
memberNameIsStatic_ = other.memberNameIsStatic_;
other.memberNameIsStatic_ = temp2
# endif
std::swap(comments_, other.comments_);
}
ValueType