GenericValue: drop duplicate/unneeded code
* Delegate constant string construction to SetStringRaw * Delegate "const Ch*" overloads to GenericValue variants of operator[], FindMember and RemoveMember * Remove repeated template arguments in nested struct Array (cherry-picked from ca9b0332d)
This commit is contained in:
parent
d178fd4e94
commit
afa8279d8a
@ -285,12 +285,7 @@ public:
|
|||||||
explicit GenericValue(double d) : data_(), flags_(kNumberDoubleFlag) { data_.n.d = d; }
|
explicit GenericValue(double d) : data_(), flags_(kNumberDoubleFlag) { data_.n.d = d; }
|
||||||
|
|
||||||
//! Constructor for constant string (i.e. do not make a copy of string)
|
//! Constructor for constant string (i.e. do not make a copy of string)
|
||||||
GenericValue(const Ch* s, SizeType length) : data_(), flags_() {
|
GenericValue(const Ch* s, SizeType length) : data_(), flags_() { SetStringRaw(s, length); }
|
||||||
RAPIDJSON_ASSERT(s != NULL);
|
|
||||||
flags_ = kConstStringFlag;
|
|
||||||
data_.s.str = s;
|
|
||||||
data_.s.length = length;
|
|
||||||
}
|
|
||||||
|
|
||||||
//! Constructor for constant string (i.e. do not make a copy of string)
|
//! Constructor for constant string (i.e. do not make a copy of string)
|
||||||
explicit GenericValue(const Ch* s) : data_(), flags_() { SetStringRaw(s, internal::StrLen(s)); }
|
explicit GenericValue(const Ch* s) : data_(), flags_() { SetStringRaw(s, internal::StrLen(s)); }
|
||||||
@ -427,14 +422,8 @@ public:
|
|||||||
A better approach is to use the now public FindMember().
|
A better approach is to use the now public FindMember().
|
||||||
*/
|
*/
|
||||||
GenericValue& operator[](const Ch* name) {
|
GenericValue& operator[](const Ch* name) {
|
||||||
MemberIterator member = FindMember(name);
|
GenericValue n(name, internal::StrLen(name));
|
||||||
if (member != MemberEnd())
|
return (*this)[n];
|
||||||
return member->value;
|
|
||||||
else {
|
|
||||||
RAPIDJSON_ASSERT(false); // see above note
|
|
||||||
static GenericValue NullValue;
|
|
||||||
return NullValue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
const GenericValue& operator[](const Ch* name) const { return const_cast<GenericValue&>(*this)[name]; }
|
const GenericValue& operator[](const Ch* name) const { return const_cast<GenericValue&>(*this)[name]; }
|
||||||
|
|
||||||
@ -486,15 +475,8 @@ public:
|
|||||||
\c std::map, this has been changed to MemberEnd() now.
|
\c std::map, this has been changed to MemberEnd() now.
|
||||||
*/
|
*/
|
||||||
MemberIterator FindMember(const Ch* name) {
|
MemberIterator FindMember(const Ch* name) {
|
||||||
RAPIDJSON_ASSERT(name);
|
GenericValue n(name, internal::StrLen(name));
|
||||||
RAPIDJSON_ASSERT(IsObject());
|
return FindMember(n);
|
||||||
|
|
||||||
SizeType len = internal::StrLen(name);
|
|
||||||
MemberIterator member = MemberBegin();
|
|
||||||
for (; member != MemberEnd(); ++member)
|
|
||||||
if (member->name.data_.s.length == len && memcmp(member->name.data_.s.str, name, len * sizeof(Ch)) == 0)
|
|
||||||
break;
|
|
||||||
return member;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ConstMemberIterator FindMember(const Ch* name) const { return const_cast<GenericValue&>(*this).FindMember(name); }
|
ConstMemberIterator FindMember(const Ch* name) const { return const_cast<GenericValue&>(*this).FindMember(name); }
|
||||||
@ -565,13 +547,8 @@ public:
|
|||||||
\note Removing member is implemented by moving the last member. So the ordering of members is changed.
|
\note Removing member is implemented by moving the last member. So the ordering of members is changed.
|
||||||
*/
|
*/
|
||||||
bool RemoveMember(const Ch* name) {
|
bool RemoveMember(const Ch* name) {
|
||||||
MemberIterator m = FindMember(name);
|
GenericValue n(name, internal::StrLen(name));
|
||||||
if (m != MemberEnd()) {
|
return RemoveMember(n);
|
||||||
RemoveMember(m);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool RemoveMember(const GenericValue& name) {
|
bool RemoveMember(const GenericValue& name) {
|
||||||
@ -891,7 +868,7 @@ private:
|
|||||||
}; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode
|
}; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode
|
||||||
|
|
||||||
struct Array {
|
struct Array {
|
||||||
GenericValue<Encoding, Allocator>* elements;
|
GenericValue* elements;
|
||||||
SizeType size;
|
SizeType size;
|
||||||
SizeType capacity;
|
SizeType capacity;
|
||||||
}; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode
|
}; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode
|
||||||
|
Loading…
x
Reference in New Issue
Block a user