StateMachine.h
Go to the documentation of this file.
1 // -*- C++ -*-
19 #ifndef RTC_STATEMACHINE_H
20 #define RTC_STATEMACHINE_H
21 
22 #include <rtm/RTC.h>
23 #include <coil/Mutex.h>
24 #include <coil/Guard.h>
25 
26 namespace RTC_Utils
27 {
54  template <class State>
55  struct StateHolder
56  {
57  State curr;
58  State prev;
59  State next;
60  };
61 
258  template <class State,
259  class Listener,
260  class States = StateHolder<State>,
261  class Callback = void (Listener::*)(const States& states)
262  >
264  {
267  public:
285  StateMachine(int num_of_state)
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])
292  {
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);
298  m_transit = NULL;
299  };
300 
301 
302  virtual ~StateMachine()
303  {
304  delete [] m_entry;
305  delete [] m_predo;
306  delete [] m_do;
307  delete [] m_postdo;
308  delete [] m_exit;
309  };
310 
311 
329  void setNOP(Callback call_back)
330  {
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;
337  }
338 
356  void setListener(Listener* listener)
357  {
358  m_listener = listener;
359  }
360 
385  bool setEntryAction(State state, Callback call_back)
386  {
387  m_entry[state] = call_back;
388  return true;
389  }
390 
414  bool setPreDoAction(State state, Callback call_back)
415  {
416  m_predo[state] = call_back;
417  return true;
418  }
419 
443  bool setDoAction(State state, Callback call_back)
444  {
445  m_do[state] = call_back;
446  return true;
447  }
448 
472  bool setPostDoAction(State state, Callback call_back)
473  {
474  m_postdo[state] = call_back;
475  return true;
476  }
477 
501  bool setExitAction(State state, Callback call_back)
502  {
503  m_exit[state] = call_back;
504  return true;
505  }
506 
530  bool setTransitionAction(Callback call_back)
531  {
532  m_transit = call_back;
533  return true;
534  }
535 
553  void setStartState(States states)
554  {
555  m_states.curr = states.curr;
556  m_states.prev = states.prev;
557  m_states.next = states.next;
558  }
559 
580  States getStates()
581  {
582  Guard guard(m_mutex);
583  return m_states;
584  }
585 
603  State getState()
604  {
605  Guard guard(m_mutex);
606  return m_states.curr;
607  }
608 
630  bool isIn(State state)
631  {
632  Guard guard(m_mutex);
633  return m_states.curr == state ? true : false;
634  }
635 
662  void goTo(State state)
663  {
664  Guard guard(m_mutex);
665  m_states.next = state;
666  if (m_states.curr == state)
667  {
668  m_selftrans = true;
669  }
670  }
671 
672 
689  void worker()
690  {
691  States state;
692 
693  sync(state);
694 
695  if (state.curr == state.next)
696  {
697  // pre-do
698  if (m_predo[state.curr] != NULL)
699  (m_listener->*m_predo [state.curr])(state);
700 
701  if (need_trans()) return;
702 
703  // do
704  if (m_do[state.curr] != NULL)
705  (m_listener->*m_do [state.curr])(state);
706 
707  if (need_trans()) return;
708 
709  // post-do
710  if (m_postdo[state.curr] != NULL)
711  (m_listener->*m_postdo[state.curr])(state);
712  }
713  else
714  {
715  if (m_exit[state.curr] != NULL)
716  (m_listener->*m_exit[state.curr])(state);
717 
718  sync(state);
719 
720  if (state.curr != state.next)
721  {
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);
726  }
727  }
728  }
729 
730  protected:
750  void setNullFunc(Callback* s, Callback nullfunc)
751  {
752  for (int i = 0; i < m_num; ++i) s[i] = nullfunc;
753  }
754 
762  int m_num;
763 
771  Listener* m_listener;
772 
780  Callback* m_entry;
781 
789  Callback* m_predo;
790 
798  Callback* m_do;
799 
807  Callback* m_postdo;
808 
816  Callback* m_exit;
817 
825  Callback m_transit;
826 
834  States m_states;
836  Mutex m_mutex;
837 
838  private:
839  inline void sync(States& st)
840  {
841  Guard guard(m_mutex);
842  st = m_states;
843  }
844 
845  inline bool need_trans()
846  {
847  Guard guard(m_mutex);
848  return (m_states.curr != m_states.next);
849  }
850 
851  inline void update_curr(const State curr)
852  {
853  Guard guard(m_mutex);
854  m_states.curr = curr;
855  }
856  };
857 }; // namespace RTC_Utils
858 
859 #endif // RTC_STATEMACHINE_H
bool setDoAction(State state, Callback call_back)
Set Do action function.
Definition: StateMachine.h:443
bool setExitAction(State state, Callback call_back)
Set Exit action function.
Definition: StateMachine.h:501
Utility functions for RT-Component.
Definition: RTCUtil.cpp:22
Mutex class.
bool setPostDoAction(State state, Callback call_back)
Set PostDo action function.
Definition: StateMachine.h:472
States getStates()
Get states.
Definition: StateMachine.h:580
coil::Guard< Mutex > Guard
Definition: StateMachine.h:266
Callback * m_entry
Callback function for Entry action.
Definition: StateMachine.h:780
States m_states
Current state information.
Definition: StateMachine.h:834
int m_num
Number of state.
Definition: StateMachine.h:762
void worker()
Worker function.
Definition: StateMachine.h:689
bool setEntryAction(State state, Callback call_back)
Set Entry action function.
Definition: StateMachine.h:385
Callback * m_exit
Callback function for Exit action.
Definition: StateMachine.h:816
Callback * m_do
Callback function for Do action.
Definition: StateMachine.h:798
bool setTransitionAction(Callback call_back)
Set state transition action function.
Definition: StateMachine.h:530
State machine class.
Definition: StateMachine.h:263
void update_curr(const State curr)
Definition: StateMachine.h:851
void goTo(State state)
Transit State.
Definition: StateMachine.h:662
bool isIn(State state)
Check current state.
Definition: StateMachine.h:630
void setNOP(Callback call_back)
Set NOP function.
Definition: StateMachine.h:329
Listener * m_listener
Callback function for listener.
Definition: StateMachine.h:771
State holder class.
Definition: StateMachine.h:55
void sync(States &st)
Definition: StateMachine.h:839
bool setPreDoAction(State state, Callback call_back)
Set PreDo action function.
Definition: StateMachine.h:414
void setStartState(States states)
Set the initial state.
Definition: StateMachine.h:553
Callback * m_predo
Callback function for PreDo action.
Definition: StateMachine.h:789
Callback * m_postdo
Callback function for PostDo action.
Definition: StateMachine.h:807
RTComponent header.
Guard template class.
Definition: Guard.h:41
void setListener(Listener *listener)
Set Listener Object.
Definition: StateMachine.h:356
Callback m_transit
Callback function for State transition action.
Definition: StateMachine.h:825
StateMachine(int num_of_state)
Constructor.
Definition: StateMachine.h:285
void setNullFunc(Callback *s, Callback nullfunc)
Set NOP function.
Definition: StateMachine.h:750
State getState()
Get current state.
Definition: StateMachine.h:603


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Mon Jun 10 2019 14:07:56