Renamed and moved internal files

This commit is contained in:
Benoit Blanchon
2018-11-30 17:53:54 +01:00
parent 04e8acd844
commit aaf0d5c3c5
52 changed files with 1098 additions and 1119 deletions

View File

@@ -0,0 +1,33 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#pragma once
namespace ARDUINOJSON_NAMESPACE {
class Key {
public:
Key(const VariantSlot* slot) : _slot(slot) {}
operator const char*() const {
return c_str();
}
const char* c_str() const {
return _slot ? slotGetKey(_slot) : 0;
}
bool isNull() const {
return _slot == 0 || _slot->key == 0;
}
friend bool operator==(Key lhs, const char* rhs) {
if (lhs.isNull()) return rhs == 0;
return rhs ? !strcmp(lhs, rhs) : false;
}
private:
const VariantSlot* _slot;
};
} // namespace ARDUINOJSON_NAMESPACE

View File

@@ -0,0 +1,124 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#pragma once
#include "../Memory/MemoryPool.hpp"
#include "../Variant/SlotFunctions.hpp"
#include "../Variant/VariantData.hpp"
namespace ARDUINOJSON_NAMESPACE {
template <typename TKey>
inline VariantSlot* objectFindSlot(const ObjectData* obj, TKey key) {
if (!obj) return 0;
VariantSlot* slot = obj->head;
while (slot) {
if (key.equals(slotGetKey(slot))) break;
slot = slot->getNext();
}
return slot;
}
template <typename TKey>
inline bool objectContainsKey(const ObjectData* obj, const TKey& key) {
return objectFindSlot(obj, key) != 0;
}
template <typename TKey>
inline VariantData* objectAdd(ObjectData* obj, TKey key, MemoryPool* pool) {
VariantSlot* slot = pool->allocVariant();
if (!slot) return 0;
slot->next = 0;
slot->value.type = JSON_NULL;
if (obj->tail) {
slot->attachTo(obj->tail);
obj->tail = slot;
} else {
slot->prev = 0;
obj->head = slot;
obj->tail = slot;
}
if (!slotSetKey(slot, key, pool)) return 0;
return &slot->value;
}
template <typename TKey>
inline VariantData* objectSet(ObjectData* 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->value;
return objectAdd(obj, key, pool);
}
template <typename TKey>
inline VariantData* objectGet(const ObjectData* obj, TKey key) {
VariantSlot* slot = objectFindSlot(obj, key);
return slot ? &slot->value : 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();
VariantSlot* next = slot->getNext();
if (prev)
prev->setNext(next);
else
obj->head = next;
if (next)
next->setPrev(prev);
else
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->value.keyIsOwned)
var = objectAdd(dst, ZeroTerminatedRamString(s->key), pool);
else
var = objectAdd(dst, ZeroTerminatedRamStringConst(s->key), pool);
if (!variantCopy(var, &s->value, 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->value;
VariantData* v2 = objectGet(o2, makeString(slotGetKey(s)));
if (!variantEquals(v1, v2)) return false;
}
return true;
}
} // namespace ARDUINOJSON_NAMESPACE

View File

@@ -0,0 +1,21 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#pragma once
#include "../Array/ArrayRef.hpp"
#include "ObjectRef.hpp"
namespace ARDUINOJSON_NAMESPACE {
template <typename TString>
inline ArrayRef ObjectRef::createNestedArray(const TString& key) const {
return set(key).template to<ArrayRef>();
}
template <typename TString>
inline ArrayRef ObjectRef::createNestedArray(TString* key) const {
return set(key).template to<ArrayRef>();
}
} // namespace ARDUINOJSON_NAMESPACE

View File

