From 2de9a56681633de2de62ff87a2225d01e9719610 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Thu, 3 Jul 2025 10:15:20 +0200 Subject: [PATCH] CollectionImpl: add helper `getCollectionData()` --- src/ArduinoJson/Collection/CollectionData.hpp | 7 ++- src/ArduinoJson/Collection/CollectionImpl.hpp | 46 +++++++++++-------- 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/src/ArduinoJson/Collection/CollectionData.hpp b/src/ArduinoJson/Collection/CollectionData.hpp index 549a9fdc..d2735b70 100644 --- a/src/ArduinoJson/Collection/CollectionData.hpp +++ b/src/ArduinoJson/Collection/CollectionData.hpp @@ -103,7 +103,7 @@ class CollectionImpl { void clear(); SlotId head() const { - return data_->head; + return getCollectionData()->head; } protected: @@ -115,6 +115,11 @@ class CollectionImpl { private: Slot getPreviousSlot(VariantData*) const; + + CollectionData* getCollectionData() const { + ARDUINOJSON_ASSERT(data_ != nullptr); + return data_; + } }; ARDUINOJSON_END_PRIVATE_NAMESPACE diff --git a/src/ArduinoJson/Collection/CollectionImpl.hpp b/src/ArduinoJson/Collection/CollectionImpl.hpp index 8c456c6a..898b7340 100644 --- a/src/ArduinoJson/Collection/CollectionImpl.hpp +++ b/src/ArduinoJson/Collection/CollectionImpl.hpp @@ -28,44 +28,50 @@ inline void CollectionIterator::next(const ResourceManager* resources) { inline CollectionImpl::iterator CollectionImpl::createIterator() const { if (!data_) 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 slot) { - ARDUINOJSON_ASSERT(data_ != nullptr); ARDUINOJSON_ASSERT(resources_ != nullptr); - if (data_->tail != NULL_SLOT) { - auto tail = resources_->getVariant(data_->tail); + auto coll = getCollectionData(); + + if (coll->tail != NULL_SLOT) { + auto tail = resources_->getVariant(coll->tail); tail->next = slot.id(); - data_->tail = slot.id(); + coll->tail = slot.id(); } else { - data_->head = slot.id(); - data_->tail = slot.id(); + coll->head = slot.id(); + coll->tail = slot.id(); } } inline void CollectionImpl::appendPair(Slot key, Slot value) { - ARDUINOJSON_ASSERT(data_ != nullptr); ARDUINOJSON_ASSERT(resources_ != nullptr); + auto coll = getCollectionData(); + key->next = value.id(); - if (data_->tail != NULL_SLOT) { - auto tail = resources_->getVariant(data_->tail); + if (coll->tail != NULL_SLOT) { + auto tail = resources_->getVariant(coll->tail); tail->next = key.id(); - data_->tail = value.id(); + coll->tail = value.id(); } else { - data_->head = key.id(); - data_->tail = value.id(); + coll->head = key.id(); + coll->tail = value.id(); } } inline void CollectionImpl::clear() { if (!data_) return; - auto next = data_->head; + + auto coll = getCollectionData(); + + auto next = coll->head; while (next != NULL_SLOT) { auto currId = next; auto slot = resources_->getVariant(next); @@ -73,14 +79,15 @@ inline void CollectionImpl::clear() { resources_->freeVariant({slot, currId}); } - data_->head = NULL_SLOT; - data_->tail = NULL_SLOT; + coll->head = NULL_SLOT; + coll->tail = NULL_SLOT; } inline Slot CollectionImpl::getPreviousSlot( VariantData* target) const { + auto coll = getCollectionData(); auto prev = Slot(); - auto currentId = data_->head; + auto currentId = coll->head; while (currentId != NULL_SLOT) { auto currentSlot = resources_->getVariant(currentId); if (currentSlot == target) @@ -94,15 +101,16 @@ inline Slot CollectionImpl::getPreviousSlot( inline void CollectionImpl::removeOne(iterator it) { if (it.done()) return; + auto coll = getCollectionData(); auto curr = it.slot_; auto prev = getPreviousSlot(curr); auto next = curr->next; if (prev) prev->next = next; else - data_->head = next; + coll->head = next; if (next == NULL_SLOT) - data_->tail = prev.id(); + coll->tail = prev.id(); resources_->freeVariant({it.slot_, it.currentId_}); }