Browse Source

resource: operator==/operator<=>

skypjack 2 months ago
parent
commit
3066d0ed5c
1 changed files with 23 additions and 67 deletions
  1. 23 67
      src/entt/resource/resource.hpp

+ 23 - 67
src/entt/resource/resource.hpp

@@ -1,6 +1,7 @@
 #ifndef ENTT_RESOURCE_RESOURCE_HPP
 #define ENTT_RESOURCE_RESOURCE_HPP
 
+#include <compare>
 #include <memory>
 #include <type_traits>
 #include <utility>
@@ -158,6 +159,28 @@ public:
         return static_cast<bool>(value);
     }
 
+    /**
+     * @brief Compares two handles.
+     * @tparam Other Type of resource managed by the other handle.
+     * @param other A valid handle.
+     * @return True if both handles refer to the same resource, false otherwise.
+     */
+    template<typename Other>
+    [[nodiscard]] bool operator==(const resource<Other> &other) const noexcept {
+        return (std::addressof(*value) == std::addressof(*other.value));
+    }
+
+    /**
+     * @brief Lexicographically compares two handles.
+     * @tparam Other Type of resource managed by the other handle.
+     * @param other A valid handle.
+     * @return The relative order between the two handles.
+     */
+    template<typename Other>
+    [[nodiscard]] auto operator<=>(const resource<Other> &other) const noexcept {
+        return (std::addressof(*value) <=> std::addressof(*other.value));
+    }
+
     /*! @brief Releases the ownership of the managed resource. */
     void reset() {
         value.reset();
@@ -183,73 +206,6 @@ private:
     handle_type value;
 };
 
-/**
- * @brief Compares two handles.
- * @tparam Lhs Type of resource managed by the first handle.
- * @tparam Rhs 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 Lhs, typename Rhs>
-[[nodiscard]] bool operator==(const resource<Lhs> &lhs, const resource<Rhs> &rhs) noexcept {
-    return (std::addressof(*lhs) == std::addressof(*rhs));
-}
-
-/**
- * @brief Compares two handles.
- * @tparam Lhs Type of resource managed by the first handle.
- * @tparam Rhs 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 Lhs, typename Rhs>
-[[nodiscard]] bool operator<(const resource<Lhs> &lhs, const resource<Rhs> &rhs) noexcept {
-    return (std::addressof(*lhs) < std::addressof(*rhs));
-}
-
-/**
- * @brief Compares two handles.
- * @tparam Lhs Type of resource managed by the first handle.
- * @tparam Rhs 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 Lhs, typename Rhs>
-[[nodiscard]] bool operator>(const resource<Lhs> &lhs, const resource<Rhs> &rhs) noexcept {
-    return rhs < lhs;
-}
-
-/**
- * @brief Compares two handles.
- * @tparam Lhs Type of resource managed by the first handle.
- * @tparam Rhs 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 Lhs, typename Rhs>
-[[nodiscard]] bool operator<=(const resource<Lhs> &lhs, const resource<Rhs> &rhs) noexcept {
-    return !(lhs > rhs);
-}
-
-/**
- * @brief Compares two handles.
- * @tparam Lhs Type of resource managed by the first handle.
- * @tparam Rhs 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 Lhs, typename Rhs>
-[[nodiscard]] bool operator>=(const resource<Lhs> &lhs, const resource<Rhs> &rhs) noexcept {
-    return !(lhs < rhs);
-}
-
 } // namespace entt
 
 #endif