Public Member Functions | Protected Member Functions | Protected Attributes | Private Types | Private Member Functions | List of all members
RTC_Utils::StateMachine< State, Listener, States, Callback > Class Template Reference

State machine class. More...

#include <StateMachine.h>

Public Member Functions

State getState ()
 Get current state. More...
 
States getStates ()
 Get states. More...
 
void goTo (State state)
 Transit State. More...
 
bool isIn (State state)
 Check current state. More...
 
bool setDoAction (State state, Callback call_back)
 Set Do action function. More...
 
bool setEntryAction (State state, Callback call_back)
 Set Entry action function. More...
 
bool setExitAction (State state, Callback call_back)
 Set Exit action function. More...
 
void setListener (Listener *listener)
 Set Listener Object. More...
 
void setNOP (Callback call_back)
 Set NOP function. More...
 
bool setPostDoAction (State state, Callback call_back)
 Set PostDo action function. More...
 
bool setPreDoAction (State state, Callback call_back)
 Set PreDo action function. More...
 
void setStartState (States states)
 Set the initial state. More...
 
bool setTransitionAction (Callback call_back)
 Set state transition action function. More...
 
 StateMachine (int num_of_state)
 Constructor. More...
 
void worker ()
 Worker function. More...
 
virtual ~StateMachine ()
 

Protected Member Functions

void setNullFunc (Callback *s, Callback nullfunc)
 Set NOP function. More...
 

Protected Attributes

Callback * m_do
 Callback function for Do action. More...
 
Callback * m_entry
 Callback function for Entry action. More...
 
Callback * m_exit
 Callback function for Exit action. More...
 
Listener * m_listener
 Callback function for listener. More...
 
Mutex m_mutex
 
int m_num
 Number of state. More...
 
Callback * m_postdo
 Callback function for PostDo action. More...
 
Callback * m_predo
 Callback function for PreDo action. More...
 
bool m_selftrans
 
States m_states
 Current state information. More...
 
Callback m_transit
 Callback function for State transition action. More...
 

Private Types

typedef coil::Guard< MutexGuard
 
typedef coil::Mutex Mutex
 

Private Member Functions

bool need_trans ()
 
void sync (States &st)
 
void update_curr (const State curr)
 

Detailed Description

template<class State, class Listener, class States = StateHolder<State>, class Callback = void (Listener::*)(const States& states)>
class RTC_Utils::StateMachine< State, Listener, States, Callback >

State machine class.

StateMachine class is a class to realize a state machine.

Example: ActiveObject assumes to be an active object that has the state machine. There are three states such as INACTIVE, ACTIVE and ERROR state, and if you want to define Entry or Exit action, this class will realize as follows:

class ActiveObject 
{  
public: 
  enum MyState { INACTIVE, ACTIVE, ERROR }; 
  typedef States<MyState> MyStates;
  ActiveObject() 
    : m_sm(3) 
  { 
    m_sm.setNOP(&ActiveObject::nullAction); 
    m_sm.setListener(this);
    m_sm.setExitAction(INACTIVE, &ActiveObject::inactiveExit); 
      : 
    m_sm.setPostDoAction(ERROR, &ActiveObject::errorPostDo); 
    m_sm.setTransitionAction(&ActiveObject:transition); 
  };
  bool nullAction(MyStates st) {}; 
  bool inactiveExit(MyStates st) {}; 
    : 
  bool errorPostDo(MyStates st) {}; 
  bool transition(MyStates st) {};
private: 
  StateMachine<MyState, bool, ActiveObject> m_sm; 
}; 

If you want to give a class to some states, you must implement the class to satisfy the following conditions:

  1. You must define states by enum.
  2. Template arguments of StateMachine must be <type of state(MyState), listener object, state holderˇ¤callback function>
  3. Constructor arguments of StateMachine must be the number of the states.
  4. You must set the following action functions as a function of (Return function_name(States))
    1. You must define a function that does not do anything and give with setNOP.
    2. You must set actions to each state by set(Entry|PreDo|Do|PostDo|Exit)Action.
    3. You should set actions at the state transition by setTransitionAction().
  5. You must implement action at the transition based on given states, such as current state, next state and previous state.
  6. You should change states by goTo() and check the state by isIn(state).
  7. goTo() is a function that sets next state forcibly, therefore, to determine the next state, you must get current state and implement that logic.

