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>
48 #include <simgear/props/props.hxx>
49 #include <simgear/timing/timestamp.hxx>
50 #include "SGSmplstat.hxx"
60 TimingInfo(string name, SGTimeStamp &t) { eventName = name; time = t;};
61 string getName() { return eventName; };
62 SGTimeStamp getTime() { return time; };
65 typedef vector<TimingInfo> eventTimeVec;
66 typedef vector<TimingInfo>::iterator eventTimeVecIterator;
71 * Basic interface for all FlightGear subsystems.
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>
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>
85 * void MySubsystem::bind ()
87 * fgTie("/controls/flight/elevator", &_elevator);
88 * fgSetArchivable("/controls/flight/elevator");
91 * void MySubsystem::unbind ()
93 * fgUntie("/controls/flight/elevator");
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>
103 * void MySubsystem::init ()
105 * _errorMargin = fgGetFloat("/display/error-margin-pct");
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>
116 * void MySubsystem::init ()
118 * _errorNode = fgGetNode("/display/error-margin-pct", true);
121 * void MySubsystem::update (double delta_time_sec)
123 * do_something(_errorNode.getFloatValue());
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>
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>
143 * Default constructor.
148 * Virtual destructor to ensure that subclass destructors are called.
150 virtual ~SGSubsystem ();
154 * Initialize the subsystem.
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>
162 virtual void init ();
166 * Initialize parts that depend on other subsystems having been initialized.
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>
174 virtual void postinit ();
178 * Reinitialize the subsystem.
180 * <p>This method should cause the subsystem to reinitialize itself,
181 * and (normally) to reload any configuration files.</p>
183 virtual void reinit ();
187 * Acquire the subsystem's property bindings.
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>
193 virtual void bind ();
197 * Release the subsystem's property bindings.
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>
203 virtual void unbind ();
207 * Update the subsystem.
209 * <p>FlightGear invokes this method every time the subsystem should
210 * update its state.</p>
212 * @param delta_time_sec The delta time, in seconds, since the last
213 * update. On first update, delta time will be 0.
215 virtual void update (double delta_time_sec) = 0;
219 * Suspend operation of this subsystem.
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>
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
230 virtual void suspend ();
234 * Suspend or resum operation of this subsystem.
236 * @param suspended true if the subsystem should be suspended, false
239 virtual void suspend (bool suspended);
243 * Resume operation of this subsystem.
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>
250 virtual void resume ();
254 * Test whether this subsystem is suspended.
256 * @return true if the subsystem is suspended, false if it is not.
258 virtual bool is_suspended () const;
262 * Keep track of execution time.
264 * <p>This method keeps track of timing statistics for each subsystem.</p>
266 * @param time execution time in ms of last call.
268 void updateExecutionTime(double time);
271 * Print details of execution time.
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
280 void printTimingInformation();
283 * Place time stamps at strategic points in the execution of subsystems
284 * update() member functions. Predominantly for debugging purposes.
286 void stamp(string name);
294 eventTimeVec timingInfo;
302 * A group of FlightGear subsystems.
304 class SGSubsystemGroup : public SGSubsystem
309 virtual ~SGSubsystemGroup ();
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;
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;
328 void collectDebugTiming(bool collect);
335 Member (const Member &member);
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; };
345 SampleStatistic timeStat;
347 SGSubsystem * subsystem;
350 bool collectTimeStats;
353 Member * get_member (const string &name, bool create = false);
355 vector<Member *> _members;
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.
391 virtual ~SGSubsystemMgr ();
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;
403 virtual void add (const char * name,
404 SGSubsystem * subsystem,
405 GroupType group = GENERAL,
406 double min_time_sec = 0);
408 virtual SGSubsystemGroup * get_group (GroupType group);
410 virtual SGSubsystem * get_subsystem(const string &name);
412 void collectDebugTiming(bool collect);
416 SGSubsystemGroup _groups[MAX_GROUPS];
417 map<string,SGSubsystem *> _subsystem_map;
423 #endif // __SUBSYSTEM_MGR_HXX