EnTT  3.8.1
cache.hpp
1 #ifndef ENTT_RESOURCE_CACHE_HPP
2 #define ENTT_RESOURCE_CACHE_HPP
3 
4 
5 #include <type_traits>
6 #include <unordered_map>
7 #include <utility>
8 #include "../config/config.h"
9 #include "../core/fwd.hpp"
10 #include "handle.hpp"
11 #include "loader.hpp"
12 #include "fwd.hpp"
13 
14 
15 namespace entt {
16 
17 
28 template<typename Resource>
31  using size_type = std::size_t;
33  using resource_type = Resource;
34 
36  resource_cache() = default;
37 
40 
43 
48  [[nodiscard]] size_type size() const ENTT_NOEXCEPT {
49  return resources.size();
50  }
51 
56  [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
57  return resources.empty();
58  }
59 
66  void clear() ENTT_NOEXCEPT {
67  resources.clear();
68  }
69 
92  template<typename Loader, typename... Args>
93  resource_handle<Resource> load(const id_type id, Args &&... 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));
97  }
98  } else {
99  return it->second;
100  }
101 
102  return {};
103  }
104 
128  template<typename Loader, typename... Args>
129  resource_handle<Resource> reload(const id_type id, Args &&... args) {
130  return (discard(id), load<Loader>(id, std::forward<Args>(args)...));
131  }
132 
145  template<typename Loader, typename... Args>
146  [[nodiscard]] resource_handle<Resource> temp(Args &&... args) const {
147  return Loader{}.get(std::forward<Args>(args)...);
148  }
149 
163  [[nodiscard]] resource_handle<Resource> handle(const id_type id) const {
164  if(auto it = resources.find(id); it != resources.cend()) {
165  return it->second;
166  }
167 
168  return {};
169  }
170 
176  [[nodiscard]] bool contains(const id_type id) const {
177  return (resources.find(id) != resources.cend());
178  }
179 
188  void discard(const id_type id) {
189  if(auto it = resources.find(id); it != resources.end()) {
190  resources.erase(it);
191  }
192  }
193 
211  template <typename Func>
212  void each(Func func) const {
213  auto begin = resources.begin();
214  auto end = resources.end();
215 
216  while(begin != end) {
217  auto curr = begin++;
218 
219  if constexpr(std::is_invocable_v<Func, id_type>) {
220  func(curr->first);
221  } else if constexpr(std::is_invocable_v<Func, resource_handle<Resource>>) {
222  func(curr->second);
223  } else {
224  func(curr->first, curr->second);
225  }
226  }
227  }
228 
229 private:
230  std::unordered_map<id_type, resource_handle<Resource>> resources;
231 };
232 
233 
234 }
235 
236 
237 #endif
Shared resource handle.
Definition: handle.hpp:28
EnTT default namespace.
Definition: algorithm.hpp:13
std::uint32_t id_type
Alias declaration for type identifiers.
Definition: fwd.hpp:17
Non-owning handle to an entity.
Definition: handle.hpp:26
Simple cache for resources of a given type.
Definition: cache.hpp:29
void each(Func func) const
Iterates all resources.
Definition: cache.hpp:212
resource_handle< Resource > reload(const id_type id, Args &&... args)
Reloads a resource or loads it for the first time if not present.
Definition: cache.hpp:129
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.
Definition: cache.hpp:188
void clear()
Clears a cache and discards all its resources.
Definition: cache.hpp:66
Resource resource_type
Type of resources managed by a cache.
Definition: cache.hpp:33
resource_handle< Resource > handle(const id_type id) const
Creates a handle for a given resource identifier.
Definition: cache.hpp:163
bool empty() const
Returns true if a cache contains no resources, false otherwise.
Definition: cache.hpp:56
resource_handle< Resource > temp(Args &&... args) const
Creates a temporary handle for a resource.
Definition: cache.hpp:146
bool contains(const id_type id) const
Checks if a cache contains a given identifier.
Definition: cache.hpp:176
size_type size() const
Number of resources managed by a cache.
Definition: cache.hpp:48
resource_cache()=default
Default constructor.
std::size_t size_type
Unsigned integer type.
Definition: cache.hpp:31
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.
Definition: cache.hpp:93