forked from bblanchon/ArduinoJson
CollectionImpl: add allocVariant()
, getVariant()
, freeVariant()
This commit is contained in:
@ -22,8 +22,7 @@ inline ArrayImpl::iterator ArrayImpl::at(size_t index) const {
|
||||
inline VariantData* ArrayImpl::addElement() {
|
||||
if (!data_)
|
||||
return nullptr;
|
||||
ARDUINOJSON_ASSERT(resources_ != nullptr);
|
||||
auto slot = resources_->allocVariant();
|
||||
auto slot = allocVariant();
|
||||
if (!slot)
|
||||
return nullptr;
|
||||
CollectionImpl::appendOne(slot);
|
||||
@ -60,13 +59,12 @@ template <typename T>
|
||||
inline bool ArrayImpl::addValue(const T& value) {
|
||||
if (!data_)
|
||||
return false;
|
||||
ARDUINOJSON_ASSERT(resources_ != nullptr);
|
||||
auto slot = resources_->allocVariant();
|
||||
auto slot = allocVariant();
|
||||
if (!slot)
|
||||
return false;
|
||||
JsonVariant variant(slot.ptr(), resources_);
|
||||
if (!variant.set(value)) {
|
||||
resources_->freeVariant(slot);
|
||||
freeVariant(slot);
|
||||
return false;
|
||||
}
|
||||
CollectionImpl::appendOne(slot);
|
||||
|
@ -4,7 +4,6 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson/Memory/MemoryPool.hpp>
|
||||
#include <ArduinoJson/Namespace.hpp>
|
||||
#include <ArduinoJson/Polyfills/assert.hpp>
|
||||
|
||||
@ -113,6 +112,21 @@ class CollectionImpl {
|
||||
void removeOne(iterator it);
|
||||
void removePair(iterator it);
|
||||
|
||||
VariantData* getVariant(SlotId id) const {
|
||||
ARDUINOJSON_ASSERT(resources_ != nullptr);
|
||||
return resources_->getVariant(id);
|
||||
}
|
||||
|
||||
void freeVariant(Slot<VariantData> slot) {
|
||||
ARDUINOJSON_ASSERT(resources_ != nullptr);
|
||||
resources_->freeVariant(slot);
|
||||
}
|
||||
|
||||
Slot<VariantData> allocVariant() {
|
||||
ARDUINOJSON_ASSERT(resources_ != nullptr);
|
||||
return resources_->allocVariant();
|
||||
}
|
||||
|
||||
private:
|
||||
Slot<VariantData> getPreviousSlot(VariantData*) const;
|
||||
|
||||
|
@ -29,16 +29,14 @@ inline CollectionImpl::iterator CollectionImpl::createIterator() const {
|
||||
if (!data_)
|
||||
return iterator();
|
||||
auto coll = getCollectionData();
|
||||
return iterator(resources_->getVariant(coll->head), coll->head);
|
||||
return iterator(getVariant(coll->head), coll->head);
|
||||
}
|
||||
|
||||
inline void CollectionImpl::appendOne(Slot<VariantData> slot) {
|
||||
ARDUINOJSON_ASSERT(resources_ != nullptr);
|
||||
|
||||
auto coll = getCollectionData();
|
||||
|
||||
if (coll->tail != NULL_SLOT) {
|
||||
auto tail = resources_->getVariant(coll->tail);
|
||||
auto tail = getVariant(coll->tail);
|
||||
tail->next = slot.id();
|
||||
coll->tail = slot.id();
|
||||
} else {
|
||||
@ -49,14 +47,12 @@ inline void CollectionImpl::appendOne(Slot<VariantData> slot) {
|
||||
|
||||
inline void CollectionImpl::appendPair(Slot<VariantData> key,
|
||||
Slot<VariantData> value) {
|
||||
ARDUINOJSON_ASSERT(resources_ != nullptr);
|
||||
|
||||
auto coll = getCollectionData();
|
||||
|
||||
key->next = value.id();
|
||||
|
||||
if (coll->tail != NULL_SLOT) {
|
||||
auto tail = resources_->getVariant(coll->tail);
|
||||
auto tail = getVariant(coll->tail);
|
||||
tail->next = key.id();
|
||||
coll->tail = value.id();
|
||||
} else {
|
||||
@ -74,9 +70,9 @@ inline void CollectionImpl::clear() {
|
||||
auto next = coll->head;
|
||||
while (next != NULL_SLOT) {
|
||||
auto currId = next;
|
||||
auto slot = resources_->getVariant(next);
|
||||
auto slot = getVariant(next);
|
||||
next = slot->next;
|
||||
resources_->freeVariant({slot, currId});
|
||||
freeVariant({slot, currId});
|
||||
}
|
||||
|
||||
coll->head = NULL_SLOT;
|
||||
@ -89,7 +85,7 @@ inline Slot<VariantData> CollectionImpl::getPreviousSlot(
|
||||
auto prev = Slot<VariantData>();
|
||||
auto currentId = coll->head;
|
||||
while (currentId != NULL_SLOT) {
|
||||
auto currentSlot = resources_->getVariant(currentId);
|
||||
auto currentSlot = getVariant(currentId);
|
||||
if (currentSlot == target)
|
||||
break;
|
||||
prev = Slot<VariantData>(currentSlot, currentId);
|
||||
@ -111,7 +107,7 @@ inline void CollectionImpl::removeOne(iterator it) {
|
||||
coll->head = next;
|
||||
if (next == NULL_SLOT)
|
||||
coll->tail = prev.id();
|
||||
resources_->freeVariant({it.slot_, it.currentId_});
|
||||
freeVariant({it.slot_, it.currentId_});
|
||||
}
|
||||
|
||||
inline void CollectionImpl::removePair(ObjectImpl::iterator it) {
|
||||
@ -121,11 +117,11 @@ inline void CollectionImpl::removePair(ObjectImpl::iterator it) {
|
||||
auto keySlot = it.slot_;
|
||||
|
||||
auto valueId = it.nextId_;
|
||||
auto valueSlot = resources_->getVariant(valueId);
|
||||
auto valueSlot = getVariant(valueId);
|
||||
|
||||
// remove value slot
|
||||
keySlot->next = valueSlot->next;
|
||||
resources_->freeVariant({valueSlot, valueId});
|
||||
freeVariant({valueSlot, valueId});
|
||||
|
||||
// remove key slot
|
||||
removeOne(it);
|
||||
|
@ -50,13 +50,12 @@ template <typename TAdaptedString>
|
||||
inline VariantData* ObjectImpl::addMember(TAdaptedString key) {
|
||||
if (!data_)
|
||||
return nullptr;
|
||||
ARDUINOJSON_ASSERT(resources_ != nullptr);
|
||||
|
||||
auto keySlot = resources_->allocVariant();
|
||||
auto keySlot = allocVariant();
|
||||
if (!keySlot)
|
||||
return nullptr;
|
||||
|
||||
auto valueSlot = resources_->allocVariant();
|
||||
auto valueSlot = allocVariant();
|
||||
if (!valueSlot)
|
||||
return nullptr;
|
||||
|
||||
@ -72,13 +71,12 @@ inline VariantData* ObjectImpl::addMember(TAdaptedString key) {
|
||||
inline VariantData* ObjectImpl::addPair(VariantData** value) {
|
||||
if (!data_)
|
||||
return nullptr;
|
||||
ARDUINOJSON_ASSERT(resources_ != nullptr);
|
||||
|
||||
auto keySlot = resources_->allocVariant();
|
||||
auto keySlot = allocVariant();
|
||||
if (!keySlot)
|
||||
return nullptr;
|
||||
|
||||
auto valueSlot = resources_->allocVariant();
|
||||
auto valueSlot = allocVariant();
|
||||
if (!valueSlot)
|
||||
return nullptr;
|
||||
*value = valueSlot.ptr();
|
||||
|
Reference in New Issue
Block a user