1 // fg_props.hxx - Declarations and inline methods for property handling.
2 // Written by David Megginson, started 2000.
4 // This file is in the Public Domain, and comes with no warranty.
7 #define __FG_PROPS_HXX 1
9 #include <simgear/debug/logstream.hxx>
10 #include <simgear/props/props.hxx>
11 #include <simgear/props/props_io.hxx>
12 #include <simgear/structure/subsystem_mgr.hxx>
14 #include "globals.hxx"
17 ////////////////////////////////////////////////////////////////////////
18 // Property management.
19 ////////////////////////////////////////////////////////////////////////
21 class FGProperties : public SGSubsystem
25 virtual ~FGProperties ();
30 void update (double dt);
35 * Save a flight to disk.
37 * This function saves all of the archivable properties to a stream
38 * so that the current flight can be restored later.
40 * @param output The output stream to write the XML save file to.
41 * @param write_all If true, write all properties rather than
42 * just the ones flagged as archivable.
43 * @return true if the flight was saved successfully.
45 extern bool fgSaveFlight (ostream &output, bool write_all = false);
49 * Load a flight from disk.
51 * This function loads an XML save file from a stream to restore
54 * @param input The input stream to read the XML from.
55 * @return true if the flight was restored successfully.
57 extern bool fgLoadFlight (istream &input);
61 * Load properties from a file.
63 * @param file The relative or absolute filename.
64 * @param props The property node to load the properties into.
65 * @param in_fg_root If true, look for the file relative to
66 * $FG_ROOT; otherwise, look for the file relative to the
67 * current working directory.
68 * @return true if the properties loaded successfully, false
71 extern bool fgLoadProps (const char * path, SGPropertyNode * props,
72 bool in_fg_root = true);
76 ////////////////////////////////////////////////////////////////////////
77 // Convenience functions for getting property values.
78 ////////////////////////////////////////////////////////////////////////
81 * Get a property node.
83 * @param path The path of the node, relative to root.
84 * @param create true to create the node if it doesn't exist.
85 * @return The node, or 0 if none exists and none was created.
87 extern SGPropertyNode * fgGetNode (const char * path, bool create = false);
91 * Get a property node with separate index.
93 * This method separates the index from the path string, to make it
94 * easier to iterate through multiple components without using sprintf
95 * to add indices. For example, fgGetNode("foo[1]/bar", 3) will
96 * return the same result as fgGetNode("foo[1]/bar[3]").
98 * @param path The path of the node, relative to root.
99 * @param index The index for the last member of the path (overrides
100 * any given in the string).
101 * @param create true to create the node if it doesn't exist.
102 * @return The node, or 0 if none exists and none was created.
104 extern SGPropertyNode * fgGetNode (const char * path,
105 int index, bool create = false);
109 * Test whether a given node exists.
111 * @param path The path of the node, relative to root.
112 * @return true if the node exists, false otherwise.
114 extern bool fgHasNode (const char * path);
118 * Add a listener to a node.
120 * @param listener The listener to add to the node.
121 * @param path The path of the node, relative to root.
122 * @param index The index for the last member of the path (overrides
123 * any given in the string).
125 extern void fgAddChangeListener (SGPropertyChangeListener * listener,
130 * Add a listener to a node.
132 * @param listener The listener to add to the node.
133 * @param path The path of the node, relative to root.
134 * @param index The index for the last member of the path (overrides
135 * any given in the string).
137 extern void fgAddChangeListener (SGPropertyChangeListener * listener,
138 const char * path, int index);
142 * Get a bool value for a property.
144 * This method is convenient but inefficient. It should be used
145 * infrequently (i.e. for initializing, loading, saving, etc.),
146 * not in the main loop. If you need to get a value frequently,
147 * it is better to look up the node itself using fgGetNode and then
148 * use the node's getBoolValue() method, to avoid the lookup overhead.
150 * @param name The property name.
151 * @param defaultValue The default value to return if the property
153 * @return The property's value as a bool, or the default value provided.
155 extern bool fgGetBool (const char * name, bool defaultValue = false);
159 * Get an int value for a property.
161 * This method is convenient but inefficient. It should be used
162 * infrequently (i.e. for initializing, loading, saving, etc.),
163 * not in the main loop. If you need to get a value frequently,
164 * it is better to look up the node itself using fgGetNode and then
165 * use the node's getIntValue() method, to avoid the lookup overhead.
167 * @param name The property name.
168 * @param defaultValue The default value to return if the property
170 * @return The property's value as an int, or the default value provided.
172 extern int fgGetInt (const char * name, int defaultValue = 0);
176 * Get a long value for a property.
178 * This method is convenient but inefficient. It should be used
179 * infrequently (i.e. for initializing, loading, saving, etc.),
180 * not in the main loop. If you need to get a value frequently,
181 * it is better to look up the node itself using fgGetNode and then
182 * use the node's getLongValue() method, to avoid the lookup overhead.
184 * @param name The property name.
185 * @param defaultValue The default value to return if the property
187 * @return The property's value as a long, or the default value provided.
189 extern int fgGetLong (const char * name, long defaultValue = 0L);
193 * Get a float value for a property.
195 * This method is convenient but inefficient. It should be used
196 * infrequently (i.e. for initializing, loading, saving, etc.),
197 * not in the main loop. If you need to get a value frequently,
198 * it is better to look up the node itself using fgGetNode and then
199 * use the node's getFloatValue() method, to avoid the lookup overhead.
201 * @param name The property name.
202 * @param defaultValue The default value to return if the property
204 * @return The property's value as a float, or the default value provided.
206 extern float fgGetFloat (const char * name, float defaultValue = 0.0);
210 * Get a double value for a property.
212 * This method is convenient but inefficient. It should be used
213 * infrequently (i.e. for initializing, loading, saving, etc.),
214 * not in the main loop. If you need to get a value frequently,
215 * it is better to look up the node itself using fgGetNode and then
216 * use the node's getDoubleValue() method, to avoid the lookup overhead.
218 * @param name The property name.
219 * @param defaultValue The default value to return if the property
221 * @return The property's value as a double, or the default value provided.
223 extern double fgGetDouble (const char * name, double defaultValue = 0.0);
227 * Get a string value for a property.
229 * This method is convenient but inefficient. It should be used
230 * infrequently (i.e. for initializing, loading, saving, etc.),
231 * not in the main loop. If you need to get a value frequently,
232 * it is better to look up the node itself using fgGetNode and then
233 * use the node's getStringValue() method, to avoid the lookup overhead.
235 * @param name The property name.
236 * @param defaultValue The default value to return if the property
238 * @return The property's value as a string, or the default value provided.
240 extern const char * fgGetString (const char * name,
241 const char * defaultValue = "");
245 * Set a bool value for a property.
247 * Assign a bool value to a property. If the property does not
248 * yet exist, it will be created and its type will be set to
249 * BOOL; if it has a type of UNKNOWN, the type will also be set to
250 * BOOL; otherwise, the bool value will be converted to the property's
253 * @param name The property name.
254 * @param val The new value for the property.
255 * @return true if the assignment succeeded, false otherwise.
257 extern bool fgSetBool (const char * name, bool val);
261 * Set an int value for a property.
263 * Assign an int value to a property. If the property does not
264 * yet exist, it will be created and its type will be set to
265 * INT; if it has a type of UNKNOWN, the type will also be set to
266 * INT; otherwise, the bool value will be converted to the property's
269 * @param name The property name.
270 * @param val The new value for the property.
271 * @return true if the assignment succeeded, false otherwise.
273 extern bool fgSetInt (const char * name, int val);
277 * Set a long value for a property.
279 * Assign a long value to a property. If the property does not
280 * yet exist, it will be created and its type will be set to
281 * LONG; if it has a type of UNKNOWN, the type will also be set to
282 * LONG; otherwise, the bool value will be converted to the property's
285 * @param name The property name.
286 * @param val The new value for the property.
287 * @return true if the assignment succeeded, false otherwise.
289 extern bool fgSetLong (const char * name, long val);
293 * Set a float value for a property.
295 * Assign a float value to a property. If the property does not
296 * yet exist, it will be created and its type will be set to
297 * FLOAT; if it has a type of UNKNOWN, the type will also be set to
298 * FLOAT; otherwise, the bool value will be converted to the property's
301 * @param name The property name.
302 * @param val The new value for the property.
303 * @return true if the assignment succeeded, false otherwise.
305 extern bool fgSetFloat (const char * name, float val);
309 * Set a double value for a property.
311 * Assign a double value to a property. If the property does not
312 * yet exist, it will be created and its type will be set to
313 * DOUBLE; if it has a type of UNKNOWN, the type will also be set to
314 * DOUBLE; otherwise, the double value will be converted to the property's
317 * @param name The property name.
318 * @param val The new value for the property.
319 * @return true if the assignment succeeded, false otherwise.
321 extern bool fgSetDouble (const char * name, double val);
325 * Set a string value for a property.
327 * Assign a string value to a property. If the property does not
328 * yet exist, it will be created and its type will be set to
329 * STRING; if it has a type of UNKNOWN, the type will also be set to
330 * STRING; otherwise, the string value will be converted to the property's
333 * @param name The property name.
334 * @param val The new value for the property.
335 * @return true if the assignment succeeded, false otherwise.
337 extern bool fgSetString (const char * name, const char * val);
341 ////////////////////////////////////////////////////////////////////////
342 // Convenience functions for setting property attributes.
343 ////////////////////////////////////////////////////////////////////////
347 * Set the state of the archive attribute for a property.
349 * If the archive attribute is true, the property will be written
350 * when a flight is saved; if it is false, the property will be
353 * A warning message will be printed if the property does not exist.
355 * @param name The property name.
356 * @param state The state of the archive attribute (defaults to true).
358 extern void fgSetArchivable (const char * name, bool state = true);
362 * Set the state of the read attribute for a property.
364 * If the read attribute is true, the property value will be readable;
365 * if it is false, the property value will always be the default value
368 * A warning message will be printed if the property does not exist.
370 * @param name The property name.
371 * @param state The state of the read attribute (defaults to true).
373 extern void fgSetReadable (const char * name, bool state = true);
377 * Set the state of the write attribute for a property.
379 * If the write attribute is true, the property value may be modified
380 * (depending on how it is tied); if the write attribute is false, the
381 * property value may not be modified.
383 * A warning message will be printed if the property does not exist.
385 * @param name The property name.
386 * @param state The state of the write attribute (defaults to true).
388 extern void fgSetWritable (const char * name, bool state = true);
392 ////////////////////////////////////////////////////////////////////////
393 // Convenience functions for tying properties, with logging.
394 ////////////////////////////////////////////////////////////////////////
398 * Untie a property from an external data source.
400 * Classes should use this function to release control of any
401 * properties they are managing.
403 extern void fgUntie (const char * name);
407 * Tie a property to a pair of simple functions.
409 * Every time the property value is queried, the getter (if any) will
410 * be invoked; every time the property value is modified, the setter
411 * (if any) will be invoked. The getter can be 0 to make the property
412 * unreadable, and the setter can be 0 to make the property
415 * @param name The property name to tie (full path).
416 * @param getter The getter function, or 0 if the value is unreadable.
417 * @param setter The setter function, or 0 if the value is unmodifiable.
418 * @param useDefault true if the setter should be invoked with any existing
419 * property value should be; false if the old value should be
420 * discarded; defaults to true.
424 fgTie (const char * name, V (*getter)(), void (*setter)(V) = 0,
425 bool useDefault = true)
427 if (!globals->get_props()->tie(name, SGRawValueFunctions<V>(getter, setter),
429 SG_LOG(SG_GENERAL, SG_WARN,
430 "Failed to tie property " << name << " to functions");
435 * Tie a property to a pair of indexed functions.
437 * Every time the property value is queried, the getter (if any) will
438 * be invoked with the index provided; every time the property value
439 * is modified, the setter (if any) will be invoked with the index
440 * provided. The getter can be 0 to make the property unreadable, and
441 * the setter can be 0 to make the property unmodifiable.
443 * @param name The property name to tie (full path).
444 * @param index The integer argument to pass to the getter and
446 * @param getter The getter function, or 0 if the value is unreadable.
447 * @param setter The setter function, or 0 if the value is unmodifiable.
448 * @param useDefault true if the setter should be invoked with any existing
449 * property value should be; false if the old value should be
450 * discarded; defaults to true.
454 fgTie (const char * name, int index, V (*getter)(int),
455 void (*setter)(int, V) = 0, bool useDefault = true)
457 if (!globals->get_props()->tie(name,
458 SGRawValueFunctionsIndexed<V>(index,
462 SG_LOG(SG_GENERAL, SG_WARN,
463 "Failed to tie property " << name << " to indexed functions");
468 * Tie a property to a pair of object methods.
470 * Every time the property value is queried, the getter (if any) will
471 * be invoked; every time the property value is modified, the setter
472 * (if any) will be invoked. The getter can be 0 to make the property
473 * unreadable, and the setter can be 0 to make the property
476 * @param name The property name to tie (full path).
477 * @param obj The object whose methods should be invoked.
478 * @param getter The object's getter method, or 0 if the value is
480 * @param setter The object's setter method, or 0 if the value is
482 * @param useDefault true if the setter should be invoked with any existing
483 * property value should be; false if the old value should be
484 * discarded; defaults to true.
486 template <class T, class V>
488 fgTie (const char * name, T * obj, V (T::*getter)() const,
489 void (T::*setter)(V) = 0, bool useDefault = true)
491 if (!globals->get_props()->tie(name,
492 SGRawValueMethods<T,V>(*obj, getter, setter),
494 SG_LOG(SG_GENERAL, SG_WARN,
495 "Failed to tie property " << name << " to object methods");
500 * Tie a property to a pair of indexed object methods.
502 * Every time the property value is queried, the getter (if any) will
503 * be invoked with the index provided; every time the property value
504 * is modified, the setter (if any) will be invoked with the index
505 * provided. The getter can be 0 to make the property unreadable, and
506 * the setter can be 0 to make the property unmodifiable.
508 * @param name The property name to tie (full path).
509 * @param obj The object whose methods should be invoked.
510 * @param index The integer argument to pass to the getter and
512 * @param getter The getter method, or 0 if the value is unreadable.
513 * @param setter The setter method, or 0 if the value is unmodifiable.
514 * @param useDefault true if the setter should be invoked with any existing
515 * property value should be; false if the old value should be
516 * discarded; defaults to true.
518 template <class T, class V>
520 fgTie (const char * name, T * obj, int index,
521 V (T::*getter)(int) const, void (T::*setter)(int, V) = 0,
522 bool useDefault = true)
524 if (!globals->get_props()->tie(name,
525 SGRawValueMethodsIndexed<T,V>(*obj,
530 SG_LOG(SG_GENERAL, SG_WARN,
531 "Failed to tie property " << name << " to indexed object methods");
535 #endif // __FG_PROPS_HXX