]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_props.hxx
Merge branches 'jmt/navradio', 'jmt/cleanup' and 'torsten/gps-fix'
[flightgear.git] / src / Main / fg_props.hxx
1 // fg_props.hxx - Declarations and inline methods for property handling.
2 // Written by David Megginson, started 2000.
3 //
4 // This file is in the Public Domain, and comes with no warranty.
5
6 #ifndef __FG_PROPS_HXX
7 #define __FG_PROPS_HXX 1
8
9 #include <iosfwd>
10
11 #include <simgear/structure/subsystem_mgr.hxx>
12 #include <simgear/math/SGMath.hxx>
13
14 #include <Main/globals.hxx>
15
16 ////////////////////////////////////////////////////////////////////////
17 // Property management.
18 ////////////////////////////////////////////////////////////////////////
19
20 class FGProperties : public SGSubsystem
21 {
22 public:
23     FGProperties ();
24     virtual ~FGProperties ();
25
26     void init ();
27     void bind ();
28     void unbind ();
29     void update (double dt);
30 };
31
32
33 /**
34  * Save a flight to disk.
35  *
36  * This function saves all of the archivable properties to a stream
37  * so that the current flight can be restored later.
38  *
39  * @param output The output stream to write the XML save file to.
40  * @param write_all If true, write all properties rather than
41  *        just the ones flagged as archivable.
42  * @return true if the flight was saved successfully.
43  */
44 extern bool fgSaveFlight (std::ostream &output, bool write_all = false);
45
46
47 /**
48  * Load a flight from disk.
49  *
50  * This function loads an XML save file from a stream to restore
51  * a flight.
52  *
53  * @param input The input stream to read the XML from.
54  * @return true if the flight was restored successfully.
55  */
56 extern bool fgLoadFlight (std::istream &input);
57
58
59 /**
60  * Load properties from a file.
61  *
62  * @param file The relative or absolute filename.
63  * @param props The property node to load the properties into.
64  * @param in_fg_root If true, look for the file relative to
65  *        $FG_ROOT; otherwise, look for the file relative to the
66  *        current working directory.
67  * @return true if the properties loaded successfully, false
68  *         otherwise.
69  */
70 extern bool fgLoadProps (const char * path, SGPropertyNode * props,
71                          bool in_fg_root = true, int default_mode = 0);
72
73
74 \f
75 ////////////////////////////////////////////////////////////////////////
76 // Convenience functions for getting property values.
77 ////////////////////////////////////////////////////////////////////////
78
79 /**
80  * Get a property node.
81  *
82  * @param path The path of the node, relative to root.
83  * @param create true to create the node if it doesn't exist.
84  * @return The node, or 0 if none exists and none was created.
85  */
86 extern SGPropertyNode * fgGetNode (const char * path, bool create = false);
87
88
89 /**
90  * Get a property node with separate index.
91  *
92  * This method separates the index from the path string, to make it
93  * easier to iterate through multiple components without using sprintf
94  * to add indices.  For example, fgGetNode("foo[1]/bar", 3) will
95  * return the same result as fgGetNode("foo[1]/bar[3]").
96  *
97  * @param path The path of the node, relative to root.
98  * @param index The index for the last member of the path (overrides
99  * any given in the string).
100  * @param create true to create the node if it doesn't exist.
101  * @return The node, or 0 if none exists and none was created.
102  */
103 extern SGPropertyNode * fgGetNode (const char * path,
104                                    int index, bool create = false);
105
106
107 /**
108  * Test whether a given node exists.
109  *
110  * @param path The path of the node, relative to root.
111  * @return true if the node exists, false otherwise.
112  */
113 extern bool fgHasNode (const char * path);
114
115
116 /**
117  * Add a listener to a node.
118  *
119  * @param listener The listener to add to the node.
120  * @param path The path of the node, relative to root.
121  * @param index The index for the last member of the path (overrides
122  * any given in the string).
123  */
124 extern void fgAddChangeListener (SGPropertyChangeListener * listener,
125                                  const char * path);
126
127
128 /**
129  * Add a listener to a node.
130  *
131  * @param listener The listener to add to the node.
132  * @param path The path of the node, relative to root.
133  * @param index The index for the last member of the path (overrides
134  * any given in the string).
135  */
136 extern void fgAddChangeListener (SGPropertyChangeListener * listener,
137                                  const char * path, int index);
138
139
140 /**
141  * Get a bool value for a property.
142  *
143  * This method is convenient but inefficient.  It should be used
144  * infrequently (i.e. for initializing, loading, saving, etc.),
145  * not in the main loop.  If you need to get a value frequently,
146  * it is better to look up the node itself using fgGetNode and then
147  * use the node's getBoolValue() method, to avoid the lookup overhead.
148  *
149  * @param name The property name.
150  * @param defaultValue The default value to return if the property
151  *        does not exist.
152  * @return The property's value as a bool, or the default value provided.
153  */
154 extern bool fgGetBool (const char * name, bool defaultValue = false);
155
156
157 /**
158  * Get an int value for a property.
159  *
160  * This method is convenient but inefficient.  It should be used
161  * infrequently (i.e. for initializing, loading, saving, etc.),
162  * not in the main loop.  If you need to get a value frequently,
163  * it is better to look up the node itself using fgGetNode and then
164  * use the node's getIntValue() method, to avoid the lookup overhead.
165  *
166  * @param name The property name.
167  * @param defaultValue The default value to return if the property
168  *        does not exist.
169  * @return The property's value as an int, or the default value provided.
170  */
171 extern int fgGetInt (const char * name, int defaultValue = 0);
172
173
174 /**
175  * Get a long value for a property.
176  *
177  * This method is convenient but inefficient.  It should be used
178  * infrequently (i.e. for initializing, loading, saving, etc.),
179  * not in the main loop.  If you need to get a value frequently,
180  * it is better to look up the node itself using fgGetNode and then
181  * use the node's getLongValue() method, to avoid the lookup overhead.
182  *
183  * @param name The property name.
184  * @param defaultValue The default value to return if the property
185  *        does not exist.
186  * @return The property's value as a long, or the default value provided.
187  */
188 extern int fgGetLong (const char * name, long defaultValue = 0L);
189
190
191 /**
192  * Get a float value for a property.
193  *
194  * This method is convenient but inefficient.  It should be used
195  * infrequently (i.e. for initializing, loading, saving, etc.),
196  * not in the main loop.  If you need to get a value frequently,
197  * it is better to look up the node itself using fgGetNode and then
198  * use the node's getFloatValue() method, to avoid the lookup overhead.
199  *
200  * @param name The property name.
201  * @param defaultValue The default value to return if the property
202  *        does not exist.
203  * @return The property's value as a float, or the default value provided.
204  */
205 extern float fgGetFloat (const char * name, float defaultValue = 0.0);
206
207
208 /**
209  * Get a double value for a property.
210  *
211  * This method is convenient but inefficient.  It should be used
212  * infrequently (i.e. for initializing, loading, saving, etc.),
213  * not in the main loop.  If you need to get a value frequently,
214  * it is better to look up the node itself using fgGetNode and then
215  * use the node's getDoubleValue() method, to avoid the lookup overhead.
216  *
217  * @param name The property name.
218  * @param defaultValue The default value to return if the property
219  *        does not exist.
220  * @return The property's value as a double, or the default value provided.
221  */
222 extern double fgGetDouble (const char * name, double defaultValue = 0.0);
223
224
225 /**
226  * Get a string value for a property.
227  *
228  * This method is convenient but inefficient.  It should be used
229  * infrequently (i.e. for initializing, loading, saving, etc.),
230  * not in the main loop.  If you need to get a value frequently,
231  * it is better to look up the node itself using fgGetNode and then
232  * use the node's getStringValue() method, to avoid the lookup overhead.
233  *
234  * @param name The property name.
235  * @param defaultValue The default value to return if the property
236  *        does not exist.
237  * @return The property's value as a string, or the default value provided.
238  */
239 extern const char * fgGetString (const char * name,
240                                  const char * defaultValue = "");
241
242
243 /**
244  * Set a bool value for a property.
245  *
246  * Assign a bool value to a property.  If the property does not
247  * yet exist, it will be created and its type will be set to
248  * BOOL; if it has a type of UNKNOWN, the type will also be set to
249  * BOOL; otherwise, the bool value will be converted to the property's
250  * type.
251  *
252  * @param name The property name.
253  * @param val The new value for the property.
254  * @return true if the assignment succeeded, false otherwise.
255  */
256 extern bool fgSetBool (const char * name, bool val);
257
258
259 /**
260  * Set an int value for a property.
261  *
262  * Assign an int value to a property.  If the property does not
263  * yet exist, it will be created and its type will be set to
264  * INT; if it has a type of UNKNOWN, the type will also be set to
265  * INT; otherwise, the bool value will be converted to the property's
266  * type.
267  *
268  * @param name The property name.
269  * @param val The new value for the property.
270  * @return true if the assignment succeeded, false otherwise.
271  */
272 extern bool fgSetInt (const char * name, int val);
273
274
275 /**
276  * Set a long value for a property.
277  *
278  * Assign a long value to a property.  If the property does not
279  * yet exist, it will be created and its type will be set to
280  * LONG; if it has a type of UNKNOWN, the type will also be set to
281  * LONG; otherwise, the bool value will be converted to the property's
282  * type.
283  *
284  * @param name The property name.
285  * @param val The new value for the property.
286  * @return true if the assignment succeeded, false otherwise.
287  */
288 extern bool fgSetLong (const char * name, long val);
289
290
291 /**
292  * Set a float value for a property.
293  *
294  * Assign a float value to a property.  If the property does not
295  * yet exist, it will be created and its type will be set to
296  * FLOAT; if it has a type of UNKNOWN, the type will also be set to
297  * FLOAT; otherwise, the bool value will be converted to the property's
298  * type.
299  *
300  * @param name The property name.
301  * @param val The new value for the property.
302  * @return true if the assignment succeeded, false otherwise.
303  */
304 extern bool fgSetFloat (const char * name, float val);
305
306
307 /**
308  * Set a double value for a property.
309  *
310  * Assign a double value to a property.  If the property does not
311  * yet exist, it will be created and its type will be set to
312  * DOUBLE; if it has a type of UNKNOWN, the type will also be set to
313  * DOUBLE; otherwise, the double value will be converted to the property's
314  * type.
315  *
316  * @param name The property name.
317  * @param val The new value for the property.
318  * @return true if the assignment succeeded, false otherwise.
319  */
320 extern bool fgSetDouble (const char * name, double val);
321
322
323 /**
324  * Set a string value for a property.
325  *
326  * Assign a string value to a property.  If the property does not
327  * yet exist, it will be created and its type will be set to
328  * STRING; if it has a type of UNKNOWN, the type will also be set to
329  * STRING; otherwise, the string value will be converted to the property's
330  * type.
331  *
332  * @param name The property name.
333  * @param val The new value for the property.
334  * @return true if the assignment succeeded, false otherwise.
335  */
336 extern bool fgSetString (const char * name, const char * val);
337
338
339 \f
340 ////////////////////////////////////////////////////////////////////////
341 // Convenience functions for setting property attributes.
342 ////////////////////////////////////////////////////////////////////////
343
344
345 /**
346  * Set the state of the archive attribute for a property.
347  *
348  * If the archive attribute is true, the property will be written
349  * when a flight is saved; if it is false, the property will be
350  * skipped.
351  *
352  * A warning message will be printed if the property does not exist.
353  *
354  * @param name The property name.
355  * @param state The state of the archive attribute (defaults to true).
356  */
357 extern void fgSetArchivable (const char * name, bool state = true);
358
359
360 /**
361  * Set the state of the read attribute for a property.
362  *
363  * If the read attribute is true, the property value will be readable;
364  * if it is false, the property value will always be the default value
365  * for its type.
366  *
367  * A warning message will be printed if the property does not exist.
368  *
369  * @param name The property name.
370  * @param state The state of the read attribute (defaults to true).
371  */
372 extern void fgSetReadable (const char * name, bool state = true);
373
374
375 /**
376  * Set the state of the write attribute for a property.
377  *
378  * If the write attribute is true, the property value may be modified
379  * (depending on how it is tied); if the write attribute is false, the
380  * property value may not be modified.
381  *
382  * A warning message will be printed if the property does not exist.
383  *
384  * @param name The property name.
385  * @param state The state of the write attribute (defaults to true).
386  */
387 extern void fgSetWritable (const char * name, bool state = true);
388
389
390 \f
391 ////////////////////////////////////////////////////////////////////////
392 // Convenience functions for tying properties, with logging.
393 ////////////////////////////////////////////////////////////////////////
394
395
396 /**
397  * Untie a property from an external data source.
398  *
399  * Classes should use this function to release control of any
400  * properties they are managing.
401  */
402 extern void fgUntie (const char * name);
403
404
405 /**
406  * Tie a property to a pair of simple functions.
407  *
408  * Every time the property value is queried, the getter (if any) will
409  * be invoked; every time the property value is modified, the setter
410  * (if any) will be invoked.  The getter can be 0 to make the property
411  * unreadable, and the setter can be 0 to make the property
412  * unmodifiable.
413  *
414  * @param name The property name to tie (full path).
415  * @param getter The getter function, or 0 if the value is unreadable.
416  * @param setter The setter function, or 0 if the value is unmodifiable.
417  * @param useDefault true if the setter should be invoked with any existing 
418  *        property value should be; false if the old value should be
419  *        discarded; defaults to true.
420  */
421 template <class V>
422 inline void
423 fgTie (const char * name, V (*getter)(), void (*setter)(V) = 0,
424        bool useDefault = true)
425 {
426   if (!globals->get_props()->tie(name, SGRawValueFunctions<V>(getter, setter),
427                                  useDefault))
428     SG_LOG(SG_GENERAL, SG_WARN,
429            "Failed to tie property " << name << " to functions");
430 }
431
432
433 /**
434  * Tie a property to a pair of indexed functions.
435  *
436  * Every time the property value is queried, the getter (if any) will
437  * be invoked with the index provided; every time the property value
438  * is modified, the setter (if any) will be invoked with the index
439  * provided.  The getter can be 0 to make the property unreadable, and
440  * the setter can be 0 to make the property unmodifiable.
441  *
442  * @param name The property name to tie (full path).
443  * @param index The integer argument to pass to the getter and
444  *        setter functions.
445  * @param getter The getter function, or 0 if the value is unreadable.
446  * @param setter The setter function, or 0 if the value is unmodifiable.
447  * @param useDefault true if the setter should be invoked with any existing 
448  *        property value should be; false if the old value should be
449  *        discarded; defaults to true.
450  */
451 template <class V>
452 inline void
453 fgTie (const char * name, int index, V (*getter)(int),
454        void (*setter)(int, V) = 0, bool useDefault = true)
455 {
456   if (!globals->get_props()->tie(name,
457                                  SGRawValueFunctionsIndexed<V>(index,
458                                                                getter,
459                                                                setter),
460                                  useDefault))
461     SG_LOG(SG_GENERAL, SG_WARN,
462            "Failed to tie property " << name << " to indexed functions");
463 }
464
465
466 /**
467  * Tie a property to a pair of object methods.
468  *
469  * Every time the property value is queried, the getter (if any) will
470  * be invoked; every time the property value is modified, the setter
471  * (if any) will be invoked.  The getter can be 0 to make the property
472  * unreadable, and the setter can be 0 to make the property
473  * unmodifiable.
474  *
475  * @param name The property name to tie (full path).
476  * @param obj The object whose methods should be invoked.
477  * @param getter The object's getter method, or 0 if the value is
478  *        unreadable.
479  * @param setter The object's setter method, or 0 if the value is
480  *        unmodifiable.
481  * @param useDefault true if the setter should be invoked with any existing 
482  *        property value should be; false if the old value should be
483  *        discarded; defaults to true.
484  */
485 template <class T, class V>
486 inline void
487 fgTie (const char * name, T * obj, V (T::*getter)() const,
488        void (T::*setter)(V) = 0, bool useDefault = true)
489 {
490   if (!globals->get_props()->tie(name,
491                                  SGRawValueMethods<T,V>(*obj, getter, setter),
492                                  useDefault))
493     SG_LOG(SG_GENERAL, SG_WARN,
494            "Failed to tie property " << name << " to object methods");
495 }
496
497
498 /**
499  * Tie a property to a pair of indexed object methods.
500  *
501  * Every time the property value is queried, the getter (if any) will
502  * be invoked with the index provided; every time the property value
503  * is modified, the setter (if any) will be invoked with the index
504  * provided.  The getter can be 0 to make the property unreadable, and
505  * the setter can be 0 to make the property unmodifiable.
506  *
507  * @param name The property name to tie (full path).
508  * @param obj The object whose methods should be invoked.
509  * @param index The integer argument to pass to the getter and
510  *        setter methods.
511  * @param getter The getter method, or 0 if the value is unreadable.
512  * @param setter The setter method, or 0 if the value is unmodifiable.
513  * @param useDefault true if the setter should be invoked with any existing 
514  *        property value should be; false if the old value should be
515  *        discarded; defaults to true.
516  */
517 template <class T, class V>
518 inline void 
519 fgTie (const char * name, T * obj, int index,
520        V (T::*getter)(int) const, void (T::*setter)(int, V) = 0,
521        bool useDefault = true)
522 {
523   if (!globals->get_props()->tie(name,
524                                  SGRawValueMethodsIndexed<T,V>(*obj,
525                                                                index,
526                                                                getter,
527                                                                setter),
528                                  useDefault))
529     SG_LOG(SG_GENERAL, SG_WARN,
530            "Failed to tie property " << name << " to indexed object methods");
531 }
532
533
534 class FGMakeUpperCase : public SGPropertyChangeListener {
535 public:
536     void valueChanged(SGPropertyNode *node) {
537         if (node->getType() != simgear::props::STRING)
538             return;
539
540         char *s = const_cast<char *>(node->getStringValue());
541         for (; *s; s++)
542             *s = toupper(*s);
543     }
544 };
545
546
547 #endif // __FG_PROPS_HXX
548