Skip to content

Commit 6f28ec0

Browse files
committed
CollectionImpl: add allocVariant(), getVariant(), freeVariant()
1 parent 6ba4507 commit 6f28ec0

File tree

4 files changed

+31
-25
lines changed

4 files changed

+31
-25
lines changed

src/ArduinoJson/Array/ArrayImpl.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ inline ArrayImpl::iterator ArrayImpl::at(size_t index) const {
2222
inline VariantData* ArrayImpl::addElement() {
2323
if (!data_)
2424
return nullptr;
25-
ARDUINOJSON_ASSERT(resources_ != nullptr);
26-
auto slot = resources_->allocVariant();
25+
auto slot = allocVariant();
2726
if (!slot)
2827
return nullptr;
2928
CollectionImpl::appendOne(slot);
@@ -60,13 +59,12 @@ template <typename T>
6059
inline bool ArrayImpl::addValue(const T& value) {
6160
if (!data_)
6261
return false;
63-
ARDUINOJSON_ASSERT(resources_ != nullptr);
64-
auto slot = resources_->allocVariant();
62+
auto slot = allocVariant();
6563
if (!slot)
6664
return false;
6765
JsonVariant variant(slot.ptr(), resources_);
6866
if (!variant.set(value)) {
69-
resources_->freeVariant(slot);
67+
freeVariant(slot);
7068
return false;
7169
}
7270
CollectionImpl::appendOne(slot);

src/ArduinoJson/Collection/CollectionData.hpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
#pragma once
66

7-
#include <ArduinoJson/Memory/MemoryPool.hpp>
87
#include <ArduinoJson/Namespace.hpp>
98
#include <ArduinoJson/Polyfills/assert.hpp>
109

@@ -114,6 +113,21 @@ class CollectionImpl {
114113
void removeOne(iterator it);
115114
void removePair(iterator it);
116115

116+
VariantData* getVariant(SlotId id) const {
117+
ARDUINOJSON_ASSERT(resources_ != nullptr);
118+
return resources_->getVariant(id);
119+
}
120+
121+
void freeVariant(Slot<VariantData> slot) {
122+
ARDUINOJSON_ASSERT(resources_ != nullptr);
123+
resources_->freeVariant(slot);
124+
}
125+
126+
Slot<VariantData> allocVariant() {
127+
ARDUINOJSON_ASSERT(resources_ != nullptr);
128+
return resources_->allocVariant();
129+
}
130+
117131
private:
118132
Slot<VariantData> getPreviousSlot(VariantData*) const;
119133

src/ArduinoJson/Collection/CollectionImpl.hpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,14 @@ inline CollectionImpl::iterator CollectionImpl::createIterator() const {
2323
if (!data_)
2424
return iterator();
2525
auto coll = getCollectionData();
26-
return iterator(resources_->getVariant(coll->head), coll->head);
26+
return iterator(getVariant(coll->head), coll->head);
2727
}
2828

2929
inline void CollectionImpl::appendOne(Slot<VariantData> slot) {
30-
ARDUINOJSON_ASSERT(resources_ != nullptr);
31-
3230
auto coll = getCollectionData();
3331

3432
if (coll->tail != NULL_SLOT) {
35-
auto tail = resources_->getVariant(coll->tail);
33+
auto tail = getVariant(coll->tail);
3634
tail->next = slot.id();
3735
coll->tail = slot.id();
3836
} else {
@@ -43,14 +41,12 @@ inline void CollectionImpl::appendOne(Slot<VariantData> slot) {
4341

4442
inline void CollectionImpl::appendPair(Slot<VariantData> key,
4543
Slot<VariantData> value) {
46-
ARDUINOJSON_ASSERT(resources_ != nullptr);
47-
4844
auto coll = getCollectionData();
4945

5046
key->next = value.id();
5147

5248
if (coll->tail != NULL_SLOT) {
53-
auto tail = resources_->getVariant(coll->tail);
49+
auto tail = getVariant(coll->tail);
5450
tail->next = key.id();
5551
coll->tail = value.id();
5652
} else {
@@ -68,9 +64,9 @@ inline void CollectionImpl::clear() {
6864
auto next = coll->head;
6965
while (next != NULL_SLOT) {
7066
auto currId = next;
71-
auto slot = resources_->getVariant(next);
67+
auto slot = getVariant(next);
7268
next = slot->next;
73-
resources_->freeVariant({slot, currId});
69+
freeVariant({slot, currId});
7470
}
7571

7672
coll->head = NULL_SLOT;
@@ -83,7 +79,7 @@ inline Slot<VariantData> CollectionImpl::getPreviousSlot(
8379
auto prev = Slot<VariantData>();
8480
auto currentId = coll->head;
8581
while (currentId != NULL_SLOT) {
86-
auto currentSlot = resources_->getVariant(currentId);
82+
auto currentSlot = getVariant(currentId);
8783
if (currentSlot == target)
8884
break;
8985
prev = Slot<VariantData>(currentSlot, currentId);
@@ -105,7 +101,7 @@ inline void CollectionImpl::removeOne(iterator it) {
105101
coll->head = next;
106102
if (next == NULL_SLOT)
107103
coll->tail = prev.id();
108-
resources_->freeVariant({it.slot_, it.currentId_});
104+
freeVariant({it.slot_, it.currentId_});
109105
}
110106

111107
inline void CollectionImpl::removePair(ObjectImpl::iterator it) {
@@ -115,11 +111,11 @@ inline void CollectionImpl::removePair(ObjectImpl::iterator it) {
115111
auto keySlot = it.slot_;
116112

117113
auto valueId = keySlot->next;
118-
auto valueSlot = resources_->getVariant(valueId);
114+
auto valueSlot = getVariant(valueId);
119115

120116
// remove value slot
121117
keySlot->next = valueSlot->next;
122-
resources_->freeVariant({valueSlot, valueId});
118+
freeVariant({valueSlot, valueId});
123119

124120
// remove key slot
125121
removeOne(it);

src/ArduinoJson/Object/ObjectImpl.hpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,12 @@ template <typename TAdaptedString>
5050
inline VariantData* ObjectImpl::addMember(TAdaptedString key) {
5151
if (!data_)
5252
return nullptr;
53-
ARDUINOJSON_ASSERT(resources_ != nullptr);
5453

55-
auto keySlot = resources_->allocVariant();
54+
auto keySlot = allocVariant();
5655
if (!keySlot)
5756
return nullptr;
5857

59-
auto valueSlot = resources_->allocVariant();
58+
auto valueSlot = allocVariant();
6059
if (!valueSlot)
6160
return nullptr;
6261

@@ -72,13 +71,12 @@ inline VariantData* ObjectImpl::addMember(TAdaptedString key) {
7271
inline VariantData* ObjectImpl::addPair(VariantData** value) {
7372
if (!data_)
7473
return nullptr;
75-
ARDUINOJSON_ASSERT(resources_ != nullptr);
7674

77-
auto keySlot = resources_->allocVariant();
75+
auto keySlot = allocVariant();
7876
if (!keySlot)
7977
return nullptr;
8078

81-
auto valueSlot = resources_->allocVariant();
79+
auto valueSlot = allocVariant();
8280
if (!valueSlot)
8381
return nullptr;
8482
*value = valueSlot.ptr();

0 commit comments

Comments
 (0)