Browse Source

type_info: operator<, operator<=, operator> and operator>=

Michele Caini 4 years ago
parent
commit
c9c5214b3e
2 changed files with 60 additions and 0 deletions
  1. 43 0
      src/entt/core/type_info.hpp
  2. 17 0
      test/entt/core/type_info.cpp

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

@@ -230,6 +230,49 @@ private:
     return !(lhs == rhs);
 }
 
+/**
+ * @brief Compares two type info objects.
+ * @param lhs A valid type info object.
+ * @param rhs A valid type info object.
+ * @return True if the first element is less than the second, false otherwise.
+ */
+[[nodiscard]] constexpr bool operator<(const type_info &lhs, const type_info &rhs) ENTT_NOEXCEPT {
+    return lhs.index() < rhs.index();
+}
+
+/**
+ * @brief Compares two type info objects.
+ * @param lhs A valid type info object.
+ * @param rhs A valid type info object.
+ * @return True if the first element is less than or equal to the second, false
+ * otherwise.
+ */
+[[nodiscard]] constexpr bool operator<=(const type_info &lhs, const type_info &rhs) ENTT_NOEXCEPT {
+    return !(rhs < lhs);
+}
+
+/**
+ * @brief Compares two type info objects.
+ * @param lhs A valid type info object.
+ * @param rhs A valid type info object.
+ * @return True if the first element is greater than the second, false
+ * otherwise.
+ */
+[[nodiscard]] constexpr bool operator>(const type_info &lhs, const type_info &rhs) ENTT_NOEXCEPT {
+    return rhs < lhs;
+}
+
+/**
+ * @brief Compares two type info objects.
+ * @param lhs A valid type info object.
+ * @param rhs A valid type info object.
+ * @return True if the first element is greater than or equal to the second,
+ * false otherwise.
+ */
+[[nodiscard]] constexpr bool operator>=(const type_info &lhs, const type_info &rhs) ENTT_NOEXCEPT {
+    return !(lhs < rhs);
+}
+
 /**
  * @brief Returns the type info object associated to a given type.
  *

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

@@ -85,3 +85,20 @@ TEST(TypeInfo, Functionalities) {
     ASSERT_TRUE(empty);
     ASSERT_EQ(empty.hash(), info.hash());
 }
+
+TEST(TypeInfo, Order) {
+    entt::type_info lhs = entt::type_id<char>();
+    entt::type_info rhs = entt::type_id<int>();
+
+    // let's adjust the two objects since values are generated at runtime
+    (rhs < lhs) || (std::swap(lhs, rhs), true);
+
+    ASSERT_FALSE(lhs < lhs);
+    ASSERT_FALSE(rhs < rhs);
+
+    ASSERT_LT(rhs, lhs);
+    ASSERT_LE(rhs, lhs);
+
+    ASSERT_GT(lhs, rhs);
+    ASSERT_GE(lhs, rhs);
+}