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
11 #include <simgear/structure/subsystem_mgr.hxx>
12 #include <simgear/math/SGMath.hxx>
14 #include <Main/globals.hxx>
16 ////////////////////////////////////////////////////////////////////////
17 // Property management.
18 ////////////////////////////////////////////////////////////////////////
20 class FGProperties : public SGSubsystem
24 virtual ~FGProperties ();
29 void update (double dt);
34 * Save a flight to disk.
36 * This function saves all of the archivable properties to a stream
37 * so that the current flight can be restored later.
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.
44 extern bool fgSaveFlight (std::ostream &output, bool write_all = false);
48 * Load a flight from disk.
50 * This function loads an XML save file from a stream to restore
53 * @param input The input stream to read the XML from.
54 * @return true if the flight was restored successfully.
56 extern bool fgLoadFlight (std::istream &input);
60 * Load properties from a file.
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
70 extern bool fgLoadProps (const char * path, SGPropertyNode * props,
71 bool in_fg_root = true, int default_mode = 0);
75 ////////////////////////////////////////////////////////////////////////
76 // Convenience functions for getting property values.
77 ////////////////////////////////////////////////////////////////////////
80 * Get a property node.
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.
86 extern SGPropertyNode * fgGetNode (const char * path, bool create = false);
90 * Get a property node with separate index.
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]").
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.
103 extern SGPropertyNode * fgGetNode (const char * path,
104 int index, bool create = false);
108 * Test whether a given node exists.
110 * @param path The path of the node, relative to root.
111 * @return true if the node exists, false otherwise.
113 extern bool fgHasNode (const char * path);
117 * Add a listener to a node.
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).
124 extern void fgAddChangeListener (SGPropertyChangeListener * listener,
129 * Add a listener to a node.
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).
136 extern void fgAddChangeListener (SGPropertyChangeListener * listener,
137 const char * path, int index);
141 * Get a bool value for a property.
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.
149 * @param name The property name.
150 * @param defaultValue The default value to return if the property
152 * @return The property's value as a bool, or the default value provided.
154 extern bool fgGetBool (const char * name, bool defaultValue = false);
158 * Get an int value for a property.
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.
166 * @param name The property name.
167 * @param defaultValue The default value to return if the property
169 * @return The property's value as an int, or the default value provided.
171 extern int fgGetInt (const char * name, int defaultValue = 0);
175 * Get a long value for a property.
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.
183 * @param name The property name.
184 * @param defaultValue The default value to return if the property
186 * @return The property's value as a long, or the default value provided.
188 extern int fgGetLong (const char * name, long defaultValue = 0L);
192 * Get a float value for a property.
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.
200 * @param name The property name.
201 * @param defaultValue The default value to return if the property
203 * @return The property's value as a float, or the default value provided.
205 extern float fgGetFloat (const char * name, float defaultValue = 0.0);
209 * Get a double value for a property.
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.
217 * @param name The property name.
218 * @param defaultValue The default value to return if the property
220 * @return The property's value as a double, or the default value provided.
222 extern double fgGetDouble (const char * name, double defaultValue = 0.0);
226 * Get a string value for a property.
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.
234 * @param name The property name.
235 * @param defaultValue The default value to return if the property
237 * @return The property's value as a string, or the default value provided.
239 extern const char * fgGetString (const char * name,
240 const char * defaultValue = "");
244 * Set a bool value for a property.
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
252 * @param name The property name.
253 * @param val The new value for the property.
254 * @return true if the assignment succeeded, false otherwise.
256 extern bool fgSetBool (const char * name, bool val);
260 * Set an int value for a property.
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
268 * @param name The property name.
269 * @param val The new value for the property.
270 * @return true if the assignment succeeded, false otherwise.
272 extern bool fgSetInt (const char * name, int val);
276 * Set a long value for a property.
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
284 * @param name The property name.
285 * @param val The new value for the property.
286 * @return true if the assignment succeeded, false otherwise.
288 extern bool fgSetLong (const char * name, long val);
292 * Set a float value for a property.
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
300 * @param name The property name.
301 * @param val The new value for the property.
302 * @return true if the assignment succeeded, false otherwise.
304 extern bool fgSetFloat (const char * name, float val);
308 * Set a double value for a property.
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
316 * @param name The property name.
317 * @param val The new value for the property.
318 * @return true if the assignment succeeded, false otherwise.
320 extern bool fgSetDouble (const char * name, double val);
324 * Set a string value for a property.
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
332 * @param name The property name.
333 * @param val The new value for the property.
334 * @return true if the assignment succeeded, false otherwise.
336 extern bool fgSetString (const char * name, const char * val);
340 ////////////////////////////////////////////////////////////////////////
341 // Convenience functions for setting property attributes.
342 ////////////////////////////////////////////////////////////////////////
346 * Set the state of the archive attribute for a property.
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
352 * A warning message will be printed if the property does not exist.
354 * @param name The property name.
355 * @param state The state of the archive attribute (defaults to true).
357 extern void fgSetArchivable (const char * name, bool state = true);
361 * Set the state of the read attribute for a property.
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
367 * A warning message will be printed if the property does not exist.
369 * @param name The property name.
370 * @param state The state of the read attribute (defaults to true).
372 extern void fgSetReadable (const char * name, bool state = true);
376 * Set the state of the write attribute for a property.
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.
382 * A warning message will be printed if the property does not exist.
384 * @param name The property name.
385 * @param state The state of the write attribute (defaults to true).
387 extern void fgSetWritable (const char * name, bool state = true);
391 ////////////////////////////////////////////////////////////////////////
392 // Convenience functions for tying properties, with logging.
393 ////////////////////////////////////////////////////////////////////////
397 * Untie a property from an external data source.
399 * Classes should use this function to release control of any
400 * properties they are managing.
402 extern void fgUntie (const char * name);
406 * Tie a property to a pair of simple functions.
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
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.
423 fgTie (const char * name, V (*getter)(), void (*setter)(V) = 0,
424 bool useDefault = true)
426 if (!globals->get_props()->tie(name, SGRawValueFunctions<V>(getter, setter),
428 SG_LOG(SG_GENERAL, SG_WARN,
429 "Failed to tie property " << name << " to functions");
434 * Tie a property to a pair of indexed functions.
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.
442 * @param name The property name to tie (full path).
443 * @param index The integer argument to pass to the getter and
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.
453 fgTie (const char * name, int index, V (*getter)(int),
454 void (*setter)(int, V) = 0, bool useDefault = true)
456 if (!globals->get_props()->tie(name,
457 SGRawValueFunctionsIndexed<V>(index,
461 SG_LOG(SG_GENERAL, SG_WARN,
462 "Failed to tie property " << name << " to indexed functions");
467 * Tie a property to a pair of object methods.
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
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
479 * @param setter The object's setter method, or 0 if the value is
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.
485 template <class T, class V>
487 fgTie (const char * name, T * obj, V (T::*getter)() const,
488 void (T::*setter)(V) = 0, bool useDefault = true)
490 if (!globals->get_props()->tie(name,
491 SGRawValueMethods<T,V>(*obj, getter, setter),
493 SG_LOG(SG_GENERAL, SG_WARN,
494 "Failed to tie property " << name << " to object methods");
499 * Tie a property to a pair of indexed object methods.
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.
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
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.
517 template <class T, class V>
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)
523 if (!globals->get_props()->tie(name,
524 SGRawValueMethodsIndexed<T,V>(*obj,
529 SG_LOG(SG_GENERAL, SG_WARN,
530 "Failed to tie property " << name << " to indexed object methods");
534 class FGMakeUpperCase : public SGPropertyChangeListener {
536 void valueChanged(SGPropertyNode *node) {
537 if (node->getType() != simgear::props::STRING)
540 char *s = const_cast<char *>(node->getStringValue());
547 #endif // __FG_PROPS_HXX