EnTT  3.9.0
cache.hpp
1 #ifndef ENTT_RESOURCE_CACHE_HPP
2 #define ENTT_RESOURCE_CACHE_HPP
3 
4 #include <type_traits>
5 #include <utility>
6 #include "../config/config.h"
7 #include "../container/dense_hash_map.hpp"
8 #include "../core/fwd.hpp"
9 #include "../core/utility.hpp"
10 #include "fwd.hpp"
11 #include "handle.hpp"
12 #include "loader.hpp"
13 
14 namespace entt {
15 
26 template<typename Resource>
28  static_assert(std::is_same_v<Resource, std::remove_const_t<std::remove_reference_t<Resource>>>, "Invalid resource type");
29 
30 public:
32  using size_type = std::size_t;
34  using resource_type = Resource;
35 
37  resource_cache() = default;
38 
41 
44 
49  [[nodiscard]] size_type size() const ENTT_NOEXCEPT {
50  return resources.size();
51  }
52 
57  [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
58  return resources.empty();
59  }
60 
67  void clear() ENTT_NOEXCEPT {
68  resources.clear();
69  }
70 
93  template<typename Loader, typename... Args>
94  resource_handle<resource_type> load(const id_type id, Args &&...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));
98  }
99  } else {
100  return it->second;
101  }
102 
103  return {};
104  }
105 
129  template<typename Loader, typename... Args>
130  resource_handle<resource_type> reload(const id_type id, Args &&...args) {
131  return (discard(id), load<Loader>(id, std::forward<Args>(args)...));
132  }
133 
146  template<typename Loader, typename... Args>
147  [[nodiscard]] resource_handle<resource_type> temp(Args &&...args) const {
148  return Loader{}.get(std::forward<Args>(args)...);
149  }
150 
164  [[nodiscard]] resource_handle<const resource_type> handle(const id_type id) const {
165  if(auto it = resources.find(id); it != resources.cend()) {
166  return it->second;
167  }
168 
169  return {};
170  }
171 
174  if(auto it = resources.find(id); it != resources.end()) {
175  return it->second;
176  }
177 
178  return {};
179  }
180 
186  [[nodiscard]] bool contains(const id_type id) const {
187  return (resources.find(id) != resources.cend());
188  }
189 
198  void discard(const id_type id) {
199  if(auto it = resources.find(id); it != resources.end()) {
200  resources.erase(it);
201  }
202  }
203 
221  template<typename Func>
222  void each(Func func) const {
223  auto begin = resources.begin();
224  auto end = resources.end();
225 
226  while(begin != end) {
227  auto curr = begin++;
228 
229  if constexpr(std::is_invocable_v<Func, id_type>) {
230  func(curr->first);
231  } else if constexpr(std::is_invocable_v<Func, resource_handle<const resource_type>>) {
232  func(resource_handle<const resource_type>{curr->second});
233  } else {
234  func(curr->first, resource_handle<const resource_type>{curr->second});
235  }
236  }
237  }
238 
256  template<typename Func>
257  void each(Func func) {
258  auto begin = resources.begin();
259  auto end = resources.end();
260 
261  while(begin != end) {
262  auto curr = begin++;
263 
264  if constexpr(std::is_invocable_v<Func, id_type>) {
265  func(curr->first);
266  } else if constexpr(std::is_invocable_v<Func, resource_handle<resource_type>>) {
267  func(curr->second);
268  } else {
269  func(curr->first, curr->second);
270  }
271  }
272  }
273 
274 private:
276 };
277 
278 } // namespace entt
279 
280 #endif
Associative container for key-value pairs with unique keys.
Simple cache for resources of a given type.
Definition: cache.hpp:27
void each(Func func) const
Iterates all resources.
Definition: cache.hpp:222
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:198
void clear()
Clears a cache and discards all its resources.
Definition: cache.hpp:67
Resource resource_type
Type of resources managed by a cache.
Definition: cache.hpp:34
resource_handle< const resource_type > handle(const id_type id) const
Creates a handle for a given resource identifier.
Definition: cache.hpp:164
resource_handle< resource_type > load(const id_type id, Args &&...args)
Loads the resource that corresponds to a given identifier.
Definition: cache.hpp:94
resource_handle< resource_type > handle(const id_type id)
Creates a handle for a given resource identifier.
Definition: cache.hpp:173
bool empty() const
Returns true if a cache contains no resources, false otherwise.
Definition: cache.hpp:57
bool contains(const id_type id) const
Checks if a cache contains a given identifier.
Definition: cache.hpp:186
size_type size() const
Number of resources managed by a cache.
Definition: cache.hpp:49
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.
Definition: cache.hpp:130
void each(Func func)
Iterates all resources.
Definition: cache.hpp:257
std::size_t size_type
Unsigned integer type.
Definition: cache.hpp:32
resource_cache(resource_cache &&)=default
Default move constructor.
resource_handle< resource_type > temp(Args &&...args) const
Creates a temporary handle for a resource.
Definition: cache.hpp:147
Shared resource handle.
Definition: handle.hpp:25
EnTT default namespace.
std::uint32_t id_type
Alias declaration for type identifiers.
Definition: fwd.hpp:13
Non-owning handle to an entity.
Definition: handle.hpp:23
Identity function object (waiting for C++20).
Definition: utility.hpp:10