-
Notifications
You must be signed in to change notification settings - Fork 0
/
stm.hpp
268 lines (220 loc) · 5.58 KB
/
stm.hpp
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/**
* @brief limited status auto machine
* @version 0.1
* @date 2019-10-15
* @author 宋炜
*/
#pragma once
#include <vector>
#include <functional>
#include <map>
template< typename sType , typename condType >
class stm{
public:
enum emErrCode{
ERR_ALLOC_MEM,
ERR_FIND_STATUS,
ERR_NULL_FROM,
ERR_NULL_TO,
OK
};
struct stStatus;
/**
*/
typedef std::function< void ( bool , stStatus * , stStatus * ) > transFun;
/// define trans arc
struct stArc{
uint32_t m_id;
condType m_cond; // trig condition
stStatus * p_from;
stStatus * p_to;
/**
*
*/
bool transfrom( condType data , transFun fun ){
bool ret = false;
if( data == m_cond ){
fun( data , p_from , p_to );
ret = true;
}
return ret;
}
bool transform_if( condType data,
std::function<
bool ( const condType ,
const condType )> ifFun,
transFun fun ){
bool ret = false;
if( ifFun( data , m_cond ) == true ){
fun( true , p_from , p_to );
ret = true;
}
return ret;
}
stArc( uint32_t id , stStatus * from , stStatus * to ):
m_id( id ) , p_from( from ), p_to( to ){}
stArc():m_id( 0 ) , p_from( nullptr ) , p_to( nullptr ){}
stArc( const stArc& b ){
m_id = b.m_id;
p_from = b.p_from;
p_to = b.p_to;
m_cond = b.m_cond;
}
virtual ~stArc(){}
stArc& operator=( const stArc& b){
m_id = b.m_id;
p_from = b.p_from;
p_to = b.p_to;
m_cond = b.m_cond;
return *this;
}
};
/// 定义状态结构体
struct stStatus{
uint32_t m_id;
sType m_data;
std::vector< stArc > m_arcs;
stStatus():m_id( 0 ){}
stStatus( uint32_t id , const sType& data ){
m_id = id;
m_data = data;
}
stStatus( const stStatus& b){
m_id = m_id;
m_data = b.m_data;
}
stStatus& operator=( const stStatus& b ){
m_id = b.m_id;
m_data = b.m_data;
return *this;
}
void addArc( uint32_t id , const stArc& arc ){
m_arc.push_back( arc );
}
stm::emErrCode eraseArc( uint32_t id ){
stm::emErrCode e = stm::OK;
decltype( m_arcs.begin()) it;
for( it = m_arcs.begin(); it = m_arcs.end(); it ++ ){
if( it->m_id == id ){
m_arcs.erase( it );
}
}
if( it == m_arcs.end() ){
ret = stm::ERR_FIND_STATUS;
}
return ret;
}
bool for_each( std::function< bool ( stArc& ) > fun ){
for( auto it : m_arcs ){
bool ret = false;
ret = fun( it );
if( ret == true ) return ret;
}
return false;
}
};
protected:
uint32_t m_sid_pool;
uint32_t m_aid_pool;
stStatus * p_current;
stStatus * p_end;
std::map< uint32_t , stStatus* > m_status;
protected:
emErrCode add_arc( stStatus * from , stStatus * to ,const cType& cond ){
if( from == nullptr ) return ERR_NULL_FROM;
if( to == nullptr ) return ERR_NULL_TO;
stArc arc( m_aid_pool , from , to );
arc.m_cond = cond;
from->addArc( m_aid_pool , arc );
m_aid_pool ++;
return OK;
}
public:
stm():m_sid_pool( 0 ), m_aid_pool( 0 ), p_current( nullptr ) , p_end( nullptr ){}
virtual ~stm(){
for( auto i : m_status ){
if( i.second ){
delete i.second;
}
}
}
stStatus * add( const sType& data ){
stStatus * ret = nullptr;
try{
stStatus * s = new stStatus( m_sid_pool , data );
m_status.insert( std::make_pair( m_sid_pool , s ));
m_sid_pool ++;
ret = s;
}catch( std::bad_alloc& e ){
}
return ret;
}
emErrCode erase( uint32_t id ){
auto it = m_status.find( id );
if( it != m_status.end() ){
delete it->second;
return OK;
}
return ERR_FIND_STATUS;
}
emErrCode addArc( stStatus* from , stStatus * to , const condType& cond ){
return add_arc( from , to cond );
}
emErrCode eraseArc( uint32_t from , uint32_t arc ){
emErrCode ret = OK;
auto itF = m_status.find( from );
if( itF == m_status.end() ) return ERR_FIND_STATUS;
ret = *itF -> eraseArc( arc );
return OK;
}
emErrCode startStatus( uint32_t id = 0 ){
auto it = m_status.find( id );
if( it == m_status.end()) return ERR_FIND_STATUS;
p_current = it->second;
return OK;
}
emErrCode endStatus( uint32_t id ){
auto it = m_status.find( id );
if( it == m_status.end() ) return ERR_FIND_STATUS;
p_end = it->second;
return OK;
}
emErrCode endStatus( stStatus * end ){
if( end == nullptr )return ERR_FIND_STATUS;
p_end == end;
return OK;
}
bool isEnd( uint32_t id ){
auto it = m_status.find( id );
if( it == m_status.end() ) return ERR_FIND_STATUS;
return it->second == p_endl;
}
bool isEnd( stStatus * end ){
return end == p_end;
}
bool trigger( const condType& data , transFun fun ){
if( p_current == nullptr ) return false;
bool ret = p_current->for_each( [ this , dta , fun ]( stArc& arc )->bool{
bool rst = false;
rst = arc.transform( data , fun );
if( rst ){
p_current = arc.p_to;
}
});
return ret;
}
bool trigger_if( const condType& data ,
std::function< bool ( const condType& , const condType& ) > ifFun ,
transFun fun ){
if( p_current == nullptr ) return false;
bool rst = p_current-> for_each( [ this , &data , ifFun , fun ]( const stArc& arc )->bool{
bool rst = arc.transform_if( data , ifFun , fun );
if( rst == true ){
p_current = arc.p_to;
}
});
return rst;
}
sType& operator(){ return p_current->m_data; }
// 双河 锦阳之星 旁边还有一个7天 400-822-9999
};