Replaced JsonArrayIterator and JsonArrayConstIterator by a template

This commit is contained in:
Benoit Blanchon
2014-11-05 09:04:26 +01:00
parent 6ce2497879
commit c6d11294e4
4 changed files with 43 additions and 77 deletions

View File

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

View File

@ -6,10 +6,10 @@
#pragma once
#include "Internals/JsonArrayNode.hpp"
#include "Internals/JsonIterator.hpp"
#include "Internals/JsonPrintable.hpp"
#include "Internals/ReferenceType.hpp"
#include "JsonArrayConstIterator.hpp"
#include "JsonArrayIterator.hpp"
#define JSON_ARRAY_SIZE(NUMBER_OF_ELEMENTS) \
(sizeof(JsonArray) + (NUMBER_OF_ELEMENTS) * sizeof(Internals::JsonArrayNode))
@ -25,8 +25,10 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
public:
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;

View File

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

View File

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