In this class, you can define the following five actions for one state:

Transition action is an action invoked at the transition between any states, and you must define its behavior.

This class executes each action according to the following timing.

Parameters
StateType of the state
ListenerListener object for action
StatesState holder
CallbackCallback function for action
Since
0.4.0

Definition at line 263 of file StateMachine.h.

Member Typedef Documentation

◆ Guard

template<class State, class Listener, class States = StateHolder<State>, class Callback = void (Listener::*)(const States& states)>
typedef coil::Guard<Mutex> RTC_Utils::StateMachine< State, Listener, States, Callback >::Guard
private

Definition at line 266 of file StateMachine.h.

◆ Mutex

template<class State, class Listener, class States = StateHolder<State>, class Callback = void (Listener::*)(const States& states)>
typedef coil::Mutex RTC_Utils::StateMachine< State, Listener, States, Callback >::Mutex
private

Definition at line 265 of file StateMachine.h.

Constructor & Destructor Documentation

◆ StateMachine()

template<class State, class Listener, class States = StateHolder<State>, class Callback = void (Listener::*)(const States& states)>
RTC_Utils::StateMachine< State, Listener, States, Callback >::StateMachine ( int  num_of_state)
inline

Constructor.

Constructor

Parameters
num_of_stateNumber of states in the state machine

Definition at line 285 of file StateMachine.h.

◆ ~StateMachine()

template<class State, class Listener, class States = StateHolder<State>, class Callback = void (Listener::*)(const States& states)>
virtual RTC_Utils::StateMachine< State, Listener, States, Callback >::~StateMachine ( )
inlinevirtual

Definition at line 302 of file StateMachine.h.

Member Function Documentation

◆ getState()

template<class State, class Listener, class States = StateHolder<State>, class Callback = void (Listener::*)(const States& states)>
State RTC_Utils::StateMachine< State, Listener, States, Callback >::getState ( )
inline

Get current state.

Get current state.

Returns
Current state

Definition at line 603 of file StateMachine.h.

◆ getStates()

template<class State, class Listener, class States = StateHolder<State>, class Callback = void (Listener::*)(const States& states)>
States RTC_Utils::StateMachine< State, Listener, States, Callback >::getStates ( )
inline

Get states.

Get state information. Get the current state, the previous state and the next state to be expected to transfer.

Returns
State information

Definition at line 580 of file StateMachine.h.

◆ goTo()

template<class State, class Listener, class States = StateHolder<State>, class Callback = void (Listener::*)(const States& states)>
void RTC_Utils::StateMachine< State, Listener, States, Callback >::goTo ( State  state)
inline

Transit State.

Transit to the specified state. This function sets the next state forcibly. Therefore, to determine the next state, users must get current state and implement that logic. If transit destination is the same as the current state, flag of self-transition will be set.

Parameters
stateState of the transition destination

Definition at line 662 of file StateMachine.h.

◆ isIn()

template<class State, class Listener, class States = StateHolder<State>, class Callback = void (Listener::*)(const States& states)>
bool RTC_Utils::StateMachine< State, Listener, States, Callback >::isIn ( State  state)
inline

Check current state.

Check whether current state matches the state specified by argument.

Parameters
stateTarget state for the check
Returns
Check state result

Definition at line 630 of file StateMachine.h.

◆ need_trans()

template<class State, class Listener, class States = StateHolder<State>, class Callback = void (Listener::*)(const States& states)>
bool RTC_Utils::StateMachine< State, Listener, States, Callback >::need_trans ( )
inlineprivate

Definition at line 845 of file StateMachine.h.

◆ setDoAction()

template<class State, class Listener, class States = StateHolder<State>, class Callback = void (Listener::*)(const States& states)>
bool RTC_Utils::StateMachine< State, Listener, States, Callback >::setDoAction ( State  state,
Callback  call_back 
)
inline

Set Do action function.

Set callback function for Do action that is executed in each state.

Parameters
stateTarget state for the set
call_backCallback function for Do action
Returns
Action execution result

Definition at line 443 of file StateMachine.h.

