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

resource: add operator==/!= to resource handles

Michele Caini 4 лет назад
Родитель
Сommit
c17ecbc78c
3 измененных файлов с 20 добавлено и 1 удалено
  1. 0 1
      TODO
  2. 18 0
      src/entt/resource/handle.hpp
  3. 2 0
      test/entt/resource/resource.cpp

+ 0 - 1
TODO

@@ -3,7 +3,6 @@
 * add examples (and credits) from @alanjfs :)
 
 WIP:
-* resource handle: add comparison operators
 * get rid of storage_traits class template
 * uses-allocator construction: any (with allocator support), cache, poly, ...
 * add an ENTT_NOEXCEPT with args and use it to make ie compressed_pair conditionally noexcept

+ 18 - 0
src/entt/resource/handle.hpp

@@ -182,6 +182,24 @@ private:
     std::shared_ptr<value_type> resource;
 };
 
+/**
+ * @brief Compares two handles.
+ * @tparam Res Type of resource managed by the first handle.
+ * @tparam Other Type of resource managed by the second handle.
+ * @param lhs A valid handle.
+ * @param rhs A valid handle.
+ * @return True if both handles refer to the same resource, false otherwise.
+ */
+template<typename Res, typename Other>
+[[nodiscard]] bool operator==(const resource_handle<Res> &lhs, const resource_handle<Other> &rhs) ENTT_NOEXCEPT {
+    return lhs.operator->() == rhs.operator->();
+}
+
+template<typename ILhs, typename IRhs>
+[[nodiscard]] bool operator!=(const resource_handle<ILhs> &lhs, const resource_handle<IRhs> &rhs) ENTT_NOEXCEPT {
+    return !(lhs == rhs);
+}
+
 } // namespace entt
 
 #endif

+ 2 - 0
test/entt/resource/resource.cpp

@@ -151,6 +151,8 @@ TEST(Resource, ConstNonConstHandle) {
     static_assert(std::is_same_v<decltype(std::as_const(handle).get()), resource &>);
 
     ASSERT_TRUE(chandle);
+    ASSERT_EQ(handle, chandle);
+    ASSERT_NE(handle, entt::resource_handle<resource>{});
     ASSERT_EQ(handle.use_count(), 2u);
     ASSERT_EQ(chandle->value, 42);