CollectionImpl: add helper getCollectionData()

This commit is contained in:
Benoit Blanchon
2025-07-03 10:15:20 +02:00
parent a265ef4927
commit 6ba4507252
2 changed files with 33 additions and 20 deletions

View File

@ -104,7 +104,7 @@ class CollectionImpl {
void clear(); void clear();
SlotId head() const { SlotId head() const {
return data_->head; return getCollectionData()->head;
} }
protected: protected:
@ -116,6 +116,11 @@ class CollectionImpl {
private: private:
Slot<VariantData> getPreviousSlot(VariantData*) const; Slot<VariantData> getPreviousSlot(VariantData*) const;
CollectionData* getCollectionData() const {
ARDUINOJSON_ASSERT(data_ != nullptr);
return data_;
}
}; };
ARDUINOJSON_END_PRIVATE_NAMESPACE ARDUINOJSON_END_PRIVATE_NAMESPACE

View File

@ -22,44 +22,50 @@ inline void CollectionIterator::next(const ResourceManager* resources) {
inline CollectionImpl::iterator CollectionImpl::createIterator() const { inline CollectionImpl::iterator CollectionImpl::createIterator() const {
if (!data_) if (!data_)
return iterator(); return iterator();
return iterator(resources_->getVariant(data_->head), data_->head); auto coll = getCollectionData();
return iterator(resources_->getVariant(coll->head), coll->head);
} }
inline void CollectionImpl::appendOne(Slot<VariantData> slot) { inline void CollectionImpl::appendOne(Slot<VariantData> slot) {
ARDUINOJSON_ASSERT(data_ != nullptr);
ARDUINOJSON_ASSERT(resources_ != nullptr); ARDUINOJSON_ASSERT(resources_ != nullptr);
if (data_->tail != NULL_SLOT) { auto coll = getCollectionData();
auto tail = resources_->getVariant(data_->tail);
if (coll->tail != NULL_SLOT) {
auto tail = resources_->getVariant(coll->tail);
tail->next = slot.id(); tail->next = slot.id();
data_->tail = slot.id(); coll->tail = slot.id();
} else { } else {
data_->head = slot.id(); coll->head = slot.id();
data_->tail = slot.id(); coll->tail = slot.id();
} }
} }
inline void CollectionImpl::appendPair(Slot<VariantData> key, inline void CollectionImpl::appendPair(Slot<VariantData> key,
Slot<VariantData> value) { Slot<VariantData> value) {
ARDUINOJSON_ASSERT(data_ != nullptr);
ARDUINOJSON_ASSERT(resources_ != nullptr); ARDUINOJSON_ASSERT(resources_ != nullptr);
auto coll = getCollectionData();
key->next = value.id(); key->next = value.id();
if (data_->tail != NULL_SLOT) { if (coll->tail != NULL_SLOT) {
auto tail = resources_->getVariant(data_->tail); auto tail = resources_->getVariant(coll->tail);
tail->next = key.id(); tail->next = key.id();
data_->tail = value.id(); coll->tail = value.id();
} else { } else {
data_->head = key.id(); coll->head = key.id();
data_->tail = value.id(); coll->tail = value.id();
} }
} }
inline void CollectionImpl::clear() { inline void CollectionImpl::clear() {
if (!data_) if (!data_)
return; return;
auto next = data_->head;
auto coll = getCollectionData();
auto next = coll->head;
while (next != NULL_SLOT) { while (next != NULL_SLOT) {
auto currId = next; auto currId = next;
auto slot = resources_->getVariant(next); auto slot = resources_->getVariant(next);
@ -67,14 +73,15 @@ inline void CollectionImpl::clear() {
resources_->freeVariant({slot, currId}); resources_->freeVariant({slot, currId});
} }
data_->head = NULL_SLOT; coll->head = NULL_SLOT;
data_->tail = NULL_SLOT; coll->tail = NULL_SLOT;
} }
inline Slot<VariantData> CollectionImpl::getPreviousSlot( inline Slot<VariantData> CollectionImpl::getPreviousSlot(
VariantData* target) const { VariantData* target) const {
auto coll = getCollectionData();
auto prev = Slot<VariantData>(); auto prev = Slot<VariantData>();
auto currentId = data_->head; auto currentId = coll->head;
while (currentId != NULL_SLOT) { while (currentId != NULL_SLOT) {
auto currentSlot = resources_->getVariant(currentId); auto currentSlot = resources_->getVariant(currentId);
if (currentSlot == target) if (currentSlot == target)
@ -88,15 +95,16 @@ inline Slot<VariantData> CollectionImpl::getPreviousSlot(
inline void CollectionImpl::removeOne(iterator it) { inline void CollectionImpl::removeOne(iterator it) {
if (it.done()) if (it.done())
return; return;
auto coll = getCollectionData();
auto curr = it.slot_; auto curr = it.slot_;
auto prev = getPreviousSlot(curr); auto prev = getPreviousSlot(curr);
auto next = curr->next; auto next = curr->next;
if (prev) if (prev)
prev->next = next; prev->next = next;
else else
data_->head = next; coll->head = next;
if (next == NULL_SLOT) if (next == NULL_SLOT)
data_->tail = prev.id(); coll->tail = prev.id();
resources_->freeVariant({it.slot_, it.currentId_}); resources_->freeVariant({it.slot_, it.currentId_});
} }