-
Notifications
You must be signed in to change notification settings - Fork 2
/
IResourcePool.h
executable file
·68 lines (56 loc) · 1.51 KB
/
IResourcePool.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
60
61
62
63
64
65
66
67
68
#pragma once
#include "config.h"
#include <map>
#include <string>
namespace dawn
{
template <class K, class V>
class IResourcePool
{
public:
IResourcePool()
{
}
virtual ~IResourcePool()
{
ReleaseAllResources();
}
void ReleaseAllResources()
{
while (m_resources.begin() != m_resources.end()) {
m_resources.erase(m_resources.begin());
}
}
void ReleaseResource(K k)
{
typename CResourceMap::iterator it;
it=m_resources.find(id(k));
if (it != m_resources.end()) {
m_resources.erase(it);
}
}
V GetResource(K k)
{
typename CResourceMap::iterator it;
V resource;
std::string name = id(k);
it=m_resources.find(name);
if (it != m_resources.end() && IsResourceValid(k, it->second)) {
resource = it->second;
} else {
resource = LoadResource(k);
#if RESOURCE_STORE_POLICY != RESOURCE_NEVER_STORE
if (resource)
m_resources[name] = resource;
#endif
}
return resource;
}
protected:
virtual std::string id(K k) = 0;
virtual bool IsResourceValid(K k, V v) { return true; }
virtual V LoadResource(K k) = 0;
typedef std::map<std::string, V> CResourceMap;
CResourceMap m_resources;
};
}