Pārlūkot izejas kodu

resource: added more comparison operators for resource handles

Michele Caini 3 gadi atpakaļ
vecāks
revīzija
7b87d17d22
3 mainītis faili ar 71 papildinājumiem un 4 dzēšanām
  1. 1 2
      TODO
  2. 56 2
      src/entt/resource/resource.hpp
  3. 14 0
      test/entt/resource/resource.cpp

+ 1 - 2
TODO

@@ -8,8 +8,6 @@ EXAMPLES
 
 WIP:
 * view/group: no storage_traits dependency -> use storage instead of components for the definition
-* resource<T>::operator</<=/>/>=
-* simplify emitter (see uvw), runtime events
 * basic_storage::bind for cross-registry setups
 * uses-allocator construction: any (with allocator support), poly, ...
 * process scheduler: reviews, use free lists internally
@@ -17,6 +15,7 @@ WIP:
 * dedicated entity storage, in-place O(1) release/destroy for non-orphaned entities, out-of-sync model
 * entity-only and exclude-only views
 * custom allocators all over
+* remove storage::patch
 * use ENTT_NOEXCEPT_IF as appropriate (ie make compressed_pair conditionally noexcept)
 
 WIP:

+ 56 - 2
src/entt/resource/resource.hpp

@@ -178,11 +178,65 @@ template<typename Res, typename Other>
  * @param rhs A valid handle.
  * @return False if both handles refer to the same registry, true otherwise.
  */
-template<typename ILhs, typename IRhs>
-[[nodiscard]] bool operator!=(const resource<ILhs> &lhs, const resource<IRhs> &rhs) ENTT_NOEXCEPT {
+template<typename Res, typename Other>
+[[nodiscard]] bool operator!=(const resource<Res> &lhs, const resource<Other> &rhs) ENTT_NOEXCEPT {
     return !(lhs == rhs);
 }
 
+/**
+ * @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 the first handle is less than the second, false otherwise.
+ */
+template<typename Res, typename Other>
+[[nodiscard]] bool operator<(const resource<Res> &lhs, const resource<Other> &rhs) ENTT_NOEXCEPT {
+    return (std::addressof(*lhs) < std::addressof(*rhs));
+}
+
+/**
+ * @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 the first handle is greater than the second, false otherwise.
+ */
+template<typename Res, typename Other>
+[[nodiscard]] bool operator>(const resource<Res> &lhs, const resource<Other> &rhs) ENTT_NOEXCEPT {
+    return (std::addressof(*lhs) > std::addressof(*rhs));
+}
+
+/**
+ * @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 the first handle is less than or equal to the second, false
+ * otherwise.
+ */
+template<typename Res, typename Other>
+[[nodiscard]] bool operator<=(const resource<Res> &lhs, const resource<Other> &rhs) ENTT_NOEXCEPT {
+    return !(lhs > rhs);
+}
+
+/**
+ * @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 the first handle is greater than or equal to the second,
+ * false otherwise.
+ */
+template<typename Res, typename Other>
+[[nodiscard]] bool operator>=(const resource<Res> &lhs, const resource<Other> &rhs) ENTT_NOEXCEPT {
+    return !(lhs < rhs);
+}
+
 } // namespace entt
 
 #endif

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

@@ -128,3 +128,17 @@ TEST(Resource, DynamicResourceHandleCast) {
     ASSERT_FALSE(cast);
     ASSERT_EQ(resource.use_count(), 1u);
 }
+
+TEST(Resource, Comparison) {
+    entt::resource<derived> resource{std::make_shared<derived>()};
+    entt::resource<const base> other = resource;
+
+    ASSERT_TRUE(resource == other);
+    ASSERT_FALSE(resource != other);
+
+    ASSERT_FALSE(resource < other);
+    ASSERT_FALSE(resource > other);
+
+    ASSERT_TRUE(resource <= other);
+    ASSERT_TRUE(resource >= other);
+}