1 #ifndef ENTT_RESOURCE_CACHE_HPP
2 #define ENTT_RESOURCE_CACHE_HPP
8 #include <unordered_map>
9 #include "../config/config.h"
28 template<
typename Resource>
51 return resources.size();
58 bool empty() const ENTT_NOEXCEPT {
59 return resources.empty();
94 template<
typename Loader,
typename... Args>
99 if(
auto it = resources.find(
id); it == resources.cend()) {
100 if(
auto instance = Loader{}.get(std::forward<Args>(args)...); instance) {
101 resources[id] = instance;
102 resource = std::move(instance);
105 resource = it->second;
134 template<
typename Loader,
typename... Args>
136 return (
discard(
id), load<Loader>(
id, std::forward<Args>(args)...));
151 template<
typename Loader,
typename... Args>
153 return { Loader{}.get(std::forward<Args>(args)...) };
170 auto it = resources.find(
id);
171 return { it == resources.end() ? nullptr : it->second };
180 return (resources.find(
id) != resources.cend());
192 if(
auto it = resources.find(
id); it != resources.end()) {
214 template <
typename Func>
216 auto begin = resources.begin();
217 auto end = resources.end();
219 while(begin != end) {
222 if constexpr(std::is_invocable_v<Func, id_type>) {
233 std::unordered_map<id_type, std::shared_ptr<Resource>> resources;
240 #endif // ENTT_RESOURCE_CACHE_HPP
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.
Resource resource_type
Type of resources managed by a cache.
entt::handle< Resource > temp(Args &&... args) const
Creates a temporary handle for a resource.
void clear() ENTT_NOEXCEPT
Clears a cache and discards all its resources.
bool contains(const id_type id) const ENTT_NOEXCEPT
Checks if a cache contains a given identifier.
Simple cache for resources of a given type.
cache & operator=(cache &&)=default
Default move assignment operator.
entt::handle< Resource > handle(const id_type id) const
Creates a handle for a given resource identifier.
std::size_t size_type
Unsigned integer type.
cache()=default
Default constructor.
void discard(const id_type id) ENTT_NOEXCEPT
Discards the resource that corresponds to a given identifier.
entt::handle< Resource > load(const id_type id, Args &&... args)
Loads the resource that corresponds to a given identifier.
Base class for resource loaders.
ENTT_ID_TYPE id_type
Unique identifier type for resources.
entt::handle< Resource > reload(const id_type id, Args &&... args)
Reloads a resource or loads it for the first time if not present.
void each(Func func) const
Iterates all resources.