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 SG_USING_STD(istream);
40 SG_USING_STD(ostream);
46 #pragma warn A sloppy coder has defined NONE as a macro!
51 #pragma warn A sloppy coder has defined ALIAS as a macro!
56 #pragma warn A sloppy coder has defined UNSPECIFIED as a macro!
61 #pragma warn A sloppy coder has defined BOOL as a macro!
66 #pragma warn A sloppy coder has defined INT as a macro!
71 #pragma warn A sloppy coder has defined LONG as a macro!
76 #pragma warn A sloppy coder has defined FLOAT as a macro!
81 #pragma warn A sloppy coder has defined DOUBLE as a macro!
86 #pragma warn A sloppy coder has defined STRING as a macro!
92 ////////////////////////////////////////////////////////////////////////
95 // This is the mechanism that information-providing routines can
96 // use to link their own values to the property manager. Any
97 // SGValue can be tied to a raw value and then untied again.
99 // Note: we are forced to use inlined methods here to ensure
100 // that the templates will be instantiated. We're probably taking
101 // a small performance hit for that.
102 ////////////////////////////////////////////////////////////////////////
106 * Abstract base class for a raw value.
108 * <p>The property manager is implemented in two layers. The {@link
109 * SGPropertyNode} is the highest and most abstract layer,
110 * representing an LValue/RValue pair: it records the position of the
111 * property in the property tree and contains facilities for
112 * navigation to other nodes. It is guaranteed to be persistent: the
113 * {@link SGPropertyNode} will not change during a session, even if
114 * the property is bound and unbound multiple times.</p>
116 * <p>When the property value is not managed internally in the
117 * SGPropertyNode, the SGPropertyNode will contain a reference to an
118 * SGRawValue (this class), which provides an abstract way to get,
119 * set, and clone the underlying value. The SGRawValue may change
120 * frequently during a session as a value is retyped or bound and
121 * unbound to various data source, but the abstract SGPropertyNode
122 * layer insulates the application from those changes. The raw value
123 * contains no facilities for data binding or for type conversion: it
124 * is simply the abstraction of a primitive data type (or a compound
125 * data type, in the case of a string).</p>
127 * <p>The SGPropertyNode class always keeps a *copy* of a raw value,
128 * not the original one passed to it; if you override a derived class
129 * but do not replace the {@link #clone} method, strange things will
132 * <p>All derived SGRawValue classes must implement {@link #getValue},
133 * {@link #setValue}, and {@link #clone} for the appropriate type.</p>
135 * @see SGPropertyNode
136 * @see SGRawValuePointer
137 * @see SGRawValueFunctions
138 * @see SGRawValueFunctionsIndexed
139 * @see SGRawValueMethods
140 * @see SGRawValueMethodsIndexed
148 * The default underlying value for this type.
150 * Every raw value has a default; the default is false for a
151 * boolean, 0 for the various numeric values, and "" for a string.
152 * If additional types of raw values are added in the future, they
153 * may need different kinds of default values (such as epoch for a
154 * date type). The default value is used when creating new values.
156 static const T DefaultValue; // Default for this kind of raw value.
162 * Use the default value for this type.
170 virtual ~SGRawValue () {}
174 * Return the underlying value.
176 * @return The actual value for the property.
179 virtual T getValue () const = 0;
183 * Assign a new underlying value.
185 * If the new value cannot be set (because this is a read-only
186 * raw value, or because the new value is not acceptable for
187 * some reason) this method returns false and leaves the original
190 * @param value The actual value for the property.
191 * @return true if the value was set successfully, false otherwise.
194 virtual bool setValue (T value) = 0;
198 * Create a new deep copy of this raw value.
200 * The copy will contain its own version of the underlying value
203 * @return A deep copy of the current object.
205 virtual SGRawValue * clone () const = 0;
210 * A raw value bound to a pointer.
212 * This is the most efficient way to tie an external value, but also
213 * the most dangerous, because there is no way for the supplier to
214 * perform bounds checking and derived calculations except by polling
215 * the variable to see if it has changed. There is no default
216 * constructor, because this class would be meaningless without a
220 class SGRawValuePointer : public SGRawValue<T>
225 * Explicit pointer constructor.
227 * Create a new raw value bound to the value of the variable
228 * referenced by the pointer.
230 * @param ptr The pointer to the variable to which this raw value
233 SGRawValuePointer (T * ptr) : _ptr(ptr) {}
238 virtual ~SGRawValuePointer () {}
241 * Get the underlying value.
243 * This method will dereference the pointer and return the
246 virtual T getValue () const { return *_ptr; }
249 * Set the underlying value.
251 * This method will dereference the pointer and change the
254 virtual bool setValue (T value) { *_ptr = value; return true; }
257 * Create a copy of this raw value.
259 * The copy will use the same external pointer as the original.
261 virtual SGRawValue<T> * clone () const {
262 return new SGRawValuePointer<T>(_ptr);
271 * A value managed through static functions.
273 * A read-only value will not have a setter; a write-only value will
277 class SGRawValueFunctions : public SGRawValue<T>
282 * The template type of a static getter function.
284 typedef T (*getter_t)();
285 typedef T (*getter_td)(void*);
288 * The template type of a static setter function.
290 typedef void (*setter_t)(T);
291 typedef void (*setter_td)(T,void*);
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),
305 _getter_d(0), _setter_d(0), _data(0) {}
308 * Explicit constructor.
310 * Create a new raw value bound to the getter and setter supplied.
312 * @param getter A static function for getting a value, or 0
313 * to read-disable the value.
314 * @param setter A static function for setting a value, or 0
315 * to write-disable the value.
316 * @param data A pointer to user data which gets passed to the
317 * getter and setter functions. This could be used to pass the this
318 * pointer to the callback function.
320 SGRawValueFunctions (getter_td getter = 0, setter_td setter = 0,
322 : _setter(0), _getter(0),
323 _getter_d(getter), _setter_d(setter), _data(data) {}
328 virtual ~SGRawValueFunctions () {}
331 * Get the underlying value.
333 * This method will invoke the getter function to get a value.
334 * If no getter function was supplied, this method will always
335 * return the default value for the type.
337 virtual T getValue () const {
338 if (_getter_d) return (*_getter_d)(_data);
339 else if (_getter) return (*_getter)();
340 else return SGRawValue<T>::DefaultValue;
344 * Set the underlying value.
346 * This method will invoke the setter function to change the
347 * underlying value. If no setter function was supplied, this
348 * method will return false.
350 virtual bool setValue (T value) {
351 if (_setter_d) { (*_setter_d)(value,_data); return true; }
352 else if (_setter) { (*_setter)(value); return true; }
357 * Create a copy of this raw value, bound to the same functions.
359 virtual SGRawValue<T> * clone () const {
361 return new SGRawValueFunctions<T>(_getter_d,_setter_d,_data);
363 return new SGRawValueFunctions<T>(_getter,_setter);
376 * An indexed value bound to static functions.
378 * A read-only value will not have a setter; a write-only value will
379 * not have a getter. An indexed value is useful for binding one
380 * of a list of possible values (such as multiple engines for a
381 * plane). The index is hard-coded at creation time.
386 class SGRawValueFunctionsIndexed : public SGRawValue<T>
389 typedef T (*getter_t)(int);
390 typedef T (*getter_td)(int,void*);
391 typedef void (*setter_t)(int,T);
392 typedef void (*setter_td)(int,T,void*);
393 SGRawValueFunctionsIndexed (int index, getter_t getter = 0, setter_t setter =
395 : _index(index), _getter(getter), _setter(setter),
396 _getter_d(0), _setter_d(0),_data(0) {}
397 SGRawValueFunctionsIndexed (int index, getter_td getter = 0, setter_td setter = 0, void *data = NULL)
398 : _index(index), _setter(0), _getter(0),
399 _getter_d(getter), _setter_d(setter), _data(data) {}
400 virtual ~SGRawValueFunctionsIndexed () {}
401 virtual T getValue () const {
402 if (_getter_d) return (*_getter_d)(_index,_data);
403 else if (_getter) return (*_getter)(_index);
404 else return SGRawValue<T>::DefaultValue;
406 virtual bool setValue (T value) {
407 if (_setter_d) { (*_setter_d)(_index, value, _data); return true; }
408 else if (_setter) { (*_setter)(_index, value); return true; }
411 virtual SGRawValue<T> * clone () const {
413 return new SGRawValueFunctionsIndexed<T>(_index,_getter_d,_setter_d,_data);
415 return new SGRawValueFunctionsIndexed<T>(_index,_getter,_setter);
428 * A value managed through an object and access methods.
430 * A read-only value will not have a setter; a write-only value will
433 template <class C, class T>
434 class SGRawValueMethods : public SGRawValue<T>
437 typedef T (C::*getter_t)() const;
438 typedef void (C::*setter_t)(T);
439 SGRawValueMethods (C &obj, getter_t getter = 0, setter_t setter = 0)
440 : _obj(obj), _getter(getter), _setter(setter) {}
441 virtual ~SGRawValueMethods () {}
442 virtual T getValue () const {
443 if (_getter) { return (_obj.*_getter)(); }
444 else { return SGRawValue<T>::DefaultValue; }
446 virtual bool setValue (T value) {
447 if (_setter) { (_obj.*_setter)(value); return true; }
450 virtual SGRawValue<T> * clone () const {
451 return new SGRawValueMethods<C,T>(_obj, _getter, _setter);
461 * An indexed value managed through an object and access methods.
463 * A read-only value will not have a setter; a write-only value will
466 template <class C, class T>
467 class SGRawValueMethodsIndexed : public SGRawValue<T>
470 typedef T (C::*getter_t)(int) const;
471 typedef void (C::*setter_t)(int, T);
472 SGRawValueMethodsIndexed (C &obj, int index,
473 getter_t getter = 0, setter_t setter = 0)
474 : _obj(obj), _index(index), _getter(getter), _setter(setter) {}
475 virtual ~SGRawValueMethodsIndexed () {}
476 virtual T getValue () const {
477 if (_getter) { return (_obj.*_getter)(_index); }
478 else { return SGRawValue<T>::DefaultValue; }
480 virtual bool setValue (T value) {
481 if (_setter) { (_obj.*_setter)(_index, value); return true; }
484 virtual SGRawValue<T> * clone () const {
485 return new SGRawValueMethodsIndexed<C,T>(_obj, _index, _getter, _setter);
496 * The smart pointer that manage reference counting
498 class SGPropertyNode;
499 class SGPropertyNode_ptr
504 * Default constructor
506 SGPropertyNode_ptr();
511 SGPropertyNode_ptr( const SGPropertyNode_ptr &r );
514 * Constructor from a pointer to a node
516 SGPropertyNode_ptr( SGPropertyNode *p );
521 ~SGPropertyNode_ptr();
524 * Assignement operator
526 SGPropertyNode_ptr &operator=( const SGPropertyNode_ptr &r );
529 * Pointer access operator
531 SGPropertyNode *operator->();
534 * Pointer access operator (const)
536 const SGPropertyNode *operator->() const;
539 * Conversion to SGPropertyNode * operator
541 operator SGPropertyNode *();
544 * Conversion to const SGPropertyNode * operator
546 operator const SGPropertyNode *() const;
549 * Return the pointer.
551 SGPropertyNode * ptr () { return _ptr; }
560 SGPropertyNode *_ptr;
566 * The property change listener interface.
568 * <p>Any class that needs to listen for property changes must implement
569 * this interface.</p>
571 class SGPropertyChangeListener
574 virtual ~SGPropertyChangeListener ();
575 virtual void valueChanged (SGPropertyNode * node);
576 virtual void childAdded (SGPropertyNode * parent, SGPropertyNode * child);
577 virtual void childRemoved (SGPropertyNode * parent, SGPropertyNode * child);
580 friend class SGPropertyNode;
581 virtual void register_property (SGPropertyNode * node);
582 virtual void unregister_property (SGPropertyNode * node);
585 vector<SGPropertyNode *> _properties;
591 * A node in a property tree.
601 MAX_STRING_LEN = 1024
605 * Property value types.
621 * Access mode attributes.
623 * <p>The ARCHIVE attribute is strictly advisory, and controls
624 * whether the property should normally be saved and restored.</p>
637 * Last used attribute
638 * Update as needed when enum Attribute is changed
640 static const int LAST_USED_ATTRIBUTE;
644 * Default constructor.
652 SGPropertyNode (const SGPropertyNode &node);
658 virtual ~SGPropertyNode ();
667 * Test whether this node contains a primitive leaf value.
669 bool hasValue () const { return (_type != NONE); }
673 * Get the node's simple (XML) name.
675 const char * getName () const { return _name; }
679 * Get the node's pretty display name, with subscript when needed.
681 const char * getDisplayName (bool simplify = false) const;
685 * Get the node's integer index.
687 int getIndex () const { return _index; }
691 * Get a non-const pointer to the node's parent.
693 SGPropertyNode * getParent () { return _parent; }
697 * Get a const pointer to the node's parent.
699 const SGPropertyNode * getParent () const { return _parent; }
708 * Get the number of child nodes.
710 int nChildren () const { return _children.size(); }
714 * Get a child node by position (*NOT* index).
716 SGPropertyNode * getChild (int position);
720 * Get a const child node by position (*NOT* index).
722 const SGPropertyNode * getChild (int position) const;
726 * Test whether a named child exists.
728 bool hasChild (const char * name, int index = 0) const
730 return (getChild(name, index) != 0);
735 * Get a child node by name and index.
737 SGPropertyNode * getChild (const char * name, int index = 0,
738 bool create = false);
742 * Get a const child node by name and index.
744 const SGPropertyNode * getChild (const char * name, int index = 0) const;
748 * Get a vector of all children with the specified name.
750 vector<SGPropertyNode_ptr> getChildren (const char * name) const;
754 * Remove a child node
756 SGPropertyNode_ptr removeChild (const char * name, int index = 0,
766 * Alias this node's leaf value to another's.
768 bool alias (SGPropertyNode * target);
772 * Alias this node's leaf value to another's by relative path.
774 bool alias (const char * path);
778 * Remove any alias for this node.
784 * Test whether the node's leaf value is aliased to another's.
786 bool isAlias () const { return (_type == ALIAS); }
790 * Get a non-const pointer to the current alias target, if any.
792 SGPropertyNode * getAliasTarget ();
796 * Get a const pointer to the current alias target, if any.
798 const SGPropertyNode * getAliasTarget () const;
807 * Get the path to this node from the root.
809 const char * getPath (bool simplify = false) const;
813 * Get a pointer to the root node.
815 SGPropertyNode * getRootNode ();
819 * Get a const pointer to the root node.
821 const SGPropertyNode * getRootNode () const;
825 * Get a pointer to another node by relative path.
827 SGPropertyNode * getNode (const char * relative_path, bool create = false);
831 * Get a pointer to another node by relative path.
833 * This method leaves the index off the last member of the path,
834 * so that the user can specify it separately (and save some
835 * string building). For example, getNode("/bar[1]/foo", 3) is
836 * exactly equivalent to getNode("bar[1]/foo[3]"). The index
837 * provided overrides any given in the path itself for the last
840 SGPropertyNode * getNode (const char * relative_path, int index,
841 bool create = false);
845 * Get a const pointer to another node by relative path.
847 const SGPropertyNode * getNode (const char * relative_path) const;
851 * Get a const pointer to another node by relative path.
853 * This method leaves the index off the last member of the path,
854 * so that the user can specify it separate.
856 const SGPropertyNode * getNode (const char * relative_path,
865 * Check a single mode attribute for the property node.
867 bool getAttribute (Attribute attr) const { return ((_attr & attr) != 0); }
871 * Set a single mode attribute for the property node.
873 void setAttribute (Attribute attr, bool state) {
874 (state ? _attr |= attr : _attr &= ~attr);
879 * Get all of the mode attributes for the property node.
881 int getAttributes () const { return _attr; }
885 * Set all of the mode attributes for the property node.
887 void setAttributes (int attr) { _attr = attr; }
891 // Leaf Value (primitive).
896 * Get the type of leaf value, if any, for this node.
898 Type getType () const;
902 * Get a bool value for this node.
904 bool getBoolValue () const;
908 * Get an int value for this node.
910 int getIntValue () const;
914 * Get a long int value for this node.
916 long getLongValue () const;
920 * Get a float value for this node.
922 float getFloatValue () const;
926 * Get a double value for this node.
928 double getDoubleValue () const;
932 * Get a string value for this node.
934 const char * getStringValue () const;
939 * Set a bool value for this node.
941 bool setBoolValue (bool value);
945 * Set an int value for this node.
947 bool setIntValue (int value);
951 * Set a long int value for this node.
953 bool setLongValue (long value);
957 * Set a float value for this node.
959 bool setFloatValue (float value);
963 * Set a double value for this node.
965 bool setDoubleValue (double value);
969 * Set a string value for this node.
971 bool setStringValue (const char * value);
975 * Set a value of unspecified type for this node.
977 bool setUnspecifiedValue (const char * value);
986 * Test whether this node is bound to an external data source.
988 bool isTied () const { return _tied; }
992 * Bind this node to an external bool source.
994 bool tie (const SGRawValue<bool> &rawValue, bool useDefault = true);
998 * Bind this node to an external int source.
1000 bool tie (const SGRawValue<int> &rawValue, bool useDefault = true);
1004 * Bind this node to an external long int source.
1006 bool tie (const SGRawValue<long> &rawValue, bool useDefault = true);
1010 * Bind this node to an external float source.
1012 bool tie (const SGRawValue<float> &rawValue, bool useDefault = true);
1016 * Bind this node to an external double source.
1018 bool tie (const SGRawValue<double> &rawValue, bool useDefault = true);
1022 * Bind this node to an external string source.
1024 bool tie (const SGRawValue<const char *> &rawValue, bool useDefault = true);
1028 * Unbind this node from any external data source.
1034 // Convenience methods using paths.
1035 // TODO: add attribute methods
1040 * Get another node's type.
1042 Type getType (const char * relative_path) const;
1046 * Test whether another node has a leaf value.
1048 bool hasValue (const char * relative_path) const;
1052 * Get another node's value as a bool.
1054 bool getBoolValue (const char * relative_path,
1055 bool defaultValue = false) const;
1059 * Get another node's value as an int.
1061 int getIntValue (const char * relative_path,
1062 int defaultValue = 0) const;
1066 * Get another node's value as a long int.
1068 long getLongValue (const char * relative_path,
1069 long defaultValue = 0L) const;
1073 * Get another node's value as a float.
1075 float getFloatValue (const char * relative_path,
1076 float defaultValue = 0.0) const;
1080 * Get another node's value as a double.
1082 double getDoubleValue (const char * relative_path,
1083 double defaultValue = 0.0L) const;
1087 * Get another node's value as a string.
1089 const char * getStringValue (const char * relative_path,
1090 const char * defaultValue = "") const;
1094 * Set another node's value as a bool.
1096 bool setBoolValue (const char * relative_path, bool value);
1100 * Set another node's value as an int.
1102 bool setIntValue (const char * relative_path, int value);
1106 * Set another node's value as a long int.
1108 bool setLongValue (const char * relative_path, long value);
1112 * Set another node's value as a float.
1114 bool setFloatValue (const char * relative_path, float value);
1118 * Set another node's value as a double.
1120 bool setDoubleValue (const char * relative_path, double value);
1124 * Set another node's value as a string.
1126 bool setStringValue (const char * relative_path, const char * value);
1130 * Set another node's value with no specified type.
1132 bool setUnspecifiedValue (const char * relative_path, const char * value);
1136 * Test whether another node is bound to an external data source.
1138 bool isTied (const char * relative_path) const;
1142 * Bind another node to an external bool source.
1144 bool tie (const char * relative_path, const SGRawValue<bool> &rawValue,
1145 bool useDefault = true);
1149 * Bind another node to an external int source.
1151 bool tie (const char * relative_path, const SGRawValue<int> &rawValue,
1152 bool useDefault = true);
1156 * Bind another node to an external long int source.
1158 bool tie (const char * relative_path, const SGRawValue<long> &rawValue,
1159 bool useDefault = true);
1163 * Bind another node to an external float source.
1165 bool tie (const char * relative_path, const SGRawValue<float> &rawValue,
1166 bool useDefault = true);
1170 * Bind another node to an external double source.
1172 bool tie (const char * relative_path, const SGRawValue<double> &rawValue,
1173 bool useDefault = true);
1177 * Bind another node to an external string source.
1179 bool tie (const char * relative_path, const SGRawValue<const char *> &rawValue,
1180 bool useDefault = true);
1184 * Unbind another node from any external data source.
1186 bool untie (const char * relative_path);
1190 * Add a change listener to the property.
1192 void addChangeListener (SGPropertyChangeListener * listener);
1196 * Remove a change listener from the property.
1198 void removeChangeListener (SGPropertyChangeListener * listener);
1202 * Fire a value change event to all listeners.
1204 void fireValueChanged ();
1208 * Fire a child-added event to all listeners.
1210 void fireChildAdded (SGPropertyNode * child);
1214 * Fire a child-removed event to all listeners.
1216 void fireChildRemoved (SGPropertyNode * child);
1221 void fireValueChanged (SGPropertyNode * node);
1222 void fireChildAdded (SGPropertyNode * parent, SGPropertyNode * child);
1223 void fireChildRemoved (SGPropertyNode * parent, SGPropertyNode * child);
1226 * Protected constructor for making new nodes on demand.
1228 SGPropertyNode (const char * name, int index, SGPropertyNode * parent);
1233 // Get the raw value
1234 bool get_bool () const;
1235 int get_int () const;
1236 long get_long () const;
1237 float get_float () const;
1238 double get_double () const;
1239 const char * get_string () const;
1241 // Set the raw value
1242 bool set_bool (bool value);
1243 bool set_int (int value);
1244 bool set_long (long value);
1245 bool set_float (float value);
1246 bool set_double (double value);
1247 bool set_string (const char * value);
1251 * Clear any existing value and set the type to NONE.
1253 void clear_value ();
1257 * Get the value as a string.
1259 const char * make_string () const;
1263 * Trace a read access.
1265 void trace_read () const;
1269 * Trace a write access.
1271 void trace_write () const;
1275 * Increment reference counter
1277 void incrementRef();
1280 * Decrement reference counter
1284 friend class SGPropertyNode_ptr;
1287 mutable char _buffer[MAX_STRING_LEN+1];
1292 mutable char * _display_name;
1294 SGPropertyNode * _parent;
1295 vector<SGPropertyNode_ptr> _children;
1296 vector<SGPropertyNode_ptr> _removedChildren;
1297 mutable char * _path;
1298 hash_table * _path_cache;
1304 // The right kind of pointer...
1306 SGPropertyNode * alias;
1307 SGRawValue<bool> * bool_val;
1308 SGRawValue<int> * int_val;
1309 SGRawValue<long> * long_val;
1310 SGRawValue<float> * float_val;
1311 SGRawValue<double> * double_val;
1312 SGRawValue<const char *> * string_val;
1324 vector <SGPropertyChangeListener *> * _listeners;
1329 * A very simple hash table with no remove functionality.
1335 * An entry in a bucket in a hash table.
1341 virtual const char * get_key () { return _key; }
1342 virtual void set_key (const char * key);
1343 virtual SGPropertyNode * get_value () { return _value; }
1344 virtual void set_value (SGPropertyNode * value);
1347 SGPropertyNode * _value;
1352 * A bucket in a hash table.
1358 virtual entry * get_entry (const char * key, bool create = false);
1364 friend class bucket;
1367 virtual ~hash_table ();
1368 virtual SGPropertyNode * get (const char * key);
1369 virtual void put (const char * key, SGPropertyNode * value);
1372 unsigned int hashcode (const char * key);
1373 unsigned int _data_length;
1379 #endif // __PROPS_HXX