mirror of
https://github.com/eledio-devices/thirdparty-ArduinoJson.git
synced 2025-11-01 16:14:05 +01:00
Extracted VariantData and CollectionData classes
This commit is contained in:
@@ -15,7 +15,7 @@ class Key {
|
||||
}
|
||||
|
||||
const char* c_str() const {
|
||||
return _slot ? slotGetKey(_slot) : 0;
|
||||
return _slot ? _slot->key() : 0;
|
||||
}
|
||||
|
||||
bool isNull() const {
|
||||
|
||||
@@ -4,116 +4,52 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../Memory/MemoryPool.hpp"
|
||||
#include "../Variant/SlotFunctions.hpp"
|
||||
#include "../Variant/VariantData.hpp"
|
||||
#include "../Collection/CollectionData.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
template <typename Visitor>
|
||||
void objectAccept(const CollectionData *obj, Visitor &visitor) {
|
||||
if (obj)
|
||||
visitor.visitObject(*obj);
|
||||
else
|
||||
visitor.visitNull();
|
||||
}
|
||||
|
||||
template <typename TKey>
|
||||
inline VariantSlot* objectFindSlot(const ObjectData* obj, TKey key) {
|
||||
inline bool objectContainsKey(const CollectionData *obj, TKey key) {
|
||||
return obj && obj->containsKey(key);
|
||||
}
|
||||
|
||||
inline bool objectEquals(const CollectionData *lhs, const CollectionData *rhs) {
|
||||
if (lhs == rhs) return true;
|
||||
if (!lhs || !rhs) return false;
|
||||
return lhs->equalsObject(*rhs);
|
||||
}
|
||||
|
||||
template <typename TKey>
|
||||
inline VariantData *objectGet(const CollectionData *obj, TKey key) {
|
||||
if (!obj) return 0;
|
||||
VariantSlot* slot = obj->head;
|
||||
while (slot) {
|
||||
if (key.equals(slotGetKey(slot))) break;
|
||||
slot = slot->getNext();
|
||||
}
|
||||
return slot;
|
||||
return obj->get(key);
|
||||
}
|
||||
|
||||
template <typename TKey>
|
||||
inline bool objectContainsKey(const ObjectData* obj, const TKey& key) {
|
||||
return objectFindSlot(obj, key) != 0;
|
||||
void objectRemove(CollectionData *obj, TKey key) {
|
||||
if (!obj) return;
|
||||
obj->remove(key);
|
||||
}
|
||||
|
||||
template <typename TKey>
|
||||
inline VariantData* objectAdd(ObjectData* obj, TKey key, MemoryPool* pool) {
|
||||
VariantSlot* slot = pool->allocVariant();
|
||||
if (!slot) return 0;
|
||||
|
||||
slot->init();
|
||||
|
||||
if (obj->tail) {
|
||||
slot->attachTo(obj->tail);
|
||||
obj->tail = slot;
|
||||
} else {
|
||||
obj->head = slot;
|
||||
obj->tail = slot;
|
||||
}
|
||||
|
||||
if (!slotSetKey(slot, key, pool)) return 0;
|
||||
return slot->getData();
|
||||
}
|
||||
|
||||
template <typename TKey>
|
||||
inline VariantData* objectSet(ObjectData* obj, TKey key, MemoryPool* pool) {
|
||||
inline VariantData *objectSet(CollectionData *obj, TKey key, MemoryPool *pool) {
|
||||
if (!obj) return 0;
|
||||
|
||||
// ignore null key
|
||||
if (key.isNull()) return 0;
|
||||
|
||||
// search a matching key
|
||||
VariantSlot* slot = objectFindSlot(obj, key);
|
||||
if (slot) return slot->getData();
|
||||
VariantData *var = obj->get(key);
|
||||
if (var) return var;
|
||||
|
||||
return objectAdd(obj, key, pool);
|
||||
}
|
||||
|
||||
template <typename TKey>
|
||||
inline VariantData* objectGet(const ObjectData* obj, TKey key) {
|
||||
VariantSlot* slot = objectFindSlot(obj, key);
|
||||
return slot ? slot->getData() : 0;
|
||||
}
|
||||
|
||||
inline void objectClear(ObjectData* obj) {
|
||||
if (!obj) return;
|
||||
obj->head = 0;
|
||||
obj->tail = 0;
|
||||
}
|
||||
|
||||
inline void objectRemove(ObjectData* obj, VariantSlot* slot) {
|
||||
if (!obj) return;
|
||||
if (!slot) return;
|
||||
VariantSlot* prev = slot->getPrev(obj->head);
|
||||
VariantSlot* next = slot->getNext();
|
||||
if (prev)
|
||||
prev->setNext(next);
|
||||
else
|
||||
obj->head = next;
|
||||
if (!next) obj->tail = prev;
|
||||
}
|
||||
|
||||
inline size_t objectSize(const ObjectData* obj) {
|
||||
if (!obj) return 0;
|
||||
return slotSize(obj->head);
|
||||
}
|
||||
|
||||
// bool variantCopy(VariantData*, const VariantData*, MemoryPool*);
|
||||
|
||||
inline bool objectCopy(ObjectData* dst, const ObjectData* src,
|
||||
MemoryPool* pool) {
|
||||
if (!dst || !src) return false;
|
||||
objectClear(dst);
|
||||
for (VariantSlot* s = src->head; s; s = s->getNext()) {
|
||||
VariantData* var;
|
||||
if (s->ownsKey())
|
||||
var = objectAdd(dst, ZeroTerminatedRamString(s->key()), pool);
|
||||
else
|
||||
var = objectAdd(dst, ZeroTerminatedRamStringConst(s->key()), pool);
|
||||
if (!variantCopy(var, s->getData(), pool)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool objectEquals(const ObjectData* o1, const ObjectData* o2) {
|
||||
if (o1 == o2) return true;
|
||||
if (!o1 || !o2) return false;
|
||||
|
||||
for (VariantSlot* s = o1->head; s; s = s->getNext()) {
|
||||
VariantData* v1 = s->getData();
|
||||
VariantData* v2 = objectGet(o2, makeString(slotGetKey(s)));
|
||||
if (!variantEquals(v1, v2)) return false;
|
||||
}
|
||||
return true;
|
||||
return obj->add(key, pool);
|
||||
}
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
||||
|
||||
@@ -11,8 +11,7 @@ namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
class PairPtr {
|
||||
public:
|
||||
PairPtr(MemoryPool *memoryPool, VariantSlot *slot)
|
||||
: _pair(memoryPool, slot) {}
|
||||
PairPtr(MemoryPool *pool, VariantSlot *slot) : _pair(pool, slot) {}
|
||||
|
||||
const Pair *operator->() const {
|
||||
return &_pair;
|
||||
@@ -30,14 +29,14 @@ class ObjectIterator {
|
||||
public:
|
||||
ObjectIterator() : _slot(0) {}
|
||||
|
||||
explicit ObjectIterator(MemoryPool *memoryPool, VariantSlot *slot)
|
||||
: _memoryPool(memoryPool), _slot(slot) {}
|
||||
explicit ObjectIterator(MemoryPool *pool, VariantSlot *slot)
|
||||
: _pool(pool), _slot(slot) {}
|
||||
|
||||
Pair operator*() const {
|
||||
return Pair(_memoryPool, _slot);
|
||||
return Pair(_pool, _slot);
|
||||
}
|
||||
PairPtr operator->() {
|
||||
return PairPtr(_memoryPool, _slot);
|
||||
return PairPtr(_pool, _slot);
|
||||
}
|
||||
|
||||
bool operator==(const ObjectIterator &other) const {
|
||||
@@ -49,12 +48,12 @@ class ObjectIterator {
|
||||
}
|
||||
|
||||
ObjectIterator &operator++() {
|
||||
_slot = _slot->getNext();
|
||||
_slot = _slot->next();
|
||||
return *this;
|
||||
}
|
||||
|
||||
ObjectIterator &operator+=(size_t distance) {
|
||||
_slot = _slot->getNext(distance);
|
||||
_slot = _slot->next(distance);
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -63,7 +62,7 @@ class ObjectIterator {
|
||||
}
|
||||
|
||||
private:
|
||||
MemoryPool *_memoryPool;
|
||||
MemoryPool *_pool;
|
||||
VariantSlot *_slot;
|
||||
};
|
||||
|
||||
@@ -105,12 +104,12 @@ class ObjectConstIterator {
|
||||
}
|
||||
|
||||
ObjectConstIterator &operator++() {
|
||||
_slot = _slot->getNext();
|
||||
_slot = _slot->next();
|
||||
return *this;
|
||||
}
|
||||
|
||||
ObjectConstIterator &operator+=(size_t distance) {
|
||||
_slot = _slot->getNext(distance);
|
||||
_slot = _slot->next(distance);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,11 @@ namespace ARDUINOJSON_NAMESPACE {
|
||||
template <typename TData>
|
||||
class ObjectRefBase {
|
||||
public:
|
||||
template <typename Visitor>
|
||||
FORCE_INLINE void accept(Visitor& visitor) const {
|
||||
objectAccept(_data, visitor);
|
||||
}
|
||||
|
||||
// Tells weither the specified key is present and associated with a value.
|
||||
//
|
||||
// bool containsKey(TKey);
|
||||
@@ -38,7 +43,7 @@ class ObjectRefBase {
|
||||
}
|
||||
|
||||
FORCE_INLINE size_t size() const {
|
||||
return objectSize(_data);
|
||||
return _data ? _data->size() : 0;
|
||||
}
|
||||
|
||||
protected:
|
||||
@@ -46,28 +51,20 @@ class ObjectRefBase {
|
||||
TData* _data;
|
||||
};
|
||||
|
||||
class ObjectConstRef : public ObjectRefBase<const ObjectData>,
|
||||
class ObjectConstRef : public ObjectRefBase<const CollectionData>,
|
||||
public Visitable {
|
||||
friend class ObjectRef;
|
||||
typedef ObjectRefBase<const ObjectData> base_type;
|
||||
typedef ObjectRefBase<const CollectionData> base_type;
|
||||
|
||||
public:
|
||||
typedef ObjectConstIterator iterator;
|
||||
|
||||
ObjectConstRef() : base_type(0) {}
|
||||
ObjectConstRef(const ObjectData* data) : base_type(data) {}
|
||||
|
||||
template <typename Visitor>
|
||||
FORCE_INLINE void accept(Visitor& visitor) const {
|
||||
if (_data)
|
||||
visitor.visitObject(*this);
|
||||
else
|
||||
visitor.visitNull();
|
||||
}
|
||||
ObjectConstRef(const CollectionData* data) : base_type(data) {}
|
||||
|
||||
FORCE_INLINE iterator begin() const {
|
||||
if (!_data) return iterator();
|
||||
return iterator(_data->head);
|
||||
return iterator(_data->head());
|
||||
}
|
||||
|
||||
FORCE_INLINE iterator end() const {
|
||||
@@ -122,18 +119,18 @@ class ObjectConstRef : public ObjectRefBase<const ObjectData>,
|
||||
}
|
||||
};
|
||||
|
||||
class ObjectRef : public ObjectRefBase<ObjectData>, public Visitable {
|
||||
typedef ObjectRefBase<ObjectData> base_type;
|
||||
class ObjectRef : public ObjectRefBase<CollectionData>, public Visitable {
|
||||
typedef ObjectRefBase<CollectionData> base_type;
|
||||
|
||||
public:
|
||||
typedef ObjectIterator iterator;
|
||||
|
||||
FORCE_INLINE ObjectRef() : base_type(0), _memoryPool(0) {}
|
||||
FORCE_INLINE ObjectRef(MemoryPool* buf, ObjectData* data)
|
||||
: base_type(data), _memoryPool(buf) {}
|
||||
FORCE_INLINE ObjectRef() : base_type(0), _pool(0) {}
|
||||
FORCE_INLINE ObjectRef(MemoryPool* buf, CollectionData* data)
|
||||
: base_type(data), _pool(buf) {}
|
||||
|
||||
operator VariantRef() const {
|
||||
return VariantRef(_memoryPool, getVariantData(_data));
|
||||
return VariantRef(_pool, reinterpret_cast<VariantData*>(_data));
|
||||
}
|
||||
|
||||
operator ObjectConstRef() const {
|
||||
@@ -142,7 +139,7 @@ class ObjectRef : public ObjectRefBase<ObjectData>, public Visitable {
|
||||
|
||||
FORCE_INLINE iterator begin() const {
|
||||
if (!_data) return iterator();
|
||||
return iterator(_memoryPool, _data->head);
|
||||
return iterator(_pool, _data->head());
|
||||
}
|
||||
|
||||
FORCE_INLINE iterator end() const {
|
||||
@@ -150,11 +147,13 @@ class ObjectRef : public ObjectRefBase<ObjectData>, public Visitable {
|
||||
}
|
||||
|
||||
void clear() const {
|
||||
objectClear(_data);
|
||||
if (!_data) return;
|
||||
_data->clear();
|
||||
}
|
||||
|
||||
FORCE_INLINE bool copyFrom(ObjectConstRef src) {
|
||||
return objectCopy(_data, src._data, _memoryPool);
|
||||
if (!_data || !src._data) return false;
|
||||
return _data->copyFrom(*src._data, _pool);
|
||||
}
|
||||
|
||||
// Creates and adds a ArrayRef.
|
||||
@@ -225,7 +224,8 @@ class ObjectRef : public ObjectRefBase<ObjectData>, public Visitable {
|
||||
}
|
||||
|
||||
FORCE_INLINE void remove(iterator it) const {
|
||||
objectRemove(_data, it.internal());
|
||||
if (!_data) return;
|
||||
_data->remove(it.internal());
|
||||
}
|
||||
|
||||
// Removes the specified key and the associated value.
|
||||
@@ -234,14 +234,14 @@ class ObjectRef : public ObjectRefBase<ObjectData>, public Visitable {
|
||||
// TKey = const std::string&, const String&
|
||||
template <typename TKey>
|
||||
FORCE_INLINE void remove(const TKey& key) const {
|
||||
remove_impl(makeString(key));
|
||||
objectRemove(_data, makeString(key));
|
||||
}
|
||||
//
|
||||
// void remove(TKey);
|
||||
// TKey = char*, const char*, char[], const char[], const FlashStringHelper*
|
||||
template <typename TKey>
|
||||
FORCE_INLINE void remove(TKey* key) const {
|
||||
remove_impl(makeString(key));
|
||||
objectRemove(_data, makeString(key));
|
||||
}
|
||||
|
||||
template <typename TKey>
|
||||
@@ -254,35 +254,17 @@ class ObjectRef : public ObjectRefBase<ObjectData>, public Visitable {
|
||||
return set_impl(makeString(key));
|
||||
}
|
||||
|
||||
FORCE_INLINE VariantRef set(StringInMemoryPool key) const {
|
||||
return set_impl(key);
|
||||
}
|
||||
|
||||
FORCE_INLINE VariantRef set(ZeroTerminatedRamStringConst key) const {
|
||||
return set_impl(key);
|
||||
}
|
||||
|
||||
template <typename Visitor>
|
||||
FORCE_INLINE void accept(Visitor& visitor) const {
|
||||
ObjectConstRef(_data).accept(visitor);
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename TStringRef>
|
||||
FORCE_INLINE VariantRef get_impl(TStringRef key) const {
|
||||
return VariantRef(_memoryPool, objectGet(_data, key));
|
||||
template <typename TKey>
|
||||
FORCE_INLINE VariantRef get_impl(TKey key) const {
|
||||
return VariantRef(_pool, objectGet(_data, key));
|
||||
}
|
||||
|
||||
template <typename TKey>
|
||||
FORCE_INLINE VariantRef set_impl(TKey key) const {
|
||||
return VariantRef(_memoryPool, objectSet(_data, key, _memoryPool));
|
||||
return VariantRef(_pool, objectSet(_data, key, _pool));
|
||||
}
|
||||
|
||||
template <typename TStringRef>
|
||||
FORCE_INLINE void remove_impl(TStringRef key) const {
|
||||
objectRemove(_data, objectFindSlot(_data, key));
|
||||
}
|
||||
|
||||
MemoryPool* _memoryPool;
|
||||
MemoryPool* _pool;
|
||||
};
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
||||
|
||||
@@ -8,12 +8,12 @@
|
||||
#include "Key.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
// A key value pair for ObjectData.
|
||||
// A key value pair for CollectionData.
|
||||
class Pair {
|
||||
public:
|
||||
Pair(MemoryPool* memoryPool, VariantSlot* slot) : _key(slot) {
|
||||
Pair(MemoryPool* pool, VariantSlot* slot) : _key(slot) {
|
||||
if (slot) {
|
||||
_value = VariantRef(memoryPool, slot->getData());
|
||||
_value = VariantRef(pool, slot->data());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ class PairConst {
|
||||
public:
|
||||
PairConst(const VariantSlot* slot) : _key(slot) {
|
||||
if (slot) {
|
||||
_value = VariantConstRef(slot->getData());
|
||||
_value = VariantConstRef(slot->data());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user