Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
ResourceManager.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "Logger.hpp"
5
6#include <entt/core/hashed_string.hpp>
7#include <entt/resource/cache.hpp>
8#include <fmt/format.h>
9#include <optional>
10
12
17template <typename ResourceType> class ResourceManager {
18 private:
24 struct ResourceLoader final {
25 using result_type = std::shared_ptr<ResourceType>;
26
27 template <typename... Args> result_type operator()(Args &&...args) const
28 {
29 return std::make_shared<ResourceType>(std::forward<Args>(args)...);
30 }
31 };
32
33 public:
34 ResourceManager() = default;
35 ~ResourceManager() = default;
36
39
40 ResourceManager(ResourceManager &&) noexcept = default;
41 ResourceManager &operator=(ResourceManager &&) noexcept = default;
42
52 template <typename... Args> entt::resource<ResourceType> Add(const entt::hashed_string &id, Args &&...args)
53 {
54 auto ret = cache.load(id, std::forward<Args>(args)...);
55
56 if (!ret.second)
57 {
58 Log::Warning(fmt::format("Resource with id {} already exists. Overwriting.", id.data()));
59 ret = cache.force_load(id, std::forward<Args>(args)...);
60 }
61
62 return ret.first->second;
63 }
64
73 [[nodiscard]] ResourceType &Get(const entt::hashed_string &id)
74 {
75 auto resource = cache[id];
76
77 if (!resource)
78 throw ResourceManagerError(fmt::format("Resource with id {} not found.", id.data()));
79
80 return *resource;
81 }
82
91 [[nodiscard]] const ResourceType &Get(const entt::hashed_string &id) const
92 {
93 const auto &resource = cache[id];
94
95 if (!resource)
97 fmt::format("Resource with id {} not found.", std::string_view(id.data(), id.size())));
98
99 return *resource;
100 }
101
107 void Remove(const entt::hashed_string &id) { cache.erase(id); }
108
115 [[nodiscard]] bool Contains(const entt::hashed_string &id) const { return cache.contains(id); }
116
126 void SetDefault(ResourceType &&resource) { defaultResource = std::move(resource); }
127
128 template <typename... Args> void SetDefault(Args &&...args)
129 {
130 defaultResource = ResourceType(std::forward<Args>(args)...);
131 }
132
140 [[nodiscard]] ResourceType &GetDefault()
141 {
142 if (!defaultResource.has_value())
143 throw ResourceManagerError("No default resource is set.");
144
145 return *defaultResource;
146 }
147
155 [[nodiscard]] const ResourceType &GetDefault() const
156 {
157 if (!defaultResource.has_value())
158 throw ResourceManagerError("No default resource is set.");
159
160 return *defaultResource;
161 }
162
171 [[nodiscard]] ResourceType &GetOrDefault(const entt::hashed_string &id)
172 {
173 auto resource = cache[id];
174
175 if (resource)
176 return *resource;
177
178 if (!defaultResource.has_value())
180 fmt::format("Resource with id {} not found and no default resource is set.", id.data()));
181
182 return *defaultResource;
183 }
184
193 [[nodiscard]] const ResourceType &GetOrDefault(const entt::hashed_string &id) const
194 {
195 const auto &resource = cache[id];
196
197 if (resource)
198 return *resource;
199
200 if (!defaultResource.has_value())
202 fmt::format("Resource with id {} not found and no default resource is set.", id.data()));
203
204 return *defaultResource;
205 }
206
212 [[nodiscard]] bool HasDefault() const { return defaultResource.has_value(); }
213
214 private:
215 entt::resource_cache<ResourceType, ResourceLoader> cache{};
216 std::optional<ResourceType> defaultResource = std::nullopt;
217};
218
219} // namespace Object::Resource
Definition ResourceManagerError.hpp:26
const ResourceType & GetDefault() const
Get the default resource.
Definition ResourceManager.hpp:155
void Remove(const entt::hashed_string &id)
Delete an resource from the manager.
Definition ResourceManager.hpp:107
ResourceManager & operator=(const ResourceManager &)=delete
entt::resource< BindGroup > Add(const entt::hashed_string &id, Args &&...args)
Definition ResourceManager.hpp:52
ResourceManager(ResourceManager &&) noexcept=default
const ResourceType & Get(const entt::hashed_string &id) const
Get the reference to a stored resource.
Definition ResourceManager.hpp:91
void SetDefault(Args &&...args)
Definition ResourceManager.hpp:128
std::optional< BindGroup > defaultResource
Definition ResourceManager.hpp:216
const ResourceType & GetOrDefault(const entt::hashed_string &id) const
Get the reference to a stored resource, or the default resource if it doesn't exist.
Definition ResourceManager.hpp:193
ResourceType & GetOrDefault(const entt::hashed_string &id)
Get the reference to a stored resource, or the default resource if it doesn't exist.
Definition ResourceManager.hpp:171
ResourceManager(const ResourceManager &)=delete
ResourceType & Get(const entt::hashed_string &id)
Get the reference to a stored resource.
Definition ResourceManager.hpp:73
void SetDefault(ResourceType &&resource)
Set the default resource that will be used as fallback.
Definition ResourceManager.hpp:126
ResourceType & GetDefault()
Get the default resource.
Definition ResourceManager.hpp:140
bool HasDefault() const
Check if a default resource has been set.
Definition ResourceManager.hpp:212
entt::resource_cache< BindGroup, ResourceLoader > cache
Definition ResourceManager.hpp:215
bool Contains(const entt::hashed_string &id) const
Check whenever the resource with given id exists in the manager.
Definition ResourceManager.hpp:115
Definition ResourceManager.hpp:11
ResourceLoader structure is used to load a resource from another resource or from arguments.
Definition ResourceManager.hpp:24
result_type operator()(Args &&...args) const
Definition ResourceManager.hpp:27
std::shared_ptr< ResourceType > result_type
Definition ResourceManager.hpp:25