00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef __CFSM_H
00020 #define __CFSM_H
00021
00022 #include "cobject.h"
00023
00029
00034 #define FSM_MAXT 64
00035
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 #define FSM_Switch(fsm) \
00086 for (int __i=1, __savedstate; \
00087 (__i<3 || (__i&1)==0 || (fsm).inTransientState()) && \
00088 (__i<2*FSM_MAXT || (opp_error(eINFLOOP,(fsm).stateName()),0)); \
00089 ((__i&1)==0 && __savedstate!=(fsm).state() && \
00090 (opp_error(eSTATECHG,(fsm).stateName()),0)), \
00091 __savedstate=(fsm).state(),++__i) \
00092 switch (FSM_Print(fsm,__i&1),(((fsm).state()<<1)|(__i&1)))
00093
00113 #define FSM_Transient(state) (-(state))
00114
00122 #define FSM_Steady(state) (state)
00123
00132 #define FSM_Enter(state) ((state)<<1)
00133
00141 #define FSM_Exit(state) (((state)<<1)|1)
00142
00153 #define FSM_Goto(fsm,state) (fsm).setState(state,#state)
00154
00155 #ifdef FSM_DEBUG
00156
00163 #define FSM_Print(fsm,exiting) \
00164 (ev << "FSM " << (fsm).name() \
00165 << ((exiting) ? ": leaving state " : ": entering state ") \
00166 << (fsm).stateName() << endl)
00167
00168
00169 #else
00170 #define FSM_Print(fsm,entering) ((void)0)
00171 #endif
00172
00174
00175
00176
00184 class SIM_API cFSM : public cObject
00185 {
00186 private:
00187
00188
00189
00190
00191
00192
00193 int _state;
00194 const char *_statename;
00195
00196 public:
00199
00203 explicit cFSM(const char *name=NULL);
00204
00208 cFSM(const cFSM& vs) {setName(vs.name());operator=(vs);}
00209
00214 cFSM& operator=(const cFSM& vs);
00216
00219
00224 virtual cObject *dup() const {return new cFSM(*this);}
00225
00230 virtual void info(char *buf);
00231
00236 virtual void writeContents(ostream& os);
00237
00243 virtual int netPack();
00244
00250 virtual int netUnpack();
00252
00255
00259 int state() const {return _state;}
00260
00264 const char *stateName() const {return _statename?_statename:"";}
00265
00269 int inTransientState() const {return _state<0;}
00270
00281 void setState(int state, const char *stn=NULL) {_state=state;_statename=stn;}
00283 };
00284
00285 #endif