mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-29 10:17:39 +02:00
Add ResourceManager*
in CollectionIterator
+80
This commit is contained in:
@ -15,7 +15,7 @@ inline VariantImpl::iterator VariantImpl::at(size_t index) const {
|
||||
|
||||
auto it = createIterator();
|
||||
while (!it.done() && index) {
|
||||
it.next(resources_);
|
||||
it.next();
|
||||
--index;
|
||||
}
|
||||
return it;
|
||||
@ -34,7 +34,7 @@ inline VariantData* VariantImpl::addElement() {
|
||||
inline VariantData* VariantImpl::getOrAddElement(size_t index) {
|
||||
auto it = createIterator();
|
||||
while (!it.done() && index > 0) {
|
||||
it.next(resources_);
|
||||
it.next();
|
||||
index--;
|
||||
}
|
||||
if (it.done())
|
||||
|
@ -73,7 +73,7 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
|
||||
// Returns an iterator to the first element of the array.
|
||||
// https://arduinojson.org/v7/api/jsonarray/begin/
|
||||
iterator begin() const {
|
||||
return iterator(impl_.createIterator(), impl_.getResourceManager());
|
||||
return iterator(impl_.createIterator());
|
||||
}
|
||||
|
||||
// Returns an iterator following the last element of the array.
|
||||
|
@ -24,7 +24,7 @@ class JsonArrayConst : public detail::VariantOperators<JsonArrayConst> {
|
||||
// Returns an iterator to the first element of the array.
|
||||
// https://arduinojson.org/v7/api/jsonarrayconst/begin/
|
||||
iterator begin() const {
|
||||
return iterator(impl_.createIterator(), impl_.getResourceManager());
|
||||
return iterator(impl_.createIterator());
|
||||
}
|
||||
|
||||
// Returns an iterator to the element following the last element of the array.
|
||||
|
@ -30,12 +30,11 @@ class JsonArrayIterator {
|
||||
|
||||
public:
|
||||
JsonArrayIterator() {}
|
||||
explicit JsonArrayIterator(detail::VariantImpl::iterator iterator,
|
||||
detail::ResourceManager* resources)
|
||||
: iterator_(iterator), resources_(resources) {}
|
||||
explicit JsonArrayIterator(const detail::VariantImpl::iterator& iterator)
|
||||
: iterator_(iterator) {}
|
||||
|
||||
JsonVariant operator*() {
|
||||
return JsonVariant(iterator_.value(resources_));
|
||||
return JsonVariant(iterator_.value());
|
||||
}
|
||||
Ptr<JsonVariant> operator->() {
|
||||
return operator*();
|
||||
@ -50,13 +49,12 @@ class JsonArrayIterator {
|
||||
}
|
||||
|
||||
JsonArrayIterator& operator++() {
|
||||
iterator_.next(resources_);
|
||||
iterator_.next();
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
detail::VariantImpl::iterator iterator_;
|
||||
detail::ResourceManager* resources_;
|
||||
};
|
||||
|
||||
class JsonArrayConstIterator {
|
||||
@ -64,12 +62,11 @@ class JsonArrayConstIterator {
|
||||
|
||||
public:
|
||||
JsonArrayConstIterator() {}
|
||||
explicit JsonArrayConstIterator(detail::VariantImpl::iterator iterator,
|
||||
detail::ResourceManager* resources)
|
||||
: iterator_(iterator), resources_(resources) {}
|
||||
explicit JsonArrayConstIterator(const detail::VariantImpl::iterator& iterator)
|
||||
: iterator_(iterator) {}
|
||||
|
||||
JsonVariantConst operator*() const {
|
||||
return JsonVariantConst(iterator_.value(resources_));
|
||||
return JsonVariantConst(iterator_.value());
|
||||
}
|
||||
Ptr<JsonVariantConst> operator->() {
|
||||
return operator*();
|
||||
@ -84,13 +81,12 @@ class JsonArrayConstIterator {
|
||||
}
|
||||
|
||||
JsonArrayConstIterator& operator++() {
|
||||
iterator_.next(resources_);
|
||||
iterator_.next();
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
mutable detail::VariantImpl::iterator iterator_;
|
||||
mutable detail::ResourceManager* resources_;
|
||||
};
|
||||
|
||||
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
||||
|
@ -12,23 +12,23 @@
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
inline void CollectionIterator::next(const ResourceManager* resources) {
|
||||
inline void CollectionIterator::next() {
|
||||
ARDUINOJSON_ASSERT(slot_);
|
||||
auto nextId = slot_->next;
|
||||
slot_ = resources->getVariant(nextId);
|
||||
slot_ = resources_->getVariant(nextId);
|
||||
currentId_ = nextId;
|
||||
}
|
||||
|
||||
inline VariantImpl CollectionIterator::value(ResourceManager* resources) const {
|
||||
inline VariantImpl CollectionIterator::value() const {
|
||||
ARDUINOJSON_ASSERT(slot_ != nullptr);
|
||||
return VariantImpl(slot_, resources);
|
||||
return VariantImpl(slot_, resources_);
|
||||
}
|
||||
|
||||
inline VariantImpl::iterator VariantImpl::createIterator() const {
|
||||
if (!data_ || !data_->isCollection())
|
||||
return iterator();
|
||||
auto coll = getCollectionData();
|
||||
return iterator(getVariant(coll->head), coll->head);
|
||||
return iterator(coll->head, resources_);
|
||||
}
|
||||
|
||||
inline void VariantImpl::appendOne(Slot<VariantData> slot) {
|
||||
@ -111,8 +111,8 @@ inline size_t VariantImpl::nesting() const {
|
||||
if (!data_ || !data_->isCollection())
|
||||
return 0;
|
||||
size_t maxChildNesting = 0;
|
||||
for (auto it = createIterator(); !it.done(); it.next(resources_)) {
|
||||
auto childNesting = it.value(resources_).nesting();
|
||||
for (auto it = createIterator(); !it.done(); it.next()) {
|
||||
auto childNesting = it.value().nesting();
|
||||
if (childNesting > maxChildNesting)
|
||||
maxChildNesting = childNesting;
|
||||
}
|
||||
|
@ -19,11 +19,11 @@ class CollectionIterator {
|
||||
friend class VariantImpl;
|
||||
|
||||
public:
|
||||
CollectionIterator() : slot_(nullptr), currentId_(NULL_SLOT) {}
|
||||
CollectionIterator() {}
|
||||
|
||||
void next(const ResourceManager* resources);
|
||||
void next();
|
||||
|
||||
VariantImpl value(ResourceManager* resources) const;
|
||||
VariantImpl value() const;
|
||||
|
||||
bool done() const {
|
||||
return slot_ == nullptr;
|
||||
@ -46,11 +46,14 @@ class CollectionIterator {
|
||||
}
|
||||
|
||||
private:
|
||||
CollectionIterator(VariantData* slot, SlotId slotId)
|
||||
: slot_(slot), currentId_(slotId) {}
|
||||
CollectionIterator(SlotId slotId, ResourceManager* resources)
|
||||
: slot_(resources->getVariant(slotId)),
|
||||
currentId_(slotId),
|
||||
resources_(resources) {}
|
||||
|
||||
VariantData* slot_;
|
||||
SlotId currentId_;
|
||||
VariantData* slot_ = nullptr;
|
||||
SlotId currentId_ = NULL_SLOT;
|
||||
ResourceManager* resources_ = nullptr;
|
||||
};
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
|
@ -26,8 +26,8 @@ class PrettyJsonSerializer : public JsonSerializer<TWriter> {
|
||||
nesting_++;
|
||||
while (!it.done()) {
|
||||
indent();
|
||||
it.value(base::resources_).accept(*this);
|
||||
it.next(base::resources_);
|
||||
it.value().accept(*this);
|
||||
it.next();
|
||||
base::write(it.done() ? "\r\n" : ",\r\n");
|
||||
}
|
||||
nesting_--;
|
||||
@ -48,8 +48,8 @@ class PrettyJsonSerializer : public JsonSerializer<TWriter> {
|
||||
while (!it.done()) {
|
||||
if (isKey)
|
||||
indent();
|
||||
it.value(base::resources_).accept(*this);
|
||||
it.next(base::resources_);
|
||||
it.value().accept(*this);
|
||||
it.next();
|
||||
if (isKey)
|
||||
base::write(": ");
|
||||
else
|
||||
|
@ -64,7 +64,7 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
|
||||
// Returns an iterator to the first key-value pair of the object.
|
||||
// https://arduinojson.org/v7/api/jsonobject/begin/
|
||||
iterator begin() const {
|
||||
return iterator(impl_.createIterator(), impl_.getResourceManager());
|
||||
return iterator(impl_.createIterator());
|
||||
}
|
||||
|
||||
// Returns an iterator following the last key-value pair of the object.
|
||||
|
@ -55,7 +55,7 @@ class JsonObjectConst : public detail::VariantOperators<JsonObjectConst> {
|
||||
// Returns an iterator to the first key-value pair of the object.
|
||||
// https://arduinojson.org/v7/api/jsonobjectconst/begin/
|
||||
iterator begin() const {
|
||||
return iterator(impl_.createIterator(), impl_.getResourceManager());
|
||||
return iterator(impl_.createIterator());
|
||||
}
|
||||
|
||||
// Returns an iterator following the last key-value pair of the object.
|
||||
|
@ -14,12 +14,11 @@ class JsonObjectIterator {
|
||||
public:
|
||||
JsonObjectIterator() {}
|
||||
|
||||
explicit JsonObjectIterator(detail::VariantImpl::iterator iterator,
|
||||
detail::ResourceManager* resources)
|
||||
: iterator_(iterator), resources_(resources) {}
|
||||
explicit JsonObjectIterator(const detail::VariantImpl::iterator& iterator)
|
||||
: iterator_(iterator) {}
|
||||
|
||||
JsonPair operator*() const {
|
||||
return JsonPair(iterator_, resources_);
|
||||
return JsonPair(iterator_);
|
||||
}
|
||||
Ptr<JsonPair> operator->() {
|
||||
return operator*();
|
||||
@ -34,14 +33,13 @@ class JsonObjectIterator {
|
||||
}
|
||||
|
||||
JsonObjectIterator& operator++() {
|
||||
iterator_.next(resources_); // key
|
||||
iterator_.next(resources_); // value
|
||||
iterator_.next(); // key
|
||||
iterator_.next(); // value
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
detail::VariantImpl::iterator iterator_;
|
||||
detail::ResourceManager* resources_;
|
||||
};
|
||||
|
||||
class JsonObjectConstIterator {
|
||||
@ -50,12 +48,12 @@ class JsonObjectConstIterator {
|
||||
public:
|
||||
JsonObjectConstIterator() {}
|
||||
|
||||
explicit JsonObjectConstIterator(detail::VariantImpl::iterator iterator,
|
||||
detail::ResourceManager* resources)
|
||||
: iterator_(iterator), resources_(resources) {}
|
||||
explicit JsonObjectConstIterator(
|
||||
const detail::VariantImpl::iterator& iterator)
|
||||
: iterator_(iterator) {}
|
||||
|
||||
JsonPairConst operator*() const {
|
||||
return JsonPairConst(iterator_, resources_);
|
||||
return JsonPairConst(iterator_);
|
||||
}
|
||||
Ptr<JsonPairConst> operator->() {
|
||||
return operator*();
|
||||
@ -70,14 +68,13 @@ class JsonObjectConstIterator {
|
||||
}
|
||||
|
||||
JsonObjectConstIterator& operator++() {
|
||||
iterator_.next(resources_); // key
|
||||
iterator_.next(resources_); // value
|
||||
iterator_.next(); // key
|
||||
iterator_.next(); // value
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
detail::VariantImpl::iterator iterator_;
|
||||
detail::ResourceManager* resources_;
|
||||
};
|
||||
|
||||
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
||||
|
@ -15,12 +15,11 @@ ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE
|
||||
class JsonPair {
|
||||
public:
|
||||
// INTERNAL USE ONLY
|
||||
JsonPair(detail::VariantImpl::iterator iterator,
|
||||
detail::ResourceManager* resources) {
|
||||
JsonPair(detail::VariantImpl::iterator iterator) {
|
||||
if (!iterator.done()) {
|
||||
key_ = iterator.value(resources).asString();
|
||||
iterator.next(resources);
|
||||
value_ = JsonVariant(iterator.value(resources));
|
||||
key_ = iterator.value().asString();
|
||||
iterator.next();
|
||||
value_ = JsonVariant(iterator.value());
|
||||
}
|
||||
}
|
||||
|
||||
@ -43,12 +42,11 @@ class JsonPair {
|
||||
// https://arduinojson.org/v7/api/jsonobjectconst/begin_end/
|
||||
class JsonPairConst {
|
||||
public:
|
||||
JsonPairConst(detail::VariantImpl::iterator iterator,
|
||||
detail::ResourceManager* resources) {
|
||||
JsonPairConst(detail::VariantImpl::iterator iterator) {
|
||||
if (!iterator.done()) {
|
||||
key_ = iterator.value(resources).asString();
|
||||
iterator.next(resources);
|
||||
value_ = JsonVariantConst(iterator.value(resources));
|
||||
key_ = iterator.value().asString();
|
||||
iterator.next();
|
||||
value_ = JsonVariantConst(iterator.value());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -14,7 +14,7 @@ inline VariantData* VariantImpl::getMember(TAdaptedString key) const {
|
||||
auto it = findKey(key);
|
||||
if (it.done())
|
||||
return nullptr;
|
||||
it.next(resources_);
|
||||
it.next();
|
||||
return it.data();
|
||||
}
|
||||
|
||||
@ -33,9 +33,8 @@ inline VariantImpl::iterator VariantImpl::findKey(TAdaptedString key) const {
|
||||
if (key.isNull())
|
||||
return iterator();
|
||||
bool isKey = true;
|
||||
for (auto it = createIterator(); !it.done(); it.next(resources_)) {
|
||||
if (isKey &&
|
||||
stringEquals(key, adaptString(it.value(resources_).asString())))
|
||||
for (auto it = createIterator(); !it.done(); it.next()) {
|
||||
if (isKey && stringEquals(key, adaptString(it.value().asString())))
|
||||
return it;
|
||||
isKey = !isKey;
|
||||
}
|
||||
|
@ -461,7 +461,7 @@ class VariantImpl {
|
||||
|
||||
size_t count = 0;
|
||||
|
||||
for (auto it = createIterator(); !it.done(); it.next(resources_))
|
||||
for (auto it = createIterator(); !it.done(); it.next())
|
||||
count++;
|
||||
|
||||
if (data_->type == VariantType::Object)
|
||||
|
Reference in New Issue
Block a user