OpenRTM-aist 2.1.0
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
RTC_Utils::StateMachine< State, Listener, States, Callback > Class Template Reference

State machine class. More...

#include <StateMachine.h>

Collaboration diagram for RTC_Utils::StateMachine< State, Listener, States, Callback >:
Collaboration graph
[legend]

Public Member Functions

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

Protected Member Functions

void setNullFunc (std::vector< Callback > &s, Callback nullfunc)
 Set NOP function.
 

Protected Attributes

int m_num
 Number of state .
 
Listenerm_listener
 Callback function for listener .
 
std::vector< Callback > m_entry
 Callback function for Entry action .
 
std::vector< Callback > m_predo
 Callback function for PreDo action .
 
std::vector< Callback > m_do
 Callback function for Do action .
 
std::vector< Callback > m_postdo
 Callback function for PostDo action .
 
std::vector< Callback > m_exit
 Callback function for Exit action .
 
Callback m_transit
 Callback function for State transition action .
 
States m_states
 Current state information .
 
bool m_selftrans
 
std::mutex m_mutex
 

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 };
  using MyStates = States<MyState>;

  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

Constructor & Destructor Documentation

◆ StateMachine() [1/2]

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)
inlineexplicit

Constructor.

◆ ~StateMachine()

template<class T , class U , class V , class W >
RTC_Utils::StateMachine< T, U, V, W >::~StateMachine ( )
virtualdefault

◆ StateMachine() [2/2]

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

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.

◆ 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.

◆ 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.

◆ 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.

◆ operator=()

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

◆ 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.

◆ 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.

◆ 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.

◆ 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.

◆ 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.

◆ 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 ( std::vector< Callback > &  s,
Callback  nullfunc 
)
inlineprotected

Set NOP function.

◆ 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.

◆ 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.

◆ 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.

◆ 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.

◆ swap()

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

◆ 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.

◆ worker_do()

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_do ( )
inline

◆ worker_post()

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_post ( )
inline

◆ worker_pre()

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_pre ( )
inline

Member Data Documentation

◆ m_do

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

Callback function for Do action .

Referenced by RTC_Utils::StateMachine< State, Listener, States, Callback >::swap().

◆ m_entry

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

Callback function for Entry action .

Referenced by RTC_Utils::StateMachine< State, Listener, States, Callback >::swap().

◆ m_exit

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

Callback function for Exit action .

Referenced by RTC_Utils::StateMachine< State, Listener, States, Callback >::swap().

◆ 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 .

Referenced by RTC_Utils::StateMachine< State, Listener, States, Callback >::swap().

◆ m_mutex

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

◆ 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

◆ m_postdo

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

Callback function for PostDo action .

Referenced by RTC_Utils::StateMachine< State, Listener, States, Callback >::swap().

◆ m_predo

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

Callback function for PreDo action .

Referenced by RTC_Utils::StateMachine< State, Listener, States, Callback >::swap().

◆ 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

◆ 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

◆ 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 .

Referenced by RTC_Utils::StateMachine< State, Listener, States, Callback >::swap().


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