31#ifndef ASYNC_STATE_MACHINE_INCLUDED
32#define ASYNC_STATE_MACHINE_INCLUDED
87template <
class ContextT,
class TopStateT>
class StateTopBase;
155template <
class ContextT,
class StateTopT>
173 assert(m_state !=
nullptr);
180 assert(m_state !=
nullptr);
240 template <
class NewStateT>
243 state->setMachine(
this);
244 StateTopT* old_state = m_state;
245 if ((old_state !=
nullptr) && (
typeid(NewStateT) == old_state->typeId()))
251 if (old_state != m_state)
256#ifdef ASYNC_STATE_MACHINE_DEBUG
257 std::cout <<
"### StateMachine: ";
258 std::cout << (old_state !=
nullptr ? old_state->name() :
"NULL");
259 std::cout <<
" -> " <<
state->name() << std::endl;
261 if (old_state !=
nullptr)
266 static_cast<StateTopBaseT*
>(m_state)->entryHandler(old_state);
283 return typeid(T) == m_state->typeId();
290 StateTopT&
state(
void) {
return *m_state; }
302#ifdef ASYNC_STATE_MACHINE_DEBUG
303 std::cout <<
"### StateMachine: setTimeout(" << timeout_ms <<
")"
334#ifdef ASYNC_STATE_MACHINE_DEBUG
335 std::cout <<
"### StateMachine: clearTimeout()" << std::endl;
352 StateTopT* m_state =
nullptr;
353 ContextT* m_ctx =
nullptr;
380template <
class ParentT,
class T>
388 virtual const std::type_info&
typeId(
void)
const override
393 virtual const char*
name(
void)
const {
return T::NAME; }
401 dynamic_cast<T*
>(
this)->
init();
410 if (
dynamic_cast<T*
>(from) ==
nullptr)
412 ParentT::entryHandler(from);
413 dynamic_cast<T*
>(
this)->
entry();
423 if (
dynamic_cast<T*
>(to) ==
nullptr)
425 dynamic_cast<T*
>(
this)->
exit();
426 ParentT::exitHandler(to);
481 assert(!
"Async::StateBase: Unhandled timeoutEvent");
486 assert(!
"Async::StateBase: Unhandled timeoutAtEvent");
512template <
class ContextT,
class TopStateT>
540 ContextT&
ctx(
void) {
return m_sm->
ctx(); }
548 template <
class NewStateT>
594 virtual const std::type_info&
typeId(
void)
const = 0;
A timer that times out at a specified absolute time.
Contains a single shot or periodic timer that emits a signal on timeout.
A timer that times out at a specified absolute time.
sigc::signal< void, AtTimer * > expired
A signal that is emitted when the timer expires.
bool start(void)
Start the timer.
void setExpireOffset(int offset_ms)
Set the expire offset.
void stop(void)
Stop the timer.
bool setTimeout(time_t t)
Set the timeout time.
Implements a hierarchial state machine.
virtual void exitHandler(typename ParentT::StateT *to) override
Handle calling the exit function on state transitions.
virtual const char * name(void) const
virtual void timeoutEvent(void) override
An event function that will be called when a timeout occurs.
virtual void initHandler(void) override
Handle calling the init function on state transitions.
virtual void timeoutAtEvent(void) override
virtual void entryHandler(typename ParentT::StateT *from) override
Handle calling the entry function on state transitions.
virtual const std::type_info & typeId(void) const override
Get the typeid for this state.
void entry(void)
Called when a state is entered.
void init(void)
Called before a transition from one state to another.
Implements a hierarchial state machine.
void setTimeoutAt(struct tm &tm, int expire_offset=0)
Set a timeout after which the timeoutAtEvent is issued.
void setState(NewStateT *state=new NewStateT)
Switch to the given state.
void clearTimeoutAt(void)
Clear a pending absolute time timeout.
void clearTimeout(void)
Clear a pending timeout.
void start(void)
Start the state machine.
~StateMachine(void)
Destructor.
ContextT & ctx(void)
Get the context object.
StateTopT & state(void)
Get the active state.
bool isActive(void) const
Check if the given state is the active one.
StateMachine(ContextT *ctx)
Constructor.
StateMachine & operator=(const StateMachine &)=delete
Disallow copy assignment.
StateMachine(const StateMachine &)=delete
Disallow copy construction.
void setTimeout(int timeout_ms)
Set a timeout after which the timeoutEvent is issued.
The base class for the top state of a state machine.
void setTimeoutAt(struct tm &tm, int expire_offset=0)
Set a timeout after which the timeoutAtEvent is issued.
virtual void entryHandler(StateT *from)
Run all entry handlers in the state hierarchy, top to bottom.
virtual void exitHandler(StateT *to)
Run all exit handlers in the state hierarchy, bottom to top.
TopStateT StateT
A type alias to access the top state type.
virtual void timeoutAtEvent(void)=0
Event function called when an absolute time timeout occurs.
void setTimeout(int timeout_ms)
Set a timeout after which the timeoutEvent is issued.
virtual void timeoutEvent(void)=0
An event function that will be called when a timeout occurs.
StateMachine< ContextT, StateT > StateMachineT
A type alias to simplify usage of the state machine type.
virtual const std::type_info & typeId(void) const =0
Get the typeid for this state.
ContextT & ctx(void)
Get the context object.
virtual ~StateTopBase(void)
Destructor.
void setState(void)
Transition to the given state.
void clearTimeoutAt(void)
Clear a pending absolute time timeout.
virtual void initHandler(void)=0
Run the init functon in a state.
void clearTimeout(void)
Clear a pending timeout.
A class that produces timer events.
void setTimeout(int timeout_ms)
Set (change) the timeout value.
void setEnable(bool do_enable)
Enable or disable the timer.
sigc::signal< void, Timer * > expired
A signal that is emitted when the timer expires.
Namespace for the asynchronous programming classes.