Kaynağa Gözat

Improved JsonArrayIterator

Benoit Blanchon 11 yıl önce
ebeveyn
işleme
bbef8931a6

+ 12 - 7
include/ArduinoJson/JsonArrayIterator.hpp

@@ -9,27 +9,32 @@
 #include "ArduinoJson/JsonValue.hpp"
 
 namespace ArduinoJson {
+
 class JsonArray;
 
 class JsonArrayIterator {
   friend class JsonArray;
 
  public:
-  explicit JsonArrayIterator(Internals::JsonNode *node) : _node(node) {}
-
-  void operator++() { _node = _node->next; }
+  explicit JsonArrayIterator(Internals::JsonNode *node) : _value(node) {}
 
-  JsonValue operator*() const { return JsonValue(_node); }
+  JsonValue operator*() const { return _value; }
+  JsonValue *operator->() { return &_value; }
 
   bool operator==(const JsonArrayIterator &other) const {
-    return _node == other._node;
+    return _value._node == other._value._node;
   }
 
   bool operator!=(const JsonArrayIterator &other) const {
-    return _node != other._node;
+    return _value._node != other._value._node;
+  }
+
+  JsonArrayIterator &operator++() {
+    _value._node = _value._node->next;
+    return *this;
   }
 
  private:
-  Internals::JsonNode *_node;
+  JsonValue _value;
 };
 }

+ 12 - 14
include/ArduinoJson/JsonObjectIterator.hpp

@@ -15,27 +15,25 @@ class JsonObjectIterator {
   friend class JsonObject;
 
  public:
-  explicit JsonObjectIterator(Internals::JsonNode *node)
-      : _objectKeyValue(node) {}
-
-  JsonObjectIterator &operator++() {
-    _objectKeyValue = JsonObjectKeyValue(_objectKeyValue.next());
-    return *this;
-  }
-
-  JsonObjectKeyValue operator*() const { return _objectKeyValue; }
-
-  JsonObjectKeyValue *operator->() { return &_objectKeyValue; }
+  JsonObjectKeyValue operator*() const { return _keyValue; }
+  JsonObjectKeyValue *operator->() { return &_keyValue; }
 
   bool operator==(const JsonObjectIterator &other) const {
-    return _objectKeyValue == other._objectKeyValue;
+    return _keyValue == other._keyValue;
   }
 
   bool operator!=(const JsonObjectIterator &other) const {
-    return _objectKeyValue != other._objectKeyValue;
+    return _keyValue != other._keyValue;
+  }
+
+  JsonObjectIterator &operator++() {
+    _keyValue._node = _keyValue._node->next;
+    return *this;
   }
 
  private:
-  JsonObjectKeyValue _objectKeyValue;
+  explicit JsonObjectIterator(Internals::JsonNode *node) : _keyValue(node) {}
+
+  JsonObjectKeyValue _keyValue;
 };
 }

+ 6 - 4
include/ArduinoJson/JsonObjectKeyValue.hpp

@@ -9,10 +9,12 @@
 #include "ArduinoJson/JsonValue.hpp"
 
 namespace ArduinoJson {
+class JsonObjectIterator;
+
 class JsonObjectKeyValue {
- public:
-  explicit JsonObjectKeyValue(Internals::JsonNode *node) : _node(node) {}
+  friend class JsonObjectIterator;
 
+ public:
   const char *key() const { return _node->getAsObjectKey(); }
 
   JsonValue value() { return JsonValue(_node->getAsObjectValue()); }
@@ -25,9 +27,9 @@ class JsonObjectKeyValue {
     return _node != other._node;
   }
 
-  Internals::JsonNode *next() { return _node->next; }
-
  private:
+  explicit JsonObjectKeyValue(Internals::JsonNode *node) : _node(node) {}
+
   Internals::JsonNode *_node;
 };
 }

+ 13 - 3
include/ArduinoJson/JsonValue.hpp

@@ -10,15 +10,22 @@
 
 namespace ArduinoJson {
 class JsonArray;
-class JsonContainer;
+class JsonArrayIterator;
+class JsonBuffer;
 class JsonObject;
+class JsonObjectIterator;
+class JsonObjectKeyValue;
 
 class JsonValue : public Internals::JsonNodeWrapper {
+  friend class JsonArray;
+  friend class JsonArrayIterator;
+  friend class JsonBuffer;
+  friend class JsonObject;
+  friend class JsonObjectKeyValue;
+
  public:
   JsonValue() {}
 
-  explicit JsonValue(Internals::JsonNode *node) : JsonNodeWrapper(node) {}
-
   void operator=(bool value);
   void operator=(const char *value);
   void operator=(double value) { set(value, 2); }
@@ -42,5 +49,8 @@ class JsonValue : public Internals::JsonNodeWrapper {
   T as() {
     return static_cast<T>(*this);
   }
+
+ private:
+  explicit JsonValue(Internals::JsonNode *node) : JsonNodeWrapper(node) {}
 };
 }

+ 2 - 2
test/JsonArray_Iterator_Tests.cpp

@@ -21,10 +21,10 @@ TEST(JsonArray_Iterator_Test, SimpleTest) {
   JsonArrayIterator end = array.end();
 
   EXPECT_NE(end, it);
-  EXPECT_EQ(12, (*it).as<int>());  // TODO: use ->
+  EXPECT_EQ(12, it->as<int>());
   ++it;
   EXPECT_NE(end, it);
-  EXPECT_EQ(34, (*it).as<int>());  // TODO: use ->
+  EXPECT_EQ(34, it->as<int>());
   ++it;
   EXPECT_EQ(array.end(), it);
 }