Skip to content

Commit 9a26c04

Browse files
committed
CollectionImpl: add allocVariant(), getVariant(), freeVariant()
1 parent 2de9a56 commit 9a26c04

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

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

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

src/ArduinoJson/Collection/CollectionImpl.hpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,14 @@ inline CollectionImpl::iterator CollectionImpl::createIterator() const {
2929
if (!data_)
3030
return iterator();
3131
auto coll = getCollectionData();
32-
return iterator(resources_->getVariant(coll->head), coll->head);
32+
return iterator(getVariant(coll->head), coll->head);
3333
}
3434

3535
inline void CollectionImpl::appendOne(Slot<VariantData> slot) {
36-
ARDUINOJSON_ASSERT(resources_ != nullptr);
37-
3836
auto coll = getCollectionData();
3937

4038
if (coll->tail != NULL_SLOT) {
41-
auto tail = resources_->getVariant(coll->tail);
39+
auto tail = getVariant(coll->tail);
4240
tail->next = slot.id();
4341
coll->tail = slot.id();
4442
} else {
@@ -49,14 +47,12 @@ inline void CollectionImpl::appendOne(Slot<VariantData> slot) {
4947

5048
inline void CollectionImpl::appendPair(Slot<VariantData> key,
5149
Slot<VariantData> value) {
52-
ARDUINOJSON_ASSERT(resources_ != nullptr);
53-
5450
auto coll = getCollectionData();
5551

5652
key->next = value.id();
5753

5854
if (coll->tail != NULL_SLOT) {
59-
auto tail = resources_->getVariant(coll->tail);
55+
auto tail = getVariant(coll->tail);
6056
tail->next = key.id();
6157
coll->tail = value.id();
6258
} else {
@@ -74,9 +70,9 @@ inline void CollectionImpl::clear() {
7470
auto next = coll->head;
7571
while (next != NULL_SLOT) {
7672
auto currId = next;
77-
auto slot = resources_->getVariant(next);
73+
auto slot = getVariant(next);
7874
next = slot->next;
79-
resources_->freeVariant({slot, currId});
75+
freeVariant({slot, currId});
8076
}
8177

8278
coll->head = NULL_SLOT;
@@ -89,7 +85,7 @@ inline Slot<VariantData> CollectionImpl::getPreviousSlot(
8985
auto prev = Slot<VariantData>();
9086
auto currentId = coll->head;
9187
while (currentId != NULL_SLOT) {
92-
auto currentSlot = resources_->getVariant(currentId);
88+
auto currentSlot = getVariant(currentId);
9389
if (currentSlot == target)
9490
break;
9591
prev = Slot<VariantData>(currentSlot, currentId);
@@ -111,7 +107,7 @@ inline void CollectionImpl::removeOne(iterator it) {
111107
coll->head = next;
112108
if (next == NULL_SLOT)
113109
coll->tail = prev.id();
114-
resources_->freeVariant({it.slot_, it.currentId_});
110+
freeVariant({it.slot_, it.currentId_});
115111
}
116112

117113
inline void CollectionImpl::removePair(ObjectImpl::iterator it) {
@@ -121,11 +117,11 @@ inline void CollectionImpl::removePair(ObjectImpl::iterator it) {
121117
auto keySlot = it.slot_;
122118

123119
auto valueId = it.nextId_;
124-
auto valueSlot = resources_->getVariant(valueId);
120+
auto valueSlot = getVariant(valueId);
125121

126122
// remove value slot
127123
keySlot->next = valueSlot->next;
128-
resources_->freeVariant({valueSlot, valueId});
124+
freeVariant({valueSlot, valueId});
129125

130126
// remove key slot
131127
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)