galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
Cache.hpp
Go to the documentation of this file.
1
7
8#ifndef GALAXY_RESOURCE_CACHE_HPP_
9#define GALAXY_RESOURCE_CACHE_HPP_
10
11#include <ankerl/unordered_dense.h>
12#include <entt/locator/locator.hpp>
13
16#include "galaxy/math/FNV1a.hpp"
18
19namespace galaxy
20{
27 template<typename Resource, typename SpecLoader>
28 requires meta::not_memory<Resource> && meta::is_class<Resource> && meta::is_instance_of_v<SpecLoader, Loader>
29 class Cache final
30 {
34 using Map = ankerl::unordered_dense::map<std::uint64_t, std::shared_ptr<Resource>>;
35
36 public:
40 Cache() noexcept;
41
45 ~Cache();
46
55 void load_file(const std::string& file);
56
65 template<typename... Args>
66 void load_resource(const std::string& key, Args&&... args);
67
76 void load_folder(const std::string& dir);
77
84 void insert(const std::string& key, std::shared_ptr<Resource> resource) noexcept;
85
93 [[nodiscard]]
94 std::shared_ptr<Resource> get(const std::string& key) noexcept;
95
103 [[nodiscard]]
104 bool has(const std::string& key) noexcept;
105
109 void clear();
110
116 [[nodiscard]]
117 bool empty() const noexcept;
118
122 [[nodiscard]]
123 std::size_t size() const noexcept;
124
130 [[nodiscard]]
131 const Map& cache() const noexcept;
132
133 private:
137 Cache(const Cache&) = delete;
138
142 Cache(Cache&&) = delete;
143
147 Cache& operator=(const Cache&) = delete;
148
152 Cache& operator=(Cache&&) = delete;
153
154 private:
158 SpecLoader m_loader;
159
164 };
165
166 template<typename Resource, typename SpecLoader>
167 requires meta::not_memory<Resource> && meta::is_class<Resource> && meta::is_instance_of_v<SpecLoader, Loader>
168 inline Cache<Resource, SpecLoader>::Cache() noexcept
169 {
170 }
171
172 template<typename Resource, typename SpecLoader>
175 {
176 clear();
177 }
178
179 template<typename Resource, typename SpecLoader>
181 inline void Cache<Resource, SpecLoader>::load_file(const std::string& file)
182 {
183 const auto hash = math::fnv1a(file.c_str());
184 if (!m_cache.contains(hash))
185 {
186 m_cache[hash] = m_loader(file);
187 }
188 else
189 {
190 GALAXY_LOG(GALAXY_WARN, "Tried to load duplicate resource: '{0}'.", file);
191 }
192 }
193
194 template<typename Resource, typename SpecLoader>
196 template<typename... Args>
197 inline void Cache<Resource, SpecLoader>::load_resource(const std::string& key, Args&&... args)
198 {
199 const auto hash = math::fnv1a(key.c_str());
200 if (!m_cache.contains(hash))
201 {
202 m_cache[hash] = m_loader(std::forward<Args>(args)...);
203 }
204 else
205 {
206 GALAXY_LOG(GALAXY_WARN, "Tried to load duplicate resource: '{0}'.", key);
207 }
208 }
209
210 template<typename Resource, typename SpecLoader>
212 inline void Cache<Resource, SpecLoader>::load_folder(const std::string& dir)
213 {
214 auto& fs = entt::locator<VirtualFileSystem>::value();
215 for (const auto& file : fs.list(dir))
216 {
217 load_file(file);
218 }
219 }
220
221 template<typename Resource, typename SpecLoader>
223 inline void Cache<Resource, SpecLoader>::insert(const std::string& key, std::shared_ptr<Resource> resource) noexcept
224 {
225 const auto hash = math::fnv1a(key.c_str());
226 if (!m_cache.contains(hash))
227 {
228 m_cache[hash] = resource;
229 }
230 else
231 {
232 GALAXY_LOG(GALAXY_WARN, "Tried to load duplicate resource: '{0}'.", key);
233 }
234 }
235
236 template<typename Resource, typename SpecLoader>
238 inline std::shared_ptr<Resource> Cache<Resource, SpecLoader>::get(const std::string& key) noexcept
239 {
240 const auto hash = math::fnv1a(key.c_str());
241 if (m_cache.contains(hash))
242 {
243 return m_cache[hash];
244 }
245
246 return nullptr;
247 }
248
249 template<typename Resource, typename SpecLoader>
251 inline bool Cache<Resource, SpecLoader>::has(const std::string& key) noexcept
252 {
253 const auto hash = math::fnv1a(key.c_str());
254 return m_cache.contains(hash);
255 }
256
257 template<typename Resource, typename SpecLoader>
260 {
261 m_cache.clear();
262 }
263
264 template<typename Resource, typename SpecLoader>
266 inline bool Cache<Resource, SpecLoader>::empty() const noexcept
267 {
268 return size() == 0;
269 }
270
271 template<typename Resource, typename SpecLoader>
273 inline std::size_t Cache<Resource, SpecLoader>::size() const noexcept
274 {
275 return m_cache.size();
276 }
277
278 template<typename Resource, typename SpecLoader>
281 {
282 return m_cache;
283 }
284} // namespace galaxy
285
286#endif
#define GALAXY_LOG(level, msg,...)
Definition Log.hpp:28
#define GALAXY_WARN
Definition Log.hpp:23
Cache for resources such as audio, fonts, etc.
Definition Cache.hpp:30
bool has(const std::string &key) noexcept
Check if a resource exists.
Definition Cache.hpp:251
const Map & cache() const noexcept
Get entire resource cache.
Definition Cache.hpp:280
std::shared_ptr< Resource > get(const std::string &key) noexcept
Get a resource.
Definition Cache.hpp:238
void load_file(const std::string &file)
Load a resource.
Definition Cache.hpp:181
void load_folder(const std::string &dir)
Load all resources in a folder.
Definition Cache.hpp:212
ankerl::unordered_dense::map< std::uint64_t, std::shared_ptr< Resource > > Map
Use a dense map for storage.
Definition Cache.hpp:34
void clear()
Destroy all data in cache.
Definition Cache.hpp:259
void load_resource(const std::string &key, Args &&... args)
Loads a resource with custom args.
Definition Cache.hpp:197
SpecLoader m_loader
Flexible Loader used to create/load a Resource into the cache.
Definition Cache.hpp:158
std::size_t size() const noexcept
Get amount of resources cached.
Definition Cache.hpp:273
Map m_cache
Resource storage map.
Definition Cache.hpp:163
~Cache()
Destructor.
Definition Cache.hpp:174
bool empty() const noexcept
Does the cache have any resources.
Definition Cache.hpp:266
Cache() noexcept
Constructor.
Definition Cache.hpp:168
void insert(const std::string &key, std::shared_ptr< Resource > resource) noexcept
Insert a resource directly into the cache.
Definition Cache.hpp:223
Only class concept.
Definition Concepts.hpp:27
Concept to restrict templates to not pointers and not references.
Definition Concepts.hpp:37
constexpr bits fnv1a(const char *const str, const bits value=fnv_1a_params< bits >::offset) noexcept
Convert string to hash.
Definition FNV1a.hpp:64
constexpr bool is_instance_of_v
Makes sure a template specialization is an instance of a template class.
Definition Concepts.hpp:96
Animated.cpp galaxy.
Definition Animated.cpp:16
STL namespace.
Loads resources for a resource cache.
Definition Loader.hpp:25