galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages Concepts
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
13#include "galaxy/core/ServiceLocator.hpp"
15#include "galaxy/math/FNV1a.hpp"
17
18namespace galaxy
19{
20 namespace resource
21 {
28 template<meta::not_memory Resource, meta::not_memory Loader>
29 // requires meta::is_class < Resource>&& meta::is_loader < Loader, Resource>
30 class Cache final
31 {
32 using CacheType = ankerl::unordered_dense::map<std::uint64_t, std::unique_ptr<Resource>>;
33
34 public:
38 inline Cache() noexcept
39 {
40 }
41
45 inline ~Cache()
46 {
47 clear();
48 }
49
55 inline void load(const std::string& file)
56 {
57 m_keys.push_back(file);
58 m_cache[hash(file)] = std::move(m_loader(file));
59 }
60
66 inline void load_folder(const std::string& dir)
67 {
68 clear();
69
70 for (const auto& file : core::ServiceLocator<fs::VirtualFileSystem>::ref().list(dir))
71 {
72 load(file);
73 }
74 }
75
82 inline void insert(std::string_view id, std::unique_ptr<Resource>& resource)
83 {
84 const auto hashed = hash(id);
85 if (!m_cache.contains(hashed))
86 {
87 m_cache[hashed] = std::move(resource);
88 }
89 else
90 {
91 GALAXY_LOG(GALAXY_WARNING, "Tried to load duplicate resource '{0}'.", id);
92 }
93 }
94
102 [[nodiscard]]
103 inline Resource* get(std::string_view id)
104 {
105 const auto hashed = hash(id);
106
107 if (m_cache.contains(hashed))
108 {
109 return m_cache[hashed].get();
110 }
111
112 return nullptr;
113 }
114
118 inline void clear()
119 {
120 m_cache.clear();
121 }
122
130 [[nodiscard]]
131 inline bool has(std::string_view id)
132 {
133 return m_cache.contains(hash(id));
134 }
135
141 [[nodiscard]]
142 inline bool empty() const
143 {
144 return size() == 0;
145 }
146
150 [[nodiscard]]
151 inline std::size_t size() const
152 {
153 return m_cache.size();
154 }
155
161 [[nodiscard]]
162 inline const CacheType& cache() const
163 {
164 return m_cache;
165 }
166
172 [[nodiscard]]
173 inline const meta::vector<std::string>& keys()
174 {
175 return m_keys;
176 }
177
178 private:
182 Cache(const Cache&) = delete;
183
187 Cache(Cache&&) = delete;
188
192 Cache& operator=(const Cache&) = delete;
193
197 Cache& operator=(Cache&&) = delete;
198
202 inline std::uint64_t hash(std::string_view str)
203 {
204 const auto hash = math::fnv1a_64(str.data());
205 return hash;
206 }
207
208 private:
213
218
222 meta::vector<std::string> m_keys;
223 };
224 } // namespace resource
225} // namespace galaxy
226
227#endif
#define GALAXY_WARNING
Definition Log.hpp:24
#define GALAXY_LOG(level, msg,...)
Definition Log.hpp:29
Cache for resources such as audio, fonts, etc.
Definition Cache.hpp:31
Cache & operator=(const Cache &)=delete
Copy assignment operator.
void clear()
Destroy resources.
Definition Cache.hpp:118
Cache & operator=(Cache &&)=delete
Move assignment operator.
meta::vector< std::string > m_keys
A list of keys currently in the cache.
Definition Cache.hpp:222
ankerl::unordered_dense::map< std::uint64_t, std::unique_ptr< Resource > > CacheType
Definition Cache.hpp:32
Loader m_loader
Used to load a resource into the cache. Allows for flexiblity.
Definition Cache.hpp:212
Cache(const Cache &)=delete
Copy constructor.
void load(const std::string &file)
Load a resource.
Definition Cache.hpp:55
std::size_t size() const
Get amount of resources cached.
Definition Cache.hpp:151
CacheType m_cache
The actual data store.
Definition Cache.hpp:217
void insert(std::string_view id, std::unique_ptr< Resource > &resource)
Insert a resource directly into the cache.
Definition Cache.hpp:82
void load_folder(const std::string &dir)
Load resources of a specific type.
Definition Cache.hpp:66
std::uint64_t hash(std::string_view str)
Hashes the key into an integer for faster map lookup time.
Definition Cache.hpp:202
Cache() noexcept
Constructor.
Definition Cache.hpp:38
Cache(Cache &&)=delete
Move constructor.
~Cache()
Destructor.
Definition Cache.hpp:45
const CacheType & cache() const
Get entire resource cache.
Definition Cache.hpp:162
bool has(std::string_view id)
Check if a resource exists.
Definition Cache.hpp:131
Resource * get(std::string_view id)
Retrieve a resource.
Definition Cache.hpp:103
const meta::vector< std::string > & keys()
Get a list of keys in the cache.
Definition Cache.hpp:173
bool empty() const
Does the cache have any resources.
Definition Cache.hpp:142
Timer.hpp galaxy.
Definition Async.hpp:17
Loads resources for a resource cache.
Definition Loader.hpp:25