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 * Load properties from a file.
70 * @param file The relative or absolute filename.
71 * @param props The property node to load the properties into.
72 * @param in_fg_root If true, look for the file relative to
73 * $FG_ROOT; otherwise, look for the file relative to the
74 * current working directory.
75 * @return true if the properties loaded successfully, false
78 extern bool fgLoadProps (const char * path, SGPropertyNode * props,
79 bool in_fg_root = true);
83 ////////////////////////////////////////////////////////////////////////
84 // Convenience functions for getting property values.
85 ////////////////////////////////////////////////////////////////////////
88 * Get a property node.
90 * @param path The path of the node, relative to root.
91 * @param create true to create the node if it doesn't exist.
92 * @return The node, or 0 if none exists and none was created.
94 extern SGPropertyNode * fgGetNode (const char * path, bool create = false);
98 * Get a property node with separate index.
100 * This method separates the index from the path string, to make it
101 * easier to iterate through multiple components without using sprintf
102 * to add indices. For example, fgGetNode("foo[1]/bar", 3) will
103 * return the same result as fgGetNode("foo[1]/bar[3]").
105 * @param path The path of the node, relative to root.
106 * @param index The index for the last member of the path (overrides
107 * any given in the string).
108 * @param create true to create the node if it doesn't exist.
109 * @return The node, or 0 if none exists and none was created.
111 extern SGPropertyNode * fgGetNode (const char * path,
112 int index, bool create = false);
116 * Test whether a given node exists.
118 * @param path The path of the node, relative to root.
119 * @return true if the node exists, false otherwise.
121 extern bool fgHasNode (const char * path);
125 * Add a listener to a node.
127 * @param listener The listener to add to the node.
128 * @param path The path of the node, relative to root.
129 * @param index The index for the last member of the path (overrides
130 * any given in the string).
132 extern void fgAddChangeListener (SGPropertyChangeListener * listener,
137 * Add a listener to a node.
139 * @param listener The listener to add to the node.
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).
144 extern void fgAddChangeListener (SGPropertyChangeListener * listener,
145 const char * path, int index);
149 * Get a bool value for a property.
151 * This method is convenient but inefficient. It should be used
152 * infrequently (i.e. for initializing, loading, saving, etc.),
153 * not in the main loop. If you need to get a value frequently,
154 * it is better to look up the node itself using fgGetNode and then
155 * use the node's getBoolValue() method, to avoid the lookup overhead.
157 * @param name The property name.
158 * @param defaultValue The default value to return if the property
160 * @return The property's value as a bool, or the default value provided.
162 extern bool fgGetBool (const char * name, bool defaultValue = false);
166 * Get an int value for a property.
168 * This method is convenient but inefficient. It should be used
169 * infrequently (i.e. for initializing, loading, saving, etc.),
170 * not in the main loop. If you need to get a value frequently,
171 * it is better to look up the node itself using fgGetNode and then
172 * use the node's getIntValue() method, to avoid the lookup overhead.
174 * @param name The property name.
175 * @param defaultValue The default value to return if the property
177 * @return The property's value as an int, or the default value provided.
179 extern int fgGetInt (const char * name, int defaultValue = 0);
183 * Get a long value for a property.
185 * This method is convenient but inefficient. It should be used
186 * infrequently (i.e. for initializing, loading, saving, etc.),
187 * not in the main loop. If you need to get a value frequently,
188 * it is better to look up the node itself using fgGetNode and then
189 * use the node's getLongValue() method, to avoid the lookup overhead.
191 * @param name The property name.
192 * @param defaultValue The default value to return if the property
194 * @return The property's value as a long, or the default value provided.
196 extern int fgGetLong (const char * name, long defaultValue = 0L);
200 * Get a float 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 getFloatValue() 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 float, or the default value provided.
213 extern float fgGetFloat (const char * name, float defaultValue = 0.0);
217 * Get a double value for a property.
219 * This method is convenient but inefficient. It should be used
220 * infrequently (i.e. for initializing, loading, saving, etc.),
221 * not in the main loop. If you need to get a value frequently,
222 * it is better to look up the node itself using fgGetNode and then
223 * use the node's getDoubleValue() method, to avoid the lookup overhead.
225 * @param name The property name.
226 * @param defaultValue The default value to return if the property
228 * @return The property's value as a double, or the default value provided.
230 extern double fgGetDouble (const char * name, double defaultValue = 0.0);
234 * Get a string value for a property.
236 * This method is convenient but inefficient. It should be used
237 * infrequently (i.e. for initializing, loading, saving, etc.),
238 * not in the main loop. If you need to get a value frequently,
239 * it is better to look up the node itself using fgGetNode and then
240 * use the node's getStringValue() method, to avoid the lookup overhead.
242 * @param name The property name.
243 * @param defaultValue The default value to return if the property
245 * @return The property's value as a string, or the default value provided.
247 extern const char * fgGetString (const char * name,
248 const char * defaultValue = "");
252 * Set a bool value for a property.
254 * Assign a bool value to a property. If the property does not
255 * yet exist, it will be created and its type will be set to
256 * BOOL; if it has a type of UNKNOWN, the type will also be set to
257 * BOOL; otherwise, the bool value will be converted to the property's
260 * @param name The property name.
261 * @param val The new value for the property.
262 * @return true if the assignment succeeded, false otherwise.
264 extern bool fgSetBool (const char * name, bool val);
268 * Set an int value for a property.
270 * Assign an int value to a property. If the property does not
271 * yet exist, it will be created and its type will be set to
272 * INT; if it has a type of UNKNOWN, the type will also be set to
273 * INT; otherwise, the bool value will be converted to the property's
276 * @param name The property name.
277 * @param val The new value for the property.
278 * @return true if the assignment succeeded, false otherwise.
280 extern bool fgSetInt (const char * name, int val);
284 * Set a long value for a property.
286 * Assign a long value to a property. If the property does not
287 * yet exist, it will be created and its type will be set to
288 * LONG; if it has a type of UNKNOWN, the type will also be set to
289 * LONG; otherwise, the bool value will be converted to the property's
292 * @param name The property name.
293 * @param val The new value for the property.
294 * @return true if the assignment succeeded, false otherwise.
296 extern bool fgSetLong (const char * name, long val);
300 * Set a float value for a property.
302 * Assign a float value to a property. If the property does not
303 * yet exist, it will be created and its type will be set to
304 * FLOAT; if it has a type of UNKNOWN, the type will also be set to
305 * FLOAT; otherwise, the bool value will be converted to the property's
308 * @param name The property name.
309 * @param val The new value for the property.
310 * @return true if the assignment succeeded, false otherwise.
312 extern bool fgSetFloat (const char * name, float 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 extern bool fgSetDouble (const char * name, double val);
332 * Set a string value for a property.
334 * Assign a string value to a property. If the property does not
335 * yet exist, it will be created and its type will be set to
336 * STRING; if it has a type of UNKNOWN, the type will also be set to
337 * STRING; otherwise, the string value will be converted to the property's
340 * @param name The property name.
341 * @param val The new value for the property.
342 * @return true if the assignment succeeded, false otherwise.
344 extern bool fgSetString (const char * name, const char * val);
348 ////////////////////////////////////////////////////////////////////////
349 // Convenience functions for setting property attributes.
350 ////////////////////////////////////////////////////////////////////////
354 * Set the state of the archive attribute for a property.
356 * If the archive attribute is true, the property will be written
357 * when a flight is saved; if it is false, the property will be
360 * A warning message will be printed if the property does not exist.
362 * @param name The property name.
363 * @param state The state of the archive attribute (defaults to true).
365 extern void fgSetArchivable (const char * name, bool state = true);
369 * Set the state of the read attribute for a property.
371 * If the read attribute is true, the property value will be readable;
372 * if it is false, the property value will always be the default value
375 * A warning message will be printed if the property does not exist.
377 * @param name The property name.
378 * @param state The state of the read attribute (defaults to true).
380 extern void fgSetReadable (const char * name, bool state = true);
384 * Set the state of the write attribute for a property.
386 * If the write attribute is true, the property value may be modified
387 * (depending on how it is tied); if the write attribute is false, the
388 * property value may not be modified.
390 * A warning message will be printed if the property does not exist.
392 * @param name The property name.
393 * @param state The state of the write attribute (defaults to true).
395 extern void fgSetWritable (const char * name, bool state = true);
399 ////////////////////////////////////////////////////////////////////////
400 // Convenience functions for tying properties, with logging.
401 ////////////////////////////////////////////////////////////////////////
405 * Untie a property from an external data source.
407 * Classes should use this function to release control of any
408 * properties they are managing.
410 extern void fgUntie (const char * name);
414 * Tie a property to a pair of simple functions.
416 * Every time the property value is queried, the getter (if any) will
417 * be invoked; every time the property value is modified, the setter
418 * (if any) will be invoked. The getter can be 0 to make the property
419 * unreadable, and the setter can be 0 to make the property
422 * @param name The property name to tie (full path).
423 * @param getter The getter function, or 0 if the value is unreadable.
424 * @param setter The setter function, or 0 if the value is unmodifiable.
425 * @param useDefault true if the setter should be invoked with any existing
426 * property value should be; false if the old value should be
427 * discarded; defaults to true.
431 fgTie (const char * name, V (*getter)(), void (*setter)(V) = 0,
432 bool useDefault = true)
434 if (!globals->get_props()->tie(name, SGRawValueFunctions<V>(getter, setter),
436 SG_LOG(SG_GENERAL, SG_WARN,
437 "Failed to tie property " << name << " to functions");
442 * Tie a property to a pair of indexed functions.
444 * Every time the property value is queried, the getter (if any) will
445 * be invoked with the index provided; every time the property value
446 * is modified, the setter (if any) will be invoked with the index
447 * provided. The getter can be 0 to make the property unreadable, and
448 * the setter can be 0 to make the property unmodifiable.
450 * @param name The property name to tie (full path).
451 * @param index The integer argument to pass to the getter and
453 * @param getter The getter function, or 0 if the value is unreadable.
454 * @param setter The setter function, or 0 if the value is unmodifiable.
455 * @param useDefault true if the setter should be invoked with any existing
456 * property value should be; false if the old value should be
457 * discarded; defaults to true.
461 fgTie (const char * name, int index, V (*getter)(int),
462 void (*setter)(int, V) = 0, bool useDefault = true)
464 if (!globals->get_props()->tie(name,
465 SGRawValueFunctionsIndexed<V>(index,
469 SG_LOG(SG_GENERAL, SG_WARN,
470 "Failed to tie property " << name << " to indexed functions");
475 * Tie a property to a pair of object methods.
477 * Every time the property value is queried, the getter (if any) will
478 * be invoked; every time the property value is modified, the setter
479 * (if any) will be invoked. The getter can be 0 to make the property
480 * unreadable, and the setter can be 0 to make the property
483 * @param name The property name to tie (full path).
484 * @param obj The object whose methods should be invoked.
485 * @param getter The object's getter method, or 0 if the value is
487 * @param setter The object's setter method, or 0 if the value is
489 * @param useDefault true if the setter should be invoked with any existing
490 * property value should be; false if the old value should be
491 * discarded; defaults to true.
493 template <class T, class V>
495 fgTie (const char * name, T * obj, V (T::*getter)() const,
496 void (T::*setter)(V) = 0, bool useDefault = true)
498 if (!globals->get_props()->tie(name,
499 SGRawValueMethods<T,V>(*obj, getter, setter),
501 SG_LOG(SG_GENERAL, SG_WARN,
502 "Failed to tie property " << name << " to object methods");
507 * Tie a property to a pair of indexed object methods.
509 * Every time the property value is queried, the getter (if any) will
510 * be invoked with the index provided; every time the property value
511 * is modified, the setter (if any) will be invoked with the index
512 * provided. The getter can be 0 to make the property unreadable, and
513 * the setter can be 0 to make the property unmodifiable.
515 * @param name The property name to tie (full path).
516 * @param obj The object whose methods should be invoked.
517 * @param index The integer argument to pass to the getter and
519 * @param getter The getter method, or 0 if the value is unreadable.
520 * @param setter The setter method, or 0 if the value is unmodifiable.
521 * @param useDefault true if the setter should be invoked with any existing
522 * property value should be; false if the old value should be
523 * discarded; defaults to true.
525 template <class T, class V>
527 fgTie (const char * name, T * obj, int index,
528 V (T::*getter)(int) const, void (T::*setter)(int, V) = 0,
529 bool useDefault = true)
531 if (!globals->get_props()->tie(name,
532 SGRawValueMethodsIndexed<T,V>(*obj,
537 SG_LOG(SG_GENERAL, SG_WARN,
538 "Failed to tie property " << name << " to indexed object methods");
543 ////////////////////////////////////////////////////////////////////////
545 ////////////////////////////////////////////////////////////////////////
549 * An encoded condition.
551 * This class encodes a single condition of some sort, possibly
552 * connected with properties.
554 * This class should migrate to somewhere more general.
560 virtual ~FGCondition ();
561 virtual bool test () const = 0;
566 * Condition for a single property.
568 * This condition is true only if the property returns a boolean
571 class FGPropertyCondition : public FGCondition
574 FGPropertyCondition (const char * propname);
575 virtual ~FGPropertyCondition ();
576 virtual bool test () const { return _node->getBoolValue(); }
578 const SGPropertyNode * _node;
583 * Condition for a 'not' operator.
585 * This condition is true only if the child condition is false.
587 class FGNotCondition : public FGCondition
590 // transfer pointer ownership
591 FGNotCondition (FGCondition * condition);
592 virtual ~FGNotCondition ();
593 virtual bool test () const;
595 FGCondition * _condition;
600 * Condition for an 'and' group.
602 * This condition is true only if all of the conditions
603 * in the group are true.
605 class FGAndCondition : public FGCondition
609 virtual ~FGAndCondition ();
610 virtual bool test () const;
611 // transfer pointer ownership
612 virtual void addCondition (FGCondition * condition);
614 vector<FGCondition *> _conditions;
619 * Condition for an 'or' group.
621 * This condition is true if at least one of the conditions in the
624 class FGOrCondition : public FGCondition
628 virtual ~FGOrCondition ();
629 virtual bool test () const;
630 // transfer pointer ownership
631 virtual void addCondition (FGCondition * condition);
633 vector<FGCondition *> _conditions;
638 * Abstract base class for property comparison conditions.
640 class FGComparisonCondition : public FGCondition
648 FGComparisonCondition (Type type, bool reverse = false);
649 virtual ~FGComparisonCondition ();
650 virtual bool test () const;
651 virtual void setLeftProperty (const char * propname);
652 virtual void setRightProperty (const char * propname);
653 // will make a local copy
654 virtual void setRightValue (const SGPropertyNode * value);
658 const SGPropertyNode * _left_property;
659 const SGPropertyNode * _right_property;
660 const SGPropertyNode * _right_value;
665 * Base class for a conditional components.
667 * This class manages the conditions and tests; the component should
668 * invoke the test() method whenever it needs to decide whether to
669 * active itself, draw itself, and so on.
675 virtual ~FGConditional ();
676 // transfer pointer ownership
677 virtual void setCondition (FGCondition * condition);
678 virtual const FGCondition * getCondition () const { return _condition; }
679 virtual bool test () const;
681 FGCondition * _condition;
686 * Global function to make a condition out of properties.
688 * The top-level is always an implicit 'and' group, whatever the
689 * node's name (it should usually be "condition").
691 * @param node The top-level condition node (usually named "condition").
692 * @return A pointer to a newly-allocated condition; it is the
693 * responsibility of the caller to delete the condition when
694 * it is no longer needed.
696 FGCondition * fgReadCondition (const SGPropertyNode * node);
699 #endif // __FG_PROPS_HXX