Add VariantData::getOrAddElement()

This commit is contained in:
Benoit Blanchon
2023-05-25 18:10:22 +02:00
parent 585795d002
commit ab4e8547cb
2 changed files with 22 additions and 18 deletions

View File

@ -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())

View File

@ -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,