2 // Written by David Megginson, started 2000-12
4 // Copyright (C) 2000 David Megginson, david@megginson.com
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License as
8 // published by the Free Software Foundation; either version 2 of the
9 // License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #ifndef __SUBSYSTEM_MGR_HXX
24 #define __SUBSYSTEM_MGR_HXX 1
27 #include <simgear/compiler.h>
33 #include <simgear/timing/timestamp.hxx>
34 #include <simgear/structure/SGSharedPtr.hxx>
40 std::string eventName;
44 TimingInfo(const std::string& name, const SGTimeStamp &t) :
45 eventName(name), time(t)
47 const std::string& getName() const { return eventName; }
48 const SGTimeStamp& getTime() const { return time; }
51 class SampleStatistic;
53 typedef std::vector<TimingInfo> eventTimeVec;
54 typedef std::vector<TimingInfo>::iterator eventTimeVecIterator;
56 typedef void (*SGSubsystemTimingCb)(void* userData, const std::string& name, SampleStatistic* pStatistic);
60 * Basic interface for all FlightGear subsystems.
62 * <p>This is an abstract interface that all FlightGear subsystems
63 * will eventually implement. It defines the basic operations for
64 * each subsystem: initialization, property binding and unbinding, and
65 * updating. Interfaces may define additional methods, but the
66 * preferred way of exchanging information with other subsystems is
67 * through the property tree.</p>
69 * <p>To publish information through a property, a subsystem should
70 * bind it to a variable or (if necessary) a getter/setter pair in the
71 * bind() method, and release the property in the unbind() method:</p>
74 * void MySubsystem::bind ()
76 * fgTie("/controls/flight/elevator", &_elevator);
77 * fgSetArchivable("/controls/flight/elevator");
80 * void MySubsystem::unbind ()
82 * fgUntie("/controls/flight/elevator");
86 * <p>To reference a property (possibly) from another subsystem, there
87 * are two alternatives. If the property will be referenced only
88 * infrequently (say, in the init() method), then the fgGet* methods
89 * declared in fg_props.hxx are the simplest:</p>
92 * void MySubsystem::init ()
94 * _errorMargin = fgGetFloat("/display/error-margin-pct");
98 * <p>On the other hand, if the property will be referenced frequently
99 * (say, in the update() method), then the hash-table lookup required
100 * by the fgGet* methods might be too expensive; instead, the
101 * subsystem should obtain a reference to the actual property node in
102 * its init() function and use that reference in the main loop:</p>
105 * void MySubsystem::init ()
107 * _errorNode = fgGetNode("/display/error-margin-pct", true);
110 * void MySubsystem::update (double delta_time_sec)
112 * do_something(_errorNode.getFloatValue());
116 * <p>The node returned will always be a pointer to SGPropertyNode,
117 * and the subsystem should <em>not</em> delete it in its destructor
118 * (the pointer belongs to the property tree, not the subsystem).</p>
120 * <p>The program may ask the subsystem to suspend or resume
121 * sim-time-dependent operations; by default, the suspend() and
122 * resume() methods set the protected variable <var>_suspended</var>,
123 * which the subsystem can reference in its update() method, but
124 * subsystems may also override the suspend() and resume() methods to
125 * take different actions.</p>
127 class SGSubsystem : public SGReferenced
132 * Default constructor.
137 * Virtual destructor to ensure that subclass destructors are called.
139 virtual ~SGSubsystem ();
143 * Initialize the subsystem.
145 * <p>This method should set up the state of the subsystem, but
146 * should not bind any properties. Note that any dependencies on
147 * the state of other subsystems should be placed here rather than
148 * in the constructor, so that FlightGear can control the
149 * initialization order.</p>
151 virtual void init ();
155 * Initialize parts that depend on other subsystems having been initialized.
157 * <p>This method should set up all parts that depend on other
158 * subsystems. One example is the scripting/Nasal subsystem, which
159 * is initialized last. So, if a subsystem wants to execute Nasal
160 * code in subsystem-specific configuration files, it has to do that
161 * in its postinit() method.</p>
163 virtual void postinit ();
167 * Reinitialize the subsystem.
169 * <p>This method should cause the subsystem to reinitialize itself,
170 * and (normally) to reload any configuration files.</p>
172 virtual void reinit ();
176 * Shutdown the subsystem.
178 * <p>Release any state associated with subsystem. Shutdown happens in
179 * the reverse order to init(), so this is the correct place to do
180 * shutdown that depends on other subsystems.
183 virtual void shutdown ();
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 resume 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;
260 * Trigger the callback to report timing information for all subsystems.
262 void reportTiming(void);
265 * Place time stamps at strategic points in the execution of subsystems
266 * update() member functions. Predominantly for debugging purposes.
268 void stamp(const std::string& name);
274 eventTimeVec timingInfo;
276 static SGSubsystemTimingCb reportTimingCb;
277 static void* reportTimingUserData;
283 * A group of FlightGear subsystems.
285 class SGSubsystemGroup : public SGSubsystem
290 virtual ~SGSubsystemGroup ();
292 virtual void init ();
293 virtual void postinit ();
294 virtual void reinit ();
295 virtual void shutdown ();
296 virtual void bind ();
297 virtual void unbind ();
298 virtual void update (double delta_time_sec);
299 virtual void suspend ();
300 virtual void resume ();
301 virtual bool is_suspended () const;
303 virtual void set_subsystem (const std::string &name,
304 SGSubsystem * subsystem,
305 double min_step_sec = 0);
306 virtual SGSubsystem * get_subsystem (const std::string &name);
307 virtual void remove_subsystem (const std::string &name);
308 virtual bool has_subsystem (const std::string &name) const;
310 void reportTiming(void);
315 void set_fixed_update_time(double fixed_dt);
319 Member* get_member (const std::string &name, bool create = false);
321 std::vector<Member *> _members;
323 double _fixedUpdateTime;
324 double _updateTimeRemainder;
330 * Manage subsystems for FlightGear.
332 * This top-level subsystem will eventually manage all of the
333 * subsystems in FlightGear: it broadcasts its life-cycle events
334 * (init, bind, etc.) to all of the subsystems it manages. Subsystems
335 * are grouped to guarantee order of initialization and execution --
336 * currently, the only two groups are INIT and GENERAL, but others
337 * will appear in the future.
339 * All subsystems are named as well as grouped, and subsystems can be
340 * looked up by name and cast to the appropriate subtype when another
341 * subsystem needs to invoke specialized methods.
343 * The subsystem manager owns the pointers to all the subsystems in
346 class SGSubsystemMgr : public SGSubsystem
351 * Types of subsystem groups.
356 FDM, ///< flight model, autopilot, instruments that run coupled
357 POST_FDM, ///< certain subsystems depend on FDM data
358 DISPLAY, ///< view, camera, rendering updates
359 SOUND/*I want to be last!*/, ///< needs to run AFTER display, to allow concurrent GPU/sound processing
364 virtual ~SGSubsystemMgr ();
366 virtual void init ();
367 virtual void postinit ();
368 virtual void reinit ();
369 virtual void shutdown ();
370 virtual void bind ();
371 virtual void unbind ();
372 virtual void update (double delta_time_sec);
373 virtual void suspend ();
374 virtual void resume ();
375 virtual bool is_suspended () const;
377 virtual void add (const char * name,
378 SGSubsystem * subsystem,
379 GroupType group = GENERAL,
380 double min_time_sec = 0);
383 * remove a subsystem, and return a pointer to it.
384 * returns NULL if the subsystem was not found.
386 virtual SGSubsystem* remove(const char* name);
388 virtual SGSubsystemGroup * get_group (GroupType group);
390 virtual SGSubsystem * get_subsystem(const std::string &name) const;
393 void setReportTimingCb(void* userData,SGSubsystemTimingCb cb) {reportTimingCb = cb;reportTimingUserData = userData;}
396 SGSubsystemGroup* _groups[MAX_GROUPS];
398 typedef std::map<std::string, SGSubsystem*> SubsystemDict;
399 SubsystemDict _subsystem_map;
404 #endif // __SUBSYSTEM_MGR_HXX