entt  2.4.1
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 "../core/hashed_string.hpp"
10 #include "handle.hpp"
11 #include "loader.hpp"
12 
13 
14 namespace entt {
15 
16 
27 template<typename Resource>
29  using container_type = std::unordered_map<HashedString::hash_type, std::shared_ptr<Resource>>;
30 
31 public:
33  using size_type = typename container_type::size_type;
36 
38  ResourceCache() = default;
39 
41  ResourceCache(const ResourceCache &) noexcept = delete;
43  ResourceCache(ResourceCache &&) noexcept = default;
44 
46  ResourceCache & operator=(const ResourceCache &) noexcept = delete;
48  ResourceCache & operator=(ResourceCache &&) noexcept = default;
49 
54  size_type size() const noexcept {
55  return resources.size();
56  }
57 
62  bool empty() const noexcept {
63  return resources.empty();
64  }
65 
72  void clear() noexcept {
73  resources.clear();
74  }
75 
94  template<typename Loader, typename... Args>
95  bool load(resource_type id, Args&&... args) {
96  static_assert(std::is_base_of<ResourceLoader<Loader, Resource>, Loader>::value, "!");
97 
98  bool loaded = true;
99 
100  if(resources.find(id) == resources.cend()) {
101  std::shared_ptr<Resource> resource = Loader{}.get(std::forward<Args>(args)...);
102  loaded = (static_cast<bool>(resource) ? (resources[id] = std::move(resource), loaded) : false);
103  }
104 
105  return loaded;
106  }
107 
127  template<typename Loader, typename... Args>
128  void reload(resource_type id, Args&&... args) {
129  return (discard(id), load(id, std::forward<Args>(args)...));
130  }
131 
144  template<typename Loader, typename... Args>
145  ResourceHandle<Resource> temp(Args&&... args) const {
146  return { Loader{}.get(std::forward<Args>(args)...) };
147  }
148 
163  auto it = resources.find(id);
164  return { it == resources.end() ? nullptr : it->second };
165  }
166 
172  bool contains(resource_type id) const noexcept {
173  return !(resources.find(id) == resources.cend());
174  }
175 
184  void discard(resource_type id) noexcept {
185  auto it = resources.find(id);
186 
187  if(it != resources.end()) {
188  resources.erase(it);
189  }
190  }
191 
192 private:
193  container_type resources;
194 };
195 
196 
197 }
198 
199 
200 #endif // ENTT_RESOURCE_CACHE_HPP
typename container_type::size_type size_type
Unsigned integer type.
Definition: cache.hpp:33
bool contains(resource_type id) const noexcept
Checks if a cache contains a given identifier.
Definition: cache.hpp:172
bool load(resource_type id, Args &&... args)
Loads the resource that corresponds to a given identifier.
Definition: cache.hpp:95
bool empty() const noexcept
Returns true if a cache contains no resources, false otherwise.
Definition: cache.hpp:62
EnTT default namespace.
Definition: family.hpp:9
ResourceHandle< Resource > temp(Args &&... args) const
Creates a temporary handle for a resource.
Definition: cache.hpp:145
void discard(resource_type id) noexcept
Discards the resource that corresponds to a given identifier.
Definition: cache.hpp:184
ResourceCache()=default
Default constructor.
ResourceHandle< Resource > handle(resource_type id) const
Creates a handle for a given resource identifier.
Definition: cache.hpp:162
Zero overhead resource identifier.
void reload(resource_type id, Args &&... args)
Reloads a resource or loads it for the first time if not present.
Definition: cache.hpp:128
Base class for resource loaders.
Definition: loader.hpp:48
size_type size() const noexcept
Number of resources managed by a cache.
Definition: cache.hpp:54
Shared resource handle.
Definition: handle.hpp:30
void clear() noexcept
Clears a cache and discards all its resources.
Definition: cache.hpp:72
Simple cache for resources of a given type.
Definition: cache.hpp:28