1 // Written by David Megginson, started 2000-12
3 // Copyright (C) 2000 David Megginson, david@megginson.com
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.
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.
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.
22 #ifndef __SUBSYSTEM_MGR_HXX
23 #define __SUBSYSTEM_MGR_HXX 1
26 #include <simgear/compiler.h>
36 #include <simgear/props/props.hxx>
37 #include <simgear/timing/timestamp.hxx>
38 #include "SGSmplstat.hxx"
48 TimingInfo(const string& name, const SGTimeStamp &t) :
49 eventName(name), time(t)
51 const string& getName() const { return eventName; }
52 const SGTimeStamp& getTime() const { return time; }
55 typedef vector<TimingInfo> eventTimeVec;
56 typedef vector<TimingInfo>::iterator eventTimeVecIterator;
61 * Basic interface for all FlightGear subsystems.
63 * <p>This is an abstract interface that all FlightGear subsystems
64 * will eventually implement. It defines the basic operations for
65 * each subsystem: initialization, property binding and unbinding, and
66 * updating. Interfaces may define additional methods, but the
67 * preferred way of exchanging information with other subsystems is
68 * through the property tree.</p>
70 * <p>To publish information through a property, a subsystem should
71 * bind it to a variable or (if necessary) a getter/setter pair in the
72 * bind() method, and release the property in the unbind() method:</p>
75 * void MySubsystem::bind ()
77 * fgTie("/controls/flight/elevator", &_elevator);
78 * fgSetArchivable("/controls/flight/elevator");
81 * void MySubsystem::unbind ()
83 * fgUntie("/controls/flight/elevator");
87 * <p>To reference a property (possibly) from another subsystem, there
88 * are two alternatives. If the property will be referenced only
89 * infrequently (say, in the init() method), then the fgGet* methods
90 * declared in fg_props.hxx are the simplest:</p>
93 * void MySubsystem::init ()
95 * _errorMargin = fgGetFloat("/display/error-margin-pct");
99 * <p>On the other hand, if the property will be referenced frequently
100 * (say, in the update() method), then the hash-table lookup required
101 * by the fgGet* methods might be too expensive; instead, the
102 * subsystem should obtain a reference to the actual property node in
103 * its init() function and use that reference in the main loop:</p>
106 * void MySubsystem::init ()
108 * _errorNode = fgGetNode("/display/error-margin-pct", true);
111 * void MySubsystem::update (double delta_time_sec)
113 * do_something(_errorNode.getFloatValue());
117 * <p>The node returned will always be a pointer to SGPropertyNode,
118 * and the subsystem should <em>not</em> delete it in its destructor
119 * (the pointer belongs to the property tree, not the subsystem).</p>
121 * <p>The program may ask the subsystem to suspend or resume
122 * sim-time-dependent operations; by default, the suspend() and
123 * resume() methods set the protected variable <var>_suspended</var>,
124 * which the subsystem can reference in its update() method, but
125 * subsystems may also override the suspend() and resume() methods to
126 * take different actions.</p>
128 class SGSubsystem : public SGReferenced
133 * Default constructor.
138 * Virtual destructor to ensure that subclass destructors are called.
140 virtual ~SGSubsystem ();
144 * Initialize the subsystem.
146 * <p>This method should set up the state of the subsystem, but
147 * should not bind any properties. Note that any dependencies on
148 * the state of other subsystems should be placed here rather than
149 * in the constructor, so that FlightGear can control the
150 * initialization order.</p>
152 virtual void init ();
156 * Initialize parts that depend on other subsystems having been initialized.
158 * <p>This method should set up all parts that depend on other
159 * subsystems. One example is the scripting/Nasal subsystem, which
160 * is initialized last. So, if a subsystem wants to execute Nasal
161 * code in subsystem-specific configuration files, it has to do that
162 * in its postinit() method.</p>
164 virtual void postinit ();
168 * Reinitialize the subsystem.
170 * <p>This method should cause the subsystem to reinitialize itself,
171 * and (normally) to reload any configuration files.</p>
173 virtual void reinit ();
177 * Acquire the subsystem's property bindings.
179 * <p>This method should bind all properties that the subsystem
180 * publishes. It will be invoked after init, but before any
181 * invocations of update.</p>
183 virtual void bind ();
187 * Release the subsystem's property bindings.
189 * <p>This method should release all properties that the subsystem
190 * publishes. It will be invoked by FlightGear (not the destructor)
191 * just before the subsystem is removed.</p>
193 virtual void unbind ();
197 * Update the subsystem.
199 * <p>FlightGear invokes this method every time the subsystem should
200 * update its state.</p>
202 * @param delta_time_sec The delta time, in seconds, since the last
203 * update. On first update, delta time will be 0.
205 virtual void update (double delta_time_sec) = 0;
209 * Suspend operation of this subsystem.
211 * <p>This method instructs the subsystem to suspend
212 * sim-time-dependent operations until asked to resume. The update
213 * method will still be invoked so that the subsystem can take any
214 * non-time-dependent actions, such as updating the display.</p>
216 * <p>It is not an error for the suspend method to be invoked when
217 * the subsystem is already suspended; the invocation should simply
220 virtual void suspend ();
224 * Suspend or resum operation of this subsystem.
226 * @param suspended true if the subsystem should be suspended, false
229 virtual void suspend (bool suspended);
233 * Resume operation of this subsystem.
235 * <p>This method instructs the subsystem to resume
236 * sim-time-depended operations. It is not an error for the resume
237 * method to be invoked when the subsystem is not suspended; the
238 * invocation should simply be ignored.</p>
240 virtual void resume ();
244 * Test whether this subsystem is suspended.
246 * @return true if the subsystem is suspended, false if it is not.
248 virtual bool is_suspended () const;
252 * Keep track of execution time.
254 * <p>This method keeps track of timing statistics for each subsystem.</p>
256 * @param time execution time in ms of last call.
258 void updateExecutionTime(double time);
261 * Print details of execution time.
263 * <p>For debugging purposes, developers can place stamp() calls
264 * at strategic points in the update() function of each subsystem, which
265 * record the time between the successive calls to stamp. This method,
266 * printExecutionTime() is called after exectution of the subsystem
267 * update function itself to conduct a post-hoc analysis of excecution
270 void printTimingInformation();
273 * Place time stamps at strategic points in the execution of subsystems
274 * update() member functions. Predominantly for debugging purposes.
276 void stamp(const string& name);
284 eventTimeVec timingInfo;
292 * A group of FlightGear subsystems.
294 class SGSubsystemGroup : public SGSubsystem
299 virtual ~SGSubsystemGroup ();
301 virtual void init ();
302 virtual void postinit ();
303 virtual void reinit ();
304 virtual void bind ();
305 virtual void unbind ();
306 virtual void update (double delta_time_sec);
307 virtual void suspend ();
308 virtual void resume ();
309 virtual bool is_suspended () const;
311 virtual void set_subsystem (const string &name,
312 SGSubsystem * subsystem,
313 double min_step_sec = 0);
314 virtual SGSubsystem * get_subsystem (const string &name);
315 virtual void remove_subsystem (const string &name);
316 virtual bool has_subsystem (const string &name) const;
318 void collectDebugTiming(bool collect);
323 void set_fixed_update_time(double fixed_dt);
329 Member (const Member &member);
334 virtual void update (double delta_time_sec);
335 void printTimingInformation(double time);
336 void printTimingStatistics();
337 void updateExecutionTime(double time);
338 double getTimeWarningThreshold();
339 void collectDebugTiming (bool collect) { collectTimeStats = collect; };
341 SampleStatistic timeStat;
343 SGSubsystem * subsystem;
346 bool collectTimeStats;
350 Member * get_member (const string &name, bool create = false);
352 vector<Member *> _members;
354 double _fixedUpdateTime;
355 double _updateTimeRemainder;
361 * Manage subsystems for FlightGear.
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.
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.
374 * The subsystem manager owns the pointers to all the subsystems in
377 class SGSubsystemMgr : public SGSubsystem
382 * Types of subsystem groups.
387 FDM, ///< flight model, autopilot, instruments that run coupled
388 POST_FDM, ///< certain subsystems depend on FDM data
389 DISPLAY, ///< view, camera, rendering updates
394 virtual ~SGSubsystemMgr ();
396 virtual void init ();
397 virtual void postinit ();
398 virtual void reinit ();
399 virtual void bind ();
400 virtual void unbind ();
401 virtual void update (double delta_time_sec);
402 virtual void suspend ();
403 virtual void resume ();
404 virtual bool is_suspended () const;
406 virtual void add (const char * name,
407 SGSubsystem * subsystem,
408 GroupType group = GENERAL,
409 double min_time_sec = 0);
411 virtual SGSubsystemGroup * get_group (GroupType group);
413 virtual SGSubsystem * get_subsystem(const string &name);
415 void collectDebugTiming(bool collect);
419 SGSubsystemGroup _groups[MAX_GROUPS];
420 map<string,SGSubsystem *> _subsystem_map;
426 #endif // __SUBSYSTEM_MGR_HXX