Made JsonNode::type private

This commit is contained in:
Benoit Blanchon
2014-10-09 14:17:09 +02:00
parent b0e12e8852
commit 24c60619d5
10 changed files with 62 additions and 61 deletions

View File

@ -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;

View File

@ -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;
void addChildToContainer(JsonNode* childToAdd);
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;
}
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

View File

@ -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);

View File

@ -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();
}

View File

@ -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();
};

View File

@ -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

View File

@ -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;
};

View File

@ -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);

View File

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

View File

@ -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");