From 451c0ee70d7f16189b435bc1925fee52f4a10cb8 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Wed, 22 Oct 2014 23:32:25 +0200 Subject: [PATCH] Added JsonObjectIterator::operator->() --- include/ArduinoJson/JsonObjectIterator.hpp | 29 +++++++++------------- include/ArduinoJson/JsonObjectKeyValue.hpp | 17 ++++++++++++- test/JsonObject_Iterator_Tests.cpp | 8 +++--- 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/include/ArduinoJson/JsonObjectIterator.hpp b/include/ArduinoJson/JsonObjectIterator.hpp index 06a6fb9d..f70bc113 100644 --- a/include/ArduinoJson/JsonObjectIterator.hpp +++ b/include/ArduinoJson/JsonObjectIterator.hpp @@ -12,41 +12,36 @@ namespace ArduinoJson public: explicit JsonObjectIterator(Internals::JsonNode* node) - : _node(node) + : _objectKeyValue(node) { } - const char* key() const - { - return operator*().key(); - } - - JsonValue value() const - { - return operator*().value(); - } - void operator++() { - _node = _node->next; + ++_objectKeyValue; } JsonObjectKeyValue operator*() const { - return JsonObjectKeyValue(_node); + return _objectKeyValue; + } + + JsonObjectKeyValue* operator->() + { + return &_objectKeyValue; } bool operator==(const JsonObjectIterator& other) const { - return _node == other._node; + return _objectKeyValue == other._objectKeyValue; } bool operator!=(const JsonObjectIterator& other) const { - return _node != other._node; - } + return _objectKeyValue != other._objectKeyValue; + } private: - Internals::JsonNode* _node; + JsonObjectKeyValue _objectKeyValue; }; } \ No newline at end of file diff --git a/include/ArduinoJson/JsonObjectKeyValue.hpp b/include/ArduinoJson/JsonObjectKeyValue.hpp index 51855fbc..27ca0133 100644 --- a/include/ArduinoJson/JsonObjectKeyValue.hpp +++ b/include/ArduinoJson/JsonObjectKeyValue.hpp @@ -12,7 +12,7 @@ namespace ArduinoJson { } - const char* key() + const char* key() const { return _node->getAsObjectKey(); } @@ -22,6 +22,21 @@ namespace ArduinoJson return JsonValue(_node->getAsObjectValue()); } + void operator++() + { + _node = _node->next; + } + + bool operator==(const JsonObjectKeyValue& other) const + { + return _node == other._node; + } + + bool operator!=(const JsonObjectKeyValue& other) const + { + return _node != other._node; + } + private: Internals::JsonNode* _node; }; diff --git a/test/JsonObject_Iterator_Tests.cpp b/test/JsonObject_Iterator_Tests.cpp index da0b24d7..7550cae2 100644 --- a/test/JsonObject_Iterator_Tests.cpp +++ b/test/JsonObject_Iterator_Tests.cpp @@ -16,12 +16,12 @@ TEST(JsonObject_Iterator_Test, SimpleTest) JsonObjectIterator end = object.end(); EXPECT_NE(end, it); - EXPECT_STREQ("ab", it.key()); - EXPECT_EQ(12, static_cast(it.value())); + EXPECT_STREQ("ab", it->key()); + EXPECT_EQ(12, static_cast(it->value())); ++it; EXPECT_NE(end, it); - EXPECT_STREQ("cd", it.key()); - EXPECT_EQ(34, static_cast(it.value())); + EXPECT_STREQ("cd", it->key()); + EXPECT_EQ(34, static_cast(it->value())); ++it; EXPECT_EQ(object.end(), it); } \ No newline at end of file