FACT++  1.0
StateMachineImp Class Referenceabstract

Base class for a state machine implementation. More...

#include <StateMachineImp.h>

+ Inheritance diagram for StateMachineImp:
+ Collaboration diagram for StateMachineImp:

Public Types

enum  DefaultStates_t {
  kSM_KeepState = -42, kSM_NotAvailable = -2, kSM_NotReady = -1, kSM_Ready = 0,
  kSM_UserMode = 1, kSM_Error = 0x100, kSM_FatalError = 0xffff
- Public Types inherited from MessageImp
enum  Severity {
  kMessage = 10, kInfo = 20, kWarn = 30, kError = 40,
  kAlarm = 45, kFatal = 50, kComment = 90, kDebug = 99
 Severity of a message. More...

Public Member Functions

 StateMachineImp (std::ostream &out=std::cout, const std::string &name="")
 ~StateMachineImp ()
 delete all object stored in fListOfEvent and in fEventQueue More...
std::function< int(const EventImp &)> Wrap (const std::function< int(const EventImp &)> &func)
const std::string & GetName () const
EventImpSubscribe (const std::string &name)
void Unsubscribe (EventImp *evt)
int GetCurrentState () const
 return the current state of the machine More...
void SetReady ()
void SetNotReady ()
virtual int Run (bool dummy)
 Start the mainloop. More...
int Run ()
virtual void Stop (int code=0)
 Request to stop the mainloop. More...
bool IsRunning () const
 Used to check if the main loop is already running or still running. More...
void EnableBuffer (bool b=true)
 Used to enable or disable buffering of events outside of the main loop. More...
bool PostEvent (std::ostream &lout, const std::string &str)
 Post an event to the event queue. More...
bool PostEvent (const std::string &evt)
bool PostEvent (const EventImp &evt)
bool PostEvent (const EventImp &evt, const char *ptr, size_t siz)
bool HasEvent (const EventImp *cmd)
EventImpFindEvent (const std::string &evt)
bool IsQueueEmpty () const
const std::vector< std::string > GetEventNames ()
void PrintListOfEvents (std::ostream &out, const std::string &evt="")
void PrintListOfEvents (const std::string &str="")
void PrintListOfAllowedEvents (std::ostream &out)
void PrintListOfAllowedEvents ()
void PrintListOfStates (std::ostream &out) const
void PrintListOfStates () const
 Print a list of all states with descriptions. More...
int GetStateIndex (const std::string &name) const
bool HasState (int index) const
const std::string GetStateName (int state) const
const std::string GetStateName () const
const std::string GetStateDesc (int state) const
const std::string GetStateDesc () const
const std::string GetStateDescription (int state) const
const std::string GetStateDescription () const
- Public Member Functions inherited from MainImp
virtual ~MainImp ()
- Public Member Functions inherited from MessageImp
 MessageImp (std::ostream &out=std::cout)
virtual ~MessageImp ()
virtual void IndicateStateChange (const Time &, const std::string &)
void StateChanged (const Time &time, const std::string &server, const std::string &msg, int state)
virtual int Write (const Time &time, const std::string &txt, int qos=kMessage)
int Update (const std::string &txt, int severity=kMessage)
int Update (const char *txt, int severity=kMessage)
int Update (const std::ostringstream &str, int severity=kMessage)
int Debug (const std::string &str)
int Message (const std::string &str)
int Info (const std::string &str)
int Warn (const std::string &str)
int Error (const std::string &str)
int Alarm (const std::string &str)
int Fatal (const std::string &str)
int Comment (const std::string &str)
int Debug (const char *txt)
int Message (const char *txt)
int Info (const char *txt)
int Warn (const char *txt)
int Error (const char *txt)
int Alarm (const char *txt)
int Fatal (const char *txt)
int Comment (const char *txt)
int Debug (const std::ostringstream &str)
int Message (const std::ostringstream &str)
int Info (const std::ostringstream &str)
int Warn (const std::ostringstream &str)
int Alarm (const std::ostringstream &str)
int Error (const std::ostringstream &str)
int Fatal (const std::ostringstream &str)
int Comment (const std::ostringstream &str)
std::ostream & operator() () const
std::ostream & Out () const
virtual bool MessageQueueEmpty () const

Protected Member Functions

virtual void PushEvent (Event *cmd)
 This is a flag which is set true if the main loop should stop. More...
std::shared_ptr< EventPopEvent ()
 Pop a command from the fifo. More...
bool HandleNewState (int newstate, const EventImp *evt, const char *txt)
virtual int Execute ()
 Is called continously to execute actions in the current state. More...
bool HandleEvent (const EventImp &evt)
virtual std::string SetCurrentState (int state, const char *txt="", const std::string &cmd="")
EventImpAddEvent (const std::string &name, const std::string &states, const std::string &fmt)
EventImpAddEvent (const std::string &name, int s1=-1, int s2=-1, int s3=-1, int s4=-1, int s5=-1)
EventImpAddEvent (const std::string &name, const std::string &fmt, int s1=-1, int s2=-1, int s3=-1, int s4=-1, int s5=-1)
virtual bool AddStateName (const int state, const std::string &name, const std::string &doc="")
void SetDefaultStateNames ()

Protected Attributes

StateNames fStateNames
 Human readable names associated with the states. More...
bool fRunning
 Flag if events should be buffered outside the event loop. More...
int fExitRequested
 Machine is in main-loop. More...

Private Types

typedef std::map< const int, std::pair< std::string, std::string > > StateNames
 Current state of the state machine. More...

Private Member Functions

virtual EventImpCreateEvent (const std::string &name, const std::string &fmt)=0
 Is called when a configuration event is to be processed (no transition of state) More...
virtual EventImpCreateService (const std::string &)
virtual void Lock ()
virtual void UnLock ()
int Wrapper (const std::function< int(const EventImp &)> &f, const EventImp &imp)

Private Attributes

std::string fName
int fCurrentState
 Name of the state-machine / server (e.g. DRIVE) More...
std::vector< EventImp * > fListOfEvents
std::list< std::shared_ptr< Event > > fEventQueue
 List of available commands as setup by user. More...
std::mutex fMutex
 Event queue (fifo) for the received commands. More...
std::mutex fMutexEvt
 Mutex to ensure thread-safe access to the command fifo. More...
std::condition_variable fCond
 Mutex to ensure thread-safe access to the command fifo. More...
bool fBufferEvents
 Conditional to signal run the an event is waiting. More...

Detailed Description

Base class for a state machine implementation.

