mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-11-15 14:59:23 +01:00
Make VariantSlot a union. Include next slot id in VariantData
This commit is contained in:
@@ -20,11 +20,11 @@ inline ArrayData::iterator ArrayData::at(
|
||||
}
|
||||
|
||||
inline VariantData* ArrayData::addElement(ResourceManager* resources) {
|
||||
auto slot = resources->allocSlot();
|
||||
auto slot = resources->allocVariant();
|
||||
if (!slot)
|
||||
return nullptr;
|
||||
CollectionData::appendOne(slot, resources);
|
||||
return slot->data();
|
||||
return slot.data();
|
||||
}
|
||||
|
||||
inline VariantData* ArrayData::getOrAddElement(size_t index,
|
||||
@@ -58,12 +58,12 @@ inline void ArrayData::removeElement(size_t index, ResourceManager* resources) {
|
||||
template <typename T>
|
||||
inline bool ArrayData::addValue(T&& value, ResourceManager* resources) {
|
||||
ARDUINOJSON_ASSERT(resources != nullptr);
|
||||
auto slot = resources->allocSlot();
|
||||
auto slot = resources->allocVariant();
|
||||
if (!slot)
|
||||
return false;
|
||||
JsonVariant variant(slot->data(), resources);
|
||||
JsonVariant variant(slot.data(), resources);
|
||||
if (!variant.set(detail::forward<T>(value))) {
|
||||
resources->freeSlot(slot);
|
||||
resources->freeVariant(slot);
|
||||
return false;
|
||||
}
|
||||
CollectionData::appendOne(slot, resources);
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
class VariantData;
|
||||
class VariantSlot;
|
||||
union VariantSlot;
|
||||
|
||||
class CollectionIterator {
|
||||
friend class CollectionData;
|
||||
@@ -58,9 +58,9 @@ class CollectionIterator {
|
||||
}
|
||||
|
||||
private:
|
||||
CollectionIterator(VariantSlot* slot, SlotId slotId);
|
||||
CollectionIterator(VariantData* slot, SlotId slotId);
|
||||
|
||||
VariantSlot* slot_;
|
||||
VariantData* slot_;
|
||||
SlotId currentId_, nextId_;
|
||||
};
|
||||
|
||||
@@ -79,7 +79,7 @@ class CollectionData {
|
||||
using iterator = CollectionIterator;
|
||||
|
||||
iterator createIterator(const ResourceManager* resources) const {
|
||||
return iterator(resources->getSlot(head_), head_);
|
||||
return iterator(resources->getVariant(head_), head_);
|
||||
}
|
||||
|
||||
size_t size(const ResourceManager*) const;
|
||||
@@ -98,15 +98,15 @@ class CollectionData {
|
||||
}
|
||||
|
||||
protected:
|
||||
void appendOne(SlotWithId slot, const ResourceManager* resources);
|
||||
void appendPair(SlotWithId key, SlotWithId value,
|
||||
void appendOne(VariantWithId slot, const ResourceManager* resources);
|
||||
void appendPair(VariantWithId key, VariantWithId value,
|
||||
const ResourceManager* resources);
|
||||
|
||||
void removeOne(iterator it, ResourceManager* resources);
|
||||
void removePair(iterator it, ResourceManager* resources);
|
||||
|
||||
private:
|
||||
SlotWithId getPreviousSlot(VariantSlot*, const ResourceManager*) const;
|
||||
VariantWithId getPreviousSlot(VariantData*, const ResourceManager*) const;
|
||||
};
|
||||
|
||||
inline const VariantData* collectionToVariant(
|
||||
|
||||
@@ -12,23 +12,23 @@
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
inline CollectionIterator::CollectionIterator(VariantSlot* slot, SlotId slotId)
|
||||
inline CollectionIterator::CollectionIterator(VariantData* slot, SlotId slotId)
|
||||
: slot_(slot), currentId_(slotId) {
|
||||
nextId_ = slot_ ? slot_->next() : NULL_SLOT;
|
||||
}
|
||||
|
||||
inline void CollectionIterator::next(const ResourceManager* resources) {
|
||||
ARDUINOJSON_ASSERT(currentId_ != NULL_SLOT);
|
||||
slot_ = resources->getSlot(nextId_);
|
||||
slot_ = resources->getVariant(nextId_);
|
||||
currentId_ = nextId_;
|
||||
if (slot_)
|
||||
nextId_ = slot_->next();
|
||||
}
|
||||
|
||||
inline void CollectionData::appendOne(SlotWithId slot,
|
||||
inline void CollectionData::appendOne(VariantWithId slot,
|
||||
const ResourceManager* resources) {
|
||||
if (tail_ != NULL_SLOT) {
|
||||
auto tail = resources->getSlot(tail_);
|
||||
auto tail = resources->getVariant(tail_);
|
||||
tail->setNext(slot.id());
|
||||
tail_ = slot.id();
|
||||
} else {
|
||||
@@ -37,12 +37,12 @@ inline void CollectionData::appendOne(SlotWithId slot,
|
||||
}
|
||||
}
|
||||
|
||||
inline void CollectionData::appendPair(SlotWithId key, SlotWithId value,
|
||||
inline void CollectionData::appendPair(VariantWithId key, VariantWithId value,
|
||||
const ResourceManager* resources) {
|
||||
key->setNext(value.id());
|
||||
|
||||
if (tail_ != NULL_SLOT) {
|
||||
auto tail = resources->getSlot(tail_);
|
||||
auto tail = resources->getVariant(tail_);
|
||||
tail->setNext(key.id());
|
||||
tail_ = value.id();
|
||||
} else {
|
||||
@@ -55,24 +55,24 @@ inline void CollectionData::clear(ResourceManager* resources) {
|
||||
auto next = head_;
|
||||
while (next != NULL_SLOT) {
|
||||
auto currId = next;
|
||||
auto slot = resources->getSlot(next);
|
||||
auto slot = resources->getVariant(next);
|
||||
next = slot->next();
|
||||
resources->freeSlot(SlotWithId(slot, currId));
|
||||
resources->freeVariant(VariantWithId(slot, currId));
|
||||
}
|
||||
|
||||
head_ = NULL_SLOT;
|
||||
tail_ = NULL_SLOT;
|
||||
}
|
||||
|
||||
inline SlotWithId CollectionData::getPreviousSlot(
|
||||
VariantSlot* target, const ResourceManager* resources) const {
|
||||
auto prev = SlotWithId();
|
||||
inline VariantWithId CollectionData::getPreviousSlot(
|
||||
VariantData* target, const ResourceManager* resources) const {
|
||||
auto prev = VariantWithId();
|
||||
auto currentId = head_;
|
||||
while (currentId != NULL_SLOT) {
|
||||
auto currentSlot = resources->getSlot(currentId);
|
||||
auto currentSlot = resources->getVariant(currentId);
|
||||
if (currentSlot == target)
|
||||
break;
|
||||
prev = SlotWithId(currentSlot, currentId);
|
||||
prev = VariantWithId(currentSlot, currentId);
|
||||
currentId = currentSlot->next();
|
||||
}
|
||||
return prev;
|
||||
@@ -90,7 +90,7 @@ inline void CollectionData::removeOne(iterator it, ResourceManager* resources) {
|
||||
head_ = next;
|
||||
if (next == NULL_SLOT)
|
||||
tail_ = prev.id();
|
||||
resources->freeSlot({it.slot_, it.currentId_});
|
||||
resources->freeVariant({it.slot_, it.currentId_});
|
||||
}
|
||||
|
||||
inline void CollectionData::removePair(ObjectData::iterator it,
|
||||
@@ -101,11 +101,11 @@ inline void CollectionData::removePair(ObjectData::iterator it,
|
||||
auto keySlot = it.slot_;
|
||||
|
||||
auto valueId = it.nextId_;
|
||||
auto valueSlot = resources->getSlot(valueId);
|
||||
auto valueSlot = resources->getVariant(valueId);
|
||||
|
||||
// remove value slot
|
||||
keySlot->setNext(valueSlot->next());
|
||||
resources->freeSlot({valueSlot, valueId});
|
||||
resources->freeVariant({valueSlot, valueId});
|
||||
|
||||
// remove key slot
|
||||
removeOne(it, resources);
|
||||
|
||||
@@ -25,9 +25,9 @@ class JsonSerializer : public VariantDataVisitor<size_t> {
|
||||
auto slotId = array.head();
|
||||
|
||||
while (slotId != NULL_SLOT) {
|
||||
auto slot = resources_->getSlot(slotId);
|
||||
auto slot = resources_->getVariant(slotId);
|
||||
|
||||
slot->data()->accept(*this);
|
||||
slot->accept(*this);
|
||||
|
||||
slotId = slot->next();
|
||||
|
||||
@@ -47,8 +47,8 @@ class JsonSerializer : public VariantDataVisitor<size_t> {
|
||||
bool isKey = true;
|
||||
|
||||
while (slotId != NULL_SLOT) {
|
||||
auto slot = resources_->getSlot(slotId);
|
||||
slot->data()->accept(*this);
|
||||
auto slot = resources_->getVariant(slotId);
|
||||
slot->accept(*this);
|
||||
|
||||
slotId = slot->next();
|
||||
|
||||
|
||||
@@ -13,8 +13,10 @@
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
class VariantSlot;
|
||||
union VariantSlot;
|
||||
class VariantPool;
|
||||
class VariantData;
|
||||
class VariantWithId;
|
||||
|
||||
class ResourceManager {
|
||||
public:
|
||||
@@ -49,18 +51,11 @@ class ResourceManager {
|
||||
return overflowed_;
|
||||
}
|
||||
|
||||
SlotWithId allocSlot() {
|
||||
auto p = variantPools_.allocSlot(allocator_);
|
||||
if (!p)
|
||||
overflowed_ = true;
|
||||
return p;
|
||||
}
|
||||
VariantWithId allocVariant();
|
||||
|
||||
void freeSlot(SlotWithId slot);
|
||||
void freeVariant(VariantWithId slot);
|
||||
|
||||
VariantSlot* getSlot(SlotId id) const {
|
||||
return variantPools_.getSlot(id);
|
||||
}
|
||||
VariantData* getVariant(SlotId id) const;
|
||||
|
||||
template <typename TAdaptedString>
|
||||
StringNode* saveString(TAdaptedString str) {
|
||||
|
||||
@@ -7,12 +7,26 @@
|
||||
#include <ArduinoJson/Collection/CollectionData.hpp>
|
||||
#include <ArduinoJson/Memory/ResourceManager.hpp>
|
||||
#include <ArduinoJson/Variant/VariantData.hpp>
|
||||
#include <ArduinoJson/Variant/VariantSlot.hpp>
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
inline void ResourceManager::freeSlot(SlotWithId slot) {
|
||||
slot->data()->setNull(this);
|
||||
variantPools_.freeSlot(slot);
|
||||
inline VariantWithId ResourceManager::allocVariant() {
|
||||
auto p = variantPools_.allocSlot(allocator_);
|
||||
if (!p) {
|
||||
overflowed_ = true;
|
||||
return {};
|
||||
}
|
||||
return {new (&p->variant) VariantData, p.id()};
|
||||
}
|
||||
|
||||
inline void ResourceManager::freeVariant(VariantWithId variant) {
|
||||
variant->setNull(this);
|
||||
variantPools_.freeSlot(variant);
|
||||
}
|
||||
|
||||
inline VariantData* ResourceManager::getVariant(SlotId id) const {
|
||||
return reinterpret_cast<VariantData*>(variantPools_.getSlot(id));
|
||||
}
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
class VariantSlot;
|
||||
union VariantSlot;
|
||||
class VariantPool;
|
||||
|
||||
class StringPool {
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
class VariantSlot;
|
||||
union VariantSlot;
|
||||
using SlotId = uint_t<ARDUINOJSON_SLOT_ID_SIZE * 8>;
|
||||
using SlotCount = SlotId;
|
||||
const SlotId NULL_SLOT = SlotId(-1);
|
||||
@@ -22,11 +22,15 @@ class SlotWithId {
|
||||
ARDUINOJSON_ASSERT((slot == nullptr) == (id == NULL_SLOT));
|
||||
}
|
||||
|
||||
explicit operator bool() const {
|
||||
return slot_ != nullptr;
|
||||
}
|
||||
|
||||
SlotId id() const {
|
||||
return id_;
|
||||
}
|
||||
|
||||
operator VariantSlot*() {
|
||||
VariantSlot* slot() const {
|
||||
return slot_;
|
||||
}
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ inline SlotWithId VariantPool::allocSlot() {
|
||||
return {};
|
||||
auto index = usage_++;
|
||||
auto slot = &slots_[index];
|
||||
return {new (slot) VariantSlot, SlotId(index)};
|
||||
return {slot, SlotId(index)};
|
||||
}
|
||||
|
||||
inline VariantSlot* VariantPool::getSlot(SlotId id) const {
|
||||
@@ -69,12 +69,12 @@ inline SlotWithId VariantPoolList::allocFromFreeList() {
|
||||
ARDUINOJSON_ASSERT(freeList_ != NULL_SLOT);
|
||||
auto id = freeList_;
|
||||
auto slot = getSlot(freeList_);
|
||||
freeList_ = slot->next();
|
||||
return {new (slot) VariantSlot, id};
|
||||
freeList_ = slot->free.next;
|
||||
return {slot, id};
|
||||
}
|
||||
|
||||
inline void VariantPoolList::freeSlot(SlotWithId slot) {
|
||||
slot->setNext(freeList_);
|
||||
slot->free.next = freeList_;
|
||||
freeList_ = slot.id();
|
||||
}
|
||||
|
||||
|
||||
@@ -138,7 +138,8 @@ class VariantPoolList {
|
||||
auto slot = pools_[poolIndex].allocSlot();
|
||||
if (!slot)
|
||||
return {};
|
||||
return {slot, SlotId(poolIndex * ARDUINOJSON_POOL_CAPACITY + slot.id())};
|
||||
return {slot.slot(),
|
||||
SlotId(poolIndex * ARDUINOJSON_POOL_CAPACITY + slot.id())};
|
||||
}
|
||||
|
||||
VariantPool* addPool(Allocator* allocator) {
|
||||
|
||||
@@ -61,8 +61,8 @@ class MsgPackSerializer : public VariantDataVisitor<size_t> {
|
||||
|
||||
auto slotId = array.head();
|
||||
while (slotId != NULL_SLOT) {
|
||||
auto slot = resources_->getSlot(slotId);
|
||||
slot->data()->accept(*this);
|
||||
auto slot = resources_->getVariant(slotId);
|
||||
slot->accept(*this);
|
||||
slotId = slot->next();
|
||||
}
|
||||
|
||||
@@ -83,8 +83,8 @@ class MsgPackSerializer : public VariantDataVisitor<size_t> {
|
||||
|
||||
auto slotId = object.head();
|
||||
while (slotId != NULL_SLOT) {
|
||||
auto slot = resources_->getSlot(slotId);
|
||||
slot->data()->accept(*this);
|
||||
auto slot = resources_->getVariant(slotId);
|
||||
slot->accept(*this);
|
||||
slotId = slot->next();
|
||||
}
|
||||
|
||||
|
||||
@@ -51,20 +51,20 @@ inline void ObjectData::removeMember(TAdaptedString key,
|
||||
template <typename TAdaptedString>
|
||||
inline VariantData* ObjectData::addMember(TAdaptedString key,
|
||||
ResourceManager* resources) {
|
||||
auto keySlot = resources->allocSlot();
|
||||
auto keySlot = resources->allocVariant();
|
||||
if (!keySlot)
|
||||
return nullptr;
|
||||
|
||||
auto valueSlot = resources->allocSlot();
|
||||
auto valueSlot = resources->allocVariant();
|
||||
if (!valueSlot)
|
||||
return nullptr;
|
||||
|
||||
if (!keySlot->data()->setString(key, resources))
|
||||
if (!keySlot->setString(key, resources))
|
||||
return nullptr;
|
||||
|
||||
CollectionData::appendPair(keySlot, valueSlot, resources);
|
||||
|
||||
return valueSlot->data();
|
||||
return valueSlot.data();
|
||||
}
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
|
||||
@@ -5,12 +5,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson/Memory/StringNode.hpp>
|
||||
#include <ArduinoJson/Memory/VariantPool.hpp>
|
||||
#include <ArduinoJson/Misc/SerializedValue.hpp>
|
||||
#include <ArduinoJson/Numbers/convertNumber.hpp>
|
||||
#include <ArduinoJson/Strings/JsonString.hpp>
|
||||
#include <ArduinoJson/Strings/StringAdapters.hpp>
|
||||
#include <ArduinoJson/Variant/VariantContent.hpp>
|
||||
#include <ArduinoJson/Variant/VariantSlot.hpp>
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
@@ -20,9 +20,25 @@ T parseNumber(const char* s);
|
||||
class VariantData {
|
||||
VariantContent content_; // must be first to allow cast from array to variant
|
||||
uint8_t type_;
|
||||
SlotId next_;
|
||||
|
||||
public:
|
||||
VariantData() : type_(VALUE_IS_NULL) {}
|
||||
// Placement new
|
||||
static void* operator new(size_t, void* p) noexcept {
|
||||
return p;
|
||||
}
|
||||
|
||||
static void operator delete(void*, void*) noexcept {}
|
||||
|
||||
VariantData() : type_(VALUE_IS_NULL), next_(NULL_SLOT) {}
|
||||
|
||||
SlotId next() const {
|
||||
return next_;
|
||||
}
|
||||
|
||||
void setNext(SlotId slot) {
|
||||
next_ = slot;
|
||||
}
|
||||
|
||||
template <typename TVisitor>
|
||||
typename TVisitor::result_type accept(TVisitor& visit) const {
|
||||
@@ -518,4 +534,20 @@ class VariantData {
|
||||
}
|
||||
};
|
||||
|
||||
class VariantWithId : public SlotWithId {
|
||||
public:
|
||||
VariantWithId() {}
|
||||
VariantWithId(VariantData* data, SlotId id)
|
||||
: SlotWithId(reinterpret_cast<VariantSlot*>(data), id) {}
|
||||
|
||||
VariantData* data() {
|
||||
return reinterpret_cast<VariantData*>(slot());
|
||||
}
|
||||
|
||||
VariantData* operator->() {
|
||||
ARDUINOJSON_ASSERT(data() != nullptr);
|
||||
return data();
|
||||
}
|
||||
};
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
|
||||
@@ -7,52 +7,22 @@
|
||||
#include <ArduinoJson/Memory/ResourceManager.hpp>
|
||||
#include <ArduinoJson/Polyfills/limits.hpp>
|
||||
#include <ArduinoJson/Polyfills/type_traits.hpp>
|
||||
#include <ArduinoJson/Variant/VariantContent.hpp>
|
||||
#include <ArduinoJson/Variant/VariantData.hpp>
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
struct StringNode;
|
||||
|
||||
class VariantSlot {
|
||||
// CAUTION: same layout as VariantData
|
||||
// we cannot use composition because it adds padding
|
||||
// (+20% on ESP8266 for example)
|
||||
VariantContent content_;
|
||||
uint8_t type_;
|
||||
SlotId next_;
|
||||
|
||||
public:
|
||||
// Placement new
|
||||
static void* operator new(size_t, void* p) noexcept {
|
||||
return p;
|
||||
}
|
||||
|
||||
static void operator delete(void*, void*) noexcept {}
|
||||
|
||||
VariantSlot() : type_(0), next_(NULL_SLOT) {
|
||||
(void)type_; // HACK: suppress Clang warning "private field is not used"
|
||||
}
|
||||
|
||||
VariantData* data() {
|
||||
return reinterpret_cast<VariantData*>(&content_);
|
||||
}
|
||||
|
||||
const VariantData* data() const {
|
||||
return reinterpret_cast<const VariantData*>(&content_);
|
||||
}
|
||||
|
||||
SlotId next() const {
|
||||
return next_;
|
||||
}
|
||||
|
||||
void setNext(SlotId slot) {
|
||||
next_ = slot;
|
||||
}
|
||||
struct FreeSlot {
|
||||
SlotId next;
|
||||
};
|
||||
|
||||
inline VariantData* slotData(VariantSlot* slot) {
|
||||
return reinterpret_cast<VariantData*>(slot);
|
||||
}
|
||||
union VariantSlot {
|
||||
VariantSlot() {}
|
||||
|
||||
VariantData variant;
|
||||
FreeSlot free;
|
||||
};
|
||||
|
||||
// Returns the size (in bytes) of an array with n elements.
|
||||
constexpr size_t sizeofArray(size_t n) {
|
||||
|
||||
Reference in New Issue
Block a user