CollectionData: addSlot() returns an iterator

This commit is contained in:
Benoit Blanchon
2023-07-03 11:53:56 +02:00
parent fac5b5a7a7
commit c37990d791
8 changed files with 51 additions and 41 deletions

View File

@ -400,7 +400,7 @@ TEST_CASE("MemberProxy under memory constraints") {
REQUIRE(doc.is<JsonObject>()); REQUIRE(doc.is<JsonObject>());
REQUIRE(doc.size() == 0); REQUIRE(doc.size() == 0);
REQUIRE(doc.memoryUsage() == sizeofObject(1)); REQUIRE(doc.memoryUsage() == 0);
REQUIRE(doc.overflowed() == true); REQUIRE(doc.overflowed() == true);
} }
} }

View File

@ -48,7 +48,8 @@ TEST_CASE("JsonDocument::garbageCollect()") {
REQUIRE(doc.memoryUsage() == sizeofObject(2) + sizeofString(7)); REQUIRE(doc.memoryUsage() == sizeofObject(2) + sizeofString(7));
REQUIRE(doc.as<std::string>() == "{\"dancing\":2}"); REQUIRE(doc.as<std::string>() == "{\"dancing\":2}");
REQUIRE(spyingAllocator.log() == AllocatorLog() REQUIRE(spyingAllocator.log() ==
<< AllocatorLog::AllocateFail(4096)); AllocatorLog() << AllocatorLog::AllocateFail(4096)
<< AllocatorLog::AllocateFail(sizeofString(7)));
} }
} }

View File

@ -10,7 +10,9 @@ ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
class ArrayData : public CollectionData { class ArrayData : public CollectionData {
public: public:
VariantData* addElement(ResourceManager* resources); VariantData* addElement(ResourceManager* resources) {
return addSlot(resources).data();
}
static VariantData* addElement(ArrayData* array, ResourceManager* resources) { static VariantData* addElement(ArrayData* array, ResourceManager* resources) {
if (!array) if (!array)

View File

@ -18,10 +18,6 @@ inline ArrayData::iterator ArrayData::at(size_t index) const {
return it; return it;
} }
inline VariantData* ArrayData::addElement(ResourceManager* resources) {
return slotData(addSlot(resources));
}
inline bool ArrayData::copyFrom(const ArrayData& src, inline bool ArrayData::copyFrom(const ArrayData& src,
ResourceManager* resources) { ResourceManager* resources) {
clear(resources); clear(resources);

View File

@ -52,6 +52,9 @@ class CollectionIterator {
const char* key() const; const char* key() const;
bool ownsKey() const; bool ownsKey() const;
void setKey(StringNode*);
void setKey(const char*);
VariantData* data() { VariantData* data() {
return reinterpret_cast<VariantData*>(slot_); return reinterpret_cast<VariantData*>(slot_);
} }
@ -107,7 +110,7 @@ class CollectionData {
} }
protected: protected:
VariantSlot* addSlot(ResourceManager*); iterator addSlot(ResourceManager*);
private: private:
VariantSlot* getPreviousSlot(VariantSlot*) const; VariantSlot* getPreviousSlot(VariantSlot*) const;

View File

@ -17,6 +17,18 @@ inline const char* CollectionIterator::key() const {
return slot_->key(); return slot_->key();
} }
inline void CollectionIterator::setKey(const char* s) {
ARDUINOJSON_ASSERT(slot_ != nullptr);
ARDUINOJSON_ASSERT(s != nullptr);
return slot_->setKey(s);
}
inline void CollectionIterator::setKey(StringNode* s) {
ARDUINOJSON_ASSERT(slot_ != nullptr);
ARDUINOJSON_ASSERT(s != nullptr);
return slot_->setKey(s);
}
inline bool CollectionIterator::ownsKey() const { inline bool CollectionIterator::ownsKey() const {
ARDUINOJSON_ASSERT(slot_ != nullptr); ARDUINOJSON_ASSERT(slot_ != nullptr);
return slot_->ownsKey(); return slot_->ownsKey();
@ -27,7 +39,8 @@ inline void CollectionIterator::next() {
slot_ = slot_->next(); slot_ = slot_->next();
} }
inline VariantSlot* CollectionData::addSlot(ResourceManager* resources) { inline CollectionData::iterator CollectionData::addSlot(
ResourceManager* resources) {
auto slot = resources->allocVariant(); auto slot = resources->allocVariant();
if (!slot) if (!slot)
return nullptr; return nullptr;

View File

@ -10,10 +10,34 @@ ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
class ObjectData : public CollectionData { class ObjectData : public CollectionData {
public: public:
VariantData* addMember(StringNode* key, ResourceManager* resources); VariantData* addMember(StringNode* key, ResourceManager* resources) {
ARDUINOJSON_ASSERT(key != nullptr);
auto it = addSlot(resources);
if (it.done())
return nullptr;
it.setKey(key);
return it.data();
}
template <typename TAdaptedString> template <typename TAdaptedString>
VariantData* addMember(TAdaptedString key, ResourceManager* resources); VariantData* addMember(TAdaptedString key, ResourceManager* resources) {
ARDUINOJSON_ASSERT(!key.isNull());
if (key.isLinked()) {
auto it = addSlot(resources);
if (!it.done())
it.setKey(key.data());
return it.data();
} else {
auto storedKey = resources->saveString(key);
if (!storedKey)
return nullptr;
auto it = addSlot(resources);
if (!it.done())
it.setKey(storedKey);
return it.data();
}
}
bool copyFrom(const ObjectData& src, ResourceManager* resources); bool copyFrom(const ObjectData& src, ResourceManager* resources);

View File

@ -9,35 +9,6 @@
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
inline VariantData* ObjectData::addMember(StringNode* key,
ResourceManager* resources) {
ARDUINOJSON_ASSERT(key != nullptr);
auto slot = addSlot(resources);
if (!slot)
return nullptr;
slot->setKey(key);
return slot->data();
}
template <typename TAdaptedString>
inline VariantData* ObjectData::addMember(TAdaptedString key,
ResourceManager* resources) {
ARDUINOJSON_ASSERT(!key.isNull());
auto slot = addSlot(resources);
if (!slot)
return nullptr;
if (key.isLinked())
slot->setKey(key.data());
else {
auto storedKey = resources->saveString(key);
if (!storedKey)
return nullptr;
slot->setKey(storedKey);
}
return slot->data();
}
inline bool ObjectData::copyFrom(const ObjectData& src, inline bool ObjectData::copyFrom(const ObjectData& src,
ResourceManager* resources) { ResourceManager* resources) {
clear(resources); clear(resources);