Browse Source

resource: strict check on resource type for cache

Michele Caini 4 years ago
parent
commit
389cb85410
1 changed files with 12 additions and 9 deletions
  1. 12 9
      src/entt/resource/cache.hpp

+ 12 - 9
src/entt/resource/cache.hpp

@@ -23,7 +23,10 @@ namespace entt {
  * @tparam Resource Type of resources managed by a cache.
  */
 template<typename Resource>
-struct resource_cache {
+class resource_cache {
+    static_assert(std::is_same_v<Resource, std::remove_const_t<std::remove_reference_t<Resource>>>, "Invalid resource type");
+
+public:
     /*! @brief Unsigned integer type. */
     using size_type = std::size_t;
     /*! @brief Type of resources managed by a cache. */
@@ -87,7 +90,7 @@ struct resource_cache {
      * @return A handle for the given resource.
      */
     template<typename Loader, typename... Args>
-    resource_handle<Resource> load(const id_type id, Args &&...args) {
+    resource_handle<resource_type> load(const id_type id, Args &&...args) {
         if(auto it = resources.find(id); it == resources.cend()) {
             if(auto handle = temp<Loader>(std::forward<Args>(args)...); handle) {
                 return (resources[id] = std::move(handle));
@@ -123,7 +126,7 @@ struct resource_cache {
      * @return A handle for the given resource.
      */
     template<typename Loader, typename... Args>
-    resource_handle<Resource> reload(const id_type id, Args &&...args) {
+    resource_handle<resource_type> reload(const id_type id, Args &&...args) {
         return (discard(id), load<Loader>(id, std::forward<Args>(args)...));
     }
 
@@ -140,7 +143,7 @@ struct resource_cache {
      * @return A handle for the given resource.
      */
     template<typename Loader, typename... Args>
-    [[nodiscard]] resource_handle<Resource> temp(Args &&...args) const {
+    [[nodiscard]] resource_handle<resource_type> temp(Args &&...args) const {
         return Loader{}.get(std::forward<Args>(args)...);
     }
 
@@ -157,7 +160,7 @@ struct resource_cache {
      * @param id Unique resource identifier.
      * @return A handle for the given resource.
      */
-    [[nodiscard]] resource_handle<Resource> handle(const id_type id) const {
+    [[nodiscard]] resource_handle<resource_type> handle(const id_type id) const {
         if(auto it = resources.find(id); it != resources.cend()) {
             return it->second;
         }
@@ -198,8 +201,8 @@ struct resource_cache {
      *
      * @code{.cpp}
      * void(const entt::id_type);
-     * void(entt::resource_handle<Resource>);
-     * void(const entt::id_type, entt::resource_handle<Resource>);
+     * void(entt::resource_handle<resource_type>);
+     * void(const entt::id_type, entt::resource_handle<resource_type>);
      * @endcode
      *
      * @tparam Func Type of the function object to invoke.
@@ -215,7 +218,7 @@ struct resource_cache {
 
             if constexpr(std::is_invocable_v<Func, id_type>) {
                 func(curr->first);
-            } else if constexpr(std::is_invocable_v<Func, resource_handle<Resource>>) {
+            } else if constexpr(std::is_invocable_v<Func, resource_handle<resource_type>>) {
                 func(curr->second);
             } else {
                 func(curr->first, curr->second);
@@ -224,7 +227,7 @@ struct resource_cache {
     }
 
 private:
-    std::unordered_map<id_type, resource_handle<Resource>> resources;
+    std::unordered_map<id_type, resource_handle<resource_type>> resources;
 };
 
 } // namespace entt