1 #ifndef ENTT_RESOURCE_CACHE_HPP
2 #define ENTT_RESOURCE_CACHE_HPP
7 #include <unordered_map>
9 #include "../config/config.h"
10 #include "../core/fwd.hpp"
29 template<
typename Resource>
50 return resources.size();
57 [[nodiscard]]
bool empty() const ENTT_NOEXCEPT {
58 return resources.empty();
93 template<
typename Loader,
typename... Args>
98 if(
auto it = resources.find(
id); it == resources.cend()) {
99 if(
auto instance = Loader{}.get(std::forward<Args>(args)...); instance) {
100 resources[id] = instance;
101 resource = std::move(instance);
104 resource = it->second;
133 template<
typename Loader,
typename... Args>
135 return (
discard(
id), load<Loader>(
id, std::forward<Args>(args)...));
150 template<
typename Loader,
typename... Args>
152 return { Loader{}.get(std::forward<Args>(args)...) };
169 auto it = resources.find(
id);
170 return { it == resources.end() ? nullptr : it->second };
179 return (resources.find(
id) != resources.cend());
191 if(
auto it = resources.find(
id); it != resources.end()) {
213 template <
typename Func>
215 auto begin = resources.begin();
216 auto end = resources.end();
218 while(begin != end) {
221 if constexpr(std::is_invocable_v<Func, id_type>) {
232 std::unordered_map<id_type, std::shared_ptr<Resource>> resources;
Base class for resource loaders.
bool contains(const id_type id) const
Checks if a cache contains a given identifier.
std::uint32_t id_type
Alias declaration for type identifiers.
void each(Func func) const
Iterates all resources.
resource_cache(resource_cache &&)=default
Default move constructor.
resource_cache()=default
Default constructor.
bool empty() const noexcept
Returns true if a cache contains no resources, false otherwise.
resource_handle< Resource > temp(Args &&... args) const
Creates a temporary handle for a resource.
resource_handle< Resource > handle(const id_type id) const
Creates a handle for a given resource identifier.
Simple cache for resources of a given type.
resource_cache & operator=(resource_cache &&)=default
Default move assignment operator.
void discard(const id_type id)
Discards the resource that corresponds to a given identifier.
void clear() noexcept
Clears a cache and discards all its resources.
resource_handle< Resource > reload(const id_type id, Args &&... args)
Reloads a resource or loads it for the first time if not present.
resource_handle< Resource > load(const id_type id, Args &&... args)
Loads the resource that corresponds to a given identifier.
Resource resource_type
Type of resources managed by a cache.
std::size_t size_type
Unsigned integer type.
size_type size() const noexcept
Number of resources managed by a cache.