EnTT  3.1.0
cache.hpp
1 #ifndef ENTT_RESOURCE_CACHE_HPP
2 #define ENTT_RESOURCE_CACHE_HPP
3 
4 
5 #include <memory>
6 #include <utility>
7 #include <type_traits>
8 #include <unordered_map>
9 #include "../config/config.h"
10 #include "handle.hpp"
11 #include "loader.hpp"
12 #include "fwd.hpp"
13 
14 
15 namespace entt {
16 
17 
28 template<typename Resource>
30  using container_type = std::unordered_map<ENTT_ID_TYPE, std::shared_ptr<Resource>>;
31 
32 public:
34  using size_type = typename container_type::size_type;
36  using resource_type = ENTT_ID_TYPE;
37 
39  resource_cache() = default;
40 
42  resource_cache(resource_cache &&) = default;
43 
45  resource_cache & operator=(resource_cache &&) = default;
46 
51  size_type size() const ENTT_NOEXCEPT {
52  return resources.size();
53  }
54 
59  bool empty() const ENTT_NOEXCEPT {
60  return resources.empty();
61  }
62 
69  void clear() ENTT_NOEXCEPT {
70  resources.clear();
71  }
72 
95  template<typename Loader, typename... Args>
96  resource_handle<Resource> load(const resource_type id, Args &&... args) {
97  static_assert(std::is_base_of_v<resource_loader<Loader, Resource>, Loader>);
99 
100  if(auto it = resources.find(id); it == resources.cend()) {
101  if(auto resource = Loader{}.get(std::forward<Args>(args)...); resource) {
102  resources[id] = resource;
103  handle = std::move(resource);
104  }
105  } else {
106  handle = it->second;
107  }
108 
109  return handle;
110  }
111 
135  template<typename Loader, typename... Args>
136  resource_handle<Resource> reload(const resource_type id, Args &&... args) {
137  return (discard(id), load<Loader>(id, std::forward<Args>(args)...));
138  }
139 
152  template<typename Loader, typename... Args>
153  resource_handle<Resource> temp(Args &&... args) const {
154  return { Loader{}.get(std::forward<Args>(args)...) };
155  }
156 
171  auto it = resources.find(id);
172  return { it == resources.end() ? nullptr : it->second };
173  }
174 
180  bool contains(const resource_type id) const ENTT_NOEXCEPT {
181  return (resources.find(id) != resources.cend());
182  }
183 
192  void discard(const resource_type id) ENTT_NOEXCEPT {
193  if(auto it = resources.find(id); it != resources.end()) {
194  resources.erase(it);
195  }
196  }
197 
215  template <typename Func>
216  void each(Func func) const {
217  auto begin = resources.begin();
218  auto end = resources.end();
219 
220  while(begin != end) {
221  auto curr = begin++;
222 
223  if constexpr(std::is_invocable_v<Func, resource_type>) {
224  func(curr->first);
225  } else if constexpr(std::is_invocable_v<Func, resource_handle<Resource>>) {
226  func(resource_handle{ curr->second });
227  } else {
228  func(curr->first, resource_handle{ curr->second });
229  }
230  }
231  }
232 
233 private:
234  container_type resources;
235 };
236 
237 
238 }
239 
240 
241 #endif // ENTT_RESOURCE_CACHE_HPP
size_type size() const ENTT_NOEXCEPT
Number of resources managed by a cache.
Definition: cache.hpp:51
Simple cache for resources of a given type.
Definition: cache.hpp:29
resource_handle< Resource > reload(const resource_type id, Args &&... args)
Reloads a resource or loads it for the first time if not present.
Definition: cache.hpp:136
resource_handle< Resource > temp(Args &&... args) const
Creates a temporary handle for a resource.
Definition: cache.hpp:153
Base class for resource loaders.
Definition: fwd.hpp:21
EnTT default namespace.
Definition: algorithm.hpp:12
ENTT_ID_TYPE resource_type
Type of resources managed by a cache.
Definition: cache.hpp:36
typename container_type::size_type size_type
Unsigned integer type.
Definition: cache.hpp:34
resource_cache & operator=(resource_cache &&)=default
Default move assignment operator.
void each(Func func) const
Iterates all resources.
Definition: cache.hpp:216
void discard(const resource_type id) ENTT_NOEXCEPT
Discards the resource that corresponds to a given identifier.
Definition: cache.hpp:192
bool empty() const ENTT_NOEXCEPT
Returns true if a cache contains no resources, false otherwise.
Definition: cache.hpp:59
bool contains(const resource_type id) const ENTT_NOEXCEPT
Checks if a cache contains a given identifier.
Definition: cache.hpp:180
Shared resource handle.
Definition: fwd.hpp:17
resource_handle< Resource > handle(const resource_type id) const
Creates a handle for a given resource identifier.
Definition: cache.hpp:170
void clear() ENTT_NOEXCEPT
Clears a cache and discards all its resources.
Definition: cache.hpp:69
resource_handle< Resource > load(const resource_type id, Args &&... args)
Loads the resource that corresponds to a given identifier.
Definition: cache.hpp:96
resource_cache()=default
Default constructor.