YourGameLib
Loading...
Searching...
No Matches
assetmanager.h
Go to the documentation of this file.
1/*
2Copyright (c) 2019-2024 Alexander Scholz
3
4This software is provided 'as-is', without any express or implied
5warranty. In no event will the authors be held liable for any damages
6arising from the use of this software.
7
8Permission is granted to anyone to use this software for any purpose,
9including commercial applications, and to alter it and redistribute it
10freely, subject to the following restrictions:
11
121. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
162. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
183. This notice may not be removed or altered from any source distribution.
19*/
20#ifndef YOURGAME_ASSETMANAGER_H
21#define YOURGAME_ASSETMANAGER_H
22
23#include <functional>
24#include <cstdint> // std::uintptr_t
25#include <typeinfo>
26#include <map>
27#include <string>
28
29namespace yourgame
30{
31 namespace util
32 {
47 {
48 public:
50
62 template <class T>
63 bool insert(std::string name, T *obj)
64 {
65 if (!obj)
66 {
67 return false;
68 }
69
70 // insert delete function for objects of type T, if not present
71 auto itDelFunc = m_delFuncs.find(typeid(T).hash_code());
72 if (itDelFunc == m_delFuncs.end())
73 {
74 m_delFuncs[typeid(T).hash_code()] =
75 [](std::uintptr_t ptr)
76 { delete reinterpret_cast<const T *>(ptr); };
77 }
78
79 // insert (or replace) obj into map for objects of type T
80 auto itT = m_ptrMaps[typeid(T).hash_code()].find(name);
81 if (itT != m_ptrMaps[typeid(T).hash_code()].end())
82 {
83 m_delFuncs[typeid(T).hash_code()](itT->second);
84 }
85 m_ptrMaps[typeid(T).hash_code()][name] = reinterpret_cast<std::uintptr_t>(obj);
86
87 return true;
88 }
89
97 template <class T>
98 void insert(std::string name, T obj)
99 {
100 static_assert(sizeof(T) + 1 == 0, "this class only works with pointer types");
101 }
102
110 template <class T>
111 T *get(std::string name)
112 {
113 auto itMap = m_ptrMaps.find(typeid(T).hash_code());
114 if (itMap != m_ptrMaps.end())
115 {
116 auto itT = itMap->second.find(name);
117 if (itT != itMap->second.end())
118 {
119 return reinterpret_cast<T *>(itT->second);
120 }
121 }
122 return nullptr;
123 }
124
131 template <class T>
132 size_t numOf()
133 {
134 auto itMap = m_ptrMaps.find(typeid(T).hash_code());
135 if (itMap != m_ptrMaps.end())
136 {
137 return itMap->second.size();
138 }
139 return 0;
140 }
141
148 template <class T>
149 void destroy(std::string name)
150 {
151 auto itMap = m_ptrMaps.find(typeid(T).hash_code());
152 if (itMap != m_ptrMaps.end())
153 {
154 auto itT = itMap->second.find(name);
155 if (itT != itMap->second.end())
156 {
157 m_delFuncs[itMap->first](itT->second);
158 itMap->second.erase(itT);
159 }
160 }
161 }
162
168 template <class T>
169 void destroy()
170 {
171 auto itMap = m_ptrMaps.find(typeid(T).hash_code());
172 if (itMap != m_ptrMaps.end())
173 {
174 for (const auto &ptr : itMap->second)
175 {
176 m_delFuncs[itMap->first](ptr.second);
177 }
178 m_ptrMaps.erase(itMap);
179 }
180 }
181
183 void clear()
184 {
185 // for all maps holding different pointer types...
186 for (const auto &map : m_ptrMaps)
187 {
188 // ...delete all objects pointed to...
189 for (const auto &ptr : map.second)
190 {
191 // ...using the registered delete function
192 m_delFuncs[map.first](ptr.second);
193 }
194 }
195 m_ptrMaps.clear();
196 }
197
198 private:
199 std::map<size_t, std::map<std::string, std::uintptr_t>> m_ptrMaps;
200 std::map<size_t, std::function<void(std::uintptr_t)>> m_delFuncs;
201 };
202 } // namespace util
203} // namespace yourgame
204
205#endif
utility to store and request pointers, and to delete dynamically allocated objects,...
Definition assetmanager.h:47
void destroy(std::string name)
destroy (delete) object, remove pointer
Definition assetmanager.h:149
~AssetManager()
Definition assetmanager.h:49
T * get(std::string name)
request pointer by name
Definition assetmanager.h:111
void insert(std::string name, T obj)
dummy for compile-time check (static_assert) of non-pointer type
Definition assetmanager.h:98
size_t numOf()
returns number of stored pointers of type T*
Definition assetmanager.h:132
bool insert(std::string name, T *obj)
insert pointer. if it exists (same type T and same name), previous object is deleted and the pointer ...
Definition assetmanager.h:63
void clear()
clear content. delete all objects, remove all pointers
Definition assetmanager.h:183
void destroy()
destroy (delete) all objects of type T, remove all pointers of type T*
Definition assetmanager.h:169
Definition audio.h:27