Cosmetic improvements for rapidjson_adapter.hpp

This commit is contained in:
Tristan Penman 2020-07-05 22:02:55 +10:00
parent d7901d4858
commit 508bc019ec

View File

@ -64,10 +64,10 @@ class GenericRapidJsonObjectMemberIterator;
/// Container for a property name and an associated RapidJson value
template<class ValueType = rapidjson::Value>
class GenericRapidJsonObjectMember :
public std::pair<std::string, GenericRapidJsonAdapter<ValueType> >
public std::pair<std::string, GenericRapidJsonAdapter<ValueType>>
{
private:
typedef std::pair<std::string, GenericRapidJsonAdapter<ValueType> > Super;
typedef std::pair<std::string, GenericRapidJsonAdapter<ValueType>> Super;
public:
GenericRapidJsonObjectMember(
@ -97,7 +97,7 @@ public:
/// Construct a RapidJsonArray referencing an empty array singleton.
GenericRapidJsonArray()
: value(emptyArray()) { }
: m_value(emptyArray()) { }
/**
* @brief Construct a RapidJsonArray referencing a specific RapidJson
@ -109,7 +109,7 @@ public:
* an array.
*/
GenericRapidJsonArray(const ValueType &value)
: value(value)
: m_value(value)
{
if (!value.IsArray()) {
throw std::runtime_error("Value is not an array.");
@ -125,7 +125,7 @@ public:
/// Return the number of elements in the array
size_t size() const
{
return value.Size();
return m_value.Size();
}
private:
@ -142,7 +142,7 @@ private:
}
/// Reference to the contained value
const ValueType &value;
const ValueType &m_value;
};
/**
@ -166,7 +166,7 @@ public:
/// Construct a GenericRapidJsonObject referencing an empty object singleton.
GenericRapidJsonObject()
: value(emptyObject()) { }
: m_value(emptyObject()) { }
/**
* @brief Construct a GenericRapidJsonObject referencing a specific
@ -178,7 +178,7 @@ public:
* an object.
*/
GenericRapidJsonObject(const ValueType &value)
: value(value)
: m_value(value)
{
if (!value.IsObject()) {
throw std::runtime_error("Value is not an object.");
@ -216,7 +216,7 @@ public:
/// Returns the number of members belonging to this object.
size_t size() const
{
return value.MemberEnd() - value.MemberBegin();
return m_value.MemberEnd() - m_value.MemberBegin();
}
private:
@ -233,7 +233,7 @@ private:
}
/// Reference to the contained object
const ValueType &value;
const ValueType &m_value;
};
/**
@ -254,12 +254,12 @@ public:
explicit GenericRapidJsonFrozenValue(const char *str)
{
value.SetString(str, allocator);
m_value.SetString(str, m_allocator);
}
explicit GenericRapidJsonFrozenValue(const std::string &str)
{
value.SetString(str.c_str(), (unsigned int)str.length(), allocator);
m_value.SetString(str.c_str(), (unsigned int)str.length(), m_allocator);
}
/**
@ -269,17 +269,17 @@ public:
*/
explicit GenericRapidJsonFrozenValue(const ValueType &source)
{
if (!copy(source, value, allocator)) {
if (!copy(source, m_value, m_allocator)) {
throw std::runtime_error("Failed to copy ValueType");
}
}
virtual FrozenValue * clone() const
FrozenValue * clone() const override
{
return new GenericRapidJsonFrozenValue(value);
return new GenericRapidJsonFrozenValue(m_value);
}
virtual bool equalTo(const Adapter &other, bool strict) const;
bool equalTo(const Adapter &other, bool strict) const override;
private:
@ -311,8 +311,7 @@ private:
return true;
case rapidjson::kObjectType:
dest.SetObject();
for (typename ValueType::ConstMemberIterator itr = source.MemberBegin();
itr != source.MemberEnd(); ++itr) {
for (typename ValueType::ConstMemberIterator itr = source.MemberBegin(); itr != source.MemberEnd(); ++itr) {
ValueType name(itr->name.GetString(), itr->name.GetStringLength(), allocator);
ValueType value;
copy(itr->value, value, allocator);
@ -351,10 +350,10 @@ private:
}
/// Local memory allocator for RapidJson value
typename ValueType::AllocatorType allocator;
typename ValueType::AllocatorType m_allocator;
/// Local RapidJson value
ValueType value;
ValueType m_value;
};
/**
@ -377,11 +376,11 @@ class GenericRapidJsonValue
public:
/// Construct a wrapper for the empty object singleton
GenericRapidJsonValue()
: value(emptyObject()) { }
: m_value(emptyObject()) { }
/// Construct a wrapper for a specific RapidJson value
GenericRapidJsonValue(const ValueType &value)
: value(value) { }
: m_value(value) { }
/**
* @brief Create a new GenericRapidJsonFrozenValue instance that contains
@ -392,7 +391,7 @@ public:
*/
FrozenValue * freeze() const
{
return new GenericRapidJsonFrozenValue<ValueType>(value);
return new GenericRapidJsonFrozenValue<ValueType>(m_value);
}
/**
@ -404,13 +403,13 @@ public:
*
* Otherwise it will return an empty optional.
*/
opt::optional<GenericRapidJsonArray<ValueType> > getArrayOptional() const
opt::optional<GenericRapidJsonArray<ValueType>> getArrayOptional() const
{
if (value.IsArray()) {
return opt::make_optional(GenericRapidJsonArray<ValueType>(value));
if (m_value.IsArray()) {
return opt::make_optional(GenericRapidJsonArray<ValueType>(m_value));
}
return opt::optional<GenericRapidJsonArray<ValueType> >();
return {};
}
/**
@ -426,8 +425,8 @@ public:
*/
bool getArraySize(size_t &result) const
{
if (value.IsArray()) {
result = value.Size();
if (m_value.IsArray()) {
result = m_value.Size();
return true;
}
@ -436,8 +435,8 @@ public:
bool getBool(bool &result) const
{
if (value.IsBool()) {
result = value.GetBool();
if (m_value.IsBool()) {
result = m_value.GetBool();
return true;
}
@ -446,8 +445,8 @@ public:
bool getDouble(double &result) const
{
if (value.IsDouble()) {
result = value.GetDouble();
if (m_value.IsDouble()) {
result = m_value.GetDouble();
return true;
}
@ -456,17 +455,17 @@ public:
bool getInteger(int64_t &result) const
{
if (value.IsInt()) {
result = value.GetInt();
if (m_value.IsInt()) {
result = m_value.GetInt();
return true;
} else if (value.IsInt64()) {
result = value.GetInt64();
} else if (m_value.IsInt64()) {
result = m_value.GetInt64();
return true;
} else if (value.IsUint()) {
result = static_cast<int64_t>(value.GetUint());
} else if (m_value.IsUint()) {
result = static_cast<int64_t>(m_value.GetUint());
return true;
} else if (value.IsUint64()) {
result = static_cast<int64_t>(value.GetUint64());
} else if (m_value.IsUint64()) {
result = static_cast<int64_t>(m_value.GetUint64());
return true;
}
@ -482,13 +481,13 @@ public:
*
* Otherwise it will return an empty optional.
*/
opt::optional<GenericRapidJsonObject<ValueType> > getObjectOptional() const
opt::optional<GenericRapidJsonObject<ValueType>> getObjectOptional() const
{
if (value.IsObject()) {
return opt::make_optional(GenericRapidJsonObject<ValueType>(value));
if (m_value.IsObject()) {
return opt::make_optional(GenericRapidJsonObject<ValueType>(m_value));
}
return opt::optional<GenericRapidJsonObject<ValueType> >();
return {};
}
/**
@ -504,8 +503,8 @@ public:
*/
bool getObjectSize(size_t &result) const
{
if (value.IsObject()) {
result = value.MemberEnd() - value.MemberBegin();
if (m_value.IsObject()) {
result = m_value.MemberEnd() - m_value.MemberBegin();
return true;
}
@ -514,8 +513,8 @@ public:
bool getString(std::string &result) const
{
if (value.IsString()) {
result.assign(value.GetString(), value.GetStringLength());
if (m_value.IsString()) {
result.assign(m_value.GetString(), m_value.GetStringLength());
return true;
}
@ -529,43 +528,42 @@ public:
bool isArray() const
{
return value.IsArray();
return m_value.IsArray();
}
bool isBool() const
{
return value.IsBool();
return m_value.IsBool();
}
bool isDouble() const
{
return value.IsDouble();
return m_value.IsDouble();
}
bool isInteger() const
{
return value.IsInt() || value.IsInt64() || value.IsUint() ||
value.IsUint64();
return m_value.IsInt() || m_value.IsInt64() || m_value.IsUint() || m_value.IsUint64();
}
bool isNull() const
{
return value.IsNull();
return m_value.IsNull();
}
bool isNumber() const
{
return value.IsNumber();
return m_value.IsNumber();
}
bool isObject() const
{
return value.IsObject();
return m_value.IsObject();
}
bool isString() const
{
return value.IsString();
return m_value.IsString();
}
private:
@ -578,7 +576,7 @@ private:
}
/// Reference to the contained RapidJson value.
const ValueType &value;
const ValueType &m_value;
};
/**
@ -596,7 +594,7 @@ class GenericRapidJsonAdapter:
GenericRapidJsonArray<ValueType>,
GenericRapidJsonObjectMember<ValueType>,
GenericRapidJsonObject<ValueType>,
GenericRapidJsonValue<ValueType> >
GenericRapidJsonValue<ValueType>>
{
public:
@ -606,7 +604,7 @@ public:
GenericRapidJsonArray<ValueType>,
GenericRapidJsonObjectMember<ValueType>,
GenericRapidJsonObject<ValueType>,
GenericRapidJsonValue<ValueType> >() { }
GenericRapidJsonValue<ValueType>>() { }
/// Construct a RapidJsonAdapter containing a specific RapidJson value
GenericRapidJsonAdapter(const ValueType &value)
@ -614,7 +612,7 @@ public:
GenericRapidJsonArray<ValueType>,
GenericRapidJsonObjectMember<ValueType>,
GenericRapidJsonObject<ValueType>,
GenericRapidJsonValue<ValueType> >(value) { }
GenericRapidJsonValue<ValueType>>(value) { }
};
/**
@ -630,7 +628,7 @@ template<class ValueType>
class GenericRapidJsonArrayValueIterator:
public std::iterator<
std::bidirectional_iterator_tag, // bi-directional iterator
GenericRapidJsonAdapter<ValueType> > // value type
GenericRapidJsonAdapter<ValueType>> // value type
{
public:
@ -642,19 +640,19 @@ public:
*/
GenericRapidJsonArrayValueIterator(
const typename ValueType::ConstValueIterator &itr)
: itr(itr) { }
: m_itr(itr) { }
/// Returns a GenericRapidJsonAdapter that contains the value of the current
/// element.
GenericRapidJsonAdapter<ValueType> operator*() const
{
return GenericRapidJsonAdapter<ValueType>(*itr);
return GenericRapidJsonAdapter<ValueType>(*m_itr);
}
/// Returns a proxy for the value of the current element
DerefProxy<GenericRapidJsonAdapter<ValueType> > operator->() const
DerefProxy<GenericRapidJsonAdapter<ValueType>> operator->() const
{
return DerefProxy<GenericRapidJsonAdapter<ValueType> >(**this);
return DerefProxy<GenericRapidJsonAdapter<ValueType>>(**this);
}
/**
@ -670,47 +668,47 @@ public:
*/
bool operator==(const GenericRapidJsonArrayValueIterator<ValueType> &other) const
{
return itr == other.itr;
return m_itr == other.m_itr;
}
bool operator!=(const GenericRapidJsonArrayValueIterator<ValueType>& other) const
{
return !(itr == other.itr);
return m_itr != other.m_itr;
}
GenericRapidJsonArrayValueIterator<ValueType>& operator++()
{
itr++;
m_itr++;
return *this;
}
GenericRapidJsonArrayValueIterator<ValueType> operator++(int) {
GenericRapidJsonArrayValueIterator<ValueType> iterator_pre(itr);
GenericRapidJsonArrayValueIterator<ValueType> iterator_pre(m_itr);
++(*this);
return iterator_pre;
}
GenericRapidJsonArrayValueIterator<ValueType>& operator--()
{
itr--;
m_itr--;
return *this;
}
void advance(std::ptrdiff_t n)
{
itr += n;
m_itr += n;
}
std::ptrdiff_t difference(const GenericRapidJsonArrayValueIterator<ValueType> &other)
{
return std::distance(itr, other.itr);
return std::distance(m_itr, other.itr);
}
private:
typename ValueType::ConstValueIterator itr;
typename ValueType::ConstValueIterator m_itr;
};
/**
@ -727,7 +725,7 @@ template<class ValueType>
class GenericRapidJsonObjectMemberIterator:
public std::iterator<
std::bidirectional_iterator_tag, // bi-directional iterator
GenericRapidJsonObjectMember<ValueType> > // value type
GenericRapidJsonObjectMember<ValueType>> // value type
{
public:
@ -738,7 +736,7 @@ public:
*/
GenericRapidJsonObjectMemberIterator(
const typename ValueType::ConstMemberIterator &itr)
: itr(itr) { }
: m_itr(itr) { }
/**
@ -748,14 +746,14 @@ public:
GenericRapidJsonObjectMember<ValueType> operator*() const
{
return GenericRapidJsonObjectMember<ValueType>(
std::string(itr->name.GetString(), itr->name.GetStringLength()),
itr->value);
std::string(m_itr->name.GetString(), m_itr->name.GetStringLength()),
m_itr->value);
}
/// Returns a proxy for the value of the current element
DerefProxy<GenericRapidJsonObjectMember<ValueType> > operator->() const
DerefProxy<GenericRapidJsonObjectMember<ValueType>> operator->() const
{
return DerefProxy<GenericRapidJsonObjectMember<ValueType> >(**this);
return DerefProxy<GenericRapidJsonObjectMember<ValueType>>(**this);
}
/**
@ -771,85 +769,77 @@ public:
*/
bool operator==(const GenericRapidJsonObjectMemberIterator<ValueType> &other) const
{
return itr == other.itr;
return m_itr == other.m_itr;
}
bool operator!=(const GenericRapidJsonObjectMemberIterator<ValueType> &other) const
{
return !(itr == other.itr);
return m_itr != other.m_itr;
}
GenericRapidJsonObjectMemberIterator<ValueType>& operator++()
{
itr++;
m_itr++;
return *this;
}
GenericRapidJsonObjectMemberIterator<ValueType> operator++(int)
{
GenericRapidJsonObjectMemberIterator<ValueType> iterator_pre(itr);
GenericRapidJsonObjectMemberIterator<ValueType> iterator_pre(m_itr);
++(*this);
return iterator_pre;
}
GenericRapidJsonObjectMemberIterator<ValueType>& operator--()
{
itr--;
m_itr--;
return *this;
}
std::ptrdiff_t difference(const GenericRapidJsonObjectMemberIterator &other)
{
return std::distance(itr, other.itr);
return std::distance(m_itr, other.itr);
}
private:
/// Iternal copy of the original RapidJson iterator
typename ValueType::ConstMemberIterator itr;
typename ValueType::ConstMemberIterator m_itr;
};
template<class ValueType>
inline bool GenericRapidJsonFrozenValue<ValueType>::equalTo(
const Adapter &other, bool strict) const
inline bool GenericRapidJsonFrozenValue<ValueType>::equalTo(const Adapter &other, bool strict) const
{
return GenericRapidJsonAdapter<ValueType>(value).equalTo(other, strict);
return GenericRapidJsonAdapter<ValueType>(m_value).equalTo(other, strict);
}
template<class ValueType>
inline typename GenericRapidJsonArray<ValueType>::iterator
GenericRapidJsonArray<ValueType>::begin() const
inline typename GenericRapidJsonArray<ValueType>::iterator GenericRapidJsonArray<ValueType>::begin() const
{
return value.Begin();
return m_value.Begin();
}
template<class ValueType>
inline typename GenericRapidJsonArray<ValueType>::iterator
GenericRapidJsonArray<ValueType>::end() const
inline typename GenericRapidJsonArray<ValueType>::iterator GenericRapidJsonArray<ValueType>::end() const
{
return value.End();
return m_value.End();
}
template<class ValueType>
inline typename GenericRapidJsonObject<ValueType>::iterator GenericRapidJsonObject<ValueType>::begin() const
{
return m_value.MemberBegin();
}
template<class ValueType>
inline typename GenericRapidJsonObject<ValueType>::iterator GenericRapidJsonObject<ValueType>::end() const
{
return m_value.MemberEnd();
}
template<class ValueType>
inline typename GenericRapidJsonObject<ValueType>::iterator
GenericRapidJsonObject<ValueType>::begin() const
{
return value.MemberBegin();
}
template<class ValueType>
inline typename GenericRapidJsonObject<ValueType>::iterator
GenericRapidJsonObject<ValueType>::end() const
{
return value.MemberEnd();
}
template<class ValueType>
inline typename GenericRapidJsonObject<ValueType>::iterator
GenericRapidJsonObject<ValueType>::find(
const std::string &propertyName) const
GenericRapidJsonObject<ValueType>::find(const std::string &propertyName) const
{
// Hack to support older versions of rapidjson where pointers are used as
// the built in iterator type. In those versions, the FindMember function
@ -866,20 +856,18 @@ inline typename GenericRapidJsonObject<ValueType>::iterator
// properties being compared. We get around this by implementing our
// own linear scan.
const size_t propertyNameLength = propertyName.length();
for (typename ValueType::ConstMemberIterator itr = value.MemberBegin();
itr != value.MemberEnd(); ++itr) {
for (typename ValueType::ConstMemberIterator itr = m_value.MemberBegin(); itr != m_value.MemberEnd(); ++itr) {
const size_t memberNameLength = itr->name.GetStringLength();
if (memberNameLength == propertyNameLength &&
strncmp(itr->name.GetString(), propertyName.c_str(),
itr->name.GetStringLength()) == 0) {
strncmp(itr->name.GetString(), propertyName.c_str(), itr->name.GetStringLength()) == 0) {
return itr;
}
}
return value.MemberEnd();
return m_value.MemberEnd();
}
return value.FindMember(propertyName.c_str()); // Times are good.
return m_value.FindMember(propertyName.c_str()); // Times are good.
}
typedef GenericRapidJsonAdapter<> RapidJsonAdapter;
@ -906,18 +894,16 @@ struct AdapterTraits<valijson::adapters::RapidJsonAdapter>
}
};
typedef rapidjson::GenericValue<rapidjson::UTF8<>, rapidjson::CrtAllocator>
RapidJsonCrt;
typedef rapidjson::GenericValue<rapidjson::UTF8<>, rapidjson::CrtAllocator> RapidJsonCrt;
/**
* @brief Specialisation of the AdapterTraits template struct for a
* RapidJsonAdapter that uses the default CRT allocator
*/
template<>
struct AdapterTraits<valijson::adapters::GenericRapidJsonAdapter<RapidJsonCrt> >
struct AdapterTraits<valijson::adapters::GenericRapidJsonAdapter<RapidJsonCrt>>
{
typedef rapidjson::GenericDocument<rapidjson::UTF8<>,
rapidjson::CrtAllocator> DocumentType;
typedef rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> DocumentType;
static std::string adapterName()
{