Epic refactoring in progress...

This commit is contained in:
Benoit Blanchon
2014-10-28 16:29:55 +01:00
parent 852256c1af
commit 61218f12fd
13 changed files with 63 additions and 32 deletions

View File

@ -20,12 +20,13 @@ class IndentedPrint;
class JsonArrayConstIterator; class JsonArrayConstIterator;
class JsonArrayImpl; class JsonArrayImpl;
class JsonArrayIterator; class JsonArrayIterator;
class JsonArrayNode;
class JsonObjectConstIterator;
class JsonObjectImpl; class JsonObjectImpl;
class JsonObjectIterator; class JsonObjectIterator;
class JsonObjectConstIterator; class JsonObjectNode;
class JsonParser; class JsonParser;
class JsonValueImpl; class JsonValueImpl;
class JsonWriter; class JsonWriter;
struct JsonArrayNode;
} }
} }

View File

@ -18,7 +18,7 @@ class JsonArrayImpl {
typedef JsonArrayIterator iterator; typedef JsonArrayIterator iterator;
typedef JsonArrayConstIterator const_iterator; typedef JsonArrayConstIterator const_iterator;
JsonArrayImpl(JsonBuffer *buffer) : _buffer(buffer) {} static JsonArrayImpl *createFrom(JsonBuffer *buffer);
int size() const; int size() const;
@ -37,6 +37,8 @@ class JsonArrayImpl {
const_iterator end() const { return const_iterator(0); } const_iterator end() const { return const_iterator(0); }
private: private:
JsonArrayImpl(JsonBuffer *buffer) : _buffer(buffer), _firstNode(NULL) {}
JsonBuffer *_buffer; JsonBuffer *_buffer;
Internals::JsonArrayNode *_firstNode; Internals::JsonArrayNode *_firstNode;
}; };

View File

@ -6,14 +6,24 @@
#pragma once #pragma once
#include "../JsonValue.hpp" #include "JsonValueImpl.hpp"
#include "../JsonBuffer.hpp"
namespace ArduinoJson { namespace ArduinoJson {
namespace Internals { namespace Internals {
struct JsonArrayNode { class JsonArrayNode {
JsonArrayNode *next; public:
static JsonArrayNode* createFrom(JsonBuffer* buffer) {
void* ptr = buffer->alloc(sizeof(JsonArrayNode));
return ptr ? new (ptr) JsonArrayNode() : NULL;
}
JsonArrayNode* next;
JsonValueImpl value; JsonValueImpl value;
private:
JsonArrayNode() : next(0) {}
}; };
} }
} }

View File

