46 lines
1.3 KiB
C++
46 lines
1.3 KiB
C++
#ifndef FSM_H
|
|
#define FSM_H
|
|
|
|
#include <string>
|
|
#include <map>
|
|
|
|
|
|
typedef struct FSM_State
|
|
{
|
|
std::string name;
|
|
unsigned int id;
|
|
std::map<std::string,FSM_State*> event;
|
|
}FSM_State;
|
|
|
|
typedef int (*FSM_Handle)(FSM_State &fsm, void *data);
|
|
// typedef int (*FSM_EventHandle)(FSM_State &fsm,std::string event,void *data);
|
|
|
|
class FSM
|
|
{
|
|
public:
|
|
FSM();
|
|
~FSM();
|
|
std::map<unsigned int, FSM_Handle> handleMap;
|
|
std::map<std::string, FSM_State> stateMap;
|
|
std::map<unsigned int, FSM_State> idStateMap;
|
|
|
|
int run(FSM_State &fsm,void* data);
|
|
int run(FSM_State *fsm,void* data, std::string event);
|
|
|
|
// 事件处理机制
|
|
int RegisterEvent(FSM_State* fsm, const std::string& event, FSM_State *state);
|
|
int unRegisterEvent(FSM_State* fsm, const std::string& event);
|
|
|
|
// 状态管理
|
|
int RegisterState(const std::string& name, unsigned int id, FSM_State &state);
|
|
FSM_State getState(const std::string& name) const;
|
|
unsigned int getStateId(const std::string& name) const;
|
|
|
|
// 处理函数管理
|
|
int RegisterHandleFunction(FSM_State* fsm, FSM_Handle handle);
|
|
int unRegisterHandleFunction(FSM_State* fsm);
|
|
|
|
// 事件触发
|
|
int triggerEvent(FSM_State* currentState, const std::string& event, void* data);
|
|
};
|
|
#endif |