00001 #if !defined(AFX_FSMACTION_H__7C6ABC20_3696_40ED_9DC4_6666666__INCLUDED_)
00002 #define AFX_FSMACTION_H__7C6ABC20_3696_40ED_9DC4_6666666__INCLUDED_
00003
00004 #if _MSC_VER > 1000
00005 #pragma once
00006 #endif // _MSC_VER > 1000
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00090 #include <map>
00091 #include <list>
00092 #include <string>
00093
00094
00096 namespace FSM {
00097 using std::map;
00098 using std::list;
00099 using std::string;
00100 using std::less;
00101
00102
00103
00104 class CFiniteStateMachine;
00105 class CFSMObject;
00106 class CFSMState;
00107 class CFSMTransition;
00108 class CFSMAction;
00109 class CFSMCondition;
00110
00111 typedef map< string, CFSMState* > State_Map;
00112 typedef State_Map::value_type SM_VT;
00113
00114 typedef map< string, int > Var_Map;
00115 typedef Var_Map::value_type VM_VT;
00116
00117 typedef void (*InitClassF)(CFiniteStateMachine* fsm);
00118 typedef CFSMObject* (*FSMObjectCreateMethod)();
00119
00120 typedef map< string, FSMObjectCreateMethod > FSMObjectCreate_Map;
00121 typedef FSMObjectCreate_Map::value_type FSM_C_VT;
00122
00123 class CFiniteStateMachine
00124 {
00125 public:
00126
00127 CFiniteStateMachine();
00128
00129
00130 ~CFiniteStateMachine();
00131
00132
00133 CFSMState* AddState(const char *stateName, const char *className="State");
00134 CFSMState* GetState(const char *stateName) const;
00135 bool DeleteState(CFSMState *state);
00136 bool DeleteState(const char *stateName);
00137
00143 State_Map::const_iterator GetStatesBegin() const;
00144
00146 State_Map::const_iterator GetStatesEnd() const;
00147
00148
00149 CFSMTransition* AddTransition(const char* fromStateName, const char* onEvent="ANY", const char* className="SimpleTransition");
00150 CFSMTransition* AddSimpleTransition(const char* fromStateName, const char* toStateName, const char* onEvent="ANY");
00151
00160
00161
00167 bool DeleteTransition(const char* fromState, int index);
00168
00169
00177 CFSMCondition* AddCondition(const char *stateName, int transition, const char *className="TestVariable");
00178
00179
00183 CFSMAction* AddEnterAction(const char *stateName, const char *className);
00184
00188 CFSMAction* AddLeaveAction(const char *stateName, const char *className);
00189
00197 CFSMAction* AddAction(const char *stateName, int transition, const char *className);
00198
00199
00203 bool AddVariable(const char *varName, const int initValue = 0);
00204
00207 bool DeleteVariable(const char *varName);
00208
00212 bool SetVariable(const char *varName, const int value);
00213
00217 bool GetVariable(const char *varName, int &value) const;
00218
00220 Var_Map::const_iterator GetVarsBegin() const;
00221
00223 Var_Map::const_iterator GetVarsEnd() const;
00224
00225
00226 bool SetInitialState(const char *stateName);
00227 const char* GetInitialState() const;
00228 bool Start(const char *stateName=0);
00229 void Stop();
00230 bool ProcessEvent(const char *Event, void *a=NULL, void *b=NULL, bool *consumed=NULL);
00231 CFSMState *GetCurrentState() const;
00232 bool Clear();
00233
00235 bool SwitchToState(CFSMState* newState);
00236
00240 bool PushState(CFSMState* newState);
00241
00245 bool PopState();
00246
00247 void Register(const char *className, FSMObjectCreateMethod create);
00248
00250 CFSMObject* Create(const char *className);
00251
00252 protected:
00253 #ifndef _WIN32_WCE
00254 bool LoadClass(const char *className);
00255 #endif
00256
00257
00258 private:
00259 void RegisterBaseTypes();
00260
00261
00262 FSMObjectCreate_Map FSMObjectCreationMethods;
00263 State_Map m_States;
00264 Var_Map m_Variables;
00265 CFSMState* m_CurrentState;
00266 string m_InitialState;
00267 bool m_Running;
00268 };
00269
00270 };
00271
00272 #endif