فهرست منبع

Removed usages of JsonNodeIterator

Benoit Blanchon 11 سال پیش
والد
کامیت
1f6cd8e56e

+ 1 - 0
include/ArduinoJson/ForwardDeclarations.hpp

@@ -9,6 +9,7 @@
 namespace ArduinoJson {
 class JsonArray;
 class JsonArrayIterator;
+class JsonArrayConstIterator;
 class JsonBuffer;
 class JsonObject;
 class JsonObjectIterator;

+ 9 - 3
include/ArduinoJson/JsonArray.hpp

@@ -12,9 +12,13 @@
 namespace ArduinoJson {
 class JsonArray : public JsonContainer {
  public:
+  typedef JsonArrayIterator iterator;
+  typedef JsonArrayConstIterator const_iterator;
+
   JsonArray() {}
 
-  explicit JsonArray(Internals::JsonNode *node) : JsonContainer(node) {}
+  explicit JsonArray(Internals::JsonNode *node)
+      : JsonContainer(node) {}  // TODO: hide
 
   JsonValue operator[](int index) const;
 
@@ -30,8 +34,10 @@ class JsonArray : public JsonContainer {
 
   bool success() { return _node && _node->isArray(); }
 
-  JsonArrayIterator begin();
+  iterator begin() { return iterator(firstChild()); }
+  iterator end() { return iterator(0); }
 
-  JsonArrayIterator end() { return JsonArrayIterator(0); }
+  const_iterator begin() const { return const_iterator(firstChild()); }
+  const_iterator end() const { return const_iterator(0); }
 };
 }

+ 27 - 1
include/ArduinoJson/JsonArrayIterator.hpp

@@ -36,4 +36,30 @@ class JsonArrayIterator {
  private:
   JsonValue _value;
 };
-}
+
+class JsonArrayConstIterator {
+  friend class JsonArray;
+
+ public:
+  explicit JsonArrayConstIterator(Internals::JsonNode *node) : _value(node) {}
+
+  const JsonValue operator*() const { return _value; }
+  const JsonValue *operator->() { return &_value; }
+
+  bool operator==(const JsonArrayConstIterator &other) const {
+    return _value._node == other._value._node;
+  }
+
+  bool operator!=(const JsonArrayConstIterator &other) const {
+    return _value._node != other._value._node;
+  }
+
+  JsonArrayConstIterator &operator++() {
+    _value._node = _value._node->next;
+    return *this;
+  }
+
+ private:
+  JsonValue _value;
+};
+}

+ 1 - 0
include/ArduinoJson/JsonContainer.hpp

@@ -44,5 +44,6 @@ class JsonContainer : public Printable, public Internals::JsonNodeWrapper {
   void addChild(Internals::JsonNode *);
   void removeChild(Internals::JsonNode *);
   Internals::JsonNode *createNode();
+  Internals::JsonNode* firstChild() const;
 };
 }

+ 1 - 1
include/ArduinoJson/JsonObject.hpp

