forked from Rolso22/edsl_cw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattr_stack.h
49 lines (37 loc) · 976 Bytes
/
attr_stack.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
#ifndef EDSL_CW_ATTR_STACK_H
#define EDSL_CW_ATTR_STACK_H
struct AttrStackItemBase {
AttrStackItemBase *next;
AttrStackItemBase(AttrStackItemBase *next): next(next) {}
virtual ~AttrStackItemBase() {}
};
template <typename T>
struct AttrStackItem : public AttrStackItemBase {
T value;
AttrStackItem(T value, AttrStackItemBase *next)
: AttrStackItemBase(next)
, value(value)
{}
};
class AttrStack {
AttrStackItemBase *stack;
public:
AttrStack(): stack(nullptr) {}
template <typename T>
void push(T value) {
stack = new AttrStackItem<T>(value, stack);
}
template <typename T>
T pop() {
auto top = dynamic_cast<AttrStackItem<T>*>(stack);
stack = stack->next;
T value = top->value;
delete top;
return value;
}
};
struct ActionBase {
virtual ~ActionBase() {}
virtual void apply(AttrStack &stack) = 0;
};
#endif //EDSL_CW_ATTR_STACK_H