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