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/props/tiedpropertylist.hxx>
13 #include <simgear/math/SGMath.hxx>
15 #include <Main/globals.hxx>
17 ////////////////////////////////////////////////////////////////////////
18 // Property management.
19 ////////////////////////////////////////////////////////////////////////
21 class FGProperties : public SGSubsystem
25 virtual ~FGProperties ();
30 void update (double dt);
33 simgear::TiedPropertyList _tiedProperties;
38 * Save a flight to disk.
40 * This function saves all of the archivable properties to a stream
41 * so that the current flight can be restored later.
43 * @param output The output stream to write the XML save file to.
44 * @param write_all If true, write all properties rather than
45 * just the ones flagged as archivable.
46 * @return true if the flight was saved successfully.
48 extern bool fgSaveFlight (std::ostream &output, bool write_all = false);
52 * Load a flight from disk.
54 * This function loads an XML save file from a stream to restore
57 * @param input The input stream to read the XML from.
58 * @return true if the flight was restored successfully.
60 extern bool fgLoadFlight (std::istream &input);
64 * Load properties from a file.
66 * @param file The relative or absolute filename.
67 * @param props The property node to load the properties into.
68 * @param in_fg_root If true, look for the file relative to
69 * $FG_ROOT; otherwise, look for the file relative to the
70 * current working directory.
71 * @return true if the properties loaded successfully, false
74 extern bool fgLoadProps (const char * path, SGPropertyNode * props,
75 bool in_fg_root = true, int default_mode = 0);
77 void setLoggingClasses (const char * c);
78 void setLoggingPriority (const char * p);
81 ////////////////////////////////////////////////////////////////////////
82 // Convenience functions for getting property values.
83 ////////////////////////////////////////////////////////////////////////
86 * Get a property node.
88 * @param path The path of the node, relative to root.
89 * @param create true to create the node if it doesn't exist.
90 * @return The node, or 0 if none exists and none was created.
92 extern SGPropertyNode * fgGetNode (const char * path, bool create = false);
95 * Get a property node.
97 * @param path The path of the node, relative to root.
98 * @param create true to create the node if it doesn't exist.
99 * @return The node, or 0 if none exists and none was created.
101 inline SGPropertyNode * fgGetNode (const std::string & path, bool create = false)
103 return fgGetNode(path.c_str(), create );
108 * Get a property node with separate index.
110 * This method separates the index from the path string, to make it
111 * easier to iterate through multiple components without using sprintf
112 * to add indices. For example, fgGetNode("foo[1]/bar", 3) will
113 * return the same result as fgGetNode("foo[1]/bar[3]").
115 * @param path The path of the node, relative to root.
116 * @param index The index for the last member of the path (overrides
117 * any given in the string).
118 * @param create true to create the node if it doesn't exist.
119 * @return The node, or 0 if none exists and none was created.
121 extern SGPropertyNode * fgGetNode (const char * path,
122 int index, bool create = false);
125 * Get a property node with separate index.
127 * This method separates the index from the path string, to make it
128 * easier to iterate through multiple components without using sprintf
129 * to add indices. For example, fgGetNode("foo[1]/bar", 3) will
130 * return the same result as fgGetNode("foo[1]/bar[3]").
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).
135 * @param create true to create the node if it doesn't exist.
136 * @return The node, or 0 if none exists and none was created.
138 inline SGPropertyNode * fgGetNode (const std::string & path,
139 int index, bool create = false)
141 return fgGetNode(path.c_str(), index, create );
146 * Test whether a given node exists.
148 * @param path The path of the node, relative to root.
149 * @return true if the node exists, false otherwise.
151 extern bool fgHasNode (const char * path);
154 * Test whether a given node exists.
156 * @param path The path of the node, relative to root.
157 * @return true if the node exists, false otherwise.
159 inline bool fgHasNode (const std::string & path)
161 return fgHasNode( path.c_str() );
166 * Add a listener to a node.
168 * @param listener The listener to add to the node.
169 * @param path The path of the node, relative to root.
170 * @param index The index for the last member of the path (overrides
171 * any given in the string).
173 extern void fgAddChangeListener (SGPropertyChangeListener * listener,
177 * Add a listener to a node.
179 * @param listener The listener to add to the node.
180 * @param path The path of the node, relative to root.
181 * @param index The index for the last member of the path (overrides
182 * any given in the string).
184 inline void fgAddChangeListener (SGPropertyChangeListener * listener,
185 const std::string & path)
187 fgAddChangeListener( listener, path.c_str() );
192 * Add a listener to a node.
194 * @param listener The listener to add to the node.
195 * @param path The path of the node, relative to root.
196 * @param index The index for the last member of the path (overrides
197 * any given in the string).
199 extern void fgAddChangeListener (SGPropertyChangeListener * listener,
200 const char * path, int index);
203 * Add a listener to a node.
205 * @param listener The listener to add to the node.
206 * @param path The path of the node, relative to root.
207 * @param index The index for the last member of the path (overrides
208 * any given in the string).
210 inline void fgAddChangeListener (SGPropertyChangeListener * listener,
211 const std::string & path, int index)
213 fgAddChangeListener( listener, path.c_str(), index );
218 * Get a bool value for a property.
220 * This method is convenient but inefficient. It should be used
221 * infrequently (i.e. for initializing, loading, saving, etc.),
222 * not in the main loop. If you need to get a value frequently,
223 * it is better to look up the node itself using fgGetNode and then
224 * use the node's getBoolValue() method, to avoid the lookup overhead.
226 * @param name The property name.
227 * @param defaultValue The default value to return if the property
229 * @return The property's value as a bool, or the default value provided.
231 extern bool fgGetBool (const char * name, bool defaultValue = false);
234 * Get a bool value for a property.
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 getBoolValue() method, to avoid the lookup overhead.
242 * @param name The property name.
243 * @param defaultValue The default value to return if the property
245 * @return The property's value as a bool, or the default value provided.
247 inline bool fgGetBool (const std::string & name, bool defaultValue = false)
249 return fgGetBool( name.c_str(), defaultValue );
254 * Get an int value for a property.
256 * This method is convenient but inefficient. It should be used
257 * infrequently (i.e. for initializing, loading, saving, etc.),
258 * not in the main loop. If you need to get a value frequently,
259 * it is better to look up the node itself using fgGetNode and then
260 * use the node's getIntValue() method, to avoid the lookup overhead.
262 * @param name The property name.
263 * @param defaultValue The default value to return if the property
265 * @return The property's value as an int, or the default value provided.
267 extern int fgGetInt (const char * name, int defaultValue = 0);
270 * Get an int value for a property.
272 * This method is convenient but inefficient. It should be used
273 * infrequently (i.e. for initializing, loading, saving, etc.),
274 * not in the main loop. If you need to get a value frequently,
275 * it is better to look up the node itself using fgGetNode and then
276 * use the node's getIntValue() method, to avoid the lookup overhead.
278 * @param name The property name.
279 * @param defaultValue The default value to return if the property
281 * @return The property's value as an int, or the default value provided.
283 inline int fgGetInt (const std::string & name, int defaultValue = 0)
285 return fgGetInt( name.c_str(), defaultValue );
290 * Get a long value for a property.
292 * This method is convenient but inefficient. It should be used
293 * infrequently (i.e. for initializing, loading, saving, etc.),
294 * not in the main loop. If you need to get a value frequently,
295 * it is better to look up the node itself using fgGetNode and then
296 * use the node's getLongValue() method, to avoid the lookup overhead.
298 * @param name The property name.
299 * @param defaultValue The default value to return if the property
301 * @return The property's value as a long, or the default value provided.
303 extern int fgGetLong (const char * name, long defaultValue = 0L);
306 * Get a long value for a property.
308 * This method is convenient but inefficient. It should be used
309 * infrequently (i.e. for initializing, loading, saving, etc.),
310 * not in the main loop. If you need to get a value frequently,
311 * it is better to look up the node itself using fgGetNode and then
312 * use the node's getLongValue() method, to avoid the lookup overhead.
314 * @param name The property name.
315 * @param defaultValue The default value to return if the property
317 * @return The property's value as a long, or the default value provided.
319 inline int fgGetLong (const std::string & name, long defaultValue = 0L)
321 return fgGetLong( name.c_str(), defaultValue );
326 * Get a float value for a property.
328 * This method is convenient but inefficient. It should be used
329 * infrequently (i.e. for initializing, loading, saving, etc.),
330 * not in the main loop. If you need to get a value frequently,
331 * it is better to look up the node itself using fgGetNode and then
332 * use the node's getFloatValue() method, to avoid the lookup overhead.
334 * @param name The property name.
335 * @param defaultValue The default value to return if the property
337 * @return The property's value as a float, or the default value provided.
339 extern float fgGetFloat (const char * name, float defaultValue = 0.0);
342 * Get a float value for a property.
344 * This method is convenient but inefficient. It should be used
345 * infrequently (i.e. for initializing, loading, saving, etc.),
346 * not in the main loop. If you need to get a value frequently,
347 * it is better to look up the node itself using fgGetNode and then
348 * use the node's getFloatValue() method, to avoid the lookup overhead.
350 * @param name The property name.
351 * @param defaultValue The default value to return if the property
353 * @return The property's value as a float, or the default value provided.
355 inline float fgGetFloat (const std::string & name, float defaultValue = 0.0)
357 return fgGetFloat( name.c_str(), defaultValue );
362 * Get a double value for a property.
364 * This method is convenient but inefficient. It should be used
365 * infrequently (i.e. for initializing, loading, saving, etc.),
366 * not in the main loop. If you need to get a value frequently,
367 * it is better to look up the node itself using fgGetNode and then
368 * use the node's getDoubleValue() method, to avoid the lookup overhead.
370 * @param name The property name.
371 * @param defaultValue The default value to return if the property
373 * @return The property's value as a double, or the default value provided.
375 extern double fgGetDouble (const char * name, double defaultValue = 0.0);
378 * Get a double value for a property.
380 * This method is convenient but inefficient. It should be used
381 * infrequently (i.e. for initializing, loading, saving, etc.),
382 * not in the main loop. If you need to get a value frequently,
383 * it is better to look up the node itself using fgGetNode and then
384 * use the node's getDoubleValue() method, to avoid the lookup overhead.
386 * @param name The property name.
387 * @param defaultValue The default value to return if the property
389 * @return The property's value as a double, or the default value provided.
391 inline double fgGetDouble (const std::string & name, double defaultValue = 0.0)
393 return fgGetDouble( name.c_str(), defaultValue );
398 * Get a string value for a property.
400 * This method is convenient but inefficient. It should be used
401 * infrequently (i.e. for initializing, loading, saving, etc.),
402 * not in the main loop. If you need to get a value frequently,
403 * it is better to look up the node itself using fgGetNode and then
404 * use the node's getStringValue() method, to avoid the lookup overhead.
406 * @param name The property name.
407 * @param defaultValue The default value to return if the property
409 * @return The property's value as a string, or the default value provided.
411 extern const char * fgGetString (const char * name,
412 const char * defaultValue = "");
415 * Get a string value for a property.
417 * This method is convenient but inefficient. It should be used
418 * infrequently (i.e. for initializing, loading, saving, etc.),
419 * not in the main loop. If you need to get a value frequently,
420 * it is better to look up the node itself using fgGetNode and then
421 * use the node's getStringValue() method, to avoid the lookup overhead.
423 * @param name The property name.
424 * @param defaultValue The default value to return if the property
426 * @return The property's value as a string, or the default value provided.
428 inline const char * fgGetString (const std::string & name,
429 const std::string & defaultValue = std::string(""))
431 return fgGetString( name.c_str(), defaultValue.c_str() );
436 * Set a bool value for a property.
438 * Assign a bool value to a property. If the property does not
439 * yet exist, it will be created and its type will be set to
440 * BOOL; if it has a type of UNKNOWN, the type will also be set to
441 * BOOL; otherwise, the bool value will be converted to the property's
444 * @param name The property name.
445 * @param val The new value for the property.
446 * @return true if the assignment succeeded, false otherwise.
448 extern bool fgSetBool (const char * name, bool val);
451 * Set a bool value for a property.
453 * Assign a bool value to a property. If the property does not
454 * yet exist, it will be created and its type will be set to
455 * BOOL; if it has a type of UNKNOWN, the type will also be set to
456 * BOOL; otherwise, the bool value will be converted to the property's
459 * @param name The property name.
460 * @param val The new value for the property.
461 * @return true if the assignment succeeded, false otherwise.
463 inline bool fgSetBool (const std::string & name, bool val)
465 return fgSetBool( name.c_str(), val );
470 * Set an int value for a property.
472 * Assign an int value to a property. If the property does not
473 * yet exist, it will be created and its type will be set to
474 * INT; if it has a type of UNKNOWN, the type will also be set to
475 * INT; otherwise, the bool value will be converted to the property's
478 * @param name The property name.
479 * @param val The new value for the property.
480 * @return true if the assignment succeeded, false otherwise.
482 extern bool fgSetInt (const char * name, int val);
485 * Set an int value for a property.
487 * Assign an int value to a property. If the property does not
488 * yet exist, it will be created and its type will be set to
489 * INT; if it has a type of UNKNOWN, the type will also be set to
490 * INT; otherwise, the bool value will be converted to the property's
493 * @param name The property name.
494 * @param val The new value for the property.
495 * @return true if the assignment succeeded, false otherwise.
497 inline bool fgSetInt (const std::string & name, int val)
499 return fgSetInt( name.c_str(), val );
503 * Set a long value for a property.
505 * Assign a long value to a property. If the property does not
506 * yet exist, it will be created and its type will be set to
507 * LONG; if it has a type of UNKNOWN, the type will also be set to
508 * LONG; otherwise, the bool value will be converted to the property's
511 * @param name The property name.
512 * @param val The new value for the property.
513 * @return true if the assignment succeeded, false otherwise.
515 extern bool fgSetLong (const char * name, long val);
518 * Set a long value for a property.
520 * Assign a long value to a property. If the property does not
521 * yet exist, it will be created and its type will be set to
522 * LONG; if it has a type of UNKNOWN, the type will also be set to
523 * LONG; otherwise, the bool value will be converted to the property's
526 * @param name The property name.
527 * @param val The new value for the property.
528 * @return true if the assignment succeeded, false otherwise.
530 inline bool fgSetLong (const std::string & name, long val)
532 return fgSetLong( name.c_str(), val );
537 * Set a float value for a property.
539 * Assign a float value to a property. If the property does not
540 * yet exist, it will be created and its type will be set to
541 * FLOAT; if it has a type of UNKNOWN, the type will also be set to
542 * FLOAT; otherwise, the bool value will be converted to the property's
545 * @param name The property name.
546 * @param val The new value for the property.
547 * @return true if the assignment succeeded, false otherwise.
549 extern bool fgSetFloat (const char * name, float val);
552 * Set a float value for a property.
554 * Assign a float value to a property. If the property does not
555 * yet exist, it will be created and its type will be set to
556 * FLOAT; if it has a type of UNKNOWN, the type will also be set to
557 * FLOAT; otherwise, the bool value will be converted to the property's
560 * @param name The property name.
561 * @param val The new value for the property.
562 * @return true if the assignment succeeded, false otherwise.
564 inline bool fgSetFloat (const std::string & name, float val)
566 return fgSetFloat( name.c_str(), val );
571 * Set a double value for a property.
573 * Assign a double value to a property. If the property does not
574 * yet exist, it will be created and its type will be set to
575 * DOUBLE; if it has a type of UNKNOWN, the type will also be set to
576 * DOUBLE; otherwise, the double value will be converted to the property's
579 * @param name The property name.
580 * @param val The new value for the property.
581 * @return true if the assignment succeeded, false otherwise.
583 extern bool fgSetDouble (const char * name, double val);
586 * Set a double value for a property.
588 * Assign a double value to a property. If the property does not
589 * yet exist, it will be created and its type will be set to
590 * DOUBLE; if it has a type of UNKNOWN, the type will also be set to
591 * DOUBLE; otherwise, the double value will be converted to the property's
594 * @param name The property name.
595 * @param val The new value for the property.
596 * @return true if the assignment succeeded, false otherwise.
598 inline bool fgSetDouble (const std::string & name, double val)
600 return fgSetDouble( name.c_str(), val );
605 * Set a string value for a property.
607 * Assign a string value to a property. If the property does not
608 * yet exist, it will be created and its type will be set to
609 * STRING; if it has a type of UNKNOWN, the type will also be set to
610 * STRING; otherwise, the string value will be converted to the property's
613 * @param name The property name.
614 * @param val The new value for the property.
615 * @return true if the assignment succeeded, false otherwise.
617 extern bool fgSetString (const char * name, const char * val);
620 * Set a string value for a property.
622 * Assign a string value to a property. If the property does not
623 * yet exist, it will be created and its type will be set to
624 * STRING; if it has a type of UNKNOWN, the type will also be set to
625 * STRING; otherwise, the string value will be converted to the property's
628 * @param name The property name.
629 * @param val The new value for the property.
630 * @return true if the assignment succeeded, false otherwise.
632 inline bool fgSetString (const std::string & name, const std::string & val)
634 return fgSetString( name.c_str(), val.c_str() );
639 ////////////////////////////////////////////////////////////////////////
640 // Convenience functions for setting property attributes.
641 ////////////////////////////////////////////////////////////////////////
645 * Set the state of the archive attribute for a property.
647 * If the archive attribute is true, the property will be written
648 * when a flight is saved; if it is false, the property will be
651 * A warning message will be printed if the property does not exist.
653 * @param name The property name.
654 * @param state The state of the archive attribute (defaults to true).
656 extern void fgSetArchivable (const char * name, bool state = true);
660 * Set the state of the read attribute for a property.
662 * If the read attribute is true, the property value will be readable;
663 * if it is false, the property value will always be the default value
666 * A warning message will be printed if the property does not exist.
668 * @param name The property name.
669 * @param state The state of the read attribute (defaults to true).
671 extern void fgSetReadable (const char * name, bool state = true);
675 * Set the state of the write attribute for a property.
677 * If the write attribute is true, the property value may be modified
678 * (depending on how it is tied); if the write attribute is false, the
679 * property value may not be modified.
681 * A warning message will be printed if the property does not exist.
683 * @param name The property name.
684 * @param state The state of the write attribute (defaults to true).
686 extern void fgSetWritable (const char * name, bool state = true);
690 ////////////////////////////////////////////////////////////////////////
691 // Convenience functions for tying properties, with logging.
692 ////////////////////////////////////////////////////////////////////////
696 * Untie a property from an external data source.
698 * Classes should use this function to release control of any
699 * properties they are managing.
701 extern void fgUntie (const char * name);
705 * Tie a property to a pair of simple functions.
707 * Every time the property value is queried, the getter (if any) will
708 * be invoked; every time the property value is modified, the setter
709 * (if any) will be invoked. The getter can be 0 to make the property
710 * unreadable, and the setter can be 0 to make the property
713 * @param name The property name to tie (full path).
714 * @param getter The getter function, or 0 if the value is unreadable.
715 * @param setter The setter function, or 0 if the value is unmodifiable.
716 * @param useDefault true if the setter should be invoked with any existing
717 * property value should be; false if the old value should be
718 * discarded; defaults to true.
722 fgTie (const char * name, V (*getter)(), void (*setter)(V) = 0,
723 bool useDefault = true)
725 if (!globals->get_props()->tie(name, SGRawValueFunctions<V>(getter, setter),
727 SG_LOG(SG_GENERAL, SG_WARN,
728 "Failed to tie property " << name << " to functions");
733 * Tie a property to a pair of indexed functions.
735 * Every time the property value is queried, the getter (if any) will
736 * be invoked with the index provided; every time the property value
737 * is modified, the setter (if any) will be invoked with the index
738 * provided. The getter can be 0 to make the property unreadable, and
739 * the setter can be 0 to make the property unmodifiable.
741 * @param name The property name to tie (full path).
742 * @param index The integer argument to pass to the getter and
744 * @param getter The getter function, or 0 if the value is unreadable.
745 * @param setter The setter function, or 0 if the value is unmodifiable.
746 * @param useDefault true if the setter should be invoked with any existing
747 * property value should be; false if the old value should be
748 * discarded; defaults to true.
752 fgTie (const char * name, int index, V (*getter)(int),
753 void (*setter)(int, V) = 0, bool useDefault = true)
755 if (!globals->get_props()->tie(name,
756 SGRawValueFunctionsIndexed<V>(index,
760 SG_LOG(SG_GENERAL, SG_WARN,
761 "Failed to tie property " << name << " to indexed functions");
766 * Tie a property to a pair of object methods.
768 * Every time the property value is queried, the getter (if any) will
769 * be invoked; every time the property value is modified, the setter
770 * (if any) will be invoked. The getter can be 0 to make the property
771 * unreadable, and the setter can be 0 to make the property
774 * @param name The property name to tie (full path).
775 * @param obj The object whose methods should be invoked.
776 * @param getter The object's getter method, or 0 if the value is
778 * @param setter The object's setter method, or 0 if the value is
780 * @param useDefault true if the setter should be invoked with any existing
781 * property value should be; false if the old value should be
782 * discarded; defaults to true.
784 template <class T, class V>
786 fgTie (const char * name, T * obj, V (T::*getter)() const,
787 void (T::*setter)(V) = 0, bool useDefault = true)
789 if (!globals->get_props()->tie(name,
790 SGRawValueMethods<T,V>(*obj, getter, setter),
792 SG_LOG(SG_GENERAL, SG_WARN,
793 "Failed to tie property " << name << " to object methods");
798 * Tie a property to a pair of indexed object methods.
800 * Every time the property value is queried, the getter (if any) will
801 * be invoked with the index provided; every time the property value
802 * is modified, the setter (if any) will be invoked with the index
803 * provided. The getter can be 0 to make the property unreadable, and
804 * the setter can be 0 to make the property unmodifiable.
806 * @param name The property name to tie (full path).
807 * @param obj The object whose methods should be invoked.
808 * @param index The integer argument to pass to the getter and
810 * @param getter The getter method, or 0 if the value is unreadable.
811 * @param setter The setter method, or 0 if the value is unmodifiable.
812 * @param useDefault true if the setter should be invoked with any existing
813 * property value should be; false if the old value should be
814 * discarded; defaults to true.
816 template <class T, class V>
818 fgTie (const char * name, T * obj, int index,
819 V (T::*getter)(int) const, void (T::*setter)(int, V) = 0,
820 bool useDefault = true)
822 if (!globals->get_props()->tie(name,
823 SGRawValueMethodsIndexed<T,V>(*obj,
828 SG_LOG(SG_GENERAL, SG_WARN,
829 "Failed to tie property " << name << " to indexed object methods");
833 class FGMakeUpperCase : public SGPropertyChangeListener {
835 void valueChanged(SGPropertyNode *node) {
836 if (node->getType() != simgear::props::STRING)
839 char *s = const_cast<char *>(node->getStringValue());
846 #endif // __FG_PROPS_HXX