-
Notifications
You must be signed in to change notification settings - Fork 3
/
elevator.cpp
75 lines (66 loc) · 1.56 KB
/
elevator.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "msfsm.hpp"
#include <iostream>
using namespace msfsm;
using namespace std;
// Events
struct Request {
const int floor;
Request(int f) : floor(f) {}
};
class Elevator : public Fsm<Elevator> {
public:
Elevator() {
transition(ground);
}
private:
friend Fsm;
class State : public Fsm::State {
friend Fsm;
using Fsm::State::State;
virtual void event(Request) = 0;
};
class Roof : public State {
friend Fsm;
using State::State;
void entry() {
cout << "Entering roof" << endl;
}
void event(Request r) override {
if (r.floor < 3)
transition(fsm.middle, r);
}
} roof {this};
class Middle : public State {
friend Fsm;
using State::State;
void entry(Request r) {
cout << "Entering middle" << endl;
event(r);
}
void event(Request r) override {
if (r.floor < 2)
transition(fsm.ground);
if (r.floor > 2)
transition(fsm.roof);
}
} middle {this};
class Ground : public State {
friend Fsm;
using State::State;
void entry() {
cout << "Entering ground" << endl;
}
void event(Request r) override {
if (r.floor > 1)
transition(fsm.middle, r);
}
} ground {this};
};
int main(int argc, char *argv[])
{
Elevator e;
e.handle(Request(3));
e.handle(Request(1));
e.handle(Request(2));
return 0;
}