-
Notifications
You must be signed in to change notification settings - Fork 3
/
events.cpp
61 lines (53 loc) · 1.23 KB
/
events.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
61
#include "msfsm.hpp"
#include <iostream>
using namespace msfsm;
using namespace std;
// Events
class Up {};
class Down {};
class Elevator : public Fsm<Elevator> {
public:
Elevator() {
transition(lower);
}
private:
friend Fsm;
class State : public Fsm::State {
friend Fsm;
using Fsm::State::State;
virtual void event(Up) { cout << "Ignoring Up event" << endl; }
virtual void event(Down) { cout << "Ignoring Down event" << endl; }
};
class Upper : public State {
friend Fsm;
using State::State;
void entry() {
cout << "Entering upper" << endl;
}
void event(Down) override {
transition(fsm.lower);
}
} upper {this};
class Lower : public State {
friend Fsm;
using State::State;
void entry() {
cout << "Entering lower" << endl;
}
void event(Up) override {
transition(fsm.upper);
}
void exit() override {
cout << "Exiting lower" << endl;
}
} lower {this};
};
int main(int argc, char *argv[])
{
Elevator f;
f.handle(Up());
f.handle(Up());
f.handle(Down());
f.handle(Down());
return 0;
}