GenericValue: accept different allocators for read-only access
Several GenericValue functions take a const-reference to another GenericValue only. These functions can safely accept values with a different allocator type. The following functions are therefore extended by a SourceAllocator template parameter in this commit: - operator==/!=/[] - HasMember, FindMember, RemoveMember - StringEqual
This commit is contained in:
parent
ab8416e1e6
commit
e294ce6f7a
@ -444,7 +444,7 @@ public:
|
|||||||
\see CopyFrom()
|
\see CopyFrom()
|
||||||
*/
|
*/
|
||||||
template< typename SourceAllocator >
|
template< typename SourceAllocator >
|
||||||
GenericValue(const GenericValue<Encoding,SourceAllocator>& rhs, Allocator & allocator);
|
GenericValue(const GenericValue<Encoding, SourceAllocator>& rhs, Allocator & allocator);
|
||||||
|
|
||||||
//! Constructor for boolean value.
|
//! Constructor for boolean value.
|
||||||
/*! \param b Boolean value
|
/*! \param b Boolean value
|
||||||
@ -603,10 +603,10 @@ public:
|
|||||||
\param allocator Allocator to use for copying
|
\param allocator Allocator to use for copying
|
||||||
*/
|
*/
|
||||||
template <typename SourceAllocator>
|
template <typename SourceAllocator>
|
||||||
GenericValue& CopyFrom(const GenericValue<Encoding,SourceAllocator>& rhs, Allocator& allocator) {
|
GenericValue& CopyFrom(const GenericValue<Encoding, SourceAllocator>& rhs, Allocator& allocator) {
|
||||||
RAPIDJSON_ASSERT((void*)this != (void const*)&rhs);
|
RAPIDJSON_ASSERT((void*)this != (void const*)&rhs);
|
||||||
this->~GenericValue();
|
this->~GenericValue();
|
||||||
new (this) GenericValue(rhs,allocator);
|
new (this) GenericValue(rhs, allocator);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -635,7 +635,9 @@ public:
|
|||||||
\note If an object contains duplicated named member, comparing equality with any object is always \c false.
|
\note If an object contains duplicated named member, comparing equality with any object is always \c false.
|
||||||
\note Linear time complexity (number of all values in the subtree and total lengths of all strings).
|
\note Linear time complexity (number of all values in the subtree and total lengths of all strings).
|
||||||
*/
|
*/
|
||||||
bool operator==(const GenericValue& rhs) const {
|
template <typename SourceAllocator>
|
||||||
|
bool operator==(const GenericValue<Encoding, SourceAllocator>& rhs) const {
|
||||||
|
typedef GenericValue<Encoding, SourceAllocator> RhsType;
|
||||||
if (GetType() != rhs.GetType())
|
if (GetType() != rhs.GetType())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
@ -644,7 +646,7 @@ public:
|
|||||||
if (data_.o.size != rhs.data_.o.size)
|
if (data_.o.size != rhs.data_.o.size)
|
||||||
return false;
|
return false;
|
||||||
for (ConstMemberIterator lhsMemberItr = MemberBegin(); lhsMemberItr != MemberEnd(); ++lhsMemberItr) {
|
for (ConstMemberIterator lhsMemberItr = MemberBegin(); lhsMemberItr != MemberEnd(); ++lhsMemberItr) {
|
||||||
ConstMemberIterator rhsMemberItr = rhs.FindMember(lhsMemberItr->name);
|
typename RhsType::ConstMemberIterator rhsMemberItr = rhs.FindMember(lhsMemberItr->name);
|
||||||
if (rhsMemberItr == rhs.MemberEnd() || lhsMemberItr->value != rhsMemberItr->value)
|
if (rhsMemberItr == rhs.MemberEnd() || lhsMemberItr->value != rhsMemberItr->value)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -690,7 +692,8 @@ public:
|
|||||||
//! Not-equal-to operator
|
//! Not-equal-to operator
|
||||||
/*! \return !(*this == rhs)
|
/*! \return !(*this == rhs)
|
||||||
*/
|
*/
|
||||||
bool operator!=(const GenericValue& rhs) const { return !(*this == rhs); }
|
template <typename SourceAllocator>
|
||||||
|
bool operator!=(const GenericValue<Encoding, SourceAllocator>& rhs) const { return !(*this == rhs); }
|
||||||
|
|
||||||
//! Not-equal-to operator with arbitrary types
|
//! Not-equal-to operator with arbitrary types
|
||||||
/*! \return !(*this == rhs)
|
/*! \return !(*this == rhs)
|
||||||
@ -774,7 +777,8 @@ public:
|
|||||||
|
|
||||||
// This version is faster because it does not need a StrLen().
|
// This version is faster because it does not need a StrLen().
|
||||||
// It can also handle string with null character.
|
// It can also handle string with null character.
|
||||||
GenericValue& operator[](const GenericValue& name) {
|
template <typename SourceAllocator>
|
||||||
|
GenericValue& operator[](const GenericValue<Encoding, SourceAllocator>& name) {
|
||||||
MemberIterator member = FindMember(name);
|
MemberIterator member = FindMember(name);
|
||||||
if (member != MemberEnd())
|
if (member != MemberEnd())
|
||||||
return member->value;
|
return member->value;
|
||||||
@ -784,7 +788,8 @@ public:
|
|||||||
return NullValue;
|
return NullValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const GenericValue& operator[](const GenericValue& name) const { return const_cast<GenericValue&>(*this)[name]; }
|
template <typename SourceAllocator>
|
||||||
|
const GenericValue& operator[](const GenericValue<Encoding, SourceAllocator>& name) const { return const_cast<GenericValue&>(*this)[name]; }
|
||||||
|
|
||||||
//! Const member iterator
|
//! Const member iterator
|
||||||
/*! \pre IsObject() == true */
|
/*! \pre IsObject() == true */
|
||||||
@ -818,7 +823,8 @@ public:
|
|||||||
\note It is better to use FindMember() directly if you need the obtain the value as well.
|
\note It is better to use FindMember() directly if you need the obtain the value as well.
|
||||||
\note Linear time complexity.
|
\note Linear time complexity.
|
||||||
*/
|
*/
|
||||||
bool HasMember(const GenericValue& name) const { return FindMember(name) != MemberEnd(); }
|
template <typename SourceAllocator>
|
||||||
|
bool HasMember(const GenericValue<Encoding, SourceAllocator>& name) const { return FindMember(name) != MemberEnd(); }
|
||||||
|
|
||||||
//! Find member by name.
|
//! Find member by name.
|
||||||
/*!
|
/*!
|
||||||
@ -852,7 +858,8 @@ public:
|
|||||||
\c std::map, this has been changed to MemberEnd() now.
|
\c std::map, this has been changed to MemberEnd() now.
|
||||||
\note Linear time complexity.
|
\note Linear time complexity.
|
||||||
*/
|
*/
|
||||||
MemberIterator FindMember(const GenericValue& name) {
|
template <typename SourceAllocator>
|
||||||
|
MemberIterator FindMember(const GenericValue<Encoding, SourceAllocator>& name) {
|
||||||
RAPIDJSON_ASSERT(IsObject());
|
RAPIDJSON_ASSERT(IsObject());
|
||||||
RAPIDJSON_ASSERT(name.IsString());
|
RAPIDJSON_ASSERT(name.IsString());
|
||||||
MemberIterator member = MemberBegin();
|
MemberIterator member = MemberBegin();
|
||||||
@ -861,7 +868,7 @@ public:
|
|||||||
break;
|
break;
|
||||||
return member;
|
return member;
|
||||||
}
|
}
|
||||||
ConstMemberIterator FindMember(const GenericValue& name) const { return const_cast<GenericValue&>(*this).FindMember(name); }
|
template <typename SourceAllocator> ConstMemberIterator FindMember(const GenericValue<Encoding, SourceAllocator>& name) const { return const_cast<GenericValue&>(*this).FindMember(name); }
|
||||||
|
|
||||||
//! Add a member (name-value pair) to the object.
|
//! Add a member (name-value pair) to the object.
|
||||||
/*! \param name A string value as name of member.
|
/*! \param name A string value as name of member.
|
||||||
@ -971,7 +978,8 @@ public:
|
|||||||
return RemoveMember(n);
|
return RemoveMember(n);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool RemoveMember(const GenericValue& name) {
|
template <typename SourceAllocator>
|
||||||
|
bool RemoveMember(const GenericValue<Encoding, SourceAllocator>& name) {
|
||||||
MemberIterator m = FindMember(name);
|
MemberIterator m = FindMember(name);
|
||||||
if (m != MemberEnd()) {
|
if (m != MemberEnd()) {
|
||||||
RemoveMember(m);
|
RemoveMember(m);
|
||||||
@ -1352,8 +1360,8 @@ int z = a[0u].GetInt(); // This works too.
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
template <typename, typename, typename>
|
template <typename, typename> friend class GenericValue;
|
||||||
friend class GenericDocument;
|
template <typename, typename, typename> friend class GenericDocument;
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
kBoolFlag = 0x100,
|
kBoolFlag = 0x100,
|
||||||
@ -1477,7 +1485,8 @@ private:
|
|||||||
rhs.flags_ = kNullFlag;
|
rhs.flags_ = kNullFlag;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool StringEqual(const GenericValue& rhs) const {
|
template <typename SourceAllocator>
|
||||||
|
bool StringEqual(const GenericValue<Encoding, SourceAllocator>& rhs) const {
|
||||||
RAPIDJSON_ASSERT(IsString());
|
RAPIDJSON_ASSERT(IsString());
|
||||||
RAPIDJSON_ASSERT(rhs.IsString());
|
RAPIDJSON_ASSERT(rhs.IsString());
|
||||||
return data_.s.length == rhs.data_.s.length &&
|
return data_.s.length == rhs.data_.s.length &&
|
||||||
@ -1671,7 +1680,7 @@ private:
|
|||||||
|
|
||||||
// callers of the following private Handler functions
|
// callers of the following private Handler functions
|
||||||
template <typename,typename,typename> friend class GenericReader; // for parsing
|
template <typename,typename,typename> friend class GenericReader; // for parsing
|
||||||
friend class GenericValue<Encoding,Allocator>; // for deep copying
|
template <typename, typename> friend class GenericValue; // for deep copying
|
||||||
|
|
||||||
// Implementation of Handler
|
// Implementation of Handler
|
||||||
bool Null() { new (stack_.template Push<ValueType>()) ValueType(); return true; }
|
bool Null() { new (stack_.template Push<ValueType>()) ValueType(); return true; }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user