Main Page   Modules   Class Hierarchy   Compound List   File List   Compound Members  

cfsm.h

00001 //==========================================================================
00002 //   CFSM.H  -  header for
00003 //                             OMNeT++
00004 //            Discrete System Simulation in C++
00005 //
00006 //  Contents:
00007 //    FSM building macros
00008 //    class cFSM: stores the state of an FSM
00009 //
00010 //==========================================================================
00011 
00012 /*--------------------------------------------------------------*
00013   Copyright (C) 1992-2001 Andras Varga
00014   Technical University of Budapest, Dept. of Telecommunications,
00015   Stoczek u.2, H-1111 Budapest, Hungary.
00016 
00017   This file is distributed WITHOUT ANY WARRANTY. See the file
00018   `license' for details on this and other legal matters.
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 // operation:
00080 // - __i counts up (starting from 1) until the FSM reaches a steady state.
00081 // - at __i=1,3,5,7,etc, FSM_Exit code is executed
00082 // - at __i=2,4,6,8,etc, FSM_Enter code is executed
00083 // - FSM_Enter code must not contain state change (this is verified)
00084 // - state changes should be encoded in FSM_Exit code
00085 // - infinite loops (when control never reaches steady state) are detected (FSM_MAXT)
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 // this may also be useful as third line:
00171 //      << ((fsm).inTransientState() ? "transient state " : "steady state ")
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     // About state codes:
00192     //  initial state is number 0
00193     //  negative state codes are transient states
00194     //  positive state codes are steady states
00195     //
00196     int _state;
00197     const char *_statename;   // just a ptr to an external string
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

Generated at Sat May 4 15:45:48 2002 for OMNeT++ by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001