@@ -24,7 +24,7 @@ class JsonObject : public JsonContainer {
 
   bool success() { return _node && _node->isObject(); }
 
-  JsonObjectIterator begin();
+  JsonObjectIterator begin() { return JsonObjectIterator(firstChild()); }
 
   JsonObjectIterator end() { return JsonObjectIterator(0); }
 

+ 2 - 2
include/ArduinoJson/JsonObjectIterator.hpp

@@ -19,11 +19,11 @@ class JsonObjectIterator {
   JsonObjectKeyValue *operator->() { return &_keyValue; }
 
   bool operator==(const JsonObjectIterator &other) const {
-    return _keyValue == other._keyValue;
+      return _keyValue._node == other._keyValue._node;
   }
 
   bool operator!=(const JsonObjectIterator &other) const {
-    return _keyValue != other._keyValue;
+    return _keyValue._node != other._keyValue._node;
   }
 
   JsonObjectIterator &operator++() {

+ 1 - 9
include/ArduinoJson/JsonObjectKeyValue.hpp

@@ -11,21 +11,13 @@
 
 namespace ArduinoJson {
 class JsonObjectKeyValue {
+  friend class JsonObject;
   friend class JsonObjectIterator;
 
  public:
   const char *key() const { return _node->getAsObjectKey(); }
-
   JsonValue value() { return JsonValue(_node->getAsObjectValue()); }
 
-  bool operator==(const JsonObjectKeyValue &other) const {
-    return _node == other._node;
-  }
-
-  bool operator!=(const JsonObjectKeyValue &other) const {
-    return _node != other._node;
-  }
-
  private:
   explicit JsonObjectKeyValue(Internals::JsonNode *node) : _node(node) {}
 

+ 1 - 0
include/ArduinoJson/JsonValue.hpp

@@ -14,6 +14,7 @@ namespace ArduinoJson {
 class JsonValue : public Internals::JsonNodeWrapper {
   friend class JsonArray;
   friend class JsonArrayIterator;
+  friend class JsonArrayConstIterator;
   friend class JsonBuffer;
   friend class JsonObject;
   friend class JsonObjectKeyValue;

+ 2 - 8
src/JsonArray.cpp

@@ -12,8 +12,8 @@ using namespace ArduinoJson;
 using namespace ArduinoJson::Internals;
 
 JsonValue JsonArray::operator[](int index) const {
-  for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it) {
-    if (!index) return JsonValue(*it);
+  for (const_iterator it = begin(); it != end(); ++it) {
+    if (!index) return *it;
     index--;
   }
 
@@ -82,9 +82,3 @@ JsonObject JsonArray::createNestedObject() {
 
   return JsonObject(node);
 }
-
-JsonArrayIterator JsonArray::begin() {
-  if (!_node) return end();
-
-  return JsonArrayIterator(_node->getContainerChild());
-}

+ 5 - 0
src/JsonContainer.cpp

@@ -73,3 +73,8 @@ size_t JsonContainer::size() const {
 
   return n;
 }
+
+JsonNode* JsonContainer::firstChild() const
+{
+    return _node ? _node->getContainerChild() : 0;
+}

+ 5 - 13
src/JsonObject.cpp

@@ -22,11 +22,9 @@ JsonValue JsonObject::operator[](char const *key) {
 }
 
 void JsonObject::remove(char const *key) {
-  for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it) {
-    const char *childKey = it->getAsObjectKey();
-
-    if (!strcmp(childKey, key)) {
-      removeChild(*it);
+  for (JsonObjectIterator it = begin(); it != end(); ++it) {
+    if (!strcmp(it->key(), key)) {
+      removeChild(it->_node);
     }
   }
 }
@@ -48,10 +46,8 @@ JsonObject JsonObject::createNestedObject(char const *key) {
 }
 
 JsonNode *JsonObject::getOrCreateNodeAt(const char *key) {
-  for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it) {
-    const char *childKey = it->getAsObjectKey();
-
-    if (!strcmp(childKey, key)) return it->getAsObjectValue();
+  for (JsonObjectIterator it = begin(); it != end(); ++it) {
+    if (!strcmp(it->key(), key)) return it->value()._node;
   }
 
   JsonNode *newValueNode = createNode();
@@ -66,7 +62,3 @@ JsonNode *JsonObject::getOrCreateNodeAt(const char *key) {
 
   return newValueNode;
 }
-
-JsonObjectIterator JsonObject::begin() {
-  return JsonObjectIterator(_node->getContainerChild());
-}

+ 1 - 1
test/CMakeLists.txt

@@ -1,7 +1,7 @@
 set(GTEST_DIR ../third-party/gtest-1.7.0)
 
 file(GLOB_RECURSE INC_FILES ../include/*.h)
-file(GLOB TESTS_FILES *.cpp)
+file(GLOB TESTS_FILES *.hpp *.cpp)
 
 include_directories(
 	../include

+ 3 - 2
test/JsonArray_Container_Tests.cpp

@@ -7,6 +7,7 @@
 #include <gtest/gtest.h>
 #include <ArduinoJson/StaticJsonBuffer.hpp>
 #include <ArduinoJson/JsonValue.hpp>
+#include "Printers.hpp"
 
 using namespace ArduinoJson;
 
@@ -81,8 +82,8 @@ TEST_F(JsonArray_Container_Tests, CanStoreBooleans) {
 }
 
 TEST_F(JsonArray_Container_Tests, CanStoreStrings) {
-  const char *firstString = "h3110";
-  const char *secondString = "w0r1d";
+  const char* firstString = "h3110";
+  const char* secondString = "w0r1d";
 
   array.add(firstString);
   array.add(secondString);

+ 1 - 0
test/JsonObject_Container_Tests.cpp

@@ -7,6 +7,7 @@
 #include <gtest/gtest.h>
 #include <ArduinoJson/StaticJsonBuffer.hpp>
 #include <ArduinoJson/JsonValue.hpp>
+#include "Printers.hpp"
 
 using namespace ArduinoJson;
 

+ 12 - 0
test/Printers.cpp

@@ -0,0 +1,12 @@
+// Copyright Benoit Blanchon 2014
+// MIT License
+//
+// Arduino JSON library
+// https://github.com/bblanchon/ArduinoJson
+
+#include "Printers.hpp"
+
+std::ostream& operator<<(std::ostream& os, const ArduinoJson::JsonValue& v) {
+  os << "JsonValue";  // TODO
+  return os;
+}

+ 12 - 0
test/Printers.hpp

@@ -0,0 +1,12 @@
+// Copyright Benoit Blanchon 2014
+// MIT License
+//
+// Arduino JSON library
+// https://github.com/bblanchon/ArduinoJson
+
+#pragma once
+
+#include <ArduinoJson/ForwardDeclarations.hpp>
+#include <ostream>
+
+std::ostream& operator<<(std::ostream& os, const ArduinoJson::JsonValue& v);