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

Added JsonObjectIterator::operator->()

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

+ 12 - 17
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;
 	};
 }

+ 16 - 1
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;
 	};

+ 4 - 4
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<int>(it.value()));
+	EXPECT_STREQ("ab", it->key());
+	EXPECT_EQ(12, static_cast<int>(it->value()));
 	++it;
 	EXPECT_NE(end, it);
-	EXPECT_STREQ("cd", it.key());
-	EXPECT_EQ(34, static_cast<int>(it.value()));
+	EXPECT_STREQ("cd", it->key());
+	EXPECT_EQ(34, static_cast<int>(it->value()));
 	++it;
 	EXPECT_EQ(object.end(), it);
 }