@ -19,7 +19,7 @@ class JsonObjectImpl {
typedef JsonObjectIterator iterator; typedef JsonObjectIterator iterator;
typedef JsonObjectConstIterator const_iterator; typedef JsonObjectConstIterator const_iterator;
JsonObjectImpl(JsonBuffer *buffer) : _buffer(buffer), _firstNode(NULL) {} static JsonObjectImpl *createFrom(JsonBuffer *buffer);
int size() const; int size() const;
@ -38,6 +38,8 @@ class JsonObjectImpl {
void writeTo(JsonWriter &writer) const; void writeTo(JsonWriter &writer) const;
private: private:
JsonObjectImpl(JsonBuffer *buffer) : _buffer(buffer), _firstNode(NULL) {}
void addNode(JsonObjectNode *nodeToAdd); void addNode(JsonObjectNode *nodeToAdd);
void removeNode(JsonObjectNode *nodeToRemove); void removeNode(JsonObjectNode *nodeToRemove);

View File

@ -7,16 +7,24 @@
#pragma once #pragma once
#include "JsonValueImpl.hpp" #include "JsonValueImpl.hpp"
#include "../JsonBuffer.hpp"
namespace ArduinoJson { namespace ArduinoJson {
namespace Internals { namespace Internals {
struct JsonObjectNode { class JsonObjectNode {
JsonObjectNode(const char* k) : key(k) {} public:
static JsonObjectNode* createFrom(JsonBuffer* buffer, const char* key) {
void* ptr = buffer->alloc(sizeof(JsonObjectNode));
return ptr ? new (ptr) JsonObjectNode(key) : NULL;
}
const char* const key; const char* const key;
JsonValueImpl value; JsonValueImpl value;
JsonObjectNode* next; JsonObjectNode* next;
private:
JsonObjectNode(const char* k) : key(k) {}
}; };
} }
} }

View File

@ -19,6 +19,8 @@ class JsonValueImpl {
public: public:
JsonValueImpl() : _type(JSON_UNDEFINED) {} JsonValueImpl() : _type(JSON_UNDEFINED) {}
static JsonValueImpl *createFrom(JsonBuffer *buffer);
void set(bool value) { void set(bool value) {
_type = JSON_BOOLEAN; _type = JSON_BOOLEAN;
_content.asBoolean = value; _content.asBoolean = value;

View File

@ -18,7 +18,7 @@ class JsonArray : public JsonPrintable {
typedef Internals::JsonArrayIterator iterator; typedef Internals::JsonArrayIterator iterator;
typedef Internals::JsonArrayConstIterator const_iterator; typedef Internals::JsonArrayConstIterator const_iterator;
JsonArray() {} JsonArray() : _impl(NULL) {}
JsonArray(Internals::JsonArrayImpl* impl) : _impl(impl) {} JsonArray(Internals::JsonArrayImpl* impl) : _impl(impl) {}
bool success() const { return _impl; } bool success() const { return _impl; }

View File

@ -35,5 +35,3 @@ class JsonBuffer {
virtual void* alloc(size_t size) = 0; virtual void* alloc(size_t size) = 0;
}; };
} }
void* operator new(size_t size, ArduinoJson::JsonBuffer* buffer);

View File

@ -13,6 +13,11 @@
using namespace ArduinoJson; using namespace ArduinoJson;
using namespace ArduinoJson::Internals; using namespace ArduinoJson::Internals;
JsonArrayImpl *JsonArrayImpl::createFrom(JsonBuffer *buffer) {
void *ptr = buffer->alloc(sizeof(JsonArrayImpl));
return ptr ? new (ptr) JsonArrayImpl(buffer) : NULL;
}
int JsonArrayImpl::size() const { int JsonArrayImpl::size() const {
int nodeCount = 0; int nodeCount = 0;
for (JsonArrayNode *node = _firstNode; node; node = node->next) nodeCount++; for (JsonArrayNode *node = _firstNode; node; node = node->next) nodeCount++;
@ -29,7 +34,7 @@ JsonValueImpl *JsonArrayImpl::operator[](int index) const {
JsonValueImpl *JsonArrayImpl::add() { JsonValueImpl *JsonArrayImpl::add() {
if (_buffer) return NULL; if (_buffer) return NULL;
JsonArrayNode *node = new (_buffer) JsonArrayNode(); JsonArrayNode *node = JsonArrayNode::createFrom(_buffer);
if (!node) return NULL; if (!node) return NULL;
return &node->value; return &node->value;
@ -39,7 +44,7 @@ JsonArrayImpl *JsonArrayImpl::createNestedArray() {
JsonValueImpl *value = add(); JsonValueImpl *value = add();
if (!value) return NULL; if (!value) return NULL;
JsonArrayImpl *array = new (_buffer) JsonArrayImpl(_buffer); JsonArrayImpl *array = JsonArrayImpl::createFrom(_buffer);
value->set(array); value->set(array);
return array; return array;
@ -49,7 +54,7 @@ JsonObjectImpl *JsonArrayImpl::createNestedObject() {
JsonValueImpl *value = add(); JsonValueImpl *value = add();
if (!value) return NULL; if (!value) return NULL;
JsonObjectImpl *array = new (_buffer) JsonObjectImpl(_buffer); JsonObjectImpl *array = JsonObjectImpl::createFrom(_buffer);
value->set(array); value->set(array);
return array; return array;

View File

@ -17,6 +17,11 @@
using namespace ArduinoJson; using namespace ArduinoJson;
using namespace ArduinoJson::Internals; using namespace ArduinoJson::Internals;
JsonObjectImpl *JsonObjectImpl::createFrom(JsonBuffer *buffer) {
void *ptr = buffer->alloc(sizeof(JsonObjectImpl));
return ptr ? new (ptr) JsonObjectImpl(buffer) : NULL;
}
int JsonObjectImpl::size() const { int JsonObjectImpl::size() const {
int nodeCount = 0; int nodeCount = 0;
for (JsonObjectNode *node = _firstNode; node; node = node->next) nodeCount++; for (JsonObjectNode *node = _firstNode; node; node = node->next) nodeCount++;
@ -34,7 +39,7 @@ JsonArrayImpl *JsonObjectImpl::createNestedArray(char const *key) {
JsonObjectNode *node = getOrCreateNodeAt(key); JsonObjectNode *node = getOrCreateNodeAt(key);
if (!node) return NULL; if (!node) return NULL;
JsonArrayImpl *array = new (_buffer) JsonArrayImpl(_buffer); JsonArrayImpl *array = JsonArrayImpl::createFrom(_buffer);
node->value.set(array); node->value.set(array);
return array; return array;
@ -44,7 +49,7 @@ JsonObjectImpl *JsonObjectImpl::createNestedObject(const char *key) {
JsonObjectNode *node = getOrCreateNodeAt(key); JsonObjectNode *node = getOrCreateNodeAt(key);
if (!node) return NULL; if (!node) return NULL;
JsonObjectImpl *object = new (_buffer) JsonObjectImpl(_buffer); JsonObjectImpl *object = JsonObjectImpl::createFrom(_buffer);
node->value.set(object); node->value.set(object);
return object; return object;
@ -61,7 +66,7 @@ JsonObjectNode *JsonObjectImpl::getOrCreateNodeAt(const char *key) {
JsonObjectNode *existingNode = getNodeAt(key); JsonObjectNode *existingNode = getNodeAt(key);
if (existingNode) return existingNode; if (existingNode) return existingNode;
JsonObjectNode *newNode = new (_buffer) JsonObjectNode(key); JsonObjectNode *newNode = JsonObjectNode::createFrom(_buffer, key);
if (newNode) addNode(newNode); if (newNode) addNode(newNode);

View File

@ -9,9 +9,15 @@
#include "ArduinoJson/Internals/JsonObjectImpl.hpp" #include "ArduinoJson/Internals/JsonObjectImpl.hpp"
#include "ArduinoJson/Internals/JsonWriter.hpp" #include "ArduinoJson/Internals/JsonWriter.hpp"
using namespace ArduinoJson;
using namespace ArduinoJson::Internals; using namespace ArduinoJson::Internals;
void JsonValueImpl::writeTo(JsonWriter &writer) const { JsonValueImpl* JsonValueImpl::createFrom(JsonBuffer* buffer) {
void* ptr = buffer->alloc(sizeof(JsonValueImpl));
return ptr ? new (ptr) JsonValueImpl() : NULL;
}
void JsonValueImpl::writeTo(JsonWriter& writer) const {
switch (_type) { switch (_type) {
case JSON_ARRAY: case JSON_ARRAY:
_content.asArray->writeTo(writer); _content.asArray->writeTo(writer);

View File

@ -17,21 +17,16 @@
using namespace ArduinoJson; using namespace ArduinoJson;
using namespace ArduinoJson::Internals; using namespace ArduinoJson::Internals;
// TODO: what happens if alloc returns NULL
void* operator new(size_t size, ArduinoJson::JsonBuffer* buffer) {
return buffer->alloc(size);
}
JsonArray JsonBuffer::createArray() { JsonArray JsonBuffer::createArray() {
return JsonArray(new (this) JsonArrayImpl(this)); return JsonArray(JsonArrayImpl::createFrom(this));
} }
JsonObject JsonBuffer::createObject() { JsonObject JsonBuffer::createObject() {
return JsonObject(new (this) JsonObjectImpl(this)); return JsonObject(JsonObjectImpl::createFrom(this));
} }
JsonValue JsonBuffer::createValue() { JsonValue JsonBuffer::createValue() {
return JsonValue(new (this) JsonValueImpl()); return JsonValue(JsonValueImpl::createFrom(this));
} }
JsonArray JsonBuffer::parseArray(char* json) { JsonArray JsonBuffer::parseArray(char* json) {

View File

@ -38,13 +38,11 @@ class Issue10 : public testing::Test {
buffer); buffer);
} }
void nodeCountMustBe(int expected) { EXPECT_EQ(expected, json.size()); }
Person persons[2]; Person persons[2];
StaticJsonBuffer<20> json;
}; };
TEST_F(Issue10, PopulateArrayByAddingAnObject) { TEST_F(Issue10, PopulateArrayByAddingAnObject) {
StaticJsonBuffer<20> json;
JsonArray array = json.createArray(); JsonArray array = json.createArray();
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
@ -58,10 +56,10 @@ TEST_F(Issue10, PopulateArrayByAddingAnObject) {
} }
checkJsonString(array); checkJsonString(array);
nodeCountMustBe(15);
} }
TEST_F(Issue10, PopulateArrayByCreatingNestedObjects) { TEST_F(Issue10, PopulateArrayByCreatingNestedObjects) {
StaticJsonBuffer<20> json;
JsonArray array = json.createArray(); JsonArray array = json.createArray();
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
@ -72,5 +70,4 @@ TEST_F(Issue10, PopulateArrayByCreatingNestedObjects) {
} }
checkJsonString(array); checkJsonString(array);
nodeCountMustBe(11);
} }