◆ setEntryAction()

template<class State, class Listener, class States = StateHolder<State>, class Callback = void (Listener::*)(const States& states)>
bool RTC_Utils::StateMachine< State, Listener, States, Callback >::setEntryAction ( State  state,
Callback  call_back 
)
inline

Set Entry action function.

Set callback function for Entry action that is executed when entering in each state.

Parameters
stateTarget state for the set
call_backCallback function for Entry action
Returns
Action execution result

Definition at line 385 of file StateMachine.h.

◆ setExitAction()

template<class State, class Listener, class States = StateHolder<State>, class Callback = void (Listener::*)(const States& states)>
bool RTC_Utils::StateMachine< State, Listener, States, Callback >::setExitAction ( State  state,
Callback  call_back 
)
inline

Set Exit action function.

Set callback function for Exit action that is executed in each state.

Parameters
stateTarget state for the set
call_backCallback function for Exit action
Returns
Action execution result

Definition at line 501 of file StateMachine.h.

◆ setListener()

template<class State, class Listener, class States = StateHolder<State>, class Callback = void (Listener::*)(const States& states)>
void RTC_Utils::StateMachine< State, Listener, States, Callback >::setListener ( Listener *  listener)
inline

Set Listener Object.

Set Listener Object invoked when various actions are executed.

Parameters
listenerListener object

Definition at line 356 of file StateMachine.h.

◆ setNOP()

template<class State, class Listener, class States = StateHolder<State>, class Callback = void (Listener::*)(const States& states)>
void RTC_Utils::StateMachine< State, Listener, States, Callback >::setNOP ( Callback  call_back)
inline

Set NOP function.

Set NOP function that does not do anything

Parameters
call_backCallback function

Definition at line 329 of file StateMachine.h.

◆ setNullFunc()

template<class State, class Listener, class States = StateHolder<State>, class Callback = void (Listener::*)(const States& states)>
void RTC_Utils::StateMachine< State, Listener, States, Callback >::setNullFunc ( Callback *  s,
Callback  nullfunc 
)
inlineprotected

Set NOP function.

Set NOP function (function to do nothing).

Parameters
sCallback function for setting
nullfuncCallback function (NOP function)

Definition at line 750 of file StateMachine.h.

◆ setPostDoAction()

template<class State, class Listener, class States = StateHolder<State>, class Callback = void (Listener::*)(const States& states)>
bool RTC_Utils::StateMachine< State, Listener, States, Callback >::setPostDoAction ( State  state,
Callback  call_back 
)
inline

Set PostDo action function.

Set callback function for PostDo action that is executed in each state.

Parameters
stateTarget state for the set
call_backCallback function for PostDo action
Returns
Action execution result

Definition at line 472 of file StateMachine.h.

◆ setPreDoAction()

template<class State, class Listener, class States = StateHolder<State>, class Callback = void (Listener::*)(const States& states)>
bool RTC_Utils::StateMachine< State, Listener, States, Callback >::setPreDoAction ( State  state,
Callback  call_back 
)
inline

Set PreDo action function.

Set callback function for PreDo action that is executed in each state.

Parameters
stateTarget state for the set
call_backCallback function for PreDo action
Returns
Action execution result

Definition at line 414 of file StateMachine.h.

◆ setStartState()

template<class State, class Listener, class States = StateHolder<State>, class Callback = void (Listener::*)(const States& states)>
void RTC_Utils::StateMachine< State, Listener, States, Callback >::setStartState ( States  states)
inline

Set the initial state.

Set the initial state of the state machine.

Parameters
statesInitial state

Definition at line 553 of file StateMachine.h.

◆ setTransitionAction()

template<class State, class Listener, class States = StateHolder<State>, class Callback = void (Listener::*)(const States& states)>
bool RTC_Utils::StateMachine< State, Listener, States, Callback >::setTransitionAction ( Callback  call_back)
inline

Set state transition action function.

Set callback function for State transition action that is executed when transiting to the state.

Parameters
call_backCallback function for State transition
Returns
Action execution result

Definition at line 530 of file StateMachine.h.

◆ sync()

