Maze Builder Docs 8.10.23
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 Resource &get(Identifier id);
42
46 const Resource &get(Identifier id) const;
47
49 void clear() noexcept
50 {
51 m_resources_map.clear();
52 }
53
55 [[nodiscard]] bool is_empty() const noexcept { return m_resources_map.empty(); }
56
57 private:
61 void insert_resource(Identifier id, std::unique_ptr<Resource> resource);
62
63 std::unordered_map<Identifier, std::unique_ptr<Resource>> m_resources_map;
64 };
65
71 template <typename Resource, typename Identifier>
73 {
74 auto found = m_resources_map.find(id);
75 if (found == m_resources_map.cend())
76 {
77 throw std::out_of_range("Key not found");
78 }
79
80 return *found->second;
81 }
82
88 template <typename Resource, typename Identifier>
89 const Resource &resource_management<Resource, Identifier>::get(Identifier id) const
90 {
91 auto found = m_resources_map.find(id);
92 if (found == m_resources_map.cend())
93 {
94 throw std::out_of_range("Key not found");
95 }
96
97 return *found->second;
98 }
99
105 template <typename Resource, typename Identifier>
106 void resource_management<Resource, Identifier>::insert_resource(Identifier id, std::unique_ptr<Resource> resource)
107 {
108 m_resources_map.insert_or_assign(id, std::move(resource));
109 }
110} // namespace mazes
111
112#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 load(Identifier id, Args &&...args)
Loads a resource of a specific type.
Definition resource_management.h:28
void clear() noexcept
Clears all resources from the manager.
Definition resource_management.h:49
bool is_empty() const noexcept
Checks if the resource manager is empty.
Definition resource_management.h:55
Resource & get(Identifier id)
Retrieves a resource by its identifier.
Definition resource_management.h:72
const Resource & get(Identifier id) const
Retrieves a const reference to a resource by its identifier.
Definition resource_management.h:89