1 #ifndef ENTT_RESOURCE_CACHE_HPP
2 #define ENTT_RESOURCE_CACHE_HPP
6 #include "../config/config.h"
7 #include "../container/dense_hash_map.hpp"
8 #include "../core/fwd.hpp"
9 #include "../core/utility.hpp"
26 template<
typename Resource>
28 static_assert(std::is_same_v<Resource, std::remove_const_t<std::remove_reference_t<Resource>>>,
"Invalid resource type");
50 return resources.size();
57 [[nodiscard]]
bool empty() const ENTT_NOEXCEPT {
58 return resources.empty();
93 template<
typename Loader,
typename... Args>
95 if(
auto it = resources.find(
id); it == resources.cend()) {
96 if(
auto handle = temp<Loader>(std::forward<Args>(args)...);
handle) {
97 return (resources[
id] = std::move(
handle));
129 template<
typename Loader,
typename... Args>
131 return (
discard(
id), load<Loader>(
id, std::forward<Args>(args)...));
146 template<
typename Loader,
typename... Args>
148 return Loader{}.get(std::forward<Args>(args)...);
165 if(
auto it = resources.find(
id); it != resources.cend()) {
174 if(
auto it = resources.find(
id); it != resources.end()) {
187 return (resources.find(
id) != resources.cend());
199 if(
auto it = resources.find(
id); it != resources.end()) {
221 template<
typename Func>
223 auto begin = resources.begin();
224 auto end = resources.end();
226 while(begin != end) {
229 if constexpr(std::is_invocable_v<Func, id_type>) {
256 template<
typename Func>
258 auto begin = resources.begin();
259 auto end = resources.end();
261 while(begin != end) {
264 if constexpr(std::is_invocable_v<Func, id_type>) {
269 func(curr->first, curr->second);
Associative container for key-value pairs with unique keys.
Simple cache for resources of a given type.
void each(Func func) const
Iterates all resources.
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< const resource_type > handle(const id_type id) const
Creates a handle for a given resource identifier.
resource_handle< resource_type > load(const id_type id, Args &&...args)
Loads the resource that corresponds to a given identifier.
resource_handle< resource_type > handle(const id_type id)
Creates a handle for a given resource identifier.
bool empty() const
Returns true if a cache contains no resources, false otherwise.
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.
resource_handle< resource_type > reload(const id_type id, Args &&...args)
Reloads a resource or loads it for the first time if not present.
void each(Func func)
Iterates all resources.
std::size_t size_type
Unsigned integer type.
resource_cache(resource_cache &&)=default
Default move constructor.
resource_handle< resource_type > temp(Args &&...args) const
Creates a temporary handle for a resource.
std::uint32_t id_type
Alias declaration for type identifiers.
Non-owning handle to an entity.
Identity function object (waiting for C++20).