Epic refactoring int progress...

This commit is contained in:
Benoit Blanchon
2014-10-27 22:50:50 +01:00
parent 8988cb4761
commit 852256c1af
34 changed files with 334 additions and 256 deletions

View File

@@ -22,6 +22,7 @@ class JsonArrayImpl;
class JsonArrayIterator;
class JsonObjectImpl;
class JsonObjectIterator;
class JsonObjectConstIterator;
class JsonParser;
class JsonValueImpl;
class JsonWriter;

View File

@@ -20,8 +20,9 @@ class JsonArrayImpl {
JsonArrayImpl(JsonBuffer *buffer) : _buffer(buffer) {}
value_type operator[](int index) const;
int size() const;
value_type operator[](int index) const;
value_type add();
JsonArrayImpl *createNestedArray();
@@ -29,15 +30,15 @@ class JsonArrayImpl {
void writeTo(JsonWriter &writer) const;
iterator begin() { return iterator(_firstChild); }
iterator begin() { return iterator(_firstNode); }
iterator end() { return iterator(0); }
const_iterator begin() const { return const_iterator(_firstChild); }
const_iterator begin() const { return const_iterator(_firstNode); }
const_iterator end() const { return const_iterator(0); }
private:
JsonBuffer *_buffer;
Internals::JsonArrayNode *_firstChild;
Internals::JsonArrayNode *_firstNode;
};
}
}

View File

@@ -13,10 +13,12 @@ namespace Internals {
class JsonArrayIterator {
public:
explicit JsonArrayIterator(Internals::JsonArrayNode *node) : _node(node) {}
explicit JsonArrayIterator(Internals::JsonArrayNode *node) : _node(node) {
updateValue();
}
JsonValueImpl &operator*() const { return _node->value; }
JsonValueImpl *operator->() { return &_node->value; }
JsonValue operator*() const { return _value; }
JsonValue *operator->() { return &_value; }
bool operator==(const JsonArrayIterator &other) const {
return _node == other._node;
@@ -28,11 +30,15 @@ class JsonArrayIterator {
JsonArrayIterator &operator++() {
_node = _node->next;
updateValue();
return *this;
}
private:
void updateValue() { _value = JsonValue(_node ? &_node->value : NULL); }
JsonArrayNode *_node;
JsonValue _value;
};
}
}

View File

@@ -6,6 +6,7 @@
#pragma once
#include "../JsonPair.hpp"
#include "JsonObjectNode.hpp"
namespace ArduinoJson {
@@ -14,26 +15,26 @@ namespace Internals {
class JsonObjectConstIterator {
public:
explicit JsonObjectConstIterator(Internals::JsonObjectNode *node)
: _node(node) {}
: _pair(node) {}
JsonPair operator*() const { return _node->pair; }
JsonPair *operator->() { return &_node->pair; }
const JsonPair operator*() const { return _pair; }
const JsonPair *operator->() { return &_pair; }
bool operator==(const JsonObjectConstIterator &other) const {
return _node == other._node;
return _pair._node == other._pair._node;
}
bool operator!=(const JsonObjectConstIterator &other) const {
return _node != other._node;
return _pair._node != other._pair._node;
}
JsonObjectConstIterator &operator++() {
_node = _node->next;
_pair._node = _pair._node->next;
return *this;
}
private:
JsonObjectNode *_node;
JsonPair _pair;
};
}
}

View File

@@ -19,27 +19,33 @@ class JsonObjectImpl {
typedef JsonObjectIterator iterator;
typedef JsonObjectConstIterator const_iterator;
JsonObjectImpl(JsonBuffer *buffer) : _buffer(buffer) {}
JsonObjectImpl(JsonBuffer *buffer) : _buffer(buffer), _firstNode(NULL) {}
JsonValueImpl *operator[](const char *key) { return getOrCreateValueAt(key); }
int size() const;
JsonValueImpl *operator[](const char *key);
void remove(key_type key);
JsonArrayImpl *createNestedArray(key_type key);
JsonObjectImpl *createNestedObject(key_type key);
iterator begin() { return iterator(_firstChild); }
iterator begin() { return iterator(_firstNode); }
iterator end() { return iterator(0); }
const_iterator begin() const { return const_iterator(_firstChild); }
const_iterator begin() const { return const_iterator(_firstNode); }
const_iterator end() const { return const_iterator(0); }
void writeTo(JsonWriter &writer) const;
private:
JsonObjectNode *getNodeAt(key_type key);
void addNode(JsonObjectNode *nodeToAdd);
void removeNode(JsonObjectNode *nodeToRemove);
JsonValueImpl *getOrCreateValueAt(key_type key);
JsonObjectNode *getNodeAt(key_type key);
JsonObjectNode *getOrCreateNodeAt(key_type key);
JsonBuffer *_buffer;
JsonObjectNode *_firstChild;
JsonObjectNode *_firstNode;
};
}
}

