-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlayeredmap.cpp
60 lines (51 loc) · 1.12 KB
/
layeredmap.cpp
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
#include "layeredmap.h"
#include <map>
#include <list>
#include <assert.h>
class LayeredMapPrivate {
public:
typedef std::list<std::map<std::string, void*> > MapList;
MapList maps;
LayeredMapPrivate()
{
maps.resize(1);
}
};
LayeredMap::LayeredMap()
{
d = new LayeredMapPrivate();
}
LayeredMap::~LayeredMap()
{
delete d;
}
void LayeredMap::add(const std::string &name, void *value)
{
d->maps.back().insert(std::make_pair(name, value));
}
void *LayeredMap::lookup(const std::string &name)
{
for (LayeredMapPrivate::MapList::reverse_iterator i = d->maps.rbegin();
i != d->maps.rend(); i++) {
std::map<std::string, void*>::iterator entry = (*i).find(name);
if (entry != (*i).end())
return (*entry).second;
}
return NULL;
}
void *LayeredMap::lookup_last_layer(const std::string &name)
{
std::map<std::string, void*>::iterator entry = d->maps.back().find(name);
if (entry != d->maps.back().end())
return (*entry).second;
return NULL;
}
void LayeredMap::newLayer()
{
d->maps.push_back(std::map<std::string, void*>());
}
void LayeredMap::removeLastLayer()
{
assert(d->maps.size() >= 1);
d->maps.pop_back();
}