Browse Source

Made JsonNode::type private

Benoit Blanchon 11 years ago
parent
commit
24c60619d5

+ 35 - 0
srcs/Internals/JsonNode.cpp

@@ -34,6 +34,41 @@ void JsonNode::writeTo(JsonWriter& writer)
     }
 }
 
+void JsonNode::addChildToContainer(JsonNode* childToAdd)
+{
+    if (type != JSON_ARRAY && type != JSON_OBJECT) return;
+
+    JsonNode* lastChild = content.asContainer.child;
+
+    if (!lastChild)
+    {
+        content.asContainer.child = childToAdd;
+        return;
+    }
+
+    while (lastChild->next)
+        lastChild = lastChild->next;
+
+    lastChild->next = childToAdd;
+}
+
+void JsonNode::removeChildFromContainer(JsonNode* childToRemove)
+{
+    if (type != JSON_ARRAY && type != JSON_OBJECT) return;
+
+    if (content.asContainer.child == childToRemove)
+    {
+        content.asContainer.child = childToRemove->next;
+        return;
+    }
+
+    for (JsonNode* child = content.asContainer.child; child; child = child->next)
+    {
+        if (child->next == childToRemove)
+            child->next = childToRemove->next;
+    }
+}
+
 void JsonNode::writeArrayTo(JsonWriter& writer)
 {
     JsonNode* child = content.asContainer.child;

+ 10 - 35
srcs/Internals/JsonNode.h

@@ -22,9 +22,13 @@ class JsonWriter;
 
 struct JsonNode
 {
-    JsonNode* next;
-    JsonNodeType type; // <- TODO: hide
+    JsonNode()
+        : type(JSON_UNDEFINED), next(0)
+    {
+        
+    }
 
+    JsonNode* next;
     void writeTo(JsonWriter&); // TODO: <- move in JsonNodeSerializer
 
     void setAsArray(JsonBuffer* buffer)
@@ -112,42 +116,13 @@ struct JsonNode
         return type == JSON_KEY_VALUE ? content.asKey.value : 0;
     }
 
-    void addChildToContainer(JsonNode* childToAdd)
-    {
-        if (type != JSON_ARRAY && type != JSON_OBJECT) return;
-
-        JsonNode* lastChild = content.asContainer.child;
-
-        if (!lastChild)
-        {
-            content.asContainer.child = childToAdd;
-            return;
-        }
-
-        while (lastChild->next)
-            lastChild = lastChild->next;
-
-        lastChild->next = childToAdd;
-    }
-
-    void removeChildFromContainer(JsonNode* childToRemove)
-    {
-        if (type != JSON_ARRAY && type != JSON_OBJECT) return;
-
-        if (content.asContainer.child == childToRemove)
-        {
-            content.asContainer.child = childToRemove->next;
-            return;
-        }
+    void addChildToContainer(JsonNode* childToAdd);
 
-        for (JsonNode* child = content.asContainer.child; child; child = child->next)
-        {
-            if (child->next == childToRemove)
-                child->next = childToRemove->next;
-        }
-    }
+    void removeChildFromContainer(JsonNode* childToRemove);
 
 private:
+    JsonNodeType type; // <- TODO: hide
+
     inline void writeArrayTo(JsonWriter&);// TODO: <- move in JsonNodeSerializer
     inline void writeObjectTo(JsonWriter&);// TODO: <- move in JsonNodeSerializer
 

+ 1 - 1
srcs/JsonArray.cpp

@@ -43,7 +43,7 @@ void JsonArray::add(double value, int decimals)
 
 void JsonArray::add(long value)
 {
-    JsonNode* node = createNode(JSON_LONG);
+    JsonNode* node = createNode();
     if (!node) return;
 
     node->setAsLong(value);

+ 6 - 8
srcs/JsonBuffer.cpp

@@ -1,5 +1,6 @@
 #include "JsonBuffer.h"
 
+#include <new>
 #include <string.h> // for memset
 
 #include "JsonObject.h"
@@ -8,16 +9,13 @@
 
 JsonValue JsonBuffer::createValue()
 {
-    JsonNode* node = createNode(JSON_UNDEFINED);
-    return JsonValue(node);
+    return JsonValue(createNode());
 }
 
-JsonNode* JsonBuffer::createNode(JsonNodeType type)
+JsonNode* JsonBuffer::createNode()
 {
-    JsonNode* node = allocateNode();
+    void* node = allocateNode();
     if (!node) return 0;
-    
-    memset(node, 0, sizeof(JsonNode));
-    node->type = type;
-    return node;
+        
+    return new (node) JsonNode();
 }

+ 2 - 2
srcs/JsonBuffer.h

@@ -27,9 +27,9 @@ public:
     JsonValue createValue();
 
 protected:
-    virtual JsonNode* allocateNode() = 0;
+    virtual void* allocateNode() = 0;
 
 private:
-    JsonNode* createNode(JsonNodeType type = JSON_UNDEFINED);
+    JsonNode* createNode();
 };
 

+ 2 - 7
srcs/JsonContainer.cpp

@@ -33,19 +33,14 @@ size_t JsonContainer::prettyPrintTo(IndentedPrint& p) const
     return writer.bytesWritten();
 }
 
