mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-30 02:37:35 +02:00
Add VariantImpl
in CollectionIterator
This commit is contained in:
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <ArduinoJson/Collection/CollectionIterator.hpp>
|
||||||
#include <ArduinoJson/Variant/VariantCompare.hpp>
|
#include <ArduinoJson/Variant/VariantCompare.hpp>
|
||||||
#include <ArduinoJson/Variant/VariantImpl.hpp>
|
#include <ArduinoJson/Variant/VariantImpl.hpp>
|
||||||
|
|
||||||
@ -53,6 +54,12 @@ inline VariantData* VariantImpl::getElement(size_t index) const {
|
|||||||
return at(index).data();
|
return at(index).data();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline void VariantImpl::removeElement(iterator it) {
|
||||||
|
if (!isArray())
|
||||||
|
return;
|
||||||
|
removeOne(it);
|
||||||
|
}
|
||||||
|
|
||||||
inline void VariantImpl::removeElement(size_t index) {
|
inline void VariantImpl::removeElement(size_t index) {
|
||||||
removeElement(at(index));
|
removeElement(at(index));
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <ArduinoJson/Collection/CollectionIterator.hpp>
|
||||||
#include <ArduinoJson/Variant/JsonVariant.hpp>
|
#include <ArduinoJson/Variant/JsonVariant.hpp>
|
||||||
|
|
||||||
ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE
|
ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE
|
||||||
|
@ -4,7 +4,6 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <ArduinoJson/Collection/CollectionIterator.hpp>
|
|
||||||
#include <ArduinoJson/Memory/Alignment.hpp>
|
#include <ArduinoJson/Memory/Alignment.hpp>
|
||||||
#include <ArduinoJson/Strings/StringAdapters.hpp>
|
#include <ArduinoJson/Strings/StringAdapters.hpp>
|
||||||
#include <ArduinoJson/Variant/VariantCompare.hpp>
|
#include <ArduinoJson/Variant/VariantCompare.hpp>
|
||||||
@ -12,18 +11,6 @@
|
|||||||
|
|
||||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||||
|
|
||||||
inline void CollectionIterator::next() {
|
|
||||||
ARDUINOJSON_ASSERT(slot_);
|
|
||||||
auto nextId = slot_->next;
|
|
||||||
slot_ = resources_->getVariant(nextId);
|
|
||||||
currentId_ = nextId;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline VariantImpl CollectionIterator::value() const {
|
|
||||||
ARDUINOJSON_ASSERT(slot_ != nullptr);
|
|
||||||
return VariantImpl(slot_, resources_);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline VariantImpl::iterator VariantImpl::createIterator() const {
|
inline VariantImpl::iterator VariantImpl::createIterator() const {
|
||||||
if (!data_ || !data_->isCollection())
|
if (!data_ || !data_->isCollection())
|
||||||
return iterator();
|
return iterator();
|
||||||
@ -78,7 +65,7 @@ inline void VariantImpl::removeOne(iterator it) {
|
|||||||
if (it.done())
|
if (it.done())
|
||||||
return;
|
return;
|
||||||
auto coll = getCollectionData();
|
auto coll = getCollectionData();
|
||||||
auto curr = it.slot_;
|
auto curr = it.data();
|
||||||
auto prev = getPreviousSlot(curr);
|
auto prev = getPreviousSlot(curr);
|
||||||
auto next = curr->next;
|
auto next = curr->next;
|
||||||
if (prev)
|
if (prev)
|
||||||
@ -87,14 +74,14 @@ inline void VariantImpl::removeOne(iterator it) {
|
|||||||
coll->head = next;
|
coll->head = next;
|
||||||
if (next == NULL_SLOT)
|
if (next == NULL_SLOT)
|
||||||
coll->tail = prev.id();
|
coll->tail = prev.id();
|
||||||
freeVariant({it.slot_, it.currentId_});
|
freeVariant({it.data(), it.currentId_});
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void VariantImpl::removePair(VariantImpl::iterator it) {
|
inline void VariantImpl::removePair(VariantImpl::iterator it) {
|
||||||
if (it.done())
|
if (it.done())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
auto keySlot = it.slot_;
|
auto keySlot = it.data();
|
||||||
|
|
||||||
auto valueId = keySlot->next;
|
auto valueId = keySlot->next;
|
||||||
auto valueSlot = getVariant(valueId);
|
auto valueSlot = getVariant(valueId);
|
||||||
@ -119,4 +106,19 @@ inline size_t VariantImpl::nesting() const {
|
|||||||
return maxChildNesting + 1;
|
return maxChildNesting + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline size_t VariantImpl::size() const {
|
||||||
|
if (!data_)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
size_t count = 0;
|
||||||
|
|
||||||
|
for (auto it = createIterator(); !it.done(); it.next())
|
||||||
|
count++;
|
||||||
|
|
||||||
|
if (data_->type == VariantType::Object)
|
||||||
|
count /= 2; // TODO: do this in JsonObject?
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
#include <ArduinoJson/Namespace.hpp>
|
#include <ArduinoJson/Namespace.hpp>
|
||||||
#include <ArduinoJson/Polyfills/assert.hpp>
|
#include <ArduinoJson/Polyfills/assert.hpp>
|
||||||
|
#include <ArduinoJson/Variant/VariantImpl.hpp>
|
||||||
|
|
||||||
#include <stddef.h> // size_t
|
#include <stddef.h> // size_t
|
||||||
|
|
||||||
@ -21,39 +22,44 @@ class CollectionIterator {
|
|||||||
public:
|
public:
|
||||||
CollectionIterator() {}
|
CollectionIterator() {}
|
||||||
|
|
||||||
void next();
|
void next() {
|
||||||
|
ARDUINOJSON_ASSERT(!done());
|
||||||
|
auto nextId = value_.getData()->next;
|
||||||
|
auto resources = value_.getResourceManager();
|
||||||
|
value_ = VariantImpl(resources->getVariant(nextId), resources);
|
||||||
|
currentId_ = nextId;
|
||||||
|
}
|
||||||
|
|
||||||
VariantImpl value() const;
|
const VariantImpl& value() const {
|
||||||
|
return value_;
|
||||||
|
}
|
||||||
|
|
||||||
bool done() const {
|
bool done() const {
|
||||||
return slot_ == nullptr;
|
return value_.isUnbound();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator==(const CollectionIterator& other) const {
|
bool operator==(const CollectionIterator& other) const {
|
||||||
return slot_ == other.slot_;
|
return data() == other.data();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator!=(const CollectionIterator& other) const {
|
bool operator!=(const CollectionIterator& other) const {
|
||||||
return slot_ != other.slot_;
|
return !operator==(other);
|
||||||
}
|
}
|
||||||
|
|
||||||
VariantData* data() {
|
VariantData* data() {
|
||||||
return slot_;
|
return value_.getData();
|
||||||
}
|
}
|
||||||
|
|
||||||
const VariantData* data() const {
|
const VariantData* data() const {
|
||||||
return slot_;
|
return value_.getData();
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
CollectionIterator(SlotId slotId, ResourceManager* resources)
|
CollectionIterator(SlotId slotId, ResourceManager* resources)
|
||||||
: slot_(resources->getVariant(slotId)),
|
: value_(resources->getVariant(slotId), resources), currentId_(slotId) {}
|
||||||
currentId_(slotId),
|
|
||||||
resources_(resources) {}
|
|
||||||
|
|
||||||
VariantData* slot_ = nullptr;
|
VariantImpl value_;
|
||||||
SlotId currentId_ = NULL_SLOT;
|
SlotId currentId_ = NULL_SLOT;
|
||||||
ResourceManager* resources_ = nullptr;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -46,6 +46,10 @@ inline void VariantImpl::removeMember(TAdaptedString key) {
|
|||||||
removeMember(findKey(key));
|
removeMember(findKey(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline void VariantImpl::removeMember(iterator it) {
|
||||||
|
removePair(it);
|
||||||
|
}
|
||||||
|
|
||||||
template <typename TAdaptedString>
|
template <typename TAdaptedString>
|
||||||
inline VariantData* VariantImpl::addMember(TAdaptedString key) {
|
inline VariantData* VariantImpl::addMember(TAdaptedString key) {
|
||||||
if (!isObject())
|
if (!isObject())
|
||||||
|
@ -4,7 +4,6 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <ArduinoJson/Collection/CollectionIterator.hpp>
|
|
||||||
#include <ArduinoJson/Memory/ResourceManager.hpp>
|
#include <ArduinoJson/Memory/ResourceManager.hpp>
|
||||||
#include <ArduinoJson/Misc/SerializedValue.hpp>
|
#include <ArduinoJson/Misc/SerializedValue.hpp>
|
||||||
#include <ArduinoJson/Numbers/convertNumber.hpp>
|
#include <ArduinoJson/Numbers/convertNumber.hpp>
|
||||||
@ -14,6 +13,8 @@
|
|||||||
|
|
||||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||||
|
|
||||||
|
class CollectionIterator;
|
||||||
|
|
||||||
class VariantImpl {
|
class VariantImpl {
|
||||||
VariantData* data_;
|
VariantData* data_;
|
||||||
ResourceManager* resources_;
|
ResourceManager* resources_;
|
||||||
@ -35,7 +36,7 @@ class VariantImpl {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename TVisitor>
|
template <typename TVisitor>
|
||||||
typename TVisitor::result_type accept(TVisitor& visit) {
|
typename TVisitor::result_type accept(TVisitor& visit) const {
|
||||||
if (!data_)
|
if (!data_)
|
||||||
return visit.visit(nullptr);
|
return visit.visit(nullptr);
|
||||||
|
|
||||||
@ -337,20 +338,14 @@ class VariantImpl {
|
|||||||
|
|
||||||
size_t nesting() const;
|
size_t nesting() const;
|
||||||
|
|
||||||
void removeElement(iterator it) {
|
void removeElement(iterator it);
|
||||||
if (!isArray())
|
|
||||||
return;
|
|
||||||
removeOne(it);
|
|
||||||
}
|
|
||||||
|
|
||||||
void removeElement(size_t index);
|
void removeElement(size_t index);
|
||||||
|
|
||||||
template <typename TAdaptedString>
|
template <typename TAdaptedString>
|
||||||
void removeMember(TAdaptedString key);
|
void removeMember(TAdaptedString key);
|
||||||
|
|
||||||
void removeMember(iterator it) {
|
void removeMember(iterator it);
|
||||||
removePair(it);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool setBoolean(bool value) {
|
bool setBoolean(bool value) {
|
||||||
if (!data_)
|
if (!data_)
|
||||||
@ -455,20 +450,7 @@ class VariantImpl {
|
|||||||
|
|
||||||
void empty();
|
void empty();
|
||||||
|
|
||||||
size_t size() const {
|
size_t size() const;
|
||||||
if (!data_)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
size_t count = 0;
|
|
||||||
|
|
||||||
for (auto it = createIterator(); !it.done(); it.next())
|
|
||||||
count++;
|
|
||||||
|
|
||||||
if (data_->type == VariantType::Object)
|
|
||||||
count /= 2; // TODO: do this in JsonObject?
|
|
||||||
|
|
||||||
return count;
|
|
||||||
}
|
|
||||||
|
|
||||||
VariantType type() const {
|
VariantType type() const {
|
||||||
return data_ ? data_->type : VariantType::Null;
|
return data_ ? data_->type : VariantType::Null;
|
||||||
|
Reference in New Issue
Block a user