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 #ifndef PROPS_STANDALONE
16 #define PROPS_STANDALONE 0
33 #include <simgear/compiler.h>
34 #include <simgear/debug/logstream.hxx>
39 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
40 SG_USING_STD(istream);
41 SG_USING_STD(ostream);
48 #pragma warn A sloppy coder has defined NONE as a macro!
53 #pragma warn A sloppy coder has defined ALIAS as a macro!
58 #pragma warn A sloppy coder has defined UNSPECIFIED as a macro!
63 #pragma warn A sloppy coder has defined BOOL as a macro!
68 #pragma warn A sloppy coder has defined INT as a macro!
73 #pragma warn A sloppy coder has defined LONG as a macro!
78 #pragma warn A sloppy coder has defined FLOAT as a macro!
83 #pragma warn A sloppy coder has defined DOUBLE as a macro!
88 #pragma warn A sloppy coder has defined STRING as a macro!
94 ////////////////////////////////////////////////////////////////////////
97 // This is the mechanism that information-providing routines can
98 // use to link their own values to the property manager. Any
99 // SGValue can be tied to a raw value and then untied again.
101 // Note: we are forced to use inlined methods here to ensure
102 // that the templates will be instantiated. We're probably taking
103 // a small performance hit for that.
104 ////////////////////////////////////////////////////////////////////////
108 * Abstract base class for a raw value.
110 * <p>The property manager is implemented in two layers. The {@link
111 * SGPropertyNode} is the highest and most abstract layer,
112 * representing an LValue/RValue pair: it records the position of the
113 * property in the property tree and contains facilities for
114 * navigation to other nodes. It is guaranteed to be persistent: the
115 * {@link SGPropertyNode} will not change during a session, even if
116 * the property is bound and unbound multiple times.</p>
118 * <p>When the property value is not managed internally in the
119 * SGPropertyNode, the SGPropertyNode will contain a reference to an
120 * SGRawValue (this class), which provides an abstract way to get,
121 * set, and clone the underlying value. The SGRawValue may change
122 * frequently during a session as a value is retyped or bound and
123 * unbound to various data source, but the abstract SGPropertyNode
124 * layer insulates the application from those changes. The raw value
125 * contains no facilities for data binding or for type conversion: it
126 * is simply the abstraction of a primitive data type (or a compound
127 * data type, in the case of a string).</p>
129 * <p>The SGPropertyNode class always keeps a *copy* of a raw value,
130 * not the original one passed to it; if you override a derived class
131 * but do not replace the {@link #clone} method, strange things will
134 * <p>All derived SGRawValue classes must implement {@link #getValue},
135 * {@link #setValue}, and {@link #clone} for the appropriate type.</p>
137 * @see SGPropertyNode
138 * @see SGRawValuePointer
139 * @see SGRawValueFunctions
140 * @see SGRawValueFunctionsIndexed
141 * @see SGRawValueMethods
142 * @see SGRawValueMethodsIndexed
150 * The default underlying value for this type.
152 * Every raw value has a default; the default is false for a
153 * boolean, 0 for the various numeric values, and "" for a string.
154 * If additional types of raw values are added in the future, they
155 * may need different kinds of default values (such as epoch for a
156 * date type). The default value is used when creating new values.
158 static const T DefaultValue; // Default for this kind of raw value.
164 * Use the default value for this type.
172 virtual ~SGRawValue () {}
176 * Return the underlying value.
178 * @return The actual value for the property.
181 virtual T getValue () const = 0;
185 * Assign a new underlying value.
187 * If the new value cannot be set (because this is a read-only
188 * raw value, or because the new value is not acceptable for
189 * some reason) this method returns false and leaves the original
192 * @param value The actual value for the property.
193 * @return true if the value was set successfully, false otherwise.
196 virtual bool setValue (T value) = 0;
200 * Create a new deep copy of this raw value.
202 * The copy will contain its own version of the underlying value
205 * @return A deep copy of the current object.
207 virtual SGRawValue * clone () const = 0;
212 * A raw value bound to a pointer.
214 * This is the most efficient way to tie an external value, but also
215 * the most dangerous, because there is no way for the supplier to
216 * perform bounds checking and derived calculations except by polling
217 * the variable to see if it has changed. There is no default
218 * constructor, because this class would be meaningless without a
222 class SGRawValuePointer : public SGRawValue<T>
227 * Explicit pointer constructor.
229 * Create a new raw value bound to the value of the variable
230 * referenced by the pointer.
232 * @param ptr The pointer to the variable to which this raw value
235 SGRawValuePointer (T * ptr) : _ptr(ptr) {}
240 virtual ~SGRawValuePointer () {}
243 * Get the underlying value.
245 * This method will dereference the pointer and return the
248 virtual T getValue () const { return *_ptr; }
251 * Set the underlying value.
253 * This method will dereference the pointer and change the
256 virtual bool setValue (T value) { *_ptr = value; return true; }
259 * Create a copy of this raw value.
261 * The copy will use the same external pointer as the original.
263 virtual SGRawValue<T> * clone () const {
264 return new SGRawValuePointer<T>(_ptr);
273 * A value managed through static functions.
275 * A read-only value will not have a setter; a write-only value will
279 class SGRawValueFunctions : public SGRawValue<T>
284 * The template type of a static getter function.
286 typedef T (*getter_t)();
289 * The template type of a static setter function.
291 typedef void (*setter_t)(T);
294 * Explicit constructor.
296 * Create a new raw value bound to the getter and setter supplied.
298 * @param getter A static function for getting a value, or 0
299 * to read-disable the value.
300 * @param setter A static function for setting a value, or 0
301 * to write-disable the value.
303 SGRawValueFunctions (getter_t getter = 0, setter_t setter = 0)
304 : _getter(getter), _setter(setter) {}
309 virtual ~SGRawValueFunctions () {}
312 * Get the underlying value.
314 * This method will invoke the getter function to get a value.
315 * If no getter function was supplied, this method will always
316 * return the default value for the type.
318 virtual T getValue () const {
319 if (_getter) return (*_getter)();
320 else return SGRawValue<T>::DefaultValue;
324 * Set the underlying value.
326 * This method will invoke the setter function to change the
327 * underlying value. If no setter function was supplied, this
328 * method will return false.
330 virtual bool setValue (T value) {
331 if (_setter) { (*_setter)(value); return true; }
336 * Create a copy of this raw value, bound to the same functions.
338 virtual SGRawValue<T> * clone () const {
339 return new SGRawValueFunctions<T>(_getter,_setter);
349 * An indexed value bound to static functions.
351 * A read-only value will not have a setter; a write-only value will
352 * not have a getter. An indexed value is useful for binding one
353 * of a list of possible values (such as multiple engines for a
354 * plane). The index is hard-coded at creation time.
359 class SGRawValueFunctionsIndexed : public SGRawValue<T>
362 typedef T (*getter_t)(int);
363 typedef void (*setter_t)(int,T);
364 SGRawValueFunctionsIndexed (int index, getter_t getter = 0, setter_t setter = 0)
365 : _index(index), _getter(getter), _setter(setter) {}
366 virtual ~SGRawValueFunctionsIndexed () {}
367 virtual T getValue () const {
368 if (_getter) return (*_getter)(_index);
369 else return SGRawValue<T>::DefaultValue;
371 virtual bool setValue (T value) {
372 if (_setter) { (*_setter)(_index, value); return true; }
375 virtual SGRawValue<T> * clone () const {
376 return new SGRawValueFunctionsIndexed<T>(_index, _getter, _setter);
386 * A value managed through an object and access methods.
388 * A read-only value will not have a setter; a write-only value will
391 template <class C, class T>
392 class SGRawValueMethods : public SGRawValue<T>
395 typedef T (C::*getter_t)() const;
396 typedef void (C::*setter_t)(T);
397 SGRawValueMethods (C &obj, getter_t getter = 0, setter_t setter = 0)
398 : _obj(obj), _getter(getter), _setter(setter) {}
399 virtual ~SGRawValueMethods () {}
400 virtual T getValue () const {
401 if (_getter) { return (_obj.*_getter)(); }
402 else { return SGRawValue<T>::DefaultValue; }
404 virtual bool setValue (T value) {
405 if (_setter) { (_obj.*_setter)(value); return true; }
408 virtual SGRawValue<T> * clone () const {
409 return new SGRawValueMethods<C,T>(_obj, _getter, _setter);
419 * An indexed value managed through an object and access methods.
421 * A read-only value will not have a setter; a write-only value will
424 template <class C, class T>
425 class SGRawValueMethodsIndexed : public SGRawValue<T>
428 typedef T (C::*getter_t)(int) const;
429 typedef void (C::*setter_t)(int, T);
430 SGRawValueMethodsIndexed (C &obj, int index,
431 getter_t getter = 0, setter_t setter = 0)
432 : _obj(obj), _index(index), _getter(getter), _setter(setter) {}
433 virtual ~SGRawValueMethodsIndexed () {}
434 virtual T getValue () const {
435 if (_getter) { return (_obj.*_getter)(_index); }
436 else { return SGRawValue<T>::DefaultValue; }
438 virtual bool setValue (T value) {
439 if (_setter) { (_obj.*_setter)(_index, value); return true; }
442 virtual SGRawValue<T> * clone () const {
443 return new SGRawValueMethodsIndexed<C,T>(_obj, _index, _getter, _setter);
454 * The smart pointer that manage reference counting
456 class SGPropertyNode;
457 class SGPropertyNode_ptr
462 * Default constructor
464 SGPropertyNode_ptr();
469 SGPropertyNode_ptr( const SGPropertyNode_ptr &r );
472 * Constructor from a pointer to a node
474 SGPropertyNode_ptr( SGPropertyNode *p );
479 ~SGPropertyNode_ptr();
482 * Assignement operator
484 SGPropertyNode_ptr &operator=( const SGPropertyNode_ptr &r );
487 * Pointer access operator
489 SGPropertyNode *operator->();
492 * Pointer access operator (const)
494 const SGPropertyNode *operator->() const;
497 * Conversion to SGPropertyNode * operator
499 operator SGPropertyNode *();
502 * Conversion to const SGPropertyNode * operator
504 operator const SGPropertyNode *() const;
507 * Return the pointer.
509 SGPropertyNode * ptr () { return _ptr; }
518 SGPropertyNode *_ptr;
524 * The property change listener interface.
526 * <p>Any class that needs to listen for property changes must implement
527 * this interface.</p>
529 class SGPropertyChangeListener
533 virtual void propertyChanged (SGPropertyNode * node) = 0;
540 * A node in a property tree.
550 MAX_STRING_LEN = 1024
554 * Property value types.
570 * Access mode attributes.
572 * <p>The ARCHIVE attribute is strictly advisory, and controls
573 * whether the property should normally be saved and restored.</p>
586 * Last used attribute
587 * Update as needed when enum Attribute is changed
589 static const int LAST_USED_ATTRIBUTE;
593 * Default constructor.
601 SGPropertyNode (const SGPropertyNode &node);
607 virtual ~SGPropertyNode ();
616 * Test whether this node contains a primitive leaf value.
618 bool hasValue () const { return (_type != NONE); }
622 * Get the node's simple (XML) name.
624 const char * getName () const { return _name; }
628 * Get the node's integer index.
630 int getIndex () const { return _index; }
634 * Get a non-const pointer to the node's parent.
636 SGPropertyNode * getParent () { return _parent; }
640 * Get a const pointer to the node's parent.
642 const SGPropertyNode * getParent () const { return _parent; }
651 * Get the number of child nodes.
653 int nChildren () const { return _children.size(); }
657 * Get a child node by position (*NOT* index).
659 SGPropertyNode * getChild (int position);
663 * Get a const child node by position (*NOT* index).
665 const SGPropertyNode * getChild (int position) const;
669 * Get a child node by name and index.
671 SGPropertyNode * getChild (const char * name, int index = 0,
672 bool create = false);
676 * Get a const child node by name and index.
678 const SGPropertyNode * getChild (const char * name, int index = 0) const;
682 * Get a vector of all children with the specified name.
684 vector<SGPropertyNode_ptr> getChildren (const char * name) const;
688 * Remove a child node
690 SGPropertyNode_ptr removeChild (const char * name, int index = 0,
700 * Alias this node's leaf value to another's.
702 bool alias (SGPropertyNode * target);
706 * Alias this node's leaf value to another's by relative path.
708 bool alias (const char * path);
712 * Remove any alias for this node.
718 * Test whether the node's leaf value is aliased to another's.
720 bool isAlias () const { return (_type == ALIAS); }
724 * Get a non-const pointer to the current alias target, if any.
726 SGPropertyNode * getAliasTarget ();
730 * Get a const pointer to the current alias target, if any.
732 const SGPropertyNode * getAliasTarget () const;
741 * Get the path to this node from the root.
743 const char * getPath (bool simplify = false) const;
747 * Get a pointer to the root node.
749 SGPropertyNode * getRootNode ();
753 * Get a const pointer to the root node.
755 const SGPropertyNode * getRootNode () const;
759 * Get a pointer to another node by relative path.
761 SGPropertyNode * getNode (const char * relative_path, bool create = false);
765 * Get a pointer to another node by relative path.
767 * This method leaves the index off the last member of the path,
768 * so that the user can specify it separately (and save some
769 * string building). For example, getNode("/bar[1]/foo", 3) is
770 * exactly equivalent to getNode("bar[1]/foo[3]"). The index
771 * provided overrides any given in the path itself for the last
774 SGPropertyNode * getNode (const char * relative_path, int index,
775 bool create = false);
779 * Get a const pointer to another node by relative path.
781 const SGPropertyNode * getNode (const char * relative_path) const;
785 * Get a const pointer to another node by relative path.
787 * This method leaves the index off the last member of the path,
788 * so that the user can specify it separate.
790 const SGPropertyNode * getNode (const char * relative_path,
799 * Check a single mode attribute for the property node.
801 bool getAttribute (Attribute attr) const { return ((_attr & attr) != 0); }
805 * Set a single mode attribute for the property node.
807 void setAttribute (Attribute attr, bool state) {
808 (state ? _attr |= attr : _attr &= ~attr);
813 * Get all of the mode attributes for the property node.
815 int getAttributes () const { return _attr; }
819 * Set all of the mode attributes for the property node.
821 void setAttributes (int attr) { _attr = attr; }
825 // Leaf Value (primitive).
830 * Get the type of leaf value, if any, for this node.
832 Type getType () const;
836 * Get a bool value for this node.
838 bool getBoolValue () const;
842 * Get an int value for this node.
844 int getIntValue () const;
848 * Get a long int value for this node.
850 long getLongValue () const;
854 * Get a float value for this node.
856 float getFloatValue () const;
860 * Get a double value for this node.
862 double getDoubleValue () const;
866 * Get a string value for this node.
868 const char * getStringValue () const;
873 * Set a bool value for this node.
875 bool setBoolValue (bool value);
879 * Set an int value for this node.
881 bool setIntValue (int value);
885 * Set a long int value for this node.
887 bool setLongValue (long value);
891 * Set a float value for this node.
893 bool setFloatValue (float value);
897 * Set a double value for this node.
899 bool setDoubleValue (double value);
903 * Set a string value for this node.
905 bool setStringValue (const char * value);
909 * Set a value of unspecified type for this node.
911 bool setUnspecifiedValue (const char * value);
920 * Test whether this node is bound to an external data source.
922 bool isTied () const { return _tied; }
926 * Bind this node to an external bool source.
928 bool tie (const SGRawValue<bool> &rawValue, bool useDefault = true);
932 * Bind this node to an external int source.
934 bool tie (const SGRawValue<int> &rawValue, bool useDefault = true);
938 * Bind this node to an external long int source.
940 bool tie (const SGRawValue<long> &rawValue, bool useDefault = true);
944 * Bind this node to an external float source.
946 bool tie (const SGRawValue<float> &rawValue, bool useDefault = true);
950 * Bind this node to an external double source.
952 bool tie (const SGRawValue<double> &rawValue, bool useDefault = true);
956 * Bind this node to an external string source.
958 bool tie (const SGRawValue<const char *> &rawValue, bool useDefault = true);
962 * Unbind this node from any external data source.
968 // Convenience methods using paths.
969 // TODO: add attribute methods
974 * Get another node's type.
976 Type getType (const char * relative_path) const;
980 * Test whether another node has a leaf value.
982 bool hasValue (const char * relative_path) const;
986 * Get another node's value as a bool.
988 bool getBoolValue (const char * relative_path,
989 bool defaultValue = false) const;
993 * Get another node's value as an int.
995 int getIntValue (const char * relative_path,
996 int defaultValue = 0) const;
1000 * Get another node's value as a long int.
1002 long getLongValue (const char * relative_path,
1003 long defaultValue = 0L) const;
1007 * Get another node's value as a float.
1009 float getFloatValue (const char * relative_path,
1010 float defaultValue = 0.0) const;
1014 * Get another node's value as a double.
1016 double getDoubleValue (const char * relative_path,
1017 double defaultValue = 0.0L) const;
1021 * Get another node's value as a string.
1023 const char * getStringValue (const char * relative_path,
1024 const char * defaultValue = "") const;
1028 * Set another node's value as a bool.
1030 bool setBoolValue (const char * relative_path, bool value);
1034 * Set another node's value as an int.
1036 bool setIntValue (const char * relative_path, int value);
1040 * Set another node's value as a long int.
1042 bool setLongValue (const char * relative_path, long value);
1046 * Set another node's value as a float.
1048 bool setFloatValue (const char * relative_path, float value);
1052 * Set another node's value as a double.
1054 bool setDoubleValue (const char * relative_path, double value);
1058 * Set another node's value as a string.
1060 bool setStringValue (const char * relative_path, const char * value);
1064 * Set another node's value with no specified type.
1066 bool setUnspecifiedValue (const char * relative_path, const char * value);
1070 * Test whether another node is bound to an external data source.
1072 bool isTied (const char * relative_path) const;
1076 * Bind another node to an external bool source.
1078 bool tie (const char * relative_path, const SGRawValue<bool> &rawValue,
1079 bool useDefault = true);
1083 * Bind another node to an external int source.
1085 bool tie (const char * relative_path, const SGRawValue<int> &rawValue,
1086 bool useDefault = true);
1090 * Bind another node to an external long int source.
1092 bool tie (const char * relative_path, const SGRawValue<long> &rawValue,
1093 bool useDefault = true);
1097 * Bind another node to an external float source.
1099 bool tie (const char * relative_path, const SGRawValue<float> &rawValue,
1100 bool useDefault = true);
1104 * Bind another node to an external double source.
1106 bool tie (const char * relative_path, const SGRawValue<double> &rawValue,
1107 bool useDefault = true);
1111 * Bind another node to an external string source.
1113 bool tie (const char * relative_path, const SGRawValue<const char *> &rawValue,
1114 bool useDefault = true);
1118 * Unbind another node from any external data source.
1120 bool untie (const char * relative_path);
1124 * Add a change listener to the property.
1126 void addChangeListener (SGPropertyChangeListener * listener);
1130 * Fire a property change to all listeners.
1132 void firePropertyChange ()
1134 firePropertyChange(this);
1142 * Fire a property change with an explicit target node.
1144 void firePropertyChange (SGPropertyNode * node);
1148 * Protected constructor for making new nodes on demand.
1150 SGPropertyNode (const char * name, int index, SGPropertyNode * parent);
1155 // Get the raw value
1156 bool get_bool () const;
1157 int get_int () const;
1158 long get_long () const;
1159 float get_float () const;
1160 double get_double () const;
1161 const char * get_string () const;
1163 // Set the raw value
1164 bool set_bool (bool value);
1165 bool set_int (int value);
1166 bool set_long (long value);
1167 bool set_float (float value);
1168 bool set_double (double value);
1169 bool set_string (const char * value);
1173 * Clear any existing value and set the type to NONE.
1175 void clear_value ();
1179 * Get the value as a string.
1181 const char * make_string () const;
1185 * Trace a read access.
1187 void trace_read () const;
1191 * Trace a write access.
1193 void trace_write () const;
1197 * Increment reference counter
1199 void incrementRef();
1202 * Decrement reference counter
1206 friend class SGPropertyNode_ptr;
1209 mutable char _buffer[MAX_STRING_LEN+1];
1215 SGPropertyNode * _parent;
1216 vector<SGPropertyNode_ptr> _children;
1217 vector<SGPropertyNode_ptr> _removedChildren;
1218 hash_table * _path_cache;
1224 // The right kind of pointer...
1226 SGPropertyNode * alias;
1227 SGRawValue<bool> * bool_val;
1228 SGRawValue<int> * int_val;
1229 SGRawValue<long> * long_val;
1230 SGRawValue<float> * float_val;
1231 SGRawValue<double> * double_val;
1232 SGRawValue<const char *> * string_val;
1244 vector <SGPropertyChangeListener *> * _listeners;
1249 * A very simple hash table with no remove functionality.
1255 * An entry in a bucket in a hash table.
1261 virtual const char * get_key () { return _key; }
1262 virtual void set_key (const char * key);
1263 virtual SGPropertyNode * get_value () { return _value; }
1264 virtual void set_value (SGPropertyNode * value);
1267 SGPropertyNode * _value;
1272 * A bucket in a hash table.
1278 virtual entry * get_entry (const char * key, bool create = false);
1284 friend class bucket;
1287 virtual ~hash_table ();
1288 virtual SGPropertyNode * get (const char * key);
1289 virtual void put (const char * key, SGPropertyNode * value);
1292 unsigned int hashcode (const char * key);
1293 unsigned int _data_length;
1299 #endif // __PROPS_HXX