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(string name, SGTimeStamp &t) { eventName = name; time = t;};
49 string getName() { return eventName; };
50 SGTimeStamp getTime() { return time; };
53 typedef vector<TimingInfo> eventTimeVec;
54 typedef vector<TimingInfo>::iterator eventTimeVecIterator;
59 * Basic interface for all FlightGear subsystems.
61 * <p>This is an abstract interface that all FlightGear subsystems
62 * will eventually implement. It defines the basic operations for
63 * each subsystem: initialization, property binding and unbinding, and
64 * updating. Interfaces may define additional methods, but the
65 * preferred way of exchanging information with other subsystems is
66 * through the property tree.</p>
68 * <p>To publish information through a property, a subsystem should
69 * bind it to a variable or (if necessary) a getter/setter pair in the
70 * bind() method, and release the property in the unbind() method:</p>
73 * void MySubsystem::bind ()
75 * fgTie("/controls/flight/elevator", &_elevator);
76 * fgSetArchivable("/controls/flight/elevator");
79 * void MySubsystem::unbind ()
81 * fgUntie("/controls/flight/elevator");
85 * <p>To reference a property (possibly) from another subsystem, there
86 * are two alternatives. If the property will be referenced only
87 * infrequently (say, in the init() method), then the fgGet* methods
88 * declared in fg_props.hxx are the simplest:</p>
91 * void MySubsystem::init ()
93 * _errorMargin = fgGetFloat("/display/error-margin-pct");
97 * <p>On the other hand, if the property will be referenced frequently
98 * (say, in the update() method), then the hash-table lookup required
99 * by the fgGet* methods might be too expensive; instead, the
100 * subsystem should obtain a reference to the actual property node in
101 * its init() function and use that reference in the main loop:</p>
104 * void MySubsystem::init ()
106 * _errorNode = fgGetNode("/display/error-margin-pct", true);
109 * void MySubsystem::update (double delta_time_sec)
111 * do_something(_errorNode.getFloatValue());
115 * <p>The node returned will always be a pointer to SGPropertyNode,
116 * and the subsystem should <em>not</em> delete it in its destructor
117 * (the pointer belongs to the property tree, not the subsystem).</p>
119 * <p>The program may ask the subsystem to suspend or resume
120 * sim-time-dependent operations; by default, the suspend() and
121 * resume() methods set the protected variable <var>_suspended</var>,
122 * which the subsystem can reference in its update() method, but
123 * subsystems may also override the suspend() and resume() methods to
124 * take different actions.</p>
126 class SGSubsystem : public SGReferenced
131 * Default constructor.
136 * Virtual destructor to ensure that subclass destructors are called.
138 virtual ~SGSubsystem ();
142 * Initialize the subsystem.
144 * <p>This method should set up the state of the subsystem, but
145 * should not bind any properties. Note that any dependencies on
146 * the state of other subsystems should be placed here rather than
147 * in the constructor, so that FlightGear can control the
148 * initialization order.</p>
150 virtual void init ();
154 * Initialize parts that depend on other subsystems having been initialized.
156 * <p>This method should set up all parts that depend on other
157 * subsystems. One example is the scripting/Nasal subsystem, which
158 * is initialized last. So, if a subsystem wants to execute Nasal
159 * code in subsystem-specific configuration files, it has to do that
160 * in its postinit() method.</p>
162 virtual void postinit ();
166 * Reinitialize the subsystem.
168 * <p>This method should cause the subsystem to reinitialize itself,
169 * and (normally) to reload any configuration files.</p>
171 virtual void reinit ();
175 * Acquire the subsystem's property bindings.
177 * <p>This method should bind all properties that the subsystem
178 * publishes. It will be invoked after init, but before any
179 * invocations of update.</p>
181 virtual void bind ();
185 * Release the subsystem's property bindings.
187 * <p>This method should release all properties that the subsystem
188 * publishes. It will be invoked by FlightGear (not the destructor)
189 * just before the subsystem is removed.</p>
191 virtual void unbind ();
195 * Update the subsystem.
197 * <p>FlightGear invokes this method every time the subsystem should
198 * update its state.</p>
200 * @param delta_time_sec The delta time, in seconds, since the last
201 * update. On first update, delta time will be 0.
203 virtual void update (double delta_time_sec) = 0;
207 * Suspend operation of this subsystem.
209 * <p>This method instructs the subsystem to suspend
210 * sim-time-dependent operations until asked to resume. The update
211 * method will still be invoked so that the subsystem can take any
212 * non-time-dependent actions, such as updating the display.</p>
214 * <p>It is not an error for the suspend method to be invoked when
215 * the subsystem is already suspended; the invocation should simply
218 virtual void suspend ();
222 * Suspend or resum operation of this subsystem.
224 * @param suspended true if the subsystem should be suspended, false
227 virtual void suspend (bool suspended);
231 * Resume operation of this subsystem.
233 * <p>This method instructs the subsystem to resume
234 * sim-time-depended operations. It is not an error for the resume
235 * method to be invoked when the subsystem is not suspended; the
236 * invocation should simply be ignored.</p>
238 virtual void resume ();
242 * Test whether this subsystem is suspended.
244 * @return true if the subsystem is suspended, false if it is not.
246 virtual bool is_suspended () const;
250 * Keep track of execution time.
252 * <p>This method keeps track of timing statistics for each subsystem.</p>
254 * @param time execution time in ms of last call.
256 void updateExecutionTime(double time);
259 * Print details of execution time.
261 * <p>For debugging purposes, developers can place stamp() calls
262 * at strategic points in the update() function of each subsystem, which
263 * record the time between the successive calls to stamp. This method,
264 * printExecutionTime() is called after exectution of the subsystem
265 * update function itself to conduct a post-hoc analysis of excecution
268 void printTimingInformation();
271 * Place time stamps at strategic points in the execution of subsystems
272 * update() member functions. Predominantly for debugging purposes.
274 void stamp(string name);
282 eventTimeVec timingInfo;
290 * A group of FlightGear subsystems.
292 class SGSubsystemGroup : public SGSubsystem
297 virtual ~SGSubsystemGroup ();
299 virtual void init ();
300 virtual void postinit ();
301 virtual void reinit ();
302 virtual void bind ();
303 virtual void unbind ();
304 virtual void update (double delta_time_sec);
305 virtual void suspend ();
306 virtual void resume ();
307 virtual bool is_suspended () const;
309 virtual void set_subsystem (const string &name,
310 SGSubsystem * subsystem,
311 double min_step_sec = 0);
312 virtual SGSubsystem * get_subsystem (const string &name);
313 virtual void remove_subsystem (const string &name);
314 virtual bool has_subsystem (const string &name) const;
316 void collectDebugTiming(bool collect);
323 Member (const Member &member);
326 virtual void update (double delta_time_sec);
327 void printTimingInformation(double time);
328 void printTimingStatistics();
329 void updateExecutionTime(double time);
330 double getTimeWarningThreshold();
331 void collectDebugTiming (bool collect) { collectTimeStats = collect; };
333 SampleStatistic timeStat;
335 SGSubsystem * subsystem;
338 bool collectTimeStats;
341 Member * get_member (const string &name, bool create = false);
343 vector<Member *> _members;
349 * Manage subsystems for FlightGear.
351 * This top-level subsystem will eventually manage all of the
352 * subsystems in FlightGear: it broadcasts its life-cycle events
353 * (init, bind, etc.) to all of the subsystems it manages. Subsystems
354 * are grouped to guarantee order of initialization and execution --
355 * currently, the only two groups are INIT and GENERAL, but others
356 * will appear in the future.
358 * All subsystems are named as well as grouped, and subsystems can be
359 * looked up by name and cast to the appropriate subtype when another
360 * subsystem needs to invoke specialized methods.
362 * The subsystem manager owns the pointers to all the subsystems in
365 class SGSubsystemMgr : public SGSubsystem
370 * Types of subsystem groups.
379 virtual ~SGSubsystemMgr ();
381 virtual void init ();
382 virtual void postinit ();
383 virtual void reinit ();
384 virtual void bind ();
385 virtual void unbind ();
386 virtual void update (double delta_time_sec);
387 virtual void suspend ();
388 virtual void resume ();
389 virtual bool is_suspended () const;
391 virtual void add (const char * name,
392 SGSubsystem * subsystem,
393 GroupType group = GENERAL,
394 double min_time_sec = 0);
396 virtual SGSubsystemGroup * get_group (GroupType group);
398 virtual SGSubsystem * get_subsystem(const string &name);
400 void collectDebugTiming(bool collect);
404 SGSubsystemGroup _groups[MAX_GROUPS];
405 map<string,SGSubsystem *> _subsystem_map;
411 #endif // __SUBSYSTEM_MGR_HXX