3 * Interface definition for a property list.
4 * Started Fall 2000 by David Megginson, david@megginson.com
5 * This code is released into the Public Domain.
7 * See props.html for documentation [replace with URL when available].
15 #include <simgear/compiler.h>
25 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
26 SG_USING_STD(istream);
27 SG_USING_STD(ostream);
31 #pragma warn A sloppy coder has defined NONE as a macro!
36 #pragma warn A sloppy coder has defined ALIAS as a macro!
41 #pragma warn A sloppy coder has defined UNSPECIFIED as a macro!
46 #pragma warn A sloppy coder has defined BOOL as a macro!
51 #pragma warn A sloppy coder has defined INT as a macro!
56 #pragma warn A sloppy coder has defined LONG as a macro!
61 #pragma warn A sloppy coder has defined FLOAT as a macro!
66 #pragma warn A sloppy coder has defined DOUBLE as a macro!
71 #pragma warn A sloppy coder has defined STRING as a macro!
77 ////////////////////////////////////////////////////////////////////////
80 // This is the mechanism that information-providing routines can
81 // use to link their own values to the property manager. Any
82 // SGValue can be tied to a raw value and then untied again.
83 ////////////////////////////////////////////////////////////////////////
87 * Abstract base class for a raw value.
89 * The property manager is implemented in three layers. The {@link
90 * SGPropertyNode} is the highest and most abstract layer,
91 * representing * an LValue/RValue pair: it * records the position
92 * of the property in the property tree and * contains facilities
93 * for navigation to other nodes. Each node * may contain an {@link
94 * SGValue}, which is guaranteed persistent: the * {@link SGValue}
95 * will not change during a session, even if the * property is bound
96 * and unbound multiple times. The SGValue is the * abstraction of
97 * an RValue: it allows for conversion among all of the different
98 * types, and can be bound to external pointers, functions, methods,
99 * or other data sources. Every SGValue contains an SGRawValue of
100 * a specific type. The SGRawValue (this class) may change frequently
101 * during a session as a value is retyped or bound and unbound to
102 * various data source, but the abstract SGValue layer insulates
103 * the application from those changes. The raw value contains no
104 * facilities for data binding or for type conversion: it is simply
105 * the abstraction of a primitive data type (or a compound data
106 * type, in the case of a string).
108 * The SGValue class always keeps a *copy* of a raw value, not the
109 * original one passed to it; if you override a derived class but do
110 * not replace the {@link #clone} method, strange things will happen.
112 * All raw values must implement {@link #getValue}, {@link #setValue},
113 * and {@link #clone} for the appropriate type.
116 * @see SGPropertyNode */
123 * The default underlying value for this type.
125 * Every raw value has a default; the default is false for a
126 * boolean, 0 for the various numeric values, and "" for a string.
127 * If additional types of raw values are added in the future, they
128 * may need different kinds of default values (such as epoch for a
129 * date type). The default value is used when creating new values.
131 static const T DefaultValue; // Default for this kind of raw value.
137 * Use the default value for this type.
145 virtual ~SGRawValue () {}
149 * Return the underlying value.
151 * @return The actual value for the property.
154 virtual T getValue () const = 0;
158 * Assign a new underlying value.
160 * If the new value cannot be set (because this is a read-only
161 * raw value, or because the new value is not acceptable for
162 * some reason) this method returns false and leaves the original
165 * @param value The actual value for the property.
166 * @return true if the value was set successfully, false otherwise.
169 virtual bool setValue (T value) = 0;
173 * Create a new deep copy of this raw value.
175 * The copy will contain its own version of the underlying value
178 * @return A deep copy of the current object.
180 virtual SGRawValue * clone () const = 0;
185 * An unbound raw value, stored internally.
187 * Instances of this class are created automatically, by default,
188 * by the SGValue class; ordinarily the application should not
192 class SGRawValueInternal : public SGRawValue<T>
197 * Default constructor.
199 * Initialize with the default value for this type.
201 SGRawValueInternal () {}
204 * Explicit value constructor.
206 * Initialize with the underlying value provided.
208 * @param value The initial value for this property.
210 SGRawValueInternal (T value) : _value(value) {}
215 virtual ~SGRawValueInternal () {}
218 * Get the underlying value.
220 virtual T getValue () const { return _value; }
223 * Set the underlying value.
225 virtual bool setValue (T value) { _value = value; return true; }
228 * Create a deep copy of this raw value.
230 virtual SGRawValue<T> * clone () const {
231 return new SGRawValueInternal<T>(_value);
240 * A raw value bound to a pointer.
242 * This is the most efficient way to tie an external value, but also
243 * the most dangerous, because there is no way for the supplier to
244 * perform bounds checking and derived calculations except by polling
245 * the variable to see if it has changed. There is no default
246 * constructor, because this class would be meaningless without a
250 class SGRawValuePointer : public SGRawValue<T>
255 * Explicit pointer constructor.
257 * Create a new raw value bound to the value of the variable
258 * referenced by the pointer.
260 * @param ptr The pointer to the variable to which this raw value
263 SGRawValuePointer (T * ptr) : _ptr(ptr) {}
268 virtual ~SGRawValuePointer () {}
271 * Get the underlying value.
273 * This method will dereference the pointer and return the
276 virtual T getValue () const { return *_ptr; }
279 * Set the underlying value.
281 * This method will dereference the pointer and change the
284 virtual bool setValue (T value) { *_ptr = value; return true; }
287 * Create a copy of this raw value.
289 * The copy will use the same external pointer as the original.
291 virtual SGRawValue<T> * clone () const {
292 return new SGRawValuePointer<T>(_ptr);
301 * A value managed through static functions.
303 * A read-only value will not have a setter; a write-only value will
307 class SGRawValueFunctions : public SGRawValue<T>
312 * The template type of a static getter function.
314 typedef T (*getter_t)();
317 * The template type of a static setter function.
319 typedef void (*setter_t)(T);
322 * Explicit constructor.
324 * Create a new raw value bound to the getter and setter supplied.
326 * @param getter A static function for getting a value, or 0
327 * to read-disable the value.
328 * @param setter A static function for setting a value, or 0
329 * to write-disable the value.
331 SGRawValueFunctions (getter_t getter = 0, setter_t setter = 0)
332 : _getter(getter), _setter(setter) {}
337 virtual ~SGRawValueFunctions () {}
340 * Get the underlying value.
342 * This method will invoke the getter function to get a value.
343 * If no getter function was supplied, this method will always
344 * return the default value for the type.
346 virtual T getValue () const {
347 if (_getter) return (*_getter)();
348 else return SGRawValue<T>::DefaultValue;
352 * Set the underlying value.
354 * This method will invoke the setter function to change the
355 * underlying value. If no setter function was supplied, this
356 * method will return false.
358 virtual bool setValue (T value) {
359 if (_setter) { (*_setter)(value); return true; }
364 * Create a copy of this raw value, bound to the same functions.
366 virtual SGRawValue<T> * clone () const {
367 return new SGRawValueFunctions<T>(_getter,_setter);
377 * An indexed value bound to static functions.
379 * A read-only value will not have a setter; a write-only value will
380 * not have a getter. An indexed value is useful for binding one
381 * of a list of possible values (such as multiple engines for a
382 * plane). The index is hard-coded at creation time.
385 class SGRawValueFunctionsIndexed : public SGRawValue<T>
388 typedef T (*getter_t)(int);
389 typedef void (*setter_t)(int,T);
390 SGRawValueFunctionsIndexed (int index, getter_t getter = 0, setter_t setter = 0)
391 : _index(index), _getter(getter), _setter(setter) {}
392 virtual ~SGRawValueFunctionsIndexed () {}
393 virtual T getValue () const {
394 if (_getter) return (*_getter)(_index);
395 else return SGRawValue<T>::DefaultValue;
397 virtual bool setValue (T value) {
398 if (_setter) { (*_setter)(_index, value); return true; }
401 virtual SGRawValue<T> * clone () const {
402 return new SGRawValueFunctionsIndexed<T>(_index, _getter, _setter);
412 * A value managed through an object and access methods.
414 * A read-only value will not have a setter; a write-only value will
417 template <class C, class T>
418 class SGRawValueMethods : public SGRawValue<T>
421 typedef T (C::*getter_t)() const;
422 typedef void (C::*setter_t)(T);
423 SGRawValueMethods (C &obj, getter_t getter = 0, setter_t setter = 0)
424 : _obj(obj), _getter(getter), _setter(setter) {}
425 virtual ~SGRawValueMethods () {}
426 virtual T getValue () const {
427 if (_getter) { return (_obj.*_getter)(); }
428 else { return SGRawValue<T>::DefaultValue; }
430 virtual bool setValue (T value) {
431 if (_setter) { (_obj.*_setter)(value); return true; }
434 virtual SGRawValue<T> * clone () const {
435 return new SGRawValueMethods<C,T>(_obj, _getter, _setter);
445 * An indexed value managed through an object and access methods.
447 * A read-only value will not have a setter; a write-only value will
450 template <class C, class T>
451 class SGRawValueMethodsIndexed : public SGRawValue<T>
454 typedef T (C::*getter_t)(int) const;
455 typedef void (C::*setter_t)(int, T);
456 SGRawValueMethodsIndexed (C &obj, int index,
457 getter_t getter = 0, setter_t setter = 0)
458 : _obj(obj), _index(index), _getter(getter), _setter(setter) {}
459 virtual ~SGRawValueMethodsIndexed () {}
460 virtual T getValue () const {
461 if (_getter) { return (_obj.*_getter)(_index); }
462 else { return SGRawValue<T>::DefaultValue; }
464 virtual bool setValue (T value) {
465 if (_setter) { (_obj.*_setter)(_index, value); return true; }
468 virtual SGRawValue<T> * clone () const {
469 return new SGRawValueMethodsIndexed<C,T>(_obj, _index, _getter, _setter);
481 * A node in a property tree.
490 * Property value types.
506 * Access mode attributes.
508 * <p>The ARCHIVE attribute is strictly advisory, and controls
509 * whether the property should normally be saved and restored.</p>
519 * Default constructor.
527 SGPropertyNode (const SGPropertyNode &node);
533 virtual ~SGPropertyNode ();
542 * Test whether this node contains a primitive leaf value.
544 bool hasValue () const { return (_type != NONE); }
548 * Get the node's simple (XML) name.
550 const string &getName () const { return _name; }
554 * Get the node's integer index.
556 const int getIndex () const { return _index; }
560 * Get a non-const pointer to the node's parent.
562 SGPropertyNode * getParent () { return _parent; }
566 * Get a const pointer to the node's parent.
568 const SGPropertyNode * getParent () const { return _parent; }
577 * Get the number of child nodes.
579 const int nChildren () const { return _children.size(); }
583 * Get a child node by position (*NOT* index).
585 SGPropertyNode * getChild (int position);
589 * Get a const child node by position (*NOT* index).
591 const SGPropertyNode * getChild (int position) const;
595 * Get a child node by name and index.
597 SGPropertyNode * getChild (const string &name, int index = 0,
598 bool create = false);
602 * Get a const child node by name and index.
604 const SGPropertyNode * getChild (const string &name, int index = 0) const;
608 * Get a vector of all children with the specified name.
610 vector<SGPropertyNode *> getChildren (const string &name);
614 * Get a vector all all children (const) with the specified name.
616 vector<const SGPropertyNode *> getChildren (const string &name) const;
625 * Alias this node's leaf value to another's.
627 bool alias (SGPropertyNode * target);
631 * Alias this node's leaf value to another's by relative path.
633 bool alias (const string &path);
637 * Remove any alias for this node.
643 * Test whether the node's leaf value is aliased to another's.
645 bool isAlias () const { return (_type == ALIAS); }
649 * Get a non-const pointer to the current alias target, if any.
651 SGPropertyNode * getAliasTarget ();
655 * Get a const pointer to the current alias target, if any.
657 const SGPropertyNode * getAliasTarget () const;
666 * Get the path to this node from the root.
668 string getPath (bool simplify = false) const;
672 * Get a pointer to the root node.
674 SGPropertyNode * getRootNode ();
678 * Get a const pointer to the root node.
680 const SGPropertyNode * getRootNode () const;
684 * Get a pointer to another node by relative path.
686 SGPropertyNode * getNode (const string &relative_path, bool create = false);
690 * Get a const pointer to another node by relative path.
692 const SGPropertyNode * getNode (const string &relative_path) const;
700 * Check a single mode attribute for the property node.
702 bool getAttribute (Attribute attr) const { return ((_attr & attr) != 0); }
706 * Set a single mode attribute for the property node.
708 void setAttribute (Attribute attr, bool state) {
709 (state ? _attr |= attr : _attr &= ~attr);
714 * Get all of the mode attributes for the property node.
716 int getAttributes () const { return _attr; }
720 * Set all of the mode attributes for the property node.
722 void setAttributes (int attr) { _attr = attr; }
726 // Leaf Value (primitive).
731 * Get the type of leaf value, if any, for this node.
733 Type getType () const;
737 * Get a bool value for this node.
739 bool getBoolValue () const;
743 * Get an int value for this node.
745 int getIntValue () const;
749 * Get a long int value for this node.
751 long getLongValue () const;
755 * Get a float value for this node.
757 float getFloatValue () const;
761 * Get a double value for this node.
763 double getDoubleValue () const;
767 * Get a string value for this node.
769 string getStringValue () const;
774 * Set a bool value for this node.
776 bool setBoolValue (bool value);
780 * Set an int value for this node.
782 bool setIntValue (int value);
786 * Set a long int value for this node.
788 bool setLongValue (long value);
792 * Set a float value for this node.
794 bool setFloatValue (float value);
798 * Set a double value for this node.
800 bool setDoubleValue (double value);
804 * Set a string value for this node.
806 bool setStringValue (string value);
810 * Set a value of unspecified type for this node.
812 bool setUnspecifiedValue (string value);
821 * Test whether this node is bound to an external data source.
823 bool isTied () const { return _tied; }
827 * Bind this node to an external bool source.
829 bool tie (const SGRawValue<bool> &rawValue, bool useDefault = true);
833 * Bind this node to an external int source.
835 bool tie (const SGRawValue<int> &rawValue, bool useDefault = true);
839 * Bind this node to an external long int source.
841 bool tie (const SGRawValue<long> &rawValue, bool useDefault = true);
845 * Bind this node to an external float source.
847 bool tie (const SGRawValue<float> &rawValue, bool useDefault = true);
851 * Bind this node to an external double source.
853 bool tie (const SGRawValue<double> &rawValue, bool useDefault = true);
857 * Bind this node to an external string source.
859 bool tie (const SGRawValue<string> &rawValue, bool useDefault = true);
863 * Unbind this node from any external data source.
869 // Convenience methods using paths.
870 // TODO: add attribute methods
875 * Get another node's type.
877 Type getType (const string &relative_path) const;
881 * Test whether another node has a leaf value.
883 bool hasValue (const string &relative_path) const;
887 * Get another node's value as a bool.
889 bool getBoolValue (const string &relative_path,
890 bool defaultValue = false) const;
894 * Get another node's value as an int.
896 int getIntValue (const string &relative_path,
897 int defaultValue = 0) const;
901 * Get another node's value as a long int.
903 long getLongValue (const string &relative_path,
904 long defaultValue = 0L) const;
908 * Get another node's value as a float.
910 float getFloatValue (const string &relative_path,
911 float defaultValue = 0.0) const;
915 * Get another node's value as a double.
917 double getDoubleValue (const string &relative_path,
918 double defaultValue = 0.0L) const;
922 * Get another node's value as a string.
924 string getStringValue (const string &relative_path,
925 string defaultValue = "") const;
929 * Set another node's value as a bool.
931 bool setBoolValue (const string &relative_path, bool value);
935 * Set another node's value as an int.
937 bool setIntValue (const string &relative_path, int value);
941 * Set another node's value as a long int.
943 bool setLongValue (const string &relative_path, long value);
947 * Set another node's value as a float.
949 bool setFloatValue (const string &relative_path, float value);
953 * Set another node's value as a double.
955 bool setDoubleValue (const string &relative_path, double value);
959 * Set another node's value as a string.
961 bool setStringValue (const string &relative_path, string value);
965 * Set another node's value with no specified type.
967 bool setUnspecifiedValue (const string &relative_path, string value);
971 * Test whether another node is bound to an external data source.
973 bool isTied (const string &relative_path) const;
977 * Bind another node to an external bool source.
979 bool tie (const string &relative_path, const SGRawValue<bool> &rawValue,
980 bool useDefault = true);
984 * Bind another node to an external int source.
986 bool tie (const string &relative_path, const SGRawValue<int> &rawValue,
987 bool useDefault = true);
991 * Bind another node to an external long int source.
993 bool tie (const string &relative_path, const SGRawValue<long> &rawValue,
994 bool useDefault = true);
998 * Bind another node to an external float source.
1000 bool tie (const string &relative_path, const SGRawValue<float> &rawValue,
1001 bool useDefault = true);
1005 * Bind another node to an external double source.
1007 bool tie (const string &relative_path, const SGRawValue<double> &rawValue,
1008 bool useDefault = true);
1012 * Bind another node to an external string source.
1014 bool tie (const string &relative_path, const SGRawValue<string> &rawValue,
1015 bool useDefault = true);
1019 * Unbind another node from any external data source.
1021 bool untie (const string &relative_path);
1028 * Protected constructor for making new nodes on demand.
1030 SGPropertyNode (const string &name, int index, SGPropertyNode * parent);
1037 * Clear any existing value and set the type to NONE.
1039 void clear_value ();
1043 SGPropertyNode * _parent;
1044 vector<SGPropertyNode *> _children;
1051 // The right kind of pointer...
1053 SGPropertyNode * alias;
1054 SGRawValue<bool> * bool_val;
1055 SGRawValue<int> * int_val;
1056 SGRawValue<long> * long_val;
1057 SGRawValue<float> * float_val;
1058 SGRawValue<double> * double_val;
1059 SGRawValue<string> * string_val;
1067 ////////////////////////////////////////////////////////////////////////
1069 ////////////////////////////////////////////////////////////////////////
1073 * Read properties from an XML input stream.
1075 void readProperties (istream &input, SGPropertyNode * start_node,
1076 const string &base = "");
1080 * Read properties from an XML file.
1082 void readProperties (const string &file, SGPropertyNode * start_node);
1086 * Write properties to an XML output stream.
1088 void writeProperties (ostream &output, const SGPropertyNode * start_node);
1092 * Write properties to an XML file.
1094 void writeProperties (const string &file, const SGPropertyNode * start_node);
1098 * Copy properties from one node to another.
1100 bool copyProperties (const SGPropertyNode *in, SGPropertyNode *out);
1103 #endif // __PROPS_HXX