Class KPStateMachine

Inheritance Relationships

Base Types

Class Documentation

class KPStateMachine : public KPComponent, public KPSubject<KPStateMachineObserver>

Public Functions

template<typename T>
void registerState(T &&state, StateName name, Middleware middleware = nullptr)

Register a state with a given unique name. “Optionally” accepts a functor as the third parameter. The functor should take care of calling transitionTo() method of this state machine to move on to the next state.

The middleware should define how the state would transition to another state according a passed-in integer value.

Template Parameters
  • T: Deduced subtype of the state
Parameters
  • state: State instance
  • name: Name of the state being registered
  • middleware: Any callable with signature: void(int)

template<typename T>
void registerState(T &&state, StateName name, StateName next)

Convenient method for registering a state with one direct transition to the next one

Template Parameters
  • T: Deduced subtype of the state
Parameters
  • state: State instance
  • name: Name of the state being registered
  • next: Name of the next state to transition to

template<typename T = KPState>
T &getState(StateName name)

Get the State object with optional static casting to the given state subclasss. The function doesn’t return if the name doesn’t exist.

Return
T& Reference to state in storage
Template Parameters
Parameters

KPState *getCurrentState() const

Get the Current State object

Return
KPState* Ptr to the internal state object

void next(int code = 0) const

This method is to be called in subclassese of KPState

Parameters
  • code: exitcode StateController/StateMachine should decide how to deal with this code

void restart()

Restart the state by passing the currentState name to transitionTo(name)

void transitionTo(StateName name)

Transition to the state registered using the given name

Parameters

Protected Functions

void setup()

The default setup method of this class do nothing

void update()