-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResourceManager.inl
133 lines (106 loc) · 2.83 KB
/
ResourceManager.inl
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "ResourceManager.h"
#include <fstream>
#include <sstream>
template <typename T>
inline ResourceManager<T>::~ResourceManager() {
removeAll();
}
template <typename T>
inline void ResourceManager<T>::release(const std::string& filename) {
auto it = m_resources.find(filename);
if(it == m_resources.end()) {
return;
}
(it->second.users)--;
if((it->second.users) <= 0) {
remove(it);
}
}
template <typename T>
inline void ResourceManager<T>::remove(std::unordered_map<std::string, ResourceData>::iterator element) {
if(element == m_resources.end()) {
return;
}
delete element->second.file;
element->second.file = nullptr;
//m_resources.erase(element);
}
template <typename T>
inline void ResourceManager<T>::remove(const std::string& filename) {
auto it = m_resources.find(filename);
if(it == m_resources.end()) {
return;
}
delete it->second.file;
m_resources.erase(it);
}
template <typename T>
inline void ResourceManager<T>::removeAll() {
for(auto& res : m_resources) {
delete res.second.file;
}
m_resources.clear();
}
template <typename T>
inline bool ResourceManager<T>::hasResource(const std::string& name) const {
return m_resources.find(name) == m_resources.end() ? false : true;
}
template <typename T>
inline T* ResourceManager<T>::get(const std::string& filename) {
auto it = m_resources.find(filename);
if(it == m_resources.end()) {
return nullptr;
}
if(it->second.file == nullptr) {
if(it->second.path != "") {
bool res = loadFile(filename);
if(!res) {
return nullptr;
}
}
else {
return nullptr;
}
}
(it->second.users)++;
return it->second.file;
}
template <typename T>
inline bool ResourceManager<T>::addPath(const std::string& name, const std::string& filepath) {
if(hasResource(name)) {
return false;
}
auto res = m_resources.emplace(name, ResourceData());
if(!res.second) {
return false;
}
res.first->second.path = filepath;
return true;
}
template <typename T>
inline bool ResourceManager<T>::loadFile(const std::string& name) {
T* temp = new T();
auto it = m_resources.find(name);
if(it == m_resources.end() || !temp->loadFromFile(it->second.path)) {
delete temp;
return false;
}
it->second.file = temp;
return true;
}
template <typename T>
inline void ResourceManager<T>::loadPaths(const std::string& pathfile) {
std::ifstream is;
if(!is.open(pathfile)) {
return;
}
std::string line;
while(std::getline(is, line)) {
std::string name;
std::string path;
std::stringstream ss;
ss >> name >> path;
addPath(name, path);
}
is.close();
}