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>
14 #include <Main/globals.hxx>
16 ////////////////////////////////////////////////////////////////////////
17 // Property management.
18 ////////////////////////////////////////////////////////////////////////
20 class FGProperties : public SGSubsystem
24 virtual ~FGProperties ();
29 void update (double dt);
32 simgear::TiedPropertyList _tiedProperties;
37 * Save a flight to disk.
39 * This function saves all of the archivable properties to a stream
40 * so that the current flight can be restored later.
42 * @param output The output stream to write the XML save file to.
43 * @param write_all If true, write all properties rather than
44 * just the ones flagged as archivable.
45 * @return true if the flight was saved successfully.
47 extern bool fgSaveFlight (std::ostream &output, bool write_all = false);
51 * Load a flight from disk.
53 * This function loads an XML save file from a stream to restore
56 * @param input The input stream to read the XML from.
57 * @return true if the flight was restored successfully.
59 extern bool fgLoadFlight (std::istream &input);
63 * Load properties from a file.
65 * @param file The relative or absolute filename.
66 * @param props The property node to load the properties into.
67 * @param in_fg_root If true, look for the file relative to
68 * $FG_ROOT; otherwise, look for the file relative to the
69 * current working directory.
70 * @return true if the properties loaded successfully, false
73 extern bool fgLoadProps (const char * path, SGPropertyNode * props,
74 bool in_fg_root = true, int default_mode = 0);
76 void setLoggingClasses (const char * c);
77 void setLoggingPriority (const char * p);
80 ////////////////////////////////////////////////////////////////////////
81 // Convenience functions for getting property values.
82 ////////////////////////////////////////////////////////////////////////
85 * Get a property node.
87 * @param path The path of the node, relative to root.
88 * @param create true to create the node if it doesn't exist.
89 * @return The node, or 0 if none exists and none was created.
91 extern SGPropertyNode * fgGetNode (const char * path, bool create = false);
94 * Get a property node.
96 * @param path The path of the node, relative to root.
97 * @param create true to create the node if it doesn't exist.
98 * @return The node, or 0 if none exists and none was created.
100 inline SGPropertyNode * fgGetNode (const std::string & path, bool create = false)
102 return fgGetNode(path.c_str(), create );
107 * Get a property node with separate index.
109 * This method separates the index from the path string, to make it
110 * easier to iterate through multiple components without using sprintf
111 * to add indices. For example, fgGetNode("foo[1]/bar", 3) will
112 * return the same result as fgGetNode("foo[1]/bar[3]").
114 * @param path The path of the node, relative to root.
115 * @param index The index for the last member of the path (overrides
116 * any given in the string).
117 * @param create true to create the node if it doesn't exist.
118 * @return The node, or 0 if none exists and none was created.
120 extern SGPropertyNode * fgGetNode (const char * path,
121 int index, bool create = false);
124 * Get a property node with separate index.
126 * This method separates the index from the path string, to make it
127 * easier to iterate through multiple components without using sprintf
128 * to add indices. For example, fgGetNode("foo[1]/bar", 3) will
129 * return the same result as fgGetNode("foo[1]/bar[3]").
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).
134 * @param create true to create the node if it doesn't exist.
135 * @return The node, or 0 if none exists and none was created.
137 inline SGPropertyNode * fgGetNode (const std::string & path,
138 int index, bool create = false)
140 return fgGetNode(path.c_str(), index, create );
145 * Test whether a given node exists.
147 * @param path The path of the node, relative to root.
148 * @return true if the node exists, false otherwise.
150 extern bool fgHasNode (const char * path);
153 * Test whether a given node exists.
155 * @param path The path of the node, relative to root.
156 * @return true if the node exists, false otherwise.
158 inline bool fgHasNode (const std::string & path)
160 return fgHasNode( path.c_str() );
165 * Add a listener to a node.
167 * @param listener The listener to add to the node.
168 * @param path The path of the node, relative to root.
169 * @param index The index for the last member of the path (overrides
170 * any given in the string).
172 extern void fgAddChangeListener (SGPropertyChangeListener * listener,
176 * Add a listener to a node.
178 * @param listener The listener to add to the node.
179 * @param path The path of the node, relative to root.
180 * @param index The index for the last member of the path (overrides
181 * any given in the string).
183 inline void fgAddChangeListener (SGPropertyChangeListener * listener,
184 const std::string & path)
186 fgAddChangeListener( listener, path.c_str() );
191 * Add a listener to a node.
193 * @param listener The listener to add to the node.
194 * @param path The path of the node, relative to root.
195 * @param index The index for the last member of the path (overrides
196 * any given in the string).
198 extern void fgAddChangeListener (SGPropertyChangeListener * listener,
199 const char * path, int index);
202 * Add a listener to a node.
204 * @param listener The listener to add to the node.
205 * @param path The path of the node, relative to root.
206 * @param index The index for the last member of the path (overrides
207 * any given in the string).
209 inline void fgAddChangeListener (SGPropertyChangeListener * listener,
210 const std::string & path, int index)
212 fgAddChangeListener( listener, path.c_str(), index );
217 * Get a bool value for a property.
219 * This method is convenient but inefficient. It should be used
220 * infrequently (i.e. for initializing, loading, saving, etc.),
221 * not in the main loop. If you need to get a value frequently,
222 * it is better to look up the node itself using fgGetNode and then
223 * use the node's getBoolValue() method, to avoid the lookup overhead.
225 * @param name The property name.
226 * @param defaultValue The default value to return if the property
228 * @return The property's value as a bool, or the default value provided.
230 extern bool fgGetBool (const char * name, bool defaultValue = false);
233 * Get a bool value for a property.
235 * This method is convenient but inefficient. It should be used
236 * infrequently (i.e. for initializing, loading, saving, etc.),
237 * not in the main loop. If you need to get a value frequently,
238 * it is better to look up the node itself using fgGetNode and then
239 * use the node's getBoolValue() method, to avoid the lookup overhead.
241 * @param name The property name.
242 * @param defaultValue The default value to return if the property
244 * @return The property's value as a bool, or the default value provided.
246 inline bool fgGetBool (const std::string & name, bool defaultValue = false)
248 return fgGetBool( name.c_str(), defaultValue );
253 * Get an int value for a property.
255 * This method is convenient but inefficient. It should be used
256 * infrequently (i.e. for initializing, loading, saving, etc.),
257 * not in the main loop. If you need to get a value frequently,
258 * it is better to look up the node itself using fgGetNode and then
259 * use the node's getIntValue() method, to avoid the lookup overhead.
261 * @param name The property name.
262 * @param defaultValue The default value to return if the property
264 * @return The property's value as an int, or the default value provided.
266 extern int fgGetInt (const char * name, int defaultValue = 0);
269 * Get an int value for a property.
271 * This method is convenient but inefficient. It should be used
272 * infrequently (i.e. for initializing, loading, saving, etc.),
273 * not in the main loop. If you need to get a value frequently,
274 * it is better to look up the node itself using fgGetNode and then
275 * use the node's getIntValue() method, to avoid the lookup overhead.
277 * @param name The property name.
278 * @param defaultValue The default value to return if the property
280 * @return The property's value as an int, or the default value provided.
282 inline int fgGetInt (const std::string & name, int defaultValue = 0)
284 return fgGetInt( name.c_str(), defaultValue );
289 * Get a long value for a property.
291 * This method is convenient but inefficient. It should be used
292 * infrequently (i.e. for initializing, loading, saving, etc.),
293 * not in the main loop. If you need to get a value frequently,
294 * it is better to look up the node itself using fgGetNode and then
295 * use the node's getLongValue() method, to avoid the lookup overhead.
297 * @param name The property name.
298 * @param defaultValue The default value to return if the property
300 * @return The property's value as a long, or the default value provided.
302 extern int fgGetLong (const char * name, long defaultValue = 0L);
305 * Get a long value for a property.
307 * This method is convenient but inefficient. It should be used
308 * infrequently (i.e. for initializing, loading, saving, etc.),
309 * not in the main loop. If you need to get a value frequently,
310 * it is better to look up the node itself using fgGetNode and then
311 * use the node's getLongValue() method, to avoid the lookup overhead.
313 * @param name The property name.
314 * @param defaultValue The default value to return if the property
316 * @return The property's value as a long, or the default value provided.
318 inline int fgGetLong (const std::string & name, long defaultValue = 0L)
320 return fgGetLong( name.c_str(), defaultValue );
325 * Get a float value for a property.
327 * This method is convenient but inefficient. It should be used
328 * infrequently (i.e. for initializing, loading, saving, etc.),
329 * not in the main loop. If you need to get a value frequently,
330 * it is better to look up the node itself using fgGetNode and then
331 * use the node's getFloatValue() method, to avoid the lookup overhead.
333 * @param name The property name.
334 * @param defaultValue The default value to return if the property
336 * @return The property's value as a float, or the default value provided.
338 extern float fgGetFloat (const char * name, float defaultValue = 0.0);
341 * Get a float value for a property.
343 * This method is convenient but inefficient. It should be used
344 * infrequently (i.e. for initializing, loading, saving, etc.),
345 * not in the main loop. If you need to get a value frequently,
346 * it is better to look up the node itself using fgGetNode and then
347 * use the node's getFloatValue() method, to avoid the lookup overhead.
349 * @param name The property name.
350 * @param defaultValue The default value to return if the property
352 * @return The property's value as a float, or the default value provided.
354 inline float fgGetFloat (const std::string & name, float defaultValue = 0.0)
356 return fgGetFloat( name.c_str(), defaultValue );
361 * Get a double value for a property.
363 * This method is convenient but inefficient. It should be used
364 * infrequently (i.e. for initializing, loading, saving, etc.),
365 * not in the main loop. If you need to get a value frequently,
366 * it is better to look up the node itself using fgGetNode and then
367 * use the node's getDoubleValue() method, to avoid the lookup overhead.
369 * @param name The property name.
370 * @param defaultValue The default value to return if the property
372 * @return The property's value as a double, or the default value provided.
374 extern double fgGetDouble (const char * name, double defaultValue = 0.0);
377 * Get a double value for a property.
379 * This method is convenient but inefficient. It should be used
380 * infrequently (i.e. for initializing, loading, saving, etc.),
381 * not in the main loop. If you need to get a value frequently,
382 * it is better to look up the node itself using fgGetNode and then
383 * use the node's getDoubleValue() method, to avoid the lookup overhead.
385 * @param name The property name.
386 * @param defaultValue The default value to return if the property
388 * @return The property's value as a double, or the default value provided.
390 inline double fgGetDouble (const std::string & name, double defaultValue = 0.0)
392 return fgGetDouble( name.c_str(), defaultValue );
397 * Get a string value for a property.
399 * This method is convenient but inefficient. It should be used
400 * infrequently (i.e. for initializing, loading, saving, etc.),
401 * not in the main loop. If you need to get a value frequently,
402 * it is better to look up the node itself using fgGetNode and then
403 * use the node's getStringValue() method, to avoid the lookup overhead.
405 * @param name The property name.
406 * @param defaultValue The default value to return if the property
408 * @return The property's value as a string, or the default value provided.
410 extern const char * fgGetString (const char * name,
411 const char * defaultValue = "");
414 * Get a string value for a property.
416 * This method is convenient but inefficient. It should be used
417 * infrequently (i.e. for initializing, loading, saving, etc.),
418 * not in the main loop. If you need to get a value frequently,
419 * it is better to look up the node itself using fgGetNode and then
420 * use the node's getStringValue() method, to avoid the lookup overhead.
422 * @param name The property name.
423 * @param defaultValue The default value to return if the property
425 * @return The property's value as a string, or the default value provided.
427 inline const char * fgGetString (const std::string & name,
428 const std::string & defaultValue = std::string(""))
430 return fgGetString( name.c_str(), defaultValue.c_str() );
435 * Set a bool value for a property.
437 * Assign a bool value to a property. If the property does not
438 * yet exist, it will be created and its type will be set to
439 * BOOL; if it has a type of UNKNOWN, the type will also be set to
440 * BOOL; otherwise, the bool value will be converted to the property's
443 * @param name The property name.
444 * @param val The new value for the property.
445 * @return true if the assignment succeeded, false otherwise.
447 extern bool fgSetBool (const char * name, bool val);
450 * Set a bool value for a property.
452 * Assign a bool value to a property. If the property does not
453 * yet exist, it will be created and its type will be set to
454 * BOOL; if it has a type of UNKNOWN, the type will also be set to
455 * BOOL; otherwise, the bool value will be converted to the property's
458 * @param name The property name.
459 * @param val The new value for the property.
460 * @return true if the assignment succeeded, false otherwise.
462 inline bool fgSetBool (const std::string & name, bool val)
464 return fgSetBool( name.c_str(), val );
469 * Set an int value for a property.
471 * Assign an int value to a property. If the property does not
472 * yet exist, it will be created and its type will be set to
473 * INT; if it has a type of UNKNOWN, the type will also be set to
474 * INT; otherwise, the bool value will be converted to the property's
477 * @param name The property name.
478 * @param val The new value for the property.
479 * @return true if the assignment succeeded, false otherwise.
481 extern bool fgSetInt (const char * name, int val);
484 * Set an int value for a property.
486 * Assign an int value to a property. If the property does not
487 * yet exist, it will be created and its type will be set to
488 * INT; if it has a type of UNKNOWN, the type will also be set to
489 * INT; otherwise, the bool value will be converted to the property's
492 * @param name The property name.
493 * @param val The new value for the property.
494 * @return true if the assignment succeeded, false otherwise.
496 inline bool fgSetInt (const std::string & name, int val)
498 return fgSetInt( name.c_str(), val );
502 * Set a long value for a property.
504 * Assign a long value to a property. If the property does not
505 * yet exist, it will be created and its type will be set to
506 * LONG; if it has a type of UNKNOWN, the type will also be set to
507 * LONG; otherwise, the bool value will be converted to the property's
510 * @param name The property name.
511 * @param val The new value for the property.
512 * @return true if the assignment succeeded, false otherwise.
514 extern bool fgSetLong (const char * name, long val);
517 * Set a long value for a property.
519 * Assign a long value to a property. If the property does not
520 * yet exist, it will be created and its type will be set to
521 * LONG; if it has a type of UNKNOWN, the type will also be set to
522 * LONG; otherwise, the bool value will be converted to the property's
525 * @param name The property name.
526 * @param val The new value for the property.
527 * @return true if the assignment succeeded, false otherwise.
529 inline bool fgSetLong (const std::string & name, long val)
531 return fgSetLong( name.c_str(), val );
536 * Set a float value for a property.
538 * Assign a float value to a property. If the property does not
539 * yet exist, it will be created and its type will be set to
540 * FLOAT; if it has a type of UNKNOWN, the type will also be set to
541 * FLOAT; otherwise, the bool value will be converted to the property's
544 * @param name The property name.
545 * @param val The new value for the property.
546 * @return true if the assignment succeeded, false otherwise.
548 extern bool fgSetFloat (const char * name, float val);
551 * Set a float value for a property.
553 * Assign a float value to a property. If the property does not
554 * yet exist, it will be created and its type will be set to
555 * FLOAT; if it has a type of UNKNOWN, the type will also be set to
556 * FLOAT; otherwise, the bool value will be converted to the property's
559 * @param name The property name.
560 * @param val The new value for the property.
561 * @return true if the assignment succeeded, false otherwise.
563 inline bool fgSetFloat (const std::string & name, float val)
565 return fgSetFloat( name.c_str(), val );
570 * Set a double value for a property.
572 * Assign a double value to a property. If the property does not
573 * yet exist, it will be created and its type will be set to
574 * DOUBLE; if it has a type of UNKNOWN, the type will also be set to
575 * DOUBLE; otherwise, the double value will be converted to the property's
578 * @param name The property name.
579 * @param val The new value for the property.
580 * @return true if the assignment succeeded, false otherwise.
582 extern bool fgSetDouble (const char * name, double val);
585 * Set a double value for a property.
587 * Assign a double value to a property. If the property does not
588 * yet exist, it will be created and its type will be set to
589 * DOUBLE; if it has a type of UNKNOWN, the type will also be set to
590 * DOUBLE; otherwise, the double value will be converted to the property's
593 * @param name The property name.
594 * @param val The new value for the property.
595 * @return true if the assignment succeeded, false otherwise.
597 inline bool fgSetDouble (const std::string & name, double val)
599 return fgSetDouble( name.c_str(), val );
604 * Set a string value for a property.
606 * Assign a string value to a property. If the property does not
607 * yet exist, it will be created and its type will be set to
608 * STRING; if it has a type of UNKNOWN, the type will also be set to
609 * STRING; otherwise, the string value will be converted to the property's
612 * @param name The property name.
613 * @param val The new value for the property.
614 * @return true if the assignment succeeded, false otherwise.
616 extern bool fgSetString (const char * name, const char * val);
619 * Set a string value for a property.
621 * Assign a string value to a property. If the property does not
622 * yet exist, it will be created and its type will be set to
623 * STRING; if it has a type of UNKNOWN, the type will also be set to
624 * STRING; otherwise, the string value will be converted to the property's
627 * @param name The property name.
628 * @param val The new value for the property.
629 * @return true if the assignment succeeded, false otherwise.
631 inline bool fgSetString (const std::string & name, const std::string & val)
633 return fgSetString( name.c_str(), val.c_str() );
638 ////////////////////////////////////////////////////////////////////////
639 // Convenience functions for setting property attributes.
640 ////////////////////////////////////////////////////////////////////////
644 * Set the state of the archive attribute for a property.
646 * If the archive attribute is true, the property will be written
647 * when a flight is saved; if it is false, the property will be
650 * A warning message will be printed if the property does not exist.
652 * @param name The property name.
653 * @param state The state of the archive attribute (defaults to true).
655 extern void fgSetArchivable (const char * name, bool state = true);
659 * Set the state of the read attribute for a property.
661 * If the read attribute is true, the property value will be readable;
662 * if it is false, the property value will always be the default value
665 * A warning message will be printed if the property does not exist.
667 * @param name The property name.
668 * @param state The state of the read attribute (defaults to true).
670 extern void fgSetReadable (const char * name, bool state = true);
674 * Set the state of the write attribute for a property.
676 * If the write attribute is true, the property value may be modified
677 * (depending on how it is tied); if the write attribute is false, the
678 * property value may not be modified.
680 * A warning message will be printed if the property does not exist.
682 * @param name The property name.
683 * @param state The state of the write attribute (defaults to true).
685 extern void fgSetWritable (const char * name, bool state = true);
689 ////////////////////////////////////////////////////////////////////////
690 // Convenience functions for tying properties, with logging.
691 ////////////////////////////////////////////////////////////////////////
695 * Untie a property from an external data source.
697 * Classes should use this function to release control of any
698 * properties they are managing.
700 extern void fgUntie (const char * name);
704 * Tie a property to a pair of simple functions.
706 * Every time the property value is queried, the getter (if any) will
707 * be invoked; every time the property value is modified, the setter
708 * (if any) will be invoked. The getter can be 0 to make the property
709 * unreadable, and the setter can be 0 to make the property
712 * @param name The property name to tie (full path).
713 * @param getter The getter function, or 0 if the value is unreadable.
714 * @param setter The setter function, or 0 if the value is unmodifiable.
715 * @param useDefault true if the setter should be invoked with any existing
716 * property value should be; false if the old value should be
717 * discarded; defaults to true.
721 fgTie (const char * name, V (*getter)(), void (*setter)(V) = 0,
722 bool useDefault = true)
724 if (!globals->get_props()->tie(name, SGRawValueFunctions<V>(getter, setter),
726 SG_LOG(SG_GENERAL, SG_WARN,
727 "Failed to tie property " << name << " to functions");
732 * Tie a property to a pair of indexed functions.
734 * Every time the property value is queried, the getter (if any) will
735 * be invoked with the index provided; every time the property value
736 * is modified, the setter (if any) will be invoked with the index
737 * provided. The getter can be 0 to make the property unreadable, and
738 * the setter can be 0 to make the property unmodifiable.
740 * @param name The property name to tie (full path).
741 * @param index The integer argument to pass to the getter and
743 * @param getter The getter function, or 0 if the value is unreadable.
744 * @param setter The setter function, or 0 if the value is unmodifiable.
745 * @param useDefault true if the setter should be invoked with any existing
746 * property value should be; false if the old value should be
747 * discarded; defaults to true.
751 fgTie (const char * name, int index, V (*getter)(int),
752 void (*setter)(int, V) = 0, bool useDefault = true)
754 if (!globals->get_props()->tie(name,
755 SGRawValueFunctionsIndexed<V>(index,
759 SG_LOG(SG_GENERAL, SG_WARN,
760 "Failed to tie property " << name << " to indexed functions");
765 * Tie a property to a pair of object methods.
767 * Every time the property value is queried, the getter (if any) will
768 * be invoked; every time the property value is modified, the setter
769 * (if any) will be invoked. The getter can be 0 to make the property
770 * unreadable, and the setter can be 0 to make the property
773 * @param name The property name to tie (full path).
774 * @param obj The object whose methods should be invoked.
775 * @param getter The object's getter method, or 0 if the value is
777 * @param setter The object's setter method, or 0 if the value is
779 * @param useDefault true if the setter should be invoked with any existing
780 * property value should be; false if the old value should be
781 * discarded; defaults to true.
783 template <class T, class V>
785 fgTie (const char * name, T * obj, V (T::*getter)() const,
786 void (T::*setter)(V) = 0, bool useDefault = true)
788 if (!globals->get_props()->tie(name,
789 SGRawValueMethods<T,V>(*obj, getter, setter),
791 SG_LOG(SG_GENERAL, SG_WARN,
792 "Failed to tie property " << name << " to object methods");
797 * Tie a property to a pair of indexed object methods.
799 * Every time the property value is queried, the getter (if any) will
800 * be invoked with the index provided; every time the property value
801 * is modified, the setter (if any) will be invoked with the index
802 * provided. The getter can be 0 to make the property unreadable, and
803 * the setter can be 0 to make the property unmodifiable.
805 * @param name The property name to tie (full path).
806 * @param obj The object whose methods should be invoked.
807 * @param index The integer argument to pass to the getter and
809 * @param getter The getter method, or 0 if the value is unreadable.
810 * @param setter The setter method, or 0 if the value is unmodifiable.
811 * @param useDefault true if the setter should be invoked with any existing
812 * property value should be; false if the old value should be
813 * discarded; defaults to true.
815 template <class T, class V>
817 fgTie (const char * name, T * obj, int index,
818 V (T::*getter)(int) const, void (T::*setter)(int, V) = 0,
819 bool useDefault = true)
821 if (!globals->get_props()->tie(name,
822 SGRawValueMethodsIndexed<T,V>(*obj,
827 SG_LOG(SG_GENERAL, SG_WARN,
828 "Failed to tie property " << name << " to indexed object methods");
832 class FGMakeUpperCase : public SGPropertyChangeListener {
834 void valueChanged(SGPropertyNode *node) {
835 if (node->getType() != simgear::props::STRING)
838 char *s = const_cast<char *>(node->getStringValue());
845 #endif // __FG_PROPS_HXX