00001
00002 #ifndef FSA_H
00003 #define FSA_H
00004
00005 #include "IS3DCommon.H"
00006
00007
00008 #include <Event.H>
00009
00010 #include <FsaHelper.H>
00011
00012 namespace IS3D {
00013
00014 typedef ReferenceCountedPointer<class Fsa> FsaRef;
00015 class Fsa : public ReferenceCountedObject
00016 {
00017 public:
00018
00026 static FsaRef fromQFSMFile(const std::string &filename);
00027
00030 Fsa(const std::string &name);
00031 virtual ~Fsa();
00032
00034 bool addState(const std::string &stateName);
00035
00042 bool addArc(const std::string &arcName,
00043 const int &fromState,
00044 const int &toState,
00045 const Array<std::string> &triggers);
00046
00053 bool addArc(const std::string &arcName,
00054 const std::string &fromState,
00055 const std::string &toState,
00056 const Array<std::string> &triggers);
00057
00058
00065 template <class T>
00066 void addStateEnterCallback(std::string stateName, T *thisPtr,
00067 void (T::*method)(EventRef)) {
00068 EventCallbackFunctor *f =
00069 new EventCallbackSpecificFunctor<T>(thisPtr, method);
00070 _states[getIndexOfState(stateName)].addEnterCallback(f);
00071 }
00072
00073
00080 template <class T>
00081 void addStateExitCallback(std::string stateName, T *thisPtr,
00082 void (T::*method)(EventRef)) {
00083 EventCallbackFunctor *f =
00084 new EventCallbackSpecificFunctor<T>(thisPtr, method);
00085 _states[getIndexOfState(stateName)].addExitCallback(f);
00086 }
00087
00088
00095 template <class T>
00096 void addArcCallback(std::string arcName, T *thisPtr,
00097 void (T::*method)(EventRef)) {
00098 EventCallbackFunctor *f =
00099 new EventCallbackSpecificFunctor<T>(thisPtr, method);
00100 storeArcCallback(arcName, f);
00101 }
00102
00103
00104
00105 void print();
00106
00107 void setDebug(bool b) { _debug = b; }
00108
00109 std::string getCurrentState() { return _states[_curState].getName(); }
00110
00111 void processEvent(EventRef event);
00112
00121 bool jumpToState(const std::string &toState);
00122
00123 std::string getName() { return _name; }
00124
00125 protected:
00126
00127 std::string stateName(int i);
00128
00129 int getIndexOfState(std::string stateName) {
00130 for (int s=0;s<_states.size();s++) {
00131 if (_states[s].getName() == stateName)
00132 return s;
00133 }
00134 alwaysAssertM(false, "Can't find match for state name " + stateName);
00135 return -1;
00136 }
00137
00138
00139 bool checkTriggerMatch(EventRef event, const std::string &triggerName);
00140
00141 void storeArcCallback(std::string arcName, EventCallbackFunctor *f);
00142
00143 Array<FsaState> _states;
00144 std::string _name;
00145 int _curState;
00146 bool _debug;
00147 };
00148
00149
00150 }
00151
00152 #endif