Skip to content

Commit f5e7570

Browse files
committed
Simplify CollectionData to work only with VariantSlot*
1 parent 0030874 commit f5e7570

12 files changed

+41
-74
lines changed

src/ArduinoJson/Array/JsonArrayConst.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class JsonArrayConst : public detail::VariantOperators<JsonArrayConst> {
7070
// Returns the element at the specified index.
7171
// https://arduinojson.org/v6/api/jsonarrayconst/subscript/
7272
FORCE_INLINE JsonVariantConst operator[](size_t index) const {
73-
return JsonVariantConst(_data ? _data->getElement(index) : 0);
73+
return JsonVariantConst(_data ? slotData(_data->get(index)) : 0);
7474
}
7575

7676
operator JsonVariantConst() const {

src/ArduinoJson/Collection/CollectionData.hpp

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,46 +19,26 @@ class CollectionData {
1919
VariantSlot* _tail;
2020

2121
public:
22-
// Must be a POD!
23-
// - no constructor
24-
// - no destructor
25-
// - no virtual
26-
// - no inheritance
27-
28-
// Array only
29-
30-
VariantData* getElement(size_t index) const;
31-
32-
// Object only
33-
34-
template <typename TAdaptedString>
35-
VariantData* getMember(TAdaptedString key) const;
36-
37-
template <typename TAdaptedString>
38-
bool containsKey(const TAdaptedString& key) const;
39-
40-
// Generic
41-
4222
void clear();
4323
size_t memoryUsage() const;
4424
size_t size() const;
4525

46-
void addSlot(VariantSlot*);
47-
void removeSlot(VariantSlot* slot);
26+
void add(VariantSlot*);
27+
void remove(VariantSlot* slot);
4828

4929
VariantSlot* head() const {
5030
return _head;
5131
}
5232

5333
void movePointers(ptrdiff_t variantDistance);
5434

55-
VariantSlot* getSlot(size_t index) const;
35+
VariantSlot* get(size_t index) const;
5636

5737
template <typename TAdaptedString>
58-
VariantSlot* getSlot(TAdaptedString key) const;
38+
VariantSlot* get(TAdaptedString key) const;
5939

6040
private:
61-
VariantSlot* getPreviousSlot(VariantSlot*) const;
41+
VariantSlot* getPrevious(VariantSlot*) const;
6242
};
6343

6444
inline const VariantData* collectionToVariant(

src/ArduinoJson/Collection/CollectionFunctions.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ inline VariantData* collectionAddElement(CollectionData* array,
1515
auto slot = pool->allocVariant();
1616
if (!slot)
1717
return nullptr;
18-
array->addSlot(slot);
18+
array->add(slot);
1919
return slot->data();
2020
}
2121

@@ -33,7 +33,7 @@ inline VariantData* collectionAddMember(CollectionData* obj, TAdaptedString key,
3333
if (!storedKey)
3434
return nullptr;
3535
slot->setKey(storedKey);
36-
obj->addSlot(slot);
36+
obj->add(slot);
3737
return slot->data();
3838
}
3939

@@ -71,23 +71,23 @@ inline void collectionRemove(CollectionData* data, VariantSlot* slot,
7171
MemoryPool* pool) {
7272
if (!data || !slot)
7373
return;
74-
data->removeSlot(slot);
74+
data->remove(slot);
7575
slotRelease(slot, pool);
7676
}
7777

7878
inline void collectionRemoveElement(CollectionData* array, size_t index,
7979
MemoryPool* pool) {
8080
if (!array)
8181
return;
82-
collectionRemove(array, array->getSlot(index), pool);
82+
collectionRemove(array, array->get(index), pool);
8383
}
8484

8585
template <typename TAdaptedString>
8686
inline void collectionRemoveMember(CollectionData* obj, TAdaptedString key,
8787
MemoryPool* pool) {
8888
if (!obj)
8989
return;
90-
collectionRemove(obj, obj->getSlot(key), pool);
90+
collectionRemove(obj, obj->get(key), pool);
9191
}
9292

9393
ARDUINOJSON_END_PRIVATE_NAMESPACE

src/ArduinoJson/Collection/CollectionImpl.hpp

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
1313

14-
inline void CollectionData::addSlot(VariantSlot* slot) {
14+
inline void CollectionData::add(VariantSlot* slot) {
1515
ARDUINOJSON_ASSERT(slot != nullptr);
1616

1717
if (_tail) {
@@ -29,12 +29,7 @@ inline void CollectionData::clear() {
2929
}
3030

3131
template <typename TAdaptedString>
32-
inline bool CollectionData::containsKey(const TAdaptedString& key) const {
33-
return getSlot(key) != 0;
34-
}
35-
36-
template <typename TAdaptedString>
37-
inline VariantSlot* CollectionData::getSlot(TAdaptedString key) const {
32+
inline VariantSlot* CollectionData::get(TAdaptedString key) const {
3833
if (key.isNull())
3934
return 0;
4035
VariantSlot* slot = _head;
@@ -46,13 +41,13 @@ inline VariantSlot* CollectionData::getSlot(TAdaptedString key) const {
4641
return slot;
4742
}
4843

49-
inline VariantSlot* CollectionData::getSlot(size_t index) const {
44+
inline VariantSlot* CollectionData::get(size_t index) const {
5045
if (!_head)
5146
return 0;
5247
return _head->next(index);
5348
}
5449

55-
inline VariantSlot* CollectionData::getPreviousSlot(VariantSlot* target) const {
50+
inline VariantSlot* CollectionData::getPrevious(VariantSlot* target) const {
5651
VariantSlot* current = _head;
5752
while (current) {
5853
VariantSlot* next = current->next();
@@ -63,21 +58,10 @@ inline VariantSlot* CollectionData::getPreviousSlot(VariantSlot* target) const {
6358
return 0;
6459
}
6560

66-
template <typename TAdaptedString>
67-
inline VariantData* CollectionData::getMember(TAdaptedString key) const {
68-
VariantSlot* slot = getSlot(key);
69-
return slot ? slot->data() : 0;
70-
}
71-
72-
inline VariantData* CollectionData::getElement(size_t index) const {
73-
VariantSlot* slot = getSlot(index);
74-
return slot ? slot->data() : 0;
75-
}
76-
77-
inline void CollectionData::removeSlot(VariantSlot* slot) {
61+
inline void CollectionData::remove(VariantSlot* slot) {
7862
if (!slot)
7963
return;
80-
VariantSlot* prev = getPreviousSlot(slot);
64+
VariantSlot* prev = getPrevious(slot);
8165
VariantSlot* next = slot->next();
8266
if (prev)
8367
prev->setNext(next);

src/ArduinoJson/Json/JsonDeserializer.hpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -274,24 +274,23 @@ class JsonDeserializer {
274274
TFilter memberFilter = filter[key.c_str()];
275275

276276
if (memberFilter.allow()) {
277-
VariantData* variant = object.getMember(adaptString(key.c_str()));
278-
if (!variant) {
277+
VariantSlot* slot = object.get(adaptString(key.c_str()));
278+
if (!slot) {
279279
// Save key in memory pool.
280280
key = _stringStorage.save();
281281

282282
// Allocate slot in object
283-
VariantSlot* slot = _pool->allocVariant();
283+
slot = _pool->allocVariant();
284284
if (!slot)
285285
return DeserializationError::NoMemory;
286286

287287
slot->setKey(key);
288-
object.addSlot(slot);
289-
290-
variant = slot->data();
288+
object.add(slot);
291289
}
292290

293291
// Parse value
294-
err = parseVariant(*variant, memberFilter, nestingLimit.decrement());
292+
err =
293+
parseVariant(*slot->data(), memberFilter, nestingLimit.decrement());
295294
if (err)
296295
return err;
297296
} else {

src/ArduinoJson/MsgPack/MsgPackDeserializer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ class MsgPackDeserializer {
501501
return DeserializationError::NoMemory;
502502

503503
slot->setKey(key);
504-
object->addSlot(slot);
504+
object->add(slot);
505505

506506
member = slot->data();
507507
} else {

src/ArduinoJson/Object/JsonObject.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,12 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
203203
inline detail::VariantData* getMember(TAdaptedString key) const {
204204
if (!_data)
205205
return 0;
206-
return _data->getMember(key);
206+
return slotData(_data->get(key));
207207
}
208208

209209
template <typename TAdaptedString>
210210
void removeMember(TAdaptedString key) const {
211-
collectionRemove(_data, _data->getSlot(key), _pool);
211+
collectionRemove(_data, _data->get(key), _pool);
212212
}
213213

214214
detail::CollectionData* _data;

src/ArduinoJson/Object/JsonObjectConst.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class JsonObjectConst : public detail::VariantOperators<JsonObjectConst> {
130130
const detail::VariantData* getMember(TAdaptedString key) const {
131131
if (!_data)
132132
return 0;
133-
return _data->getMember(key);
133+
return slotData(_data->get(key));
134134
}
135135

136136
const detail::CollectionData* _data;

src/ArduinoJson/Variant/SlotFunctions.hpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ inline size_t slotSize(const VariantSlot* var) {
1818
return n;
1919
}
2020

21-
inline VariantData* slotData(VariantSlot* slot) {
22-
return reinterpret_cast<VariantData*>(slot);
23-
}
24-
2521
inline void slotRelease(const VariantSlot* slot, MemoryPool* pool) {
2622
ARDUINOJSON_ASSERT(slot != nullptr);
2723
if (slot->ownsKey())

src/ArduinoJson/Variant/VariantData.hpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,14 +226,18 @@ class VariantData {
226226
}
227227

228228
VariantData* getElement(size_t index) const {
229-
const CollectionData* col = asArray();
230-
return col ? col->getElement(index) : 0;
229+
auto array = asArray();
230+
if (!array)
231+
return nullptr;
232+
return slotData(array->get(index));
231233
}
232234

233235
template <typename TAdaptedString>
234236
VariantData* getMember(TAdaptedString key) const {
235-
const CollectionData* col = asObject();
236-
return col ? col->getMember(key) : 0;
237+
auto object = asObject();
238+
if (!object)
239+
return nullptr;
240+
return slotData(object->get(key));
237241
}
238242

239243
void movePointers(ptrdiff_t variantDistance) {

0 commit comments

Comments
 (0)