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);
89 * Get a property node.
91 * @param path The path of the node, relative to root.
92 * @param create true to create the node if it doesn't exist.
93 * @return The node, or 0 if none exists and none was created.
95 inline SGPropertyNode * fgGetNode (const std::string & path, bool create = false)
97 return fgGetNode(path.c_str(), create );
102 * Get a property node with separate index.
104 * This method separates the index from the path string, to make it
105 * easier to iterate through multiple components without using sprintf
106 * to add indices. For example, fgGetNode("foo[1]/bar", 3) will
107 * return the same result as fgGetNode("foo[1]/bar[3]").
109 * @param path The path of the node, relative to root.
110 * @param index The index for the last member of the path (overrides
111 * any given in the string).
112 * @param create true to create the node if it doesn't exist.
113 * @return The node, or 0 if none exists and none was created.
115 extern SGPropertyNode * fgGetNode (const char * path,
116 int index, bool create = false);
119 * Get a property node with separate index.
121 * This method separates the index from the path string, to make it
122 * easier to iterate through multiple components without using sprintf
123 * to add indices. For example, fgGetNode("foo[1]/bar", 3) will
124 * return the same result as fgGetNode("foo[1]/bar[3]").
126 * @param path The path of the node, relative to root.
127 * @param index The index for the last member of the path (overrides
128 * any given in the string).
129 * @param create true to create the node if it doesn't exist.
130 * @return The node, or 0 if none exists and none was created.
132 inline SGPropertyNode * fgGetNode (const std::string & path,
133 int index, bool create = false)
135 return fgGetNode(path.c_str(), index, create );
140 * Test whether a given node exists.
142 * @param path The path of the node, relative to root.
143 * @return true if the node exists, false otherwise.
145 extern bool fgHasNode (const char * path);
148 * Test whether a given node exists.
150 * @param path The path of the node, relative to root.
151 * @return true if the node exists, false otherwise.
153 inline bool fgHasNode (const std::string & path)
155 return fgHasNode( path.c_str() );
160 * Add a listener to a node.
162 * @param listener The listener to add to the node.
163 * @param path The path of the node, relative to root.
164 * @param index The index for the last member of the path (overrides
165 * any given in the string).
167 extern void fgAddChangeListener (SGPropertyChangeListener * listener,
171 * Add a listener to a node.
173 * @param listener The listener to add to the node.
174 * @param path The path of the node, relative to root.
175 * @param index The index for the last member of the path (overrides
176 * any given in the string).
178 inline void fgAddChangeListener (SGPropertyChangeListener * listener,
179 const std::string & path)
181 fgAddChangeListener( listener, path.c_str() );
186 * Add a listener to a node.
188 * @param listener The listener to add to the node.
189 * @param path The path of the node, relative to root.
190 * @param index The index for the last member of the path (overrides
191 * any given in the string).
193 extern void fgAddChangeListener (SGPropertyChangeListener * listener,
194 const char * path, int index);
197 * Add a listener to a node.
199 * @param listener The listener to add to the node.
200 * @param path The path of the node, relative to root.
201 * @param index The index for the last member of the path (overrides
202 * any given in the string).
204 inline void fgAddChangeListener (SGPropertyChangeListener * listener,
205 const std::string & path, int index)
207 fgAddChangeListener( listener, path.c_str(), index );
212 * Get a bool value for a property.
214 * This method is convenient but inefficient. It should be used
215 * infrequently (i.e. for initializing, loading, saving, etc.),
216 * not in the main loop. If you need to get a value frequently,
217 * it is better to look up the node itself using fgGetNode and then
218 * use the node's getBoolValue() method, to avoid the lookup overhead.
220 * @param name The property name.
221 * @param defaultValue The default value to return if the property
223 * @return The property's value as a bool, or the default value provided.
225 extern bool fgGetBool (const char * name, bool defaultValue = false);
228 * Get a bool value for a property.
230 * This method is convenient but inefficient. It should be used
231 * infrequently (i.e. for initializing, loading, saving, etc.),
232 * not in the main loop. If you need to get a value frequently,
233 * it is better to look up the node itself using fgGetNode and then
234 * use the node's getBoolValue() method, to avoid the lookup overhead.
236 * @param name The property name.
237 * @param defaultValue The default value to return if the property
239 * @return The property's value as a bool, or the default value provided.
241 inline bool fgGetBool (const std::string & name, bool defaultValue = false)
243 return fgGetBool( name.c_str(), defaultValue );
248 * Get an int value for a property.
250 * This method is convenient but inefficient. It should be used
251 * infrequently (i.e. for initializing, loading, saving, etc.),
252 * not in the main loop. If you need to get a value frequently,
253 * it is better to look up the node itself using fgGetNode and then
254 * use the node's getIntValue() method, to avoid the lookup overhead.
256 * @param name The property name.
257 * @param defaultValue The default value to return if the property
259 * @return The property's value as an int, or the default value provided.
261 extern int fgGetInt (const char * name, int defaultValue = 0);
264 * Get an int value for a property.
266 * This method is convenient but inefficient. It should be used
267 * infrequently (i.e. for initializing, loading, saving, etc.),
268 * not in the main loop. If you need to get a value frequently,
269 * it is better to look up the node itself using fgGetNode and then
270 * use the node's getIntValue() method, to avoid the lookup overhead.
272 * @param name The property name.
273 * @param defaultValue The default value to return if the property
275 * @return The property's value as an int, or the default value provided.
277 inline int fgGetInt (const std::string & name, int defaultValue = 0)
279 return fgGetInt( name.c_str(), defaultValue );
284 * Get a long value for a property.
286 * This method is convenient but inefficient. It should be used
287 * infrequently (i.e. for initializing, loading, saving, etc.),
288 * not in the main loop. If you need to get a value frequently,
289 * it is better to look up the node itself using fgGetNode and then
290 * use the node's getLongValue() method, to avoid the lookup overhead.
292 * @param name The property name.
293 * @param defaultValue The default value to return if the property
295 * @return The property's value as a long, or the default value provided.
297 extern int fgGetLong (const char * name, long defaultValue = 0L);
300 * Get a long value for a property.
302 * This method is convenient but inefficient. It should be used
303 * infrequently (i.e. for initializing, loading, saving, etc.),
304 * not in the main loop. If you need to get a value frequently,
305 * it is better to look up the node itself using fgGetNode and then
306 * use the node's getLongValue() method, to avoid the lookup overhead.
308 * @param name The property name.
309 * @param defaultValue The default value to return if the property
311 * @return The property's value as a long, or the default value provided.
313 inline int fgGetLong (const std::string & name, long defaultValue = 0L)
315 return fgGetLong( name.c_str(), defaultValue );
320 * Get a float value for a property.
322 * This method is convenient but inefficient. It should be used
323 * infrequently (i.e. for initializing, loading, saving, etc.),
324 * not in the main loop. If you need to get a value frequently,
325 * it is better to look up the node itself using fgGetNode and then
326 * use the node's getFloatValue() method, to avoid the lookup overhead.
328 * @param name The property name.
329 * @param defaultValue The default value to return if the property
331 * @return The property's value as a float, or the default value provided.
333 extern float fgGetFloat (const char * name, float defaultValue = 0.0);
336 * Get a float value for a property.
338 * This method is convenient but inefficient. It should be used
339 * infrequently (i.e. for initializing, loading, saving, etc.),
340 * not in the main loop. If you need to get a value frequently,
341 * it is better to look up the node itself using fgGetNode and then
342 * use the node's getFloatValue() method, to avoid the lookup overhead.
344 * @param name The property name.
345 * @param defaultValue The default value to return if the property
347 * @return The property's value as a float, or the default value provided.
349 inline float fgGetFloat (const std::string & name, float defaultValue = 0.0)
351 return fgGetFloat( name.c_str(), defaultValue );
356 * Get a double value for a property.
358 * This method is convenient but inefficient. It should be used
359 * infrequently (i.e. for initializing, loading, saving, etc.),
360 * not in the main loop. If you need to get a value frequently,
361 * it is better to look up the node itself using fgGetNode and then
362 * use the node's getDoubleValue() method, to avoid the lookup overhead.
364 * @param name The property name.
365 * @param defaultValue The default value to return if the property
367 * @return The property's value as a double, or the default value provided.
369 extern double fgGetDouble (const char * name, double defaultValue = 0.0);
372 * Get a double value for a property.
374 * This method is convenient but inefficient. It should be used
375 * infrequently (i.e. for initializing, loading, saving, etc.),
376 * not in the main loop. If you need to get a value frequently,
377 * it is better to look up the node itself using fgGetNode and then
378 * use the node's getDoubleValue() method, to avoid the lookup overhead.
380 * @param name The property name.
381 * @param defaultValue The default value to return if the property
383 * @return The property's value as a double, or the default value provided.
385 inline double fgGetDouble (const std::string & name, double defaultValue = 0.0)
387 return fgGetDouble( name.c_str(), defaultValue );
392 * Get a string value for a property.
394 * This method is convenient but inefficient. It should be used
395 * infrequently (i.e. for initializing, loading, saving, etc.),
396 * not in the main loop. If you need to get a value frequently,
397 * it is better to look up the node itself using fgGetNode and then
398 * use the node's getStringValue() method, to avoid the lookup overhead.
400 * @param name The property name.
401 * @param defaultValue The default value to return if the property
403 * @return The property's value as a string, or the default value provided.
405 extern const char * fgGetString (const char * name,
406 const char * defaultValue = "");
409 * Get a string value for a property.
411 * This method is convenient but inefficient. It should be used
412 * infrequently (i.e. for initializing, loading, saving, etc.),
413 * not in the main loop. If you need to get a value frequently,
414 * it is better to look up the node itself using fgGetNode and then
415 * use the node's getStringValue() method, to avoid the lookup overhead.
417 * @param name The property name.
418 * @param defaultValue The default value to return if the property
420 * @return The property's value as a string, or the default value provided.
422 inline const char * fgGetString (const std::string & name,
423 const std::string & defaultValue = string(""))
425 return fgGetString( name.c_str(), defaultValue.c_str() );
430 * Set a bool value for a property.
432 * Assign a bool value to a property. If the property does not
433 * yet exist, it will be created and its type will be set to
434 * BOOL; if it has a type of UNKNOWN, the type will also be set to
435 * BOOL; otherwise, the bool value will be converted to the property's
438 * @param name The property name.
439 * @param val The new value for the property.
440 * @return true if the assignment succeeded, false otherwise.
442 extern bool fgSetBool (const char * name, bool val);
445 * Set a bool value for a property.
447 * Assign a bool value to a property. If the property does not
448 * yet exist, it will be created and its type will be set to
449 * BOOL; if it has a type of UNKNOWN, the type will also be set to
450 * BOOL; otherwise, the bool value will be converted to the property's
453 * @param name The property name.
454 * @param val The new value for the property.
455 * @return true if the assignment succeeded, false otherwise.
457 inline bool fgSetBool (const std::string & name, bool val)
459 return fgSetBool( name.c_str(), val );
464 * Set an int value for a property.
466 * Assign an int value to a property. If the property does not
467 * yet exist, it will be created and its type will be set to
468 * INT; if it has a type of UNKNOWN, the type will also be set to
469 * INT; otherwise, the bool value will be converted to the property's
472 * @param name The property name.
473 * @param val The new value for the property.
474 * @return true if the assignment succeeded, false otherwise.
476 extern bool fgSetInt (const char * name, int val);
479 * Set an int value for a property.
481 * Assign an int value to a property. If the property does not
482 * yet exist, it will be created and its type will be set to
483 * INT; if it has a type of UNKNOWN, the type will also be set to
484 * INT; otherwise, the bool value will be converted to the property's
487 * @param name The property name.
488 * @param val The new value for the property.
489 * @return true if the assignment succeeded, false otherwise.
491 inline bool fgSetInt (const std::string & name, int val)
493 return fgSetInt( name.c_str(), val );
497 * Set a long value for a property.
499 * Assign a long value to a property. If the property does not
500 * yet exist, it will be created and its type will be set to
501 * LONG; if it has a type of UNKNOWN, the type will also be set to
502 * LONG; otherwise, the bool value will be converted to the property's
505 * @param name The property name.
506 * @param val The new value for the property.
507 * @return true if the assignment succeeded, false otherwise.
509 extern bool fgSetLong (const char * name, long val);
512 * Set a long value for a property.
514 * Assign a long value to a property. If the property does not
515 * yet exist, it will be created and its type will be set to
516 * LONG; if it has a type of UNKNOWN, the type will also be set to
517 * LONG; otherwise, the bool value will be converted to the property's
520 * @param name The property name.
521 * @param val The new value for the property.
522 * @return true if the assignment succeeded, false otherwise.
524 inline bool fgSetLong (const std::string & name, long val)
526 return fgSetLong( name.c_str(), val );
531 * Set a float value for a property.
533 * Assign a float value to a property. If the property does not
534 * yet exist, it will be created and its type will be set to
535 * FLOAT; if it has a type of UNKNOWN, the type will also be set to
536 * FLOAT; otherwise, the bool value will be converted to the property's
539 * @param name The property name.
540 * @param val The new value for the property.
541 * @return true if the assignment succeeded, false otherwise.
543 extern bool fgSetFloat (const char * name, float val);
546 * Set a float value for a property.
548 * Assign a float value to a property. If the property does not
549 * yet exist, it will be created and its type will be set to
550 * FLOAT; if it has a type of UNKNOWN, the type will also be set to
551 * FLOAT; otherwise, the bool value will be converted to the property's
554 * @param name The property name.
555 * @param val The new value for the property.
556 * @return true if the assignment succeeded, false otherwise.
558 inline bool fgSetFloat (const std::string & name, float val)
560 return fgSetFloat( name.c_str(), val );
565 * Set a double value for a property.
567 * Assign a double value to a property. If the property does not
568 * yet exist, it will be created and its type will be set to
569 * DOUBLE; if it has a type of UNKNOWN, the type will also be set to
570 * DOUBLE; otherwise, the double value will be converted to the property's
573 * @param name The property name.
574 * @param val The new value for the property.
575 * @return true if the assignment succeeded, false otherwise.
577 extern bool fgSetDouble (const char * name, double val);
580 * Set a double value for a property.
582 * Assign a double value to a property. If the property does not
583 * yet exist, it will be created and its type will be set to
584 * DOUBLE; if it has a type of UNKNOWN, the type will also be set to
585 * DOUBLE; otherwise, the double value will be converted to the property's
588 * @param name The property name.
589 * @param val The new value for the property.
590 * @return true if the assignment succeeded, false otherwise.
592 inline bool fgSetDouble (const std::string & name, double val)
594 return fgSetDouble( name.c_str(), val );
599 * Set a string value for a property.
601 * Assign a string value to a property. If the property does not
602 * yet exist, it will be created and its type will be set to
603 * STRING; if it has a type of UNKNOWN, the type will also be set to
604 * STRING; otherwise, the string value will be converted to the property's
607 * @param name The property name.
608 * @param val The new value for the property.
609 * @return true if the assignment succeeded, false otherwise.
611 extern bool fgSetString (const char * name, const char * val);
614 * Set a string value for a property.
616 * Assign a string value to a property. If the property does not
617 * yet exist, it will be created and its type will be set to
618 * STRING; if it has a type of UNKNOWN, the type will also be set to
619 * STRING; otherwise, the string value will be converted to the property's
622 * @param name The property name.
623 * @param val The new value for the property.
624 * @return true if the assignment succeeded, false otherwise.
626 inline bool fgSetString (const std::string & name, const std::string & val)
628 return fgSetString( name.c_str(), val.c_str() );
633 ////////////////////////////////////////////////////////////////////////
634 // Convenience functions for setting property attributes.
635 ////////////////////////////////////////////////////////////////////////
639 * Set the state of the archive attribute for a property.
641 * If the archive attribute is true, the property will be written
642 * when a flight is saved; if it is false, the property will be
645 * A warning message will be printed if the property does not exist.
647 * @param name The property name.
648 * @param state The state of the archive attribute (defaults to true).
650 extern void fgSetArchivable (const char * name, bool state = true);
654 * Set the state of the read attribute for a property.
656 * If the read attribute is true, the property value will be readable;
657 * if it is false, the property value will always be the default value
660 * A warning message will be printed if the property does not exist.
662 * @param name The property name.
663 * @param state The state of the read attribute (defaults to true).
665 extern void fgSetReadable (const char * name, bool state = true);
669 * Set the state of the write attribute for a property.
671 * If the write attribute is true, the property value may be modified
672 * (depending on how it is tied); if the write attribute is false, the
673 * property value may not be modified.
675 * A warning message will be printed if the property does not exist.
677 * @param name The property name.
678 * @param state The state of the write attribute (defaults to true).
680 extern void fgSetWritable (const char * name, bool state = true);
684 ////////////////////////////////////////////////////////////////////////
685 // Convenience functions for tying properties, with logging.
686 ////////////////////////////////////////////////////////////////////////
690 * Untie a property from an external data source.
692 * Classes should use this function to release control of any
693 * properties they are managing.
695 extern void fgUntie (const char * name);
699 * Tie a property to a pair of simple functions.
701 * Every time the property value is queried, the getter (if any) will
702 * be invoked; every time the property value is modified, the setter
703 * (if any) will be invoked. The getter can be 0 to make the property
704 * unreadable, and the setter can be 0 to make the property
707 * @param name The property name to tie (full path).
708 * @param getter The getter function, or 0 if the value is unreadable.
709 * @param setter The setter function, or 0 if the value is unmodifiable.
710 * @param useDefault true if the setter should be invoked with any existing
711 * property value should be; false if the old value should be
712 * discarded; defaults to true.
716 fgTie (const char * name, V (*getter)(), void (*setter)(V) = 0,
717 bool useDefault = true)
719 if (!globals->get_props()->tie(name, SGRawValueFunctions<V>(getter, setter),
721 SG_LOG(SG_GENERAL, SG_WARN,
722 "Failed to tie property " << name << " to functions");
727 * Tie a property to a pair of indexed functions.
729 * Every time the property value is queried, the getter (if any) will
730 * be invoked with the index provided; every time the property value
731 * is modified, the setter (if any) will be invoked with the index
732 * provided. The getter can be 0 to make the property unreadable, and
733 * the setter can be 0 to make the property unmodifiable.
735 * @param name The property name to tie (full path).
736 * @param index The integer argument to pass to the getter and
738 * @param getter The getter function, or 0 if the value is unreadable.
739 * @param setter The setter function, or 0 if the value is unmodifiable.
740 * @param useDefault true if the setter should be invoked with any existing
741 * property value should be; false if the old value should be
742 * discarded; defaults to true.
746 fgTie (const char * name, int index, V (*getter)(int),
747 void (*setter)(int, V) = 0, bool useDefault = true)
749 if (!globals->get_props()->tie(name,
750 SGRawValueFunctionsIndexed<V>(index,
754 SG_LOG(SG_GENERAL, SG_WARN,
755 "Failed to tie property " << name << " to indexed functions");
760 * Tie a property to a pair of object methods.
762 * Every time the property value is queried, the getter (if any) will
763 * be invoked; every time the property value is modified, the setter
764 * (if any) will be invoked. The getter can be 0 to make the property
765 * unreadable, and the setter can be 0 to make the property
768 * @param name The property name to tie (full path).
769 * @param obj The object whose methods should be invoked.
770 * @param getter The object's getter method, or 0 if the value is
772 * @param setter The object's setter method, or 0 if the value is
774 * @param useDefault true if the setter should be invoked with any existing
775 * property value should be; false if the old value should be
776 * discarded; defaults to true.
778 template <class T, class V>
780 fgTie (const char * name, T * obj, V (T::*getter)() const,
781 void (T::*setter)(V) = 0, bool useDefault = true)
783 if (!globals->get_props()->tie(name,
784 SGRawValueMethods<T,V>(*obj, getter, setter),
786 SG_LOG(SG_GENERAL, SG_WARN,
787 "Failed to tie property " << name << " to object methods");
792 * Tie a property to a pair of indexed object methods.
794 * Every time the property value is queried, the getter (if any) will
795 * be invoked with the index provided; every time the property value
796 * is modified, the setter (if any) will be invoked with the index
797 * provided. The getter can be 0 to make the property unreadable, and
798 * the setter can be 0 to make the property unmodifiable.
800 * @param name The property name to tie (full path).
801 * @param obj The object whose methods should be invoked.
802 * @param index The integer argument to pass to the getter and
804 * @param getter The getter method, or 0 if the value is unreadable.
805 * @param setter The setter method, or 0 if the value is unmodifiable.
806 * @param useDefault true if the setter should be invoked with any existing
807 * property value should be; false if the old value should be
808 * discarded; defaults to true.
810 template <class T, class V>
812 fgTie (const char * name, T * obj, int index,
813 V (T::*getter)(int) const, void (T::*setter)(int, V) = 0,
814 bool useDefault = true)
816 if (!globals->get_props()->tie(name,
817 SGRawValueMethodsIndexed<T,V>(*obj,
822 SG_LOG(SG_GENERAL, SG_WARN,
823 "Failed to tie property " << name << " to indexed object methods");
827 class FGMakeUpperCase : public SGPropertyChangeListener {
829 void valueChanged(SGPropertyNode *node) {
830 if (node->getType() != simgear::props::STRING)
833 char *s = const_cast<char *>(node->getStringValue());
840 #endif // __FG_PROPS_HXX