Usage modern C++ features on JSON modules (enhanced) (#4613)

This commit is contained in:
Matej Kenda 2024-07-25 18:18:37 +02:00 committed by GitHub
parent 891c1e03bf
commit 5117e27515
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
19 changed files with 286 additions and 368 deletions

View File

@ -217,7 +217,7 @@ public:
private:
void resetDynArray() const;
typedef SharedPtr<Poco::Dynamic::Array> ArrayPtr;
using ArrayPtr = SharedPtr<Poco::Dynamic::Array>;
ValueVec _values;
mutable ArrayPtr _pArray;
@ -340,98 +340,96 @@ public:
{
}
~VarHolderImpl()
{
}
~VarHolderImpl() override = default;
const std::type_info& type() const
const std::type_info& type() const override
{
return typeid(JSON::Array::Ptr);
}
void convert(Int8&) const
void convert(Int8&) const override
{
throw BadCastException();
}
void convert(Int16&) const
void convert(Int16&) const override
{
throw BadCastException();
}
void convert(Int32&) const
void convert(Int32&) const override
{
throw BadCastException();
}
void convert(Int64&) const
void convert(Int64&) const override
{
throw BadCastException();
}
void convert(UInt8&) const
void convert(UInt8&) const override
{
throw BadCastException();
}
void convert(UInt16&) const
void convert(UInt16&) const override
{
throw BadCastException();
}
void convert(UInt32&) const
void convert(UInt32&) const override
{
throw BadCastException();
}
void convert(UInt64&) const
void convert(UInt64&) const override
{
throw BadCastException();
}
void convert(bool& value) const
void convert(bool& value) const override
{
value = !_val.isNull() && _val->size() > 0;
}
void convert(float&) const
void convert(float&) const override
{
throw BadCastException();
}
void convert(double&) const
void convert(double&) const override
{
throw BadCastException();
}
void convert(char&) const
void convert(char&) const override
{
throw BadCastException();
}
void convert(std::string& s) const
void convert(std::string& s) const override
{
std::ostringstream oss;
_val->stringify(oss);
s = oss.str();
}
void convert(DateTime& /*val*/) const
void convert(DateTime& /*val*/) const override
{
throw BadCastException("Cannot convert Array to DateTime");
}
void convert(LocalDateTime& /*ldt*/) const
void convert(LocalDateTime& /*ldt*/) const override
{
throw BadCastException("Cannot convert Array to LocalDateTime");
}
void convert(Timestamp& /*ts*/) const
void convert(Timestamp& /*ts*/) const override
{
throw BadCastException("Cannot convert Array to Timestamp");
}
VarHolder* clone(Placeholder<VarHolder>* pVarHolder = 0) const
VarHolder* clone(Placeholder<VarHolder>* pVarHolder = nullptr) const override
{
return cloneHolder(pVarHolder, _val);
}
@ -441,22 +439,22 @@ public:
return _val;
}
bool isInteger() const
bool isInteger() const override
{
return false;
}
bool isSigned() const
bool isSigned() const override
{
return false;
}
bool isNumeric() const
bool isNumeric() const override
{
return false;
}
bool isString() const
bool isString() const override
{
return false;
}
@ -474,98 +472,96 @@ public:
{
}
~VarHolderImpl()
{
}
~VarHolderImpl() override = default;
const std::type_info& type() const
const std::type_info& type() const override
{
return typeid(JSON::Array);
}
void convert(Int8&) const
void convert(Int8&) const override
{
throw BadCastException();
}
void convert(Int16&) const
void convert(Int16&) const override
{
throw BadCastException();
}
void convert(Int32&) const
void convert(Int32&) const override
{
throw BadCastException();
}
void convert(Int64&) const
void convert(Int64&) const override
{
throw BadCastException();
}
void convert(UInt8&) const
void convert(UInt8&) const override
{
throw BadCastException();
}
void convert(UInt16&) const
void convert(UInt16&) const override
{
throw BadCastException();
}
void convert(UInt32&) const
void convert(UInt32&) const override
{
throw BadCastException();
}
void convert(UInt64&) const
void convert(UInt64&) const override
{
throw BadCastException();
}
void convert(bool& value) const
void convert(bool& value) const override
{
value = _val.size() > 0;
}
void convert(float&) const
void convert(float&) const override
{
throw BadCastException();
}
void convert(double&) const
void convert(double&) const override
{
throw BadCastException();
}
void convert(char&) const
void convert(char&) const override
{
throw BadCastException();
}
void convert(std::string& s) const
void convert(std::string& s) const override
{
std::ostringstream oss;
_val.stringify(oss);
s = oss.str();
}
void convert(DateTime& /*val*/) const
void convert(DateTime& /*val*/) const override
{
throw BadCastException("Cannot convert Array to DateTime");
}
void convert(LocalDateTime& /*ldt*/) const
void convert(LocalDateTime& /*ldt*/) const override
{
throw BadCastException("Cannot convert Array to LocalDateTime");
}
void convert(Timestamp& /*ts*/) const
void convert(Timestamp& /*ts*/) const override
{
throw BadCastException("Cannot convert Array to Timestamp");
}
VarHolder* clone(Placeholder<VarHolder>* pVarHolder = 0) const
VarHolder* clone(Placeholder<VarHolder>* pVarHolder = nullptr) const override
{
return cloneHolder(pVarHolder, _val);
}
@ -575,22 +571,22 @@ public:
return _val;
}
bool isInteger() const
bool isInteger() const override
{
return false;
}
bool isSigned() const
bool isSigned() const override
{
return false;
}
bool isNumeric() const
bool isNumeric() const override
{
return false;
}
bool isString() const
bool isString() const override
{
return false;
}

View File

