Skip to content

Commit 0ba8bd7

Browse files
committed
add move assignment operator for CZString and change copy assignment to const reference.
1 parent 23c44d9 commit 0ba8bd7

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

include/json/value.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,12 @@ class JSON_API Value {
233233
CZString(CZString&& other);
234234
#endif
235235
~CZString();
236-
CZString& operator=(CZString other);
236+
CZString& operator=(const CZString& other);
237+
238+
#if JSON_HAS_RVALUE_REFERENCES
239+
CZString& operator=(CZString&& other);
240+
#endif
241+
237242
bool operator<(CZString const& other) const;
238243
bool operator==(CZString const& other) const;
239244
ArrayIndex index() const;
@@ -447,7 +452,7 @@ Json::Value obj_value(Json::objectValue); // {}
447452
/// Equivalent to jsonvalue[jsonvalue.size()] = value;
448453
Value& append(const Value& value);
449454

450-
#ifdef JSON_HAS_RVALUE_REFERENCES
455+
#if JSON_HAS_RVALUE_REFERENCES
451456
Value& append(Value&& value);
452457
#endif
453458

src/lib_json/json_value.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,20 @@ void Value::CZString::swap(CZString& other) {
292292
std::swap(index_, other.index_);
293293
}
294294

295-
Value::CZString& Value::CZString::operator=(CZString other) {
296-
swap(other);
295+
Value::CZString& Value::CZString::operator=(const CZString& other) {
296+
cstr_ = other.cstr_;
297+
index_ = other.index_;
298+
return *this;
299+
}
300+
301+
#if JSON_HAS_RVALUE_REFERENCES
302+
Value::CZString& Value::CZString::operator=(CZString&& other) {
303+
cstr_ = other.cstr_;
304+
index_ = other.index_;
305+
other.cstr_ = nullptr;
297306
return *this;
298307
}
308+
#endif
299309

300310
bool Value::CZString::operator<(const CZString& other) const {
301311
if (!cstr_) return index_ < other.index_;
@@ -1145,7 +1155,7 @@ Value const& Value::operator[](CppTL::ConstString const& key) const
11451155

11461156
Value& Value::append(const Value& value) { return (*this)[size()] = value; }
11471157

1148-
#ifdef JSON_HAS_RVALUE_REFERENCES
1158+
#if JSON_HAS_RVALUE_REFERENCES
11491159
Value& Value::append(Value&& value) { return (*this)[size()] = value; }
11501160
#endif
11511161

0 commit comments

Comments
 (0)