From 61218f12fdf46bd4be7fd93f4483ea2b565e50b8 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Tue, 28 Oct 2014 16:29:55 +0100 Subject: [PATCH] Epic refactoring in progress... --- include/ArduinoJson/ForwardDeclarations.hpp | 5 +++-- include/ArduinoJson/Internals/JsonArrayImpl.hpp | 4 +++- include/ArduinoJson/Internals/JsonArrayNode.hpp | 16 +++++++++++++--- include/ArduinoJson/Internals/JsonObjectImpl.hpp | 4 +++- include/ArduinoJson/Internals/JsonObjectNode.hpp | 12 ++++++++++-- include/ArduinoJson/Internals/JsonValueImpl.hpp | 2 ++ include/ArduinoJson/JsonArray.hpp | 2 +- include/ArduinoJson/JsonBuffer.hpp | 2 -- src/Internals/JsonArrayImpl.cpp | 11 ++++++++--- src/Internals/JsonObjectImpl.cpp | 11 ++++++++--- src/Internals/JsonValueImpl.cpp | 8 +++++++- src/JsonBuffer.cpp | 11 +++-------- test/Issue10.cpp | 7 ++----- 13 files changed, 63 insertions(+), 32 deletions(-) diff --git a/include/ArduinoJson/ForwardDeclarations.hpp b/include/ArduinoJson/ForwardDeclarations.hpp index 7c91b707..42cc6495 100644 --- a/include/ArduinoJson/ForwardDeclarations.hpp +++ b/include/ArduinoJson/ForwardDeclarations.hpp @@ -20,12 +20,13 @@ class IndentedPrint; class JsonArrayConstIterator; class JsonArrayImpl; class JsonArrayIterator; +class JsonArrayNode; +class JsonObjectConstIterator; class JsonObjectImpl; class JsonObjectIterator; -class JsonObjectConstIterator; +class JsonObjectNode; class JsonParser; class JsonValueImpl; class JsonWriter; -struct JsonArrayNode; } } diff --git a/include/ArduinoJson/Internals/JsonArrayImpl.hpp b/include/ArduinoJson/Internals/JsonArrayImpl.hpp index 8069e537..e7375916 100644 --- a/include/ArduinoJson/Internals/JsonArrayImpl.hpp +++ b/include/ArduinoJson/Internals/JsonArrayImpl.hpp @@ -18,7 +18,7 @@ class JsonArrayImpl { typedef JsonArrayIterator iterator; typedef JsonArrayConstIterator const_iterator; - JsonArrayImpl(JsonBuffer *buffer) : _buffer(buffer) {} + static JsonArrayImpl *createFrom(JsonBuffer *buffer); int size() const; @@ -37,6 +37,8 @@ class JsonArrayImpl { const_iterator end() const { return const_iterator(0); } private: + JsonArrayImpl(JsonBuffer *buffer) : _buffer(buffer), _firstNode(NULL) {} + JsonBuffer *_buffer; Internals::JsonArrayNode *_firstNode; }; diff --git a/include/ArduinoJson/Internals/JsonArrayNode.hpp b/include/ArduinoJson/Internals/JsonArrayNode.hpp index f58bba56..0c7a9c7e 100644 --- a/include/ArduinoJson/Internals/JsonArrayNode.hpp +++ b/include/ArduinoJson/Internals/JsonArrayNode.hpp @@ -6,14 +6,24 @@ #pragma once -#include "../JsonValue.hpp" +#include "JsonValueImpl.hpp" +#include "../JsonBuffer.hpp" namespace ArduinoJson { namespace Internals { -struct JsonArrayNode { - JsonArrayNode *next; +class JsonArrayNode { + public: + static JsonArrayNode* createFrom(JsonBuffer* buffer) { + void* ptr = buffer->alloc(sizeof(JsonArrayNode)); + return ptr ? new (ptr) JsonArrayNode() : NULL; + } + + JsonArrayNode* next; JsonValueImpl value; + + private: + JsonArrayNode() : next(0) {} }; } } diff --git a/include/ArduinoJson/Internals/JsonObjectImpl.hpp b/include/ArduinoJson/Internals/JsonObjectImpl.hpp index e935de59..f3299560 100644 --- a/include/ArduinoJson/Internals/JsonObjectImpl.hpp +++ b/include/ArduinoJson/Internals/JsonObjectImpl.hpp @@ -19,7 +19,7 @@ class JsonObjectImpl { typedef JsonObjectIterator iterator; typedef JsonObjectConstIterator const_iterator; - JsonObjectImpl(JsonBuffer *buffer) : _buffer(buffer), _firstNode(NULL) {} + static JsonObjectImpl *createFrom(JsonBuffer *buffer); int size() const; @@ -38,6 +38,8 @@ class JsonObjectImpl { void writeTo(JsonWriter &writer) const; private: + JsonObjectImpl(JsonBuffer *buffer) : _buffer(buffer), _firstNode(NULL) {} + void addNode(JsonObjectNode *nodeToAdd); void removeNode(JsonObjectNode *nodeToRemove); diff --git a/include/ArduinoJson/Internals/JsonObjectNode.hpp b/include/ArduinoJson/Internals/JsonObjectNode.hpp index ec202cf4..0f516057 100644 --- a/include/ArduinoJson/Internals/JsonObjectNode.hpp +++ b/include/ArduinoJson/Internals/JsonObjectNode.hpp @@ -7,16 +7,24 @@ #pragma once #include "JsonValueImpl.hpp" +#include "../JsonBuffer.hpp" namespace ArduinoJson { namespace Internals { -struct JsonObjectNode { - JsonObjectNode(const char* k) : key(k) {} +class JsonObjectNode { + 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; JsonValueImpl value; JsonObjectNode* next; + + private: + JsonObjectNode(const char* k) : key(k) {} }; } } diff --git a/include/ArduinoJson/Internals/JsonValueImpl.hpp b/include/ArduinoJson/Internals/JsonValueImpl.hpp index d22a5e8b..15f2a5c8 100644 --- a/include/ArduinoJson/Internals/JsonValueImpl.hpp +++ b/include/ArduinoJson/Internals/JsonValueImpl.hpp @@ -19,6 +19,8 @@ class JsonValueImpl { public: JsonValueImpl() : _type(JSON_UNDEFINED) {} + static JsonValueImpl *createFrom(JsonBuffer *buffer); + void set(bool value) { _type = JSON_BOOLEAN; _content.asBoolean = value; diff --git a/include/ArduinoJson/JsonArray.hpp b/include/ArduinoJson/JsonArray.hpp index dc13b64a..7294bb4f 100644 --- a/include/ArduinoJson/JsonArray.hpp +++ b/include/ArduinoJson/JsonArray.hpp @@ -18,7 +18,7 @@ class JsonArray : public JsonPrintable { typedef Internals::JsonArrayIterator iterator; typedef Internals::JsonArrayConstIterator const_iterator; - JsonArray() {} + JsonArray() : _impl(NULL) {} JsonArray(Internals::JsonArrayImpl* impl) : _impl(impl) {} bool success() const { return _impl; } diff --git a/include/ArduinoJson/JsonBuffer.hpp b/include/ArduinoJson/JsonBuffer.hpp index db73a9e5..a2cc4d9e 100644 --- a/include/ArduinoJson/JsonBuffer.hpp +++ b/include/ArduinoJson/JsonBuffer.hpp @@ -35,5 +35,3 @@ class JsonBuffer { virtual void* alloc(size_t size) = 0; }; } - -void* operator new(size_t size, ArduinoJson::JsonBuffer* buffer); diff --git a/src/Internals/JsonArrayImpl.cpp b/src/Internals/JsonArrayImpl.cpp index 3a85d11b..b67721cb 100644 --- a/src/Internals/JsonArrayImpl.cpp +++ b/src/Internals/JsonArrayImpl.cpp @@ -13,6 +13,11 @@ using namespace ArduinoJson; 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 nodeCount = 0; for (JsonArrayNode *node = _firstNode; node; node = node->next) nodeCount++; @@ -29,7 +34,7 @@ JsonValueImpl *JsonArrayImpl::operator[](int index) const { JsonValueImpl *JsonArrayImpl::add() { if (_buffer) return NULL; - JsonArrayNode *node = new (_buffer) JsonArrayNode(); + JsonArrayNode *node = JsonArrayNode::createFrom(_buffer); if (!node) return NULL; return &node->value; @@ -39,7 +44,7 @@ JsonArrayImpl *JsonArrayImpl::createNestedArray() { JsonValueImpl *value = add(); if (!value) return NULL; - JsonArrayImpl *array = new (_buffer) JsonArrayImpl(_buffer); + JsonArrayImpl *array = JsonArrayImpl::createFrom(_buffer); value->set(array); return array; @@ -49,7 +54,7 @@ JsonObjectImpl *JsonArrayImpl::createNestedObject() { JsonValueImpl *value = add(); if (!value) return NULL; - JsonObjectImpl *array = new (_buffer) JsonObjectImpl(_buffer); + JsonObjectImpl *array = JsonObjectImpl::createFrom(_buffer); value->set(array); return array; diff --git a/src/Internals/JsonObjectImpl.cpp b/src/Internals/JsonObjectImpl.cpp index 97177c44..adabea68 100644 --- a/src/Internals/JsonObjectImpl.cpp +++ b/src/Internals/JsonObjectImpl.cpp @@ -17,6 +17,11 @@ using namespace ArduinoJson; 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 nodeCount = 0; for (JsonObjectNode *node = _firstNode; node; node = node->next) nodeCount++; @@ -34,7 +39,7 @@ JsonArrayImpl *JsonObjectImpl::createNestedArray(char const *key) { JsonObjectNode *node = getOrCreateNodeAt(key); if (!node) return NULL; - JsonArrayImpl *array = new (_buffer) JsonArrayImpl(_buffer); + JsonArrayImpl *array = JsonArrayImpl::createFrom(_buffer); node->value.set(array); return array; @@ -44,7 +49,7 @@ JsonObjectImpl *JsonObjectImpl::createNestedObject(const char *key) { JsonObjectNode *node = getOrCreateNodeAt(key); if (!node) return NULL; - JsonObjectImpl *object = new (_buffer) JsonObjectImpl(_buffer); + JsonObjectImpl *object = JsonObjectImpl::createFrom(_buffer); node->value.set(object); return object; @@ -61,7 +66,7 @@ JsonObjectNode *JsonObjectImpl::getOrCreateNodeAt(const char *key) { JsonObjectNode *existingNode = getNodeAt(key); if (existingNode) return existingNode; - JsonObjectNode *newNode = new (_buffer) JsonObjectNode(key); + JsonObjectNode *newNode = JsonObjectNode::createFrom(_buffer, key); if (newNode) addNode(newNode); diff --git a/src/Internals/JsonValueImpl.cpp b/src/Internals/JsonValueImpl.cpp index dcdb4a05..a500b80c 100644 --- a/src/Internals/JsonValueImpl.cpp +++ b/src/Internals/JsonValueImpl.cpp @@ -9,9 +9,15 @@ #include "ArduinoJson/Internals/JsonObjectImpl.hpp" #include "ArduinoJson/Internals/JsonWriter.hpp" +using namespace ArduinoJson; 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) { case JSON_ARRAY: _content.asArray->writeTo(writer); diff --git a/src/JsonBuffer.cpp b/src/JsonBuffer.cpp index cde321a0..29a83492 100644 --- a/src/JsonBuffer.cpp +++ b/src/JsonBuffer.cpp @@ -17,21 +17,16 @@ using namespace ArduinoJson; 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() { - return JsonArray(new (this) JsonArrayImpl(this)); + return JsonArray(JsonArrayImpl::createFrom(this)); } JsonObject JsonBuffer::createObject() { - return JsonObject(new (this) JsonObjectImpl(this)); + return JsonObject(JsonObjectImpl::createFrom(this)); } JsonValue JsonBuffer::createValue() { - return JsonValue(new (this) JsonValueImpl()); + return JsonValue(JsonValueImpl::createFrom(this)); } JsonArray JsonBuffer::parseArray(char* json) { diff --git a/test/Issue10.cpp b/test/Issue10.cpp index 71616e2e..dab35f43 100644 --- a/test/Issue10.cpp +++ b/test/Issue10.cpp @@ -38,13 +38,11 @@ class Issue10 : public testing::Test { buffer); } - void nodeCountMustBe(int expected) { EXPECT_EQ(expected, json.size()); } - Person persons[2]; - StaticJsonBuffer<20> json; }; TEST_F(Issue10, PopulateArrayByAddingAnObject) { + StaticJsonBuffer<20> json; JsonArray array = json.createArray(); for (int i = 0; i < 2; i++) { @@ -58,10 +56,10 @@ TEST_F(Issue10, PopulateArrayByAddingAnObject) { } checkJsonString(array); - nodeCountMustBe(15); } TEST_F(Issue10, PopulateArrayByCreatingNestedObjects) { + StaticJsonBuffer<20> json; JsonArray array = json.createArray(); for (int i = 0; i < 2; i++) { @@ -72,5 +70,4 @@ TEST_F(Issue10, PopulateArrayByCreatingNestedObjects) { } checkJsonString(array); - nodeCountMustBe(11); }