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