cfsm.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __CFSM_H
00021 #define __CFSM_H
00022
00023 #include "cownedobject.h"
00024
00025 NAMESPACE_BEGIN
00026
00032
00037 #define FSM_MAXT 64
00038
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088 #define FSM_Switch(fsm) \
00089 for (int __i=1, __savedstate; \
00090 (__i<3 || (__i&1)==0 || (fsm).isInTransientState()) && \
00091 (__i<2*FSM_MAXT || (opp_error(eINFLOOP,(fsm).getStateName()),0)); \
00092 ((__i&1)==0 && __savedstate!=(fsm).getState() && \
00093 (opp_error(eSTATECHG,(fsm).getStateName()),0)), \
00094 __savedstate=(fsm).getState(),++__i) \
00095 switch (FSM_Print(fsm,__i&1),(((fsm).getState()<<1)|(__i&1)))
00096
00116 #define FSM_Transient(state) (-(state))
00117
00125 #define FSM_Steady(state) (state)
00126
00134 #define FSM_EnterExit(state) ( ((state)<0) ? -((-state)<<1) : (state)<<1 )
00135
00144 #define FSM_Enter(state) (FSM_EnterExit(state)|0)
00145
00153 #define FSM_Exit(state) (FSM_EnterExit(state)|1)
00154
00165 #define FSM_Goto(fsm,state) (fsm).setState(state,#state)
00166
00167 #ifdef FSM_DEBUG
00168
00175 #define FSM_Print(fsm,exiting) \
00176 (ev << "FSM " << (fsm).getName() \
00177 << ((exiting) ? ": leaving state " : ": entering state ") \
00178 << (fsm).getStateName() << endl)
00179
00180
00181 #else
00182 #define FSM_Print(fsm,entering) ((void)0)
00183 #endif
00184
00186
00187
00188
00196 class SIM_API cFSM : public cOwnedObject
00197 {
00198 private:
00199
00200
00201
00202
00203
00204
00205 int _state;
00206 const char *_statename;
00207
00208 private:
00209 void copy(const cFSM& other);
00210
00211 public:
00214
00218 explicit cFSM(const char *name=NULL);
00219
00223 cFSM(const cFSM& other) : cOwnedObject(other) {copy(other);}
00224
00229 cFSM& operator=(const cFSM& vs);
00231
00234
00239 virtual cFSM *dup() const {return new cFSM(*this);}
00240
00245 virtual std::string info() const;
00246
00252 virtual void parsimPack(cCommBuffer *buffer);
00253
00259 virtual void parsimUnpack(cCommBuffer *buffer);
00261
00264
00268 int getState() const {return _state;}
00269
00273 const char *getStateName() const {return _statename?_statename:"";}
00274
00278 int isInTransientState() const {return _state<0;}
00279
00290 void setState(int state, const char *stn=NULL) {_state=state;_statename=stn;}
00292 };
00293
00294 NAMESPACE_END
00295
00296
00297 #endif