1 #ifndef FACT_StateMachineImp 2 #define FACT_StateMachineImp 9 #include <condition_variable> 39 typedef std::map<const int, std::pair<std::string, std::string>>
StateNames;
84 const int rc = f(imp);
94 virtual std::string
SetCurrentState(
int state,
const char *txt=
"",
const std::string &cmd=
"");
97 EventImp &
AddEvent(
const std::string &name,
int s1=-1,
int s2=-1,
int s3=-1,
int s4=-1,
int s5=-1);
98 EventImp &
AddEvent(
const std::string &name,
const std::string &fmt,
int s1=-1,
int s2=-1,
int s3=-1,
int s4=-1,
int s5=-1);
100 virtual bool AddStateName(
const int state,
const std::string &name,
const std::string &doc=
"");
105 StateMachineImp(std::ostream &out=std::cout,
const std::string &name=
"");
108 std::function<int(const EventImp &)>
Wrap(
const std::function<
int(
const EventImp &)> &func)
125 virtual int Run(
bool dummy);
129 virtual void Stop(
int code=0);
138 bool PostEvent(std::ostream &lout,
const std::string &
str);
int GetStateIndex(const std::string &name) const
int fCurrentState
Name of the state-machine / server (e.g. DRIVE)
void PrintListOfAllowedEvents()
EventImp & AddEvent(const std::string &name, const std::string &states, const std::string &fmt)
int fExitRequested
Machine is in main-loop.
void EnableBuffer(bool b=true)
Used to enable or disable buffering of events outside of the main loop.
virtual void Stop(int code=0)
Request to stop the mainloop.
Mainloop running, state machine in operation.
int GetCurrentState() const
return the current state of the machine
A general base-class describing events issues in a state machine.
void PrintListOfStates() const
Print a list of all states with descriptions.
Base class for a state machine implementation.
std::list< std::shared_ptr< Event > > fEventQueue
List of available commands as setup by user.
bool PostEvent(std::ostream &lout, const std::string &str)
Post an event to the event queue.
The base implementation of a distributed messaging system.
const std::string & GetName() const
Fatal error: stop program.
bool HandleEvent(const EventImp &evt)
EventImp * FindEvent(const std::string &evt)
void SetDefaultStateNames()
bool HandleNewState(int newstate, const EventImp *evt, const char *txt)
bool HasEvent(const EventImp *cmd)
bool fRunning
Flag if events should be buffered outside the event loop.
const std::string GetStateDesc() const
First user defined mode (to be used in derived classes' enums)
virtual EventImp * CreateEvent(const std::string &name, const std::string &fmt)=0
Is called when a configuration event is to be processed (no transition of state)
Mainloop not running, state machine stopped.
bool HasState(int index) const
int Wrapper(const std::function< int(const EventImp &)> &f, const EventImp &imp)
bool PostEvent(const std::string &evt)
std::vector< EventImp * > fListOfEvents
const std::vector< std::string > GetEventNames()
EventImp & Subscribe(const std::string &name)
std::map< const int, std::pair< std::string, std::string > > StateNames
Current state of the state machine.
virtual void PushEvent(Event *cmd)
This is a flag which is set true if the main loop should stop.
const std::string GetStateDescription() const
StateNames fStateNames
Human readable names associated with the states.
Concerete implementation of an EventImp stroring name, format, data and time.
virtual std::string SetCurrentState(int state, const char *txt="", const std::string &cmd="")
void PrintListOfEvents(std::ostream &out, const std::string &evt="")
Error states should be between 0x100 and 0xffff.
Possible return value for GetStateIndex.
void Unsubscribe(EventImp *evt)
bool IsQueueEmpty() const
virtual bool AddStateName(const int state, const std::string &name, const std::string &doc="")
std::condition_variable fCond
Mutex to ensure thread-safe access to the command fifo.
const std::string GetStateName() const
std::mutex fMutexEvt
Mutex to ensure thread-safe access to the command fifo.
std::shared_ptr< Event > PopEvent()
Pop a command from the fifo.
bool IsRunning() const
Used to check if the main loop is already running or still running.
bool fBufferEvents
Conditional to signal run the an event is waiting.
virtual int Execute()
Is called continously to execute actions in the current state.
~StateMachineImp()
delete all object stored in fListOfEvent and in fEventQueue
StateMachineImp(std::ostream &out=std::cout, const std::string &name="")
virtual EventImp * CreateService(const std::string &)
std::mutex fMutex
Event queue (fifo) for the received commands.
std::function< int(const EventImp &)> Wrap(const std::function< int(const EventImp &)> &func)