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)...));
146 auto it = resources.find(
id);
147 return { it == resources.end() ? nullptr : it->second };
156 return !(resources.find(
id) == resources.cend());
168 auto it = resources.find(
id);
170 if(it != resources.end()) {
176 container_type resources;
183 #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 the given identifier.
bool load(resource_type id, Args &&... args)
Loads the resource that corresponds to the given identifier.
bool empty() const noexcept
Returns true if a cache contains no resources, false otherwise.
void discard(resource_type id) noexcept
Discards the resource that corresponds to the given identifier.
ResourceCache()=default
Default constructor.
ResourceHandle< Resource > handle(resource_type id) const
Creates a handle for the 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.