Extracted VariantData and CollectionData classes

This commit is contained in:
Benoit Blanchon
2018-12-07 09:16:58 +01:00
parent 1ad97ebf85
commit b77b203935
45 changed files with 1129 additions and 1007 deletions

View File

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