EnTT  3.5.0
cache.hpp
1 #ifndef ENTT_RESOURCE_CACHE_HPP
2 #define ENTT_RESOURCE_CACHE_HPP
3 
4 
5 #include <memory>
6 #include <type_traits>
7 #include <unordered_map>
8 #include <utility>
9 #include "../config/config.h"
10 #include "../core/fwd.hpp"
11 #include "handle.hpp"
12 #include "loader.hpp"
13 #include "fwd.hpp"
14 
15 
16 namespace entt {
17 
18 
29 template<typename Resource>
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> load(const id_type id, Args &&... args) {
95  static_assert(std::is_base_of_v<resource_loader<Loader, Resource>, Loader>, "Invalid loader type");
96  resource_handle<Resource> resource{};
97 
98  if(auto it = resources.find(id); it == resources.cend()) {
99  if(auto instance = Loader{}.get(std::forward<Args>(args)...); instance) {
100  resources[id] = instance;
101  resource = std::move(instance);
102  }
103  } else {
104  resource = it->second;
105  }
106 
107  return resource;
108  }
109 
133  template<typename Loader, typename... Args>
134  resource_handle<Resource> reload(const id_type id, Args &&... args) {
135  return (discard(id), load<Loader>(id, std::forward<Args>(args)...));
136  }
137 
150  template<typename Loader, typename... Args>
151  [[nodiscard]] resource_handle<Resource> temp(Args &&... args) const {
152  return { Loader{}.get(std::forward<Args>(args)...) };
153  }
154 
168  [[nodiscard]] resource_handle<Resource> handle(const id_type id) const {
169  auto it = resources.find(id);
170  return { it == resources.end() ? nullptr : it->second };
171  }
172 
178  [[nodiscard]] bool contains(const id_type id) const {
179  return (resources.find(id) != resources.cend());
180  }
181 
190  void discard(const id_type id) {
191  if(auto it = resources.find(id); it != resources.end()) {
192  resources.erase(it);
193  }
194  }
195 
213  template <typename Func>
214  void each(Func func) const {
215  auto begin = resources.begin();
216  auto end = resources.end();
217 
218  while(begin != end) {
219  auto curr = begin++;
220 
221  if constexpr(std::is_invocable_v<Func, id_type>) {
222  func(curr->first);
223  } else if constexpr(std::is_invocable_v<Func, resource_handle<Resource>>) {
224  func(resource_handle{ curr->second });
225  } else {
226  func(curr->first, resource_handle{ curr->second });
227  }
228  }
229  }
230 
231 private:
232  std::unordered_map<id_type, std::shared_ptr<Resource>> resources;
233 };
234 
235 
236 }
237 
238 
239 #endif
entt::resource_loader
Base class for resource loaders.
Definition: fwd.hpp:17
entt::resource_cache::contains
bool contains(const id_type id) const
Checks if a cache contains a given identifier.
Definition: cache.hpp:178
entt::id_type
std::uint32_t id_type
Alias declaration for type identifiers.
Definition: fwd.hpp:12
entt::resource_cache::each
void each(Func func) const
Iterates all resources.
Definition: cache.hpp:214
entt::resource_cache::resource_cache
resource_cache(resource_cache &&)=default
Default move constructor.
entt::resource_cache::resource_cache
resource_cache()=default
Default constructor.
entt::resource_cache::empty
bool empty() const noexcept
Returns true if a cache contains no resources, false otherwise.
Definition: cache.hpp:57
entt::resource_cache::temp
resource_handle< Resource > temp(Args &&... args) const
Creates a temporary handle for a resource.
Definition: cache.hpp:151
entt::resource_cache::handle
resource_handle< Resource > handle(const id_type id) const
Creates a handle for a given resource identifier.
Definition: cache.hpp:168
entt::resource_cache
Simple cache for resources of a given type.
Definition: cache.hpp:30
entt
EnTT default namespace.
Definition: algorithm.hpp:13
entt::resource_cache::operator=
resource_cache & operator=(resource_cache &&)=default
Default move assignment operator.
entt::resource_cache::discard
void discard(const id_type id)
Discards the resource that corresponds to a given identifier.
Definition: cache.hpp:190
entt::resource_cache::clear
void clear() noexcept
Clears a cache and discards all its resources.
Definition: cache.hpp:67
entt::resource_cache::reload
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:134
entt::resource_cache::load
resource_handle< Resource > load(const id_type id, Args &&... args)
Loads the resource that corresponds to a given identifier.
Definition: cache.hpp:94
entt::resource_cache::resource_type
Resource resource_type
Type of resources managed by a cache.
Definition: cache.hpp:34
entt::resource_handle
Shared resource handle.
Definition: fwd.hpp:13
entt::resource_cache::size_type
std::size_t size_type
Unsigned integer type.
Definition: cache.hpp:32
entt::resource_cache::size
size_type size() const noexcept
Number of resources managed by a cache.
Definition: cache.hpp:49