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 <simgear/misc/props_io.hxx>
13 #include "globals.hxx"
16 ////////////////////////////////////////////////////////////////////////
17 // Property management.
18 ////////////////////////////////////////////////////////////////////////
22 * Initialize the default FlightGear properties.
24 * These are mostly properties that haven't been claimed by a
25 * specific module yet. This function should be invoked once,
26 * while the program is starting (and after the global property
27 * tree has been created).
29 extern void fgInitProps (); // fixme: how are they untied?
33 * Update the default FlightGear properties.
35 * This function should be invoked once in each loop to update all
36 * of the default properties.
38 extern void fgUpdateProps ();
42 * Save a flight to disk.
44 * This function saves all of the archivable properties to a stream
45 * so that the current flight can be restored later.
47 * @param output The output stream to write the XML save file to.
48 * @param write_all If true, write all properties rather than
49 * just the ones flagged as archivable.
50 * @return true if the flight was saved successfully.
52 extern bool fgSaveFlight (ostream &output, bool write_all = false);
56 * Load a flight from disk.
58 * This function loads an XML save file from a stream to restore
61 * @param input The input stream to read the XML from.
62 * @return true if the flight was restored successfully.
64 extern bool fgLoadFlight (istream &input);
68 ////////////////////////////////////////////////////////////////////////
69 // Convenience functions for getting property values.
70 ////////////////////////////////////////////////////////////////////////
73 * Get a property node.
75 * @param path The path of the node, relative to root.
76 * @param create true to create the node if it doesn't exist.
77 * @return The node, or 0 if none exists and none was created.
79 extern SGPropertyNode * fgGetNode (const char * path, bool create = false);
83 * Get a property node with separate index.
85 * This method separates the index from the path string, to make it
86 * easier to iterate through multiple components without using sprintf
87 * to add indices. For example, fgGetNode("foo[1]/bar", 3) will
88 * return the same result as fgGetNode("foo[1]/bar[3]").
90 * @param path The path of the node, relative to root.
91 * @param index The index for the last member of the path (overrides
92 * any given in the string).
93 * @param create true to create the node if it doesn't exist.
94 * @return The node, or 0 if none exists and none was created.
96 extern SGPropertyNode * fgGetNode (const char * path,
97 int index, bool create = false);
101 * Test whether a given node exists.
103 * @param path The path of the node, relative to root.
104 * @return true if the node exists, false otherwise.
106 extern bool fgHasNode (const char * path);
110 * Get a bool value for a property.
112 * This method is convenient but inefficient. It should be used
113 * infrequently (i.e. for initializing, loading, saving, etc.),
114 * not in the main loop. If you need to get a value frequently,
115 * it is better to look up the node itself using fgGetNode and then
116 * use the node's getBoolValue() method, to avoid the lookup overhead.
118 * @param name The property name.
119 * @param defaultValue The default value to return if the property
121 * @return The property's value as a bool, or the default value provided.
123 extern bool fgGetBool (const char * name, bool defaultValue = false);
127 * Get an int value for a property.
129 * This method is convenient but inefficient. It should be used
130 * infrequently (i.e. for initializing, loading, saving, etc.),
131 * not in the main loop. If you need to get a value frequently,
132 * it is better to look up the node itself using fgGetNode and then
133 * use the node's getIntValue() method, to avoid the lookup overhead.
135 * @param name The property name.
136 * @param defaultValue The default value to return if the property
138 * @return The property's value as an int, or the default value provided.
140 extern int fgGetInt (const char * name, int defaultValue = 0);
144 * Get a long value for a property.
146 * This method is convenient but inefficient. It should be used
147 * infrequently (i.e. for initializing, loading, saving, etc.),
148 * not in the main loop. If you need to get a value frequently,
149 * it is better to look up the node itself using fgGetNode and then
150 * use the node's getLongValue() method, to avoid the lookup overhead.
152 * @param name The property name.
153 * @param defaultValue The default value to return if the property
155 * @return The property's value as a long, or the default value provided.
157 extern int fgGetLong (const char * name, long defaultValue = 0L);
161 * Get a float value for a property.
163 * This method is convenient but inefficient. It should be used
164 * infrequently (i.e. for initializing, loading, saving, etc.),
165 * not in the main loop. If you need to get a value frequently,
166 * it is better to look up the node itself using fgGetNode and then
167 * use the node's getFloatValue() method, to avoid the lookup overhead.
169 * @param name The property name.
170 * @param defaultValue The default value to return if the property
172 * @return The property's value as a float, or the default value provided.
174 extern float fgGetFloat (const char * name, float defaultValue = 0.0);
178 * Get a double value for a property.
180 * This method is convenient but inefficient. It should be used
181 * infrequently (i.e. for initializing, loading, saving, etc.),
182 * not in the main loop. If you need to get a value frequently,
183 * it is better to look up the node itself using fgGetNode and then
184 * use the node's getDoubleValue() method, to avoid the lookup overhead.
186 * @param name The property name.
187 * @param defaultValue The default value to return if the property
189 * @return The property's value as a double, or the default value provided.
191 extern double fgGetDouble (const char * name, double defaultValue = 0.0);
195 * Get a string value for a property.
197 * This method is convenient but inefficient. It should be used
198 * infrequently (i.e. for initializing, loading, saving, etc.),
199 * not in the main loop. If you need to get a value frequently,
200 * it is better to look up the node itself using fgGetNode and then
201 * use the node's getStringValue() method, to avoid the lookup overhead.
203 * @param name The property name.
204 * @param defaultValue The default value to return if the property
206 * @return The property's value as a string, or the default value provided.
208 extern const char * fgGetString (const char * name,
209 const char * defaultValue = "");
213 * Set a bool value for a property.
215 * Assign a bool value to a property. If the property does not
216 * yet exist, it will be created and its type will be set to
217 * BOOL; if it has a type of UNKNOWN, the type will also be set to
218 * BOOL; otherwise, the bool value will be converted to the property's
221 * @param name The property name.
222 * @param val The new value for the property.
223 * @return true if the assignment succeeded, false otherwise.
225 extern bool fgSetBool (const char * name, bool val);
229 * Set an int value for a property.
231 * Assign an int value to a property. If the property does not
232 * yet exist, it will be created and its type will be set to
233 * INT; if it has a type of UNKNOWN, the type will also be set to
234 * INT; otherwise, the bool value will be converted to the property's
237 * @param name The property name.
238 * @param val The new value for the property.
239 * @return true if the assignment succeeded, false otherwise.
241 extern bool fgSetInt (const char * name, int val);
245 * Set a long value for a property.
247 * Assign a long value to a property. If the property does not
248 * yet exist, it will be created and its type will be set to
249 * LONG; if it has a type of UNKNOWN, the type will also be set to
250 * LONG; otherwise, the bool value will be converted to the property's
253 * @param name The property name.
254 * @param val The new value for the property.
255 * @return true if the assignment succeeded, false otherwise.
257 extern bool fgSetLong (const char * name, long val);
261 * Set a float value for a property.
263 * Assign a float value to a property. If the property does not
264 * yet exist, it will be created and its type will be set to
265 * FLOAT; if it has a type of UNKNOWN, the type will also be set to
266 * FLOAT; otherwise, the bool value will be converted to the property's
269 * @param name The property name.
270 * @param val The new value for the property.
271 * @return true if the assignment succeeded, false otherwise.
273 extern bool fgSetFloat (const char * name, float val);
277 * Set a double value for a property.
279 * Assign a double value to a property. If the property does not
280 * yet exist, it will be created and its type will be set to
281 * DOUBLE; if it has a type of UNKNOWN, the type will also be set to
282 * DOUBLE; otherwise, the double value will be converted to the property's
285 * @param name The property name.
286 * @param val The new value for the property.
287 * @return true if the assignment succeeded, false otherwise.
289 extern bool fgSetDouble (const char * name, double val);
293 * Set a string value for a property.
295 * Assign a string value to a property. If the property does not
296 * yet exist, it will be created and its type will be set to
297 * STRING; if it has a type of UNKNOWN, the type will also be set to
298 * STRING; otherwise, the string 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 extern bool fgSetString (const char * name, const char * val);
309 ////////////////////////////////////////////////////////////////////////
310 // Convenience functions for setting property attributes.
311 ////////////////////////////////////////////////////////////////////////
315 * Set the state of the archive attribute for a property.
317 * If the archive attribute is true, the property will be written
318 * when a flight is saved; if it is false, the property will be
321 * A warning message will be printed if the property does not exist.
323 * @param name The property name.
324 * @param state The state of the archive attribute (defaults to true).
326 extern void fgSetArchivable (const char * name, bool state = true);
330 * Set the state of the read attribute for a property.
332 * If the read attribute is true, the property value will be readable;
333 * if it is false, the property value will always be the default value
336 * A warning message will be printed if the property does not exist.
338 * @param name The property name.
339 * @param state The state of the read attribute (defaults to true).
341 extern void fgSetReadable (const char * name, bool state = true);
345 * Set the state of the write attribute for a property.
347 * If the write attribute is true, the property value may be modified
348 * (depending on how it is tied); if the write attribute is false, the
349 * property value may not be modified.
351 * A warning message will be printed if the property does not exist.
353 * @param name The property name.
354 * @param state The state of the write attribute (defaults to true).
356 extern void fgSetWritable (const char * name, bool state = true);
360 ////////////////////////////////////////////////////////////////////////
361 // Convenience functions for tying properties, with logging.
362 ////////////////////////////////////////////////////////////////////////
366 * Untie a property from an external data source.
368 * Classes should use this function to release control of any
369 * properties they are managing.
371 extern void fgUntie (const char * name);
375 * Tie a property to a pair of simple functions.
377 * Every time the property value is queried, the getter (if any) will
378 * be invoked; every time the property value is modified, the setter
379 * (if any) will be invoked. The getter can be 0 to make the property
380 * unreadable, and the setter can be 0 to make the property
383 * @param name The property name to tie (full path).
384 * @param getter The getter function, or 0 if the value is unreadable.
385 * @param setter The setter function, or 0 if the value is unmodifiable.
386 * @param useDefault true if the setter should be invoked with any existing
387 * property value should be; false if the old value should be
388 * discarded; defaults to true.
392 fgTie (const char * name, V (*getter)(), void (*setter)(V) = 0,
393 bool useDefault = true)
395 if (!globals->get_props()->tie(name, SGRawValueFunctions<V>(getter, setter),
397 SG_LOG(SG_GENERAL, SG_WARN,
398 "Failed to tie property " << name << " to functions");
403 * Tie a property to a pair of indexed functions.
405 * Every time the property value is queried, the getter (if any) will
406 * be invoked with the index provided; every time the property value
407 * is modified, the setter (if any) will be invoked with the index
408 * provided. The getter can be 0 to make the property unreadable, and
409 * the setter can be 0 to make the property unmodifiable.
411 * @param name The property name to tie (full path).
412 * @param index The integer argument to pass to the getter and
414 * @param getter The getter function, or 0 if the value is unreadable.
415 * @param setter The setter function, or 0 if the value is unmodifiable.
416 * @param useDefault true if the setter should be invoked with any existing
417 * property value should be; false if the old value should be
418 * discarded; defaults to true.
422 fgTie (const char * name, int index, V (*getter)(int),
423 void (*setter)(int, V) = 0, bool useDefault = true)
425 if (!globals->get_props()->tie(name,
426 SGRawValueFunctionsIndexed<V>(index,
430 SG_LOG(SG_GENERAL, SG_WARN,
431 "Failed to tie property " << name << " to indexed functions");
436 * Tie a property to a pair of object methods.
438 * Every time the property value is queried, the getter (if any) will
439 * be invoked; every time the property value is modified, the setter
440 * (if any) will be invoked. The getter can be 0 to make the property
441 * unreadable, and the setter can be 0 to make the property
444 * @param name The property name to tie (full path).
445 * @param obj The object whose methods should be invoked.
446 * @param getter The object's getter method, or 0 if the value is
448 * @param setter The object's setter method, or 0 if the value is
450 * @param useDefault true if the setter should be invoked with any existing
451 * property value should be; false if the old value should be
452 * discarded; defaults to true.
454 template <class T, class V>
456 fgTie (const char * name, T * obj, V (T::*getter)() const,
457 void (T::*setter)(V) = 0, bool useDefault = true)
459 if (!globals->get_props()->tie(name,
460 SGRawValueMethods<T,V>(*obj, getter, setter),
462 SG_LOG(SG_GENERAL, SG_WARN,
463 "Failed to tie property " << name << " to object methods");
468 * Tie a property to a pair of indexed object methods.
470 * Every time the property value is queried, the getter (if any) will
471 * be invoked with the index provided; every time the property value
472 * is modified, the setter (if any) will be invoked with the index
473 * provided. The getter can be 0 to make the property unreadable, and
474 * the setter can be 0 to make the property unmodifiable.
476 * @param name The property name to tie (full path).
477 * @param obj The object whose methods should be invoked.
478 * @param index The integer argument to pass to the getter and
480 * @param getter The getter method, or 0 if the value is unreadable.
481 * @param setter The setter method, or 0 if the value is unmodifiable.
482 * @param useDefault true if the setter should be invoked with any existing
483 * property value should be; false if the old value should be
484 * discarded; defaults to true.
486 template <class T, class V>
488 fgTie (const char * name, T * obj, int index,
489 V (T::*getter)(int) const, void (T::*setter)(int, V) = 0,
490 bool useDefault = true)
492 if (!globals->get_props()->tie(name,
493 SGRawValueMethodsIndexed<T,V>(*obj,
498 SG_LOG(SG_GENERAL, SG_WARN,
499 "Failed to tie property " << name << " to indexed object methods");
504 ////////////////////////////////////////////////////////////////////////
506 ////////////////////////////////////////////////////////////////////////
510 * An encoded condition.
512 * This class encodes a single condition of some sort, possibly
513 * connected with properties.
515 * This class should migrate to somewhere more general.
521 virtual ~FGCondition ();
522 virtual bool test () const = 0;
527 * Condition for a single property.
529 * This condition is true only if the property returns a boolean
532 class FGPropertyCondition : public FGCondition
535 FGPropertyCondition (const char * propname);
536 virtual ~FGPropertyCondition ();
537 virtual bool test () const { return _node->getBoolValue(); }
539 const SGPropertyNode * _node;
544 * Condition for a 'not' operator.
546 * This condition is true only if the child condition is false.
548 class FGNotCondition : public FGCondition
551 // transfer pointer ownership
552 FGNotCondition (FGCondition * condition);
553 virtual ~FGNotCondition ();
554 virtual bool test () const;
556 FGCondition * _condition;
561 * Condition for an 'and' group.
563 * This condition is true only if all of the conditions
564 * in the group are true.
566 class FGAndCondition : public FGCondition
570 virtual ~FGAndCondition ();
571 virtual bool test () const;
572 // transfer pointer ownership
573 virtual void addCondition (FGCondition * condition);
575 vector<FGCondition *> _conditions;
580 * Condition for an 'or' group.
582 * This condition is true if at least one of the conditions in the
585 class FGOrCondition : public FGCondition
589 virtual ~FGOrCondition ();
590 virtual bool test () const;
591 // transfer pointer ownership
592 virtual void addCondition (FGCondition * condition);
594 vector<FGCondition *> _conditions;
599 * Abstract base class for property comparison conditions.
601 class FGComparisonCondition : public FGCondition
609 FGComparisonCondition (Type type, bool reverse = false);
610 virtual ~FGComparisonCondition ();
611 virtual bool test () const;
612 virtual void setLeftProperty (const char * propname);
613 virtual void setRightProperty (const char * propname);
614 // will make a local copy
615 virtual void setRightValue (const SGPropertyNode * value);
619 const SGPropertyNode * _left_property;
620 const SGPropertyNode * _right_property;
621 const SGPropertyNode * _right_value;
626 * Base class for a conditional components.
628 * This class manages the conditions and tests; the component should
629 * invoke the test() method whenever it needs to decide whether to
630 * active itself, draw itself, and so on.
636 virtual ~FGConditional ();
637 // transfer pointer ownership
638 virtual void setCondition (FGCondition * condition);
639 virtual const FGCondition * getCondition () const { return _condition; }
640 virtual bool test () const;
642 FGCondition * _condition;
647 * Global function to make a condition out of properties.
649 * The top-level is always an implicit 'and' group, whatever the
650 * node's name (it should usually be "condition").
652 * @param node The top-level condition node (usually named "condition").
653 * @return A pointer to a newly-allocated condition; it is the
654 * responsibility of the caller to delete the condition when
655 * it is no longer needed.
657 FGCondition * fgReadCondition (const SGPropertyNode * node);
660 #endif // __FG_PROPS_HXX