-JsonNode* JsonContainer::createNode(JsonNodeType type)
+JsonNode* JsonContainer::createNode()
 {
     if (!_node) return 0;
 
     JsonBuffer* buffer = _node->getContainerBuffer();
     if (!buffer) return 0;
 
-    return buffer->createNode(type);
-}
-
-bool JsonContainer::checkNodeType(JsonNodeType expectedType)
-{
-    return _node && _node->type == expectedType;
+    return buffer->createNode();
 }
 
 bool JsonContainer::operator==(const JsonContainer & other) const

+ 1 - 3
srcs/JsonContainer.h

@@ -53,9 +53,7 @@ protected:
 
     void addChild(JsonNode*);
     void removeChild(JsonNode*);
-    JsonNode* createNode(JsonNodeType type = JSON_UNDEFINED);
-
-    bool checkNodeType(JsonNodeType expectedType);
+    JsonNode* createNode();
 
     JsonNode* _node;
 };

+ 2 - 4
srcs/JsonObject.cpp

@@ -51,8 +51,6 @@ JsonObject JsonObject::createNestedObject(char const* key)
 
 JsonNode* JsonObject::getOrCreateNodeAt(const char* key)
 {
-    if (!checkNodeType(JSON_OBJECT)) return 0;
-
     for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it)
     {
         const char* childKey = it->getAsObjectKey();
@@ -61,10 +59,10 @@ JsonNode* JsonObject::getOrCreateNodeAt(const char* key)
             return it->getAsObjectValue();
     }
       
-    JsonNode* newValueNode = createNode(JSON_UNDEFINED);
+    JsonNode* newValueNode = createNode();
     if (!newValueNode) return 0;
     
-    JsonNode* newKeyNode = createNode(JSON_KEY_VALUE);
+    JsonNode* newKeyNode = createNode();
     if (!newKeyNode) return 0;
 
     newKeyNode->setAsObjectKeyValue(key, newValueNode);

+ 1 - 1
srcs/StaticJsonBuffer.h

@@ -28,7 +28,7 @@ public:
     }
 
 protected:
-    virtual JsonNode* allocateNode()
+    virtual void* allocateNode()
     {
         if (_size >= CAPACITY) return 0;
 

+ 2 - 0
tests/JsonValueTests.cpp

@@ -104,6 +104,7 @@ TEST_F(JsonValueTests, ObjectsAreCopiedByReference)
     jsonValue2 = jsonValue1;
 
     object["hello"] = "world";
+    jsonValue1 = 0;
 
     EXPECT_EQ(1, ((JsonObject) jsonValue2).size());
 }
@@ -114,6 +115,7 @@ TEST_F(JsonValueTests, ArraysAreCopiedByReference)
 
     jsonValue1 = array;
     jsonValue2 = jsonValue1;
+    jsonValue1 = 0;
     
     array.add("world");