Remove the FSM EventMap

Comments By: @vladislavbelov
Differential Revision: https://code.wildfiregames.com/D5089
This was SVN commit r27987.
This commit is contained in:
phosit 2023-12-20 20:44:06 +00:00
parent 6dfe29959f
commit 05356bb9fc
2 changed files with 16 additions and 101 deletions

View File

@ -19,10 +19,10 @@
#include "FSM.h"
CFsmEvent::CFsmEvent(unsigned int type)
CFsmEvent::CFsmEvent(unsigned int type, void* pParam)
{
m_Type = type;
m_Param = nullptr;
m_Param = pParam;
}
CFsmEvent::~CFsmEvent()
@ -30,19 +30,14 @@ CFsmEvent::~CFsmEvent()
m_Param = nullptr;
}
void CFsmEvent::SetParamRef(void* pParam)
{
m_Param = pParam;
}
CFsmTransition::CFsmTransition(const unsigned int state, const CallbackFunction action)
: m_CurrState{state},
m_Action{action}
{}
void CFsmTransition::SetEvent(CFsmEvent* pEvent)
void CFsmTransition::SetEventType(const unsigned int eventType)
{
m_Event = pEvent;
m_EventType = eventType;
}
void CFsmTransition::SetNextState(unsigned int nextState)
@ -50,9 +45,9 @@ void CFsmTransition::SetNextState(unsigned int nextState)
m_NextState = nextState;
}
bool CFsmTransition::RunAction() const
bool CFsmTransition::RunAction(CFsmEvent& event) const
{
return !m_Action.pFunction || m_Action.pFunction(m_Action.pContext, m_Event);
return !m_Action.pFunction || m_Action.pFunction(m_Action.pContext, &event);
}
CFsm::CFsm()
@ -80,13 +75,7 @@ void CFsm::Shutdown()
for (; itTransition < m_Transitions.end(); ++itTransition)
delete *itTransition;
// Release events
EventMap::iterator itEvent = m_Events.begin();
for (; itEvent != m_Events.end(); ++itEvent)
delete itEvent->second;
m_States.clear();
m_Events.clear();
m_Transitions.clear();
m_Done = false;
@ -100,27 +89,6 @@ void CFsm::AddState(unsigned int state)
m_States.insert(state);
}
CFsmEvent* CFsm::AddEvent(unsigned int eventType)
{
CFsmEvent* pEvent = nullptr;
// Lookup event by type
EventMap::iterator it = m_Events.find(eventType);
if (it != m_Events.end())
{
pEvent = it->second;
}
else
{
pEvent = new CFsmEvent(eventType);
// Store new event into internal map
m_Events[eventType] = pEvent;
}
return pEvent;
}
CFsmTransition* CFsm::AddTransition(unsigned int state, unsigned int eventType, unsigned int nextState,
Action* pAction /* = nullptr */, void* pContext /* = nullptr*/)
{
@ -130,16 +98,11 @@ CFsmTransition* CFsm::AddTransition(unsigned int state, unsigned int eventType,
// Make sure we store the next state
AddState(nextState);
// Make sure we store the event
CFsmEvent* pEvent = AddEvent(eventType);
if (!pEvent)
return nullptr;
// Create new transition
CFsmTransition* pNewTransition = new CFsmTransition(state, {pAction, pContext});
// Setup new transition
pNewTransition->SetEvent(pEvent);
pNewTransition->SetEventType(eventType);
pNewTransition->SetNextState(nextState);
// Store new transition
@ -153,9 +116,6 @@ CFsmTransition* CFsm::GetTransition(unsigned int state, unsigned int eventType)
if (!IsValidState(state))
return nullptr;
if (!IsValidEvent(eventType))
return nullptr;
TransitionList::const_iterator it = m_Transitions.begin();
for (; it != m_Transitions.end(); ++it)
{
@ -163,12 +123,8 @@ CFsmTransition* CFsm::GetTransition(unsigned int state, unsigned int eventType)
if (!pCurrTransition)
continue;
CFsmEvent* pCurrEvent = pCurrTransition->GetEvent();
if (!pCurrEvent)
continue;
// Is it our transition?
if (pCurrTransition->GetCurrState() == state && pCurrEvent->GetType() == eventType)
if (pCurrTransition->GetCurrState() == state && pCurrTransition->GetEventType() == eventType)
return pCurrTransition;
}
@ -193,9 +149,6 @@ bool CFsm::IsFirstTime() const
bool CFsm::Update(unsigned int eventType, void* pEventParam)
{
if (!IsValidEvent(eventType))
return false;
if (IsFirstTime())
m_CurrState = m_FirstState;
@ -204,20 +157,13 @@ bool CFsm::Update(unsigned int eventType, void* pEventParam)
if (!pTransition)
return false;
// Setup event parameter
EventMap::iterator it = m_Events.find(eventType);
if (it != m_Events.end())
{
CFsmEvent* pEvent = it->second;
if (pEvent)
pEvent->SetParamRef(pEventParam);
}
CFsmEvent event{eventType, pEventParam};
// Save the default state transition (actions might call SetNextState
// to override this)
SetNextState(pTransition->GetNextState());
if (!pTransition->RunAction())
if (!pTransition->RunAction(event))
return false;
SetCurrState(GetNextState());
@ -242,12 +188,3 @@ bool CFsm::IsValidState(unsigned int state) const
return true;
}
bool CFsm::IsValidEvent(unsigned int eventType) const
{
EventMap::const_iterator it = m_Events.find(eventType);
if (it == m_Events.end())
return false;
return true;
}

View File

@ -19,7 +19,6 @@
#define FSM_H
#include <limits>
#include <map>
#include <set>
#include <vector>
@ -39,7 +38,6 @@ struct CallbackFunction
};
using StateSet = std::set<unsigned int>;
using EventMap = std::map<unsigned int, CFsmEvent*>;
using TransitionList = std::vector<CFsmTransition*>;
/**
@ -52,7 +50,7 @@ class CFsmEvent
NONCOPYABLE(CFsmEvent);
public:
CFsmEvent(unsigned int type);
CFsmEvent(unsigned int type, void* pParam);
~CFsmEvent();
unsigned int GetType() const
@ -65,8 +63,6 @@ public:
return m_Param;
}
void SetParamRef(void* pParam);
private:
unsigned int m_Type; // Event type
void* m_Param; // Event paramater
@ -88,10 +84,10 @@ public:
/**
* Set event for which transition will occur.
*/
void SetEvent(CFsmEvent* pEvent);
CFsmEvent* GetEvent() const
void SetEventType(const unsigned int eventType);
unsigned int GetEventType() const
{
return m_Event;
return m_EventType;
}
/**
@ -118,12 +114,12 @@ public:
* @note If there are no action, assume true.
* @return whether the action returned true.
*/
bool RunAction() const;
bool RunAction(CFsmEvent& event) const;
private:
unsigned int m_CurrState;
unsigned int m_NextState;
CFsmEvent* m_Event;
unsigned int m_EventType;
CallbackFunction m_Action;
};
@ -163,13 +159,6 @@ public:
*/
void AddState(unsigned int state);
/**
* Adds the specified event to the internal list of events.
* @note If an eveny with the specified ID exists, the event is not added.
* @return a pointer to the new event.
*/
CFsmEvent* AddEvent(unsigned int eventType);
/**
* Adds a new transistion to the state machine.
* @return a pointer to the new transition.
@ -212,11 +201,6 @@ public:
return m_States;
}
const EventMap& GetEvents() const
{
return m_Events;
}
const TransitionList& GetTransitions() const
{
return m_Transitions;
@ -233,11 +217,6 @@ public:
*/
bool IsValidState(unsigned int state) const;
/**
* Verifies whether the specified event is managed by the FSM.
*/
bool IsValidEvent(unsigned int eventType) const;
/**
* Tests whether the state machine has finished its work.
* @note This is state machine specific.
@ -255,7 +234,6 @@ private:
unsigned int m_CurrState;
unsigned int m_NextState;
StateSet m_States;
EventMap m_Events;
TransitionList m_Transitions;
};