mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-30 02:37:35 +02:00
Add VariantData::getOrAddElement()
This commit is contained in:
@ -185,6 +185,27 @@ class VariantData {
|
|||||||
return slotData(object->get(key));
|
return slotData(object->get(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VariantData* getOrAddElement(size_t index, MemoryPool* pool) {
|
||||||
|
auto array = isNull() ? &toArray() : asArray();
|
||||||
|
if (!array)
|
||||||
|
return nullptr;
|
||||||
|
VariantSlot* slot = array->head();
|
||||||
|
while (slot && index > 0) {
|
||||||
|
slot = slot->next();
|
||||||
|
index--;
|
||||||
|
}
|
||||||
|
if (!slot)
|
||||||
|
index++;
|
||||||
|
while (index > 0) {
|
||||||
|
slot = new (pool) VariantSlot();
|
||||||
|
if (!slot)
|
||||||
|
return nullptr;
|
||||||
|
array->add(slot);
|
||||||
|
index--;
|
||||||
|
}
|
||||||
|
return slot->data();
|
||||||
|
}
|
||||||
|
|
||||||
template <typename TAdaptedString>
|
template <typename TAdaptedString>
|
||||||
VariantData* getOrAddMember(TAdaptedString key, MemoryPool* pool) {
|
VariantData* getOrAddMember(TAdaptedString key, MemoryPool* pool) {
|
||||||
if (key.isNull())
|
if (key.isNull())
|
||||||
|
@ -131,24 +131,7 @@ inline NO_INLINE VariantData* variantGetOrAddElement(VariantData* var,
|
|||||||
MemoryPool* pool) {
|
MemoryPool* pool) {
|
||||||
if (!var)
|
if (!var)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
auto array = var->isNull() ? &var->toArray() : var->asArray();
|
return var->getOrAddElement(index, pool);
|
||||||
if (!array)
|
|
||||||
return nullptr;
|
|
||||||
VariantSlot* slot = array->head();
|
|
||||||
while (slot && index > 0) {
|
|
||||||
slot = slot->next();
|
|
||||||
index--;
|
|
||||||
}
|
|
||||||
if (!slot)
|
|
||||||
index++;
|
|
||||||
while (index > 0) {
|
|
||||||
slot = new (pool) VariantSlot();
|
|
||||||
if (!slot)
|
|
||||||
return nullptr;
|
|
||||||
array->add(slot);
|
|
||||||
index--;
|
|
||||||
}
|
|
||||||
return slot->data();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void variantRemoveElement(VariantData* var, size_t index,
|
inline void variantRemoveElement(VariantData* var, size_t index,
|
||||||
|
Reference in New Issue
Block a user