resource: added more comparison operators for resource handles

This commit is contained in:
Michele Caini
2022-04-16 11:38:45 +02:00
parent e5172a9240
commit 7b87d17d22
3 changed files with 71 additions and 4 deletions

3
TODO
View File

@@ -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:

View File

@@ -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

View File

@@ -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);
}