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>
44 #include <simgear/props/props.hxx>
49 * Basic interface for all FlightGear subsystems.
51 * <p>This is an abstract interface that all FlightGear subsystems
52 * will eventually implement. It defines the basic operations for
53 * each subsystem: initialization, property binding and unbinding, and
54 * updating. Interfaces may define additional methods, but the
55 * preferred way of exchanging information with other subsystems is
56 * through the property tree.</p>
58 * <p>To publish information through a property, a subsystem should
59 * bind it to a variable or (if necessary) a getter/setter pair in the
60 * bind() method, and release the property in the unbind() method:</p>
63 * void MySubsystem::bind ()
65 * fgTie("/controls/flight/elevator", &_elevator);
66 * fgSetArchivable("/controls/flight/elevator");
69 * void MySubsystem::unbind ()
71 * fgUntie("/controls/flight/elevator");
75 * <p>To reference a property (possibly) from another subsystem, there
76 * are two alternatives. If the property will be referenced only
77 * infrequently (say, in the init() method), then the fgGet* methods
78 * declared in fg_props.hxx are the simplest:</p>
81 * void MySubsystem::init ()
83 * _errorMargin = fgGetFloat("/display/error-margin-pct");
87 * <p>On the other hand, if the property will be referenced frequently
88 * (say, in the update() method), then the hash-table lookup required
89 * by the fgGet* methods might be too expensive; instead, the
90 * subsystem should obtain a reference to the actual property node in
91 * its init() function and use that reference in the main loop:</p>
94 * void MySubsystem::init ()
96 * _errorNode = fgGetNode("/display/error-margin-pct", true);
99 * void MySubsystem::update (double delta_time_sec)
101 * do_something(_errorNode.getFloatValue());
105 * <p>The node returned will always be a pointer to SGPropertyNode,
106 * and the subsystem should <em>not</em> delete it in its destructor
107 * (the pointer belongs to the property tree, not the subsystem).</p>
109 * <p>The program may ask the subsystem to suspend or resume
110 * sim-time-dependent operations; by default, the suspend() and
111 * resume() methods set the protected variable <var>_suspended</var>,
112 * which the subsystem can reference in its update() method, but
113 * subsystems may also override the suspend() and resume() methods to
114 * take different actions.</p>
121 * Default constructor.
126 * Virtual destructor to ensure that subclass destructors are called.
128 virtual ~SGSubsystem ();
132 * Initialize the subsystem.
134 * <p>This method should set up the state of the subsystem, but
135 * should not bind any properties. Note that any dependencies on
136 * the state of other subsystems should be placed here rather than
137 * in the constructor, so that FlightGear can control the
138 * initialization order.</p>
140 virtual void init ();
144 * Initialize parts that depend on other subsystems having been initialized.
146 * <p>This method should set up all parts that depend on other
147 * subsystems. One example is the scripting/Nasal subsystem, which
148 * is initialized last. So, if a subsystem wants to execute Nasal
149 * code in subsystem-specific configuration files, it has to do that
150 * in its postinit() method.</p>
152 virtual void postinit ();
156 * Reinitialize the subsystem.
158 * <p>This method should cause the subsystem to reinitialize itself,
159 * and (normally) to reload any configuration files.</p>
161 virtual void reinit ();
165 * Acquire the subsystem's property bindings.
167 * <p>This method should bind all properties that the subsystem
168 * publishes. It will be invoked after init, but before any
169 * invocations of update.</p>
171 virtual void bind ();
175 * Release the subsystem's property bindings.
177 * <p>This method should release all properties that the subsystem
178 * publishes. It will be invoked by FlightGear (not the destructor)
179 * just before the subsystem is removed.</p>
181 virtual void unbind ();
185 * Update the subsystem.
187 * <p>FlightGear invokes this method every time the subsystem should
188 * update its state.</p>
190 * @param delta_time_sec The delta time, in seconds, since the last
191 * update. On first update, delta time will be 0.
193 virtual void update (double delta_time_sec) = 0;
197 * Suspend operation of this subsystem.
199 * <p>This method instructs the subsystem to suspend
200 * sim-time-dependent operations until asked to resume. The update
201 * method will still be invoked so that the subsystem can take any
202 * non-time-dependent actions, such as updating the display.</p>
204 * <p>It is not an error for the suspend method to be invoked when
205 * the subsystem is already suspended; the invocation should simply
208 virtual void suspend ();
212 * Suspend or resum operation of this subsystem.
214 * @param suspended true if the subsystem should be suspended, false
217 virtual void suspend (bool suspended);
221 * Resume operation of this subsystem.
223 * <p>This method instructs the subsystem to resume
224 * sim-time-depended operations. It is not an error for the resume
225 * method to be invoked when the subsystem is not suspended; the
226 * invocation should simply be ignored.</p>
228 virtual void resume ();
232 * Test whether this subsystem is suspended.
234 * @return true if the subsystem is suspended, false if it is not.
236 virtual bool is_suspended () const;
248 * A group of FlightGear subsystems.
250 class SGSubsystemGroup : public SGSubsystem
255 virtual ~SGSubsystemGroup ();
257 virtual void init ();
258 virtual void postinit ();
259 virtual void reinit ();
260 virtual void bind ();
261 virtual void unbind ();
262 virtual void update (double delta_time_sec);
263 virtual void suspend ();
264 virtual void resume ();
265 virtual bool is_suspended () const;
267 virtual void set_subsystem (const string &name,
268 SGSubsystem * subsystem,
269 double min_step_sec = 0);
270 virtual SGSubsystem * get_subsystem (const string &name);
271 virtual void remove_subsystem (const string &name);
272 virtual bool has_subsystem (const string &name) const;
279 Member (const Member &member);
282 virtual void update (double delta_time_sec);
285 SGSubsystem * subsystem;
290 Member * get_member (const string &name, bool create = false);
292 vector<Member *> _members;
298 * Manage subsystems for FlightGear.
300 * This top-level subsystem will eventually manage all of the
301 * subsystems in FlightGear: it broadcasts its life-cycle events
302 * (init, bind, etc.) to all of the subsystems it manages. Subsystems
303 * are grouped to guarantee order of initialization and execution --
304 * currently, the only two groups are INIT and GENERAL, but others
305 * will appear in the future.
307 * All subsystems are named as well as grouped, and subsystems can be
308 * looked up by name and cast to the appropriate subtype when another
309 * subsystem needs to invoke specialized methods.
311 * The subsystem manager owns the pointers to all the subsystems in
314 class SGSubsystemMgr : public SGSubsystem
319 * Types of subsystem groups.
328 virtual ~SGSubsystemMgr ();
330 virtual void init ();
331 virtual void postinit ();
332 virtual void reinit ();
333 virtual void bind ();
334 virtual void unbind ();
335 virtual void update (double delta_time_sec);
336 virtual void suspend ();
337 virtual void resume ();
338 virtual bool is_suspended () const;
340 virtual void add (const char * name,
341 SGSubsystem * subsystem,
342 GroupType group = GENERAL,
343 double min_time_sec = 0);
345 virtual SGSubsystemGroup * get_group (GroupType group);
347 virtual SGSubsystem * get_subsystem(const string &name);
351 SGSubsystemGroup _groups[MAX_GROUPS];
352 map<string,SGSubsystem *> _subsystem_map;
358 #endif // __SUBSYSTEM_MGR_HXX