Skip to content

Commit 6ba4507

Browse files
committed
CollectionImpl: add helper getCollectionData()
1 parent a265ef4 commit 6ba4507

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

src/ArduinoJson/Collection/CollectionData.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class CollectionImpl {
104104
void clear();
105105

106106
SlotId head() const {
107-
return data_->head;
107+
return getCollectionData()->head;
108108
}
109109

110110
protected:
@@ -116,6 +116,11 @@ class CollectionImpl {
116116

117117
private:
118118
Slot<VariantData> getPreviousSlot(VariantData*) const;
119+
120+
CollectionData* getCollectionData() const {
121+
ARDUINOJSON_ASSERT(data_ != nullptr);
122+
return data_;
123+
}
119124
};
120125

121126
ARDUINOJSON_END_PRIVATE_NAMESPACE

src/ArduinoJson/Collection/CollectionImpl.hpp

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,59 +22,66 @@ inline void CollectionIterator::next(const ResourceManager* resources) {
2222
inline CollectionImpl::iterator CollectionImpl::createIterator() const {
2323
if (!data_)
2424
return iterator();
25-
return iterator(resources_->getVariant(data_->head), data_->head);
25+
auto coll = getCollectionData();
26+
return iterator(resources_->getVariant(coll->head), coll->head);
2627
}
2728

2829
inline void CollectionImpl::appendOne(Slot<VariantData> slot) {
29-
ARDUINOJSON_ASSERT(data_ != nullptr);
3030
ARDUINOJSON_ASSERT(resources_ != nullptr);
3131

32-
if (data_->tail != NULL_SLOT) {
33-
auto tail = resources_->getVariant(data_->tail);
32+
auto coll = getCollectionData();
33+
34+
if (coll->tail != NULL_SLOT) {
35+
auto tail = resources_->getVariant(coll->tail);
3436
tail->next = slot.id();
35-
data_->tail = slot.id();
37+
coll->tail = slot.id();
3638
} else {
37-
data_->head = slot.id();
38-
data_->tail = slot.id();
39+
coll->head = slot.id();
40+
coll->tail = slot.id();
3941
}
4042
}
4143

4244
inline void CollectionImpl::appendPair(Slot<VariantData> key,
4345
Slot<VariantData> value) {
44-
ARDUINOJSON_ASSERT(data_ != nullptr);
4546
ARDUINOJSON_ASSERT(resources_ != nullptr);
4647

48+
auto coll = getCollectionData();
49+
4750
key->next = value.id();
4851

49-
if (data_->tail != NULL_SLOT) {
50-
auto tail = resources_->getVariant(data_->tail);
52+
if (coll->tail != NULL_SLOT) {
53+
auto tail = resources_->getVariant(coll->tail);
5154
tail->next = key.id();
52-
data_->tail = value.id();
55+
coll->tail = value.id();
5356
} else {
54-
data_->head = key.id();
55-
data_->tail = value.id();
57+
coll->head = key.id();
58+
coll->tail = value.id();
5659
}
5760
}
5861

5962
inline void CollectionImpl::clear() {
6063
if (!data_)
6164
return;
62-
auto next = data_->head;
65+
66+
auto coll = getCollectionData();
67+
68+
auto next = coll->head;
6369
while (next != NULL_SLOT) {
6470
auto currId = next;
6571
auto slot = resources_->getVariant(next);
6672
next = slot->next;
6773
resources_->freeVariant({slot, currId});
6874
}
6975

70-
data_->head = NULL_SLOT;
71-
data_->tail = NULL_SLOT;
76+
coll->head = NULL_SLOT;
77+
coll->tail = NULL_SLOT;
7278
}
7379

7480
inline Slot<VariantData> CollectionImpl::getPreviousSlot(
7581
VariantData* target) const {
82+
auto coll = getCollectionData();
7683
auto prev = Slot<VariantData>();
77-
auto currentId = data_->head;
84+
auto currentId = coll->head;
7885
while (currentId != NULL_SLOT) {
7986
auto currentSlot = resources_->getVariant(currentId);
8087
if (currentSlot == target)
@@ -88,15 +95,16 @@ inline Slot<VariantData> CollectionImpl::getPreviousSlot(
8895
inline void CollectionImpl::removeOne(iterator it) {
8996
if (it.done())
9097
return;
98+
auto coll = getCollectionData();
9199
auto curr = it.slot_;
92100
auto prev = getPreviousSlot(curr);
93101
auto next = curr->next;
94102
if (prev)
95103
prev->next = next;
96104
else
97-
data_->head = next;
105+
coll->head = next;
98106
if (next == NULL_SLOT)
99-
data_->tail = prev.id();
107+
coll->tail = prev.id();
100108
resources_->freeVariant({it.slot_, it.currentId_});
101109
}
102110

0 commit comments

Comments
 (0)