EnTT  3.2.2
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>
29 struct cache {
31  using size_type = std::size_t;
33  using resource_type = Resource;
35  using id_type = ENTT_ID_TYPE;
36 
38  cache() = default;
39 
41  cache(cache &&) = default;
42 
44  cache & operator=(cache &&) = default;
45 
50  size_type size() const ENTT_NOEXCEPT {
51  return resources.size();
52  }
53 
58  bool empty() const ENTT_NOEXCEPT {
59  return resources.empty();
60  }
61 
68  void clear() ENTT_NOEXCEPT {
69  resources.clear();
70  }
71 
94  template<typename Loader, typename... Args>
95  entt::handle<Resource> load(const id_type id, Args &&... args) {
96  static_assert(std::is_base_of_v<loader<Loader, Resource>, Loader>);
97  entt::handle<Resource> resource{};
98 
99  if(auto it = resources.find(id); it == resources.cend()) {
100  if(auto instance = Loader{}.get(std::forward<Args>(args)...); instance) {
101  resources[id] = instance;
102  resource = std::move(instance);
103  }
104  } else {
105  resource = it->second;
106  }
107 
108  return resource;
109  }
110 
134  template<typename Loader, typename... Args>
135  entt::handle<Resource> reload(const id_type id, Args &&... args) {
136  return (discard(id), load<Loader>(id, std::forward<Args>(args)...));
137  }
138 
151  template<typename Loader, typename... Args>
152  entt::handle<Resource> temp(Args &&... args) const {
153  return { Loader{}.get(std::forward<Args>(args)...) };
154  }
155 
170  auto it = resources.find(id);
171  return { it == resources.end() ? nullptr : it->second };
172  }
173 
179  bool contains(const id_type id) const ENTT_NOEXCEPT {
180  return (resources.find(id) != resources.cend());
181  }
182 
191  void discard(const id_type id) ENTT_NOEXCEPT {
192  if(auto it = resources.find(id); it != resources.end()) {
193  resources.erase(it);
194  }
195  }
196 
214  template <typename Func>
215  void each(Func func) const {
216  auto begin = resources.begin();
217  auto end = resources.end();
218 
219  while(begin != end) {
220  auto curr = begin++;
221 
222  if constexpr(std::is_invocable_v<Func, id_type>) {
223  func(curr->first);
224  } else if constexpr(std::is_invocable_v<Func, entt::handle<Resource>>) {
225  func(entt::handle{ curr->second });
226  } else {
227  func(curr->first, entt::handle{ curr->second });
228  }
229  }
230  }
231 
232 private:
233  std::unordered_map<id_type, std::shared_ptr<Resource>> resources;
234 };
235 
236 
237 }
238 
239 
240 #endif // ENTT_RESOURCE_CACHE_HPP
entt::cache::size
size_type size() const ENTT_NOEXCEPT
Number of resources managed by a cache.
Definition: cache.hpp:50
entt::cache::empty
bool empty() const ENTT_NOEXCEPT
Returns true if a cache contains no resources, false otherwise.
Definition: cache.hpp:58
entt::cache::resource_type
Resource resource_type
Type of resources managed by a cache.
Definition: cache.hpp:33
entt::cache::temp
entt::handle< Resource > temp(Args &&... args) const
Creates a temporary handle for a resource.
Definition: cache.hpp:152
entt::cache::clear
void clear() ENTT_NOEXCEPT
Clears a cache and discards all its resources.
Definition: cache.hpp:68
entt::cache::contains
bool contains(const id_type id) const ENTT_NOEXCEPT
Checks if a cache contains a given identifier.
Definition: cache.hpp:179
entt::cache
Simple cache for resources of a given type.
Definition: cache.hpp:29
entt::cache::operator=
cache & operator=(cache &&)=default
Default move assignment operator.
entt::cache::handle
entt::handle< Resource > handle(const id_type id) const
Creates a handle for a given resource identifier.
Definition: cache.hpp:169
entt::handle
Shared resource handle.
Definition: fwd.hpp:14
entt::cache::size_type
std::size_t size_type
Unsigned integer type.
Definition: cache.hpp:31
entt::cache::cache
cache()=default
Default constructor.
entt
EnTT default namespace.
Definition: algorithm.hpp:12
entt::cache::discard
void discard(const id_type id) ENTT_NOEXCEPT
Discards the resource that corresponds to a given identifier.
Definition: cache.hpp:191
entt::cache::load
entt::handle< Resource > load(const id_type id, Args &&... args)
Loads the resource that corresponds to a given identifier.
Definition: cache.hpp:95
entt::loader
Base class for resource loaders.
Definition: fwd.hpp:18
entt::cache::id_type
ENTT_ID_TYPE id_type
Unique identifier type for resources.
Definition: cache.hpp:35
entt::cache::reload
entt::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:135
entt::cache::each
void each(Func func) const
Iterates all resources.
Definition: cache.hpp:215