View File

@@ -11,26 +11,26 @@ namespace Internals {
class JsonObjectIterator {
public:
explicit JsonObjectIterator(Internals::JsonObjectNode *node) : _node(node) {}
explicit JsonObjectIterator(Internals::JsonObjectNode *node) : _pair(node) {}
JsonPair &operator*() const { return _node->pair; }
JsonPair *operator->() { return &_node->pair; }
JsonPair &operator*() { return _pair; }
JsonPair *operator->() { return &_pair; }
bool operator==(const JsonObjectIterator &other) const {
return _node == other._node;
return _pair._node == other._pair._node;
}
bool operator!=(const JsonObjectIterator &other) const {
return _node != other._node;
return _pair._node != other._pair._node;
}
JsonObjectIterator &operator++() {
_node = _node->next;
_pair._node = _pair._node->next;
return *this;
}
private:
JsonObjectNode *_node;
JsonPair _pair;
};
}
}

View File

@@ -6,14 +6,17 @@
#pragma once
#include "../JsonPair.hpp"
#include "JsonValueImpl.hpp"
namespace ArduinoJson {
namespace Internals {
struct JsonObjectNode {
JsonObjectNode *next;
JsonPair pair;
JsonObjectNode(const char* k) : key(k) {}
const char* const key;
JsonValueImpl value;
JsonObjectNode* next;
};
}
}

View File

