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