Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/network/FSM.h
Show All 13 Lines | |||||
* You should have received a copy of the GNU General Public License | * You should have received a copy of the GNU General Public License | ||||
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | ||||
*/ | */ | ||||
#ifndef FSM_H | #ifndef FSM_H | ||||
#define FSM_H | #define FSM_H | ||||
#include <limits> | #include <limits> | ||||
#include <set> | |||||
#include <unordered_map> | #include <unordered_map> | ||||
constexpr unsigned int FSM_INVALID_STATE{std::numeric_limits<unsigned int>::max()}; | constexpr unsigned int FSM_INVALID_STATE{std::numeric_limits<unsigned int>::max()}; | ||||
class CFsmEvent; | class CFsmEvent; | ||||
class CFsm; | class CFsm; | ||||
using Action = bool(void* pContext, CFsmEvent* pEvent); | using Action = bool(void* pContext, CFsmEvent* pEvent); | ||||
struct CallbackFunction | struct CallbackFunction | ||||
{ | { | ||||
Action* pFunction{nullptr}; | Action* pFunction{nullptr}; | ||||
void* pContext{nullptr}; | void* pContext{nullptr}; | ||||
bool operator()(CFsmEvent& event) const | bool operator()(CFsmEvent& event) const | ||||
{ | { | ||||
return !pFunction || pFunction(pContext, &event); | return !pFunction || pFunction(pContext, &event); | ||||
} | } | ||||
}; | }; | ||||
using StateSet = std::set<unsigned int>; | |||||
/** | /** | ||||
* Represents a signal in the state machine that a change has occurred. | * Represents a signal in the state machine that a change has occurred. | ||||
* The CFsmEvent objects are under the control of CFsm so | * The CFsmEvent objects are under the control of CFsm so | ||||
* they are created and deleted via CFsm. | * they are created and deleted via CFsm. | ||||
*/ | */ | ||||
class CFsmEvent | class CFsmEvent | ||||
{ | { | ||||
NONCOPYABLE(CFsmEvent); | NONCOPYABLE(CFsmEvent); | ||||
▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | public: | ||||
virtual void Setup(); | virtual void Setup(); | ||||
/** | /** | ||||
* Clear event, transition lists and reset state machine. | * Clear event, transition lists and reset state machine. | ||||
*/ | */ | ||||
void Shutdown(); | void Shutdown(); | ||||
/** | /** | ||||
* Adds the specified state to the internal list of states. | |||||
* @note If a state with the specified ID exists, the state is not added. | |||||
*/ | |||||
void AddState(unsigned int state); | |||||
/** | |||||
* Adds a new transistion to the state machine. | * Adds a new transistion to the state machine. | ||||
*/ | */ | ||||
void AddTransition(unsigned int state, unsigned int eventType, unsigned int nextState, | void AddTransition(unsigned int state, unsigned int eventType, unsigned int nextState, | ||||
Action* pAction = nullptr, void* pContext = nullptr); | Action* pAction = nullptr, void* pContext = nullptr); | ||||
/** | /** | ||||
* Sets the initial state for FSM. | * Sets the initial state for FSM. | ||||
*/ | */ | ||||
Show All 13 Lines | void SetNextState(unsigned int nextState) | ||||
m_NextState = nextState; | m_NextState = nextState; | ||||
} | } | ||||
unsigned int GetNextState() const | unsigned int GetNextState() const | ||||
{ | { | ||||
return m_NextState; | return m_NextState; | ||||
} | } | ||||
const StateSet& GetStates() const | |||||
{ | |||||
return m_States; | |||||
} | |||||
/** | /** | ||||
* Updates the FSM and retrieves next state. | * Updates the FSM and retrieves next state. | ||||
* @return whether the state was changed. | * @return whether the state was changed. | ||||
*/ | */ | ||||
bool Update(unsigned int eventType, void* pEventData); | bool Update(unsigned int eventType, void* pEventData); | ||||
/** | /** | ||||
* Verifies whether the specified state is managed by the FSM. | |||||
*/ | |||||
bool IsValidState(unsigned int state) const; | |||||
/** | |||||
* Tests whether the state machine has finished its work. | * Tests whether the state machine has finished its work. | ||||
* @note This is state machine specific. | * @note This is state machine specific. | ||||
*/ | */ | ||||
virtual bool IsDone() const; | virtual bool IsDone() const; | ||||
private: | private: | ||||
struct TransitionKey | struct TransitionKey | ||||
{ | { | ||||
Show All 30 Lines | private: | ||||
* Verifies whether state machine has already been updated. | * Verifies whether state machine has already been updated. | ||||
*/ | */ | ||||
bool IsFirstTime() const; | bool IsFirstTime() const; | ||||
bool m_Done; | bool m_Done; | ||||
unsigned int m_FirstState; | unsigned int m_FirstState; | ||||
unsigned int m_CurrState; | unsigned int m_CurrState; | ||||
unsigned int m_NextState; | unsigned int m_NextState; | ||||
StateSet m_States; | |||||
TransitionMap m_Transitions; | TransitionMap m_Transitions; | ||||
}; | }; | ||||
#endif // FSM_H | #endif // FSM_H |
Wildfire Games · Phabricator