Skip to content

Commit 8996c37

Browse files
committed
add move assignment operator for Json::Value class.
1 parent a679dde commit 8996c37

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

include/json/value.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#endif
2424

2525
//Conditional NORETURN attribute on the throw functions would:
26-
// a) suppress false positives from static code analysis
26+
// a) suppress false positives from static code analysis
2727
// b) possibly improve optimization opportunities.
2828
#if !defined(JSONCPP_NORETURN)
2929
# if defined(_MSC_VER)
@@ -64,7 +64,7 @@ class JSON_API Exception : public std::exception {
6464
/** Exceptions which the user cannot easily avoid.
6565
*
6666
* E.g. out-of-memory (when we use malloc), stack-overflow, malicious input
67-
*
67+
*
6868
* \remark derived from Json::Exception
6969
*/
7070
class JSON_API RuntimeError : public Exception {
@@ -75,7 +75,7 @@ class JSON_API RuntimeError : public Exception {
7575
/** Exceptions thrown by JSON_ASSERT/JSON_FAIL macros.
7676
*
7777
* These are precondition-violations (user bugs) and internal errors (our bugs).
78-
*
78+
*
7979
* \remark derived from Json::Exception
8080
*/
8181
class JSON_API LogicError : public Exception {
@@ -322,12 +322,21 @@ Json::Value obj_value(Json::objectValue); // {}
322322

323323
/// Deep copy, then swap(other).
324324
/// \note Over-write existing comments. To preserve comments, use #swapPayload().
325-
Value& operator=(Value other);
325+
Value& operator=(const Value& other);
326+
#if JSON_HAS_RVALUE_REFERENCES
327+
Value& operator=(Value&& other);
328+
#endif
329+
326330
/// Swap everything.
327331
void swap(Value& other);
328332
/// Swap values but leave comments and source offsets in place.
329333
void swapPayload(Value& other);
330334

335+
/// copy everything.
336+
void copy(const Value& other);
337+
/// copy values but leave comments and source offsets in place.
338+
void copyPayload(const Value& other);
339+
331340
ValueType type() const;
332341

333342
/// Compare payload only, not comments etc.

src/lib_json/json_value.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ Value::Value(double value) {
398398

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

@@ -508,10 +508,18 @@ Value::~Value() {
508508
value_.uint_ = 0;
509509
}
510510

511-
Value& Value::operator=(Value other) {
511+
Value& Value::operator=(const Value& other) {
512+
swap(const_cast<Value&>(other));
513+
return *this;
514+
}
515+
516+
#if JSON_HAS_RVALUE_REFERENCES
517+
Value& Value::operator=(Value&& other) {
518+
initBasic(nullValue);
512519
swap(other);
513520
return *this;
514521
}
522+
#endif
515523

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

534+
void Value::copyPayload(const Value& other) {
535+
type_ = other.type_;
536+
value_ = other.value_;
537+
allocated_ = other.allocated_;
538+
}
539+
526540
void Value::swap(Value& other) {
527541
swapPayload(other);
528542
std::swap(comments_, other.comments_);
529543
std::swap(start_, other.start_);
530544
std::swap(limit_, other.limit_);
531545
}
532546

547+
void Value::copy(const Value& other) {
548+
copyPayload(other);
549+
comments_ = other.comments_;
550+
start_ = other.start_;
551+
limit_ = other.limit_;
552+
}
553+
533554
ValueType Value::type() const { return type_; }
534555

535556
int Value::compare(const Value& other) const {

0 commit comments

Comments
 (0)