Browse Source

type_info: comparison operators

Michele Caini 5 years ago
parent
commit
f7e80879ae
2 changed files with 26 additions and 0 deletions
  1. 20 0
      src/entt/core/type_info.hpp
  2. 6 0
      test/entt/core/type_info.cpp

+ 20 - 0
src/entt/core/type_info.hpp

@@ -188,6 +188,15 @@ public:
         return name_func();
     }
 
+    /**
+    * @brief Compares the contents of two type info objects.
+    * @param other Object with which to compare.
+    * @return False if the two contents differ, true otherwise.
+    */
+    [[nodiscard]] bool operator==(const type_info &other) const ENTT_NOEXCEPT {
+        return hash_func == other.hash_func;
+    }
+
 private:
     seq_fn *seq_func;
     hash_fn *hash_func;
@@ -195,6 +204,17 @@ private:
 };
 
 
+/**
+* @brief Compares the contents of two type info objects.
+* @param lhs A type info object.
+* @param rhs A type info object.
+* @return True if the two contents differ, false otherwise.
+*/
+[[nodiscard]] inline bool operator!=(const type_info &lhs, const type_info &rhs) ENTT_NOEXCEPT {
+    return !(lhs == rhs);
+}
+
+
 /**
  * @brief Returns the type info object for a given type.
  * @tparam Type Type for which to generate a type info object.

+ 6 - 0
test/entt/core/type_info.cpp

@@ -40,6 +40,12 @@ TEST(TypeInfo, Functionalities) {
     static_assert(std::is_copy_assignable_v<decltype(info)>);
     static_assert(std::is_move_assignable_v<decltype(info)>);
 
+    ASSERT_EQ(info, other);
+    ASSERT_NE(info, empty);
+
+    ASSERT_TRUE(info == info);
+    ASSERT_FALSE(info != other);
+
     ASSERT_EQ(info.seq(), other.seq());
     ASSERT_EQ(info.hash(), other.hash());
     ASSERT_EQ(info.name(), other.name());