@ -188,7 +188,7 @@ public:
bool isArray(const std::string& key) const;
/// Returns true when the given property contains an array.
bool isArray(ConstIterator& it) const;
bool isArray(const ConstIterator& it) const;
/// Returns true when the given property contains an array.
bool isNull(const std::string& key) const;
@ -197,7 +197,7 @@ public:
bool isObject(const std::string& key) const;
/// Returns true when the given property contains an object.
bool isObject(ConstIterator& it) const;
bool isObject(const ConstIterator& it) const;
/// Returns true when the given property contains an object.
template<typename T>
@ -207,7 +207,7 @@ public:
/// def will be returned.
{
T value = def;
ValueMap::const_iterator it = _values.find(key);
auto it = _values.find(key);
if (it != _values.end() && ! it->second.isEmpty())
{
try
@ -255,9 +255,9 @@ public:
/// Insertion order preservation property is left intact.
private:
typedef std::deque<ValueMap::const_iterator> KeyList;
typedef Poco::DynamicStruct::Ptr StructPtr;
typedef Poco::OrderedDynamicStruct::Ptr OrdStructPtr;
using KeyList = std::deque<ValueMap::const_iterator>;
using StructPtr = Poco::DynamicStruct::Ptr;
using OrdStructPtr = Poco::OrderedDynamicStruct::Ptr;
void syncKeys(const KeyList& keys);
@ -311,30 +311,28 @@ private:
if (obj->_preserveInsOrder)
{
KeyList::const_iterator it = obj->_keys.begin();
KeyList::const_iterator end = obj->_keys.end();
for (; it != end; ++it)
for (const auto& it: obj->_keys)
{
if (obj->isObject((*it)->first))
if (obj->isObject(it->first))
{
Object::Ptr pObj = obj->getObject((*it)->first);
Object::Ptr pObj = obj->getObject(it->first);
S str = makeStructImpl<S>(pObj);
ds.insert((*it)->first, str);
ds.insert(it->first, str);
}
else if (obj->isArray((*it)->first))
else if (obj->isArray(it->first))
{
Array::Ptr pArr = obj->getArray((*it)->first);
Array::Ptr pArr = obj->getArray(it->first);
std::vector<Poco::Dynamic::Var> v = Poco::JSON::Array::makeArray(pArr);
ds.insert((*it)->first, v);
ds.insert(it->first, v);
}
else
ds.insert((*it)->first, (*it)->second);
ds.insert(it->first, it->second);
}
}
else
{
ConstIterator it = obj->begin();
ConstIterator end = obj->end();
auto it = obj->begin();
const auto end = obj->end();
for (; it != end; ++it)
{
if (obj->isObject(it))
@ -430,19 +428,19 @@ inline Object::ConstIterator Object::end() const
inline bool Object::has(const std::string& key) const
{
ValueMap::const_iterator it = _values.find(key);
const auto it = _values.find(key);
return it != _values.end();
}
inline bool Object::isArray(const std::string& key) const
{
ValueMap::const_iterator it = _values.find(key);
const auto it = _values.find(key);
return isArray(it);
}
inline bool Object::isArray(ConstIterator& it) const
inline bool Object::isArray(const ConstIterator& it) const
{
return it != _values.end() && (it->second.type() == typeid(Array::Ptr) || it->second.type() == typeid(Array));
}
@ -450,19 +448,19 @@ inline bool Object::isArray(ConstIterator& it) const
inline bool Object::isNull(const std::string& key) const
{
ValueMap::const_iterator it = _values.find(key);
const auto it = _values.find(key);
return it == _values.end() || it->second.isEmpty();
}
inline bool Object::isObject(const std::string& key) const
{
ValueMap::const_iterator it = _values.find(key);
const auto it = _values.find(key);
return isObject(it);
}
inline bool Object::isObject(ConstIterator& it) const
inline bool Object::isObject(const ConstIterator& it) const
{
return it != _values.end() && (it->second.type() == typeid(Object::Ptr) || it->second.type() == typeid(Object));
}
@ -478,8 +476,8 @@ inline void Object::remove(const std::string& key)
{
if (_preserveInsOrder)
{
KeyList::iterator it = _keys.begin();
KeyList::iterator end = _keys.end();
auto it = _keys.begin();
const auto end = _keys.end();
for (; it != end; ++it)
{
if (key == (*it)->first)
@ -508,7 +506,7 @@ inline const Dynamic::Var& Object::getValue(ValueMap::const_iterator& it) const
inline const Dynamic::Var& Object::getValue(KeyList::const_iterator& it) const
{
ValueMap::const_iterator itv = _values.find((*it)->first);
const auto itv = _values.find((*it)->first);
if (itv != _values.end())
return itv->second;
else
@ -531,101 +529,99 @@ public:
{
}
~VarHolderImpl()
{
}
~VarHolderImpl() override = default;
const std::type_info& type() const
const std::type_info& type() const override
{
return typeid(JSON::Object::Ptr);
}
void convert(Int8&) const
void convert(Int8&) const override
{
throw BadCastException();
}
void convert(Int16&) const
void convert(Int16&) const override
{
throw BadCastException();
}
void convert(Int32&) const
void convert(Int32&) const override
{
throw BadCastException();
}
void convert(Int64&) const
void convert(Int64&) const override
{
throw BadCastException();
}
void convert(UInt8&) const
void convert(UInt8&) const override
{
throw BadCastException();
}
void convert(UInt16&) const
void convert(UInt16&) const override
{
throw BadCastException();
}
void convert(UInt32&) const
void convert(UInt32&) const override
{
throw BadCastException();
}
void convert(UInt64&) const
void convert(UInt64&) const override
{
throw BadCastException();
}
void convert(bool& value) const
void convert(bool& value) const override
{
value = !_val.isNull() && _val->size() > 0;
}
void convert(float&) const
void convert(float&) const override
{
throw BadCastException();
}
void convert(double&) const
void convert(double&) const override
{
throw BadCastException();
}
void convert(char&) const
void convert(char&) const override
{
throw BadCastException();
}
void convert(std::string& s) const
void convert(std::string& s) const override
{
std::ostringstream oss;
_val->stringify(oss);
s = oss.str();
}
void convert(DateTime& /*val*/) const
void convert(DateTime& /*val*/) const override
{
//TODO: val = _val;
throw NotImplementedException("Conversion not implemented: JSON:Object => DateTime");
}
void convert(LocalDateTime& /*ldt*/) const
void convert(LocalDateTime& /*ldt*/) const override
{
//TODO: ldt = _val.timestamp();
throw NotImplementedException("Conversion not implemented: JSON:Object => LocalDateTime");
}
void convert(Timestamp& /*ts*/) const
void convert(Timestamp& /*ts*/) const override
{
//TODO: ts = _val.timestamp();
throw NotImplementedException("Conversion not implemented: JSON:Object => Timestamp");
}
VarHolder* clone(Placeholder<VarHolder>* pVarHolder = 0) const
VarHolder* clone(Placeholder<VarHolder>* pVarHolder = nullptr) const override
{
return cloneHolder(pVarHolder, _val);
}
@ -635,27 +631,27 @@ public:
return _val;
}
bool isArray() const
bool isArray() const override
{
return false;
}
bool isInteger() const
bool isInteger() const override
{
return false;
}
bool isSigned() const
bool isSigned() const override
{
return false;
}
bool isNumeric() const
bool isNumeric() const override
{
return false;
}
bool isString() const
bool isString() const override
{
return false;
}
@ -673,101 +669,99 @@ public:
{
}
~VarHolderImpl()
{
}
~VarHolderImpl() override = default;
const std::type_info& type() const
const std::type_info& type() const override
{
return typeid(JSON::Object);
}
void convert(Int8&) const
void convert(Int8&) const override
{
throw BadCastException();
}
void convert(Int16&) const
void convert(Int16&) const override
{
throw BadCastException();
}
void convert(Int32&) const
void convert(Int32&) const override
{
throw BadCastException();
}
void convert(Int64&) const
void convert(Int64&) const override
{
throw BadCastException();
}
void convert(UInt8&) const
void convert(UInt8&) const override
{
throw BadCastException();
}
void convert(UInt16&) const
void convert(UInt16&) const override
{
throw BadCastException();
}
void convert(UInt32&) const
void convert(UInt32&) const override
{
throw BadCastException();
}
void convert(UInt64&) const
void convert(UInt64&) const override
{
throw BadCastException();
}
void convert(bool& value) const
void convert(bool& value) const override
{
value = _val.size() > 0;
}
void convert(float&) const
void convert(float&) const override
{
throw BadCastException();
}
void convert(double&) const
void convert(double&) const override
{
throw BadCastException();
}
void convert(char&) const
void convert(char&) const override
{
throw BadCastException();
}
void convert(std::string& s) const
void convert(std::string& s) const override
{
std::ostringstream oss;
_val.stringify(oss);
s = oss.str();
}
void convert(DateTime& /*val*/) const
void convert(DateTime& /*val*/) const override
{
//TODO: val = _val;
throw NotImplementedException("Conversion not implemented: JSON:Object => DateTime");
}
void convert(LocalDateTime& /*ldt*/) const
void convert(LocalDateTime& /*ldt*/) const override
{
//TODO: ldt = _val.timestamp();
throw NotImplementedException("Conversion not implemented: JSON:Object => LocalDateTime");
}
void convert(Timestamp& /*ts*/) const
void convert(Timestamp& /*ts*/) const override
{
//TODO: ts = _val.timestamp();
throw NotImplementedException("Conversion not implemented: JSON:Object => Timestamp");
}
VarHolder* clone(Placeholder<VarHolder>* pVarHolder = 0) const
VarHolder* clone(Placeholder<VarHolder>* pVarHolder = nullptr) const override
{
return cloneHolder(pVarHolder, _val);
}
@ -777,27 +771,27 @@ public:
return _val;
}
bool isArray() const
bool isArray() const override
{
return false;
}
bool isInteger() const
bool isInteger() const override
{
return false;
}
bool isSigned() const
bool isSigned() const override
{
return false;
}
bool isNumeric() const
bool isNumeric() const override
{
return false;
}
bool isString() const
bool isString() const override
{
return false;
}

View File

@ -40,61 +40,61 @@ public:
/// inside objects is preserved. Otherwise, items
/// will be sorted by keys.
virtual ~ParseHandler();
~ParseHandler() override;
/// Destroys the ParseHandler.
virtual void reset();
void reset() override;
/// Resets the handler state.
void startObject();
void startObject() override;
/// Handles a '{'; a new object is started.
void endObject();
void endObject() override;
/// Handles a '}'; the object is closed.
void startArray();
void startArray() override;
/// Handles a '['; a new array is started.
void endArray();
void endArray() override;
/// Handles a ']'; the array is closed.
void key(const std::string& k);
void key(const std::string& k) override;
/// A key is read
Dynamic::Var asVar() const;
Dynamic::Var asVar() const override;
/// Returns the result of the parser (an object or an array).
virtual void value(int v);
void value(int v) override;
/// An integer value is read
virtual void value(unsigned v);
void value(unsigned v) override;
/// An unsigned value is read. This will only be triggered if the
/// value cannot fit into a signed int.
#if defined(POCO_HAVE_INT64)
virtual void value(Int64 v);
void value(Int64 v) override;
/// A 64-bit integer value is read
virtual void value(UInt64 v);
void value(UInt64 v) override;
/// An unsigned 64-bit integer value is read. This will only be
/// triggered if the value cannot fit into a signed 64-bit integer.
#endif
virtual void value(const std::string& s);
void value(const std::string& s) override;
/// A string value is read.
virtual void value(double d);
void value(double d) override;
/// A double value is read.
virtual void value(bool b);
void value(bool b) override;
/// A boolean value is read.
virtual void null();
void null() override;
/// A null value is read.
private:
void setValue(const Poco::Dynamic::Var& value);
typedef std::stack<Dynamic::Var> Stack;
using Stack = std::stack<Dynamic::Var>;
Stack _stack;
std::string _key;

View File

@ -20,11 +20,7 @@
#include "Poco/JSON/JSON.h"
#include "Poco/JSON/ParserImpl.h"
#include "Poco/JSON/Object.h"
#include "Poco/JSON/Array.h"
#include "Poco/JSON/ParseHandler.h"
#include "Poco/JSON/JSONException.h"
#include "Poco/UTF8Encoding.h"
#include "Poco/Dynamic/Var.h"
#include <string>
@ -68,7 +64,7 @@ public:
Parser(const Handler::Ptr& pHandler = new ParseHandler);
/// Creates JSON Parser, using the given Handler and buffer size.
virtual ~Parser();
~Parser() override;
/// Destroys JSON Parser.
void reset();

View File

@ -19,11 +19,7 @@
#include "Poco/JSON/JSON.h"
#include "Poco/JSON/Object.h"
#include "Poco/JSON/Array.h"
#include "Poco/JSON/ParseHandler.h"
#include "Poco/JSON/JSONException.h"
#include "Poco/UTF8Encoding.h"
#include "Poco/Dynamic/Var.h"
#include <string>
@ -183,7 +179,7 @@ inline Dynamic::Var ParserImpl::asVarImpl() const
{
if (_pHandler) return _pHandler->asVar();
return Dynamic::Var();
return {};
}

View File

@ -44,56 +44,56 @@ public:
PrintHandler(std::ostream& out, unsigned indent = 0, int options = Poco::JSON_WRAP_STRINGS);
/// Creates the PrintHandler.
~PrintHandler();
~PrintHandler() override;
/// Destroys the PrintHandler.
void reset();
void reset() override;
/// Resets the handler state.
void startObject();
void startObject() override;
/// The parser has read a '{'; a new object is started.
/// If indent is greater than zero, a newline will be appended.
void endObject();
void endObject() override;
/// The parser has read a '}'; the object is closed.
void startArray();
void startArray() override;
/// The parser has read a [; a new array will be started.
/// If indent is greater than zero, a newline will be appended.
void endArray();
void endArray() override;
/// The parser has read a ]; the array is closed.
void key(const std::string& k);
void key(const std::string& k) override;
/// A key of an object is read; it will be written to the output,
/// followed by a ':'. If indent is greater than zero, the colon
/// is padded by a space before and after.
void null();
void null() override;
/// A null value is read; "null" will be written to the output.
void value(int v);
void value(int v) override;
/// An integer value is read.
void value(unsigned v);
void value(unsigned v) override;
/// An unsigned value is read. This will only be triggered if the
/// value cannot fit into a signed int.
#if defined(POCO_HAVE_INT64)
void value(Int64 v);
void value(Int64 v) override;
/// A 64-bit integer value is read; it will be written to the output.
void value(UInt64 v);
void value(UInt64 v) override;
/// An unsigned 64-bit integer value is read; it will be written to the output.
#endif
void value(const std::string& value);
void value(const std::string& value) override;
/// A string value is read; it will be formatted and written to the output.
void value(double d);
void value(double d) override;
/// A double value is read; it will be written to the output.
void value(bool b);
void value(bool b) override;
/// A boolean value is read; it will be written to the output.
void comma();

View File

@ -21,7 +21,6 @@
#include "Poco/JSON/JSON.h"
#include "Poco/JSON/Template.h"
#include "Poco/Path.h"
#include "Poco/SharedPtr.h"
#include "Poco/Logger.h"
#include <vector>
#include <map>

View File

@ -11,7 +11,7 @@
#include "Poco/JSON/Parser.h"
#include "Poco/JSON/ParseHandler.h"
#include "Poco/JSON/Object.h"
#include "Poco/JSON/JSONException.h"
#include "Poco/Environment.h"
#include "Poco/Path.h"
@ -20,7 +20,7 @@
#include "Poco/StreamCopier.h"
#include "Poco/Stopwatch.h"
#include <iostream>
#include <iomanip>
#include <sstream>
int main(int argc, char** argv)

View File

@ -42,14 +42,7 @@ Array::Array(int options):
}
Array::Array(const Array& other) :
_values(other._values),
_pArray(other._pArray),
_modified(other._modified),
_escapeUnicode(other._escapeUnicode),
_lowercaseHex(other._lowercaseHex)
{
}
Array::Array(const Array& other) = default;
Array::Array(Array&& other) noexcept:
@ -88,9 +81,7 @@ Array& Array::operator = (Array&& other) noexcept
}
Array::~Array()
{
}
Array::~Array() = default;
Var Array::get(unsigned int index) const
@ -176,7 +167,7 @@ void Array::stringify(std::ostream& out, unsigned int indent, int step) const
if (indent > 0) out << std::endl;
for (ValueVec::const_iterator it = _values.begin(); it != _values.end();)
for (auto it = _values.begin(); it != _values.end();)
{
for (int i = 0; i < indent; i++) out << ' ';
@ -210,14 +201,14 @@ void Array::resetDynArray() const
Array::operator const Poco::Dynamic::Array& () const
{
if (!_values.size())
if (_values.empty())
{
resetDynArray();
}
else if (_modified)
{
ValueVec::const_iterator it = _values.begin();
ValueVec::const_iterator end = _values.end();
auto it = _values.begin();
const auto end = _values.end();
resetDynArray();
int index = 0;
for (; it != end; ++it, ++index)
@ -246,8 +237,8 @@ Poco::Dynamic::Array Array::makeArray(const JSON::Array::Ptr& arr)
{
Poco::Dynamic::Array vec;
JSON::Array::ConstIterator it = arr->begin();
JSON::Array::ConstIterator end = arr->end();
auto it = arr->begin();
const auto end = arr->end();
int index = 0;
for (; it != end; ++it, ++index)
{
@ -274,7 +265,7 @@ Poco::Dynamic::Array Array::makeArray(const JSON::Array::Ptr& arr)
void Array::clear()
{
_values.clear();
_pArray = 0;
_pArray = nullptr;
}

View File

@ -13,32 +13,27 @@
#include "Poco/JSON/Handler.h"
#include "Poco/JSON/Object.h"
namespace Poco {
namespace JSON {
Handler::Handler()
{
}
Handler::Handler() = default;
Handler::~Handler()
{
}
Handler::~Handler() = default;
Dynamic::Var Handler::asVar() const
{
return Dynamic::Var();
return {};
}
Poco::DynamicStruct Handler::asStruct() const
{
return Poco::DynamicStruct();
return {};
}

View File

@ -14,7 +14,6 @@
#include "Poco/JSON/Object.h"
#include <iostream>
#include <sstream>
using Poco::Dynamic::Var;
@ -43,11 +42,12 @@ Object::Object(int options):
}
Object::Object(const Object& other) : _values(other._values),
Object::Object(const Object& other) :
_values(other._values),
_preserveInsOrder(other._preserveInsOrder),
_escapeUnicode(other._escapeUnicode),
_lowercaseHex(other._lowercaseHex),
_pStruct(!other._modified ? other._pStruct : 0),
_pStruct(!other._modified ? other._pStruct : nullptr),
_modified(other._modified)
{
syncKeys(other._keys);
@ -67,9 +67,7 @@ Object::Object(Object&& other) noexcept:
}
Object::~Object()
{
}
Object::~Object() = default;
Object &Object::operator = (const Object &other)
@ -81,7 +79,7 @@ Object &Object::operator = (const Object &other)
_preserveInsOrder = other._preserveInsOrder;
_escapeUnicode = other._escapeUnicode;
_lowercaseHex = other._lowercaseHex;
_pStruct = !other._modified ? other._pStruct : 0;
_pStruct = !other._modified ? other._pStruct : nullptr;
_modified = other._modified;
}
return *this;
@ -108,11 +106,10 @@ void Object::syncKeys(const KeyList& keys)
if(_preserveInsOrder)
{
// update iterators in _keys to point to copied _values
for(KeyList::const_iterator it = keys.begin(); it != keys.end(); ++it)
{
ValueMap::const_iterator itv = _values.find((*it)->first);
poco_assert (itv != _values.end());
_keys.push_back(itv);
for (const auto& key : keys) {
auto itv = _values.find(key->first);
poco_assert(itv != _values.end());
_keys.emplace_back(itv);
}
}
}
@ -120,37 +117,37 @@ void Object::syncKeys(const KeyList& keys)
Var Object::get(const std::string& key) const
{
ValueMap::const_iterator it = _values.find(key);
auto it = _values.find(key);
if (it != _values.end())
{
return it->second;
}
return Var();
return {};
}
Array::Ptr Object::getArray(const std::string& key) const
{
ValueMap::const_iterator it = _values.find(key);
auto it = _values.find(key);
if ((it != _values.end()) && (it->second.type() == typeid(Array::Ptr)))
{
return it->second.extract<Array::Ptr>();
}
return 0;
return nullptr;
}
Object::Ptr Object::getObject(const std::string& key) const
{
ValueMap::const_iterator it = _values.find(key);
auto it = _values.find(key);
if ((it != _values.end()) && (it->second.type() == typeid(Object::Ptr)))
{
return it->second.extract<Object::Ptr>();
}
return 0;
return nullptr;
}
@ -159,16 +156,14 @@ void Object::getNames(NameList& names) const
names.clear();
if (_preserveInsOrder)
{
for(KeyList::const_iterator it = _keys.begin(); it != _keys.end(); ++it)
{
names.push_back((*it)->first);
for (const auto& _key : _keys) {
names.push_back(_key->first);
}
}
else
{
for(ValueMap::const_iterator it = _values.begin(); it != _values.end(); ++it)
{
names.push_back(it->first);
for (const auto& _value : _values) {
names.push_back(_value.first);
}
}
}
@ -195,8 +190,8 @@ void Object::stringify(std::ostream& out, unsigned int indent, int step) const
const std::string& Object::getKey(KeyList::const_iterator& iter) const
{
ValueMap::const_iterator it = _values.begin();
ValueMap::const_iterator end = _values.end();
auto it = _values.begin();
auto end = _values.end();
for (; it != end; ++it)
{
if (it == *iter) return it->first;
@ -212,13 +207,13 @@ Object& Object::set(const std::string& key, const Dynamic::Var& value)
if (!ret.second) ret.first->second = value;
if (_preserveInsOrder)
{
KeyList::iterator it = _keys.begin();
KeyList::iterator end = _keys.end();
auto it = _keys.begin();
const auto end = _keys.end();
for (; it != end; ++it)
{
if (key == (*it)->first) return *this;
}
_keys.push_back(ret.first);
_keys.emplace_back(ret.first);
}
_modified = true;
return *this;
@ -259,14 +254,14 @@ void Object::resetDynStruct() const
Object::operator const Poco::DynamicStruct& () const
{
if (!_values.size())
if (_values.empty())
{
resetDynStruct(_pStruct);
}
else if (_modified)
{
ValueMap::const_iterator it = _values.begin();
ValueMap::const_iterator end = _values.end();
auto it = _values.begin();
const auto end = _values.end();
resetDynStruct(_pStruct);
for (; it != end; ++it)
{
@ -291,7 +286,7 @@ Object::operator const Poco::DynamicStruct& () const
Object::operator const Poco::OrderedDynamicStruct& () const
{
if (!_values.size())
if (_values.empty())
{
resetDynStruct(_pOrdStruct);
}
@ -299,8 +294,8 @@ Object::operator const Poco::OrderedDynamicStruct& () const
{
if (_preserveInsOrder)
{
KeyList::const_iterator it = _keys.begin();
KeyList::const_iterator end = _keys.end();
auto it = _keys.begin();
const auto end = _keys.end();
resetDynStruct(_pOrdStruct);
for (; it != end; ++it)
{
@ -320,8 +315,8 @@ Object::operator const Poco::OrderedDynamicStruct& () const
}
else
{
ValueMap::const_iterator it = _values.begin();
ValueMap::const_iterator end = _values.end();
auto it = _values.begin();
const auto end = _values.end();
resetDynStruct(_pOrdStruct);
for (; it != end; ++it)
{
@ -349,7 +344,7 @@ void Object::clear()
{
_values.clear();
_keys.clear();
_pStruct = 0;
_pStruct = nullptr;
_modified = true;
}

View File

@ -30,9 +30,7 @@ ParseHandler::ParseHandler(bool preserveObjectOrder) : Handler(),
}
ParseHandler::~ParseHandler()
{
}
ParseHandler::~ParseHandler() = default;
void ParseHandler::reset()
@ -67,7 +65,7 @@ void ParseHandler::startObject()
}
}
_stack.push(newObj);
_stack.emplace(newObj);
}
@ -102,7 +100,7 @@ void ParseHandler::startArray()
}
}
_stack.push(newArr);
_stack.emplace(newArr);
}
@ -120,7 +118,7 @@ void ParseHandler::key(const std::string& k)
void ParseHandler::setValue(const Var& value)
{
if (_stack.size())
if (!_stack.empty())
{
Var parent = _stack.top();

View File

@ -13,16 +13,9 @@
#include "Poco/JSON/Parser.h"
#include "Poco/JSON/JSONException.h"
#include "Poco/Ascii.h"
#include "Poco/Token.h"
#include "Poco/UTF8Encoding.h"
#include "Poco/String.h"
#undef min
#undef max
#include <limits>
#include <clocale>
#include <istream>
namespace Poco {
@ -35,9 +28,7 @@ Parser::Parser(const Handler::Ptr& pHandler):
}
Parser::~Parser()
{
}
Parser::~Parser() = default;
void Parser::setHandler(const Handler::Ptr& pHandler)

View File

@ -11,23 +11,20 @@
// SPDX-License-Identifier: BSL-1.0
//
#include <Poco/JSON/ParserImpl.h>
#include <Poco/JSON/JSONException.h>
#include <Poco/StreamCopier.h>
#include "Poco/JSON/Parser.h"
#include "Poco/JSON/JSONException.h"
#include "Poco/Ascii.h"
#include "Poco/Token.h"
#include "Poco/UTF8Encoding.h"
#include "Poco/String.h"
#include "Poco/StreamCopier.h"
#undef min
#undef max
#include <limits>
#include <clocale>
#include <istream>
#include <streambuf>
#include <clocale>
#include "pdjson.h"
typedef struct json_stream json_stream;
using json_stream = struct json_stream;
namespace Poco {
@ -38,13 +35,13 @@ extern "C"
{
static int istream_get(void* ptr)
{
std::streambuf* pBuf = reinterpret_cast<std::streambuf*>(ptr);
auto pBuf = reinterpret_cast<std::streambuf*>(ptr);
return pBuf->sbumpc();
}
static int istream_peek(void* ptr)
{
std::streambuf* pBuf = reinterpret_cast<std::streambuf*>(ptr);
auto pBuf = reinterpret_cast<std::streambuf*>(ptr);
return pBuf->sgetc();
}
}
@ -167,8 +164,7 @@ void ParserImpl::stripComments(std::string& json)
std::string::iterator it = json.begin();
for (; it != json.end();)
{
if (*it == '"' && !inString) inString = true;
else inString = false;
inString = *it == '"' && !inString;
if (!inString)
{
if (*it == '/' && it + 1 != json.end() && *(it + 1) == '*')
@ -220,7 +216,7 @@ void ParserImpl::handleObject()
while (tok != JSON_OBJECT_END && checkError())
{
json_next(_pJSON);
if (_pHandler) _pHandler->key(std::string(json_get_string(_pJSON, NULL)));
if (_pHandler) _pHandler->key(std::string(json_get_string(_pJSON, nullptr)));
handle();
tok = json_peek(_pJSON);
}
@ -252,7 +248,7 @@ void ParserImpl::handle()
case JSON_NUMBER:
if (_pHandler)
{
std::string str(json_get_string(_pJSON, NULL));
std::string str(json_get_string(_pJSON, nullptr));
if (str.find(_decimalPoint) != str.npos || str.find('e') != str.npos || str.find('E') != str.npos)
{
_pHandler->value(NumberParser::parseFloat(str));

View File

@ -41,9 +41,7 @@ PrintHandler::PrintHandler(std::ostream& out, unsigned indent, int options):
}
PrintHandler::~PrintHandler()
{
}
PrintHandler::~PrintHandler() = default;
void PrintHandler::reset()

View File

@ -37,9 +37,7 @@ Query::Query(const Var& source): _source(source)
}
Query::~Query()
{
}
Query::~Query() = default;
Object::Ptr Query::findObject(const std::string& path) const
@ -51,7 +49,7 @@ Object::Ptr Query::findObject(const std::string& path) const
else if (result.type() == typeid(Object))
return new Object(result.extract<Object>());
return 0;
return nullptr;
}
@ -79,7 +77,7 @@ Array::Ptr Query::findArray(const std::string& path) const
else if (result.type() == typeid(Array))
return new Array(result.extract<Array>());
return 0;
return nullptr;
}
@ -150,7 +148,7 @@ Var Query::find(const std::string& path) const
if (!result.isEmpty() && !indexes.empty())
{
for (auto i: indexes)
for (const auto& i: indexes)
{
if (result.type() == typeid(Array::Ptr))
{

View File

@ -15,7 +15,6 @@
#include "Poco/JSON/Stringifier.h"
#include "Poco/JSON/Array.h"
#include "Poco/JSON/Object.h"
#include <iomanip>
using Poco::Dynamic::Var;
@ -34,28 +33,28 @@ void Stringifier::stringify(const Var& any, std::ostream& out, unsigned int inde
if (any.type() == typeid(Object))
{
Object& o = const_cast<Object&>(any.extract<Object>());
auto& o = const_cast<Object&>(any.extract<Object>());
o.setEscapeUnicode(escapeUnicode);
o.setLowercaseHex(lowercaseHex);
o.stringify(out, indent == 0 ? 0 : indent, step);
}
else if (any.type() == typeid(Array))
{
Array& a = const_cast<Array&>(any.extract<Array>());
auto& a = const_cast<Array&>(any.extract<Array>());
a.setEscapeUnicode(escapeUnicode);
a.setLowercaseHex(lowercaseHex);
a.stringify(out, indent == 0 ? 0 : indent, step);
}
else if (any.type() == typeid(Object::Ptr))
{
Object::Ptr& o = const_cast<Object::Ptr&>(any.extract<Object::Ptr>());
auto& o = const_cast<Object::Ptr&>(any.extract<Object::Ptr>());
o->setEscapeUnicode(escapeUnicode);
o->setLowercaseHex(lowercaseHex);
o->stringify(out, indent == 0 ? 0 : indent, step);
}
else if (any.type() == typeid(Array::Ptr))
{
Array::Ptr& a = const_cast<Array::Ptr&>(any.extract<Array::Ptr>());
auto& a = const_cast<Array::Ptr&>(any.extract<Array::Ptr>());
a->setEscapeUnicode(escapeUnicode);
a->setLowercaseHex(lowercaseHex);
a->stringify(out, indent == 0 ? 0 : indent, step);
@ -66,7 +65,7 @@ void Stringifier::stringify(const Var& any, std::ostream& out, unsigned int inde
}
else if (any.isNumeric() || any.isBoolean())
{
std::string value = any.convert<std::string>();
auto value = any.convert<std::string>();
if ((Poco::icompare(value, "nan") == 0) ||
(Poco::icompare(value, "inf") == 0)) value = "null";
if (any.type() == typeid(char)) formatString(value, out, options);
@ -74,7 +73,7 @@ void Stringifier::stringify(const Var& any, std::ostream& out, unsigned int inde
}
else if (any.isString() || any.isDateTime() || any.isDate() || any.isTime())
{
std::string value = any.convert<std::string>();
auto value = any.convert<std::string>();
formatString(value, out, options);
}
else

View File

@ -32,17 +32,13 @@ POCO_IMPLEMENT_EXCEPTION(JSONTemplateException, Exception, "Template Exception")
class Part
{
public:
Part()
{
}
Part() = default;
virtual ~Part()
{
}
virtual ~Part() = default;
virtual void render(const Var& data, std::ostream& out) const = 0;
typedef std::vector<SharedPtr<Part>> VectorParts;
using VectorParts = std::vector<SharedPtr<Part>>;
};
@ -57,11 +53,9 @@ public:
{
}
virtual ~StringPart()
{
}
~StringPart() override = default;
void render(const Var& data, std::ostream& out) const
void render(const Var& data, std::ostream& out) const override
{
out << _content;
}
@ -84,20 +78,16 @@ private:
class MultiPart: public Part
{
public:
MultiPart()
{
}
MultiPart() = default;
virtual ~MultiPart()
{
}
~MultiPart() override = default;
virtual void addPart(Part* part)
{
_parts.push_back(part);
_parts.emplace_back(part);
}
void render(const Var& data, std::ostream& out) const
void render(const Var& data, std::ostream& out) const override
{
for (const auto& p: _parts)
{
@ -117,11 +107,9 @@ public:
{
}
virtual ~EchoPart()
{
}
~EchoPart() override = default;
void render(const Var& data, std::ostream& out) const
void render(const Var& data, std::ostream& out) const override
{
Query query(data);
Var value = query.find(_query);
@ -144,9 +132,7 @@ public:
{
}
virtual ~LogicQuery()
{
}
virtual ~LogicQuery() = default;
virtual bool apply(const Var& data) const
{
@ -161,7 +147,7 @@ public:
// An empty string must result in false, otherwise true
// Which is not the case when we convert to bool with Var
{
std::string s = value.convert<std::string>();
auto s = value.convert<std::string>();
logic = !s.empty();
}
else
@ -188,11 +174,9 @@ public:
{
}
virtual ~LogicExistQuery()
{
}
~LogicExistQuery() override = default;
virtual bool apply(const Var& data) const
bool apply(const Var& data) const override
{
Query query(data);
Var value = query.find(_queryString);
@ -209,11 +193,9 @@ public:
{
}
virtual ~LogicElseQuery()
{
}
~LogicElseQuery() override = default;
virtual bool apply(const Var& data) const
bool apply(const Var& data) const override
{
return true;
}
@ -227,23 +209,21 @@ public:
{
}
virtual ~LogicPart()
{
}
~LogicPart() override = default;
void addPart(LogicQuery* query, Part* part)
{
MultiPart::addPart(part);
_queries.push_back(query);
_queries.emplace_back(query);
}
void addPart(Part* part)
void addPart(Part* part) override
{
MultiPart::addPart(part);
_queries.push_back(new LogicElseQuery());
}
void render(const Var& data, std::ostream& out) const
void render(const Var& data, std::ostream& out) const override
{
int count = 0;
for (auto it = _queries.begin(); it != _queries.end(); ++it, ++count)
@ -268,11 +248,9 @@ public:
{
}
virtual ~LoopPart()
{
}
~LoopPart() override = default;
void render(const Var& data, std::ostream& out) const
void render(const Var& data, std::ostream& out) const override
{
Query query(data);
@ -322,14 +300,12 @@ public:
}
}
virtual ~IncludePart()
{
}
~IncludePart() override = default;
void render(const Var& data, std::ostream& out) const
void render(const Var& data, std::ostream& out) const override
{
TemplateCache* cache = TemplateCache::instance();
if (cache == 0)
if (cache == nullptr)
{
Template tpl(_path);
tpl.parse();
@ -348,16 +324,16 @@ private:
Template::Template(const Path& templatePath):
_parts(0),
_currentPart(0),
_parts(nullptr),
_currentPart(nullptr),
_templatePath(templatePath)
{
}
Template::Template():
_parts(0),
_currentPart(0)
_parts(nullptr),
_currentPart(nullptr)
{
}
@ -430,24 +406,24 @@ void Template::parse(std::istream& in)
}
_partStack.push(_currentPart);
LoopPart* part = new LoopPart(loopVariable, query);
auto part = new LoopPart(loopVariable, query);
_partStack.push(part);
_currentPart->addPart(part);
_currentPart = part;
}
else if (command.compare("else") == 0)
{
if (_partStack.size() == 0)
if (_partStack.empty())
{
throw JSONTemplateException("Unexpected <? else ?> found");
}
_currentPart = _partStack.top();
LogicPart* lp = dynamic_cast<LogicPart*>(_currentPart);
if (lp == 0)
auto lp = dynamic_cast<LogicPart*>(_currentPart);
if (lp == nullptr)
{
throw JSONTemplateException("Missing <? if ?> or <? ifexist ?> for <? else ?>");
}
MultiPart* part = new MultiPart();
auto part = new MultiPart();
lp->addPart(part);
_currentPart = part;
}
@ -459,18 +435,18 @@ void Template::parse(std::istream& in)
throw JSONTemplateException("Missing query in <? " + command + " ?>");
}
if (_partStack.size() == 0)
if (_partStack.empty())
{
throw JSONTemplateException("Unexpected <? elsif / elif ?> found");
}
_currentPart = _partStack.top();
LogicPart* lp = dynamic_cast<LogicPart*>(_currentPart);
if (lp == 0)
auto lp = dynamic_cast<LogicPart*>(_currentPart);
if (lp == nullptr)
{
throw JSONTemplateException("Missing <? if ?> or <? ifexist ?> for <? elsif / elif ?>");
}
MultiPart* part = new MultiPart();
auto part = new MultiPart();
lp->addPart(new LogicQuery(query), part);
_currentPart = part;
}
@ -481,8 +457,8 @@ void Template::parse(std::istream& in)
throw JSONTemplateException("Unexpected <? endfor ?> found");
}
MultiPart* loopPart = _partStack.top();
LoopPart* lp = dynamic_cast<LoopPart*>(loopPart);
if (lp == 0)
auto lp = dynamic_cast<LoopPart*>(loopPart);
if (lp == nullptr)
{
throw JSONTemplateException("Missing <? for ?> command");
}
@ -498,8 +474,8 @@ void Template::parse(std::istream& in)
}
_currentPart = _partStack.top();
LogicPart* lp = dynamic_cast<LogicPart*>(_currentPart);
if (lp == 0)
auto lp = dynamic_cast<LogicPart*>(_currentPart);
if (lp == nullptr)
{
throw JSONTemplateException("Missing <? if ?> or <? ifexist ?> for <? endif ?>");
}
@ -516,7 +492,7 @@ void Template::parse(std::istream& in)
throw JSONTemplateException("Missing query in <? " + command + " ?>");
}
_partStack.push(_currentPart);
LogicPart* lp = new LogicPart();
auto lp = new LogicPart();
_partStack.push(lp);
_currentPart->addPart(lp);
_currentPart = new MultiPart();
@ -590,7 +566,7 @@ std::string Template::readText(std::istream& in)
break;
}
}
text += c;
text += static_cast<char>(c);
c = in.get();
}
@ -612,7 +588,7 @@ std::string Template::readTemplateCommand(std::istream& in)
if (c == '?' && in.peek() == '>')
{
in.putback(c);
in.putback(static_cast<char>(c));
break;
}
@ -622,7 +598,7 @@ std::string Template::readTemplateCommand(std::istream& in)
break;
}
command += c;
command += static_cast<char>(c);
c = in.get();
}
@ -638,7 +614,7 @@ std::string Template::readWord(std::istream& in)
while ((c = in.peek()) != -1 && !Ascii::isSpace(c))
{
in.get();
word += c;
word += static_cast<char>(c);
}
return word;
}
@ -652,7 +628,7 @@ std::string Template::readQuery(std::istream& in)
{
if (c == '?' && in.peek() == '>')
{
in.putback(c);
in.putback(static_cast<char>(c));
break;
}
@ -660,7 +636,7 @@ std::string Template::readQuery(std::istream& in)
{
break;
}
word += c;
word += static_cast<char>(c);
}
return word;
}
@ -685,7 +661,7 @@ std::string Template::readString(std::istream& in)
{
while ((c = in.get()) != -1 && c != '"')
{
str += c;
str += static_cast<char>(c);
}
}
return str;

View File

@ -20,10 +20,10 @@ namespace Poco {
namespace JSON {
TemplateCache* TemplateCache::_pInstance = 0;
TemplateCache* TemplateCache::_pInstance = nullptr;
TemplateCache::TemplateCache(): _pLogger(0)
TemplateCache::TemplateCache(): _pLogger(nullptr)
{
setup();
}
@ -31,13 +31,13 @@ TemplateCache::TemplateCache(): _pLogger(0)
TemplateCache::~TemplateCache()
{
_pInstance = 0;
_pInstance = nullptr;
}
void TemplateCache::setup()
{
poco_assert (_pInstance == 0);
poco_assert (_pInstance == nullptr);
_pInstance = this;
}
@ -61,7 +61,7 @@ Template::Ptr TemplateCache::getTemplate(const Path& path)
Template::Ptr tpl;
std::map<std::string, Template::Ptr>::iterator it = _cache.find(templatePathname);
auto it = _cache.find(templatePathname);
if (it == _cache.end())
{
if (templateFile.exists())