]> git.mxchange.org Git - flightgear.git/blob - src/Main/fgfs.hxx
Modify the property-assign command to optionally copy a value from
[flightgear.git] / src / Main / fgfs.hxx
1 // fgfs.hxx -- top level include file for FlightGear.
2 //
3 // Written by David Megginson, started 2000-12
4 //
5 // Copyright (C) 2000  David Megginson, david@megginson.com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #ifndef __FGFS_HXX
25 #define __FGFS_HXX 1
26
27
28 #ifdef HAVE_CONFIG_H
29 #  include <config.h>
30 #endif
31
32 #include <simgear/compiler.h>
33
34 // #ifdef SG_MATH_EXCEPTION_CLASH
35 // #  include <math.h>
36 // #endif
37
38 #ifdef HAVE_WINDOWS_H
39 #  include <windows.h>                     
40 #  include <float.h>                    
41 #endif
42
43 #include STL_STRING
44 SG_USING_STD(string);
45
46 #include <vector>
47 SG_USING_STD(vector);
48
49 #include <map>
50 SG_USING_STD(map);
51
52 #include <simgear/misc/props.hxx>
53
54
55 \f
56 /**
57  * Basic interface for all FlightGear subsystems.
58  *
59  * <p>This is an abstract interface that all FlightGear subsystems
60  * will eventually implement.  It defines the basic operations for
61  * each subsystem: initialization, property binding and unbinding, and
62  * updating.  Interfaces may define additional methods, but the
63  * preferred way of exchanging information with other subsystems is
64  * through the property tree.</p>
65  *
66  * <p>To publish information through a property, a subsystem should
67  * bind it to a variable or (if necessary) a getter/setter pair in the
68  * bind() method, and release the property in the unbind() method:</p>
69  *
70  * <pre>
71  * void MySubsystem::bind ()
72  * {
73  *   fgTie("/controls/elevator", &_elevator);
74  *   fgSetArchivable("/controls/elevator");
75  * }
76  *
77  * void MySubsystem::unbind ()
78  * {
79  *   fgUntie("/controls/elevator");
80  * }
81  * </pre>
82  *
83  * <p>To reference a property (possibly) from another subsystem, there
84  * are two alternatives.  If the property will be referenced only
85  * infrequently (say, in the init() method), then the fgGet* methods
86  * declared in fg_props.hxx are the simplest:</p>
87  *
88  * <pre>
89  * void MySubsystem::init ()
90  * {
91  *   _errorMargin = fgGetFloat("/display/error-margin-pct");
92  * }
93  * </pre>
94  *
95  * <p>On the other hand, if the property will be referenced frequently
96  * (say, in the update() method), then the hash-table lookup required
97  * by the fgGet* methods might be too expensive; instead, the
98  * subsystem should obtain a reference to the actual property node in
99  * its init() function and use that reference in the main loop:</p>
100  *
101  * <pre>
102  * void MySubsystem::init ()
103  * {
104  *   _errorNode = fgGetNode("/display/error-margin-pct", true);
105  * }
106  *
107  * void MySubsystem::update (double delta_time_sec)
108  * {
109  *   do_something(_errorNode.getFloatValue());
110  * }
111  * </pre>
112  *
113  * <p>The node returned will always be a pointer to SGPropertyNode,
114  * and the subsystem should <em>not</em> delete it in its destructor
115  * (the pointer belongs to the property tree, not the subsystem).</p>
116  *
117  * <p>The program may ask the subsystem to suspend or resume
118  * sim-time-dependent operations; by default, the suspend() and
119  * resume() methods set the protected variable <var>_suspended</var>,
120  * which the subsystem can reference in its update() method, but
121  * subsystems may also override the suspend() and resume() methods to
122  * take different actions.</p>
123  */
124 class FGSubsystem
125 {
126 public:
127
128   /**
129    * Default constructor.
130    */
131   FGSubsystem ();
132
133   /**
134    * Virtual destructor to ensure that subclass destructors are called.
135    */
136   virtual ~FGSubsystem ();
137
138
139   /**
140    * Initialize the subsystem.
141    *
142    * <p>This method should set up the state of the subsystem, but
143    * should not bind any properties.  Note that any dependencies on
144    * the state of other subsystems should be placed here rather than
145    * in the constructor, so that FlightGear can control the
146    * initialization order.</p>
147    */
148   virtual void init ();
149
150
151   /**
152    * Reinitialize the subsystem.
153    *
154    * <p>This method should cause the subsystem to reinitialize itself,
155    * and (normally) to reload any configuration files.</p>
156    */
157   virtual void reinit ();
158
159
160   /**
161    * Acquire the subsystem's property bindings.
162    *
163    * <p>This method should bind all properties that the subsystem
164    * publishes.  It will be invoked after init, but before any
165    * invocations of update.</p>
166    */
167   virtual void bind ();
168
169
170   /**
171    * Release the subsystem's property bindings.
172    *
173    * <p>This method should release all properties that the subsystem
174    * publishes.  It will be invoked by FlightGear (not the destructor)
175    * just before the subsystem is removed.</p>
176    */
177   virtual void unbind ();
178
179
180   /**
181    * Update the subsystem.
182    *
183    * <p>FlightGear invokes this method every time the subsystem should
184    * update its state.</p>
185    *
186    * @param delta_time_sec The delta time, in seconds, since the last
187    * update.  On first update, delta time will be 0.
188    */
189   virtual void update (double delta_time_sec) = 0;
190
191
192   /**
193    * Suspend operation of this subsystem.
194    *
195    * <p>This method instructs the subsystem to suspend
196    * sim-time-dependent operations until asked to resume.  The update
197    * method will still be invoked so that the subsystem can take any
198    * non-time-dependent actions, such as updating the display.</p>
199    *
200    * <p>It is not an error for the suspend method to be invoked when
201    * the subsystem is already suspended; the invocation should simply
202    * be ignored.</p>
203    */
204   virtual void suspend ();
205
206
207   /**
208    * Suspend or resum operation of this subsystem.
209    *
210    * @param suspended true if the subsystem should be suspended, false
211    * otherwise.
212    */
213   virtual void suspend (bool suspended);
214
215
216   /**
217    * Resume operation of this subsystem.
218    *
219    * <p>This method instructs the subsystem to resume
220    * sim-time-depended operations.  It is not an error for the resume
221    * method to be invoked when the subsystem is not suspended; the
222    * invocation should simply be ignored.</p>
223    */
224   virtual void resume ();
225
226
227   /**
228    * Test whether this subsystem is suspended.
229    *
230    * @return true if the subsystem is suspended, false if it is not.
231    */
232   virtual bool is_suspended () const;
233
234
235 protected:
236
237   mutable SGPropertyNode_ptr _freeze_master_node;
238   bool _suspended;
239
240 };
241
242
243 \f
244 /**
245  * A group of FlightGear subsystems.
246  */
247 class FGSubsystemGroup : public FGSubsystem
248 {
249 public:
250
251     FGSubsystemGroup ();
252     virtual ~FGSubsystemGroup ();
253
254     virtual void init ();
255     virtual void bind ();
256     virtual void unbind ();
257     virtual void update (double delta_time_sec);
258
259     virtual void set_subsystem (const string &name,
260                                 FGSubsystem * subsystem,
261                                 double min_step_sec = 0);
262     virtual FGSubsystem * get_subsystem (const string &name);
263     virtual void remove_subsystem (const string &name);
264     virtual bool has_subsystem (const string &name) const;
265
266 private:
267
268     struct Member {
269
270         Member ();
271         Member (const Member &member);
272         virtual ~Member ();
273
274         virtual void update (double delta_time_sec);
275
276         string name;
277         FGSubsystem * subsystem;
278         double min_step_sec;
279         double elapsed_sec;
280     };
281
282     Member * get_member (const string &name, bool create = false);
283
284     vector<Member *> _members;
285 };
286
287
288 \f
289 /**
290  * Manage subsystems for the application.
291  */
292 class FGSubsystemMgr : public FGSubsystem
293 {
294 public:
295
296     enum GroupType {
297         INIT = 0,
298         GENERAL,
299         MAX_GROUPS
300     };
301
302     FGSubsystemMgr ();
303     virtual ~FGSubsystemMgr ();
304
305     virtual void init ();
306     virtual void bind ();
307     virtual void unbind ();
308     virtual void update (double delta_time_sec);
309
310     virtual void add (GroupType group, const string &name,
311                       FGSubsystem * subsystem,
312                       double min_time_sec = 0);
313
314     virtual FGSubsystemGroup * get_group (GroupType group);
315
316 private:
317
318     FGSubsystemGroup _groups[MAX_GROUPS];
319
320 };
321
322
323
324 #endif // __FGFS_HXX
325
326 // end of fgfs.hxx