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)();
287 * The template type of a static setter function.
289 typedef void (*setter_t)(T);
292 * Explicit constructor.
294 * Create a new raw value bound to the getter and setter supplied.
296 * @param getter A static function for getting a value, or 0
297 * to read-disable the value.
298 * @param setter A static function for setting a value, or 0
299 * to write-disable the value.
301 SGRawValueFunctions (getter_t getter = 0, setter_t setter = 0)
302 : _getter(getter), _setter(setter) {}
307 virtual ~SGRawValueFunctions () {}
310 * Get the underlying value.
312 * This method will invoke the getter function to get a value.
313 * If no getter function was supplied, this method will always
314 * return the default value for the type.
316 virtual T getValue () const {
317 if (_getter) return (*_getter)();
318 else return SGRawValue<T>::DefaultValue;
322 * Set the underlying value.
324 * This method will invoke the setter function to change the
325 * underlying value. If no setter function was supplied, this
326 * method will return false.
328 virtual bool setValue (T value) {
329 if (_setter) { (*_setter)(value); return true; }
334 * Create a copy of this raw value, bound to the same functions.
336 virtual SGRawValue<T> * clone () const {
337 return new SGRawValueFunctions<T>(_getter,_setter);
347 * An indexed value bound to static functions.
349 * A read-only value will not have a setter; a write-only value will
350 * not have a getter. An indexed value is useful for binding one
351 * of a list of possible values (such as multiple engines for a
352 * plane). The index is hard-coded at creation time.
357 class SGRawValueFunctionsIndexed : public SGRawValue<T>
360 typedef T (*getter_t)(int);
361 typedef void (*setter_t)(int,T);
362 SGRawValueFunctionsIndexed (int index, getter_t getter = 0, setter_t setter = 0)
363 : _index(index), _getter(getter), _setter(setter) {}
364 virtual ~SGRawValueFunctionsIndexed () {}
365 virtual T getValue () const {
366 if (_getter) return (*_getter)(_index);
367 else return SGRawValue<T>::DefaultValue;
369 virtual bool setValue (T value) {
370 if (_setter) { (*_setter)(_index, value); return true; }
373 virtual SGRawValue<T> * clone () const {
374 return new SGRawValueFunctionsIndexed<T>(_index, _getter, _setter);
384 * A value managed through an object and access methods.
386 * A read-only value will not have a setter; a write-only value will
389 template <class C, class T>
390 class SGRawValueMethods : public SGRawValue<T>
393 typedef T (C::*getter_t)() const;
394 typedef void (C::*setter_t)(T);
395 SGRawValueMethods (C &obj, getter_t getter = 0, setter_t setter = 0)
396 : _obj(obj), _getter(getter), _setter(setter) {}
397 virtual ~SGRawValueMethods () {}
398 virtual T getValue () const {
399 if (_getter) { return (_obj.*_getter)(); }
400 else { return SGRawValue<T>::DefaultValue; }
402 virtual bool setValue (T value) {
403 if (_setter) { (_obj.*_setter)(value); return true; }
406 virtual SGRawValue<T> * clone () const {
407 return new SGRawValueMethods<C,T>(_obj, _getter, _setter);
417 * An indexed value managed through an object and access methods.
419 * A read-only value will not have a setter; a write-only value will
422 template <class C, class T>
423 class SGRawValueMethodsIndexed : public SGRawValue<T>
426 typedef T (C::*getter_t)(int) const;
427 typedef void (C::*setter_t)(int, T);
428 SGRawValueMethodsIndexed (C &obj, int index,
429 getter_t getter = 0, setter_t setter = 0)
430 : _obj(obj), _index(index), _getter(getter), _setter(setter) {}
431 virtual ~SGRawValueMethodsIndexed () {}
432 virtual T getValue () const {
433 if (_getter) { return (_obj.*_getter)(_index); }
434 else { return SGRawValue<T>::DefaultValue; }
436 virtual bool setValue (T value) {
437 if (_setter) { (_obj.*_setter)(_index, value); return true; }
440 virtual SGRawValue<T> * clone () const {
441 return new SGRawValueMethodsIndexed<C,T>(_obj, _index, _getter, _setter);
452 * The smart pointer that manage reference counting
454 class SGPropertyNode;
455 class SGPropertyNode_ptr
460 * Default constructor
462 SGPropertyNode_ptr();
467 SGPropertyNode_ptr( const SGPropertyNode_ptr &r );
470 * Constructor from a pointer to a node
472 SGPropertyNode_ptr( SGPropertyNode *p );
477 ~SGPropertyNode_ptr();
480 * Assignement operator
482 SGPropertyNode_ptr &operator=( const SGPropertyNode_ptr &r );
485 * Pointer access operator
487 SGPropertyNode *operator->();
490 * Pointer access operator (const)
492 const SGPropertyNode *operator->() const;
495 * Conversion to SGPropertyNode * operator
497 operator SGPropertyNode *();
500 * Conversion to const SGPropertyNode * operator
502 operator const SGPropertyNode *() const;
505 * Return the pointer.
507 SGPropertyNode * ptr () { return _ptr; }
516 SGPropertyNode *_ptr;
522 * The property change listener interface.
524 * <p>Any class that needs to listen for property changes must implement
525 * this interface.</p>
527 class SGPropertyChangeListener
530 virtual ~SGPropertyChangeListener ();
531 virtual void valueChanged (SGPropertyNode * node);
532 virtual void childAdded (SGPropertyNode * parent, SGPropertyNode * child);
533 virtual void childRemoved (SGPropertyNode * parent, SGPropertyNode * child);
536 friend class SGPropertyNode;
537 virtual void register_property (SGPropertyNode * node);
538 virtual void unregister_property (SGPropertyNode * node);
541 vector<SGPropertyNode *> _properties;
547 * A node in a property tree.
557 MAX_STRING_LEN = 1024
561 * Property value types.
577 * Access mode attributes.
579 * <p>The ARCHIVE attribute is strictly advisory, and controls
580 * whether the property should normally be saved and restored.</p>
594 * Last used attribute
595 * Update as needed when enum Attribute is changed
597 static const int LAST_USED_ATTRIBUTE;
601 * Default constructor.
609 SGPropertyNode (const SGPropertyNode &node);
615 virtual ~SGPropertyNode ();
624 * Test whether this node contains a primitive leaf value.
626 bool hasValue () const { return (_type != NONE); }
630 * Get the node's simple (XML) name.
632 const char * getName () const { return _name; }
636 * Get the node's pretty display name, with subscript when needed.
638 const char * getDisplayName (bool simplify = false) const;
642 * Get the node's integer index.
644 int getIndex () const { return _index; }
648 * Get a non-const pointer to the node's parent.
650 SGPropertyNode * getParent () { return _parent; }
654 * Get a const pointer to the node's parent.
656 const SGPropertyNode * getParent () const { return _parent; }
665 * Get the number of child nodes.
667 int nChildren () const { return _children.size(); }
671 * Get a child node by position (*NOT* index).
673 SGPropertyNode * getChild (int position);
677 * Get a const child node by position (*NOT* index).
679 const SGPropertyNode * getChild (int position) const;
683 * Test whether a named child exists.
685 bool hasChild (const char * name, int index = 0) const
687 return (getChild(name, index) != 0);
692 * Get a child node by name and index.
694 SGPropertyNode * getChild (const char * name, int index = 0,
695 bool create = false);
699 * Get a const child node by name and index.
701 const SGPropertyNode * getChild (const char * name, int index = 0) const;
705 * Get a vector of all children with the specified name.
707 vector<SGPropertyNode_ptr> getChildren (const char * name) const;
711 * Remove child by position.
713 SGPropertyNode_ptr removeChild (int pos, bool keep = true);
717 * Remove a child node
719 SGPropertyNode_ptr removeChild (const char * name, int index = 0,
723 * Remove all children with the specified name.
725 vector<SGPropertyNode_ptr> removeChildren (const char * name,
735 * Alias this node's leaf value to another's.
737 bool alias (SGPropertyNode * target);
741 * Alias this node's leaf value to another's by relative path.
743 bool alias (const char * path);
747 * Remove any alias for this node.
753 * Test whether the node's leaf value is aliased to another's.
755 bool isAlias () const { return (_type == ALIAS); }
759 * Get a non-const pointer to the current alias target, if any.
761 SGPropertyNode * getAliasTarget ();
765 * Get a const pointer to the current alias target, if any.
767 const SGPropertyNode * getAliasTarget () const;
776 * Get the path to this node from the root.
778 const char * getPath (bool simplify = false) const;
782 * Get a pointer to the root node.
784 SGPropertyNode * getRootNode ();
788 * Get a const pointer to the root node.
790 const SGPropertyNode * getRootNode () const;
794 * Get a pointer to another node by relative path.
796 SGPropertyNode * getNode (const char * relative_path, bool create = false);
800 * Get a pointer to another node by relative path.
802 * This method leaves the index off the last member of the path,
803 * so that the user can specify it separately (and save some
804 * string building). For example, getNode("/bar[1]/foo", 3) is
805 * exactly equivalent to getNode("bar[1]/foo[3]"). The index
806 * provided overrides any given in the path itself for the last
809 SGPropertyNode * getNode (const char * relative_path, int index,
810 bool create = false);
814 * Get a const pointer to another node by relative path.
816 const SGPropertyNode * getNode (const char * relative_path) const;
820 * Get a const pointer to another node by relative path.
822 * This method leaves the index off the last member of the path,
823 * so that the user can specify it separate.
825 const SGPropertyNode * getNode (const char * relative_path,
834 * Check a single mode attribute for the property node.
836 bool getAttribute (Attribute attr) const { return ((_attr & attr) != 0); }
840 * Set a single mode attribute for the property node.
842 void setAttribute (Attribute attr, bool state) {
843 (state ? _attr |= attr : _attr &= ~attr);
848 * Get all of the mode attributes for the property node.
850 int getAttributes () const { return _attr; }
854 * Set all of the mode attributes for the property node.
856 void setAttributes (int attr) { _attr = attr; }
860 // Leaf Value (primitive).
865 * Get the type of leaf value, if any, for this node.
867 Type getType () const;
871 * Get a bool value for this node.
873 bool getBoolValue () const;
877 * Get an int value for this node.
879 int getIntValue () const;
883 * Get a long int value for this node.
885 long getLongValue () const;
889 * Get a float value for this node.
891 float getFloatValue () const;
895 * Get a double value for this node.
897 double getDoubleValue () const;
901 * Get a string value for this node.
903 const char * getStringValue () const;
908 * Set a bool value for this node.
910 bool setBoolValue (bool value);
914 * Set an int value for this node.
916 bool setIntValue (int value);
920 * Set a long int value for this node.
922 bool setLongValue (long value);
926 * Set a float value for this node.
928 bool setFloatValue (float value);
932 * Set a double value for this node.
934 bool setDoubleValue (double value);
938 * Set a string value for this node.
940 bool setStringValue (const char * value);
944 * Set a value of unspecified type for this node.
946 bool setUnspecifiedValue (const char * value);
955 * Test whether this node is bound to an external data source.
957 bool isTied () const { return _tied; }
961 * Bind this node to an external bool source.
963 bool tie (const SGRawValue<bool> &rawValue, bool useDefault = true);
967 * Bind this node to an external int source.
969 bool tie (const SGRawValue<int> &rawValue, bool useDefault = true);
973 * Bind this node to an external long int source.
975 bool tie (const SGRawValue<long> &rawValue, bool useDefault = true);
979 * Bind this node to an external float source.
981 bool tie (const SGRawValue<float> &rawValue, bool useDefault = true);
985 * Bind this node to an external double source.
987 bool tie (const SGRawValue<double> &rawValue, bool useDefault = true);
991 * Bind this node to an external string source.
993 bool tie (const SGRawValue<const char *> &rawValue, bool useDefault = true);
997 * Unbind this node from any external data source.
1003 // Convenience methods using paths.
1004 // TODO: add attribute methods
1009 * Get another node's type.
1011 Type getType (const char * relative_path) const;
1015 * Test whether another node has a leaf value.
1017 bool hasValue (const char * relative_path) const;
1021 * Get another node's value as a bool.
1023 bool getBoolValue (const char * relative_path,
1024 bool defaultValue = false) const;
1028 * Get another node's value as an int.
1030 int getIntValue (const char * relative_path,
1031 int defaultValue = 0) const;
1035 * Get another node's value as a long int.
1037 long getLongValue (const char * relative_path,
1038 long defaultValue = 0L) const;
1042 * Get another node's value as a float.
1044 float getFloatValue (const char * relative_path,
1045 float defaultValue = 0.0) const;
1049 * Get another node's value as a double.
1051 double getDoubleValue (const char * relative_path,
1052 double defaultValue = 0.0L) const;
1056 * Get another node's value as a string.
1058 const char * getStringValue (const char * relative_path,
1059 const char * defaultValue = "") const;
1063 * Set another node's value as a bool.
1065 bool setBoolValue (const char * relative_path, bool value);
1069 * Set another node's value as an int.
1071 bool setIntValue (const char * relative_path, int value);
1075 * Set another node's value as a long int.
1077 bool setLongValue (const char * relative_path, long value);
1081 * Set another node's value as a float.
1083 bool setFloatValue (const char * relative_path, float value);
1087 * Set another node's value as a double.
1089 bool setDoubleValue (const char * relative_path, double value);
1093 * Set another node's value as a string.
1095 bool setStringValue (const char * relative_path, const char * value);
1099 * Set another node's value with no specified type.
1101 bool setUnspecifiedValue (const char * relative_path, const char * value);
1105 * Test whether another node is bound to an external data source.
1107 bool isTied (const char * relative_path) const;
1111 * Bind another node to an external bool source.
1113 bool tie (const char * relative_path, const SGRawValue<bool> &rawValue,
1114 bool useDefault = true);
1118 * Bind another node to an external int source.
1120 bool tie (const char * relative_path, const SGRawValue<int> &rawValue,
1121 bool useDefault = true);
1125 * Bind another node to an external long int source.
1127 bool tie (const char * relative_path, const SGRawValue<long> &rawValue,
1128 bool useDefault = true);
1132 * Bind another node to an external float source.
1134 bool tie (const char * relative_path, const SGRawValue<float> &rawValue,
1135 bool useDefault = true);
1139 * Bind another node to an external double source.
1141 bool tie (const char * relative_path, const SGRawValue<double> &rawValue,
1142 bool useDefault = true);
1146 * Bind another node to an external string source.
1148 bool tie (const char * relative_path, const SGRawValue<const char *> &rawValue,
1149 bool useDefault = true);
1153 * Unbind another node from any external data source.
1155 bool untie (const char * relative_path);
1159 * Add a change listener to the property.
1161 void addChangeListener (SGPropertyChangeListener * listener);
1165 * Remove a change listener from the property.
1167 void removeChangeListener (SGPropertyChangeListener * listener);
1171 * Fire a value change event to all listeners.
1173 void fireValueChanged ();
1177 * Fire a child-added event to all listeners.
1179 void fireChildAdded (SGPropertyNode * child);
1183 * Fire a child-removed event to all listeners.
1185 void fireChildRemoved (SGPropertyNode * child);
1189 * Clear any existing value and set the type to NONE.
1196 void fireValueChanged (SGPropertyNode * node);
1197 void fireChildAdded (SGPropertyNode * parent, SGPropertyNode * child);
1198 void fireChildRemoved (SGPropertyNode * parent, SGPropertyNode * child);
1201 * Protected constructor for making new nodes on demand.
1203 SGPropertyNode (const char * name, int index, SGPropertyNode * parent);
1208 // Get the raw value
1209 bool get_bool () const;
1210 int get_int () const;
1211 long get_long () const;
1212 float get_float () const;
1213 double get_double () const;
1214 const char * get_string () const;
1216 // Set the raw value
1217 bool set_bool (bool value);
1218 bool set_int (int value);
1219 bool set_long (long value);
1220 bool set_float (float value);
1221 bool set_double (double value);
1222 bool set_string (const char * value);
1226 * Get the value as a string.
1228 const char * make_string () const;
1232 * Trace a read access.
1234 void trace_read () const;
1238 * Trace a write access.
1240 void trace_write () const;
1244 * Increment reference counter
1246 void incrementRef();
1249 * Decrement reference counter
1253 friend class SGPropertyNode_ptr;
1256 mutable char _buffer[MAX_STRING_LEN+1];
1261 mutable char * _display_name;
1263 SGPropertyNode * _parent;
1264 vector<SGPropertyNode_ptr> _children;
1265 vector<SGPropertyNode_ptr> _removedChildren;
1266 mutable char * _path;
1267 hash_table * _path_cache;
1273 // The right kind of pointer...
1275 SGPropertyNode * alias;
1276 SGRawValue<bool> * bool_val;
1277 SGRawValue<int> * int_val;
1278 SGRawValue<long> * long_val;
1279 SGRawValue<float> * float_val;
1280 SGRawValue<double> * double_val;
1281 SGRawValue<const char *> * string_val;
1293 vector <SGPropertyChangeListener *> * _listeners;
1298 * A very simple hash table with no remove functionality.
1304 * An entry in a bucket in a hash table.
1310 const char * get_key () { return _key; }
1311 void set_key (const char * key);
1312 SGPropertyNode * get_value () { return _value; }
1313 void set_value (SGPropertyNode * value);
1316 SGPropertyNode * _value;
1321 * A bucket in a hash table.
1327 entry * get_entry (const char * key, bool create = false);
1328 void erase(const char * key);
1334 friend class bucket;
1338 SGPropertyNode * get (const char * key);
1339 void put (const char * key, SGPropertyNode * value);
1340 void erase(const char * key);
1343 unsigned int hashcode (const char * key);
1344 unsigned int _data_length;
1350 #endif // __PROPS_HXX