template<class StateType, class HashFunc = StdHash<StateType>>
class Murl::Logic::StateMachine< StateType, HashFunc >
A template class to create a BaseStepable object with simple statemachine capabilities.
The statemachine can register any number of states, for each state a class method call can be registered for:
- OnEnterTick the state
- OnProcessTick the state
- OnLeaveTick the state
- OnFinishTick the state
Usage e.g. my_processor.h:
#ifndef __MY_PROCESSOR_H__
#define __MY_PROCESSOR_H__
#include "murl_app_types.h"
#include "murl_logic_base_processor.h"
{
namespace App
{
class MyProcessor : public Logic::BaseProcessor
{
public:
MyProcessor(Logic::IFactory* factory);
protected:
virtual Bool OnInit(
const Logic::IState* state);
enum States
{
STATE_IDLE = 0,
STATE_PLAYING,
STATE_PAUSED
};
Logic::EnumStateMachine<States>::Type mStateMachine;
void OnEnterPlaying(const Logic::IState* logicState);
void OnProcessPlaying(const Logic::IState* logicState);
void OnLeavePlaying(const Logic::IState* logicState);
void OnEnterPaused(const Logic::IState* logicState);
void OnProcessPaused(const Logic::IState* logicState);
void OnLeavePaused(const Logic::IState* logicState);
};
}
}
#endif // __MY_PROCESSOR_H__
and e.g. my_processor.cpp:
#include "my_processor.h"
: BaseProcessor(factory)
{
}
{
mStateMachine.Register<MyProcessor>(STATE_PLAYING, this, &MyProcessor::OnProcessPlaying,
&MyProcessor::OnEnterPlaying, &MyProcessor::OnLeavePlaying);
mStateMachine.Register<MyProcessor>(STATE_PAUSED, this, &MyProcessor::OnProcessPaused,
&MyProcessor::OnEnterPaused, &MyProcessor::OnLeavePaused);
AddStepable(mStateMachine);
return true;
}
void App::MyProcessor::OnEnterPlaying(
const Logic::IState* logicState)
{
if (mStateMachine.GetPreviousState() == STATE_PAUSED)
{
}
}
void App::MyProcessor::OnProcessPlaying(
const Logic::IState* logicState)
{
}
void App::MyProcessor::OnLeavePlaying(
const Logic::IState* logicState)
{
if (mStateMachine.GetNextState() == STATE_PAUSED)
{
}
}
void App::MyProcessor::OnEnterPaused(
const Logic::IState* logicState)
{
}
void App::MyProcessor::OnProcessPaused(
const Logic::IState* logicState)
{
}
void App::MyProcessor::OnLeavePaused(
const Logic::IState* logicState)
{
}
To change the state simply call:
mStateMachine.SetNextState(STATE_PLAYING);
This execute at the next logic tick:
- OnLeaveTick of the current state and OnEnterTick of the next state.
- All following logic ticks execute OnProcessTick / OnFinishTick of the (new) current state.
If OnEnterTick is null OnProcessTick is called instead.
- Template Parameters
-
StateType | The data type of the state. |
HashFunc | The hash function of the state type. |