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