entt  2.3.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 "../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 
146  auto it = resources.find(id);
147  return { it == resources.end() ? nullptr : it->second };
148  }
149 
155  bool contains(resource_type id) const noexcept {
156  return !(resources.find(id) == resources.cend());
157  }
158 
167  void discard(resource_type id) noexcept {
168  auto it = resources.find(id);
169 
170  if(it != resources.end()) {
171  resources.erase(it);
172  }
173  }
174 
175 private:
176  container_type resources;
177 };
178 
179 
180 }
181 
182 
183 #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 the given identifier.
Definition: cache.hpp:155
bool load(resource_type id, Args &&... args)
Loads the resource that corresponds to the 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
void discard(resource_type id) noexcept
Discards the resource that corresponds to the given identifier.
Definition: cache.hpp:167
ResourceCache()=default
Default constructor.
ResourceHandle< Resource > handle(resource_type id) const
Creates a handle for the given resource identifier.
Definition: cache.hpp:145
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