1 #ifndef ENTT_RESOURCE_CACHE_HPP 2 #define ENTT_RESOURCE_CACHE_HPP 8 #include <unordered_map> 9 #include "../config/config.h" 10 #include "../core/hashed_string.hpp" 28 template<
typename Resource>
30 using container_type = std::unordered_map<HashedString::hash_type, std::shared_ptr<Resource>>;
34 using size_type =
typename container_type::size_type;
56 return resources.size();
63 bool empty() const ENTT_NOEXCEPT {
64 return resources.empty();
95 template<
typename Loader,
typename... Args>
101 if(resources.find(
id) == resources.cend()) {
102 std::shared_ptr<Resource> resource = Loader{}.get(std::forward<Args>(args)...);
103 loaded = (
static_cast<bool>(resource) ? (resources[
id] = std::move(resource), loaded) :
false);
128 template<
typename Loader,
typename... Args>
130 return (
discard(
id), load<Loader>(
id, std::forward<Args>(args)...));
145 template<
typename Loader,
typename... Args>
147 return { Loader{}.get(std::forward<Args>(args)...) };
164 auto it = resources.find(
id);
165 return { it == resources.end() ? nullptr : it->second };
174 return (resources.find(
id) != resources.cend());
186 auto it = resources.find(
id);
188 if(it != resources.end()) {
194 container_type resources;
201 #endif // ENTT_RESOURCE_CACHE_HPP typename container_type::size_type size_type
Unsigned integer type.
void discard(const resource_type id) ENTT_NOEXCEPT
Discards the resource that corresponds to a given identifier.
bool contains(const resource_type id) const ENTT_NOEXCEPT
Checks if a cache contains a given identifier.
bool reload(const resource_type id, Args &&... args)
Reloads a resource or loads it for the first time if not present.
ResourceHandle< Resource > temp(Args &&... args) const
Creates a temporary handle for a resource.
ResourceCache()=default
Default constructor.
Zero overhead resource identifier.
size_type size() const ENTT_NOEXCEPT
Number of resources managed by a cache.
bool empty() const ENTT_NOEXCEPT
Returns true if a cache contains no resources, false otherwise.
Base class for resource loaders.
bool load(const resource_type id, Args &&... args)
Loads the resource that corresponds to a given identifier.
ResourceHandle< Resource > handle(const resource_type id) const
Creates a handle for a given resource identifier.
Simple cache for resources of a given type.
void clear() ENTT_NOEXCEPT
Clears a cache and discards all its resources.