mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-29 18:27:37 +02:00
Add VariantData::getOrAddElement()
This commit is contained in:
@ -185,6 +185,27 @@ class VariantData {
|
||||
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>
|
||||
VariantData* getOrAddMember(TAdaptedString key, MemoryPool* pool) {
|
||||
if (key.isNull())
|
||||
|
@ -131,24 +131,7 @@ inline NO_INLINE VariantData* variantGetOrAddElement(VariantData* var,
|
||||
MemoryPool* pool) {
|
||||
if (!var)
|
||||
return nullptr;
|
||||
auto array = var->isNull() ? &var->toArray() : var->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();
|
||||
return var->getOrAddElement(index, pool);
|
||||
}
|
||||
|
||||
inline void variantRemoveElement(VariantData* var, size_t index,
|
||||
|
Reference in New Issue
Block a user