Просмотр исходного кода

Splitted JsonIterator into NodeIterator and NodeConstIterator

Benoit Blanchon 11 лет назад
Родитель
Сommit
31dea656d5

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

@@ -1,37 +0,0 @@
-// 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->content; }
-  TValue *operator->() { return &_node->content; }
-
-  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;
-};
-}
-}

+ 2 - 0
include/ArduinoJson/Internals/Node.hpp

@@ -6,6 +6,8 @@
 
 #pragma once
 
+#include <stddef.h>  // for NULL
+
 namespace ArduinoJson {
 namespace Internals {
 

+ 37 - 0
include/ArduinoJson/Internals/NodeConstIterator.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 T>
+class NodeConstIterator {
+ public:
+  explicit NodeConstIterator(const Node<T> *node) : _node(node) {}
+
+  const T &operator*() const { return _node->content; }
+  const T *operator->() { return &_node->content; }
+
+  bool operator==(const NodeConstIterator<T> &other) const {
+    return _node == other._node;
+  }
+
+  bool operator!=(const NodeConstIterator<T> &other) const {
+    return _node != other._node;
+  }
+
+  NodeConstIterator<T> &operator++() {
+    if (_node) _node = _node->next;
+    return *this;
+  }
+
+ private:
+  const Node<T> *_node;
+};
+}
+}

+ 39 - 0
include/ArduinoJson/Internals/NodeIterator.hpp

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

+ 4 - 3
include/ArduinoJson/JsonArray.hpp

@@ -6,9 +6,10 @@
 
 #pragma once
 
-#include "Internals/JsonIterator.hpp"
 #include "Internals/JsonPrintable.hpp"
 #include "Internals/Node.hpp"
+#include "Internals/NodeConstIterator.hpp"
+#include "Internals/NodeIterator.hpp"
 #include "Internals/ReferenceType.hpp"
 #include "JsonVariant.hpp"
 
@@ -27,8 +28,8 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
  public:
   typedef JsonVariant value_type;
   typedef Internals::Node<JsonVariant> node_type;
-  typedef Internals::JsonIterator<node_type, JsonVariant> iterator;
-  typedef Internals::JsonIterator<node_type, const JsonVariant> const_iterator;
+  typedef Internals::NodeIterator<JsonVariant> iterator;
+  typedef Internals::NodeConstIterator<JsonVariant> const_iterator;
 
   int size() const;
 

+ 4 - 3
include/ArduinoJson/JsonObject.hpp

@@ -6,7 +6,8 @@
 
 #pragma once
 
-#include "Internals/JsonIterator.hpp"
+#include "Internals/NodeIterator.hpp"
+#include "Internals/NodeConstIterator.hpp"
 #include "Internals/JsonPrintable.hpp"
 #include "Internals/Node.hpp"
 #include "Internals/ReferenceType.hpp"
@@ -28,8 +29,8 @@ class JsonObject : public Internals::JsonPrintable<JsonObject>,
   typedef const char *key_type;
   typedef JsonPair value_type;
   typedef Internals::Node<JsonPair> node_type;
-  typedef Internals::JsonIterator<node_type, JsonPair> iterator;
-  typedef Internals::JsonIterator<node_type, const JsonPair> const_iterator;
+  typedef Internals::NodeIterator<JsonPair> iterator;
+  typedef Internals::NodeConstIterator<JsonPair> const_iterator;
 
   bool success() const { return _buffer != NULL; }
   int size() const;