@@ -0,0 +1,124 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#pragma once
#include "../Variant/SlotFunctions.hpp"
#include "Pair.hpp"
namespace ARDUINOJSON_NAMESPACE {
class PairPtr {
public:
PairPtr(MemoryPool *memoryPool, VariantSlot *slot)
: _pair(memoryPool, slot) {}
const Pair *operator->() const {
return &_pair;
}
const Pair &operator*() const {
return _pair;
}
private:
Pair _pair;
};
class ObjectIterator {
public:
ObjectIterator() : _slot(0) {}
explicit ObjectIterator(MemoryPool *memoryPool, VariantSlot *slot)
: _memoryPool(memoryPool), _slot(slot) {}
Pair operator*() const {
return Pair(_memoryPool, _slot);
}
PairPtr operator->() {
return PairPtr(_memoryPool, _slot);
}
bool operator==(const ObjectIterator &other) const {
return _slot == other._slot;
}
bool operator!=(const ObjectIterator &other) const {
return _slot != other._slot;
}
ObjectIterator &operator++() {
_slot = _slot->getNext();
return *this;
}
ObjectIterator &operator+=(size_t distance) {
_slot = _slot->getNext(distance);
return *this;
}
VariantSlot *internal() {
return _slot;
}
private:
MemoryPool *_memoryPool;
VariantSlot *_slot;
};
class PairConstPtr {
public:
PairConstPtr(const VariantSlot *slot) : _pair(slot) {}
const PairConst *operator->() const {
return &_pair;
}
const PairConst &operator*() const {
return _pair;
}
private:
PairConst _pair;
};
class ObjectConstIterator {
public:
ObjectConstIterator() : _slot(0) {}
explicit ObjectConstIterator(const VariantSlot *slot) : _slot(slot) {}
PairConst operator*() const {
return PairConst(_slot);
}
PairConstPtr operator->() {
return PairConstPtr(_slot);
}
bool operator==(const ObjectConstIterator &other) const {
return _slot == other._slot;
}
bool operator!=(const ObjectConstIterator &other) const {
return _slot != other._slot;
}
ObjectConstIterator &operator++() {
_slot = _slot->getNext();
return *this;
}
ObjectConstIterator &operator+=(size_t distance) {
_slot = _slot->getNext(distance);
return *this;
}
const VariantSlot *internal() {
return _slot;
}
private:
const VariantSlot *_slot;
};
} // namespace ARDUINOJSON_NAMESPACE

View File

