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

Implement object comparison out of class

Benoit Blanchon 2 лет назад
Родитель
Сommit
f6b014582f

+ 0 - 5
src/ArduinoJson/Object/JsonObject.hpp

@@ -98,11 +98,6 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
     return detail::ObjectData::copy(data_, src.data_, resources_);
   }
 
-  // Compares the content of two objects.
-  FORCE_INLINE bool operator==(JsonObject rhs) const {
-    return detail::ObjectData::equals(data_, rhs.data_);
-  }
-
   // Gets or sets the member with specified key.
   // https://arduinojson.org/v6/api/jsonobject/subscript/
   template <typename TString>

+ 19 - 5
src/ArduinoJson/Object/JsonObjectConst.hpp

@@ -106,11 +106,6 @@ class JsonObjectConst : public detail::VariantOperators<JsonObjectConst> {
         detail::ObjectData::getMember(data_, detail::adaptString(key)));
   }
 
-  // Compares objects.
-  FORCE_INLINE bool operator==(JsonObjectConst rhs) const {
-    return detail::ObjectData::equals(data_, rhs.data_);
-  }
-
  private:
   const detail::VariantData* getData() const {
     return collectionToVariant(data_);
@@ -137,4 +132,23 @@ struct Converter<JsonObjectConst> : private detail::VariantAttorney {
   }
 };
 
+inline bool operator==(JsonObjectConst lhs, JsonObjectConst rhs) {
+  if (!lhs && !rhs)  // both are null
+    return true;
+
+  if (!lhs || !rhs)  // only one is null
+    return false;
+
+  size_t count = 0;
+  for (auto kvp : lhs) {
+    auto rhsValue = rhs[kvp.key()];
+    if (rhsValue.isUnbound())
+      return false;
+    if (kvp.value() != rhsValue)
+      return false;
+    count++;
+  }
+  return count == rhs.size();
+}
+
 ARDUINOJSON_END_PUBLIC_NAMESPACE

+ 0 - 12
src/ArduinoJson/Object/ObjectData.hpp

@@ -25,18 +25,6 @@ class ObjectData : public CollectionData {
     return dst->copyFrom(*src, resources);
   }
 
-  bool equals(const ObjectData& other) const;
-
-  static bool equals(const ObjectData* lhs, const ObjectData* rhs) {
-    if (lhs == rhs)
-      return true;
-
-    if (!lhs || !rhs)
-      return false;
-
-    return lhs->equals(*rhs);
-  }
-
   template <typename TAdaptedString>
   VariantData* getOrAddMember(TAdaptedString key, ResourceManager* resources);
 

+ 0 - 14
src/ArduinoJson/Object/ObjectImpl.hpp

@@ -57,20 +57,6 @@ inline bool ObjectData::copyFrom(const ObjectData& src,
   return true;
 }
 
-inline bool ObjectData::equals(const ObjectData& other) const {
-  size_t count = 0;
-  for (auto it = begin(); it; ++it) {
-    auto a = it.data();
-    auto b = other.getMember(adaptString(it.key()));
-    if (!b)
-      return false;
-    if (compare(a, b) != COMPARE_RESULT_EQUAL)
-      return false;
-    count++;
-  }
-  return count == other.size();
-}
-
 template <typename TAdaptedString>
 inline VariantData* ObjectData::getMember(TAdaptedString key) const {
   return findKey(key).data();

+ 1 - 1
src/ArduinoJson/Variant/VariantCompare.hpp

@@ -97,7 +97,7 @@ struct ObjectComparer : ComparerBase {
   explicit ObjectComparer(const ObjectData& rhs) : rhs_(&rhs) {}
 
   CompareResult visitObject(const ObjectData& lhs) {
-    if (rhs_->equals(lhs))
+    if (JsonObjectConst(&lhs) == JsonObjectConst(rhs_))
       return COMPARE_RESULT_EQUAL;
     else
       return COMPARE_RESULT_DIFFER;