Cosmetics improvements for picojson_adapter.hpp

This commit is contained in:
Tristan Penman 2020-07-05 21:49:11 +10:00
parent 3211a04dd1
commit d7901d4858

View File

@ -62,7 +62,7 @@ public:
/// Construct a PicoJsonArray referencing an empty array. /// Construct a PicoJsonArray referencing an empty array.
PicoJsonArray() PicoJsonArray()
: value(emptyArray()) { } : m_value(emptyArray()) { }
/** /**
* @brief Construct a PicoJsonArray referencing a specific PicoJson * @brief Construct a PicoJsonArray referencing a specific PicoJson
@ -74,7 +74,7 @@ public:
* an array. * an array.
*/ */
explicit PicoJsonArray(const picojson::value &value) explicit PicoJsonArray(const picojson::value &value)
: value(value) : m_value(value)
{ {
if (!value.is<picojson::array>()) { if (!value.is<picojson::array>()) {
throw std::runtime_error("Value is not an array."); throw std::runtime_error("Value is not an array.");
@ -100,7 +100,7 @@ public:
/// Return the number of elements in the array /// Return the number of elements in the array
size_t size() const size_t size() const
{ {
const picojson::array &array = value.get<picojson::array>(); const picojson::array &array = m_value.get<picojson::array>();
return array.size(); return array.size();
} }
@ -118,7 +118,7 @@ private:
} }
/// Reference to the contained value /// Reference to the contained value
const picojson::value &value; const picojson::value &m_value;
}; };
/** /**
@ -141,7 +141,7 @@ public:
/// Construct a PicoJsonObject referencing an empty object singleton. /// Construct a PicoJsonObject referencing an empty object singleton.
PicoJsonObject() PicoJsonObject()
: value(emptyObject()) { } : m_value(emptyObject()) { }
/** /**
* @brief Construct a PicoJsonObject referencing a specific PicoJson * @brief Construct a PicoJsonObject referencing a specific PicoJson
@ -153,7 +153,7 @@ public:
* an object. * an object.
*/ */
PicoJsonObject(const picojson::value &value) PicoJsonObject(const picojson::value &value)
: value(value) : m_value(value)
{ {
if (!value.is<picojson::object>()) { if (!value.is<picojson::object>()) {
throw std::runtime_error("Value is not an object."); throw std::runtime_error("Value is not an object.");
@ -191,7 +191,7 @@ public:
/// Returns the number of members belonging to this object. /// Returns the number of members belonging to this object.
size_t size() const size_t size() const
{ {
const picojson::object &object = value.get<picojson::object>(); const picojson::object &object = m_value.get<picojson::object>();
return object.size(); return object.size();
} }
@ -209,7 +209,7 @@ private:
} }
/// Reference to the contained object /// Reference to the contained object
const picojson::value &value; const picojson::value &m_value;
}; };
/** /**
@ -231,19 +231,19 @@ public:
* @param source the PicoJson value to be copied * @param source the PicoJson value to be copied
*/ */
explicit PicoJsonFrozenValue(const picojson::value &source) explicit PicoJsonFrozenValue(const picojson::value &source)
: value(source) { } : m_value(source) { }
virtual FrozenValue * clone() const FrozenValue * clone() const override
{ {
return new PicoJsonFrozenValue(value); return new PicoJsonFrozenValue(m_value);
} }
virtual bool equalTo(const Adapter &other, bool strict) const; bool equalTo(const Adapter &other, bool strict) const override;
private: private:
/// Stored PicoJson value /// Stored PicoJson value
picojson::value value; picojson::value m_value;
}; };
/** /**
@ -266,11 +266,11 @@ public:
/// Construct a wrapper for the empty object singleton /// Construct a wrapper for the empty object singleton
PicoJsonValue() PicoJsonValue()
: value(emptyObject()) { } : m_value(emptyObject()) { }
/// Construct a wrapper for a specific PicoJson value /// Construct a wrapper for a specific PicoJson value
PicoJsonValue(const picojson::value &value) PicoJsonValue(const picojson::value &value)
: value(value) { } : m_value(value) { }
/** /**
* @brief Create a new PicoJsonFrozenValue instance that contains the * @brief Create a new PicoJsonFrozenValue instance that contains the
@ -281,7 +281,7 @@ public:
*/ */
FrozenValue * freeze() const FrozenValue * freeze() const
{ {
return new PicoJsonFrozenValue(value); return new PicoJsonFrozenValue(m_value);
} }
/** /**
@ -295,11 +295,11 @@ public:
*/ */
opt::optional<PicoJsonArray> getArrayOptional() const opt::optional<PicoJsonArray> getArrayOptional() const
{ {
if (value.is<picojson::array>()) { if (m_value.is<picojson::array>()) {
return opt::make_optional(PicoJsonArray(value)); return opt::make_optional(PicoJsonArray(m_value));
} }
return opt::optional<PicoJsonArray>(); return {};
} }
/** /**
@ -315,8 +315,8 @@ public:
*/ */
bool getArraySize(size_t &result) const bool getArraySize(size_t &result) const
{ {
if (value.is<picojson::array>()) { if (m_value.is<picojson::array>()) {
const picojson::array& array = value.get<picojson::array>(); const picojson::array& array = m_value.get<picojson::array>();
result = array.size(); result = array.size();
return true; return true;
} }
@ -326,8 +326,8 @@ public:
bool getBool(bool &result) const bool getBool(bool &result) const
{ {
if (value.is<bool>()) { if (m_value.is<bool>()) {
result = value.get<bool>(); result = m_value.get<bool>();
return true; return true;
} }
@ -336,8 +336,8 @@ public:
bool getDouble(double &result) const bool getDouble(double &result) const
{ {
if (value.is<double>()) { if (m_value.is<double>()) {
result = value.get<double>(); result = m_value.get<double>();
return true; return true;
} }
@ -346,8 +346,8 @@ public:
bool getInteger(int64_t &result) const bool getInteger(int64_t &result) const
{ {
if (value.is<int64_t>()) { if (m_value.is<int64_t>()) {
result = value.get<int64_t>(); result = m_value.get<int64_t>();
return true; return true;
} }
@ -365,11 +365,11 @@ public:
*/ */
opt::optional<PicoJsonObject> getObjectOptional() const opt::optional<PicoJsonObject> getObjectOptional() const
{ {
if (value.is<picojson::object>()) { if (m_value.is<picojson::object>()) {
return opt::make_optional(PicoJsonObject(value)); return opt::make_optional(PicoJsonObject(m_value));
} }
return opt::optional<PicoJsonObject>(); return {};
} }
/** /**
@ -385,8 +385,8 @@ public:
*/ */
bool getObjectSize(size_t &result) const bool getObjectSize(size_t &result) const
{ {
if (value.is<picojson::object>()) { if (m_value.is<picojson::object>()) {
const picojson::object &object = value.get<picojson::object>(); const picojson::object &object = m_value.get<picojson::object>();
result = object.size(); result = object.size();
return true; return true;
} }
@ -396,8 +396,8 @@ public:
bool getString(std::string &result) const bool getString(std::string &result) const
{ {
if (value.is<std::string>()) { if (m_value.is<std::string>()) {
result = value.get<std::string>(); result = m_value.get<std::string>();
return true; return true;
} }
@ -411,46 +411,46 @@ public:
bool isArray() const bool isArray() const
{ {
return value.is<picojson::array>(); return m_value.is<picojson::array>();
} }
bool isBool() const bool isBool() const
{ {
return value.is<bool>(); return m_value.is<bool>();
} }
bool isDouble() const bool isDouble() const
{ {
if (value.is<int64_t>()) { if (m_value.is<int64_t>()) {
return false; return false;
} }
return value.is<double>(); return m_value.is<double>();
} }
bool isInteger() const bool isInteger() const
{ {
return value.is<int64_t>(); return m_value.is<int64_t>();
} }
bool isNull() const bool isNull() const
{ {
return value.is<picojson::null>(); return m_value.is<picojson::null>();
} }
bool isNumber() const bool isNumber() const
{ {
return value.is<double>(); return m_value.is<double>();
} }
bool isObject() const bool isObject() const
{ {
return value.is<picojson::object>(); return m_value.is<picojson::object>();
} }
bool isString() const bool isString() const
{ {
return value.is<std::string>(); return m_value.is<std::string>();
} }
private: private:
@ -463,7 +463,7 @@ private:
} }
/// Reference to the contained PicoJson value. /// Reference to the contained PicoJson value.
const picojson::value &value; const picojson::value &m_value;
}; };
/** /**
@ -515,15 +515,14 @@ public:
* *
* @param itr PicoJson iterator to store * @param itr PicoJson iterator to store
*/ */
PicoJsonArrayValueIterator( PicoJsonArrayValueIterator(const picojson::array::const_iterator &itr)
const picojson::array::const_iterator &itr) : m_itr(itr) { }
: itr(itr) { }
/// Returns a PicoJsonAdapter that contains the value of the current /// Returns a PicoJsonAdapter that contains the value of the current
/// element. /// element.
PicoJsonAdapter operator*() const PicoJsonAdapter operator*() const
{ {
return PicoJsonAdapter(*itr); return PicoJsonAdapter(*m_itr);
} }
DerefProxy<PicoJsonAdapter> operator->() const DerefProxy<PicoJsonAdapter> operator->() const
@ -544,43 +543,43 @@ public:
*/ */
bool operator==(const PicoJsonArrayValueIterator &other) const bool operator==(const PicoJsonArrayValueIterator &other) const
{ {
return itr == other.itr; return m_itr == other.m_itr;
} }
bool operator!=(const PicoJsonArrayValueIterator &other) const bool operator!=(const PicoJsonArrayValueIterator &other) const
{ {
return !(itr == other.itr); return !(m_itr == other.m_itr);
} }
const PicoJsonArrayValueIterator& operator++() const PicoJsonArrayValueIterator& operator++()
{ {
itr++; m_itr++;
return *this; return *this;
} }
PicoJsonArrayValueIterator operator++(int) PicoJsonArrayValueIterator operator++(int)
{ {
PicoJsonArrayValueIterator iterator_pre(itr); PicoJsonArrayValueIterator iterator_pre(m_itr);
++(*this); ++(*this);
return iterator_pre; return iterator_pre;
} }
const PicoJsonArrayValueIterator& operator--() const PicoJsonArrayValueIterator& operator--()
{ {
itr--; m_itr--;
return *this; return *this;
} }
void advance(std::ptrdiff_t n) void advance(std::ptrdiff_t n)
{ {
itr += n; m_itr += n;
} }
private: private:
picojson::array::const_iterator itr; picojson::array::const_iterator m_itr;
}; };
/** /**
@ -605,9 +604,8 @@ public:
* *
* @param itr PicoJson iterator to store * @param itr PicoJson iterator to store
*/ */
PicoJsonObjectMemberIterator( PicoJsonObjectMemberIterator(const picojson::object::const_iterator &itr)
const picojson::object::const_iterator &itr) : m_itr(itr) { }
: itr(itr) { }
/** /**
* @brief Returns a PicoJsonObjectMember that contains the key and value * @brief Returns a PicoJsonObjectMember that contains the key and value
@ -615,7 +613,7 @@ public:
*/ */
PicoJsonObjectMember operator*() const PicoJsonObjectMember operator*() const
{ {
return PicoJsonObjectMember(itr->first, itr->second); return PicoJsonObjectMember(m_itr->first, m_itr->second);
} }
DerefProxy<PicoJsonObjectMember> operator->() const DerefProxy<PicoJsonObjectMember> operator->() const
@ -636,31 +634,31 @@ public:
*/ */
bool operator==(const PicoJsonObjectMemberIterator &other) const bool operator==(const PicoJsonObjectMemberIterator &other) const
{ {
return itr == other.itr; return m_itr == other.m_itr;
} }
bool operator!=(const PicoJsonObjectMemberIterator &other) const bool operator!=(const PicoJsonObjectMemberIterator &other) const
{ {
return !(itr == other.itr); return !(m_itr == other.m_itr);
} }
const PicoJsonObjectMemberIterator& operator++() const PicoJsonObjectMemberIterator& operator++()
{ {
itr++; m_itr++;
return *this; return *this;
} }
PicoJsonObjectMemberIterator operator++(int) PicoJsonObjectMemberIterator operator++(int)
{ {
PicoJsonObjectMemberIterator iterator_pre(itr); PicoJsonObjectMemberIterator iterator_pre(m_itr);
++(*this); ++(*this);
return iterator_pre; return iterator_pre;
} }
const PicoJsonObjectMemberIterator& operator--(int) const PicoJsonObjectMemberIterator& operator--(int)
{ {
itr--; m_itr--;
return *this; return *this;
} }
@ -668,7 +666,7 @@ public:
private: private:
/// Iternal copy of the original PicoJson iterator /// Iternal copy of the original PicoJson iterator
picojson::object::const_iterator itr; picojson::object::const_iterator m_itr;
}; };
/// Specialisation of the AdapterTraits template struct for PicoJsonAdapter. /// Specialisation of the AdapterTraits template struct for PicoJsonAdapter.
@ -685,37 +683,37 @@ struct AdapterTraits<valijson::adapters::PicoJsonAdapter>
inline bool PicoJsonFrozenValue::equalTo(const Adapter &other, bool strict) const inline bool PicoJsonFrozenValue::equalTo(const Adapter &other, bool strict) const
{ {
return PicoJsonAdapter(value).equalTo(other, strict); return PicoJsonAdapter(m_value).equalTo(other, strict);
} }
inline PicoJsonArrayValueIterator PicoJsonArray::begin() const inline PicoJsonArrayValueIterator PicoJsonArray::begin() const
{ {
const picojson::array &array = value.get<picojson::array>(); const picojson::array &array = m_value.get<picojson::array>();
return array.begin(); return array.begin();
} }
inline PicoJsonArrayValueIterator PicoJsonArray::end() const inline PicoJsonArrayValueIterator PicoJsonArray::end() const
{ {
const picojson::array &array = value.get<picojson::array>(); const picojson::array &array = m_value.get<picojson::array>();
return array.end(); return array.end();
} }
inline PicoJsonObjectMemberIterator PicoJsonObject::begin() const inline PicoJsonObjectMemberIterator PicoJsonObject::begin() const
{ {
const picojson::object &object = value.get<picojson::object>(); const picojson::object &object = m_value.get<picojson::object>();
return object.begin(); return object.begin();
} }
inline PicoJsonObjectMemberIterator PicoJsonObject::end() const inline PicoJsonObjectMemberIterator PicoJsonObject::end() const
{ {
const picojson::object &object = value.get<picojson::object>(); const picojson::object &object = m_value.get<picojson::object>();
return object.end(); return object.end();
} }
inline PicoJsonObjectMemberIterator PicoJsonObject::find( inline PicoJsonObjectMemberIterator PicoJsonObject::find(
const std::string &propertyName) const const std::string &propertyName) const
{ {
const picojson::object &object = value.get<picojson::object>(); const picojson::object &object = m_value.get<picojson::object>();
return object.find(propertyName); return object.find(propertyName);
} }