@@ -0,0 +1,288 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#pragma once
#include "ObjectFunctions.hpp"
#include "ObjectIterator.hpp"
// Returns the size (in bytes) of an object with n elements.
// Can be very handy to determine the size of a StaticMemoryPool.
#define JSON_OBJECT_SIZE(NUMBER_OF_ELEMENTS) \
((NUMBER_OF_ELEMENTS) * sizeof(ARDUINOJSON_NAMESPACE::VariantSlot))
namespace ARDUINOJSON_NAMESPACE {
template <typename TData>
class ObjectRefBase {
public:
// Tells weither the specified key is present and associated with a value.
//
// bool containsKey(TKey);
// TKey = const std::string&, const String&
template <typename TKey>
FORCE_INLINE bool containsKey(const TKey& key) const {
return objectContainsKey(_data, makeString(key));
}
//
// bool containsKey(TKey);
// TKey = char*, const char*, char[], const char[], const FlashStringHelper*
template <typename TKey>
FORCE_INLINE bool containsKey(TKey* key) const {
return objectContainsKey(_data, makeString(key));
}
FORCE_INLINE bool isNull() const {
return _data == 0;
}
FORCE_INLINE size_t size() const {
return objectSize(_data);
}
protected:
ObjectRefBase(TData* data) : _data(data) {}
TData* _data;
};
class ObjectConstRef : public ObjectRefBase<const ObjectData>,
public Visitable {
friend class ObjectRef;
typedef ObjectRefBase<const ObjectData> 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();
}
FORCE_INLINE iterator begin() const {
if (!_data) return iterator();
return iterator(_data->head);
}
FORCE_INLINE iterator end() const {
return iterator();
}
// Gets the value associated with the specified key.
//
// TValue get<TValue>(TKey) const;
// TKey = const std::string&, const String&
// TValue = bool, char, long, int, short, float, double,
// std::string, String, ArrayConstRef, ObjectConstRef
template <typename TKey>
FORCE_INLINE VariantConstRef get(const TKey& key) const {
return get_impl(makeString(key));
}
//
// TValue get<TValue>(TKey) const;
// TKey = char*, const char*, const FlashStringHelper*
// TValue = bool, char, long, int, short, float, double,
// std::string, String, ArrayConstRef, ObjectConstRef
template <typename TKey>
FORCE_INLINE VariantConstRef get(TKey* key) const {
return get_impl(makeString(key));
}
//
// VariantConstRef operator[](TKey) const;
// TKey = const std::string&, const String&
template <typename TKey>
FORCE_INLINE typename enable_if<IsString<TKey>::value, VariantConstRef>::type
operator[](const TKey& key) const {
return get_impl(makeString(key));
}
//
// VariantConstRef operator[](TKey) const;
// TKey = const char*, const char[N], const FlashStringHelper*
template <typename TKey>
FORCE_INLINE typename enable_if<IsString<TKey*>::value, VariantConstRef>::type
operator[](TKey* key) const {
return get_impl(makeString(key));
}
FORCE_INLINE bool operator==(ObjectConstRef rhs) const {
return objectEquals(_data, rhs._data);
}
private:
template <typename TKey>
FORCE_INLINE VariantConstRef get_impl(TKey key) const {
return VariantConstRef(objectGet(_data, key));
}
};
class ObjectRef : public ObjectRefBase<ObjectData>, public Visitable {
typedef ObjectRefBase<ObjectData> 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) {}
operator VariantRef() const {
return VariantRef(_memoryPool, getVariantData(_data));
}
operator ObjectConstRef() const {
return ObjectConstRef(_data);
}
FORCE_INLINE iterator begin() const {
if (!_data) return iterator();
return iterator(_memoryPool, _data->head);
}
FORCE_INLINE iterator end() const {
return iterator();
}
void clear() const {
objectClear(_data);
}
FORCE_INLINE bool copyFrom(ObjectConstRef src) {
return objectCopy(_data, src._data, _memoryPool);
}
// Creates and adds a ArrayRef.
//
// ArrayRef createNestedArray(TKey);
// TKey = const std::string&, const String&
template <typename TKey>
FORCE_INLINE ArrayRef createNestedArray(const TKey& key) const;
// ArrayRef createNestedArray(TKey);
// TKey = char*, const char*, char[], const char[], const FlashStringHelper*
template <typename TKey>
FORCE_INLINE ArrayRef createNestedArray(TKey* key) const;
// Creates and adds a ObjectRef.
//
// ObjectRef createNestedObject(TKey);
// TKey = const std::string&, const String&
template <typename TKey>
FORCE_INLINE ObjectRef createNestedObject(const TKey& key) const {
return set(key).template to<ObjectRef>();
}
//
// ObjectRef createNestedObject(TKey);
// TKey = char*, const char*, char[], const char[], const FlashStringHelper*
template <typename TKey>
FORCE_INLINE ObjectRef createNestedObject(TKey* key) const {
return set(key).template to<ObjectRef>();
}
// Gets the value associated with the specified key.
//
// TValue get<TValue>(TKey) const;
// TKey = const std::string&, const String&
// TValue = bool, char, long, int, short, float, double,
// std::string, String, ArrayRef, ObjectRef
template <typename TKey>
FORCE_INLINE VariantRef get(const TKey& key) const {
return get_impl(makeString(key));
}
//
// TValue get<TValue>(TKey) const;
// TKey = char*, const char*, const FlashStringHelper*
// TValue = bool, char, long, int, short, float, double,
// std::string, String, ArrayRef, ObjectRef
template <typename TKey>
FORCE_INLINE VariantRef get(TKey* key) const {
return get_impl(makeString(key));
}
// Gets or sets the value associated with the specified key.
//
// ObjectSubscript operator[](TKey)
// TKey = const std::string&, const String&
template <typename TKey>
FORCE_INLINE ObjectSubscript<const TKey&> operator[](const TKey& key) const {
return ObjectSubscript<const TKey&>(*this, key);
}
//
// ObjectSubscript operator[](TKey)
// TKey = char*, const char*, char[], const char[N], const FlashStringHelper*
template <typename TKey>
FORCE_INLINE ObjectSubscript<TKey*> operator[](TKey* key) const {
return ObjectSubscript<TKey*>(*this, key);
}
FORCE_INLINE bool operator==(ObjectRef rhs) const {
return objectEquals(_data, rhs._data);
}
FORCE_INLINE void remove(iterator it) const {
objectRemove(_data, it.internal());
}
// Removes the specified key and the associated value.
//
// void remove(TKey);
// TKey = const std::string&, const String&
template <typename TKey>
FORCE_INLINE void remove(const TKey& key) const {
remove_impl(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));
}
template <typename TKey>
FORCE_INLINE VariantRef set(TKey* key) const {
return set_impl(makeString(key));
}
template <typename TKey>
FORCE_INLINE VariantRef set(const TKey& key) const {
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 set_impl(TKey key) const {
return VariantRef(_memoryPool, objectSet(_data, key, _memoryPool));
}
template <typename TStringRef>
FORCE_INLINE void remove_impl(TStringRef key) const {
objectRemove(_data, objectFindSlot(_data, key));
}
MemoryPool* _memoryPool;
};
} // namespace ARDUINOJSON_NAMESPACE

