1 #ifndef ENTT_RESOURCE_CACHE_HPP
2 #define ENTT_RESOURCE_CACHE_HPP
6 #include <unordered_map>
8 #include "../config/config.h"
9 #include "../core/fwd.hpp"
28 template<
typename Resource>
49 return resources.size();
56 [[nodiscard]]
bool empty() const ENTT_NOEXCEPT {
57 return resources.empty();
92 template<
typename Loader,
typename... Args>
94 if(
auto it = resources.find(
id); it == resources.cend()) {
95 if(
auto handle = temp<Loader>(std::forward<Args>(args)...);
handle) {
96 return (resources[
id] = std::move(
handle));
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 if(
auto it = resources.find(
id); it != resources.cend()) {
177 return (resources.find(
id) != resources.cend());
189 if(
auto it = resources.find(
id); it != resources.end()) {
211 template <
typename Func>
213 auto begin = resources.begin();
214 auto end = resources.end();
216 while(begin != end) {
219 if constexpr(std::is_invocable_v<Func, id_type>) {
224 func(curr->first, curr->second);
230 std::unordered_map<id_type, resource_handle<Resource>> resources;
std::uint32_t id_type
Alias declaration for type identifiers.
Non-owning handle to an entity.
Simple cache for resources of a given type.
void each(Func func) const
Iterates all 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_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()
Clears a cache and discards all its resources.
Resource resource_type
Type of resources managed by a cache.
resource_handle< Resource > handle(const id_type id) const
Creates a handle for a given resource identifier.
bool empty() const
Returns true if a cache contains no resources, false otherwise.
resource_handle< Resource > temp(Args &&... args) const
Creates a temporary handle for a resource.
bool contains(const id_type id) const
Checks if a cache contains a given identifier.
size_type size() const
Number of resources managed by a cache.
resource_cache()=default
Default constructor.
std::size_t size_type
Unsigned integer type.
resource_cache(resource_cache &&)=default
Default move constructor.
resource_handle< Resource > load(const id_type id, Args &&... args)
Loads the resource that corresponds to a given identifier.