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

typedef StateType ValueType
 The template state parameter value type. More...
 

Public Member Functions

 StateMachine ()
 The default constructor.
 
virtual ~StateMachine ()
 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...
 
virtual ~BaseStepable ()
 The destructor. More...
 
 operator IStepablePtr ()
 Conversion operator. More...
 
- Public Member Functions inherited from Murl::Logic::Stepable
virtual ~Stepable ()
 The destructor.
 
virtual Bool SetAppStepable (IAppStepablePtr appStepable)
 Implementation of IStepable::SetAppStepable() method.
 
virtual void SetEnabled (Bool isEnabled)
 Implementation of IStepable::SetEnabled() method.
 
virtual Bool IsEnabled () const
 Implementation of IStepable::IsEnabled() method.
 
virtual void Reset ()
 Implementation of IStepable::Reset() method.
 
virtual void ProcessTick (const Logic::IState *state)
 Implementation of IStepable::ProcessTick() method.
 
virtual void FinishTick (const Logic::IState *state)
 Implementation of IStepable::FinishTick() method.
 

Protected Member Functions

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

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__

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 Typedef Documentation

◆ ValueType

template<class StateType , class HashFunc = StdHash<StateType>>
typedef StateType Murl::Logic::StateMachine< StateType, HashFunc >::ValueType

The template state parameter value 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.

References Murl::ObjectMap< KeyType, DataType, HashFunc >::Add(), and Murl::MapBase< KeyType, DataType, ArrayType, HashFunc >::Find().

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

References Murl::MapBase< KeyType, DataType, ArrayType, HashFunc >::RemoveKey().

◆ 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>>
virtual void Murl::Logic::StateMachine< StateType, HashFunc >::OnReset ( )
inlineprotectedvirtual

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>>
virtual void Murl::Logic::StateMachine< StateType, HashFunc >::OnProcessTick ( const IState state)
inlineprotectedvirtual

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.

References Murl::MapBase< KeyType, DataType, ArrayType, HashFunc >::Find().

Referenced by Murl::Logic::StateMachine< StateType, HashFunc >::OnFinishTick().

◆ OnFinishTick()

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

Overwrite of the BaseStepable::OnFinishTick() method.

Execute OnFinishTick of the current state.

Parameters
stateThe IState object.

Reimplemented from Murl::Logic::BaseStepable.

References Murl::MapBase< KeyType, DataType, ArrayType, HashFunc >::Find(), and Murl::Logic::StateMachine< StateType, HashFunc >::OnProcessTick().


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


Copyright © 2011-2018 Spraylight GmbH.