19 #ifndef RTC_STATEMACHINE_H 20 #define RTC_STATEMACHINE_H 23 #include <coil/Mutex.h> 24 #include <coil/Guard.h> 54 template <
class State>
258 template <
class State,
261 class Callback = void (Listener::*)(
const States& states)
286 : m_num(num_of_state),
287 m_entry (new Callback[m_num]),
288 m_predo (new Callback[m_num]),
289 m_do (new Callback[m_num]),
290 m_postdo(new Callback[m_num]),
291 m_exit (new Callback[m_num])
293 setNullFunc(m_entry, NULL);
294 setNullFunc(m_do, NULL);
295 setNullFunc(m_exit, NULL);
296 setNullFunc(m_predo, NULL);
297 setNullFunc(m_postdo, NULL);
331 setNullFunc(m_entry, call_back);
332 setNullFunc(m_do, call_back);
333 setNullFunc(m_exit, call_back);
334 setNullFunc(m_predo, call_back);
335 setNullFunc(m_postdo, call_back);
336 m_transit = call_back;
358 m_listener = listener;
387 m_entry[
state] = call_back;
416 m_predo[
state] = call_back;
445 m_do[
state] = call_back;
474 m_postdo[
state] = call_back;
503 m_exit[
state] = call_back;
532 m_transit = call_back;
555 m_states.curr = states.curr;
556 m_states.prev = states.prev;
557 m_states.next = states.next;
582 Guard guard(m_mutex);
605 Guard guard(m_mutex);
606 return m_states.curr;
632 Guard guard(m_mutex);
633 return m_states.curr == state ?
true :
false;
664 Guard guard(m_mutex);
665 m_states.next =
state;
666 if (m_states.curr == state)
695 if (state.curr == state.next)
698 if (m_predo[state.curr] != NULL)
699 (m_listener->*m_predo [state.curr])(state);
701 if (need_trans())
return;
704 if (m_do[state.curr] != NULL)
705 (m_listener->*m_do [state.curr])(state);
707 if (need_trans())
return;
710 if (m_postdo[state.curr] != NULL)
711 (m_listener->*m_postdo[state.curr])(state);
715 if (m_exit[state.curr] != NULL)
716 (m_listener->*m_exit[state.curr])(state);
720 if (state.curr != state.next)
722 state.curr = state.next;
723 if(m_entry[state.curr] != NULL)
724 (m_listener->*m_entry[state.curr])(state);
725 update_curr(state.curr);
752 for (
int i = 0; i < m_num; ++i) s[i] = nullfunc;
841 Guard guard(m_mutex);
847 Guard guard(m_mutex);
848 return (m_states.curr != m_states.next);
853 Guard guard(m_mutex);
854 m_states.curr =
curr;
859 #endif // RTC_STATEMACHINE_H
bool setDoAction(State state, Callback call_back)
Set Do action function.
bool setExitAction(State state, Callback call_back)
Set Exit action function.
Utility functions for RT-Component.
bool setPostDoAction(State state, Callback call_back)
Set PostDo action function.
States getStates()
Get states.
coil::Guard< Mutex > Guard
Callback * m_entry
Callback function for Entry action.
States m_states
Current state information.
int m_num
Number of state.
void worker()
Worker function.
bool setEntryAction(State state, Callback call_back)
Set Entry action function.
Callback * m_exit
Callback function for Exit action.
Callback * m_do
Callback function for Do action.
bool setTransitionAction(Callback call_back)
Set state transition action function.
void update_curr(const State curr)
void goTo(State state)
Transit State.
bool isIn(State state)
Check current state.
void setNOP(Callback call_back)
Set NOP function.
Listener * m_listener
Callback function for listener.
bool setPreDoAction(State state, Callback call_back)
Set PreDo action function.
void setStartState(States states)
Set the initial state.
Callback * m_predo
Callback function for PreDo action.
Callback * m_postdo
Callback function for PostDo action.
void setListener(Listener *listener)
Set Listener Object.
Callback m_transit
Callback function for State transition action.
StateMachine(int num_of_state)
Constructor.
void setNullFunc(Callback *s, Callback nullfunc)
Set NOP function.
State getState()
Get current state.