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
9 #include <simgear/debug/logstream.hxx>
10 #include <simgear/misc/props.hxx>
11 #include "globals.hxx"
14 ////////////////////////////////////////////////////////////////////////
15 // Property management.
16 ////////////////////////////////////////////////////////////////////////
20 * Initialize the default FlightGear properties.
22 * These are mostly properties that haven't been claimed by a
23 * specific module yet. This function should be invoked once,
24 * while the program is starting (and after the global property
25 * tree has been created).
27 extern void fgInitProps (); // fixme: how are they untied?
31 * Update the default FlightGear properties.
33 * This function should be invoked once in each loop to update all
34 * of the default properties.
36 extern void fgUpdateProps ();
40 * Save a flight to disk.
42 * This function saves all of the archivable properties to a stream
43 * so that the current flight can be restored later.
45 * @param output The output stream to write the XML save file to.
46 * @param write_all If true, write all properties rather than
47 * just the ones flagged as archivable.
48 * @return true if the flight was saved successfully.
50 extern bool fgSaveFlight (ostream &output, bool write_all = false);
54 * Load a flight from disk.
56 * This function loads an XML save file from a stream to restore
59 * @param input The input stream to read the XML from.
60 * @return true if the flight was restored successfully.
62 extern bool fgLoadFlight (istream &input);
66 ////////////////////////////////////////////////////////////////////////
67 // Convenience functions for getting property values.
68 ////////////////////////////////////////////////////////////////////////
72 * Get a property node.
74 * @param path The path of the node, relative to root.
75 * @param create true to create the node if it doesn't exist.
76 * @return The node, or 0 if none exists and none was created.
78 inline SGPropertyNode *
79 fgGetNode (const string &path, bool create = false)
81 return globals->get_props()->getNode(path, create);
86 * Get a property node with separate index.
88 * This method separates the index from the path string, to make it
89 * easier to iterate through multiple components without using sprintf
90 * to add indices. For example, fgGetNode("foo[1]/bar", 3) will
91 * return the same result as fgGetNode("foo[1]/bar[3]").
93 * @param path The path of the node, relative to root.
94 * @param index The index for the last member of the path (overrides
95 * any given in the string).
96 * @param create true to create the node if it doesn't exist.
97 * @return The node, or 0 if none exists and none was created.
99 inline SGPropertyNode *
100 fgGetNode (const string &path, int index, bool create = false)
102 return globals->get_props()->getNode(path, index, create);
107 * Test whether a given node exists.
109 * @param path The path of the node, relative to root.
110 * @return true if the node exists, false otherwise.
113 fgHasNode (const string &path)
115 return (fgGetNode(path, false) != 0);
120 * Get a bool value for a property.
122 * This method is convenient but inefficient. It should be used
123 * infrequently (i.e. for initializing, loading, saving, etc.),
124 * not in the main loop. If you need to get a value frequently,
125 * it is better to look up the node itself using fgGetNode and then
126 * use the node's getBoolValue() method, to avoid the lookup overhead.
128 * @param name The property name.
129 * @param defaultValue The default value to return if the property
131 * @return The property's value as a bool, or the default value provided.
133 inline bool fgGetBool (const string &name, bool defaultValue = false)
135 return globals->get_props()->getBoolValue(name, defaultValue);
140 * Get an int value for a property.
142 * This method is convenient but inefficient. It should be used
143 * infrequently (i.e. for initializing, loading, saving, etc.),
144 * not in the main loop. If you need to get a value frequently,
145 * it is better to look up the node itself using fgGetNode and then
146 * use the node's getIntValue() method, to avoid the lookup overhead.
148 * @param name The property name.
149 * @param defaultValue The default value to return if the property
151 * @return The property's value as an int, or the default value provided.
153 inline int fgGetInt (const string &name, int defaultValue = 0)
155 return globals->get_props()->getIntValue(name, defaultValue);
160 * Get a long value for a property.
162 * This method is convenient but inefficient. It should be used
163 * infrequently (i.e. for initializing, loading, saving, etc.),
164 * not in the main loop. If you need to get a value frequently,
165 * it is better to look up the node itself using fgGetNode and then
166 * use the node's getLongValue() method, to avoid the lookup overhead.
168 * @param name The property name.
169 * @param defaultValue The default value to return if the property
171 * @return The property's value as a long, or the default value provided.
173 inline int fgGetLong (const string &name, long defaultValue = 0L)
175 return globals->get_props()->getLongValue(name, defaultValue);
180 * Get a float value for a property.
182 * This method is convenient but inefficient. It should be used
183 * infrequently (i.e. for initializing, loading, saving, etc.),
184 * not in the main loop. If you need to get a value frequently,
185 * it is better to look up the node itself using fgGetNode and then
186 * use the node's getFloatValue() method, to avoid the lookup overhead.
188 * @param name The property name.
189 * @param defaultValue The default value to return if the property
191 * @return The property's value as a float, or the default value provided.
193 inline float fgGetFloat (const string &name, float defaultValue = 0.0)
195 return globals->get_props()->getFloatValue(name, defaultValue);
200 * Get a double value for a property.
202 * This method is convenient but inefficient. It should be used
203 * infrequently (i.e. for initializing, loading, saving, etc.),
204 * not in the main loop. If you need to get a value frequently,
205 * it is better to look up the node itself using fgGetNode and then
206 * use the node's getDoubleValue() method, to avoid the lookup overhead.
208 * @param name The property name.
209 * @param defaultValue The default value to return if the property
211 * @return The property's value as a double, or the default value provided.
213 inline double fgGetDouble (const string &name, double defaultValue = 0.0)
215 return globals->get_props()->getDoubleValue(name, defaultValue);
220 * Get a string value for a property.
222 * This method is convenient but inefficient. It should be used
223 * infrequently (i.e. for initializing, loading, saving, etc.),
224 * not in the main loop. If you need to get a value frequently,
225 * it is better to look up the node itself using fgGetNode and then
226 * use the node's getStringValue() method, to avoid the lookup overhead.
228 * @param name The property name.
229 * @param defaultValue The default value to return if the property
231 * @return The property's value as a string, or the default value provided.
233 inline string fgGetString (const string &name, string defaultValue = "")
235 return globals->get_props()->getStringValue(name, defaultValue);
240 * Set a bool value for a property.
242 * Assign a bool value to a property. If the property does not
243 * yet exist, it will be created and its type will be set to
244 * BOOL; if it has a type of UNKNOWN, the type will also be set to
245 * BOOL; otherwise, the bool value will be converted to the property's
248 * @param name The property name.
249 * @param val The new value for the property.
250 * @return true if the assignment succeeded, false otherwise.
252 inline bool fgSetBool (const string &name, bool val)
254 return globals->get_props()->setBoolValue(name, val);
259 * Set an int value for a property.
261 * Assign an int value to a property. If the property does not
262 * yet exist, it will be created and its type will be set to
263 * INT; if it has a type of UNKNOWN, the type will also be set to
264 * INT; otherwise, the bool value will be converted to the property's
267 * @param name The property name.
268 * @param val The new value for the property.
269 * @return true if the assignment succeeded, false otherwise.
271 inline bool fgSetInt (const string &name, int val)
273 return globals->get_props()->setIntValue(name, val);
278 * Set a long value for a property.
280 * Assign a long value to a property. If the property does not
281 * yet exist, it will be created and its type will be set to
282 * LONG; if it has a type of UNKNOWN, the type will also be set to
283 * LONG; otherwise, the bool value will be converted to the property's
286 * @param name The property name.
287 * @param val The new value for the property.
288 * @return true if the assignment succeeded, false otherwise.
290 inline bool fgSetLong (const string &name, long val)
292 return globals->get_props()->setLongValue(name, val);
297 * Set a float value for a property.
299 * Assign a float value to a property. If the property does not
300 * yet exist, it will be created and its type will be set to
301 * FLOAT; if it has a type of UNKNOWN, the type will also be set to
302 * FLOAT; otherwise, the bool value will be converted to the property's
305 * @param name The property name.
306 * @param val The new value for the property.
307 * @return true if the assignment succeeded, false otherwise.
309 inline bool fgSetFloat (const string &name, float val)
311 return globals->get_props()->setFloatValue(name, val);
316 * Set a double value for a property.
318 * Assign a double value to a property. If the property does not
319 * yet exist, it will be created and its type will be set to
320 * DOUBLE; if it has a type of UNKNOWN, the type will also be set to
321 * DOUBLE; otherwise, the double value will be converted to the property's
324 * @param name The property name.
325 * @param val The new value for the property.
326 * @return true if the assignment succeeded, false otherwise.
328 inline bool fgSetDouble (const string &name, double val)
330 return globals->get_props()->setDoubleValue(name, val);
335 * Set a string value for a property.
337 * Assign a string value to a property. If the property does not
338 * yet exist, it will be created and its type will be set to
339 * STRING; if it has a type of UNKNOWN, the type will also be set to
340 * STRING; otherwise, the string value will be converted to the property's
343 * @param name The property name.
344 * @param val The new value for the property.
345 * @return true if the assignment succeeded, false otherwise.
347 inline bool fgSetString (const string &name, const string &val)
349 return globals->get_props()->setStringValue(name, val);
354 ////////////////////////////////////////////////////////////////////////
355 // Convenience functions for setting property attributes.
356 ////////////////////////////////////////////////////////////////////////
360 * Set the state of the archive attribute for a property.
362 * If the archive attribute is true, the property will be written
363 * when a flight is saved; if it is false, the property will be
366 * A warning message will be printed if the property does not exist.
368 * @param name The property name.
369 * @param state The state of the archive attribute (defaults to true).
372 fgSetArchivable (const string &name, bool state = true)
374 SGPropertyNode * node = globals->get_props()->getNode(name);
376 SG_LOG(SG_GENERAL, SG_ALERT,
377 "Attempt to set archive flag for non-existant property "
380 node->setAttribute(SGPropertyNode::ARCHIVE, state);
385 * Set the state of the read attribute for a property.
387 * If the read attribute is true, the property value will be readable;
388 * if it is false, the property value will always be the default value
391 * A warning message will be printed if the property does not exist.
393 * @param name The property name.
394 * @param state The state of the read attribute (defaults to true).
397 fgSetReadable (const string &name, bool state = true)
399 SGPropertyNode * node = globals->get_props()->getNode(name);
401 SG_LOG(SG_GENERAL, SG_ALERT,
402 "Attempt to set read flag for non-existant property "
405 node->setAttribute(SGPropertyNode::READ, state);
410 * Set the state of the write attribute for a property.
412 * If the write attribute is true, the property value may be modified
413 * (depending on how it is tied); if the write attribute is false, the
414 * property value may not be modified.
416 * A warning message will be printed if the property does not exist.
418 * @param name The property name.
419 * @param state The state of the write attribute (defaults to true).
422 fgSetWritable (const string &name, bool state = true)
424 SGPropertyNode * node = globals->get_props()->getNode(name);
426 SG_LOG(SG_GENERAL, SG_ALERT,
427 "Attempt to set write flag for non-existant property "
430 node->setAttribute(SGPropertyNode::WRITE, state);
435 ////////////////////////////////////////////////////////////////////////
436 // Convenience functions for tying properties, with logging.
437 ////////////////////////////////////////////////////////////////////////
441 * Untie a property from an external data source.
443 * Classes should use this function to release control of any
444 * properties they are managing.
447 fgUntie (const string &name)
449 if (!globals->get_props()->untie(name))
450 SG_LOG(SG_GENERAL, SG_WARN, "Failed to untie property " << name);
454 // Templates cause ambiguity here
457 * Tie a property to an external bool variable.
459 * The property's value will automatically mirror the variable's
460 * value, and vice-versa, until the property is untied.
462 * @param name The property name to tie (full path).
463 * @param pointer A pointer to the variable.
464 * @param useDefault true if any existing property value should be
465 * copied to the variable; false if the variable should not
466 * be modified; defaults to true.
469 fgTie (const string &name, bool *pointer, bool useDefault = true)
471 if (!globals->get_props()->tie(name, SGRawValuePointer<bool>(pointer),
473 SG_LOG(SG_GENERAL, SG_WARN,
474 "Failed to tie property " << name << " to a pointer");
479 * Tie a property to an external int variable.
481 * The property's value will automatically mirror the variable's
482 * value, and vice-versa, until the property is untied.
484 * @param name The property name to tie (full path).
485 * @param pointer A pointer to the variable.
486 * @param useDefault true if any existing property value should be
487 * copied to the variable; false if the variable should not
488 * be modified; defaults to true.
491 fgTie (const string &name, int *pointer, bool useDefault = true)
493 if (!globals->get_props()->tie(name, SGRawValuePointer<int>(pointer),
495 SG_LOG(SG_GENERAL, SG_WARN,
496 "Failed to tie property " << name << " to a pointer");
501 * Tie a property to an external long variable.
503 * The property's value will automatically mirror the variable's
504 * value, and vice-versa, until the property is untied.
506 * @param name The property name to tie (full path).
507 * @param pointer A pointer to the variable.
508 * @param useDefault true if any existing property value should be
509 * copied to the variable; false if the variable should not
510 * be modified; defaults to true.
513 fgTie (const string &name, long *pointer, bool useDefault = true)
515 if (!globals->get_props()->tie(name, SGRawValuePointer<long>(pointer),
517 SG_LOG(SG_GENERAL, SG_WARN,
518 "Failed to tie property " << name << " to a pointer");
523 * Tie a property to an external float variable.
525 * The property's value will automatically mirror the variable's
526 * value, and vice-versa, until the property is untied.
528 * @param name The property name to tie (full path).
529 * @param pointer A pointer to the variable.
530 * @param useDefault true if any existing property value should be
531 * copied to the variable; false if the variable should not
532 * be modified; defaults to true.
535 fgTie (const string &name, float *pointer, bool useDefault = true)
537 if (!globals->get_props()->tie(name, SGRawValuePointer<float>(pointer),
539 SG_LOG(SG_GENERAL, SG_WARN,
540 "Failed to tie property " << name << " to a pointer");
545 * Tie a property to an external double variable.
547 * The property's value will automatically mirror the variable's
548 * value, and vice-versa, until the property is untied.
550 * @param name The property name to tie (full path).
551 * @param pointer A pointer to the variable.
552 * @param useDefault true if any existing property value should be
553 * copied to the variable; false if the variable should not
554 * be modified; defaults to true.
557 fgTie (const string &name, double *pointer, bool useDefault = true)
559 if (!globals->get_props()->tie(name, SGRawValuePointer<double>(pointer),
561 SG_LOG(SG_GENERAL, SG_WARN,
562 "Failed to tie property " << name << " to a pointer");
567 * Tie a property to an external string variable.
569 * The property's value will automatically mirror the variable's
570 * value, and vice-versa, until the property is untied.
572 * @param name The property name to tie (full path).
573 * @param pointer A pointer to the variable.
574 * @param useDefault true if any existing property value should be
575 * copied to the variable; false if the variable should not
576 * be modified; defaults to true.
579 fgTie (const string &name, string *pointer, bool useDefault = true)
581 if (!globals->get_props()->tie(name, SGRawValuePointer<string>(pointer),
583 SG_LOG(SG_GENERAL, SG_WARN,
584 "Failed to tie property " << name << " to a pointer");
589 * Tie a property to a pair of simple functions.
591 * Every time the property value is queried, the getter (if any) will
592 * be invoked; every time the property value is modified, the setter
593 * (if any) will be invoked. The getter can be 0 to make the property
594 * unreadable, and the setter can be 0 to make the property
597 * @param name The property name to tie (full path).
598 * @param getter The getter function, or 0 if the value is unreadable.
599 * @param setter The setter function, or 0 if the value is unmodifiable.
600 * @param useDefault true if the setter should be invoked with any existing
601 * property value should be; false if the old value should be
602 * discarded; defaults to true.
606 fgTie (const string &name, V (*getter)(), void (*setter)(V) = 0,
607 bool useDefault = true)
609 if (!globals->get_props()->tie(name, SGRawValueFunctions<V>(getter, setter),
611 SG_LOG(SG_GENERAL, SG_WARN,
612 "Failed to tie property " << name << " to functions");
617 * Tie a property to a pair of indexed functions.
619 * Every time the property value is queried, the getter (if any) will
620 * be invoked with the index provided; every time the property value
621 * is modified, the setter (if any) will be invoked with the index
622 * provided. The getter can be 0 to make the property unreadable, and
623 * the setter can be 0 to make the property unmodifiable.
625 * @param name The property name to tie (full path).
626 * @param index The integer argument to pass to the getter and
628 * @param getter The getter function, or 0 if the value is unreadable.
629 * @param setter The setter function, or 0 if the value is unmodifiable.
630 * @param useDefault true if the setter should be invoked with any existing
631 * property value should be; false if the old value should be
632 * discarded; defaults to true.
636 fgTie (const string &name, int index, V (*getter)(int),
637 void (*setter)(int, V) = 0, bool useDefault = true)
639 if (!globals->get_props()->tie(name,
640 SGRawValueFunctionsIndexed<V>(index,
644 SG_LOG(SG_GENERAL, SG_WARN,
645 "Failed to tie property " << name << " to indexed functions");
650 * Tie a property to a pair of object methods.
652 * Every time the property value is queried, the getter (if any) will
653 * be invoked; every time the property value is modified, the setter
654 * (if any) will be invoked. The getter can be 0 to make the property
655 * unreadable, and the setter can be 0 to make the property
658 * @param name The property name to tie (full path).
659 * @param obj The object whose methods should be invoked.
660 * @param getter The object's getter method, or 0 if the value is
662 * @param setter The object's setter method, or 0 if the value is
664 * @param useDefault true if the setter should be invoked with any existing
665 * property value should be; false if the old value should be
666 * discarded; defaults to true.
668 template <class T, class V>
670 fgTie (const string &name, T * obj, V (T::*getter)() const,
671 void (T::*setter)(V) = 0, bool useDefault = true)
673 if (!globals->get_props()->tie(name,
674 SGRawValueMethods<T,V>(*obj, getter, setter),
676 SG_LOG(SG_GENERAL, SG_WARN,
677 "Failed to tie property " << name << " to object methods");
682 * Tie a property to a pair of indexed object methods.
684 * Every time the property value is queried, the getter (if any) will
685 * be invoked with the index provided; every time the property value
686 * is modified, the setter (if any) will be invoked with the index
687 * provided. The getter can be 0 to make the property unreadable, and
688 * the setter can be 0 to make the property unmodifiable.
690 * @param name The property name to tie (full path).
691 * @param obj The object whose methods should be invoked.
692 * @param index The integer argument to pass to the getter and
694 * @param getter The getter method, or 0 if the value is unreadable.
695 * @param setter The setter method, or 0 if the value is unmodifiable.
696 * @param useDefault true if the setter should be invoked with any existing
697 * property value should be; false if the old value should be
698 * discarded; defaults to true.
700 template <class T, class V>
702 fgTie (const string &name, T * obj, int index,
703 V (T::*getter)(int) const, void (T::*setter)(int, V) = 0,
704 bool useDefault = true)
706 if (!globals->get_props()->tie(name,
707 SGRawValueMethodsIndexed<T,V>(*obj,
712 SG_LOG(SG_GENERAL, SG_WARN,
713 "Failed to tie property " << name << " to indexed object methods");
718 ////////////////////////////////////////////////////////////////////////
720 ////////////////////////////////////////////////////////////////////////
724 * An encoded condition.
726 * This class encodes a single condition of some sort, possibly
727 * connected with properties.
729 * This class should migrate to somewhere more general.
735 virtual ~FGCondition ();
736 virtual bool test () const = 0;
741 * Condition for a single property.
743 * This condition is true only if the property returns a boolean
746 class FGPropertyCondition : public FGCondition
749 FGPropertyCondition (const string &propname);
750 virtual ~FGPropertyCondition ();
751 virtual bool test () const { return _node->getBoolValue(); }
753 const SGPropertyNode * _node;
758 * Condition for a 'not' operator.
760 * This condition is true only if the child condition is false.
762 class FGNotCondition : public FGCondition
765 // transfer pointer ownership
766 FGNotCondition (FGCondition * condition);
767 virtual ~FGNotCondition ();
768 virtual bool test () const;
770 FGCondition * _condition;
775 * Condition for an 'and' group.
777 * This condition is true only if all of the conditions
778 * in the group are true.
780 class FGAndCondition : public FGCondition
784 virtual ~FGAndCondition ();
785 virtual bool test () const;
786 // transfer pointer ownership
787 virtual void addCondition (FGCondition * condition);
789 vector<FGCondition *> _conditions;
794 * Condition for an 'or' group.
796 * This condition is true if at least one of the conditions in the
799 class FGOrCondition : public FGCondition
803 virtual ~FGOrCondition ();
804 virtual bool test () const;
805 // transfer pointer ownership
806 virtual void addCondition (FGCondition * condition);
808 vector<FGCondition *> _conditions;
813 * Abstract base class for property comparison conditions.
815 class FGComparisonCondition : public FGCondition
823 FGComparisonCondition (Type type, bool reverse = false);
824 virtual ~FGComparisonCondition ();
825 virtual bool test () const;
826 virtual void setLeftProperty (const string &propname);
827 virtual void setRightProperty (const string &propname);
828 // will make a local copy
829 virtual void setRightValue (const SGPropertyNode * value);
833 const SGPropertyNode * _left_property;
834 const SGPropertyNode * _right_property;
835 const SGPropertyNode * _right_value;
840 * Base class for a conditional components.
842 * This class manages the conditions and tests; the component should
843 * invoke the test() method whenever it needs to decide whether to
844 * active itself, draw itself, and so on.
850 virtual ~FGConditional ();
851 // transfer pointer ownership
852 virtual void setCondition (FGCondition * condition);
853 virtual const FGCondition * getCondition () const { return _condition; }
854 virtual bool test () const;
856 FGCondition * _condition;
861 * Global function to make a condition out of properties.
863 * The top-level is always an implicit 'and' group, whatever the
864 * node's name (it should usually be "condition").
866 * @param node The top-level condition node (usually named "condition").
867 * @return A pointer to a newly-allocated condition; it is the
868 * responsibility of the caller to delete the condition when
869 * it is no longer needed.
871 FGCondition * fgReadCondition (const SGPropertyNode * node);
874 #endif // __FG_PROPS_HXX