-
Notifications
You must be signed in to change notification settings - Fork 2
/
ResourceManager.h
59 lines (48 loc) · 1.57 KB
/
ResourceManager.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#pragma once
#include "Log.h"
#include <iostream>
#include <string>
#include <unordered_map>
namespace g3d {
struct SResource {
std::string name;
void *resData;
};
class CResourceManager {
std::unordered_map<std::string, SResource *> data;
public:
CResourceManager();
~CResourceManager();
template <class T> void load(const std::string &name, const T *ptr);
template <typename T> T *getT(const std::string &name) const;
struct retTypeDeduce {
retTypeDeduce(CResourceManager *manager, const std::string &name)
: manager(manager), name(name) {}
CResourceManager *manager;
const std::string &name;
template <typename T> operator T *() {
return (T *)manager->getT<T>(name);
}
};
retTypeDeduce get(const std::string &name) {
return retTypeDeduce(this, name);
}
};
template <class T>
void CResourceManager::load(const std::string &name, const T *ptr) {
if (data.find(name) != data.end()) {
g3d::ENGINE_LOG_WARN("Resource with that name already exist {"
+ name + "}; overwriting");
delete data.find(name)->second;
}
data[name] = new SResource{name, (void *)ptr};
g3d::ENGINE_LOG_INFO("New resource: " + name);
}
template <typename T> T *CResourceManager::getT(const std::string &name) const {
if (data.find(name) == data.end())
g3d::ENGINE_LOG_ERROR("Resource {" + name + "} doesn't exist!");
auto resource = data.find(name)->second;
return (T *)resource->resData;
}
extern CResourceManager resourceManager;
};