From c7a3e9d4ac15680b30a74a470f207e07660eface Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Sat, 2 Oct 2021 23:57:44 +0200 Subject: [PATCH] resource: cache const correctness review --- src/entt/resource/cache.hpp | 48 +++++++++++++++++++++++++++++++-- test/entt/resource/resource.cpp | 17 +++++++----- 2 files changed, 56 insertions(+), 9 deletions(-) diff --git a/src/entt/resource/cache.hpp b/src/entt/resource/cache.hpp index 4c0d9c78c..7e6a97b4e 100644 --- a/src/entt/resource/cache.hpp +++ b/src/entt/resource/cache.hpp @@ -160,7 +160,7 @@ public: * @param id Unique resource identifier. * @return A handle for the given resource. */ - [[nodiscard]] resource_handle handle(const id_type id) const { + [[nodiscard]] resource_handle handle(const id_type id) const { if(auto it = resources.find(id); it != resources.cend()) { return it->second; } @@ -168,6 +168,15 @@ public: return {}; } + /*! @copydoc handle */ + [[nodiscard]] resource_handle handle(const id_type id) { + if(auto it = resources.find(id); it != resources.end()) { + return it->second; + } + + return {}; + } + /** * @brief Checks if a cache contains a given identifier. * @param id Unique resource identifier. @@ -201,6 +210,41 @@ public: * * @code{.cpp} * void(const entt::id_type); + * void(entt::resource_handle); + * void(const entt::id_type, entt::resource_handle); + * @endcode + * + * @tparam Func Type of the function object to invoke. + * @param func A valid function object. + */ + template + void each(Func func) const { + auto begin = resources.begin(); + auto end = resources.end(); + + while(begin != end) { + auto curr = begin++; + + if constexpr(std::is_invocable_v) { + func(curr->first); + } else if constexpr(std::is_invocable_v>) { + func(resource_handle{curr->second}); + } else { + func(curr->first, resource_handle{curr->second}); + } + } + } + + /** + * @copybrief each + * + * The function object is invoked for each element. It is provided with + * either the resource identifier, the resource handle or both of them.
+ * The signature of the function must be equivalent to one of the following + * forms: + * + * @code{.cpp} + * void(const entt::id_type); * void(entt::resource_handle); * void(const entt::id_type, entt::resource_handle); * @endcode @@ -209,7 +253,7 @@ public: * @param func A valid function object. */ template - void each(Func func) const { + void each(Func func) { auto begin = resources.begin(); auto end = resources.end(); diff --git a/test/entt/resource/resource.cpp b/test/entt/resource/resource.cpp index 281abd787..f7c87f5b6 100644 --- a/test/entt/resource/resource.cpp +++ b/test/entt/resource/resource.cpp @@ -51,7 +51,7 @@ TEST(Resource, Functionalities) { auto tmp = cache.handle(hs1); - ASSERT_EQ(cache.handle(hs1).use_count(), 3); + ASSERT_EQ(std::as_const(cache).handle(hs1).use_count(), 3); ASSERT_TRUE(static_cast(tmp)); tmp = {}; @@ -63,7 +63,7 @@ TEST(Resource, Functionalities) { ASSERT_FALSE(cache.empty()); ASSERT_TRUE(cache.contains(hs1)); ASSERT_FALSE(cache.contains(hs2)); - ASSERT_EQ((*cache.handle(hs1)).value, 42); + ASSERT_EQ((*std::as_const(cache).handle(hs1)).value, 42); ASSERT_TRUE(cache.load>(hs1, 42)); ASSERT_TRUE(cache.load>(hs2, 42)); @@ -73,7 +73,7 @@ TEST(Resource, Functionalities) { ASSERT_TRUE(cache.contains(hs1)); ASSERT_TRUE(cache.contains(hs2)); ASSERT_EQ((*cache.handle(hs1)).value, 42); - ASSERT_EQ(cache.handle(hs2)->value, 42); + ASSERT_EQ(std::as_const(cache).handle(hs2)->value, 42); ASSERT_NO_FATAL_FAILURE(cache.discard(hs1)); @@ -93,10 +93,10 @@ TEST(Resource, Functionalities) { ASSERT_NE(cache.size(), 0u); ASSERT_FALSE(cache.empty()); - ASSERT_TRUE(cache.handle(hs1)); + ASSERT_TRUE(std::as_const(cache).handle(hs1)); ASSERT_FALSE(cache.handle(hs2)); - ASSERT_TRUE(cache.handle(hs1)); + ASSERT_TRUE(std::as_const(cache).handle(hs1)); ASSERT_EQ(&cache.handle(hs1).get(), &static_cast(cache.handle(hs1))); ASSERT_NO_FATAL_FAILURE(cache.clear()); @@ -201,13 +201,16 @@ TEST(Resource, Each) { ASSERT_FALSE(cache.empty()); ASSERT_EQ(cache.handle("resource"_hs)->value, 1); - cache.each([](auto id, auto res) { + cache.each([](entt::id_type id, entt::resource_handle res) { ASSERT_EQ(id, "resource"_hs); ++res->value; }); ASSERT_FALSE(cache.empty()); - ASSERT_EQ(cache.handle("resource"_hs)->value, 2); + + std::as_const(cache).each([](entt::id_type id, entt::resource_handle res) { + ASSERT_EQ(res->value, 2); + }); cache.each([&cache](entt::id_type id) { cache.discard(id);