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

This commit is contained in:
Michele Caini
2022-03-16 16:50:26 +01:00
parent 0feb9f0c56
commit c17ecbc78c
3 changed files with 20 additions and 1 deletions

1
TODO
View File

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

View File

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

View File

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