OMNeT++ Simulation Library  6.0.3
cfsm.h
1 //==========================================================================
2 // CFSM.H - header for
3 // OMNeT++/OMNEST
4 // Discrete System Simulation in C++
5 //
6 //==========================================================================
7 
8 /*--------------------------------------------------------------*
9  Copyright (C) 1992-2017 Andras Varga
10  Copyright (C) 2006-2017 OpenSim Ltd.
11 
12  This file is distributed WITHOUT ANY WARRANTY. See the file
13  `license' for details on this and other legal matters.
14 *--------------------------------------------------------------*/
15 
16 #ifndef __OMNETPP_CFSM_H
17 #define __OMNETPP_CFSM_H
18 
19 #include "cownedobject.h"
20 
21 namespace omnetpp {
22 
32 #define FSM_MAXT 64
33 
74 //
75 // operation:
76 // - __i counts up (starting from 1) until the FSM reaches a steady state.
77 // - at __i=1,3,5,7,etc, FSM_Exit code is executed
78 // - at __i=2,4,6,8,etc, FSM_Enter code is executed
79 // - FSM_Enter code must not contain state change (this is verified)
80 // - state changes should be encoded in FSM_Exit code
81 // - infinite loops (when control never reaches steady state) are detected (FSM_MAXT)
82 //
83 #define FSM_Switch(fsm) \
84  for (int __i=1, __savedstate; \
85  (__i<3 || (__i&1)==0 || (fsm).isInTransientState()) && \
86  (__i<2*FSM_MAXT || (throw cRuntimeError(E_INFLOOP,(fsm).getStateName()),0)); \
87  ((__i&1)==0 && __savedstate!=(fsm).getState() && \
88  (throw cRuntimeError(E_STATECHG,(fsm).getStateName()),0)), \
89  __savedstate=(fsm).getState(),++__i) \
90  switch (FSM_Print(fsm,__i&1),(((fsm).getState()*2)|(__i&1)))
91 
111 #define FSM_Transient(state) (-(state))
112 
121 #define FSM_Steady(state) (state)
122 
132 #define FSM_Enter(state) (2*(state))
133 
141 #define FSM_Exit(state) (2*(state)|1)
142 
153 #define FSM_Goto(fsm,state) (fsm).setState(state,#state)
154 
155 #ifdef FSM_DEBUG
156 
163 #define FSM_Print(fsm,exiting) \
164  (EV << "FSM " << (fsm).getName() \
165  << ((exiting) ? ": leaving state " : ": entering state ") \
166  << (fsm).getStateName() << endl)
167 // this may also be useful as third line:
168 // << ((fsm).isInTransientState() ? "transient state " : "steady state ")
169 #else
170 #define FSM_Print(fsm,entering) ((void)0)
171 #endif
172 
175 //-----------------------------------------------------
176 
184 class SIM_API cFSM : public cOwnedObject
185 {
186  private:
187  //
188  // About state codes:
189  // initial state is number 0
190  // negative state codes are transient states
191  // positive state codes are steady states
192  //
193  int state = 0;
194  const char *stateName = "INIT"; // just a ptr to an external string
195 
196  private:
197  void copy(const cFSM& other);
198  virtual void parsimPack(cCommBuffer *) const override {throw cRuntimeError(this, E_CANTPACK);}
199  virtual void parsimUnpack(cCommBuffer *) override {throw cRuntimeError(this, E_CANTPACK);}
200 
201  public:
204 
208  explicit cFSM(const char *name=nullptr);
209 
213  cFSM(const cFSM& other) : cOwnedObject(other) {copy(other);}
214 
219  cFSM& operator=(const cFSM& vs);
221 
224 
229  virtual cFSM *dup() const override {return new cFSM(*this);}
230 
235  virtual std::string str() const override;
237 
240 
244  int getState() const {return state;}
245 
249  const char *getStateName() const {return stateName?stateName:"";}
250 
254  int isInTransientState() const {return state<0;}
255 
266  void setState(int state, const char *stateName=nullptr) {this->state=state;this->stateName=stateName;}
268 };
269 
270 } // namespace omnetpp
271 
272 
273 #endif
omnetpp::cFSM::getState
int getState() const
Definition: cfsm.h:244
omnetpp::cFSM::isInTransientState
int isInTransientState() const
Definition: cfsm.h:254
omnetpp::cFSM::getStateName
const char * getStateName() const
Definition: cfsm.h:249
omnetpp::cFSM
Store the state of an FSM. This class is used in conjunction with the FSM_Switch() and other FSM_ mac...
Definition: cfsm.h:184
omnetpp::cFSM::cFSM
cFSM(const cFSM &other)
Definition: cfsm.h:213
omnetpp::cFSM::dup
virtual cFSM * dup() const override
Definition: cfsm.h:229
omnetpp::cRuntimeError
Thrown when the simulation kernel or other components detect a runtime error.
Definition: cexception.h:286
omnetpp::cFSM::setState
void setState(int state, const char *stateName=nullptr)
Definition: cfsm.h:266
omnetpp::cCommBuffer
Buffer for the communications layer of parallel simulation.
Definition: ccommbuffer.h:41
omnetpp::cOwnedObject
A cObject that keeps track of its owner. It serves as base class for many classes in the OMNeT++ libr...
Definition: cownedobject.h:105