-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStateMachine.cpp
185 lines (152 loc) · 4.92 KB
/
StateMachine.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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "stdgroup.h"
#include "StateMachine.h"
//----------------------------------------------------------------------------
// StateMachine
//----------------------------------------------------------------------------
StateMachine::StateMachine(BYTE maxStates, BYTE initialState) :
MAX_STATES(maxStates),
m_currentState(initialState),
m_newState(FALSE),
m_eventGenerated(FALSE),
m_pEventData(NULL)
{
ASSERT_TRUE(MAX_STATES < EVENT_IGNORED);
}
//----------------------------------------------------------------------------
// ExternalEvent
//----------------------------------------------------------------------------
void StateMachine::ExternalEvent(BYTE newState, const EventData* pData)
{
// If we are supposed to ignore this event
if (newState == EVENT_IGNORED)
{
// Just delete the event data, if any
if (pData != NULL)
delete pData;
}
else
{
// TODO - capture software lock here for thread-safety if necessary
// Generate the event
InternalEvent(newState, pData);
// Execute the state engine. This function call will only return
// when all state machine events are processed.
StateEngine();
// TODO - release software lock here
}
}
//----------------------------------------------------------------------------
// InternalEvent
//----------------------------------------------------------------------------
void StateMachine::InternalEvent(BYTE newState, const EventData* pData)
{
if (pData == NULL)
pData = new NoEventData();
m_pEventData = pData;
m_eventGenerated = TRUE;
m_newState = newState;
}
//----------------------------------------------------------------------------
// StateEngine
//----------------------------------------------------------------------------
void StateMachine::StateEngine(void)
{
const StateMapRow* pStateMap = GetStateMap();
if (pStateMap != NULL)
StateEngine(pStateMap);
else
{
const StateMapRowEx* pStateMapEx = GetStateMapEx();
if (pStateMapEx != NULL)
StateEngine(pStateMapEx);
else
ASSERT();
}
}
//----------------------------------------------------------------------------
// StateEngine
//----------------------------------------------------------------------------
void StateMachine::StateEngine(const StateMapRow* const pStateMap)
{
const EventData* pDataTemp = NULL;
// While events are being generated keep executing states
while (m_eventGenerated)
{
// Error check that the new state is valid before proceeding
ASSERT_TRUE(m_newState < MAX_STATES);
// Get the pointer from the state map
const StateBase* state = pStateMap[m_newState].State;
// Copy of event data pointer
pDataTemp = m_pEventData;
// Event data used up, reset the pointer
m_pEventData = NULL;
// Event used up, reset the flag
m_eventGenerated = FALSE;
// Switch to the new current state
SetCurrentState(m_newState);
// Execute the state action passing in event data
ASSERT_TRUE(state != NULL);
state->InvokeStateAction(this, pDataTemp);
// If event data was used, then delete it
if (pDataTemp)
{
delete pDataTemp;
pDataTemp = NULL;
}
}
}
//----------------------------------------------------------------------------
// StateEngine
//----------------------------------------------------------------------------
void StateMachine::StateEngine(const StateMapRowEx* const pStateMapEx)
{
const EventData* pDataTemp = NULL;
// While events are being generated keep executing states
while (m_eventGenerated)
{
// Error check that the new state is valid before proceeding
ASSERT_TRUE(m_newState < MAX_STATES);
// Get the pointers from the state map
const StateBase* state = pStateMapEx[m_newState].State;
const GuardBase* guard = pStateMapEx[m_newState].Guard;
const EntryBase* entry = pStateMapEx[m_newState].Entry;
const ExitBase* exit = pStateMapEx[m_currentState].Exit;
// Copy of event data pointer
pDataTemp = m_pEventData;
// Event data used up, reset the pointer
m_pEventData = NULL;
// Event used up, reset the flag
m_eventGenerated = FALSE;
// Execute the guard condition
BOOL guardResult = TRUE;
if (guard != NULL)
guardResult = guard->InvokeGuardCondition(this, pDataTemp);
// If the guard condition succeeds
if (guardResult == TRUE)
{
// Transitioning to a new state?
if (m_newState != m_currentState)
{
// Execute the state exit action on current state before switching to new state
if (exit != NULL)
exit->InvokeExitAction(this);
// Execute the state entry action on the new state
if (entry != NULL)
entry->InvokeEntryAction(this, pDataTemp);
// Ensure exit/entry actions didn't call InternalEvent by accident
ASSERT_TRUE(m_eventGenerated == FALSE);
}
// Switch to the new current state
SetCurrentState(m_newState);
// Execute the state action passing in event data
ASSERT_TRUE(state != NULL);
state->InvokeStateAction(this, pDataTemp);
}
// If event data was used, then delete it
if (pDataTemp)
{
delete pDataTemp;
pDataTemp = NULL;
}
}
}