Improved JsonArrayIterator

This commit is contained in:
Benoit Blanchon
2014-10-24 16:12:05 +02:00
parent 8071434515
commit bbef8931a6
5 changed files with 46 additions and 31 deletions

View File

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

View File

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

View File

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

View File

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

View File

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