Skip to content

Commit

Permalink
Usage modern C++ features on JSON modules
Browse files Browse the repository at this point in the history
  • Loading branch information
cngzhnp committed May 5, 2024
1 parent 1a0355f commit d3b7947
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 152 deletions.
27 changes: 9 additions & 18 deletions JSON/src/Array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -88,9 +81,7 @@ Array& Array::operator = (Array&& other) noexcept
}


Array::~Array()
{
}
Array::~Array() = default;


Var Array::get(unsigned int index) const
Expand Down Expand Up @@ -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 << ' ';

Expand Down Expand Up @@ -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();
auto end = _values.end();
resetDynArray();
int index = 0;
for (; it != end; ++it, ++index)
Expand Down Expand Up @@ -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();
auto end = arr->end();
int index = 0;
for (; it != end; ++it, ++index)
{
Expand All @@ -274,7 +265,7 @@ Poco::Dynamic::Array Array::makeArray(const JSON::Array::Ptr& arr)
void Array::clear()
{
_values.clear();
_pArray = 0;
_pArray = nullptr;
}


Expand Down
12 changes: 4 additions & 8 deletions JSON/src/Handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,21 @@ 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 {};
}


Expand Down
56 changes: 26 additions & 30 deletions JSON/src/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ Object::Object(int options):
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);
Expand All @@ -67,9 +66,7 @@ Object::Object(Object&& other) noexcept:
}


Object::~Object()
{
}
Object::~Object() = default;


Object &Object::operator = (const Object &other)
Expand All @@ -80,8 +77,7 @@ Object &Object::operator = (const Object &other)
_keys = other._keys;
_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;
Expand All @@ -108,7 +104,7 @@ 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)
for(auto it = keys.begin(); it != keys.end(); ++it)
{
ValueMap::const_iterator itv = _values.find((*it)->first);
poco_assert (itv != _values.end());
Expand All @@ -120,37 +116,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;
}


Expand All @@ -159,14 +155,14 @@ void Object::getNames(NameList& names) const
names.clear();
if (_preserveInsOrder)
{
for(KeyList::const_iterator it = _keys.begin(); it != _keys.end(); ++it)
for(auto it = _keys.begin(); it != _keys.end(); ++it)
{
names.push_back((*it)->first);
}
}
else
{
for(ValueMap::const_iterator it = _values.begin(); it != _values.end(); ++it)
for(auto it = _values.begin(); it != _values.end(); ++it)
{
names.push_back(it->first);
}
Expand Down Expand Up @@ -195,8 +191,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;
Expand All @@ -212,13 +208,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();
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;
Expand Down Expand Up @@ -259,14 +255,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();
auto end = _values.end();
resetDynStruct(_pStruct);
for (; it != end; ++it)
{
Expand All @@ -291,16 +287,16 @@ Object::operator const Poco::DynamicStruct& () const

Object::operator const Poco::OrderedDynamicStruct& () const
{
if (!_values.size())
if (_values.empty())
{
resetDynStruct(_pOrdStruct);
}
else if (_modified)
{
if (_preserveInsOrder)
{
KeyList::const_iterator it = _keys.begin();
KeyList::const_iterator end = _keys.end();
auto it = _keys.begin();
auto end = _keys.end();
resetDynStruct(_pOrdStruct);
for (; it != end; ++it)
{
Expand All @@ -320,8 +316,8 @@ Object::operator const Poco::OrderedDynamicStruct& () const
}
else
{
ValueMap::const_iterator it = _values.begin();
ValueMap::const_iterator end = _values.end();
auto it = _values.begin();
auto end = _values.end();
resetDynStruct(_pOrdStruct);
for (; it != end; ++it)
{
Expand Down Expand Up @@ -349,7 +345,7 @@ void Object::clear()
{
_values.clear();
_keys.clear();
_pStruct = 0;
_pStruct = nullptr;
_modified = true;
}

Expand Down
10 changes: 4 additions & 6 deletions JSON/src/ParseHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ ParseHandler::ParseHandler(bool preserveObjectOrder) : Handler(),
}


ParseHandler::~ParseHandler()
{
}
ParseHandler::~ParseHandler() = default;


void ParseHandler::reset()
Expand Down Expand Up @@ -67,7 +65,7 @@ void ParseHandler::startObject()
}
}

_stack.push(newObj);
_stack.emplace(newObj);
}


Expand Down Expand Up @@ -102,7 +100,7 @@ void ParseHandler::startArray()
}
}

_stack.push(newArr);
_stack.emplace(newArr);
}


Expand All @@ -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();

Expand Down
4 changes: 1 addition & 3 deletions JSON/src/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ Parser::Parser(const Handler::Ptr& pHandler):
}


Parser::~Parser()
{
}
Parser::~Parser() = default;


void Parser::setHandler(const Handler::Ptr& pHandler)
Expand Down
13 changes: 6 additions & 7 deletions JSON/src/ParserImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include "pdjson.h"


typedef struct json_stream json_stream;
using json_stream = struct json_stream;


namespace Poco {
Expand All @@ -38,13 +38,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();
}
}
Expand Down Expand Up @@ -167,8 +167,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) == '*')
Expand Down Expand Up @@ -220,7 +219,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);
}
Expand Down Expand Up @@ -252,7 +251,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));
Expand Down
Loading

0 comments on commit d3b7947

Please sign in to comment.