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>
13 #include <Main/globals.hxx>
15 ////////////////////////////////////////////////////////////////////////
16 // Property management.
17 ////////////////////////////////////////////////////////////////////////
19 class FGProperties : public SGSubsystem
23 virtual ~FGProperties ();
28 void update (double dt);
33 * Save a flight to disk.
35 * This function saves all of the archivable properties to a stream
36 * so that the current flight can be restored later.
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.
43 extern bool fgSaveFlight (std::ostream &output, bool write_all = false);
47 * Load a flight from disk.
49 * This function loads an XML save file from a stream to restore
52 * @param input The input stream to read the XML from.
53 * @return true if the flight was restored successfully.
55 extern bool fgLoadFlight (std::istream &input);
59 * Load properties from a file.
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
69 extern bool fgLoadProps (const char * path, SGPropertyNode * props,
70 bool in_fg_root = true, int default_mode = 0);
74 ////////////////////////////////////////////////////////////////////////
75 // Convenience functions for getting property values.
76 ////////////////////////////////////////////////////////////////////////
79 * Get a property node.
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.
85 extern SGPropertyNode * fgGetNode (const char * path, bool create = false);
89 * Get a property node with separate index.
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]").
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.
102 extern SGPropertyNode * fgGetNode (const char * path,
103 int index, bool create = false);
107 * Test whether a given node exists.
109 * @param path The path of the node, relative to root.
110 * @return true if the node exists, false otherwise.
112 extern bool fgHasNode (const char * path);
116 * Add a listener to a node.
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).
123 extern void fgAddChangeListener (SGPropertyChangeListener * listener,
128 * Add a listener to a node.
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).
135 extern void fgAddChangeListener (SGPropertyChangeListener * listener,
136 const char * path, int index);
140 * Get a bool value for a property.
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.
148 * @param name The property name.
149 * @param defaultValue The default value to return if the property
151 * @return The property's value as a bool, or the default value provided.
153 extern bool fgGetBool (const char * name, bool defaultValue = false);
157 * Get an int value for a property.
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.
165 * @param name The property name.
166 * @param defaultValue The default value to return if the property
168 * @return The property's value as an int, or the default value provided.
170 extern int fgGetInt (const char * name, int defaultValue = 0);
174 * Get a long value for a property.
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.
182 * @param name The property name.
183 * @param defaultValue The default value to return if the property
185 * @return The property's value as a long, or the default value provided.
187 extern int fgGetLong (const char * name, long defaultValue = 0L);
191 * Get a float value for a property.
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.
199 * @param name The property name.
200 * @param defaultValue The default value to return if the property
202 * @return The property's value as a float, or the default value provided.
204 extern float fgGetFloat (const char * name, float defaultValue = 0.0);
208 * Get a double value for a property.
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.
216 * @param name The property name.
217 * @param defaultValue The default value to return if the property
219 * @return The property's value as a double, or the default value provided.
221 extern double fgGetDouble (const char * name, double defaultValue = 0.0);
225 * Get a string value for a property.
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.
233 * @param name The property name.
234 * @param defaultValue The default value to return if the property
236 * @return The property's value as a string, or the default value provided.
238 extern const char * fgGetString (const char * name,
239 const char * defaultValue = "");
243 * Set a bool value for a property.
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
251 * @param name The property name.
252 * @param val The new value for the property.
253 * @return true if the assignment succeeded, false otherwise.
255 extern bool fgSetBool (const char * name, bool val);
259 * Set an int value for a property.
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
267 * @param name The property name.
268 * @param val The new value for the property.
269 * @return true if the assignment succeeded, false otherwise.
271 extern bool fgSetInt (const char * name, int val);
275 * Set a long value for a property.
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
283 * @param name The property name.
284 * @param val The new value for the property.
285 * @return true if the assignment succeeded, false otherwise.
287 extern bool fgSetLong (const char * name, long val);
291 * Set a float value for a property.
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
299 * @param name The property name.
300 * @param val The new value for the property.
301 * @return true if the assignment succeeded, false otherwise.
303 extern bool fgSetFloat (const char * name, float val);
307 * Set a double value for a property.
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
315 * @param name The property name.
316 * @param val The new value for the property.
317 * @return true if the assignment succeeded, false otherwise.
319 extern bool fgSetDouble (const char * name, double val);
323 * Set a string value for a property.
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
331 * @param name The property name.
332 * @param val The new value for the property.
333 * @return true if the assignment succeeded, false otherwise.
335 extern bool fgSetString (const char * name, const char * val);
339 ////////////////////////////////////////////////////////////////////////
340 // Convenience functions for setting property attributes.
341 ////////////////////////////////////////////////////////////////////////
345 * Set the state of the archive attribute for a property.
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
351 * A warning message will be printed if the property does not exist.
353 * @param name The property name.
354 * @param state The state of the archive attribute (defaults to true).
356 extern void fgSetArchivable (const char * name, bool state = true);
360 * Set the state of the read attribute for a property.
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
366 * A warning message will be printed if the property does not exist.
368 * @param name The property name.
369 * @param state The state of the read attribute (defaults to true).
371 extern void fgSetReadable (const char * name, bool state = true);
375 * Set the state of the write attribute for a property.
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.
381 * A warning message will be printed if the property does not exist.
383 * @param name The property name.
384 * @param state The state of the write attribute (defaults to true).
386 extern void fgSetWritable (const char * name, bool state = true);
390 ////////////////////////////////////////////////////////////////////////
391 // Convenience functions for tying properties, with logging.
392 ////////////////////////////////////////////////////////////////////////
396 * Untie a property from an external data source.
398 * Classes should use this function to release control of any
399 * properties they are managing.
401 extern void fgUntie (const char * name);
405 * Tie a property to a pair of simple functions.
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
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.
422 fgTie (const char * name, V (*getter)(), void (*setter)(V) = 0,
423 bool useDefault = true)
425 if (!globals->get_props()->tie(name, SGRawValueFunctions<V>(getter, setter),
427 SG_LOG(SG_GENERAL, SG_WARN,
428 "Failed to tie property " << name << " to functions");
433 * Tie a property to a pair of indexed functions.
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.
441 * @param name The property name to tie (full path).
442 * @param index The integer argument to pass to the getter and
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.
452 fgTie (const char * name, int index, V (*getter)(int),
453 void (*setter)(int, V) = 0, bool useDefault = true)
455 if (!globals->get_props()->tie(name,
456 SGRawValueFunctionsIndexed<V>(index,
460 SG_LOG(SG_GENERAL, SG_WARN,
461 "Failed to tie property " << name << " to indexed functions");
466 * Tie a property to a pair of object methods.
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
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
478 * @param setter The object's setter method, or 0 if the value is
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.
484 template <class T, class V>
486 fgTie (const char * name, T * obj, V (T::*getter)() const,
487 void (T::*setter)(V) = 0, bool useDefault = true)
489 if (!globals->get_props()->tie(name,
490 SGRawValueMethods<T,V>(*obj, getter, setter),
492 SG_LOG(SG_GENERAL, SG_WARN,
493 "Failed to tie property " << name << " to object methods");
498 * Tie a property to a pair of indexed object methods.
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.
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
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.
516 template <class T, class V>
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)
522 if (!globals->get_props()->tie(name,
523 SGRawValueMethodsIndexed<T,V>(*obj,
528 SG_LOG(SG_GENERAL, SG_WARN,
529 "Failed to tie property " << name << " to indexed object methods");
533 class FGMakeUpperCase : public SGPropertyChangeListener {
535 void valueChanged(SGPropertyNode *node) {
536 if (node->getType() != SGPropertyNode::STRING)
539 char *s = const_cast<char *>(node->getStringValue());
546 #endif // __FG_PROPS_HXX