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>
59 TimingInfo(string name, SGTimeStamp &t) { eventName = name; time = t;};
60 string getName() { return eventName; };
61 SGTimeStamp getTime() { return time; };
64 typedef vector<TimingInfo> eventTimeVec;
65 typedef vector<TimingInfo>::iterator eventTimeVecIterator;
70 * Basic interface for all FlightGear subsystems.
72 * <p>This is an abstract interface that all FlightGear subsystems
73 * will eventually implement. It defines the basic operations for
74 * each subsystem: initialization, property binding and unbinding, and
75 * updating. Interfaces may define additional methods, but the
76 * preferred way of exchanging information with other subsystems is
77 * through the property tree.</p>
79 * <p>To publish information through a property, a subsystem should
80 * bind it to a variable or (if necessary) a getter/setter pair in the
81 * bind() method, and release the property in the unbind() method:</p>
84 * void MySubsystem::bind ()
86 * fgTie("/controls/flight/elevator", &_elevator);
87 * fgSetArchivable("/controls/flight/elevator");
90 * void MySubsystem::unbind ()
92 * fgUntie("/controls/flight/elevator");
96 * <p>To reference a property (possibly) from another subsystem, there
97 * are two alternatives. If the property will be referenced only
98 * infrequently (say, in the init() method), then the fgGet* methods
99 * declared in fg_props.hxx are the simplest:</p>
102 * void MySubsystem::init ()
104 * _errorMargin = fgGetFloat("/display/error-margin-pct");
108 * <p>On the other hand, if the property will be referenced frequently
109 * (say, in the update() method), then the hash-table lookup required
110 * by the fgGet* methods might be too expensive; instead, the
111 * subsystem should obtain a reference to the actual property node in
112 * its init() function and use that reference in the main loop:</p>
115 * void MySubsystem::init ()
117 * _errorNode = fgGetNode("/display/error-margin-pct", true);
120 * void MySubsystem::update (double delta_time_sec)
122 * do_something(_errorNode.getFloatValue());
126 * <p>The node returned will always be a pointer to SGPropertyNode,
127 * and the subsystem should <em>not</em> delete it in its destructor
128 * (the pointer belongs to the property tree, not the subsystem).</p>
130 * <p>The program may ask the subsystem to suspend or resume
131 * sim-time-dependent operations; by default, the suspend() and
132 * resume() methods set the protected variable <var>_suspended</var>,
133 * which the subsystem can reference in its update() method, but
134 * subsystems may also override the suspend() and resume() methods to
135 * take different actions.</p>
142 * Default constructor.
147 * Virtual destructor to ensure that subclass destructors are called.
149 virtual ~SGSubsystem ();
153 * Initialize the subsystem.
155 * <p>This method should set up the state of the subsystem, but
156 * should not bind any properties. Note that any dependencies on
157 * the state of other subsystems should be placed here rather than
158 * in the constructor, so that FlightGear can control the
159 * initialization order.</p>
161 virtual void init ();
165 * Initialize parts that depend on other subsystems having been initialized.
167 * <p>This method should set up all parts that depend on other
168 * subsystems. One example is the scripting/Nasal subsystem, which
169 * is initialized last. So, if a subsystem wants to execute Nasal
170 * code in subsystem-specific configuration files, it has to do that
171 * in its postinit() method.</p>
173 virtual void postinit ();
177 * Reinitialize the subsystem.
179 * <p>This method should cause the subsystem to reinitialize itself,
180 * and (normally) to reload any configuration files.</p>
182 virtual void reinit ();
186 * Acquire the subsystem's property bindings.
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>
192 virtual void bind ();
196 * Release the subsystem's property bindings.
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>
202 virtual void unbind ();
206 * Update the subsystem.
208 * <p>FlightGear invokes this method every time the subsystem should
209 * update its state.</p>
211 * @param delta_time_sec The delta time, in seconds, since the last
212 * update. On first update, delta time will be 0.
214 virtual void update (double delta_time_sec) = 0;
218 * Suspend operation of this subsystem.
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>
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
229 virtual void suspend ();
233 * Suspend or resum operation of this subsystem.
235 * @param suspended true if the subsystem should be suspended, false
238 virtual void suspend (bool suspended);
242 * Resume operation of this subsystem.
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>
249 virtual void resume ();
253 * Test whether this subsystem is suspended.
255 * @return true if the subsystem is suspended, false if it is not.
257 virtual bool is_suspended () const;
259 void printTimingInformation();
261 void stamp(string name);
268 eventTimeVec timingInfo;
276 * A group of FlightGear subsystems.
278 class SGSubsystemGroup : public SGSubsystem
283 virtual ~SGSubsystemGroup ();
285 virtual void init ();
286 virtual void postinit ();
287 virtual void reinit ();
288 virtual void bind ();
289 virtual void unbind ();
290 virtual void update (double delta_time_sec);
291 virtual void suspend ();
292 virtual void resume ();
293 virtual bool is_suspended () const;
295 virtual void set_subsystem (const string &name,
296 SGSubsystem * subsystem,
297 double min_step_sec = 0);
298 virtual SGSubsystem * get_subsystem (const string &name);
299 virtual void remove_subsystem (const string &name);
300 virtual bool has_subsystem (const string &name) const;
308 Member (const Member &member);
311 virtual void update (double delta_time_sec);
312 void printTimingInformation();
315 SGSubsystem * subsystem;
320 Member * get_member (const string &name, bool create = false);
322 vector<Member *> _members;
328 * Manage subsystems for FlightGear.
330 * This top-level subsystem will eventually manage all of the
331 * subsystems in FlightGear: it broadcasts its life-cycle events
332 * (init, bind, etc.) to all of the subsystems it manages. Subsystems
333 * are grouped to guarantee order of initialization and execution --
334 * currently, the only two groups are INIT and GENERAL, but others
335 * will appear in the future.
337 * All subsystems are named as well as grouped, and subsystems can be
338 * looked up by name and cast to the appropriate subtype when another
339 * subsystem needs to invoke specialized methods.
341 * The subsystem manager owns the pointers to all the subsystems in
344 class SGSubsystemMgr : public SGSubsystem
349 * Types of subsystem groups.
358 virtual ~SGSubsystemMgr ();
360 virtual void init ();
361 virtual void postinit ();
362 virtual void reinit ();
363 virtual void bind ();
364 virtual void unbind ();
365 virtual void update (double delta_time_sec);
366 virtual void suspend ();
367 virtual void resume ();
368 virtual bool is_suspended () const;
370 virtual void add (const char * name,
371 SGSubsystem * subsystem,
372 GroupType group = GENERAL,
373 double min_time_sec = 0);
375 virtual SGSubsystemGroup * get_group (GroupType group);
377 virtual SGSubsystem * get_subsystem(const string &name);
381 SGSubsystemGroup _groups[MAX_GROUPS];
382 map<string,SGSubsystem *> _subsystem_map;
388 #endif // __SUBSYSTEM_MGR_HXX