]> git.mxchange.org Git - simgear.git/blob - simgear/structure/subsystem_mgr.hxx
model paging patch from Till Busch
[simgear.git] / simgear / structure / subsystem_mgr.hxx
1 // Written by David Megginson, started 2000-12
2 //
3 // Copyright (C) 2000  David Megginson, david@megginson.com
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 //
19 // $Id$
20
21
22 #ifndef __SUBSYSTEM_MGR_HXX
23 #define __SUBSYSTEM_MGR_HXX 1
24
25
26 #include <simgear/compiler.h>
27
28 #if 0
29 #ifdef HAVE_WINDOWS_H
30 #  include <windows.h>
31 #  include <float.h>
32 #endif
33
34 #include STL_STRING
35 SG_USING_STD(string);
36
37 #include <vector>
38 SG_USING_STD(vector);
39 #endif
40
41 #include <string>
42 #include <map>
43 #include <vector>
44 SG_USING_STD(map);
45 SG_USING_STD(vector);
46 SG_USING_STD(string);
47
48 #include <simgear/props/props.hxx>
49 #include <simgear/timing/timestamp.hxx>
50 #include "SGSmplstat.hxx"
51
52
53 class TimingInfo
54 {
55 private:
56     string eventName;
57     SGTimeStamp time;
58
59 public: 
60     TimingInfo(string name, SGTimeStamp &t) { eventName = name; time = t;};
61     string getName() { return eventName; };
62     SGTimeStamp getTime() { return time; };
63 };
64
65 typedef vector<TimingInfo> eventTimeVec;
66 typedef vector<TimingInfo>::iterator eventTimeVecIterator;
67
68
69 \f
70 /**
71  * Basic interface for all FlightGear subsystems.
72  *
73  * <p>This is an abstract interface that all FlightGear subsystems
74  * will eventually implement.  It defines the basic operations for
75  * each subsystem: initialization, property binding and unbinding, and
76  * updating.  Interfaces may define additional methods, but the
77  * preferred way of exchanging information with other subsystems is
78  * through the property tree.</p>
79  *
80  * <p>To publish information through a property, a subsystem should
81  * bind it to a variable or (if necessary) a getter/setter pair in the
82  * bind() method, and release the property in the unbind() method:</p>
83  *
84  * <pre>
85  * void MySubsystem::bind ()
86  * {
87  *   fgTie("/controls/flight/elevator", &_elevator);
88  *   fgSetArchivable("/controls/flight/elevator");
89  * }
90  *
91  * void MySubsystem::unbind ()
92  * {
93  *   fgUntie("/controls/flight/elevator");
94  * }
95  * </pre>
96  *
97  * <p>To reference a property (possibly) from another subsystem, there
98  * are two alternatives.  If the property will be referenced only
99  * infrequently (say, in the init() method), then the fgGet* methods
100  * declared in fg_props.hxx are the simplest:</p>
101  *
102  * <pre>
103  * void MySubsystem::init ()
104  * {
105  *   _errorMargin = fgGetFloat("/display/error-margin-pct");
106  * }
107  * </pre>
108  *
109  * <p>On the other hand, if the property will be referenced frequently
110  * (say, in the update() method), then the hash-table lookup required
111  * by the fgGet* methods might be too expensive; instead, the
112  * subsystem should obtain a reference to the actual property node in
113  * its init() function and use that reference in the main loop:</p>
114  *
115  * <pre>
116  * void MySubsystem::init ()
117  * {
118  *   _errorNode = fgGetNode("/display/error-margin-pct", true);
119  * }
120  *
121  * void MySubsystem::update (double delta_time_sec)
122  * {
123  *   do_something(_errorNode.getFloatValue());
124  * }
125  * </pre>
126  *
127  * <p>The node returned will always be a pointer to SGPropertyNode,
128  * and the subsystem should <em>not</em> delete it in its destructor
129  * (the pointer belongs to the property tree, not the subsystem).</p>
130  *
131  * <p>The program may ask the subsystem to suspend or resume
132  * sim-time-dependent operations; by default, the suspend() and
133  * resume() methods set the protected variable <var>_suspended</var>,
134  * which the subsystem can reference in its update() method, but
135  * subsystems may also override the suspend() and resume() methods to
136  * take different actions.</p>
137  */
138 class SGSubsystem : public SGReferenced
139 {
140 public:
141
142   /**
143    * Default constructor.
144    */
145   SGSubsystem ();
146
147   /**
148    * Virtual destructor to ensure that subclass destructors are called.
149    */
150   virtual ~SGSubsystem ();
151
152
153   /**
154    * Initialize the subsystem.
155    *
156    * <p>This method should set up the state of the subsystem, but
157    * should not bind any properties.  Note that any dependencies on
158    * the state of other subsystems should be placed here rather than
159    * in the constructor, so that FlightGear can control the
160    * initialization order.</p>
161    */
162   virtual void init ();
163
164
165   /**
166    * Initialize parts that depend on other subsystems having been initialized.
167    *
168    * <p>This method should set up all parts that depend on other
169    * subsystems. One example is the scripting/Nasal subsystem, which
170    * is initialized last. So, if a subsystem wants to execute Nasal
171    * code in subsystem-specific configuration files, it has to do that
172    * in its postinit() method.</p>
173    */
174   virtual void postinit ();
175
176
177   /**
178    * Reinitialize the subsystem.
179    *
180    * <p>This method should cause the subsystem to reinitialize itself,
181    * and (normally) to reload any configuration files.</p>
182    */
183   virtual void reinit ();
184
185
186   /**
187    * Acquire the subsystem's property bindings.
188    *
189    * <p>This method should bind all properties that the subsystem
190    * publishes.  It will be invoked after init, but before any
191    * invocations of update.</p>
192    */
193   virtual void bind ();
194
195
196   /**
197    * Release the subsystem's property bindings.
198    *
199    * <p>This method should release all properties that the subsystem
200    * publishes.  It will be invoked by FlightGear (not the destructor)
201    * just before the subsystem is removed.</p>
202    */
203   virtual void unbind ();
204
205
206   /**
207    * Update the subsystem.
208    *
209    * <p>FlightGear invokes this method every time the subsystem should
210    * update its state.</p>
211    *
212    * @param delta_time_sec The delta time, in seconds, since the last
213    * update.  On first update, delta time will be 0.
214    */
215   virtual void update (double delta_time_sec) = 0;
216
217
218   /**
219    * Suspend operation of this subsystem.
220    *
221    * <p>This method instructs the subsystem to suspend
222    * sim-time-dependent operations until asked to resume.  The update
223    * method will still be invoked so that the subsystem can take any
224    * non-time-dependent actions, such as updating the display.</p>
225    *
226    * <p>It is not an error for the suspend method to be invoked when
227    * the subsystem is already suspended; the invocation should simply
228    * be ignored.</p>
229    */
230   virtual void suspend ();
231
232
233   /**
234    * Suspend or resum operation of this subsystem.
235    *
236    * @param suspended true if the subsystem should be suspended, false
237    * otherwise.
238    */
239   virtual void suspend (bool suspended);
240
241
242   /**
243    * Resume operation of this subsystem.
244    *
245    * <p>This method instructs the subsystem to resume
246    * sim-time-depended operations.  It is not an error for the resume
247    * method to be invoked when the subsystem is not suspended; the
248    * invocation should simply be ignored.</p>
249    */
250   virtual void resume ();
251
252
253   /**
254    * Test whether this subsystem is suspended.
255    *
256    * @return true if the subsystem is suspended, false if it is not.
257    */
258   virtual bool is_suspended () const;
259
260
261   /**
262    * Keep track of execution time.
263    *
264    * <p>This method keeps track of timing statistics for each subsystem.</p>
265    * 
266    * @param time execution time in ms of last call.
267    */
268   void updateExecutionTime(double time);
269
270   /**
271    * Print details of execution time.
272    *
273    * <p>For debugging purposes, developers can place stamp() calls
274    * at strategic points in the update() function of each subsystem, which 
275    * record the time between the successive calls to stamp. This method,
276    * printExecutionTime() is called after exectution of the subsystem
277    * update function itself to conduct a post-hoc analysis of excecution
278    * time</p>
279    */ 
280   void printTimingInformation();
281
282   /**
283    * Place time stamps at strategic points in the execution of subsystems 
284    * update() member functions. Predominantly for debugging purposes.
285    */
286   void stamp(string name);
287   
288
289
290 protected:
291
292   bool _suspended;
293
294   eventTimeVec timingInfo;
295   //int test;
296
297 };
298
299
300 \f
301 /**
302  * A group of FlightGear subsystems.
303  */
304 class SGSubsystemGroup : public SGSubsystem
305 {
306 public:
307
308     SGSubsystemGroup ();
309     virtual ~SGSubsystemGroup ();
310
311     virtual void init ();
312     virtual void postinit ();
313     virtual void reinit ();
314     virtual void bind ();
315     virtual void unbind ();
316     virtual void update (double delta_time_sec);
317     virtual void suspend ();
318     virtual void resume ();
319     virtual bool is_suspended () const;
320
321     virtual void set_subsystem (const string &name,
322                                 SGSubsystem * subsystem,
323                                 double min_step_sec = 0);
324     virtual SGSubsystem * get_subsystem (const string &name);
325     virtual void remove_subsystem (const string &name);
326     virtual bool has_subsystem (const string &name) const;
327
328     void collectDebugTiming(bool collect);
329
330 private:
331
332     struct Member {
333
334         Member ();
335         Member (const Member &member);
336         virtual ~Member ();
337
338         virtual void update (double delta_time_sec);
339         void printTimingInformation(double time);
340         void printTimingStatistics();
341         void updateExecutionTime(double time);
342         double getTimeWarningThreshold();
343         void collectDebugTiming (bool collect) { collectTimeStats = collect; };
344
345         SampleStatistic timeStat;
346         string name;
347         SGSubsystem * subsystem;
348         double min_step_sec;
349         double elapsed_sec;
350         bool collectTimeStats;
351     };
352
353     Member * get_member (const string &name, bool create = false);
354
355     vector<Member *> _members;
356 };
357
358
359 \f
360 /**
361  * Manage subsystems for FlightGear.
362  *
363  * This top-level subsystem will eventually manage all of the
364  * subsystems in FlightGear: it broadcasts its life-cycle events
365  * (init, bind, etc.) to all of the subsystems it manages.  Subsystems
366  * are grouped to guarantee order of initialization and execution --
367  * currently, the only two groups are INIT and GENERAL, but others
368  * will appear in the future.
369  *
370  * All subsystems are named as well as grouped, and subsystems can be
371  * looked up by name and cast to the appropriate subtype when another
372  * subsystem needs to invoke specialized methods.
373  *
374  * The subsystem manager owns the pointers to all the subsystems in
375  * it.
376  */
377 class SGSubsystemMgr : public SGSubsystem
378 {
379 public:
380
381     /**
382      * Types of subsystem groups.
383      */
384     enum GroupType {
385         INIT = 0,
386         GENERAL,
387         MAX_GROUPS
388     };
389
390     SGSubsystemMgr ();
391     virtual ~SGSubsystemMgr ();
392
393     virtual void init ();
394     virtual void postinit ();
395     virtual void reinit ();
396     virtual void bind ();
397     virtual void unbind ();
398     virtual void update (double delta_time_sec);
399     virtual void suspend ();
400     virtual void resume ();
401     virtual bool is_suspended () const;
402
403     virtual void add (const char * name,
404                       SGSubsystem * subsystem,
405                       GroupType group = GENERAL, 
406                       double min_time_sec = 0);
407
408     virtual SGSubsystemGroup * get_group (GroupType group);
409
410     virtual SGSubsystem * get_subsystem(const string &name);
411
412    void collectDebugTiming(bool collect);
413
414 private:
415
416     SGSubsystemGroup _groups[MAX_GROUPS];
417     map<string,SGSubsystem *> _subsystem_map;
418
419 };
420
421
422
423 #endif // __SUBSYSTEM_MGR_HXX
424