1
0
Эх сурвалжийг харах

handle: support comparison with null_t - close #1130

Michele Caini 2 жил өмнө
parent
commit
294792060c

+ 48 - 1
src/entt/entity/handle.hpp

@@ -145,7 +145,6 @@ struct basic_handle {
     operator basic_handle<Other, Args...>() const noexcept {
         static_assert(std::is_same_v<Other, Registry> || std::is_same_v<std::remove_const_t<Other>, Registry>, "Invalid conversion between different handles");
         static_assert((sizeof...(Scope) == 0 || ((sizeof...(Args) != 0 && sizeof...(Args) <= sizeof...(Scope)) && ... && (type_list_contains_v<type_list<Scope...>, Args>))), "Invalid conversion between different handles");
-
         return reg ? basic_handle<Other, Args...>{*reg, entt} : basic_handle<Other, Args...>{};
     }
 
@@ -373,6 +372,54 @@ template<typename... Args, typename... Other>
     return !(lhs == rhs);
 }
 
+/**
+ * @brief Compares a handle with the null object.
+ * @tparam Args Scope of the handle.
+ * @param lhs A valid handle.
+ * @param rhs A null object yet to be converted.
+ * @return False if the two elements differ, true otherwise.
+ */
+template<typename... Args>
+[[nodiscard]] constexpr bool operator==(const basic_handle<Args...> &lhs, const null_t rhs) noexcept {
+    return (lhs.entity() == rhs);
+}
+
+/**
+ * @brief Compares a handle with the null object.
+ * @tparam Args Scope of the handle.
+ * @param lhs A null object yet to be converted.
+ * @param rhs A valid handle.
+ * @return False if the two elements differ, true otherwise.
+ */
+template<typename... Args>
+[[nodiscard]] constexpr bool operator==(const null_t lhs, const basic_handle<Args...> &rhs) noexcept {
+    return (rhs == lhs);
+}
+
+/**
+ * @brief Compares a handle with the null object.
+ * @tparam Args Scope of the handle.
+ * @param lhs A valid handle.
+ * @param rhs A null object yet to be converted.
+ * @return True if the two elements differ, false otherwise.
+ */
+template<typename... Args>
+[[nodiscard]] constexpr bool operator!=(const basic_handle<Args...> &lhs, const null_t rhs) noexcept {
+    return (lhs.entity() != rhs);
+}
+
+/**
+ * @brief Compares a handle with the null object.
+ * @tparam Args Scope of the handle.
+ * @param lhs A null object yet to be converted.
+ * @param rhs A valid handle.
+ * @return True if the two elements differ, false otherwise.
+ */
+template<typename... Args>
+[[nodiscard]] constexpr bool operator!=(const null_t lhs, const basic_handle<Args...> &rhs) noexcept {
+    return (rhs != lhs);
+}
+
 } // namespace entt
 
 #endif

+ 33 - 0
test/entt/entity/handle.cpp

@@ -299,3 +299,36 @@ TYPED_TEST(BasicHandle, HandleStorageIterator) {
     ASSERT_EQ(begin++, iterable.begin());
     ASSERT_EQ(++begin, iterable.end());
 }
+
+TYPED_TEST(BasicHandle, Null) {
+    using handle_type = typename TestFixture::type;
+
+    handle_type handle{};
+
+    ASSERT_TRUE(handle == entt::null);
+    ASSERT_TRUE(entt::null == handle);
+
+    ASSERT_FALSE(handle != entt::null);
+    ASSERT_FALSE(entt::null != handle);
+
+    entt::registry registry;
+    const auto entity = registry.create();
+
+    handle = handle_type{registry, entity};
+
+    ASSERT_FALSE(handle == entt::null);
+    ASSERT_FALSE(entt::null == handle);
+
+    ASSERT_TRUE(handle != entt::null);
+    ASSERT_TRUE(entt::null != handle);
+
+    if constexpr(!std::is_const_v<typename handle_type::registry_type>) {
+        handle.destroy();
+
+        ASSERT_TRUE(handle == entt::null);
+        ASSERT_TRUE(entt::null == handle);
+
+        ASSERT_FALSE(handle != entt::null);
+        ASSERT_FALSE(entt::null != handle);
+    }
+}