Skip to content

Commit

Permalink
add move assignment operator for Json::Value class.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvCW committed Jul 8, 2017
1 parent a679dde commit 8996c37
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
17 changes: 13 additions & 4 deletions include/json/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#endif

//Conditional NORETURN attribute on the throw functions would:
// a) suppress false positives from static code analysis
// a) suppress false positives from static code analysis
// b) possibly improve optimization opportunities.
#if !defined(JSONCPP_NORETURN)
# if defined(_MSC_VER)
Expand Down Expand Up @@ -64,7 +64,7 @@ class JSON_API Exception : public std::exception {
/** Exceptions which the user cannot easily avoid.
*
* E.g. out-of-memory (when we use malloc), stack-overflow, malicious input
*
*
* \remark derived from Json::Exception
*/
class JSON_API RuntimeError : public Exception {
Expand All @@ -75,7 +75,7 @@ class JSON_API RuntimeError : public Exception {
/** Exceptions thrown by JSON_ASSERT/JSON_FAIL macros.
*
* These are precondition-violations (user bugs) and internal errors (our bugs).
*
*
* \remark derived from Json::Exception
*/
class JSON_API LogicError : public Exception {
Expand Down Expand Up @@ -322,12 +322,21 @@ Json::Value obj_value(Json::objectValue); // {}

/// Deep copy, then swap(other).
/// \note Over-write existing comments. To preserve comments, use #swapPayload().
Value& operator=(Value other);
Value& operator=(const Value& other);
#if JSON_HAS_RVALUE_REFERENCES
Value& operator=(Value&& other);
#endif

/// Swap everything.
void swap(Value& other);
/// Swap values but leave comments and source offsets in place.
void swapPayload(Value& other);

/// copy everything.
void copy(const Value& other);
/// copy values but leave comments and source offsets in place.
void copyPayload(const Value& other);

ValueType type() const;

/// Compare payload only, not comments etc.
Expand Down
25 changes: 23 additions & 2 deletions src/lib_json/json_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ Value::Value(double value) {

Value::Value(const char* value) {
initBasic(stringValue, true);
JSON_ASSERT_MESSAGE(value != NULL, "Null Value Passed to Value Constructor");
JSON_ASSERT_MESSAGE(value != NULL, "Null Value Passed to Value Constructor");
value_.string_ = duplicateAndPrefixStringValue(value, static_cast<unsigned>(strlen(value)));
}

Expand Down Expand Up @@ -508,10 +508,18 @@ Value::~Value() {
value_.uint_ = 0;
}

Value& Value::operator=(Value other) {
Value& Value::operator=(const Value& other) {
swap(const_cast<Value&>(other));
return *this;
}

#if JSON_HAS_RVALUE_REFERENCES
Value& Value::operator=(Value&& other) {
initBasic(nullValue);
swap(other);
return *this;
}
#endif

void Value::swapPayload(Value& other) {
ValueType temp = type_;
Expand All @@ -523,13 +531,26 @@ void Value::swapPayload(Value& other) {
other.allocated_ = temp2 & 0x1;
}

void Value::copyPayload(const Value& other) {
type_ = other.type_;
value_ = other.value_;
allocated_ = other.allocated_;
}

void Value::swap(Value& other) {
swapPayload(other);
std::swap(comments_, other.comments_);
std::swap(start_, other.start_);
std::swap(limit_, other.limit_);
}

void Value::copy(const Value& other) {
copyPayload(other);
comments_ = other.comments_;
start_ = other.start_;
limit_ = other.limit_;
}

ValueType Value::type() const { return type_; }

int Value::compare(const Value& other) const {
Expand Down

0 comments on commit 8996c37

Please sign in to comment.