@@ -18,6 +18,7 @@ class JsonParser {
JsonArray parseArray();
JsonObject parseObject();
JsonValue parseValue();
private:
bool isEnd() { return *_ptr == 0; }

View File

@@ -49,28 +49,28 @@ class JsonValueImpl {
_content.asObject = object;
}
operator bool() const {
return _type == JSON_BOOLEAN ? _content.asBoolean : false;
}
operator char const *() const {
return _type == JSON_STRING ? _content.asString : NULL;
}
operator double() const {
return _type >= JSON_DOUBLE_0_DECIMALS ? _content.asDouble : 0;
}
operator long() const { return _type == JSON_LONG ? _content.asInteger : 0; }
operator JsonArrayImpl *() const {
JsonArrayImpl *asArray() {
return _type == JSON_ARRAY ? _content.asArray : NULL;
}
operator JsonObjectImpl *() const {
JsonObjectImpl *asObject() {
return _type == JSON_OBJECT ? _content.asObject : NULL;
}
bool asBool() const {
return _type == JSON_BOOLEAN ? _content.asBoolean : false;
}
const char *asString() const {
return _type == JSON_STRING ? _content.asString : NULL;
}
double asDouble() const {
return _type >= JSON_DOUBLE_0_DECIMALS ? _content.asDouble : 0;
}
long asLong() const { return _type == JSON_LONG ? _content.asInteger : 0; }
void writeTo(JsonWriter &writer) const;
private:

View File

@@ -6,11 +6,11 @@
#pragma once
#include "JsonContainer.hpp"
#include "JsonPrintable.hpp"
#include "Internals/JsonArrayImpl.hpp"
namespace ArduinoJson {
class JsonArray : public JsonContainer {
class JsonArray : public JsonPrintable {
friend class JsonValue;
public:
@@ -21,8 +21,11 @@ class JsonArray : public JsonContainer {
JsonArray() {}
JsonArray(Internals::JsonArrayImpl* impl) : _impl(impl) {}
value_type operator[](int index) const;
bool success() const { return _impl; }
int size() const { return _impl ? _impl->size() : 0; }
value_type operator[](int index) const;
value_type add();
template <typename T>
@@ -47,7 +50,12 @@ class JsonArray : public JsonContainer {
}
const_iterator end() const { return const_iterator(0); }
static JsonArray null() { return JsonArray(NULL); }
bool operator==(const JsonArray& other) const { return _impl == other._impl; }
protected:
virtual void writeTo(Internals::JsonWriter& writer) const {
if (_impl) _impl->writeTo(writer);
}
private:
Internals::JsonArrayImpl* _impl;

View File

@@ -6,11 +6,11 @@
#pragma once
#include "JsonContainer.hpp"
#include "JsonPrintable.hpp"
#include "Internals/JsonObjectImpl.hpp"
namespace ArduinoJson {
class JsonObject : public JsonContainer {
class JsonObject : public JsonPrintable {
friend class JsonValue;
public:
@@ -19,10 +19,17 @@ class JsonObject : public JsonContainer {
typedef Internals::JsonObjectIterator iterator;
typedef Internals::JsonObjectConstIterator const_iterator;
JsonObject() : _impl(NULL) {}
JsonObject(Internals::JsonObjectImpl* impl) : _impl(impl) {}
bool success() const { return _impl; }
int size() const { return _impl ? _impl->size() : 0; }
JsonValue operator[](key_type key);
void remove(key_type key);
void remove(key_type key) {
if (_impl) _impl->remove(key);
}
JsonArray createNestedArray(key_type key);
JsonObject createNestedObject(key_type key);
@@ -39,6 +46,15 @@ class JsonObject : public JsonContainer {
}
const_iterator end() const { return const_iterator(0); }
bool operator==(const JsonObject& other) const {
return _impl == other._impl;
}
protected:
virtual void writeTo(Internals::JsonWriter& writer) const {
if (_impl) _impl->writeTo(writer);
}
private:
Internals::JsonObjectImpl* _impl;
};

View File

@@ -7,17 +7,20 @@
#pragma once
#include "JsonValue.hpp"
#include "Internals/JsonObjectNode.hpp"
namespace ArduinoJson {
class JsonPair {
public:
JsonPair(const char *k) : _key(k) {}
friend class Internals::JsonObjectIterator;
friend class Internals::JsonObjectConstIterator;
const char *key() const { return _key; }
JsonValue &value() { return _value; }
public:
JsonPair(Internals::JsonObjectNode *node) : _node(node) {}
const char *key() const { return _node->key; }
JsonValue value() { return JsonValue(&_node->value); }
private:
const char *_key;
JsonValue _value;
Internals::JsonObjectNode *_node;
};
}

View File

@@ -6,13 +6,12 @@
#pragma once
#include "ForwardDeclarations.hpp"
#include "Arduino/Printable.hpp"
#include "Internals/IndentedPrint.hpp"
namespace ArduinoJson {
// TODO: renale to JsonPrintable
class JsonContainer : public Printable {
class JsonPrintable : public Printable {
public:
size_t printTo(char *buffer, size_t bufferSize) const;
virtual size_t printTo(Print &print) const;
@@ -20,5 +19,8 @@ class JsonContainer : public Printable {
size_t prettyPrintTo(char *buffer, size_t bufferSize) const;
size_t prettyPrintTo(Internals::IndentedPrint &print) const;
size_t prettyPrintTo(Print &print) const;
protected:
virtual void writeTo(Internals::JsonWriter &) const = 0;
};
}

View File

@@ -18,35 +18,51 @@ class JsonValue {
JsonValue() : _impl(NULL) {}
JsonValue(Internals::JsonValueImpl *impl) : _impl(impl) {}
template <typename T>
void operator=(T value) {
void set(const char *value) {
if (_impl) _impl->set(value);
}
void operator=(JsonArray array);
void operator=(JsonObject object);
void set(double value, int decimals) {
void set(bool value) {
if (_impl) _impl->set(value);
}
void set(double value, int decimals = 2) {
if (_impl) _impl->set(value, decimals);
}
void set(int value) {
if (_impl) _impl->set(static_cast<long>(value));
}
void set(long value) {
if (_impl) _impl->set(value);
}
void set(JsonObject object);
void set(JsonArray array);
operator bool() const { return _impl ? _impl->asBool() : false; }
operator int() const { return _impl ? _impl->asLong() : 0; }
operator long() const { return _impl ? _impl->asLong() : 0; }
operator double() const { return _impl ? _impl->asDouble() : 0.0; }
operator const char *() const {
return _impl ? _impl->asString() : static_cast<char *>(NULL);
}
operator JsonArray();
operator JsonObject();
bool success() { return _impl; }
template <typename T>
JsonValue operator=(T value) {
set(value);
return *this;
}
template <typename T>
T as() {
return static_cast<T>(*this);
}
operator bool() const { return _impl ? *_impl : false; }
operator int() const { return _impl ? *_impl : 0; }
operator long() const { return _impl ? *_impl : 0; }
operator double() const { return _impl ? *_impl : 0.0; }
operator const char *() const {
return _impl ? *_impl : static_cast<char *>(NULL);
protected:
virtual void writeTo(Internals::JsonWriter &writer) const {
if (_impl) _impl->writeTo(writer);
}
operator JsonArray() const;
bool success() { return _impl; }
static JsonValue null() { return JsonValue(NULL); }
private:
Internals::JsonValueImpl *_impl;

View File

@@ -22,14 +22,15 @@ class StaticJsonBuffer : public JsonBuffer {
int size() { return _size; }
protected:
virtual void *allocateNode() {
if (_size >= CAPACITY) return 0;
return &_buffer[_size++];
virtual void* alloc(size_t size) {
if (_size + size > CAPACITY) return NULL;
void* p = &_buffer[_size];
_size += size;
return p;
}
private:
Internals::JsonNode _buffer[CAPACITY];
char _buffer[CAPACITY];
int _size;
};
}