-
Notifications
You must be signed in to change notification settings - Fork 71
/
Alert.h
101 lines (83 loc) · 2.69 KB
/
Alert.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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#pragma once
#include "utils.h"
/* class Alert { */
/* shared_ptr<BlinkDetector> blinkdet; */
/* public: */
/* Alert(const shared_ptr<BlinkDetector>& blinkdet); */
/* void alert(const string& string); */
/* }; */
enum StateEventType {
EVENT_NOTHING, EVENT_SELECTED, EVENT_DESELECTED, EVENT_TICK,
EVENT_BLINK, EVENT_2BLINK};
template <class ReturnType> class StateNode;
template<class ReturnType>
struct NodeResult {
shared_ptr<StateNode<ReturnType> > nextNode;
shared_ptr<ReturnType> result;
NodeResult(const shared_ptr<StateNode<ReturnType> > &nextNode =
shared_ptr<StateNode<ReturnType> >(),
const shared_ptr<ReturnType> &result = shared_ptr<ReturnType>()):
nextNode(nextNode), result(result) {}
};
template<class ReturnType>
class StateNode {
protected:
int ticks;
public:
StateNode(): ticks(0) {}
virtual NodeResult<ReturnType> handleEvent(StateEventType event);
virtual ~StateNode() {};
};
template<class ReturnType>
class StateMachine: public StateNode<ReturnType>{
shared_ptr<StateNode<ReturnType> > currentNode;
public:
StateMachine(const shared_ptr<StateNode<ReturnType> >& initialNode);
virtual NodeResult<ReturnType> handleEvent(StateEventType event);
};
template<class ReturnType>
NodeResult<ReturnType> StateNode<ReturnType>::handleEvent(StateEventType event) {
switch(event) {
case EVENT_SELECTED:
case EVENT_DESELECTED: ticks = 0; break;
case EVENT_TICK: ticks++; break;
default:;
}
return NodeResult<ReturnType>();
}
template<class ReturnType>
StateMachine<ReturnType>::StateMachine(const shared_ptr<StateNode<ReturnType> >& initialNode):
currentNode(initialNode)
{
}
template<class ReturnType>
NodeResult<ReturnType> StateMachine<ReturnType>::handleEvent(StateEventType event) {
for(;;) {
NodeResult<ReturnType> result = currentNode->handleEvent(event);
if (result.result.get() != 0)
return result;
if (result.nextNode.get() == 0)
return result;
currentNode->handleEvent(EVENT_DESELECTED);
currentNode = result.nextNode;
event = EVENT_SELECTED;
}
}
class AlertWindow: public StateNode<void> {
Gtk::Window window;
string text;
public:
AlertWindow(const string &text);
virtual ~AlertWindow();
virtual NodeResult<void> handleEvent(StateEventType event);
};
/* class MessageNode: public StateNode { */
/* shared_ptr<StateNode> oknode, cancelnode; */
/* scoped_ptr<AlertWindow> alert; */
/* string text; */
/* public: */
/* MessageNode(const shared_ptr<StateNode>& oknode, */
/* const shared_ptr<StateNode>& cancelnode, */
/* const string& text); */
/* virtual shared_ptr<StateNode> handleEvent(StateEventType event); */
/* }; */