Maze Builder Docs 8.2.1
Loading...
Searching...
No Matches
resource_management.h
Go to the documentation of this file.
1#ifndef RESOURCE_MANAGEMENT_H
2#define RESOURCE_MANAGEMENT_H
3
4#include <memory>
5#include <string_view>
6#include <stdexcept>
7#include <unordered_map>
8#include <utility>
9
12namespace mazes
13{
17 template <typename Resource, typename Identifier>
19 {
20 public:
27 template <typename Concrete, typename... Args>
28 void load(Identifier id, Args&&... args)
29 {
30 auto resource = std::make_unique<Concrete>(std::forward<Args>(args)...);
31 if (!resource)
32 {
33 throw std::runtime_error("Failed to load resource");
34 }
35 insert_resource(id, std::move(resource));
36 }
37
41 void load(Identifier id, std::string_view txt);
42
46 Resource& get(Identifier id);
47
51 const Resource& get(Identifier id) const;
52
54 void clear() noexcept
55 {
56 m_resources_map.clear();
57 }
58
60 [[nodiscard]] bool is_empty() const noexcept { return m_resources_map.empty(); }
61
62 private:
66 void insert_resource(Identifier id, std::unique_ptr<Resource> resource);
67
68 std::unordered_map<Identifier, std::unique_ptr<Resource>> m_resources_map;
69 };
70
71 template <typename Resource, typename Identifier>
72 void resource_management<Resource, Identifier>::load(Identifier id, std::string_view txt)
73 {
74 // Create and load resource
75 auto resource = std::make_unique<Resource>();
76
77 if (!resource->set(txt))
78 {
79 throw std::runtime_error("Failed to load resource");
80 }
81
82 // If loading successful, insert resource to map
83 insert_resource(id, std::move(resource));
84 }
85
91 template <typename Resource, typename Identifier>
93 {
94 auto found = m_resources_map.find(id);
95 if (found == m_resources_map.cend())
96 {
97 throw std::out_of_range("Key not found");
98 }
99
100 return *found->second;
101 }
102
108 template <typename Resource, typename Identifier>
109 const Resource& resource_management<Resource, Identifier>::get(Identifier id) const
110 {
111 auto found = m_resources_map.find(id);
112 if (found == m_resources_map.cend())
113 {
114 throw std::out_of_range("Key not found");
115 }
116
117 return *found->second;
118 }
119
125 template <typename Resource, typename Identifier>
126 void resource_management<Resource, Identifier>::insert_resource(Identifier id, std::unique_ptr<Resource> resource)
127 {
128 m_resources_map.insert_or_assign(id, std::move(resource));
129 }
130} // namespace mazes
131
132#endif // RESOURCE_MANAGEMENT_H
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