OMNeT++ Simulation Library  5.6.1
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 
25 
30 #define FSM_MAXT 64
31 
72 //
73 // operation:
74 // - __i counts up (starting from 1) until the FSM reaches a steady state.
75 // - at __i=1,3,5,7,etc, FSM_Exit code is executed
76 // - at __i=2,4,6,8,etc, FSM_Enter code is executed
77 // - FSM_Enter code must not contain state change (this is verified)
78 // - state changes should be encoded in FSM_Exit code
79 // - infinite loops (when control never reaches steady state) are detected (FSM_MAXT)
80 //
81 #define FSM_Switch(fsm) \
82  for (int __i=1, __savedstate; \
83  (__i<3 || (__i&1)==0 || (fsm).isInTransientState()) && \
84  (__i<2*FSM_MAXT || (throw cRuntimeError(E_INFLOOP,(fsm).getStateName()),0)); \
85  ((__i&1)==0 && __savedstate!=(fsm).getState() && \
86  (throw cRuntimeError(E_STATECHG,(fsm).getStateName()),0)), \
87  __savedstate=(fsm).getState(),++__i) \
88  switch (FSM_Print(fsm,__i&1),(((fsm).getState()*2)|(__i&1)))
89 
109 #define FSM_Transient(state) (-(state))
110 
119 #define FSM_Steady(state) (state)
120 
130 #define FSM_Enter(state) (2*(state))
131 
139 #define FSM_Exit(state) (2*(state)|1)
140 
151 #define FSM_Goto(fsm,state) (fsm).setState(state,#state)
152 
153 #ifdef FSM_DEBUG
154 
161 #define FSM_Print(fsm,exiting) \
162  (EV << "FSM " << (fsm).getName() \
163  << ((exiting) ? ": leaving state " : ": entering state ") \
164  << (fsm).getStateName() << endl)
165 // this may also be useful as third line:
166 // << ((fsm).isInTransientState() ? "transient state " : "steady state ")
167 #else
168 #define FSM_Print(fsm,entering) ((void)0)
169 #endif
170 
172 
173 //-----------------------------------------------------
174 
182 class SIM_API cFSM : public cOwnedObject
183 {
184  private:
185  //
186  // About state codes:
187  // initial state is number 0
188  // negative state codes are transient states
189  // positive state codes are steady states
190  //
191  int state;
192  const char *stateName; // just a ptr to an external string
193 
194  private:
195  void copy(const cFSM& other);
196 
197  public:
200 
204  explicit cFSM(const char *name=nullptr);
205 
209  cFSM(const cFSM& other) : cOwnedObject(other) {copy(other);}
210 
215  cFSM& operator=(const cFSM& vs);
217 
220 
225  virtual cFSM *dup() const override {return new cFSM(*this);}
226 
231  virtual std::string str() const override;
232 
238  virtual void parsimPack(cCommBuffer *buffer) const override;
239 
245  virtual void parsimUnpack(cCommBuffer *buffer) override;
247 
250 
254  int getState() const {return state;}
255 
259  const char *getStateName() const {return stateName?stateName:"";}
260 
264  int isInTransientState() const {return state<0;}
265 
276  void setState(int state, const char *stateName=nullptr) {this->state=state;this->stateName=stateName;}
278 };
279 
280 } // namespace omnetpp
281 
282 
283 #endif
virtual cFSM * dup() const override
Definition: cfsm.h:225
int getState() const
Definition: cfsm.h:254
const char * getStateName() const
Definition: cfsm.h:259
int isInTransientState() const
Definition: cfsm.h:264
Store the state of an FSM. This class is used in conjunction with the FSM_Switch() and other FSM_ mac...
Definition: cfsm.h:182
Buffer for the communications layer of parallel simulation.
Definition: ccommbuffer.h:41
A cObject that keeps track of its owner. It serves as base class for many classes in the OMNeT++ libr...
Definition: cownedobject.h:104
Definition: cabstracthistogram.h:21
void setState(int state, const char *stateName=nullptr)
Definition: cfsm.h:276
cFSM(const cFSM &other)
Definition: cfsm.h:209