  • Red box: Internal states. Events which are received are discarded.
  • Black box: State machine running. Events are accepted and processed according to the implemented functions Transition(), Configuration() and Execute(). Events are accepted accoding to the lookup table of allowed transitions.
  • Red solid arrow: A transition initiated by the program itself.
  • Dashed arrows in general: Transitions which can be initiated by a dim-command or get inistiated by the program.
  • Solid arrows in general: These transitions are always initiated by the program.
  • Red dashed: Suggested RESET event (should be implemented by the derived class)
  • Black dashed arrow: Exit from the main loop. This can either happen by the Dim-provided EXIT-command or a call to StateMachineDim::Stop.
  • Black arrows: Other events or transitions which can be implemented by the derived class.
  • Dotted black arrow: Exit from the main-loop which is initiated by the program itself through StateMachineDim::Stop() and not by the state machine itself (Execute(), Configure() and Transition())
  • Blue dashed arrows: Transitions which happen either by receiving a event or are initiated from the state machine itself (by return values of (Execute(), Configure() and Transition())
  • Blue solid: Transitions which cannot be initiated by dim event but only by the state machine itself.
  • From the program point of view the fatal error is identical with the kSM_Configuring state, i.e. it is returned from the main-loop. Usually this will result in program termination. However, depending on the state the program might decide to use different cleaning routines.
  • A proper and correct cleanup after an EXIT or Stop() is missing. maybe we have to force a state 0 first?

Definition at line 17 of file StateMachineImp.h.

The documentation for this class was generated from the following files: