Murl::Logic::StateMachine< StateType, HashFunc > Class Template Reference

A template class to create a BaseStepable object with simple statemachine capabilities. More...

#include "murl_logic_state_machine.h"

Inheritance diagram for Murl::Logic::StateMachine< StateType, HashFunc >:

Public Types

using ValueType = StateType
 The template state parameter value type.
 

Public Member Functions

 StateMachine ()
 The default constructor.
 
 ~StateMachine () override
 The destructor.
 
template<class ObjectType >
Bool Register (const StateType &state, ObjectType *objectInstance, typename StateMethods< ObjectType >::ObjectMethod onProcessTick, typename StateMethods< ObjectType >::ObjectMethod onEnterTick=0, typename StateMethods< ObjectType >::ObjectMethod onLeaveTick=0, typename StateMethods< ObjectType >::ObjectMethod onFinishTick=0)
 Register a state and the corresponding method calls. More...
 
Bool Unregister (const StateType &state)
 Unregister a state and the corresponding method calls. More...
 
void SetNextState (const StateType &nextState)
 Set the next state. More...
 
Bool IsNextState () const
 Check if the next state is set. More...
 
const StateType & GetNextState () const
 Get the next state. More...
 
const StateType & GetPreviousState () const
 Get the previous state. More...
 
const StateType & GetCurrentState () const
 Get the current state. More...
 
- Public Member Functions inherited from Murl::Logic::BaseStepable
 BaseStepable ()
 The default constructor. More...
 
 ~BaseStepable () override
 The destructor. More...
 
 operator IStepablePtr ()
 Conversion operator. More...
 
- Public Member Functions inherited from Murl::Logic::Stepable
 ~Stepable () override
 The destructor.
 
Bool SetAppStepable (IAppStepablePtr appStepable) override
 Implementation of IStepable::SetAppStepable() method. More...
 
void SetEnabled (Bool isEnabled) override
 Implementation of IStepable::SetEnabled() method. More...
 
Bool IsEnabled () const override
 Implementation of IStepable::IsEnabled() method. More...
 
void Reset () override
 Implementation of IStepable::Reset() method.
 
void ProcessTick (const Logic::IState *state) override
 Implementation of IStepable::ProcessTick() method. More...
 
void FinishTick (const Logic::IState *state) override
 Implementation of IStepable::FinishTick() method. More...
 

Protected Member Functions

void OnReset () override
 Overwrite of the BaseStepable::OnReset() method. More...
 
void OnProcessTick (const IState *state) override
 Overwrite of the BaseStepable::OnProcessTick() method. More...
 
void OnFinishTick (const IState *state) override
 Overwrite of the BaseStepable::OnFinishTick() method. More...
 
- Protected Member Functions inherited from Murl::Logic::BaseStepable
void OnSetEnabled (Bool isEnabled) override
 Default implementation of IAppStepable::OnSetEnabled() method, this method is empty and can be overwritten. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from Murl::Logic::Stepable
static IStepablePtr Create ()
 Create a stepable object. More...
 

