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

Reduced sze by 26 bytes by inlining getOrCreateNodeAt()

Benoit Blanchon 11 лет назад
Родитель
Сommit
36ee4876c6
2 измененных файлов с 10 добавлено и 18 удалено
  1. 0 4
      include/ArduinoJson/JsonObject.hpp
  2. 10 14
      src/JsonObject.cpp

+ 0 - 4
include/ArduinoJson/JsonObject.hpp

@@ -99,10 +99,6 @@ class JsonObject : public Internals::JsonPrintable<JsonObject>,
   // Returns the list node that matches the specified key.
   node_type *getNodeAt(key_type key) const;
 
-  // Returns the list node that matches the specified key, creating it if
-  // needed.
-  node_type *getOrCreateNodeAt(key_type key);
-
   // The instance returned by JsonObject::invalid()
   static JsonObject _invalid;
 };

+ 10 - 14
src/JsonObject.cpp

@@ -29,8 +29,16 @@ const JsonVariant &JsonObject::at(const char *key) const {
 }
 
 JsonVariant &JsonObject::operator[](const char *key) {
-  node_type *node = getOrCreateNodeAt(key);
-  return node ? node->content.value : JsonVariant::invalid();
+  node_type *existingNode = getNodeAt(key);
+  if (existingNode) return existingNode->content.value;
+
+  node_type *newNode = createNode();
+  if (!newNode) return JsonVariant::invalid();
+
+  newNode->content.key = key;
+  addNode(newNode);
+
+  return newNode->content.value;
 }
 
 void JsonObject::remove(char const *key) { removeNode(getNodeAt(key)); }
@@ -56,18 +64,6 @@ JsonObject::node_type *JsonObject::getNodeAt(const char *key) const {
   return NULL;
 }
 
-JsonObject::node_type *JsonObject::getOrCreateNodeAt(const char *key) {
-  node_type *existingNode = getNodeAt(key);
-  if (existingNode) return existingNode;
-
-  node_type *newNode = createNode();
-  if (!newNode) return NULL;
-
-  newNode->content.key = key;
-  addNode(newNode);
-  return newNode;
-}
-
 void JsonObject::writeTo(JsonWriter &writer) const {
   node_type *node = _firstNode;