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);
455 * Tie a property to a pair of simple functions.
457 * Every time the property value is queried, the getter (if any) will
458 * be invoked; every time the property value is modified, the setter
459 * (if any) will be invoked. The getter can be 0 to make the property
460 * unreadable, and the setter can be 0 to make the property
463 * @param name The property name to tie (full path).
464 * @param getter The getter function, or 0 if the value is unreadable.
465 * @param setter The setter function, or 0 if the value is unmodifiable.
466 * @param useDefault true if the setter should be invoked with any existing
467 * property value should be; false if the old value should be
468 * discarded; defaults to true.
472 fgTie (const string &name, V (*getter)(), void (*setter)(V) = 0,
473 bool useDefault = true)
475 if (!globals->get_props()->tie(name, SGRawValueFunctions<V>(getter, setter),
477 SG_LOG(SG_GENERAL, SG_WARN,
478 "Failed to tie property " << name << " to functions");
483 * Tie a property to a pair of indexed functions.
485 * Every time the property value is queried, the getter (if any) will
486 * be invoked with the index provided; every time the property value
487 * is modified, the setter (if any) will be invoked with the index
488 * provided. The getter can be 0 to make the property unreadable, and
489 * the setter can be 0 to make the property unmodifiable.
491 * @param name The property name to tie (full path).
492 * @param index The integer argument to pass to the getter and
494 * @param getter The getter function, or 0 if the value is unreadable.
495 * @param setter The setter function, or 0 if the value is unmodifiable.
496 * @param useDefault true if the setter should be invoked with any existing
497 * property value should be; false if the old value should be
498 * discarded; defaults to true.
502 fgTie (const string &name, int index, V (*getter)(int),
503 void (*setter)(int, V) = 0, bool useDefault = true)
505 if (!globals->get_props()->tie(name,
506 SGRawValueFunctionsIndexed<V>(index,
510 SG_LOG(SG_GENERAL, SG_WARN,
511 "Failed to tie property " << name << " to indexed functions");
516 * Tie a property to a pair of object methods.
518 * Every time the property value is queried, the getter (if any) will
519 * be invoked; every time the property value is modified, the setter
520 * (if any) will be invoked. The getter can be 0 to make the property
521 * unreadable, and the setter can be 0 to make the property
524 * @param name The property name to tie (full path).
525 * @param obj The object whose methods should be invoked.
526 * @param getter The object's getter method, or 0 if the value is
528 * @param setter The object's setter method, or 0 if the value is
530 * @param useDefault true if the setter should be invoked with any existing
531 * property value should be; false if the old value should be
532 * discarded; defaults to true.
534 template <class T, class V>
536 fgTie (const string &name, T * obj, V (T::*getter)() const,
537 void (T::*setter)(V) = 0, bool useDefault = true)
539 if (!globals->get_props()->tie(name,
540 SGRawValueMethods<T,V>(*obj, getter, setter),
542 SG_LOG(SG_GENERAL, SG_WARN,
543 "Failed to tie property " << name << " to object methods");
548 * Tie a property to a pair of indexed object methods.
550 * Every time the property value is queried, the getter (if any) will
551 * be invoked with the index provided; every time the property value
552 * is modified, the setter (if any) will be invoked with the index
553 * provided. The getter can be 0 to make the property unreadable, and
554 * the setter can be 0 to make the property unmodifiable.
556 * @param name The property name to tie (full path).
557 * @param obj The object whose methods should be invoked.
558 * @param index The integer argument to pass to the getter and
560 * @param getter The getter method, or 0 if the value is unreadable.
561 * @param setter The setter method, or 0 if the value is unmodifiable.
562 * @param useDefault true if the setter should be invoked with any existing
563 * property value should be; false if the old value should be
564 * discarded; defaults to true.
566 template <class T, class V>
568 fgTie (const string &name, T * obj, int index,
569 V (T::*getter)(int) const, void (T::*setter)(int, V) = 0,
570 bool useDefault = true)
572 if (!globals->get_props()->tie(name,
573 SGRawValueMethodsIndexed<T,V>(*obj,
578 SG_LOG(SG_GENERAL, SG_WARN,
579 "Failed to tie property " << name << " to indexed object methods");
584 ////////////////////////////////////////////////////////////////////////
586 ////////////////////////////////////////////////////////////////////////
590 * An encoded condition.
592 * This class encodes a single condition of some sort, possibly
593 * connected with properties.
595 * This class should migrate to somewhere more general.
601 virtual ~FGCondition ();
602 virtual bool test () const = 0;
607 * Condition for a single property.
609 * This condition is true only if the property returns a boolean
612 class FGPropertyCondition : public FGCondition
615 FGPropertyCondition (const string &propname);
616 virtual ~FGPropertyCondition ();
617 virtual bool test () const { return _node->getBoolValue(); }
619 const SGPropertyNode * _node;
624 * Condition for a 'not' operator.
626 * This condition is true only if the child condition is false.
628 class FGNotCondition : public FGCondition
631 // transfer pointer ownership
632 FGNotCondition (FGCondition * condition);
633 virtual ~FGNotCondition ();
634 virtual bool test () const;
636 FGCondition * _condition;
641 * Condition for an 'and' group.
643 * This condition is true only if all of the conditions
644 * in the group are true.
646 class FGAndCondition : public FGCondition
650 virtual ~FGAndCondition ();
651 virtual bool test () const;
652 // transfer pointer ownership
653 virtual void addCondition (FGCondition * condition);
655 vector<FGCondition *> _conditions;
660 * Condition for an 'or' group.
662 * This condition is true if at least one of the conditions in the
665 class FGOrCondition : public FGCondition
669 virtual ~FGOrCondition ();
670 virtual bool test () const;
671 // transfer pointer ownership
672 virtual void addCondition (FGCondition * condition);
674 vector<FGCondition *> _conditions;
679 * Abstract base class for property comparison conditions.
681 class FGComparisonCondition : public FGCondition
689 FGComparisonCondition (Type type, bool reverse = false);
690 virtual ~FGComparisonCondition ();
691 virtual bool test () const;
692 virtual void setLeftProperty (const string &propname);
693 virtual void setRightProperty (const string &propname);
694 // will make a local copy
695 virtual void setRightValue (const SGPropertyNode * value);
699 const SGPropertyNode * _left_property;
700 const SGPropertyNode * _right_property;
701 const SGPropertyNode * _right_value;
706 * Base class for a conditional components.
708 * This class manages the conditions and tests; the component should
709 * invoke the test() method whenever it needs to decide whether to
710 * active itself, draw itself, and so on.
716 virtual ~FGConditional ();
717 // transfer pointer ownership
718 virtual void setCondition (FGCondition * condition);
719 virtual const FGCondition * getCondition () const { return _condition; }
720 virtual bool test () const;
722 FGCondition * _condition;
727 * Global function to make a condition out of properties.
729 * The top-level is always an implicit 'and' group, whatever the
730 * node's name (it should usually be "condition").
732 * @param node The top-level condition node (usually named "condition").
733 * @return A pointer to a newly-allocated condition; it is the
734 * responsibility of the caller to delete the condition when
735 * it is no longer needed.
737 FGCondition * fgReadCondition (const SGPropertyNode * node);
740 #endif // __FG_PROPS_HXX