1#ifndef RESOURCE_MANAGEMENT_H
2#define RESOURCE_MANAGEMENT_H
7#include <unordered_map>
17 template <
typename Resource,
typename Identifier>
27 template <
typename Concrete,
typename... Args>
30 auto resource = std::make_unique<Concrete>(std::forward<Args>(
args)...);
33 throw std::runtime_error(
"Failed to load resource");
35 insert_resource(
id, std::move(resource));
41 void load(Identifier
id, std::string_view txt);
46 Resource&
get(Identifier
id);
51 const Resource&
get(Identifier
id)
const;
56 m_resources_map.clear();
60 [[nodiscard]]
bool is_empty() const noexcept {
return m_resources_map.empty(); }
66 void insert_resource(Identifier
id, std::unique_ptr<Resource> resource);
68 std::unordered_map<Identifier, std::unique_ptr<Resource>> m_resources_map;
71 template <
typename Resource,
typename Identifier>
75 auto resource = std::make_unique<Resource>();
77 if (!resource->set(txt))
79 throw std::runtime_error(
"Failed to load resource");
83 insert_resource(
id, std::move(resource));
91 template <
typename Resource,
typename Identifier>
94 auto found = m_resources_map.find(
id);
95 if (found == m_resources_map.cend())
97 throw std::out_of_range(
"Key not found");
100 return *found->second;
108 template <
typename Resource,
typename Identifier>
111 auto found = m_resources_map.find(
id);
112 if (found == m_resources_map.cend())
114 throw std::out_of_range(
"Key not found");
117 return *found->second;
125 template <
typename Resource,
typename Identifier>
128 m_resources_map.insert_or_assign(
id, std::move(resource));
Command-line argument handler with JSON support.
Definition args.h:19
Template class for managing resources.
Definition resource_management.h:19
void clear() noexcept
Clears all resources from the manager.
Definition resource_management.h:54
bool is_empty() const noexcept
Checks if the resource manager is empty.
Definition resource_management.h:60
Resource & get(Identifier id)
Retrieves a resource by its identifier.
Definition resource_management.h:92
const Resource & get(Identifier id) const
Retrieves a const reference to a resource by its identifier.
Definition resource_management.h:109
void load(Identifier id, Args &&... args)
Loads a resource of a specific type.
Definition resource_management.h:28
void load(Identifier id, std::string_view txt)
Loads a resource from a string.
Definition resource_management.h:72