Pyrogenesis  trunk
fsm.h
Go to the documentation of this file.
1 /* Copyright (C) 2015 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef FSM_H
19 #define FSM_H
20 
21 // INCLUDES
22 #include <vector>
23 #include <set>
24 #include <map>
25 
26 // DECLARATIONS
27 #define FSM_INVALID_STATE ( unsigned int )( ~0 )
28 
29 class CFsmEvent;
30 class CFsmTransition;
31 class CFsm;
32 
33 typedef bool ( *CONDITION ) ( void* pContext );
34 typedef bool ( *ACTION ) ( void* pContext, const CFsmEvent* pEvent );
35 
36 typedef struct
37 {
38  void* pFunction;
39  void* pContext;
40 
42 
43 typedef std::set< unsigned int > StateSet;
44 typedef std::map< unsigned int, CFsmEvent* > EventMap;
45 typedef std::vector< CFsmTransition* > TransitionList;
46 typedef std::vector< CallbackFunction > CallbackList;
47 
48 /**
49  * Represents a signal in the state machine that a change has occurred.
50  * The CFsmEvent objects are under the control of CFsm so
51  * they are created and deleted via CFsm.
52  */
53 class CFsmEvent
54 {
56 public:
57 
58  CFsmEvent( unsigned int type );
59  ~CFsmEvent( void );
60 
61  unsigned int GetType ( void ) const { return m_Type; }
62  void* GetParamRef ( void ) { return m_Param; }
63  void SetParamRef ( void* pParam );
64 
65 private:
66  unsigned int m_Type; // Event type
67  void* m_Param; // Event paramater
68 };
69 
70 
71 /**
72  * An association of event, condition, action and next state.
73  */
75 {
77 public:
78 
79  CFsmTransition( unsigned int state );
80  ~CFsmTransition( void );
81 
82  void RegisterAction (
83  void* pAction,
84  void* pContext );
85  void RegisterCondition (
86  void* pCondition,
87  void* pContext );
88  void SetEvent ( CFsmEvent* pEvent );
89  CFsmEvent* GetEvent ( void ) const { return m_Event; }
90  void SetNextState ( unsigned int nextState );
91  unsigned int GetNextState ( void ) const { return m_NextState; }
92  unsigned int GetCurrState ( void ) const { return m_CurrState; }
93  const CallbackList& GetActions ( void ) const { return m_Actions; }
94  const CallbackList& GetConditions ( void ) const { return m_Conditions; }
95  bool ApplyConditions ( void ) const;
96  bool RunActions ( void ) const;
97 
98 private:
99  unsigned int m_CurrState; // Current state
100  unsigned int m_NextState; // Next state
101  CFsmEvent* m_Event; // Transition event
102  CallbackList m_Actions; // List of actions for transition
103  CallbackList m_Conditions; // List of conditions for transition
104 };
105 
106 /**
107  * Manages states, events, actions and transitions
108  * between states. It provides an interface for advertising
109  * events and track the current state. The implementation is
110  * a Mealy state machine, so the system respond to events
111  * and execute some action.
112  *
113  * A Mealy state machine has behaviour associated with state
114  * transitions; Mealy machines are event driven where an
115  * event triggers a state transition
116  */
117 class CFsm
118 {
119  NONCOPYABLE(CFsm);
120 public:
121 
122  CFsm( void );
123  virtual ~CFsm( void );
124 
125  /**
126  * Constructs the state machine. This method must be overriden so that
127  * connections are constructed for the particular state machine implemented
128  */
129  virtual void Setup( void );
130 
131  /**
132  * Clear event, action and condition lists and reset state machine
133  */
134  void Shutdown( void );
135 
136  void AddState ( unsigned int state );
137  CFsmEvent* AddEvent ( unsigned int eventType );
138  CFsmTransition* AddTransition (
139  unsigned int state,
140  unsigned int eventType,
141  unsigned int nextState );
142  CFsmTransition* AddTransition (
143  unsigned int state,
144  unsigned int eventType,
145  unsigned int nextState,
146  void* pAction,
147  void* pContext );
148  CFsmTransition* GetTransition (
149  unsigned int state,
150  unsigned int eventType ) const;
151  CFsmTransition* GetEventTransition ( unsigned int eventType ) const;
152  void SetFirstState ( unsigned int firstState );
153  void SetCurrState ( unsigned int state );
154  unsigned int GetCurrState ( void ) const { return m_CurrState; }
155  void SetNextState ( unsigned int nextState ) { m_NextState = nextState; }
156  unsigned int GetNextState ( void ) const { return m_NextState; }
157  const StateSet& GetStates ( void ) const { return m_States; }
158  const EventMap& GetEvents ( void ) const { return m_Events; }
159  const TransitionList& GetTransitions ( void ) const { return m_Transitions; }
160  bool Update ( unsigned int eventType, void* pEventData );
161  bool IsValidState ( unsigned int state ) const;
162  bool IsValidEvent ( unsigned int eventType ) const;
163  virtual bool IsDone ( void ) const;
164 
165 private:
166  bool IsFirstTime ( void ) const;
167 
168  bool m_Done; // FSM work is done
169  unsigned int m_FirstState; // Initial state
170  unsigned int m_CurrState; // Current state
171  unsigned int m_NextState; // Next state
172  StateSet m_States; // List of states
173  EventMap m_Events; // List of events
174  TransitionList m_Transitions; // State transitions
175 };
176 
177 #endif // FSM_H
178 
static enum @39 state
Manages states, events, actions and transitions between states.
Definition: fsm.h:117
unsigned int m_NextState
Definition: fsm.h:171
NONCOPYABLE(CFsmEvent)
std::vector< CallbackFunction > CallbackList
Definition: fsm.h:46
Represents a signal in the state machine that a change has occurred.
Definition: fsm.h:53
const EventMap & GetEvents(void) const
Definition: fsm.h:158
CFsmEvent * m_Event
Definition: fsm.h:101
unsigned int m_Type
Definition: fsm.h:66
void * pFunction
Definition: fsm.h:38
bool m_Done
Definition: fsm.h:168
static void Shutdown()
Definition: h_mgr.cpp:762
void * pContext
Definition: fsm.h:39
unsigned int GetCurrState(void) const
Definition: fsm.h:92
bool(* ACTION)(void *pContext, const CFsmEvent *pEvent)
Definition: fsm.h:34
bool(* CONDITION)(void *pContext)
Definition: fsm.h:33
const StateSet & GetStates(void) const
Definition: fsm.h:157
void * m_Param
Definition: fsm.h:67
void SetNextState(unsigned int nextState)
Definition: fsm.h:155
unsigned int m_CurrState
Definition: fsm.h:99
std::vector< CFsmTransition * > TransitionList
Definition: fsm.h:45
std::map< unsigned int, CFsmEvent * > EventMap
Definition: fsm.h:44
unsigned int m_CurrState
Definition: fsm.h:170
const TransitionList & GetTransitions(void) const
Definition: fsm.h:159
CallbackList m_Conditions
Definition: fsm.h:103
unsigned int GetNextState(void) const
Definition: fsm.h:91
EventMap m_Events
Definition: fsm.h:173
CFsmEvent * GetEvent(void) const
Definition: fsm.h:89
unsigned int GetCurrState(void) const
Definition: fsm.h:154
TransitionList m_Transitions
Definition: fsm.h:174
void SetParamRef(void *pParam)
Definition: fsm.cpp:47
CFsmEvent(unsigned int type)
Definition: fsm.cpp:28
Definition: fsm.h:36
StateSet m_States
Definition: fsm.h:172
unsigned int m_FirstState
Definition: fsm.h:169
~CFsmEvent(void)
Definition: fsm.cpp:38
unsigned int m_NextState
Definition: fsm.h:100
CallbackList m_Actions
Definition: fsm.h:102
unsigned int GetType(void) const
Definition: fsm.h:61
unsigned int GetNextState(void) const
Definition: fsm.h:156
void * GetParamRef(void)
Definition: fsm.h:62
std::set< unsigned int > StateSet
Definition: fsm.h:43
const CallbackList & GetConditions(void) const
Definition: fsm.h:94
const CallbackList & GetActions(void) const
Definition: fsm.h:93
An association of event, condition, action and next state.
Definition: fsm.h:74