View File

@@ -0,0 +1,132 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#pragma once
#include "../Configuration.hpp"
#include "../Operators/VariantOperators.hpp"
#include "../Polyfills/type_traits.hpp"
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4522)
#endif
namespace ARDUINOJSON_NAMESPACE {
template <typename TStringRef>
class ObjectSubscript : public VariantOperators<ObjectSubscript<TStringRef> >,
public Visitable {
typedef ObjectSubscript<TStringRef> this_type;
public:
FORCE_INLINE ObjectSubscript(ObjectRef object, TStringRef key)
: _object(object), _key(key) {}
operator VariantConstRef() const {
return get_impl();
}
FORCE_INLINE this_type &operator=(const this_type &src) {
set_impl().set(src);
return *this;
}
// Set the specified value
//
// operator=(const TValue&);
// TValue = bool, char, long, int, short, float, double,
// std::string, String, ArrayRef, ObjectRef
template <typename TValue>
FORCE_INLINE typename enable_if<!is_array<TValue>::value, this_type &>::type
operator=(const TValue &src) {
set_impl().set(src);
return *this;
}
//
// operator=(TValue);
// TValue = char*, const char*, const FlashStringHelper*
template <typename TValue>
FORCE_INLINE this_type &operator=(TValue *src) {
set_impl().set(src);
return *this;
}
FORCE_INLINE bool isNull() const {
return get_impl().isNull();
}
template <typename TValue>
FORCE_INLINE typename VariantAs<TValue>::type as() const {
return get_impl().template as<TValue>();
}
template <typename TValue>
FORCE_INLINE bool is() const {
return get_impl().template is<TValue>();
}
template <typename TValue>
FORCE_INLINE typename VariantTo<TValue>::type to() {
return set_impl().template to<TValue>();
}
// Sets the specified value.
//
// bool set(const TValue&);
// TValue = bool, char, long, int, short, float, double, serialized,
// VariantRef,
// std::string, String, ArrayRef, ObjectRef
template <typename TValue>
FORCE_INLINE typename enable_if<!is_array<TValue>::value, bool>::type set(
const TValue &value) {
return set_impl().set(value);
}
//
// bool set(TValue);
// TValue = char*, const char, const FlashStringHelper*
template <typename TValue>
FORCE_INLINE bool set(const TValue *value) {
return set_impl().set(value);
}
template <typename Visitor>
void accept(Visitor &visitor) const {
return get_impl().accept(visitor);
}
private:
FORCE_INLINE VariantRef get_impl() const {
return _object.get(_key);
}
FORCE_INLINE VariantRef set_impl() const {
return _object.set(_key);
}
ObjectRef _object;
TStringRef _key;
};
template <typename TImpl>
template <typename TString>
inline typename enable_if<IsString<TString>::value,
ObjectSubscript<const TString &> >::type
VariantSubscripts<TImpl>::operator[](const TString &key) const {
return impl()->template as<ObjectRef>()[key];
}
template <typename TImpl>
template <typename TString>
inline typename enable_if<IsString<TString *>::value,
ObjectSubscript<TString *> >::type
VariantSubscripts<TImpl>::operator[](TString *key) const {
return impl()->template as<ObjectRef>()[key];
}
} // namespace ARDUINOJSON_NAMESPACE
#ifdef _MSC_VER
#pragma warning(pop)
#endif

View File

@@ -0,0 +1,53 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#pragma once
#include "../Variant/VariantRef.hpp"
#include "Key.hpp"
namespace ARDUINOJSON_NAMESPACE {
// A key value pair for ObjectData.
class Pair {
public:
Pair(MemoryPool* memoryPool, VariantSlot* slot) : _key(slot) {
if (slot) {
_value = VariantRef(memoryPool, &slot->value);
}
}
Key key() const {
return _key;
}
VariantRef value() const {
return _value;
}
private:
Key _key;
VariantRef _value;
};
class PairConst {
public:
PairConst(const VariantSlot* slot) : _key(slot) {
if (slot) {
_value = VariantConstRef(&slot->value);
}
}
Key key() const {
return _key;
}
VariantConstRef value() const {
return _value;
}
private:
Key _key;
VariantConstRef _value;
};
} // namespace ARDUINOJSON_NAMESPACE