19 #ifndef RTC_STATEMACHINE_H 20 #define RTC_STATEMACHINE_H 55 template <
class State>
259 template <
class State,
262 class Callback = void (Listener::*)(
const States& states)
287 : m_num(num_of_state),
289 m_entry(m_num, (Callback)NULL),
290 m_predo(m_num, (Callback)NULL),
291 m_do(m_num, (Callback)NULL),
292 m_postdo(m_num, (Callback)NULL),
293 m_exit(m_num, (Callback)NULL),
303 : m_num(other.m_num),
304 m_listener(other.m_listener),
305 m_entry (other.m_entry),
306 m_predo (other.m_predo),
308 m_postdo(other.m_postdo),
309 m_exit (other.m_exit),
310 m_transit(other.m_transit),
311 m_states(other.m_states),
312 m_selftrans(other.m_selftrans)
325 std::swap(m_num, other.
m_num);
327 std::swap(m_entry, other.
m_entry);
328 std::swap(m_predo, other.
m_predo);
329 std::swap(m_do, other.
m_do);
330 std::swap(m_postdo, other.
m_postdo);
331 std::swap(m_exit, other.
m_exit);
333 std::swap(m_states, other.
m_states);
355 setNullFunc(m_entry, call_back);
356 setNullFunc(m_do, call_back);
357 setNullFunc(m_exit, call_back);
358 setNullFunc(m_predo, call_back);
359 setNullFunc(m_postdo, call_back);
360 m_transit = call_back;
382 assert(listener != NULL);
383 m_listener = listener;
414 m_entry.at(state) = call_back;
451 m_predo.at(state) = call_back;
488 m_do.at(state) = call_back;
525 m_postdo.at(state) = call_back;
562 m_exit.at(state) = call_back;
597 m_transit = call_back;
620 m_states.curr = states.curr;
621 m_states.prev = states.prev;
622 m_states.next = states.next;
647 Guard guard(m_mutex);
670 Guard guard(m_mutex);
671 return m_states.curr;
697 Guard guard(m_mutex);
698 return m_states.curr == state ?
true :
false;
729 Guard guard(m_mutex);
730 m_states.next = state;
731 if (m_states.curr == state)
760 if (state.curr == state.next)
763 if (m_predo[state.curr] != NULL)
764 (m_listener->*m_predo [state.curr])(state);
766 if (need_trans())
return;
769 if (m_do[state.curr] != NULL)
770 (m_listener->*m_do [state.curr])(state);
772 if (need_trans())
return;
775 if (m_postdo[state.curr] != NULL)
776 (m_listener->*m_postdo[state.curr])(state);
780 if (m_exit[state.curr] != NULL)
781 (m_listener->*m_exit[state.curr])(state);
785 if (state.curr != state.next)
787 state.curr = state.next;
788 if(m_entry[state.curr] != NULL)
789 (m_listener->*m_entry[state.curr])(state);
790 update_curr(state.curr);
806 if (state.curr == state.next)
808 if (m_predo[state.curr] != NULL)
810 (m_listener->*m_predo[state.curr])(state);
816 if (m_exit[state.curr] != NULL)
818 (m_listener->*m_exit[state.curr])(state);
821 if (state.curr != state.next)
823 state.curr = state.next;
824 if(m_entry[state.curr] != NULL)
826 (m_listener->*m_entry[state.curr])(state);
828 update_curr(state.curr);
836 if (m_do[state.curr] != NULL)
838 (m_listener->*m_do[state.curr])(state);
846 if (m_postdo[state.curr] != NULL)
848 (m_listener->*m_postdo[state.curr])(state);
876 for (
size_t i(0); i < m_num; ++i)
878 s.push_back(nullfunc);
966 inline void sync(States& st)
968 Guard guard(m_mutex);
972 inline bool need_trans()
974 Guard guard(m_mutex);
975 return (m_states.curr != m_states.next);
978 inline void update_curr(
const State
curr)
980 Guard guard(m_mutex);
981 m_states.curr =
curr;
986 #endif // RTC_STATEMACHINE_H virtual ~StateMachine()
Definition: StateMachine.h:298
bool setDoAction(State state, Callback call_back)
Set Do action function.
Definition: StateMachine.h:484
bool setExitAction(State state, Callback call_back)
Set Exit action function.
Definition: StateMachine.h:558
Utility functions for RT-Component.
Mutex class.
Definition: Mutex.h:40
bool setPostDoAction(State state, Callback call_back)
Set PostDo action function.
Definition: StateMachine.h:521
void swap(StateMachine &other)
Definition: StateMachine.h:323
States getStates()
Get states.
Definition: StateMachine.h:645
std::vector< Callback > m_predo
Callback function for PreDo action.
Definition: StateMachine.h:916
StateMachine(const StateMachine &other)
Definition: StateMachine.h:302
void worker_pre()
Definition: StateMachine.h:802
std::vector< Callback > m_do
Callback function for Do action.
Definition: StateMachine.h:925
States m_states
Current state information.
Definition: StateMachine.h:961
int m_num
Number of state.
Definition: StateMachine.h:889
std::vector< Callback > m_postdo
Callback function for PostDo action.
Definition: StateMachine.h:934
Mutex m_mutex
Definition: StateMachine.h:963
coil::Guard< coil::Mutex > Guard
Definition: LocalServiceAdmin.h:36
void worker()
Worker function.
Definition: StateMachine.h:754
bool setEntryAction(State state, Callback call_back)
Set Entry action function.
Definition: StateMachine.h:410
bool setTransitionAction(Callback call_back)
Set state transition action function.
Definition: StateMachine.h:595
State machine class.
Definition: StateMachine.h:264
void goTo(State state)
Transit State.
Definition: StateMachine.h:727
std::vector< Callback > m_exit
Callback function for Exit action.
Definition: StateMachine.h:943
bool isIn(State state)
Check current state.
Definition: StateMachine.h:695
void setNOP(Callback call_back)
Set NOP function.
Definition: StateMachine.h:353
State prev
Definition: StateMachine.h:59
Listener * m_listener
Callback function for listener.
Definition: StateMachine.h:898
State holder class.
Definition: StateMachine.h:56
void setNullFunc(std::vector< Callback > &s, Callback nullfunc)
Set NOP function.
Definition: StateMachine.h:872
bool setPreDoAction(State state, Callback call_back)
Set PreDo action function.
Definition: StateMachine.h:447
void setStartState(States states)
Set the initial state.
Definition: StateMachine.h:618
State curr
Definition: StateMachine.h:58
bool m_selftrans
Definition: StateMachine.h:962
StateMachine & operator=(const StateMachine &other)
Definition: StateMachine.h:316
void worker_do()
Definition: StateMachine.h:832
Guard template class.
Definition: Guard.h:41
State next
Definition: StateMachine.h:60
void setListener(Listener *listener)
Set Listener Object.
Definition: StateMachine.h:380
Callback m_transit
Callback function for State transition action.
Definition: StateMachine.h:952
StateMachine(int num_of_state)
Constructor.
Definition: StateMachine.h:286
State getState()
Get current state.
Definition: StateMachine.h:668
std::vector< Callback > m_entry
Callback function for Entry action.
Definition: StateMachine.h:907
void worker_post()
Definition: StateMachine.h:842