forked from bblanchon/ArduinoJson
Convert CollectionIterator
to GoF style
This commit is contained in:
@@ -10,9 +10,9 @@
|
|||||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||||
|
|
||||||
inline ArrayData::iterator ArrayData::at(size_t index) const {
|
inline ArrayData::iterator ArrayData::at(size_t index) const {
|
||||||
auto it = begin();
|
auto it = createIterator();
|
||||||
while (it && index) {
|
while (!it.done() && index) {
|
||||||
++it;
|
it.next();
|
||||||
--index;
|
--index;
|
||||||
}
|
}
|
||||||
return it;
|
return it;
|
||||||
@@ -30,7 +30,7 @@ inline bool ArrayData::copyFrom(const ArrayData& src,
|
|||||||
ResourceManager* resources) {
|
ResourceManager* resources) {
|
||||||
clear(resources);
|
clear(resources);
|
||||||
|
|
||||||
for (auto it = src.begin(); it; ++it) {
|
for (auto it = src.createIterator(); !it.done(); it.next()) {
|
||||||
auto var = addElement(resources);
|
auto var = addElement(resources);
|
||||||
if (!var)
|
if (!var)
|
||||||
return false;
|
return false;
|
||||||
@@ -42,12 +42,12 @@ inline bool ArrayData::copyFrom(const ArrayData& src,
|
|||||||
|
|
||||||
inline VariantData* ArrayData::getOrAddElement(size_t index,
|
inline VariantData* ArrayData::getOrAddElement(size_t index,
|
||||||
ResourceManager* resources) {
|
ResourceManager* resources) {
|
||||||
auto it = begin();
|
auto it = createIterator();
|
||||||
while (it && index > 0) {
|
while (!it.done() && index > 0) {
|
||||||
++it;
|
it.next();
|
||||||
index--;
|
index--;
|
||||||
}
|
}
|
||||||
if (!it)
|
if (it.done())
|
||||||
index++;
|
index++;
|
||||||
VariantData* element = it.data();
|
VariantData* element = it.data();
|
||||||
while (index > 0) {
|
while (index > 0) {
|
||||||
|
@@ -68,7 +68,7 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
|
|||||||
FORCE_INLINE iterator begin() const {
|
FORCE_INLINE iterator begin() const {
|
||||||
if (!data_)
|
if (!data_)
|
||||||
return iterator();
|
return iterator();
|
||||||
return iterator(data_->begin(), resources_);
|
return iterator(data_->createIterator(), resources_);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns an iterator following the last element of the array.
|
// Returns an iterator following the last element of the array.
|
||||||
|
@@ -26,7 +26,7 @@ class JsonArrayConst : public detail::VariantOperators<JsonArrayConst> {
|
|||||||
FORCE_INLINE iterator begin() const {
|
FORCE_INLINE iterator begin() const {
|
||||||
if (!data_)
|
if (!data_)
|
||||||
return iterator();
|
return iterator();
|
||||||
return iterator(data_->begin(), resources_);
|
return iterator(data_->createIterator(), resources_);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns an iterator to the element following the last element of the array.
|
// Returns an iterator to the element following the last element of the array.
|
||||||
|
@@ -51,7 +51,7 @@ class JsonArrayIterator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
JsonArrayIterator& operator++() {
|
JsonArrayIterator& operator++() {
|
||||||
++iterator_;
|
iterator_.next();
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,7 +65,8 @@ class JsonArrayConstIterator {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
JsonArrayConstIterator() {}
|
JsonArrayConstIterator() {}
|
||||||
explicit JsonArrayConstIterator(detail::ArrayData::iterator iterator, const detail::ResourceManager* resources)
|
explicit JsonArrayConstIterator(detail::ArrayData::iterator iterator,
|
||||||
|
const detail::ResourceManager* resources)
|
||||||
: iterator_(iterator), resources_(resources) {}
|
: iterator_(iterator), resources_(resources) {}
|
||||||
|
|
||||||
JsonVariantConst operator*() const {
|
JsonVariantConst operator*() const {
|
||||||
@@ -84,7 +85,7 @@ class JsonArrayConstIterator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
JsonArrayConstIterator& operator++() {
|
JsonArrayConstIterator& operator++() {
|
||||||
++iterator_;
|
iterator_.next();
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -20,10 +20,10 @@ class CollectionIterator {
|
|||||||
public:
|
public:
|
||||||
CollectionIterator() : slot_(nullptr) {}
|
CollectionIterator() : slot_(nullptr) {}
|
||||||
|
|
||||||
CollectionIterator& operator++();
|
void next();
|
||||||
|
|
||||||
operator bool() const {
|
bool done() const {
|
||||||
return slot_ != nullptr;
|
return slot_ == nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator==(const CollectionIterator& other) const {
|
bool operator==(const CollectionIterator& other) const {
|
||||||
@@ -80,14 +80,10 @@ class CollectionData {
|
|||||||
|
|
||||||
using iterator = CollectionIterator;
|
using iterator = CollectionIterator;
|
||||||
|
|
||||||
iterator begin() const {
|
iterator createIterator() const {
|
||||||
return iterator(head_);
|
return iterator(head_);
|
||||||
}
|
}
|
||||||
|
|
||||||
iterator end() const {
|
|
||||||
return iterator(nullptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t memoryUsage() const;
|
size_t memoryUsage() const;
|
||||||
size_t size() const;
|
size_t size() const;
|
||||||
size_t nesting() const;
|
size_t nesting() const;
|
||||||
|
@@ -22,10 +22,9 @@ inline bool CollectionIterator::ownsKey() const {
|
|||||||
return slot_->ownsKey();
|
return slot_->ownsKey();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline CollectionIterator& CollectionIterator::operator++() {
|
inline void CollectionIterator::next() {
|
||||||
ARDUINOJSON_ASSERT(slot_ != nullptr);
|
ARDUINOJSON_ASSERT(slot_ != nullptr);
|
||||||
slot_ = slot_->next();
|
slot_ = slot_->next();
|
||||||
return *this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void CollectionData::addSlot(VariantSlot* slot) {
|
inline void CollectionData::addSlot(VariantSlot* slot) {
|
||||||
@@ -59,7 +58,7 @@ inline VariantSlot* CollectionData::getPreviousSlot(VariantSlot* target) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
inline void CollectionData::remove(iterator it, ResourceManager* resources) {
|
inline void CollectionData::remove(iterator it, ResourceManager* resources) {
|
||||||
if (!it)
|
if (it.done())
|
||||||
return;
|
return;
|
||||||
auto curr = it.slot_;
|
auto curr = it.slot_;
|
||||||
auto prev = getPreviousSlot(curr);
|
auto prev = getPreviousSlot(curr);
|
||||||
|
@@ -21,13 +21,13 @@ class JsonSerializer : public Visitor<size_t> {
|
|||||||
FORCE_INLINE size_t visitArray(const ArrayData& array) {
|
FORCE_INLINE size_t visitArray(const ArrayData& array) {
|
||||||
write('[');
|
write('[');
|
||||||
|
|
||||||
auto it = array.begin();
|
auto it = array.createIterator();
|
||||||
|
|
||||||
while (it) {
|
while (!it.done()) {
|
||||||
it->accept(*this);
|
it->accept(*this);
|
||||||
|
|
||||||
++it;
|
it.next();
|
||||||
if (!it)
|
if (it.done())
|
||||||
break;
|
break;
|
||||||
|
|
||||||
write(',');
|
write(',');
|
||||||
@@ -40,15 +40,15 @@ class JsonSerializer : public Visitor<size_t> {
|
|||||||
size_t visitObject(const ObjectData& object) {
|
size_t visitObject(const ObjectData& object) {
|
||||||
write('{');
|
write('{');
|
||||||
|
|
||||||
auto it = object.begin();
|
auto it = object.createIterator();
|
||||||
|
|
||||||
while (it) {
|
while (!it.done()) {
|
||||||
formatter_.writeString(it.key());
|
formatter_.writeString(it.key());
|
||||||
write(':');
|
write(':');
|
||||||
it->accept(*this);
|
it->accept(*this);
|
||||||
|
|
||||||
++it;
|
it.next();
|
||||||
if (!it)
|
if (it.done())
|
||||||
break;
|
break;
|
||||||
|
|
||||||
write(',');
|
write(',');
|
||||||
|
@@ -19,16 +19,16 @@ class PrettyJsonSerializer : public JsonSerializer<TWriter> {
|
|||||||
PrettyJsonSerializer(TWriter writer) : base(writer), nesting_(0) {}
|
PrettyJsonSerializer(TWriter writer) : base(writer), nesting_(0) {}
|
||||||
|
|
||||||
size_t visitArray(const ArrayData& array) {
|
size_t visitArray(const ArrayData& array) {
|
||||||
auto it = array.begin();
|
auto it = array.createIterator();
|
||||||
if (it) {
|
if (!it.done()) {
|
||||||
base::write("[\r\n");
|
base::write("[\r\n");
|
||||||
nesting_++;
|
nesting_++;
|
||||||
while (it) {
|
while (!it.done()) {
|
||||||
indent();
|
indent();
|
||||||
it->accept(*this);
|
it->accept(*this);
|
||||||
|
|
||||||
++it;
|
it.next();
|
||||||
base::write(it ? ",\r\n" : "\r\n");
|
base::write(it.done() ? "\r\n" : ",\r\n");
|
||||||
}
|
}
|
||||||
nesting_--;
|
nesting_--;
|
||||||
indent();
|
indent();
|
||||||
@@ -40,18 +40,18 @@ class PrettyJsonSerializer : public JsonSerializer<TWriter> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
size_t visitObject(const ObjectData& object) {
|
size_t visitObject(const ObjectData& object) {
|
||||||
auto it = object.begin();
|
auto it = object.createIterator();
|
||||||
if (it) {
|
if (!it.done()) {
|
||||||
base::write("{\r\n");
|
base::write("{\r\n");
|
||||||
nesting_++;
|
nesting_++;
|
||||||
while (it) {
|
while (!it.done()) {
|
||||||
indent();
|
indent();
|
||||||
base::visitString(it.key());
|
base::visitString(it.key());
|
||||||
base::write(": ");
|
base::write(": ");
|
||||||
it->accept(*this);
|
it->accept(*this);
|
||||||
|
|
||||||
++it;
|
it.next();
|
||||||
base::write(it ? ",\r\n" : "\r\n");
|
base::write(it.done() ? "\r\n" : ",\r\n");
|
||||||
}
|
}
|
||||||
nesting_--;
|
nesting_--;
|
||||||
indent();
|
indent();
|
||||||
|
@@ -55,7 +55,7 @@ class MsgPackSerializer : public Visitor<size_t> {
|
|||||||
writeByte(0xDD);
|
writeByte(0xDD);
|
||||||
writeInteger(uint32_t(n));
|
writeInteger(uint32_t(n));
|
||||||
}
|
}
|
||||||
for (auto it = array.begin(); it; ++it) {
|
for (auto it = array.createIterator(); !it.done(); it.next()) {
|
||||||
it->accept(*this);
|
it->accept(*this);
|
||||||
}
|
}
|
||||||
return bytesWritten();
|
return bytesWritten();
|
||||||
@@ -72,7 +72,7 @@ class MsgPackSerializer : public Visitor<size_t> {
|
|||||||
writeByte(0xDF);
|
writeByte(0xDF);
|
||||||
writeInteger(uint32_t(n));
|
writeInteger(uint32_t(n));
|
||||||
}
|
}
|
||||||
for (auto it = object.begin(); it; ++it) {
|
for (auto it = object.createIterator(); !it.done(); it.next()) {
|
||||||
visitString(it.key());
|
visitString(it.key());
|
||||||
it->accept(*this);
|
it->accept(*this);
|
||||||
}
|
}
|
||||||
|
@@ -76,7 +76,7 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
|
|||||||
FORCE_INLINE iterator begin() const {
|
FORCE_INLINE iterator begin() const {
|
||||||
if (!data_)
|
if (!data_)
|
||||||
return iterator();
|
return iterator();
|
||||||
return iterator(data_->begin(), resources_);
|
return iterator(data_->createIterator(), resources_);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns an iterator following the last key-value pair of the object.
|
// Returns an iterator following the last key-value pair of the object.
|
||||||
|
@@ -65,7 +65,7 @@ class JsonObjectConst : public detail::VariantOperators<JsonObjectConst> {
|
|||||||
FORCE_INLINE iterator begin() const {
|
FORCE_INLINE iterator begin() const {
|
||||||
if (!data_)
|
if (!data_)
|
||||||
return iterator();
|
return iterator();
|
||||||
return iterator(data_->begin(), resources_);
|
return iterator(data_->createIterator(), resources_);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns an iterator following the last key-value pair of the object.
|
// Returns an iterator following the last key-value pair of the object.
|
||||||
|
@@ -35,7 +35,7 @@ class JsonObjectIterator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
JsonObjectIterator& operator++() {
|
JsonObjectIterator& operator++() {
|
||||||
++iterator_;
|
iterator_.next();
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,7 +70,7 @@ class JsonObjectConstIterator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
JsonObjectConstIterator& operator++() {
|
JsonObjectConstIterator& operator++() {
|
||||||
++iterator_;
|
iterator_.next();
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -21,7 +21,7 @@ class JsonPair {
|
|||||||
|
|
||||||
// Returns the key.
|
// Returns the key.
|
||||||
JsonString key() const {
|
JsonString key() const {
|
||||||
if (iterator_)
|
if (!iterator_.done())
|
||||||
return JsonString(iterator_.key(), iterator_.ownsKey()
|
return JsonString(iterator_.key(), iterator_.ownsKey()
|
||||||
? JsonString::Copied
|
? JsonString::Copied
|
||||||
: JsonString::Linked);
|
: JsonString::Linked);
|
||||||
@@ -49,7 +49,7 @@ class JsonPairConst {
|
|||||||
|
|
||||||
// Returns the key.
|
// Returns the key.
|
||||||
JsonString key() const {
|
JsonString key() const {
|
||||||
if (iterator_)
|
if (!iterator_.done())
|
||||||
return JsonString(iterator_.key(), iterator_.ownsKey()
|
return JsonString(iterator_.key(), iterator_.ownsKey()
|
||||||
? JsonString::Copied
|
? JsonString::Copied
|
||||||
: JsonString::Linked);
|
: JsonString::Linked);
|
||||||
|
@@ -44,7 +44,7 @@ inline bool ObjectData::copyFrom(const ObjectData& src,
|
|||||||
ResourceManager* resources) {
|
ResourceManager* resources) {
|
||||||
clear(resources);
|
clear(resources);
|
||||||
|
|
||||||
for (auto it = src.begin(); it; ++it) {
|
for (auto it = src.createIterator(); !it.done(); it.next()) {
|
||||||
ARDUINOJSON_ASSERT(it.key() != 0);
|
ARDUINOJSON_ASSERT(it.key() != 0);
|
||||||
JsonString key(it.key(),
|
JsonString key(it.key(),
|
||||||
it.ownsKey() ? JsonString::Copied : JsonString::Linked);
|
it.ownsKey() ? JsonString::Copied : JsonString::Linked);
|
||||||
@@ -66,7 +66,7 @@ template <typename TAdaptedString>
|
|||||||
VariantData* ObjectData::getOrAddMember(TAdaptedString key,
|
VariantData* ObjectData::getOrAddMember(TAdaptedString key,
|
||||||
ResourceManager* resources) {
|
ResourceManager* resources) {
|
||||||
auto it = findKey(key);
|
auto it = findKey(key);
|
||||||
if (it)
|
if (!it.done())
|
||||||
return it.data();
|
return it.data();
|
||||||
return addMember(key, resources);
|
return addMember(key, resources);
|
||||||
}
|
}
|
||||||
@@ -74,12 +74,12 @@ VariantData* ObjectData::getOrAddMember(TAdaptedString key,
|
|||||||
template <typename TAdaptedString>
|
template <typename TAdaptedString>
|
||||||
inline ObjectData::iterator ObjectData::findKey(TAdaptedString key) const {
|
inline ObjectData::iterator ObjectData::findKey(TAdaptedString key) const {
|
||||||
if (key.isNull())
|
if (key.isNull())
|
||||||
return end();
|
return iterator();
|
||||||
for (auto it = begin(); it; ++it) {
|
for (auto it = createIterator(); !it.done(); it.next()) {
|
||||||
if (stringEquals(key, adaptString(it.key())))
|
if (stringEquals(key, adaptString(it.key())))
|
||||||
return it;
|
return it;
|
||||||
}
|
}
|
||||||
return end();
|
return iterator();
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TAdaptedString>
|
template <typename TAdaptedString>
|
||||||
|
Reference in New Issue
Block a user