6#include <entt/core/hashed_string.hpp>
7#include <entt/resource/cache.hpp>
14template <
typename TType>
15concept CViewStringable = std::is_constructible_v<std::string_view, const TType &> &&
16 !std::is_same_v<std::decay_t<TType>, entt::hashed_string>;
34 return std::make_shared<ResourceType>(std::forward<Args>(args)...);
57 template <typename... Args> entt::resource<ResourceType>
Add(const entt::hashed_string &
id, Args &&...args)
59 auto ret =
cache.load(
id, std::forward<Args>(args)...);
63 Log::Warning(fmt::format(
"Resource with id {} already exists. Overwriting.", id.data()));
64 ret = cache.force_load(id, std::forward<Args>(args)...);
67 return ret.first->second;
71 entt::resource<ResourceType>
Add(
const TViewStringable &
id, Args &&...args)
73 std::string_view stringViewId{
id};
74 return Add(entt::hashed_string{stringViewId.data(), stringViewId.size()}, std::forward<Args>(args)...);
85 [[nodiscard]] ResourceType &
Get(
const entt::hashed_string &
id)
87 auto resource =
cache[id];
95 template <CViewStringable TViewStringable> [[nodiscard]] ResourceType &
Get(
const TViewStringable &
id)
97 std::string_view stringViewId{
id};
98 return Get(entt::hashed_string{stringViewId.data(), stringViewId.size()});
109 [[nodiscard]]
const ResourceType &
Get(
const entt::hashed_string &
id)
const
111 const auto &resource =
cache[id];
115 fmt::format(
"Resource with id {} not found.", std::string_view(
id.data(),
id.size())));
119 template <CViewStringable TViewStringable> [[nodiscard]]
const ResourceType &
Get(
const TViewStringable &
id)
const
121 std::string_view stringViewId{
id};
122 return Get(entt::hashed_string{stringViewId.data(), stringViewId.size()});
132 template <CViewStringable TViewStringable>
void Remove(
const TViewStringable &
id)
134 std::string_view stringViewId{
id};
135 Remove(entt::hashed_string{stringViewId.data(), stringViewId.size()});
144 [[nodiscard]]
bool Contains(
const entt::hashed_string &
id)
const {
return cache.contains(
id); }
146 template <CViewStringable TViewStringable> [[nodiscard]]
bool Contains(
const TViewStringable &
id)
const
148 std::string_view stringViewId{
id};
149 return Contains(entt::hashed_string{stringViewId.data(), stringViewId.size()});
206 [[nodiscard]] ResourceType &
GetOrDefault(
const entt::hashed_string &
id)
208 auto resource =
cache[id];
215 fmt::format(
"Resource with id {} not found and no default resource is set.",
id.data()));
220 template <CViewStringable TViewStringable> [[nodiscard]] ResourceType &
GetOrDefault(
const TViewStringable &
id)
222 std::string_view stringViewId{
id};
223 return GetOrDefault(entt::hashed_string{stringViewId.data(), stringViewId.size()});
234 [[nodiscard]]
const ResourceType &
GetOrDefault(
const entt::hashed_string &
id)
const
236 const auto &resource =
cache[id];
243 fmt::format(
"Resource with id {} not found and no default resource is set.",
id.data()));
248 template <CViewStringable TViewStringable>
249 [[nodiscard]]
const ResourceType &
GetOrDefault(
const TViewStringable &
id)
const
251 std::string_view stringViewId{
id};
252 return GetOrDefault(entt::hashed_string{stringViewId.data(), stringViewId.size()});
263 entt::resource_cache<ResourceType, ResourceLoader>
cache{};
Definition ResourceManagerError.hpp:26
const ResourceType & GetDefault() const
Get the default resource.
Definition ResourceManager.hpp:190
void Remove(const entt::hashed_string &id)
Delete an resource from the manager.
Definition ResourceManager.hpp:130
ResourceManager & operator=(const ResourceManager &)=delete
entt::resource< BindGroup > Add(const entt::hashed_string &id, Args &&...args)
Definition ResourceManager.hpp:57
ResourceManager(ResourceManager &&) noexcept=default
void Remove(const TViewStringable &id)
Definition ResourceManager.hpp:132
const ResourceType & Get(const entt::hashed_string &id) const
Get the reference to a stored resource.
Definition ResourceManager.hpp:109
void SetDefault(Args &&...args)
Definition ResourceManager.hpp:163
~ResourceManager()=default
std::optional< BindGroup > defaultResource
Definition ResourceManager.hpp:264
entt::resource< ResourceType > Add(const TViewStringable &id, Args &&...args)
Definition ResourceManager.hpp:71
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:234
const ResourceType & Get(const TViewStringable &id) const
Definition ResourceManager.hpp:119
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:206
const ResourceType & GetOrDefault(const TViewStringable &id) const
Definition ResourceManager.hpp:249
ResourceManager(const ResourceManager &)=delete
ResourceType & Get(const entt::hashed_string &id)
Get the reference to a stored resource.
Definition ResourceManager.hpp:85
ResourceType & Get(const TViewStringable &id)
Definition ResourceManager.hpp:95
ResourceManager()=default
void SetDefault(ResourceType &&resource)
Set the default resource that will be used as fallback.
Definition ResourceManager.hpp:161
ResourceType & GetOrDefault(const TViewStringable &id)
Definition ResourceManager.hpp:220
ResourceType & GetDefault()
Get the default resource.
Definition ResourceManager.hpp:175
bool HasDefault() const
Check if a default resource has been set.
Definition ResourceManager.hpp:260
entt::resource_cache< BindGroup, ResourceLoader > cache
Definition ResourceManager.hpp:263
bool Contains(const entt::hashed_string &id) const
Check whenever the resource with given id exists in the manager.
Definition ResourceManager.hpp:144
bool Contains(const TViewStringable &id) const
Definition ResourceManager.hpp:146
Definition ResourceManager.hpp:15
Definition ResourceManager.hpp:12
ResourceLoader structure is used to load a resource from another resource or from arguments.
Definition ResourceManager.hpp:29
result_type operator()(Args &&...args) const
Definition ResourceManager.hpp:32
std::shared_ptr< ResourceType > result_type
Definition ResourceManager.hpp:30