-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStateMachineTree.h
More file actions
176 lines (144 loc) · 3.12 KB
/
StateMachineTree.h
File metadata and controls
176 lines (144 loc) · 3.12 KB
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
#pragma once
#ifndef SNL_ASSERT
#include <assert.h>
#define SNL_ASSERT assert
#endif
namespace SNL
{
typedef unsigned short StateDataType;
/*configurable max depth of the state machine tree*/
const static int MaxStateStackDepth = 64;
/*a hierarchical state machine tree implementation,support fake coroutine */
class StateMachineTree
{
public:
struct ScopedStateStackPointer
{
public:
ScopedStateStackPointer(StateMachineTree * InStateMachine)
{
InStateMachine->StepInState();
CurrentStateMachine = InStateMachine;
}
~ScopedStateStackPointer()
{
CurrentStateMachine->StepOutState();
}
protected:
StateMachineTree * CurrentStateMachine;
};
public:
StateMachineTree()
{
Reset();
}
virtual ~StateMachineTree() {}
StateDataType& CurrentLevelState()
{
return this->StateStack[CurrentLevel];
}
void GotoState(StateDataType b)
{
this->StateStack[CurrentLevel] = b;
this->StateStack[CurrentLevel+1] = 0;
}
void GotoState(StateDataType b, int level)
{
if (level < 0)
{
level = CurrentLevel + level;
}
SNL_ASSERT(level < MaxStateStackDepth);
SNL_ASSERT(level >= 0);
this->StateStack[level] = b;
this->StateStack[level + 1] = 0;
}
void ConfirmState(StateDataType b)
{
if (CurrentLevelState() != b)
{
GotoState(b);
}
MaxStackDepth = CurrentLevel > MaxStackDepth ? CurrentLevel : MaxStackDepth;
}
StateDataType GetState(int level) const
{
if (level < 0)
{
level = CurrentLevel + level;
}
return this->StateStack[level];
}
void StepInState()
{
CurrentLevel++;
SNL_ASSERT(CurrentLevel < MaxStateStackDepth);
}
void StepOutState()
{
CurrentLevel--;
SNL_ASSERT(CurrentLevel >= 0);
}
void DebugState(const char* name)
{
DebugStateStack[CurrentLevel] = name;
LastDebugStackDepth = CurrentLevel;
}
void SetStackDepth(int i)
{
CurrentLevel = i;
MaxStackDepth = i;
}
void Reset()
{
memset(StateStack, 0, sizeof(StateStack));
memset(DebugStateStack, 0, sizeof(DebugStateStack));
CurrentLevel = LastDebugStackDepth = 0;
}
public:
int CurrentLevel;
int MaxStackDepth;
StateDataType StateStack[MaxStateStackDepth];
const char * DebugStateStack[MaxStateStackDepth];
int LastDebugStackDepth;
};
}
#define SMT_REMINDER_STR(x) #x
#define SMT_MACRO_TO_STRING(x) SMT_REMINDER_STR(x)
#define BGN_SMT \
SetStackDepth(0); \
{ \
switch (CurrentLevelState()) \
{ \
case 0:
#define END_SMT \
} \
}
#define BGN_STATE(state) \
}\
case state: \
{ \
ConfirmState( state); \
DebugState( SMT_MACRO_TO_STRING(state) ); \
{ \
ScopedStateStackPointer pt(this); \
switch (CurrentLevelState()) \
{ \
case 0:
#define END_STATE \
} \
break; \
} \
} \
{
#define SMT_POINT(state, condition) \
} \
case state: \
{ \
ConfirmState(state); \
DebugState( #state ); \
if (condition) break;
#define SMT_POINT2(state, condition) SMT_POINT(state, condition)
#define SMT_CHECK_POINT SMT_POINT2(__LINE__,false)
#define SMT_PAUSE_IF(condition) SMT_POINT2(__LINE__,condition)
#define BGN_NEW_STATE BGN_STATE(__LINE__)