Browse Source

Replaced JsonArrayIterator and JsonArrayConstIterator by a template

Benoit Blanchon 11 years ago
parent
commit
c6d11294e4

+ 37 - 0
include/ArduinoJson/Internals/JsonIterator.hpp

@@ -0,0 +1,37 @@
+// Copyright Benoit Blanchon 2014
+// MIT License
+//
+// Arduino JSON library
+// https://github.com/bblanchon/ArduinoJson
+
+#pragma once
+
+namespace ArduinoJson {
+namespace Internals {
+
+template <typename TNode, typename TValue>
+class JsonIterator {
+ public:
+  explicit JsonIterator(TNode *node) : _node(node) {}
+
+  TValue &operator*() const { return _node->value; }
+  TValue *operator->() { return &_node->value; }
+
+  bool operator==(const JsonIterator<TNode, TValue> &other) const {
+    return _node == other._node;
+  }
+
+  bool operator!=(const JsonIterator<TNode, TValue> &other) const {
+    return _node != other._node;
+  }
+
+  JsonIterator<TNode, TValue> &operator++() {
+    if (_node) _node = _node->next;
+    return *this;
+  }
+
+ private:
+  TNode *_node;
+};
+}
+}

+ 6 - 4
include/ArduinoJson/JsonArray.hpp

@@ -6,10 +6,10 @@
 
 
 #pragma once
 #pragma once
 
 
+#include "Internals/JsonArrayNode.hpp"
+#include "Internals/JsonIterator.hpp"
 #include "Internals/JsonPrintable.hpp"
 #include "Internals/JsonPrintable.hpp"
 #include "Internals/ReferenceType.hpp"
 #include "Internals/ReferenceType.hpp"
-#include "JsonArrayConstIterator.hpp"
-#include "JsonArrayIterator.hpp"
 
 
 #define JSON_ARRAY_SIZE(NUMBER_OF_ELEMENTS) \
 #define JSON_ARRAY_SIZE(NUMBER_OF_ELEMENTS) \
   (sizeof(JsonArray) + (NUMBER_OF_ELEMENTS) * sizeof(Internals::JsonArrayNode))
   (sizeof(JsonArray) + (NUMBER_OF_ELEMENTS) * sizeof(Internals::JsonArrayNode))
@@ -25,8 +25,10 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
 
 
  public:
  public:
   typedef JsonVariant value_type;
   typedef JsonVariant value_type;
-  typedef JsonArrayIterator iterator;
-  typedef JsonArrayConstIterator const_iterator;
+  typedef Internals::JsonIterator<Internals::JsonArrayNode, JsonVariant>
+      iterator;
+  typedef Internals::JsonIterator<Internals::JsonArrayNode, const JsonVariant>
+      const_iterator;
 
 
   int size() const;
   int size() const;
 
 

+ 0 - 37
include/ArduinoJson/JsonArrayConstIterator.hpp

@@ -1,37 +0,0 @@
-// Copyright Benoit Blanchon 2014
-// MIT License
-//
-// Arduino JSON library
-// https://github.com/bblanchon/ArduinoJson
-
-#pragma once
-
-#include "Internals/JsonArrayNode.hpp"
-
-namespace ArduinoJson {
-
-class JsonArrayConstIterator {
- public:
-  explicit JsonArrayConstIterator(Internals::JsonArrayNode *node)
-      : _node(node) {}
-
-  const JsonVariant &operator*() const { return _node->value; }
-  const JsonVariant *operator->() { return &_node->value; }
-
-  bool operator==(const JsonArrayConstIterator &other) const {
-    return _node == other._node;
-  }
-
-  bool operator!=(const JsonArrayConstIterator &other) const {
-    return _node != other._node;
-  }
-
-  JsonArrayConstIterator &operator++() {
-    if (_node) _node = _node->next;
-    return *this;
-  }
-
- private:
-  Internals::JsonArrayNode *_node;
-};
-}

+ 0 - 36
include/ArduinoJson/JsonArrayIterator.hpp

@@ -1,36 +0,0 @@
-// Copyright Benoit Blanchon 2014
-// MIT License
-//
-// Arduino JSON library
-// https://github.com/bblanchon/ArduinoJson
-
-#pragma once
-
-#include "Internals/JsonArrayNode.hpp"
-
-namespace ArduinoJson {
-
-class JsonArrayIterator {
- public:
-  explicit JsonArrayIterator(Internals::JsonArrayNode *node) : _node(node) {}
-
-  JsonVariant &operator*() const { return _node->value; }
-  JsonVariant *operator->() { return &_node->value; }
-
-  bool operator==(const JsonArrayIterator &other) const {
-    return _node == other._node;
-  }
-
-  bool operator!=(const JsonArrayIterator &other) const {
-    return _node != other._node;
-  }
-
-  JsonArrayIterator &operator++() {
-    if (_node) _node = _node->next;
-    return *this;
-  }
-
- private:
-  Internals::JsonArrayNode *_node;
-};
-}