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;
513 SGPropertyNode *_ptr;
518 * A node in a property tree.
528 MAX_STRING_LEN = 1024
532 * Property value types.
548 * Access mode attributes.
550 * <p>The ARCHIVE attribute is strictly advisory, and controls
551 * whether the property should normally be saved and restored.</p>
564 * Last used attribute
565 * Update as needed when enum Attribute is changed
567 static const int LAST_USED_ATTRIBUTE;
571 * Default constructor.
579 SGPropertyNode (const SGPropertyNode &node);
585 virtual ~SGPropertyNode ();
594 * Test whether this node contains a primitive leaf value.
596 bool hasValue () const { return (_type != NONE); }
600 * Get the node's simple (XML) name.
602 const char * getName () const { return _name; }
606 * Get the node's integer index.
608 int getIndex () const { return _index; }
612 * Get a non-const pointer to the node's parent.
614 SGPropertyNode * getParent () { return _parent; }
618 * Get a const pointer to the node's parent.
620 const SGPropertyNode * getParent () const { return _parent; }
629 * Get the number of child nodes.
631 int nChildren () const { return _children.size(); }
635 * Get a child node by position (*NOT* index).
637 SGPropertyNode * getChild (int position);
641 * Get a const child node by position (*NOT* index).
643 const SGPropertyNode * getChild (int position) const;
647 * Get a child node by name and index.
649 SGPropertyNode * getChild (const char * name, int index = 0,
650 bool create = false);
654 * Get a const child node by name and index.
656 const SGPropertyNode * getChild (const char * name, int index = 0) const;
660 * Get a vector of all children with the specified name.
662 vector<SGPropertyNode_ptr> getChildren (const char * name) const;
666 * Remove a child node
668 SGPropertyNode_ptr removeChild (const char * name, int index = 0,
678 * Alias this node's leaf value to another's.
680 bool alias (SGPropertyNode * target);
684 * Alias this node's leaf value to another's by relative path.
686 bool alias (const char * path);
690 * Remove any alias for this node.
696 * Test whether the node's leaf value is aliased to another's.
698 bool isAlias () const { return (_type == ALIAS); }
702 * Get a non-const pointer to the current alias target, if any.
704 SGPropertyNode * getAliasTarget ();
708 * Get a const pointer to the current alias target, if any.
710 const SGPropertyNode * getAliasTarget () const;
719 * Get the path to this node from the root.
721 const char * getPath (bool simplify = false) const;
725 * Get a pointer to the root node.
727 SGPropertyNode * getRootNode ();
731 * Get a const pointer to the root node.
733 const SGPropertyNode * getRootNode () const;
737 * Get a pointer to another node by relative path.
739 SGPropertyNode * getNode (const char * relative_path, bool create = false);
743 * Get a pointer to another node by relative path.
745 * This method leaves the index off the last member of the path,
746 * so that the user can specify it separately (and save some
747 * string building). For example, getNode("/bar[1]/foo", 3) is
748 * exactly equivalent to getNode("bar[1]/foo[3]"). The index
749 * provided overrides any given in the path itself for the last
752 SGPropertyNode * getNode (const char * relative_path, int index,
753 bool create = false);
757 * Get a const pointer to another node by relative path.
759 const SGPropertyNode * getNode (const char * relative_path) const;
763 * Get a const pointer to another node by relative path.
765 * This method leaves the index off the last member of the path,
766 * so that the user can specify it separate.
768 const SGPropertyNode * getNode (const char * relative_path,
777 * Check a single mode attribute for the property node.
779 bool getAttribute (Attribute attr) const { return ((_attr & attr) != 0); }
783 * Set a single mode attribute for the property node.
785 void setAttribute (Attribute attr, bool state) {
786 (state ? _attr |= attr : _attr &= ~attr);
791 * Get all of the mode attributes for the property node.
793 int getAttributes () const { return _attr; }
797 * Set all of the mode attributes for the property node.
799 void setAttributes (int attr) { _attr = attr; }
803 // Leaf Value (primitive).
808 * Get the type of leaf value, if any, for this node.
810 Type getType () const;
814 * Get a bool value for this node.
816 bool getBoolValue () const;
820 * Get an int value for this node.
822 int getIntValue () const;
826 * Get a long int value for this node.
828 long getLongValue () const;
832 * Get a float value for this node.
834 float getFloatValue () const;
838 * Get a double value for this node.
840 double getDoubleValue () const;
844 * Get a string value for this node.
846 const char * getStringValue () const;
851 * Set a bool value for this node.
853 bool setBoolValue (bool value);
857 * Set an int value for this node.
859 bool setIntValue (int value);
863 * Set a long int value for this node.
865 bool setLongValue (long value);
869 * Set a float value for this node.
871 bool setFloatValue (float value);
875 * Set a double value for this node.
877 bool setDoubleValue (double value);
881 * Set a string value for this node.
883 bool setStringValue (const char * value);
887 * Set a value of unspecified type for this node.
889 bool setUnspecifiedValue (const char * value);
898 * Test whether this node is bound to an external data source.
900 bool isTied () const { return _tied; }
904 * Bind this node to an external bool source.
906 bool tie (const SGRawValue<bool> &rawValue, bool useDefault = true);
910 * Bind this node to an external int source.
912 bool tie (const SGRawValue<int> &rawValue, bool useDefault = true);
916 * Bind this node to an external long int source.
918 bool tie (const SGRawValue<long> &rawValue, bool useDefault = true);
922 * Bind this node to an external float source.
924 bool tie (const SGRawValue<float> &rawValue, bool useDefault = true);
928 * Bind this node to an external double source.
930 bool tie (const SGRawValue<double> &rawValue, bool useDefault = true);
934 * Bind this node to an external string source.
936 bool tie (const SGRawValue<const char *> &rawValue, bool useDefault = true);
940 * Unbind this node from any external data source.
946 // Convenience methods using paths.
947 // TODO: add attribute methods
952 * Get another node's type.
954 Type getType (const char * relative_path) const;
958 * Test whether another node has a leaf value.
960 bool hasValue (const char * relative_path) const;
964 * Get another node's value as a bool.
966 bool getBoolValue (const char * relative_path,
967 bool defaultValue = false) const;
971 * Get another node's value as an int.
973 int getIntValue (const char * relative_path,
974 int defaultValue = 0) const;
978 * Get another node's value as a long int.
980 long getLongValue (const char * relative_path,
981 long defaultValue = 0L) const;
985 * Get another node's value as a float.
987 float getFloatValue (const char * relative_path,
988 float defaultValue = 0.0) const;
992 * Get another node's value as a double.
994 double getDoubleValue (const char * relative_path,
995 double defaultValue = 0.0L) const;
999 * Get another node's value as a string.
1001 const char * getStringValue (const char * relative_path,
1002 const char * defaultValue = "") const;
1006 * Set another node's value as a bool.
1008 bool setBoolValue (const char * relative_path, bool value);
1012 * Set another node's value as an int.
1014 bool setIntValue (const char * relative_path, int value);
1018 * Set another node's value as a long int.
1020 bool setLongValue (const char * relative_path, long value);
1024 * Set another node's value as a float.
1026 bool setFloatValue (const char * relative_path, float value);
1030 * Set another node's value as a double.
1032 bool setDoubleValue (const char * relative_path, double value);
1036 * Set another node's value as a string.
1038 bool setStringValue (const char * relative_path, const char * value);
1042 * Set another node's value with no specified type.
1044 bool setUnspecifiedValue (const char * relative_path, const char * value);
1048 * Test whether another node is bound to an external data source.
1050 bool isTied (const char * relative_path) const;
1054 * Bind another node to an external bool source.
1056 bool tie (const char * relative_path, const SGRawValue<bool> &rawValue,
1057 bool useDefault = true);
1061 * Bind another node to an external int source.
1063 bool tie (const char * relative_path, const SGRawValue<int> &rawValue,
1064 bool useDefault = true);
1068 * Bind another node to an external long int source.
1070 bool tie (const char * relative_path, const SGRawValue<long> &rawValue,
1071 bool useDefault = true);
1075 * Bind another node to an external float source.
1077 bool tie (const char * relative_path, const SGRawValue<float> &rawValue,
1078 bool useDefault = true);
1082 * Bind another node to an external double source.
1084 bool tie (const char * relative_path, const SGRawValue<double> &rawValue,
1085 bool useDefault = true);
1089 * Bind another node to an external string source.
1091 bool tie (const char * relative_path, const SGRawValue<const char *> &rawValue,
1092 bool useDefault = true);
1096 * Unbind another node from any external data source.
1098 bool untie (const char * relative_path);
1105 * Protected constructor for making new nodes on demand.
1107 SGPropertyNode (const char * name, int index, SGPropertyNode * parent);
1112 // Get the raw value
1113 bool get_bool () const;
1114 int get_int () const;
1115 long get_long () const;
1116 float get_float () const;
1117 double get_double () const;
1118 const char * get_string () const;
1120 // Set the raw value
1121 bool set_bool (bool value);
1122 bool set_int (int value);
1123 bool set_long (long value);
1124 bool set_float (float value);
1125 bool set_double (double value);
1126 bool set_string (const char * value);
1130 * Clear any existing value and set the type to NONE.
1132 void clear_value ();
1136 * Get the value as a string.
1138 const char * make_string () const;
1142 * Trace a read access.
1144 void trace_read () const;
1148 * Trace a write access.
1150 void trace_write () const;
1154 * Increment reference counter
1156 void incrementRef();
1159 * Decrement reference counter
1163 friend class SGPropertyNode_ptr;
1166 mutable char _buffer[MAX_STRING_LEN+1];
1172 SGPropertyNode * _parent;
1173 vector<SGPropertyNode_ptr> _children;
1174 vector<SGPropertyNode_ptr> _removedChildren;
1175 hash_table * _path_cache;
1181 // The right kind of pointer...
1183 SGPropertyNode * alias;
1184 SGRawValue<bool> * bool_val;
1185 SGRawValue<int> * int_val;
1186 SGRawValue<long> * long_val;
1187 SGRawValue<float> * float_val;
1188 SGRawValue<double> * double_val;
1189 SGRawValue<const char *> * string_val;
1204 * A very simple hash table with no remove functionality.
1210 * An entry in a bucket in a hash table.
1216 virtual const char * get_key () { return _key; }
1217 virtual void set_key (const char * key);
1218 virtual SGPropertyNode * get_value () { return _value; }
1219 virtual void set_value (SGPropertyNode * value);
1222 SGPropertyNode * _value;
1227 * A bucket in a hash table.
1233 virtual entry * get_entry (const char * key, bool create = false);
1239 friend class bucket;
1242 virtual ~hash_table ();
1243 virtual SGPropertyNode * get (const char * key);
1244 virtual void put (const char * key, SGPropertyNode * value);
1247 unsigned int hashcode (const char * key);
1248 unsigned int _data_length;
1254 #endif // __PROPS_HXX