Detailed Description

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 Murl
{
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__
bool Bool
Boolean data type This typedef represents a boolean value (true or false).
Definition: murl_types.h:158
The Murl Engine main namespace.
Definition: murl_addons_filepanel_factory.h:9

and e.g. my_processor.cpp:

#include "my_processor.h"
using namespace Murl;
App::MyProcessor::MyProcessor(Logic::IFactory* factory)
: BaseProcessor(factory)
{
}
Bool App::MyProcessor::OnInit(const Logic::IState* state)
{
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)
{
// State changed from paused to playing.
}
}
void App::MyProcessor::OnProcessPlaying(const Logic::IState* logicState)
{
}
void App::MyProcessor::OnLeavePlaying(const Logic::IState* logicState)
{
if (mStateMachine.GetNextState() == STATE_PAUSED)
{
// State changed from playing to 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
StateTypeThe data type of the state.
HashFuncThe hash function of the state type.

Member Function Documentation

◆ Register()

template<class StateType , class HashFunc = StdHash<StateType>>
template<class ObjectType >
Bool Murl::Logic::StateMachine< StateType, HashFunc >::Register ( const StateType &  state,
ObjectType *  objectInstance,
typename StateMethods< ObjectType >::ObjectMethod  onProcessTick,
typename StateMethods< ObjectType >::ObjectMethod  onEnterTick = 0,
typename StateMethods< ObjectType >::ObjectMethod  onLeaveTick = 0,
typename StateMethods< ObjectType >::ObjectMethod  onFinishTick = 0 
)
inline

Register a state and the corresponding method calls.

Template Parameters
ObjectTypeThe type of the class to call.
Parameters
stateThe state to register.
objectInstanceThe instance of the object to call.
onProcessTickThe method to call on process or null for no action.
onEnterTickThe method to call on enter or null for no action.
onLeaveTickThe method to call on leave or null for no action.
onFinishTickThe method to call on finish or null for no action.
Returns
true if the state was successfuly registered, false if the state is already registered.

◆ Unregister()

template<class StateType , class HashFunc = StdHash<StateType>>
Bool Murl::Logic::StateMachine< StateType, HashFunc >::Unregister ( const StateType &  state)
inline

Unregister a state and the corresponding method calls.

Parameters
stateThe state to unregister.
Returns
true if the state was successfuly unregistered.

◆ SetNextState()

template<class StateType , class HashFunc = StdHash<StateType>>
void Murl::Logic::StateMachine< StateType, HashFunc >::SetNextState ( const StateType &  nextState)
inline

Set the next state.

Parameters
nextStateThe next state.

◆ IsNextState()

template<class StateType , class HashFunc = StdHash<StateType>>
Bool Murl::Logic::StateMachine< StateType, HashFunc >::IsNextState ( ) const
inline

Check if the next state is set.

Returns
true if the next state is set.

◆ GetNextState()

template<class StateType , class HashFunc = StdHash<StateType>>
const StateType& Murl::Logic::StateMachine< StateType, HashFunc >::GetNextState ( ) const
inline

Get the next state.

Returns
The next state.

◆ GetPreviousState()

template<class StateType , class HashFunc = StdHash<StateType>>
const StateType& Murl::Logic::StateMachine< StateType, HashFunc >::GetPreviousState ( ) const
inline

Get the previous state.

Returns
The previous state.

◆ GetCurrentState()

template<class StateType , class HashFunc = StdHash<StateType>>
const StateType& Murl::Logic::StateMachine< StateType, HashFunc >::GetCurrentState ( ) const
inline

Get the current state.

Returns
The current state.

◆ OnReset()

template<class StateType , class HashFunc = StdHash<StateType>>
void Murl::Logic::StateMachine< StateType, HashFunc >::OnReset ( )
inlineoverrideprotectedvirtual

Overwrite of the BaseStepable::OnReset() method.

Reset the states to the state's default constructor.

Reimplemented from Murl::Logic::BaseStepable.

◆ OnProcessTick()

template<class StateType , class HashFunc = StdHash<StateType>>
void Murl::Logic::StateMachine< StateType, HashFunc >::OnProcessTick ( const IState state)
inlineoverrideprotectedvirtual

Overwrite of the BaseStepable::OnProcessTick() method.

Either execute OnProcessTick of the current state, or OnLeaveTick of the current state and OnEnterTick of the next state, if the next state was set. If OnEnterTick is null OnProcessTick is called instead.

Parameters
stateThe IState object.

Reimplemented from Murl::Logic::BaseStepable.

◆ OnFinishTick()

template<class StateType , class HashFunc = StdHash<StateType>>
void Murl::Logic::StateMachine< StateType, HashFunc >::OnFinishTick ( const IState state)
inlineoverrideprotectedvirtual

Overwrite of the BaseStepable::OnFinishTick() method.

Execute OnFinishTick of the current state.

Parameters
stateThe IState object.

Reimplemented from Murl::Logic::BaseStepable.


The documentation for this class was generated from the following file:
  • murl_logic_state_machine.h


Copyright © 2011-2024 Spraylight GmbH.