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