20#ifndef YOURGAME_ASSETMANAGER_H
21#define YOURGAME_ASSETMANAGER_H
63 bool insert(std::string name, T *obj)
71 auto itDelFunc = m_delFuncs.find(
typeid(T).hash_code());
72 if (itDelFunc == m_delFuncs.end())
74 m_delFuncs[
typeid(T).hash_code()] =
75 [](std::uintptr_t ptr)
76 {
delete reinterpret_cast<const T *
>(ptr); };
80 auto itT = m_ptrMaps[
typeid(T).hash_code()].find(name);
81 if (itT != m_ptrMaps[
typeid(T).hash_code()].end())
83 m_delFuncs[
typeid(T).hash_code()](itT->second);
85 m_ptrMaps[
typeid(T).hash_code()][name] =
reinterpret_cast<std::uintptr_t
>(obj);
100 static_assert(
sizeof(T) + 1 == 0,
"this class only works with pointer types");
113 auto itMap = m_ptrMaps.find(
typeid(T).hash_code());
114 if (itMap != m_ptrMaps.end())
116 auto itT = itMap->second.find(name);
117 if (itT != itMap->second.end())
119 return reinterpret_cast<T *
>(itT->second);
134 auto itMap = m_ptrMaps.find(
typeid(T).hash_code());
135 if (itMap != m_ptrMaps.end())
137 return itMap->second.size();
151 auto itMap = m_ptrMaps.find(
typeid(T).hash_code());
152 if (itMap != m_ptrMaps.end())
154 auto itT = itMap->second.find(name);
155 if (itT != itMap->second.end())
157 m_delFuncs[itMap->first](itT->second);
158 itMap->second.erase(itT);
171 auto itMap = m_ptrMaps.find(
typeid(T).hash_code());
172 if (itMap != m_ptrMaps.end())
174 for (
const auto &ptr : itMap->second)
176 m_delFuncs[itMap->first](ptr.second);
178 m_ptrMaps.erase(itMap);
186 for (
const auto &map : m_ptrMaps)
189 for (
const auto &ptr : map.second)
192 m_delFuncs[map.first](ptr.second);
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;
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