-
Notifications
You must be signed in to change notification settings - Fork 0
/
resourcepool.hpp
executable file
·61 lines (55 loc) · 1.26 KB
/
resourcepool.hpp
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
#ifndef RESOURCEPOOL_HPP
#define RESOURCEPOOL_HPP
#include <map>
template<class Key, class Value>
class ResourcePool
{
public:
Value *get(Key k);
void preload(Key k);
void reload(Key k);
void clear();
void reload();
protected:
typedef std::map<Key,Value*> Pool;
Pool contents;
};
template<class Key, class Value>
Value *ResourcePool<Key,Value>::get(Key k)
{
Pool::iterator ii=contents.find(k);
if(ii==contents.end()) {
contents[k] = NEW Value(k);
return contents[k];
}
return ii->second;
}
template<class Key, class Value>
void ResourcePool<Key,Value>::preload(Key k)
{
get(k);
}
template<class Key, class Value>
void ResourcePool<Key,Value>::reload(Key k)
{
Pool::iterator ii=contents.find(k);
if(ii==contents.end())
return (contents[k]=NEW Value(k));
delete(ii->second);
ii->second = NEW Value(k);
}
template<class Key, class Value>
void ResourcePool<Key,Value>::clear()
{
for(Pool::iterator ii=contents.begin(); ii!=contents.end(); ii++)
delete(ii->second);
contents.clear();
}
template<class Key, class Value>
void ResourcePool<Key,Value>::reload() {
for(Pool::iterator ii=contents.begin(); ii!=contents.end(); ii++) {
delete(ii->second);
ii->second = NEW Value(ii->first);
}
}
#endif