Simplify CollectionData to work only with VariantSlot*

This commit is contained in:
Benoit Blanchon
2023-04-17 11:57:19 +02:00
parent 003087406c
commit f5e7570f84
12 changed files with 41 additions and 74 deletions

View File

@ -70,7 +70,7 @@ class JsonArrayConst : public detail::VariantOperators<JsonArrayConst> {
// Returns the element at the specified index. // Returns the element at the specified index.
// https://arduinojson.org/v6/api/jsonarrayconst/subscript/ // https://arduinojson.org/v6/api/jsonarrayconst/subscript/
FORCE_INLINE JsonVariantConst operator[](size_t index) const { FORCE_INLINE JsonVariantConst operator[](size_t index) const {
return JsonVariantConst(_data ? _data->getElement(index) : 0); return JsonVariantConst(_data ? slotData(_data->get(index)) : 0);
} }
operator JsonVariantConst() const { operator JsonVariantConst() const {

View File

@ -19,32 +19,12 @@ class CollectionData {
VariantSlot* _tail; VariantSlot* _tail;
public: public:
// Must be a POD!
// - no constructor
// - no destructor
// - no virtual
// - no inheritance
// Array only
VariantData* getElement(size_t index) const;
// Object only
template <typename TAdaptedString>
VariantData* getMember(TAdaptedString key) const;
template <typename TAdaptedString>
bool containsKey(const TAdaptedString& key) const;
// Generic
void clear(); void clear();
size_t memoryUsage() const; size_t memoryUsage() const;
size_t size() const; size_t size() const;
void addSlot(VariantSlot*); void add(VariantSlot*);
void removeSlot(VariantSlot* slot); void remove(VariantSlot* slot);
VariantSlot* head() const { VariantSlot* head() const {
return _head; return _head;
@ -52,13 +32,13 @@ class CollectionData {
void movePointers(ptrdiff_t variantDistance); void movePointers(ptrdiff_t variantDistance);
VariantSlot* getSlot(size_t index) const; VariantSlot* get(size_t index) const;
template <typename TAdaptedString> template <typename TAdaptedString>
VariantSlot* getSlot(TAdaptedString key) const; VariantSlot* get(TAdaptedString key) const;
private: private:
VariantSlot* getPreviousSlot(VariantSlot*) const; VariantSlot* getPrevious(VariantSlot*) const;
}; };
inline const VariantData* collectionToVariant( inline const VariantData* collectionToVariant(

View File

@ -15,7 +15,7 @@ inline VariantData* collectionAddElement(CollectionData* array,
auto slot = pool->allocVariant(); auto slot = pool->allocVariant();
if (!slot) if (!slot)
return nullptr; return nullptr;
array->addSlot(slot); array->add(slot);
return slot->data(); return slot->data();
} }
@ -33,7 +33,7 @@ inline VariantData* collectionAddMember(CollectionData* obj, TAdaptedString key,
if (!storedKey) if (!storedKey)
return nullptr; return nullptr;
slot->setKey(storedKey); slot->setKey(storedKey);
obj->addSlot(slot); obj->add(slot);
return slot->data(); return slot->data();
} }
@ -71,7 +71,7 @@ inline void collectionRemove(CollectionData* data, VariantSlot* slot,
MemoryPool* pool) { MemoryPool* pool) {
if (!data || !slot) if (!data || !slot)
return; return;
data->removeSlot(slot); data->remove(slot);
slotRelease(slot, pool); slotRelease(slot, pool);
} }
@ -79,7 +79,7 @@ inline void collectionRemoveElement(CollectionData* array, size_t index,
MemoryPool* pool) { MemoryPool* pool) {
if (!array) if (!array)
return; return;
collectionRemove(array, array->getSlot(index), pool); collectionRemove(array, array->get(index), pool);
} }
template <typename TAdaptedString> template <typename TAdaptedString>
@ -87,7 +87,7 @@ inline void collectionRemoveMember(CollectionData* obj, TAdaptedString key,
MemoryPool* pool) { MemoryPool* pool) {
if (!obj) if (!obj)
return; return;
collectionRemove(obj, obj->getSlot(key), pool); collectionRemove(obj, obj->get(key), pool);
} }
ARDUINOJSON_END_PRIVATE_NAMESPACE ARDUINOJSON_END_PRIVATE_NAMESPACE

View File

@ -11,7 +11,7 @@
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
inline void CollectionData::addSlot(VariantSlot* slot) { inline void CollectionData::add(VariantSlot* slot) {
ARDUINOJSON_ASSERT(slot != nullptr); ARDUINOJSON_ASSERT(slot != nullptr);
if (_tail) { if (_tail) {
@ -29,12 +29,7 @@ inline void CollectionData::clear() {
} }
template <typename TAdaptedString> template <typename TAdaptedString>
inline bool CollectionData::containsKey(const TAdaptedString& key) const { inline VariantSlot* CollectionData::get(TAdaptedString key) const {
return getSlot(key) != 0;
}
template <typename TAdaptedString>
inline VariantSlot* CollectionData::getSlot(TAdaptedString key) const {
if (key.isNull()) if (key.isNull())
return 0; return 0;
VariantSlot* slot = _head; VariantSlot* slot = _head;
@ -46,13 +41,13 @@ inline VariantSlot* CollectionData::getSlot(TAdaptedString key) const {
return slot; return slot;
} }
inline VariantSlot* CollectionData::getSlot(size_t index) const { inline VariantSlot* CollectionData::get(size_t index) const {
if (!_head) if (!_head)
return 0; return 0;
return _head->next(index); return _head->next(index);
} }
inline VariantSlot* CollectionData::getPreviousSlot(VariantSlot* target) const { inline VariantSlot* CollectionData::getPrevious(VariantSlot* target) const {
VariantSlot* current = _head; VariantSlot* current = _head;
while (current) { while (current) {
VariantSlot* next = current->next(); VariantSlot* next = current->next();
@ -63,21 +58,10 @@ inline VariantSlot* CollectionData::getPreviousSlot(VariantSlot* target) const {
return 0; return 0;
} }
template <typename TAdaptedString> inline void CollectionData::remove(VariantSlot* slot) {
inline VariantData* CollectionData::getMember(TAdaptedString key) const {
VariantSlot* slot = getSlot(key);
return slot ? slot->data() : 0;
}
inline VariantData* CollectionData::getElement(size_t index) const {
VariantSlot* slot = getSlot(index);
return slot ? slot->data() : 0;
}
inline void CollectionData::removeSlot(VariantSlot* slot) {
if (!slot) if (!slot)
return; return;
VariantSlot* prev = getPreviousSlot(slot); VariantSlot* prev = getPrevious(slot);
VariantSlot* next = slot->next(); VariantSlot* next = slot->next();
if (prev) if (prev)
prev->setNext(next); prev->setNext(next);

View File

@ -274,24 +274,23 @@ class JsonDeserializer {
TFilter memberFilter = filter[key.c_str()]; TFilter memberFilter = filter[key.c_str()];
if (memberFilter.allow()) { if (memberFilter.allow()) {
VariantData* variant = object.getMember(adaptString(key.c_str())); VariantSlot* slot = object.get(adaptString(key.c_str()));
if (!variant) { if (!slot) {
// Save key in memory pool. // Save key in memory pool.
key = _stringStorage.save(); key = _stringStorage.save();
// Allocate slot in object // Allocate slot in object
VariantSlot* slot = _pool->allocVariant(); slot = _pool->allocVariant();
if (!slot) if (!slot)
return DeserializationError::NoMemory; return DeserializationError::NoMemory;
slot->setKey(key); slot->setKey(key);
object.addSlot(slot); object.add(slot);
variant = slot->data();
} }
// Parse value // Parse value
err = parseVariant(*variant, memberFilter, nestingLimit.decrement()); err =
parseVariant(*slot->data(), memberFilter, nestingLimit.decrement());
if (err) if (err)
return err; return err;
} else { } else {

View File

@ -501,7 +501,7 @@ class MsgPackDeserializer {
return DeserializationError::NoMemory; return DeserializationError::NoMemory;
slot->setKey(key); slot->setKey(key);
object->addSlot(slot); object->add(slot);
member = slot->data(); member = slot->data();
} else { } else {

View File

@ -203,12 +203,12 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
inline detail::VariantData* getMember(TAdaptedString key) const { inline detail::VariantData* getMember(TAdaptedString key) const {
if (!_data) if (!_data)
return 0; return 0;
return _data->getMember(key); return slotData(_data->get(key));
} }
template <typename TAdaptedString> template <typename TAdaptedString>
void removeMember(TAdaptedString key) const { void removeMember(TAdaptedString key) const {
collectionRemove(_data, _data->getSlot(key), _pool); collectionRemove(_data, _data->get(key), _pool);
} }
detail::CollectionData* _data; detail::CollectionData* _data;

View File

@ -130,7 +130,7 @@ class JsonObjectConst : public detail::VariantOperators<JsonObjectConst> {
const detail::VariantData* getMember(TAdaptedString key) const { const detail::VariantData* getMember(TAdaptedString key) const {
if (!_data) if (!_data)
return 0; return 0;
return _data->getMember(key); return slotData(_data->get(key));
} }
const detail::CollectionData* _data; const detail::CollectionData* _data;

View File

@ -18,10 +18,6 @@ inline size_t slotSize(const VariantSlot* var) {
return n; return n;
} }
inline VariantData* slotData(VariantSlot* slot) {
return reinterpret_cast<VariantData*>(slot);
}
inline void slotRelease(const VariantSlot* slot, MemoryPool* pool) { inline void slotRelease(const VariantSlot* slot, MemoryPool* pool) {
ARDUINOJSON_ASSERT(slot != nullptr); ARDUINOJSON_ASSERT(slot != nullptr);
if (slot->ownsKey()) if (slot->ownsKey())

View File

@ -226,14 +226,18 @@ class VariantData {
} }
VariantData* getElement(size_t index) const { VariantData* getElement(size_t index) const {
const CollectionData* col = asArray(); auto array = asArray();
return col ? col->getElement(index) : 0; if (!array)
return nullptr;
return slotData(array->get(index));
} }
template <typename TAdaptedString> template <typename TAdaptedString>
VariantData* getMember(TAdaptedString key) const { VariantData* getMember(TAdaptedString key) const {
const CollectionData* col = asObject(); auto object = asObject();
return col ? col->getMember(key) : 0; if (!object)
return nullptr;
return slotData(object->get(key));
} }
void movePointers(ptrdiff_t variantDistance) { void movePointers(ptrdiff_t variantDistance) {

View File

@ -195,7 +195,7 @@ inline NO_INLINE VariantData* variantGetOrAddElement(VariantData* var,
slot = pool->allocVariant(); slot = pool->allocVariant();
if (!slot) if (!slot)
return nullptr; return nullptr;
array->addSlot(slot); array->add(slot);
index--; index--;
} }
return slot->data(); return slot->data();
@ -223,7 +223,7 @@ VariantData* variantGetOrAddMember(VariantData* var, TAdaptedString key,
auto obj = var->isNull() ? &var->toObject() : var->asObject(); auto obj = var->isNull() ? &var->toObject() : var->asObject();
if (!obj) if (!obj)
return nullptr; return nullptr;
auto slot = obj->getSlot(key); auto slot = obj->get(key);
if (slot) if (slot)
return slot->data(); return slot->data();
return collectionAddMember(obj, key, pool); return collectionAddMember(obj, key, pool);

View File

@ -105,4 +105,8 @@ class VariantSlot {
} }
}; };
inline VariantData* slotData(VariantSlot* slot) {
return reinterpret_cast<VariantData*>(slot);
}
ARDUINOJSON_END_PRIVATE_NAMESPACE ARDUINOJSON_END_PRIVATE_NAMESPACE