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);
455 * A node in a property tree.
465 MAX_STRING_LEN = 1024
469 * Property value types.
485 * Access mode attributes.
487 * <p>The ARCHIVE attribute is strictly advisory, and controls
488 * whether the property should normally be saved and restored.</p>
500 * Default constructor.
508 SGPropertyNode (const SGPropertyNode &node);
514 virtual ~SGPropertyNode ();
523 * Test whether this node contains a primitive leaf value.
525 bool hasValue () const { return (_type != NONE); }
529 * Get the node's simple (XML) name.
531 const char * getName () const { return _name; }
535 * Get the node's integer index.
537 const int getIndex () const { return _index; }
541 * Get a non-const pointer to the node's parent.
543 SGPropertyNode * getParent () { return _parent; }
547 * Get a const pointer to the node's parent.
549 const SGPropertyNode * getParent () const { return _parent; }
558 * Get the number of child nodes.
560 const int nChildren () const { return _children.size(); }
564 * Get a child node by position (*NOT* index).
566 SGPropertyNode * getChild (int position);
570 * Get a const child node by position (*NOT* index).
572 const SGPropertyNode * getChild (int position) const;
576 * Get a child node by name and index.
578 SGPropertyNode * getChild (const char * name, int index = 0,
579 bool create = false);
583 * Get a const child node by name and index.
585 const SGPropertyNode * getChild (const char * name, int index = 0) const;
589 * Get a vector of all children with the specified name.
591 vector<SGPropertyNode *> getChildren (const char * name);
595 * Get a vector all all children (const) with the specified name.
597 vector<const SGPropertyNode *> getChildren (const char * name) const;
606 * Alias this node's leaf value to another's.
608 bool alias (SGPropertyNode * target);
612 * Alias this node's leaf value to another's by relative path.
614 bool alias (const char * path);
618 * Remove any alias for this node.
624 * Test whether the node's leaf value is aliased to another's.
626 bool isAlias () const { return (_type == ALIAS); }
630 * Get a non-const pointer to the current alias target, if any.
632 SGPropertyNode * getAliasTarget ();
636 * Get a const pointer to the current alias target, if any.
638 const SGPropertyNode * getAliasTarget () const;
647 * Get the path to this node from the root.
649 const char * getPath (bool simplify = false) const;
653 * Get a pointer to the root node.
655 SGPropertyNode * getRootNode ();
659 * Get a const pointer to the root node.
661 const SGPropertyNode * getRootNode () const;
665 * Get a pointer to another node by relative path.
667 SGPropertyNode * getNode (const char * relative_path, bool create = false);
671 * Get a pointer to another node by relative path.
673 * This method leaves the index off the last member of the path,
674 * so that the user can specify it separately (and save some
675 * string building). For example, getNode("/bar[1]/foo", 3) is
676 * exactly equivalent to getNode("bar[1]/foo[3]"). The index
677 * provided overrides any given in the path itself for the last
680 SGPropertyNode * getNode (const char * relative_path, int index,
681 bool create = false);
685 * Get a const pointer to another node by relative path.
687 const SGPropertyNode * getNode (const char * relative_path) const;
691 * Get a const pointer to another node by relative path.
693 * This method leaves the index off the last member of the path,
694 * so that the user can specify it separate.
696 const SGPropertyNode * getNode (const char * relative_path,
705 * Check a single mode attribute for the property node.
707 bool getAttribute (Attribute attr) const { return ((_attr & attr) != 0); }
711 * Set a single mode attribute for the property node.
713 void setAttribute (Attribute attr, bool state) {
714 (state ? _attr |= attr : _attr &= ~attr);
719 * Get all of the mode attributes for the property node.
721 int getAttributes () const { return _attr; }
725 * Set all of the mode attributes for the property node.
727 void setAttributes (int attr) { _attr = attr; }
731 // Leaf Value (primitive).
736 * Get the type of leaf value, if any, for this node.
738 Type getType () const;
742 * Get a bool value for this node.
744 bool getBoolValue () const;
748 * Get an int value for this node.
750 int getIntValue () const;
754 * Get a long int value for this node.
756 long getLongValue () const;
760 * Get a float value for this node.
762 float getFloatValue () const;
766 * Get a double value for this node.
768 double getDoubleValue () const;
772 * Get a string value for this node.
774 const char * getStringValue () const;
779 * Set a bool value for this node.
781 bool setBoolValue (bool value);
785 * Set an int value for this node.
787 bool setIntValue (int value);
791 * Set a long int value for this node.
793 bool setLongValue (long value);
797 * Set a float value for this node.
799 bool setFloatValue (float value);
803 * Set a double value for this node.
805 bool setDoubleValue (double value);
809 * Set a string value for this node.
811 bool setStringValue (const char * value);
815 * Set a value of unspecified type for this node.
817 bool setUnspecifiedValue (const char * value);
826 * Test whether this node is bound to an external data source.
828 bool isTied () const { return _tied; }
832 * Bind this node to an external bool source.
834 bool tie (const SGRawValue<bool> &rawValue, bool useDefault = true);
838 * Bind this node to an external int source.
840 bool tie (const SGRawValue<int> &rawValue, bool useDefault = true);
844 * Bind this node to an external long int source.
846 bool tie (const SGRawValue<long> &rawValue, bool useDefault = true);
850 * Bind this node to an external float source.
852 bool tie (const SGRawValue<float> &rawValue, bool useDefault = true);
856 * Bind this node to an external double source.
858 bool tie (const SGRawValue<double> &rawValue, bool useDefault = true);
862 * Bind this node to an external string source.
864 bool tie (const SGRawValue<const char *> &rawValue, bool useDefault = true);
868 * Unbind this node from any external data source.
874 // Convenience methods using paths.
875 // TODO: add attribute methods
880 * Get another node's type.
882 Type getType (const char * relative_path) const;
886 * Test whether another node has a leaf value.
888 bool hasValue (const char * relative_path) const;
892 * Get another node's value as a bool.
894 bool getBoolValue (const char * relative_path,
895 bool defaultValue = false) const;
899 * Get another node's value as an int.
901 int getIntValue (const char * relative_path,
902 int defaultValue = 0) const;
906 * Get another node's value as a long int.
908 long getLongValue (const char * relative_path,
909 long defaultValue = 0L) const;
913 * Get another node's value as a float.
915 float getFloatValue (const char * relative_path,
916 float defaultValue = 0.0) const;
920 * Get another node's value as a double.
922 double getDoubleValue (const char * relative_path,
923 double defaultValue = 0.0L) const;
927 * Get another node's value as a string.
929 const char * getStringValue (const char * relative_path,
930 const char * defaultValue = "") const;
934 * Set another node's value as a bool.
936 bool setBoolValue (const char * relative_path, bool value);
940 * Set another node's value as an int.
942 bool setIntValue (const char * relative_path, int value);
946 * Set another node's value as a long int.
948 bool setLongValue (const char * relative_path, long value);
952 * Set another node's value as a float.
954 bool setFloatValue (const char * relative_path, float value);
958 * Set another node's value as a double.
960 bool setDoubleValue (const char * relative_path, double value);
964 * Set another node's value as a string.
966 bool setStringValue (const char * relative_path, const char * value);
970 * Set another node's value with no specified type.
972 bool setUnspecifiedValue (const char * relative_path, const char * value);
976 * Test whether another node is bound to an external data source.
978 bool isTied (const char * relative_path) const;
982 * Bind another node to an external bool source.
984 bool tie (const char * relative_path, const SGRawValue<bool> &rawValue,
985 bool useDefault = true);
989 * Bind another node to an external int source.
991 bool tie (const char * relative_path, const SGRawValue<int> &rawValue,
992 bool useDefault = true);
996 * Bind another node to an external long int source.
998 bool tie (const char * relative_path, const SGRawValue<long> &rawValue,
999 bool useDefault = true);
1003 * Bind another node to an external float source.
1005 bool tie (const char * relative_path, const SGRawValue<float> &rawValue,
1006 bool useDefault = true);
1010 * Bind another node to an external double source.
1012 bool tie (const char * relative_path, const SGRawValue<double> &rawValue,
1013 bool useDefault = true);
1017 * Bind another node to an external string source.
1019 bool tie (const char * relative_path, const SGRawValue<const char *> &rawValue,
1020 bool useDefault = true);
1024 * Unbind another node from any external data source.
1026 bool untie (const char * relative_path);
1033 * Protected constructor for making new nodes on demand.
1035 SGPropertyNode (const char * name, int index, SGPropertyNode * parent);
1040 // Get the raw value
1041 bool get_bool () const;
1042 int get_int () const;
1043 long get_long () const;
1044 float get_float () const;
1045 double get_double () const;
1046 const char * get_string () const;
1048 // Set the raw value
1049 bool set_bool (bool value);
1050 bool set_int (int value);
1051 bool set_long (long value);
1052 bool set_float (float value);
1053 bool set_double (double value);
1054 bool set_string (const char * value);
1058 * Clear any existing value and set the type to NONE.
1060 void clear_value ();
1064 * Get the value as a string.
1066 const char * make_string () const;
1070 * Trace a read access.
1072 void trace_read () const;
1076 * Trace a write access.
1078 void trace_write () const;
1080 mutable char _buffer[MAX_STRING_LEN+1];
1086 SGPropertyNode * _parent;
1087 vector<SGPropertyNode *> _children;
1088 hash_table * _path_cache;
1093 // The right kind of pointer...
1095 SGPropertyNode * alias;
1096 SGRawValue<bool> * bool_val;
1097 SGRawValue<int> * int_val;
1098 SGRawValue<long> * long_val;
1099 SGRawValue<float> * float_val;
1100 SGRawValue<double> * double_val;
1101 SGRawValue<const char *> * string_val;
1116 * A very simple hash table with no remove functionality.
1122 * An entry in a bucket in a hash table.
1128 virtual const char * get_key () { return _key; }
1129 virtual void set_key (const char * key);
1130 virtual SGPropertyNode * get_value () { return _value; }
1131 virtual void set_value (SGPropertyNode * value);
1134 SGPropertyNode * _value;
1139 * A bucket in a hash table.
1145 virtual entry * get_entry (const char * key, bool create = false);
1151 friend class bucket;
1154 virtual ~hash_table ();
1155 virtual SGPropertyNode * get (const char * key);
1156 virtual void put (const char * key, SGPropertyNode * value);
1159 unsigned int hashcode (const char * key);
1160 unsigned int _data_length;
1166 #endif // __PROPS_HXX