1 #ifndef ENTT_RESOURCE_CACHE_HPP 2 #define ENTT_RESOURCE_CACHE_HPP 8 #include <unordered_map> 9 #include "../core/hashed_string.hpp" 27 template<
typename Resource>
29 using container_type = std::unordered_map<HashedString::hash_type, std::shared_ptr<Resource>>;
33 using size_type =
typename container_type::size_type;
55 return resources.size();
63 return resources.empty();
94 template<
typename Loader,
typename... Args>
100 if(resources.find(
id) == resources.cend()) {
101 std::shared_ptr<Resource> resource = Loader{}.get(std::forward<Args>(args)...);
102 loaded = (
static_cast<bool>(resource) ? (resources[
id] = std::move(resource), loaded) :
false);
127 template<
typename Loader,
typename... Args>
129 return (
discard(
id),
load(
id, std::forward<Args>(args)...));
144 template<
typename Loader,
typename... Args>
146 return { Loader{}.get(std::forward<Args>(args)...) };
163 auto it = resources.find(
id);
164 return { it == resources.end() ? nullptr : it->second };
173 return !(resources.find(
id) == resources.cend());
185 auto it = resources.find(
id);
187 if(it != resources.end()) {
193 container_type resources;
200 #endif // ENTT_RESOURCE_CACHE_HPP typename container_type::size_type size_type
Unsigned integer type.
bool contains(resource_type id) const noexcept
Checks if a cache contains a given identifier.
bool load(resource_type id, Args &&... args)
Loads the resource that corresponds to a given identifier.
bool empty() const noexcept
Returns true if a cache contains no resources, false otherwise.
ResourceHandle< Resource > temp(Args &&... args) const
Creates a temporary handle for a resource.
void discard(resource_type id) noexcept
Discards the resource that corresponds to a given identifier.
ResourceCache()=default
Default constructor.
ResourceHandle< Resource > handle(resource_type id) const
Creates a handle for a given resource identifier.
Zero overhead resource identifier.
void reload(resource_type id, Args &&... args)
Reloads a resource or loads it for the first time if not present.
Base class for resource loaders.
size_type size() const noexcept
Number of resources managed by a cache.
void clear() noexcept
Clears a cache and discards all its resources.
Simple cache for resources of a given type.