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;
34 static const char* getLatitudeString ();
35 static const char* getLongitudeString ();
37 static SGConstPropertyNode_ptr _longDeg, _latDeg, _lonLatformat;
39 SGPropertyNode_ptr _offset;
40 SGPropertyNode_ptr _uyear, _umonth, _uday, _uhour, _umin, _usec, _uwday, _udsec;
41 SGPropertyNode_ptr _ryear, _rmonth, _rday, _rhour, _rmin, _rsec, _rwday, _rdsec;
46 * Save a flight to disk.
48 * This function saves all of the archivable properties to a stream
49 * so that the current flight can be restored later.
51 * @param output The output stream to write the XML save file to.
52 * @param write_all If true, write all properties rather than
53 * just the ones flagged as archivable.
54 * @return true if the flight was saved successfully.
56 extern bool fgSaveFlight (std::ostream &output, bool write_all = false);
60 * Load a flight from disk.
62 * This function loads an XML save file from a stream to restore
65 * @param input The input stream to read the XML from.
66 * @return true if the flight was restored successfully.
68 extern bool fgLoadFlight (std::istream &input);
72 * Load properties from a file.
74 * @param file The relative or absolute filename.
75 * @param props The property node to load the properties into.
76 * @param in_fg_root If true, look for the file relative to
77 * $FG_ROOT; otherwise, look for the file relative to the
78 * current working directory.
79 * @return true if the properties loaded successfully, false
82 extern bool fgLoadProps (const char * path, SGPropertyNode * props,
83 bool in_fg_root = true, int default_mode = 0);
85 void setLoggingClasses (const char * c);
86 void setLoggingPriority (const char * p);
89 ////////////////////////////////////////////////////////////////////////
90 // Convenience functions for getting property values.
91 ////////////////////////////////////////////////////////////////////////
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 extern SGPropertyNode * fgGetNode (const char * path, bool create = false);
103 * Get a property node.
105 * @param path The path of the node, relative to root.
106 * @param create true to create the node if it doesn't exist.
107 * @return The node, or 0 if none exists and none was created.
109 inline SGPropertyNode * fgGetNode (const std::string & path, bool create = false)
111 return fgGetNode(path.c_str(), create );
116 * Get a property node with separate index.
118 * This method separates the index from the path string, to make it
119 * easier to iterate through multiple components without using sprintf
120 * to add indices. For example, fgGetNode("foo[1]/bar", 3) will
121 * return the same result as fgGetNode("foo[1]/bar[3]").
123 * @param path The path of the node, relative to root.
124 * @param index The index for the last member of the path (overrides
125 * any given in the string).
126 * @param create true to create the node if it doesn't exist.
127 * @return The node, or 0 if none exists and none was created.
129 extern SGPropertyNode * fgGetNode (const char * path,
130 int index, bool create = false);
133 * Get a property node with separate index.
135 * This method separates the index from the path string, to make it
136 * easier to iterate through multiple components without using sprintf
137 * to add indices. For example, fgGetNode("foo[1]/bar", 3) will
138 * return the same result as fgGetNode("foo[1]/bar[3]").
140 * @param path The path of the node, relative to root.
141 * @param index The index for the last member of the path (overrides
142 * any given in the string).
143 * @param create true to create the node if it doesn't exist.
144 * @return The node, or 0 if none exists and none was created.
146 inline SGPropertyNode * fgGetNode (const std::string & path,
147 int index, bool create = false)
149 return fgGetNode(path.c_str(), index, create );
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 extern bool fgHasNode (const char * path);
162 * Test whether a given node exists.
164 * @param path The path of the node, relative to root.
165 * @return true if the node exists, false otherwise.
167 inline bool fgHasNode (const std::string & path)
169 return fgHasNode( path.c_str() );
174 * Add a listener to a node.
176 * @param listener The listener to add to the node.
177 * @param path The path of the node, relative to root.
178 * @param index The index for the last member of the path (overrides
179 * any given in the string).
181 extern void fgAddChangeListener (SGPropertyChangeListener * listener,
185 * Add a listener to a node.
187 * @param listener The listener to add to the node.
188 * @param path The path of the node, relative to root.
189 * @param index The index for the last member of the path (overrides
190 * any given in the string).
192 inline void fgAddChangeListener (SGPropertyChangeListener * listener,
193 const std::string & path)
195 fgAddChangeListener( listener, path.c_str() );
200 * Add a listener to a node.
202 * @param listener The listener to add to the node.
203 * @param path The path of the node, relative to root.
204 * @param index The index for the last member of the path (overrides
205 * any given in the string).
207 extern void fgAddChangeListener (SGPropertyChangeListener * listener,
208 const char * path, int index);
211 * Add a listener to a node.
213 * @param listener The listener to add to the node.
214 * @param path The path of the node, relative to root.
215 * @param index The index for the last member of the path (overrides
216 * any given in the string).
218 inline void fgAddChangeListener (SGPropertyChangeListener * listener,
219 const std::string & path, int index)
221 fgAddChangeListener( listener, path.c_str(), index );
226 * Get a bool value for a property.
228 * This method is convenient but inefficient. It should be used
229 * infrequently (i.e. for initializing, loading, saving, etc.),
230 * not in the main loop. If you need to get a value frequently,
231 * it is better to look up the node itself using fgGetNode and then
232 * use the node's getBoolValue() method, to avoid the lookup overhead.
234 * @param name The property name.
235 * @param defaultValue The default value to return if the property
237 * @return The property's value as a bool, or the default value provided.
239 extern bool fgGetBool (const char * name, bool defaultValue = false);
242 * Get a bool value for a property.
244 * This method is convenient but inefficient. It should be used
245 * infrequently (i.e. for initializing, loading, saving, etc.),
246 * not in the main loop. If you need to get a value frequently,
247 * it is better to look up the node itself using fgGetNode and then
248 * use the node's getBoolValue() method, to avoid the lookup overhead.
250 * @param name The property name.
251 * @param defaultValue The default value to return if the property
253 * @return The property's value as a bool, or the default value provided.
255 inline bool fgGetBool (const std::string & name, bool defaultValue = false)
257 return fgGetBool( name.c_str(), defaultValue );
262 * Get an int value for a property.
264 * This method is convenient but inefficient. It should be used
265 * infrequently (i.e. for initializing, loading, saving, etc.),
266 * not in the main loop. If you need to get a value frequently,
267 * it is better to look up the node itself using fgGetNode and then
268 * use the node's getIntValue() method, to avoid the lookup overhead.
270 * @param name The property name.
271 * @param defaultValue The default value to return if the property
273 * @return The property's value as an int, or the default value provided.
275 extern int fgGetInt (const char * name, int defaultValue = 0);
278 * Get an int value for a property.
280 * This method is convenient but inefficient. It should be used
281 * infrequently (i.e. for initializing, loading, saving, etc.),
282 * not in the main loop. If you need to get a value frequently,
283 * it is better to look up the node itself using fgGetNode and then
284 * use the node's getIntValue() method, to avoid the lookup overhead.
286 * @param name The property name.
287 * @param defaultValue The default value to return if the property
289 * @return The property's value as an int, or the default value provided.
291 inline int fgGetInt (const std::string & name, int defaultValue = 0)
293 return fgGetInt( name.c_str(), defaultValue );
298 * Get a long value for a property.
300 * This method is convenient but inefficient. It should be used
301 * infrequently (i.e. for initializing, loading, saving, etc.),
302 * not in the main loop. If you need to get a value frequently,
303 * it is better to look up the node itself using fgGetNode and then
304 * use the node's getLongValue() method, to avoid the lookup overhead.
306 * @param name The property name.
307 * @param defaultValue The default value to return if the property
309 * @return The property's value as a long, or the default value provided.
311 extern long fgGetLong (const char * name, long defaultValue = 0L);
314 * Get a long value for a property.
316 * This method is convenient but inefficient. It should be used
317 * infrequently (i.e. for initializing, loading, saving, etc.),
318 * not in the main loop. If you need to get a value frequently,
319 * it is better to look up the node itself using fgGetNode and then
320 * use the node's getLongValue() method, to avoid the lookup overhead.
322 * @param name The property name.
323 * @param defaultValue The default value to return if the property
325 * @return The property's value as a long, or the default value provided.
327 inline long fgGetLong (const std::string & name, long defaultValue = 0L)
329 return fgGetLong( name.c_str(), defaultValue );
334 * Get a float value for a property.
336 * This method is convenient but inefficient. It should be used
337 * infrequently (i.e. for initializing, loading, saving, etc.),
338 * not in the main loop. If you need to get a value frequently,
339 * it is better to look up the node itself using fgGetNode and then
340 * use the node's getFloatValue() method, to avoid the lookup overhead.
342 * @param name The property name.
343 * @param defaultValue The default value to return if the property
345 * @return The property's value as a float, or the default value provided.
347 extern float fgGetFloat (const char * name, float defaultValue = 0.0);
350 * Get a float value for a property.
352 * This method is convenient but inefficient. It should be used
353 * infrequently (i.e. for initializing, loading, saving, etc.),
354 * not in the main loop. If you need to get a value frequently,
355 * it is better to look up the node itself using fgGetNode and then
356 * use the node's getFloatValue() method, to avoid the lookup overhead.
358 * @param name The property name.
359 * @param defaultValue The default value to return if the property
361 * @return The property's value as a float, or the default value provided.
363 inline float fgGetFloat (const std::string & name, float defaultValue = 0.0)
365 return fgGetFloat( name.c_str(), defaultValue );
370 * Get a double value for a property.
372 * This method is convenient but inefficient. It should be used
373 * infrequently (i.e. for initializing, loading, saving, etc.),
374 * not in the main loop. If you need to get a value frequently,
375 * it is better to look up the node itself using fgGetNode and then
376 * use the node's getDoubleValue() method, to avoid the lookup overhead.
378 * @param name The property name.
379 * @param defaultValue The default value to return if the property
381 * @return The property's value as a double, or the default value provided.
383 extern double fgGetDouble (const char * name, double defaultValue = 0.0);
386 * Get a double value for a property.
388 * This method is convenient but inefficient. It should be used
389 * infrequently (i.e. for initializing, loading, saving, etc.),
390 * not in the main loop. If you need to get a value frequently,
391 * it is better to look up the node itself using fgGetNode and then
392 * use the node's getDoubleValue() method, to avoid the lookup overhead.
394 * @param name The property name.
395 * @param defaultValue The default value to return if the property
397 * @return The property's value as a double, or the default value provided.
399 inline double fgGetDouble (const std::string & name, double defaultValue = 0.0)
401 return fgGetDouble( name.c_str(), defaultValue );
406 * Get a string value for a property.
408 * This method is convenient but inefficient. It should be used
409 * infrequently (i.e. for initializing, loading, saving, etc.),
410 * not in the main loop. If you need to get a value frequently,
411 * it is better to look up the node itself using fgGetNode and then
412 * use the node's getStringValue() method, to avoid the lookup overhead.
414 * @param name The property name.
415 * @param defaultValue The default value to return if the property
417 * @return The property's value as a string, or the default value provided.
419 extern const char * fgGetString (const char * name,
420 const char * defaultValue = "");
423 * Get a string value for a property.
425 * This method is convenient but inefficient. It should be used
426 * infrequently (i.e. for initializing, loading, saving, etc.),
427 * not in the main loop. If you need to get a value frequently,
428 * it is better to look up the node itself using fgGetNode and then
429 * use the node's getStringValue() method, to avoid the lookup overhead.
431 * @param name The property name.
432 * @param defaultValue The default value to return if the property
434 * @return The property's value as a string, or the default value provided.
436 inline const char * fgGetString (const std::string & name,
437 const std::string & defaultValue = std::string(""))
439 return fgGetString( name.c_str(), defaultValue.c_str() );
444 * Set a bool value for a property.
446 * Assign a bool value to a property. If the property does not
447 * yet exist, it will be created and its type will be set to
448 * BOOL; if it has a type of UNKNOWN, the type will also be set to
449 * BOOL; otherwise, the bool value will be converted to the property's
452 * @param name The property name.
453 * @param val The new value for the property.
454 * @return true if the assignment succeeded, false otherwise.
456 extern bool fgSetBool (const char * name, bool val);
459 * Set a bool value for a property.
461 * Assign a bool value to a property. If the property does not
462 * yet exist, it will be created and its type will be set to
463 * BOOL; if it has a type of UNKNOWN, the type will also be set to
464 * BOOL; otherwise, the bool value will be converted to the property's
467 * @param name The property name.
468 * @param val The new value for the property.
469 * @return true if the assignment succeeded, false otherwise.
471 inline bool fgSetBool (const std::string & name, bool val)
473 return fgSetBool( name.c_str(), val );
478 * Set an int value for a property.
480 * Assign an int value to a property. If the property does not
481 * yet exist, it will be created and its type will be set to
482 * INT; if it has a type of UNKNOWN, the type will also be set to
483 * INT; otherwise, the bool value will be converted to the property's
486 * @param name The property name.
487 * @param val The new value for the property.
488 * @return true if the assignment succeeded, false otherwise.
490 extern bool fgSetInt (const char * name, int val);
493 * Set an int value for a property.
495 * Assign an int value to a property. If the property does not
496 * yet exist, it will be created and its type will be set to
497 * INT; if it has a type of UNKNOWN, the type will also be set to
498 * INT; otherwise, the bool value will be converted to the property's
501 * @param name The property name.
502 * @param val The new value for the property.
503 * @return true if the assignment succeeded, false otherwise.
505 inline bool fgSetInt (const std::string & name, int val)
507 return fgSetInt( name.c_str(), val );
511 * Set a long value for a property.
513 * Assign a long value to a property. If the property does not
514 * yet exist, it will be created and its type will be set to
515 * LONG; if it has a type of UNKNOWN, the type will also be set to
516 * LONG; otherwise, the bool value will be converted to the property's
519 * @param name The property name.
520 * @param val The new value for the property.
521 * @return true if the assignment succeeded, false otherwise.
523 extern bool fgSetLong (const char * name, long val);
526 * Set a long value for a property.
528 * Assign a long value to a property. If the property does not
529 * yet exist, it will be created and its type will be set to
530 * LONG; if it has a type of UNKNOWN, the type will also be set to
531 * LONG; otherwise, the bool value will be converted to the property's
534 * @param name The property name.
535 * @param val The new value for the property.
536 * @return true if the assignment succeeded, false otherwise.
538 inline bool fgSetLong (const std::string & name, long val)
540 return fgSetLong( name.c_str(), val );
545 * Set a float value for a property.
547 * Assign a float value to a property. If the property does not
548 * yet exist, it will be created and its type will be set to
549 * FLOAT; if it has a type of UNKNOWN, the type will also be set to
550 * FLOAT; otherwise, the bool value will be converted to the property's
553 * @param name The property name.
554 * @param val The new value for the property.
555 * @return true if the assignment succeeded, false otherwise.
557 extern bool fgSetFloat (const char * name, float val);
560 * Set a float value for a property.
562 * Assign a float value to a property. If the property does not
563 * yet exist, it will be created and its type will be set to
564 * FLOAT; if it has a type of UNKNOWN, the type will also be set to
565 * FLOAT; otherwise, the bool value will be converted to the property's
568 * @param name The property name.
569 * @param val The new value for the property.
570 * @return true if the assignment succeeded, false otherwise.
572 inline bool fgSetFloat (const std::string & name, float val)
574 return fgSetFloat( name.c_str(), val );
579 * Set a double value for a property.
581 * Assign a double value to a property. If the property does not
582 * yet exist, it will be created and its type will be set to
583 * DOUBLE; if it has a type of UNKNOWN, the type will also be set to
584 * DOUBLE; otherwise, the double value will be converted to the property's
587 * @param name The property name.
588 * @param val The new value for the property.
589 * @return true if the assignment succeeded, false otherwise.
591 extern bool fgSetDouble (const char * name, double val);
594 * Set a double value for a property.
596 * Assign a double value to a property. If the property does not
597 * yet exist, it will be created and its type will be set to
598 * DOUBLE; if it has a type of UNKNOWN, the type will also be set to
599 * DOUBLE; otherwise, the double value will be converted to the property's
602 * @param name The property name.
603 * @param val The new value for the property.
604 * @return true if the assignment succeeded, false otherwise.
606 inline bool fgSetDouble (const std::string & name, double val)
608 return fgSetDouble( name.c_str(), val );
613 * Set a string value for a property.
615 * Assign a string value to a property. If the property does not
616 * yet exist, it will be created and its type will be set to
617 * STRING; if it has a type of UNKNOWN, the type will also be set to
618 * STRING; otherwise, the string value will be converted to the property's
621 * @param name The property name.
622 * @param val The new value for the property.
623 * @return true if the assignment succeeded, false otherwise.
625 extern bool fgSetString (const char * name, const char * val);
628 * Set a string value for a property.
630 * Assign a string value to a property. If the property does not
631 * yet exist, it will be created and its type will be set to
632 * STRING; if it has a type of UNKNOWN, the type will also be set to
633 * STRING; otherwise, the string value will be converted to the property's
636 * @param name The property name.
637 * @param val The new value for the property.
638 * @return true if the assignment succeeded, false otherwise.
640 inline bool fgSetString (const std::string & name, const std::string & val)
642 return fgSetString( name.c_str(), val.c_str() );
647 ////////////////////////////////////////////////////////////////////////
648 // Convenience functions for setting property attributes.
649 ////////////////////////////////////////////////////////////////////////
653 * Set the state of the archive attribute for a property.
655 * If the archive attribute is true, the property will be written
656 * when a flight is saved; if it is false, the property will be
659 * A warning message will be printed if the property does not exist.
661 * @param name The property name.
662 * @param state The state of the archive attribute (defaults to true).
664 extern void fgSetArchivable (const char * name, bool state = true);
668 * Set the state of the read attribute for a property.
670 * If the read attribute is true, the property value will be readable;
671 * if it is false, the property value will always be the default value
674 * A warning message will be printed if the property does not exist.
676 * @param name The property name.
677 * @param state The state of the read attribute (defaults to true).
679 extern void fgSetReadable (const char * name, bool state = true);
683 * Set the state of the write attribute for a property.
685 * If the write attribute is true, the property value may be modified
686 * (depending on how it is tied); if the write attribute is false, the
687 * property value may not be modified.
689 * A warning message will be printed if the property does not exist.
691 * @param name The property name.
692 * @param state The state of the write attribute (defaults to true).
694 extern void fgSetWritable (const char * name, bool state = true);
698 ////////////////////////////////////////////////////////////////////////
699 // Convenience functions for tying properties, with logging.
700 ////////////////////////////////////////////////////////////////////////
704 * Untie a property from an external data source.
706 * Classes should use this function to release control of any
707 * properties they are managing.
709 extern void fgUntie (const char * name);
713 * Tie a property to a pair of simple functions.
715 * Every time the property value is queried, the getter (if any) will
716 * be invoked; every time the property value is modified, the setter
717 * (if any) will be invoked. The getter can be 0 to make the property
718 * unreadable, and the setter can be 0 to make the property
721 * @param name The property name to tie (full path).
722 * @param getter The getter function, or 0 if the value is unreadable.
723 * @param setter The setter function, or 0 if the value is unmodifiable.
724 * @param useDefault true if the setter should be invoked with any existing
725 * property value should be; false if the old value should be
726 * discarded; defaults to true.
730 fgTie (const char * name, V (*getter)(), void (*setter)(V) = 0,
731 bool useDefault = true)
733 if (!globals->get_props()->tie(name, SGRawValueFunctions<V>(getter, setter),
735 SG_LOG(SG_GENERAL, SG_WARN,
736 "Failed to tie property " << name << " to functions");
741 * Tie a property to a pair of indexed functions.
743 * Every time the property value is queried, the getter (if any) will
744 * be invoked with the index provided; every time the property value
745 * is modified, the setter (if any) will be invoked with the index
746 * provided. The getter can be 0 to make the property unreadable, and
747 * the setter can be 0 to make the property unmodifiable.
749 * @param name The property name to tie (full path).
750 * @param index The integer argument to pass to the getter and
752 * @param getter The getter function, or 0 if the value is unreadable.
753 * @param setter The setter function, or 0 if the value is unmodifiable.
754 * @param useDefault true if the setter should be invoked with any existing
755 * property value should be; false if the old value should be
756 * discarded; defaults to true.
760 fgTie (const char * name, int index, V (*getter)(int),
761 void (*setter)(int, V) = 0, bool useDefault = true)
763 if (!globals->get_props()->tie(name,
764 SGRawValueFunctionsIndexed<V>(index,
768 SG_LOG(SG_GENERAL, SG_WARN,
769 "Failed to tie property " << name << " to indexed functions");
774 * Tie a property to a pair of object methods.
776 * Every time the property value is queried, the getter (if any) will
777 * be invoked; every time the property value is modified, the setter
778 * (if any) will be invoked. The getter can be 0 to make the property
779 * unreadable, and the setter can be 0 to make the property
782 * @param name The property name to tie (full path).
783 * @param obj The object whose methods should be invoked.
784 * @param getter The object's getter method, or 0 if the value is
786 * @param setter The object's setter method, or 0 if the value is
788 * @param useDefault true if the setter should be invoked with any existing
789 * property value should be; false if the old value should be
790 * discarded; defaults to true.
792 template <class T, class V>
794 fgTie (const char * name, T * obj, V (T::*getter)() const,
795 void (T::*setter)(V) = 0, bool useDefault = true)
797 if (!globals->get_props()->tie(name,
798 SGRawValueMethods<T,V>(*obj, getter, setter),
800 SG_LOG(SG_GENERAL, SG_WARN,
801 "Failed to tie property " << name << " to object methods");
806 * Tie a property to a pair of indexed object methods.
808 * Every time the property value is queried, the getter (if any) will
809 * be invoked with the index provided; every time the property value
810 * is modified, the setter (if any) will be invoked with the index
811 * provided. The getter can be 0 to make the property unreadable, and
812 * the setter can be 0 to make the property unmodifiable.
814 * @param name The property name to tie (full path).
815 * @param obj The object whose methods should be invoked.
816 * @param index The integer argument to pass to the getter and
818 * @param getter The getter method, or 0 if the value is unreadable.
819 * @param setter The setter method, or 0 if the value is unmodifiable.
820 * @param useDefault true if the setter should be invoked with any existing
821 * property value should be; false if the old value should be
822 * discarded; defaults to true.
824 template <class T, class V>
826 fgTie (const char * name, T * obj, int index,
827 V (T::*getter)(int) const, void (T::*setter)(int, V) = 0,
828 bool useDefault = true)
830 if (!globals->get_props()->tie(name,
831 SGRawValueMethodsIndexed<T,V>(*obj,
836 SG_LOG(SG_GENERAL, SG_WARN,
837 "Failed to tie property " << name << " to indexed object methods");
841 class FGMakeUpperCase : public SGPropertyChangeListener {
843 void valueChanged(SGPropertyNode *node) {
844 if (node->getType() != simgear::props::STRING)
847 char *s = const_cast<char *>(node->getStringValue());
854 #endif // __FG_PROPS_HXX