-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.h
50 lines (38 loc) · 1016 Bytes
/
context.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
#pragma once
#include <vector>
#include <unordered_map>
#include <string>
#include <iostream>
template <typename T>
class Context {
private:
std::vector<std::unordered_map<std::string, T>> scopes;
public:
Context() {
pushScope();
};
void pushScope() {
scopes.push_back(std::unordered_map<std::string, T>());
};
void popScope() {
scopes.pop_back();
}
std::unordered_map<std::string, T> getLastScope() {
return scopes.back();
}
bool add(std::string id, T element) {
if (id == "_") {
return false;
}
return scopes.back()[id] = element;
}
T find(std::string name) {
for (auto scope = scopes.rbegin(); scope != scopes.rend(); ++scope) {
if ((*scope).count(name)) return (*scope)[name];
}
return nullptr;
};
T findAtLastScope(std::string name) {
return scopes.back().count(name)? scopes.back()[name] : nullptr;
}
};