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 * @return true if the flight was saved successfully.
48 extern bool fgSaveFlight (ostream &output);
52 * Load a flight from disk.
54 * This function loads an XML save file from a stream to restore
57 * @param input The input stream to read the XML from.
58 * @return true if the flight was restored successfully.
60 extern bool fgLoadFlight (istream &input);
64 ////////////////////////////////////////////////////////////////////////
65 // Convenience functions for getting property values.
66 ////////////////////////////////////////////////////////////////////////
70 * Get a property node.
72 * @param path The path of the node, relative to root.
73 * @param create true to create the node if it doesn't exist.
74 * @return The node, or 0 if none exists and none was created.
76 inline SGPropertyNode *
77 fgGetNode (const string &path, bool create = false)
79 return globals->get_props()->getNode(path, create);
84 * Test whether a given node exists.
86 * @param path The path of the node, relative to root.
87 * @return true if the node exists, false otherwise.
90 fgHasNode (const string &path)
92 return (fgGetNode(path, false) != 0);
97 * Get a bool value for a property.
99 * This method is convenient but inefficient. It should be used
100 * infrequently (i.e. for initializing, loading, saving, etc.),
101 * not in the main loop. If you need to get a value frequently,
102 * it is better to look up the node itself using fgGetNode and then
103 * use the node's getBoolValue() method, to avoid the lookup overhead.
105 * @param name The property name.
106 * @param defaultValue The default value to return if the property
108 * @return The property's value as a bool, or the default value provided.
110 inline bool fgGetBool (const string &name, bool defaultValue = false)
112 return globals->get_props()->getBoolValue(name, defaultValue);
117 * Get an int value for a property.
119 * This method is convenient but inefficient. It should be used
120 * infrequently (i.e. for initializing, loading, saving, etc.),
121 * not in the main loop. If you need to get a value frequently,
122 * it is better to look up the node itself using fgGetNode and then
123 * use the node's getIntValue() method, to avoid the lookup overhead.
125 * @param name The property name.
126 * @param defaultValue The default value to return if the property
128 * @return The property's value as an int, or the default value provided.
130 inline int fgGetInt (const string &name, int defaultValue = 0)
132 return globals->get_props()->getIntValue(name, defaultValue);
137 * Get a long value for a property.
139 * This method is convenient but inefficient. It should be used
140 * infrequently (i.e. for initializing, loading, saving, etc.),
141 * not in the main loop. If you need to get a value frequently,
142 * it is better to look up the node itself using fgGetNode and then
143 * use the node's getLongValue() method, to avoid the lookup overhead.
145 * @param name The property name.
146 * @param defaultValue The default value to return if the property
148 * @return The property's value as a long, or the default value provided.
150 inline int fgGetLong (const string &name, long defaultValue = 0L)
152 return globals->get_props()->getLongValue(name, defaultValue);
157 * Get a float value for a property.
159 * This method is convenient but inefficient. It should be used
160 * infrequently (i.e. for initializing, loading, saving, etc.),
161 * not in the main loop. If you need to get a value frequently,
162 * it is better to look up the node itself using fgGetNode and then
163 * use the node's getFloatValue() method, to avoid the lookup overhead.
165 * @param name The property name.
166 * @param defaultValue The default value to return if the property
168 * @return The property's value as a float, or the default value provided.
170 inline float fgGetFloat (const string &name, float defaultValue = 0.0)
172 return globals->get_props()->getFloatValue(name, defaultValue);
177 * Get a double value for a property.
179 * This method is convenient but inefficient. It should be used
180 * infrequently (i.e. for initializing, loading, saving, etc.),
181 * not in the main loop. If you need to get a value frequently,
182 * it is better to look up the node itself using fgGetNode and then
183 * use the node's getDoubleValue() method, to avoid the lookup overhead.
185 * @param name The property name.
186 * @param defaultValue The default value to return if the property
188 * @return The property's value as a double, or the default value provided.
190 inline double fgGetDouble (const string &name, double defaultValue = 0.0)
192 return globals->get_props()->getDoubleValue(name, defaultValue);
197 * Get a string value for a property.
199 * This method is convenient but inefficient. It should be used
200 * infrequently (i.e. for initializing, loading, saving, etc.),
201 * not in the main loop. If you need to get a value frequently,
202 * it is better to look up the node itself using fgGetNode and then
203 * use the node's getStringValue() method, to avoid the lookup overhead.
205 * @param name The property name.
206 * @param defaultValue The default value to return if the property
208 * @return The property's value as a string, or the default value provided.
210 inline string fgGetString (const string &name, string defaultValue = "")
212 return globals->get_props()->getStringValue(name, defaultValue);
217 * Set a bool value for a property.
219 * Assign a bool value to a property. If the property does not
220 * yet exist, it will be created and its type will be set to
221 * BOOL; if it has a type of UNKNOWN, the type will also be set to
222 * BOOL; otherwise, the bool value will be converted to the property's
225 * @param name The property name.
226 * @param val The new value for the property.
227 * @return true if the assignment succeeded, false otherwise.
229 inline bool fgSetBool (const string &name, bool val)
231 return globals->get_props()->setBoolValue(name, val);
236 * Set an int value for a property.
238 * Assign an int value to a property. If the property does not
239 * yet exist, it will be created and its type will be set to
240 * INT; if it has a type of UNKNOWN, the type will also be set to
241 * INT; otherwise, the bool value will be converted to the property's
244 * @param name The property name.
245 * @param val The new value for the property.
246 * @return true if the assignment succeeded, false otherwise.
248 inline bool fgSetInt (const string &name, int val)
250 return globals->get_props()->setIntValue(name, val);
255 * Set a long value for a property.
257 * Assign a long value to a property. If the property does not
258 * yet exist, it will be created and its type will be set to
259 * LONG; if it has a type of UNKNOWN, the type will also be set to
260 * LONG; otherwise, the bool value will be converted to the property's
263 * @param name The property name.
264 * @param val The new value for the property.
265 * @return true if the assignment succeeded, false otherwise.
267 inline bool fgSetLong (const string &name, long val)
269 return globals->get_props()->setLongValue(name, val);
274 * Set a float value for a property.
276 * Assign a float value to a property. If the property does not
277 * yet exist, it will be created and its type will be set to
278 * FLOAT; if it has a type of UNKNOWN, the type will also be set to
279 * FLOAT; otherwise, the bool value will be converted to the property's
282 * @param name The property name.
283 * @param val The new value for the property.
284 * @return true if the assignment succeeded, false otherwise.
286 inline bool fgSetFloat (const string &name, float val)
288 return globals->get_props()->setFloatValue(name, val);
293 * Set a double value for a property.
295 * Assign a double value to a property. If the property does not
296 * yet exist, it will be created and its type will be set to
297 * DOUBLE; if it has a type of UNKNOWN, the type will also be set to
298 * DOUBLE; otherwise, the double value will be converted to the property's
301 * @param name The property name.
302 * @param val The new value for the property.
303 * @return true if the assignment succeeded, false otherwise.
305 inline bool fgSetDouble (const string &name, double val)
307 return globals->get_props()->setDoubleValue(name, val);
312 * Set a string value for a property.
314 * Assign a string value to a property. If the property does not
315 * yet exist, it will be created and its type will be set to
316 * STRING; if it has a type of UNKNOWN, the type will also be set to
317 * STRING; otherwise, the string value will be converted to the property's
320 * @param name The property name.
321 * @param val The new value for the property.
322 * @return true if the assignment succeeded, false otherwise.
324 inline bool fgSetString (const string &name, const string &val)
326 return globals->get_props()->setStringValue(name, val);
331 ////////////////////////////////////////////////////////////////////////
332 // Convenience functions for setting property attributes.
333 ////////////////////////////////////////////////////////////////////////
337 * Set the state of the archive attribute for a property.
339 * If the archive attribute is true, the property will be written
340 * when a flight is saved; if it is false, the property will be
343 * A warning message will be printed if the property does not exist.
345 * @param name The property name.
346 * @param state The state of the archive attribute (defaults to true).
349 fgSetArchivable (const string &name, bool state = true)
351 SGPropertyNode * node = globals->get_props()->getNode(name);
353 SG_LOG(SG_GENERAL, SG_ALERT,
354 "Attempt to set archive flag for non-existant property "
357 node->setAttribute(SGPropertyNode::ARCHIVE, state);
362 * Set the state of the read attribute for a property.
364 * If the read attribute is true, the property value will be readable;
365 * if it is false, the property value will always be the default value
368 * A warning message will be printed if the property does not exist.
370 * @param name The property name.
371 * @param state The state of the read attribute (defaults to true).
374 fgSetReadable (const string &name, bool state = true)
376 SGPropertyNode * node = globals->get_props()->getNode(name);
378 SG_LOG(SG_GENERAL, SG_ALERT,
379 "Attempt to set read flag for non-existant property "
382 node->setAttribute(SGPropertyNode::READ, state);
387 * Set the state of the write attribute for a property.
389 * If the write attribute is true, the property value may be modified
390 * (depending on how it is tied); if the write attribute is false, the
391 * property value may not be modified.
393 * A warning message will be printed if the property does not exist.
395 * @param name The property name.
396 * @param state The state of the write attribute (defaults to true).
399 fgSetWritable (const string &name, bool state = true)
401 SGPropertyNode * node = globals->get_props()->getNode(name);
403 SG_LOG(SG_GENERAL, SG_ALERT,
404 "Attempt to set write flag for non-existant property "
407 node->setAttribute(SGPropertyNode::WRITE, state);
412 ////////////////////////////////////////////////////////////////////////
413 // Convenience functions for tying properties, with logging.
414 ////////////////////////////////////////////////////////////////////////
418 * Untie a property from an external data source.
420 * Classes should use this function to release control of any
421 * properties they are managing.
424 fgUntie (const string &name)
426 if (!globals->get_props()->untie(name))
427 SG_LOG(SG_GENERAL, SG_WARN, "Failed to untie property " << name);
431 // Templates cause ambiguity here
434 * Tie a property to an external bool variable.
436 * The property's value will automatically mirror the variable's
437 * value, and vice-versa, until the property is untied.
439 * @param name The property name to tie (full path).
440 * @param pointer A pointer to the variable.
441 * @param useDefault true if any existing property value should be
442 * copied to the variable; false if the variable should not
443 * be modified; defaults to true.
446 fgTie (const string &name, bool *pointer, bool useDefault = true)
448 if (!globals->get_props()->tie(name, SGRawValuePointer<bool>(pointer),
450 SG_LOG(SG_GENERAL, SG_WARN,
451 "Failed to tie property " << name << " to a pointer");
456 * Tie a property to an external int variable.
458 * The property's value will automatically mirror the variable's
459 * value, and vice-versa, until the property is untied.
461 * @param name The property name to tie (full path).
462 * @param pointer A pointer to the variable.
463 * @param useDefault true if any existing property value should be
464 * copied to the variable; false if the variable should not
465 * be modified; defaults to true.
468 fgTie (const string &name, int *pointer, bool useDefault = true)
470 if (!globals->get_props()->tie(name, SGRawValuePointer<int>(pointer),
472 SG_LOG(SG_GENERAL, SG_WARN,
473 "Failed to tie property " << name << " to a pointer");
478 * Tie a property to an external long variable.
480 * The property's value will automatically mirror the variable's
481 * value, and vice-versa, until the property is untied.
483 * @param name The property name to tie (full path).
484 * @param pointer A pointer to the variable.
485 * @param useDefault true if any existing property value should be
486 * copied to the variable; false if the variable should not
487 * be modified; defaults to true.
490 fgTie (const string &name, long *pointer, bool useDefault = true)
492 if (!globals->get_props()->tie(name, SGRawValuePointer<long>(pointer),
494 SG_LOG(SG_GENERAL, SG_WARN,
495 "Failed to tie property " << name << " to a pointer");
500 * Tie a property to an external float variable.
502 * The property's value will automatically mirror the variable's
503 * value, and vice-versa, until the property is untied.
505 * @param name The property name to tie (full path).
506 * @param pointer A pointer to the variable.
507 * @param useDefault true if any existing property value should be
508 * copied to the variable; false if the variable should not
509 * be modified; defaults to true.
512 fgTie (const string &name, float *pointer, bool useDefault = true)
514 if (!globals->get_props()->tie(name, SGRawValuePointer<float>(pointer),
516 SG_LOG(SG_GENERAL, SG_WARN,
517 "Failed to tie property " << name << " to a pointer");
522 * Tie a property to an external double variable.
524 * The property's value will automatically mirror the variable's
525 * value, and vice-versa, until the property is untied.
527 * @param name The property name to tie (full path).
528 * @param pointer A pointer to the variable.
529 * @param useDefault true if any existing property value should be
530 * copied to the variable; false if the variable should not
531 * be modified; defaults to true.
534 fgTie (const string &name, double *pointer, bool useDefault = true)
536 if (!globals->get_props()->tie(name, SGRawValuePointer<double>(pointer),
538 SG_LOG(SG_GENERAL, SG_WARN,
539 "Failed to tie property " << name << " to a pointer");
544 * Tie a property to an external string variable.
546 * The property's value will automatically mirror the variable's
547 * value, and vice-versa, until the property is untied.
549 * @param name The property name to tie (full path).
550 * @param pointer A pointer to the variable.
551 * @param useDefault true if any existing property value should be
552 * copied to the variable; false if the variable should not
553 * be modified; defaults to true.
556 fgTie (const string &name, string *pointer, bool useDefault = true)
558 if (!globals->get_props()->tie(name, SGRawValuePointer<string>(pointer),
560 SG_LOG(SG_GENERAL, SG_WARN,
561 "Failed to tie property " << name << " to a pointer");
566 * Tie a property to a pair of simple functions.
568 * Every time the property value is queried, the getter (if any) will
569 * be invoked; every time the property value is modified, the setter
570 * (if any) will be invoked. The getter can be 0 to make the property
571 * unreadable, and the setter can be 0 to make the property
574 * @param name The property name to tie (full path).
575 * @param getter The getter function, or 0 if the value is unreadable.
576 * @param setter The setter function, or 0 if the value is unmodifiable.
577 * @param useDefault true if the setter should be invoked with any existing
578 * property value should be; false if the old value should be
579 * discarded; defaults to true.
583 fgTie (const string &name, V (*getter)(), void (*setter)(V) = 0,
584 bool useDefault = true)
586 if (!globals->get_props()->tie(name, SGRawValueFunctions<V>(getter, setter),
588 SG_LOG(SG_GENERAL, SG_WARN,
589 "Failed to tie property " << name << " to functions");
594 * Tie a property to a pair of indexed functions.
596 * Every time the property value is queried, the getter (if any) will
597 * be invoked with the index provided; every time the property value
598 * is modified, the setter (if any) will be invoked with the index
599 * provided. The getter can be 0 to make the property unreadable, and
600 * the setter can be 0 to make the property unmodifiable.
602 * @param name The property name to tie (full path).
603 * @param index The integer argument to pass to the getter and
605 * @param getter The getter function, or 0 if the value is unreadable.
606 * @param setter The setter function, or 0 if the value is unmodifiable.
607 * @param useDefault true if the setter should be invoked with any existing
608 * property value should be; false if the old value should be
609 * discarded; defaults to true.
613 fgTie (const string &name, int index, V (*getter)(int),
614 void (*setter)(int, V) = 0, bool useDefault = true)
616 if (!globals->get_props()->tie(name,
617 SGRawValueFunctionsIndexed<V>(index,
621 SG_LOG(SG_GENERAL, SG_WARN,
622 "Failed to tie property " << name << " to indexed functions");
627 * Tie a property to a pair of object methods.
629 * Every time the property value is queried, the getter (if any) will
630 * be invoked; every time the property value is modified, the setter
631 * (if any) will be invoked. The getter can be 0 to make the property
632 * unreadable, and the setter can be 0 to make the property
635 * @param name The property name to tie (full path).
636 * @param obj The object whose methods should be invoked.
637 * @param getter The object's getter method, or 0 if the value is
639 * @param setter The object's setter method, or 0 if the value is
641 * @param useDefault true if the setter should be invoked with any existing
642 * property value should be; false if the old value should be
643 * discarded; defaults to true.
645 template <class T, class V>
647 fgTie (const string &name, T * obj, V (T::*getter)() const,
648 void (T::*setter)(V) = 0, bool useDefault = true)
650 if (!globals->get_props()->tie(name,
651 SGRawValueMethods<T,V>(*obj, getter, setter),
653 SG_LOG(SG_GENERAL, SG_WARN,
654 "Failed to tie property " << name << " to object methods");
659 * Tie a property to a pair of indexed object methods.
661 * Every time the property value is queried, the getter (if any) will
662 * be invoked with the index provided; every time the property value
663 * is modified, the setter (if any) will be invoked with the index
664 * provided. The getter can be 0 to make the property unreadable, and
665 * the setter can be 0 to make the property unmodifiable.
667 * @param name The property name to tie (full path).
668 * @param obj The object whose methods should be invoked.
669 * @param index The integer argument to pass to the getter and
671 * @param getter The getter method, or 0 if the value is unreadable.
672 * @param setter The setter method, or 0 if the value is unmodifiable.
673 * @param useDefault true if the setter should be invoked with any existing
674 * property value should be; false if the old value should be
675 * discarded; defaults to true.
677 template <class T, class V>
679 fgTie (const string &name, T * obj, int index,
680 V (T::*getter)(int) const, void (T::*setter)(int, V) = 0,
681 bool useDefault = true)
683 if (!globals->get_props()->tie(name,
684 SGRawValueMethodsIndexed<T,V>(*obj,
689 SG_LOG(SG_GENERAL, SG_WARN,
690 "Failed to tie property " << name << " to indexed object methods");
695 ////////////////////////////////////////////////////////////////////////
697 ////////////////////////////////////////////////////////////////////////
701 * An encoded condition.
703 * This class encodes a single condition of some sort, possibly
704 * connected with properties.
706 * This class should migrate to somewhere more general.
712 virtual ~FGCondition ();
713 virtual bool test () const = 0;
718 * Condition for a single property.
720 * This condition is true only if the property returns a boolean
723 class FGPropertyCondition : public FGCondition
726 FGPropertyCondition (const string &propname);
727 virtual ~FGPropertyCondition ();
728 virtual bool test () const { return _node->getBoolValue(); }
730 const SGPropertyNode * _node;
735 * Condition for a 'not' operator.
737 * This condition is true only if the child condition is false.
739 class FGNotCondition : public FGCondition
742 // transfer pointer ownership
743 FGNotCondition (FGCondition * condition);
744 virtual ~FGNotCondition ();
745 virtual bool test () const;
747 FGCondition * _condition;
752 * Condition for an 'and' group.
754 * This condition is true only if all of the conditions
755 * in the group are true.
757 class FGAndCondition : public FGCondition
761 virtual ~FGAndCondition ();
762 virtual bool test () const;
763 // transfer pointer ownership
764 virtual void addCondition (FGCondition * condition);
766 vector<FGCondition *> _conditions;
771 * Condition for an 'or' group.
773 * This condition is true if at least one of the conditions in the
776 class FGOrCondition : public FGCondition
780 virtual ~FGOrCondition ();
781 virtual bool test () const;
782 // transfer pointer ownership
783 virtual void addCondition (FGCondition * condition);
785 vector<FGCondition *> _conditions;
790 * Abstract base class for property comparison conditions.
792 class FGComparisonCondition : public FGCondition
800 FGComparisonCondition (Type type, bool reverse = false);
801 virtual ~FGComparisonCondition ();
802 virtual bool test () const;
803 virtual void setLeftProperty (const string &propname);
804 virtual void setRightProperty (const string &propname);
805 // will make a local copy
806 virtual void setRightValue (const SGPropertyNode * value);
810 const SGPropertyNode * _left_property;
811 const SGPropertyNode * _right_property;
812 const SGPropertyNode * _right_value;
817 * Base class for a conditional components.
819 * This class manages the conditions and tests; the component should
820 * invoke the test() method whenever it needs to decide whether to
821 * active itself, draw itself, and so on.
827 virtual ~FGConditional ();
828 // transfer pointer ownership
829 virtual void setCondition (FGCondition * condition);
830 virtual const FGCondition * getCondition () const { return _condition; }
831 virtual bool test () const;
833 FGCondition * _condition;
838 * Global function to make a condition out of properties.
840 * The top-level is always an implicit 'and' group, whatever the
841 * node's name (it should usually be "condition").
843 * @param node The top-level condition node (usually named "condition").
844 * @return A pointer to a newly-allocated condition; it is the
845 * responsibility of the caller to delete the condition when
846 * it is no longer needed.
848 FGCondition * fgReadCondition (const SGPropertyNode * node);
851 #endif // __FG_PROPS_HXX