00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef __CFSM_H
00022 #define __CFSM_H
00023
00024 #include "cobject.h"
00025
00031
00036 #define FSM_MAXT 64
00037
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 #define FSM_Switch(fsm) \
00088 for (int __i=1, __savedstate; \
00089 (__i<3 || (__i&1)==0 || (fsm).inTransientState()) && \
00090 (__i<2*FSM_MAXT || (opp_error(eINFLOOP,(fsm).stateName()),0)); \
00091 ((__i&1)==0 && __savedstate!=(fsm).state() && \
00092 (opp_error(eSTATECHG,(fsm).stateName()),0)), \
00093 __savedstate=(fsm).state(),++__i) \
00094 switch (FSM_Print(fsm,__i&1),(((fsm).state()<<1)|(__i&1)))
00095
00115 #define FSM_Transient(state) (-(state))
00116
00124 #define FSM_Steady(state) (state)
00125
00134 #define FSM_Enter(state) ((state)<<1)
00135
00143 #define FSM_Exit(state) (((state)<<1)|1)
00144
00155 #define FSM_Goto(fsm,state) (fsm).setState(state,#state)
00156
00165 #ifdef FSM_DEBUG
00166 #define FSM_Print(fsm,exiting) \
00167 (ev << "FSM " << (fsm).name() \
00168 << ((exiting) ? ": exiting " : ": entering ") \
00169 << (fsm).stateName() << endl)
00170
00171
00172 #else
00173 #define FSM_Print(fsm,entering) ((void)0)
00174 #endif
00175
00177
00178
00179
00187 class SIM_API cFSM : public cObject
00188 {
00189 private:
00190
00191
00192
00193
00194
00195
00196 int _state;
00197 const char *_statename;
00198
00199 public:
00202
00206 explicit cFSM(const char *name=NULL);
00207
00211 cFSM(const cFSM& vs) {setName(vs.name());operator=(vs);}
00212
00217 cFSM& operator=(const cFSM& vs);
00219
00222
00226 virtual const char *className() const {return "cFSM";}
00227
00232 virtual cObject *dup() const {return new cFSM(*this);}
00233
00238 virtual void info(char *buf);
00239
00244 virtual const char *inspectorFactoryName() const {return "cFSMIFC";}
00245
00250 virtual void writeContents(ostream& os);
00251
00257 virtual int netPack();
00258
00264 virtual int netUnpack();
00266
00269
00273 int state() const {return _state;}
00274
00278 const char *stateName() const {return _statename?_statename:"";}
00279
00283 int inTransientState() const {return _state<0;}
00284
00295 void setState(int state, const char *stn=NULL) {_state=state;_statename=stn;}
00297 };
00298
00299 #endif