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