forked from bblanchon/ArduinoJson
Simplify CollectionData
to work only with VariantSlot*
This commit is contained in:
@ -70,7 +70,7 @@ class JsonArrayConst : public detail::VariantOperators<JsonArrayConst> {
|
||||
// Returns the element at the specified index.
|
||||
// https://arduinojson.org/v6/api/jsonarrayconst/subscript/
|
||||
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 {
|
||||
|
@ -19,32 +19,12 @@ class CollectionData {
|
||||
VariantSlot* _tail;
|
||||
|
||||
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();
|
||||
size_t memoryUsage() const;
|
||||
size_t size() const;
|
||||
|
||||
void addSlot(VariantSlot*);
|
||||
void removeSlot(VariantSlot* slot);
|
||||
void add(VariantSlot*);
|
||||
void remove(VariantSlot* slot);
|
||||
|
||||
VariantSlot* head() const {
|
||||
return _head;
|
||||
@ -52,13 +32,13 @@ class CollectionData {
|
||||
|
||||
void movePointers(ptrdiff_t variantDistance);
|
||||
|
||||
VariantSlot* getSlot(size_t index) const;
|
||||
VariantSlot* get(size_t index) const;
|
||||
|
||||
template <typename TAdaptedString>
|
||||
VariantSlot* getSlot(TAdaptedString key) const;
|
||||
VariantSlot* get(TAdaptedString key) const;
|
||||
|
||||
private:
|
||||
VariantSlot* getPreviousSlot(VariantSlot*) const;
|
||||
VariantSlot* getPrevious(VariantSlot*) const;
|
||||
};
|
||||
|
||||
inline const VariantData* collectionToVariant(
|
||||
|
@ -15,7 +15,7 @@ inline VariantData* collectionAddElement(CollectionData* array,
|
||||
auto slot = pool->allocVariant();
|
||||
if (!slot)
|
||||
return nullptr;
|
||||
array->addSlot(slot);
|
||||
array->add(slot);
|
||||
return slot->data();
|
||||
}
|
||||
|
||||
@ -33,7 +33,7 @@ inline VariantData* collectionAddMember(CollectionData* obj, TAdaptedString key,
|
||||
if (!storedKey)
|
||||
return nullptr;
|
||||
slot->setKey(storedKey);
|
||||
obj->addSlot(slot);
|
||||
obj->add(slot);
|
||||
return slot->data();
|
||||
}
|
||||
|
||||
@ -71,7 +71,7 @@ inline void collectionRemove(CollectionData* data, VariantSlot* slot,
|
||||
MemoryPool* pool) {
|
||||
if (!data || !slot)
|
||||
return;
|
||||
data->removeSlot(slot);
|
||||
data->remove(slot);
|
||||
slotRelease(slot, pool);
|
||||
}
|
||||
|
||||
@ -79,7 +79,7 @@ inline void collectionRemoveElement(CollectionData* array, size_t index,
|
||||
MemoryPool* pool) {
|
||||
if (!array)
|
||||
return;
|
||||
collectionRemove(array, array->getSlot(index), pool);
|
||||
collectionRemove(array, array->get(index), pool);
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
@ -87,7 +87,7 @@ inline void collectionRemoveMember(CollectionData* obj, TAdaptedString key,
|
||||
MemoryPool* pool) {
|
||||
if (!obj)
|
||||
return;
|
||||
collectionRemove(obj, obj->getSlot(key), pool);
|
||||
collectionRemove(obj, obj->get(key), pool);
|
||||
}
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
|
@ -11,7 +11,7 @@
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
inline void CollectionData::addSlot(VariantSlot* slot) {
|
||||
inline void CollectionData::add(VariantSlot* slot) {
|
||||
ARDUINOJSON_ASSERT(slot != nullptr);
|
||||
|
||||
if (_tail) {
|
||||
@ -29,12 +29,7 @@ inline void CollectionData::clear() {
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
inline bool CollectionData::containsKey(const TAdaptedString& key) const {
|
||||
return getSlot(key) != 0;
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
inline VariantSlot* CollectionData::getSlot(TAdaptedString key) const {
|
||||
inline VariantSlot* CollectionData::get(TAdaptedString key) const {
|
||||
if (key.isNull())
|
||||
return 0;
|
||||
VariantSlot* slot = _head;
|
||||
@ -46,13 +41,13 @@ inline VariantSlot* CollectionData::getSlot(TAdaptedString key) const {
|
||||
return slot;
|
||||
}
|
||||
|
||||
inline VariantSlot* CollectionData::getSlot(size_t index) const {
|
||||
inline VariantSlot* CollectionData::get(size_t index) const {
|
||||
if (!_head)
|
||||
return 0;
|
||||
return _head->next(index);
|
||||
}
|
||||
|
||||
inline VariantSlot* CollectionData::getPreviousSlot(VariantSlot* target) const {
|
||||
inline VariantSlot* CollectionData::getPrevious(VariantSlot* target) const {
|
||||
VariantSlot* current = _head;
|
||||
while (current) {
|
||||
VariantSlot* next = current->next();
|
||||
@ -63,21 +58,10 @@ inline VariantSlot* CollectionData::getPreviousSlot(VariantSlot* target) const {
|
||||
return 0;
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
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) {
|
||||
inline void CollectionData::remove(VariantSlot* slot) {
|
||||
if (!slot)
|
||||
return;
|
||||
VariantSlot* prev = getPreviousSlot(slot);
|
||||
VariantSlot* prev = getPrevious(slot);
|
||||
VariantSlot* next = slot->next();
|
||||
if (prev)
|
||||
prev->setNext(next);
|
||||
|
@ -274,24 +274,23 @@ class JsonDeserializer {
|
||||
TFilter memberFilter = filter[key.c_str()];
|
||||
|
||||
if (memberFilter.allow()) {
|
||||
VariantData* variant = object.getMember(adaptString(key.c_str()));
|
||||
if (!variant) {
|
||||
VariantSlot* slot = object.get(adaptString(key.c_str()));
|
||||
if (!slot) {
|
||||
// Save key in memory pool.
|
||||
key = _stringStorage.save();
|
||||
|
||||
// Allocate slot in object
|
||||
VariantSlot* slot = _pool->allocVariant();
|
||||
slot = _pool->allocVariant();
|
||||
if (!slot)
|
||||
return DeserializationError::NoMemory;
|
||||
|
||||
slot->setKey(key);
|
||||
object.addSlot(slot);
|
||||
|
||||
variant = slot->data();
|
||||
object.add(slot);
|
||||
}
|
||||
|
||||
// Parse value
|
||||
err = parseVariant(*variant, memberFilter, nestingLimit.decrement());
|
||||
err =
|
||||
parseVariant(*slot->data(), memberFilter, nestingLimit.decrement());
|
||||
if (err)
|
||||
return err;
|
||||
} else {
|
||||
|
@ -501,7 +501,7 @@ class MsgPackDeserializer {
|
||||
return DeserializationError::NoMemory;
|
||||
|
||||
slot->setKey(key);
|
||||
object->addSlot(slot);
|
||||
object->add(slot);
|
||||
|
||||
member = slot->data();
|
||||
} else {
|
||||
|
@ -203,12 +203,12 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
|
||||
inline detail::VariantData* getMember(TAdaptedString key) const {
|
||||
if (!_data)
|
||||
return 0;
|
||||
return _data->getMember(key);
|
||||
return slotData(_data->get(key));
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
void removeMember(TAdaptedString key) const {
|
||||
collectionRemove(_data, _data->getSlot(key), _pool);
|
||||
collectionRemove(_data, _data->get(key), _pool);
|
||||
}
|
||||
|
||||
detail::CollectionData* _data;
|
||||
|
@ -130,7 +130,7 @@ class JsonObjectConst : public detail::VariantOperators<JsonObjectConst> {
|
||||
const detail::VariantData* getMember(TAdaptedString key) const {
|
||||
if (!_data)
|
||||
return 0;
|
||||
return _data->getMember(key);
|
||||
return slotData(_data->get(key));
|
||||
}
|
||||
|
||||
const detail::CollectionData* _data;
|
||||
|
@ -18,10 +18,6 @@ inline size_t slotSize(const VariantSlot* var) {
|
||||
return n;
|
||||
}
|
||||
|
||||
inline VariantData* slotData(VariantSlot* slot) {
|
||||
return reinterpret_cast<VariantData*>(slot);
|
||||
}
|
||||
|
||||
inline void slotRelease(const VariantSlot* slot, MemoryPool* pool) {
|
||||
ARDUINOJSON_ASSERT(slot != nullptr);
|
||||
if (slot->ownsKey())
|
||||
|
@ -226,14 +226,18 @@ class VariantData {
|
||||
}
|
||||
|
||||
VariantData* getElement(size_t index) const {
|
||||
const CollectionData* col = asArray();
|
||||
return col ? col->getElement(index) : 0;
|
||||
auto array = asArray();
|
||||
if (!array)
|
||||
return nullptr;
|
||||
return slotData(array->get(index));
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
VariantData* getMember(TAdaptedString key) const {
|
||||
const CollectionData* col = asObject();
|
||||
return col ? col->getMember(key) : 0;
|
||||
auto object = asObject();
|
||||
if (!object)
|
||||
return nullptr;
|
||||
return slotData(object->get(key));
|
||||
}
|
||||
|
||||
void movePointers(ptrdiff_t variantDistance) {
|
||||
|
@ -195,7 +195,7 @@ inline NO_INLINE VariantData* variantGetOrAddElement(VariantData* var,
|
||||
slot = pool->allocVariant();
|
||||
if (!slot)
|
||||
return nullptr;
|
||||
array->addSlot(slot);
|
||||
array->add(slot);
|
||||
index--;
|
||||
}
|
||||
return slot->data();
|
||||
@ -223,7 +223,7 @@ VariantData* variantGetOrAddMember(VariantData* var, TAdaptedString key,
|
||||
auto obj = var->isNull() ? &var->toObject() : var->asObject();
|
||||
if (!obj)
|
||||
return nullptr;
|
||||
auto slot = obj->getSlot(key);
|
||||
auto slot = obj->get(key);
|
||||
if (slot)
|
||||
return slot->data();
|
||||
return collectionAddMember(obj, key, pool);
|
||||
|
@ -105,4 +105,8 @@ class VariantSlot {
|
||||
}
|
||||
};
|
||||
|
||||
inline VariantData* slotData(VariantSlot* slot) {
|
||||
return reinterpret_cast<VariantData*>(slot);
|
||||
}
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
|
Reference in New Issue
Block a user