Skip to content

Commit 2de9a56

Browse files
committed
CollectionImpl: add helper getCollectionData()
1 parent e02fbe1 commit 2de9a56

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
@@ -103,7 +103,7 @@ class CollectionImpl {
103103
void clear();
104104

105105
SlotId head() const {
106-
return data_->head;
106+
return getCollectionData()->head;
107107
}
108108

109109
protected:
@@ -115,6 +115,11 @@ class CollectionImpl {
115115

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

120125
ARDUINOJSON_END_PRIVATE_NAMESPACE

src/ArduinoJson/Collection/CollectionImpl.hpp

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,59 +28,66 @@ inline void CollectionIterator::next(const ResourceManager* resources) {
2828
inline CollectionImpl::iterator CollectionImpl::createIterator() const {
2929
if (!data_)
3030
return iterator();
31-
return iterator(resources_->getVariant(data_->head), data_->head);
31+
auto coll = getCollectionData();
32+
return iterator(resources_->getVariant(coll->head), coll->head);
3233
}
3334

3435
inline void CollectionImpl::appendOne(Slot<VariantData> slot) {
35-
ARDUINOJSON_ASSERT(data_ != nullptr);
3636
ARDUINOJSON_ASSERT(resources_ != nullptr);
3737

38-
if (data_->tail != NULL_SLOT) {
39-
auto tail = resources_->getVariant(data_->tail);
38+
auto coll = getCollectionData();
39+
40+
if (coll->tail != NULL_SLOT) {
41+
auto tail = resources_->getVariant(coll->tail);
4042
tail->next = slot.id();
41-
data_->tail = slot.id();
43+
coll->tail = slot.id();
4244
} else {
43-
data_->head = slot.id();
44-
data_->tail = slot.id();
45+
coll->head = slot.id();
46+
coll->tail = slot.id();
4547
}
4648
}
4749

4850
inline void CollectionImpl::appendPair(Slot<VariantData> key,
4951
Slot<VariantData> value) {
50-
ARDUINOJSON_ASSERT(data_ != nullptr);
5152
ARDUINOJSON_ASSERT(resources_ != nullptr);
5253

54+
auto coll = getCollectionData();
55+
5356
key->next = value.id();
5457

55-
if (data_->tail != NULL_SLOT) {
56-
auto tail = resources_->getVariant(data_->tail);
58+
if (coll->tail != NULL_SLOT) {
59+
auto tail = resources_->getVariant(coll->tail);
5760
tail->next = key.id();
58-
data_->tail = value.id();
61+
coll->tail = value.id();
5962
} else {
60-
data_->head = key.id();
61-
data_->tail = value.id();
63+
coll->head = key.id();
64+
coll->tail = value.id();
6265
}
6366
}
6467

6568
inline void CollectionImpl::clear() {
6669
if (!data_)
6770
return;
68-
auto next = data_->head;
71+
72+
auto coll = getCollectionData();
73+
74+
auto next = coll->head;
6975
while (next != NULL_SLOT) {
7076
auto currId = next;
7177
auto slot = resources_->getVariant(next);
7278
next = slot->next;
7379
resources_->freeVariant({slot, currId});
7480
}
7581

76-
data_->head = NULL_SLOT;
77-
data_->tail = NULL_SLOT;
82+
coll->head = NULL_SLOT;
83+
coll->tail = NULL_SLOT;
7884
}
7985

8086
inline Slot<VariantData> CollectionImpl::getPreviousSlot(
8187
VariantData* target) const {
88+
auto coll = getCollectionData();
8289
auto prev = Slot<VariantData>();
83-
auto currentId = data_->head;
90+
auto currentId = coll->head;
8491
while (currentId != NULL_SLOT) {
8592
auto currentSlot = resources_->getVariant(currentId);
8693
if (currentSlot == target)
@@ -94,15 +101,16 @@ inline Slot<VariantData> CollectionImpl::getPreviousSlot(
94101
inline void CollectionImpl::removeOne(iterator it) {
95102
if (it.done())
96103
return;
104+
auto coll = getCollectionData();
97105
auto curr = it.slot_;
98106
auto prev = getPreviousSlot(curr);
99107
auto next = curr->next;
100108
if (prev)
101109
prev->next = next;
102110
else
103-
data_->head = next;
111+
coll->head = next;
104112
if (next == NULL_SLOT)
105-
data_->tail = prev.id();
113+
coll->tail = prev.id();
106114
resources_->freeVariant({it.slot_, it.currentId_});
107115
}
108116

0 commit comments

Comments
 (0)