resource: handle as value type, const correctness review

This commit is contained in:
Michele Caini
2021-10-02 23:52:22 +02:00
parent 389cb85410
commit a05e7a84c4
2 changed files with 24 additions and 25 deletions

View File

@@ -121,33 +121,17 @@ public:
*
* @return A reference to the managed resource.
*/
[[nodiscard]] const resource_type &get() const ENTT_NOEXCEPT {
ENTT_ASSERT(static_cast<bool>(resource), "Invalid resource");
[[nodiscard]] resource_type &get() const ENTT_NOEXCEPT {
return *resource;
}
/*! @copydoc get */
[[nodiscard]] resource_type &get() ENTT_NOEXCEPT {
return const_cast<resource_type &>(std::as_const(*this).get());
}
/*! @copydoc get */
[[nodiscard]] operator const resource_type &() const ENTT_NOEXCEPT {
[[nodiscard]] operator resource_type &() const ENTT_NOEXCEPT {
return get();
}
/*! @copydoc get */
[[nodiscard]] operator resource_type &() ENTT_NOEXCEPT {
return get();
}
/*! @copydoc get */
[[nodiscard]] const resource_type &operator*() const ENTT_NOEXCEPT {
return get();
}
/*! @copydoc get */
[[nodiscard]] resource_type &operator*() ENTT_NOEXCEPT {
[[nodiscard]] resource_type &operator*() const ENTT_NOEXCEPT {
return get();
}
@@ -160,12 +144,7 @@ public:
* @return A pointer to the managed resource or `nullptr` if the handle
* contains no resource at all.
*/
[[nodiscard]] const resource_type *operator->() const ENTT_NOEXCEPT {
return resource.get();
}
/*! @copydoc operator-> */
[[nodiscard]] resource_type *operator->() ENTT_NOEXCEPT {
[[nodiscard]] resource_type *operator->() const ENTT_NOEXCEPT {
return resource.get();
}

View File

@@ -113,6 +113,26 @@ TEST(Resource, Functionalities) {
ASSERT_TRUE(std::is_move_assignable_v<entt::resource_handle<resource>>);
}
TEST(Resource, ConstNonConstHandle) {
entt::resource_cache<resource> cache;
entt::resource_handle<resource> handle = cache.temp<loader<resource>>(42);
entt::resource_handle<const resource> chandle = handle;
static_assert(std::is_same_v<decltype(handle.get()), resource &>);
static_assert(std::is_same_v<decltype(chandle.get()), const resource &>);
static_assert(std::is_same_v<decltype(std::as_const(handle).get()), resource &>);
ASSERT_TRUE(chandle);
ASSERT_EQ(handle.use_count(), 2u);
ASSERT_EQ(chandle->value, 42);
chandle = {};
ASSERT_FALSE(chandle);
ASSERT_EQ(handle.use_count(), 1u);
}
TEST(Resource, MutableHandle) {
entt::resource_cache<resource> cache;