template<class State, class Listener, class States = StateHolder<State>, class Callback = void (Listener::*)(const States& states)>
void RTC_Utils::StateMachine< State, Listener, States, Callback >::sync ( States &  st)
inlineprivate

Definition at line 839 of file StateMachine.h.

◆ update_curr()

template<class State, class Listener, class States = StateHolder<State>, class Callback = void (Listener::*)(const States& states)>
void RTC_Utils::StateMachine< State, Listener, States, Callback >::update_curr ( const State  curr)
inlineprivate

Definition at line 851 of file StateMachine.h.

◆ worker()

template<class State, class Listener, class States = StateHolder<State>, class Callback = void (Listener::*)(const States& states)>
void RTC_Utils::StateMachine< State, Listener, States, Callback >::worker ( )
inline

Worker function.

This is a worker function of the state machine. Execute the invocation of each action at actual state transition and the state transition occurrence.

Definition at line 689 of file StateMachine.h.

Member Data Documentation

◆ m_do

template<class State, class Listener, class States = StateHolder<State>, class Callback = void (Listener::*)(const States& states)>
Callback* RTC_Utils::StateMachine< State, Listener, States, Callback >::m_do
protected

Callback function for Do action.

Definition at line 798 of file StateMachine.h.

◆ m_entry

template<class State, class Listener, class States = StateHolder<State>, class Callback = void (Listener::*)(const States& states)>
Callback* RTC_Utils::StateMachine< State, Listener, States, Callback >::m_entry
protected

Callback function for Entry action.

Definition at line 780 of file StateMachine.h.

◆ m_exit

template<class State, class Listener, class States = StateHolder<State>, class Callback = void (Listener::*)(const States& states)>
Callback* RTC_Utils::StateMachine< State, Listener, States, Callback >::m_exit
protected

Callback function for Exit action.

Definition at line 816 of file StateMachine.h.

◆ m_listener

template<class State, class Listener, class States = StateHolder<State>, class Callback = void (Listener::*)(const States& states)>
Listener* RTC_Utils::StateMachine< State, Listener, States, Callback >::m_listener
protected

Callback function for listener.

Definition at line 771 of file StateMachine.h.

◆ m_mutex

template<class State, class Listener, class States = StateHolder<State>, class Callback = void (Listener::*)(const States& states)>
Mutex RTC_Utils::StateMachine< State, Listener, States, Callback >::m_mutex
protected

Definition at line 836 of file StateMachine.h.

◆ m_num

template<class State, class Listener, class States = StateHolder<State>, class Callback = void (Listener::*)(const States& states)>
int RTC_Utils::StateMachine< State, Listener, States, Callback >::m_num
protected

Number of state.

Definition at line 762 of file StateMachine.h.

◆ m_postdo

template<class State, class Listener, class States = StateHolder<State>, class Callback = void (Listener::*)(const States& states)>
Callback* RTC_Utils::StateMachine< State, Listener, States, Callback >::m_postdo
protected

Callback function for PostDo action.

Definition at line 807 of file StateMachine.h.

◆ m_predo

template<class State, class Listener, class States = StateHolder<State>, class Callback = void (Listener::*)(const States& states)>
Callback* RTC_Utils::StateMachine< State, Listener, States, Callback >::m_predo
protected

Callback function for PreDo action.

Definition at line 789 of file StateMachine.h.

◆ m_selftrans

template<class State, class Listener, class States = StateHolder<State>, class Callback = void (Listener::*)(const States& states)>
bool RTC_Utils::StateMachine< State, Listener, States, Callback >::m_selftrans
protected

Definition at line 835 of file StateMachine.h.

◆ m_states

template<class State, class Listener, class States = StateHolder<State>, class Callback = void (Listener::*)(const States& states)>
States RTC_Utils::StateMachine< State, Listener, States, Callback >::m_states
protected

Current state information.

Definition at line 834 of file StateMachine.h.

◆ m_transit

template<class State, class Listener, class States = StateHolder<State>, class Callback = void (Listener::*)(const States& states)>
Callback RTC_Utils::StateMachine< State, Listener, States, Callback >::m_transit
protected

Callback function for State transition action.

Definition at line 825 of file StateMachine.h.


The documentation for this class was generated from the following file:


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Mon Feb 28 2022 23:00:48