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);
44 #include <simgear/structure/SGReferenced.hxx>
45 #include <simgear/structure/SGSharedPtr.hxx>
49 #pragma warn A sloppy coder has defined NONE as a macro!
54 #pragma warn A sloppy coder has defined ALIAS as a macro!
59 #pragma warn A sloppy coder has defined UNSPECIFIED as a macro!
64 #pragma warn A sloppy coder has defined BOOL as a macro!
69 #pragma warn A sloppy coder has defined INT as a macro!
74 #pragma warn A sloppy coder has defined LONG as a macro!
79 #pragma warn A sloppy coder has defined FLOAT as a macro!
84 #pragma warn A sloppy coder has defined DOUBLE as a macro!
89 #pragma warn A sloppy coder has defined STRING as a macro!
95 ////////////////////////////////////////////////////////////////////////
98 // This is the mechanism that information-providing routines can
99 // use to link their own values to the property manager. Any
100 // SGValue can be tied to a raw value and then untied again.
102 // Note: we are forced to use inlined methods here to ensure
103 // that the templates will be instantiated. We're probably taking
104 // a small performance hit for that.
105 ////////////////////////////////////////////////////////////////////////
109 * Abstract base class for a raw value.
111 * <p>The property manager is implemented in two layers. The {@link
112 * SGPropertyNode} is the highest and most abstract layer,
113 * representing an LValue/RValue pair: it records the position of the
114 * property in the property tree and contains facilities for
115 * navigation to other nodes. It is guaranteed to be persistent: the
116 * {@link SGPropertyNode} will not change during a session, even if
117 * the property is bound and unbound multiple times.</p>
119 * <p>When the property value is not managed internally in the
120 * SGPropertyNode, the SGPropertyNode will contain a reference to an
121 * SGRawValue (this class), which provides an abstract way to get,
122 * set, and clone the underlying value. The SGRawValue may change
123 * frequently during a session as a value is retyped or bound and
124 * unbound to various data source, but the abstract SGPropertyNode
125 * layer insulates the application from those changes. The raw value
126 * contains no facilities for data binding or for type conversion: it
127 * is simply the abstraction of a primitive data type (or a compound
128 * data type, in the case of a string).</p>
130 * <p>The SGPropertyNode class always keeps a *copy* of a raw value,
131 * not the original one passed to it; if you override a derived class
132 * but do not replace the {@link #clone} method, strange things will
135 * <p>All derived SGRawValue classes must implement {@link #getValue},
136 * {@link #setValue}, and {@link #clone} for the appropriate type.</p>
138 * @see SGPropertyNode
139 * @see SGRawValuePointer
140 * @see SGRawValueFunctions
141 * @see SGRawValueFunctionsIndexed
142 * @see SGRawValueMethods
143 * @see SGRawValueMethodsIndexed
151 * The default underlying value for this type.
153 * Every raw value has a default; the default is false for a
154 * boolean, 0 for the various numeric values, and "" for a string.
155 * If additional types of raw values are added in the future, they
156 * may need different kinds of default values (such as epoch for a
157 * date type). The default value is used when creating new values.
159 static const T DefaultValue; // Default for this kind of raw value.
165 * Use the default value for this type.
173 virtual ~SGRawValue () {}
177 * Return the underlying value.
179 * @return The actual value for the property.
182 virtual T getValue () const = 0;
186 * Assign a new underlying value.
188 * If the new value cannot be set (because this is a read-only
189 * raw value, or because the new value is not acceptable for
190 * some reason) this method returns false and leaves the original
193 * @param value The actual value for the property.
194 * @return true if the value was set successfully, false otherwise.
197 virtual bool setValue (T value) = 0;
201 * Create a new deep copy of this raw value.
203 * The copy will contain its own version of the underlying value
206 * @return A deep copy of the current object.
208 virtual SGRawValue * clone () const = 0;
213 * A raw value bound to a pointer.
215 * This is the most efficient way to tie an external value, but also
216 * the most dangerous, because there is no way for the supplier to
217 * perform bounds checking and derived calculations except by polling
218 * the variable to see if it has changed. There is no default
219 * constructor, because this class would be meaningless without a
223 class SGRawValuePointer : public SGRawValue<T>
228 * Explicit pointer constructor.
230 * Create a new raw value bound to the value of the variable
231 * referenced by the pointer.
233 * @param ptr The pointer to the variable to which this raw value
236 SGRawValuePointer (T * ptr) : _ptr(ptr) {}
241 virtual ~SGRawValuePointer () {}
244 * Get the underlying value.
246 * This method will dereference the pointer and return the
249 virtual T getValue () const { return *_ptr; }
252 * Set the underlying value.
254 * This method will dereference the pointer and change the
257 virtual bool setValue (T value) { *_ptr = value; return true; }
260 * Create a copy of this raw value.
262 * The copy will use the same external pointer as the original.
264 virtual SGRawValue<T> * clone () const {
265 return new SGRawValuePointer<T>(_ptr);
274 * A value managed through static functions.
276 * A read-only value will not have a setter; a write-only value will
280 class SGRawValueFunctions : public SGRawValue<T>
285 * The template type of a static getter function.
287 typedef T (*getter_t)();
290 * The template type of a static setter function.
292 typedef void (*setter_t)(T);
295 * Explicit constructor.
297 * Create a new raw value bound to the getter and setter supplied.
299 * @param getter A static function for getting a value, or 0
300 * to read-disable the value.
301 * @param setter A static function for setting a value, or 0
302 * to write-disable the value.
304 SGRawValueFunctions (getter_t getter = 0, setter_t setter = 0)
305 : _getter(getter), _setter(setter) {}
310 virtual ~SGRawValueFunctions () {}
313 * Get the underlying value.
315 * This method will invoke the getter function to get a value.
316 * If no getter function was supplied, this method will always
317 * return the default value for the type.
319 virtual T getValue () const {
320 if (_getter) return (*_getter)();
321 else return SGRawValue<T>::DefaultValue;
325 * Set the underlying value.
327 * This method will invoke the setter function to change the
328 * underlying value. If no setter function was supplied, this
329 * method will return false.
331 virtual bool setValue (T value) {
332 if (_setter) { (*_setter)(value); return true; }
337 * Create a copy of this raw value, bound to the same functions.
339 virtual SGRawValue<T> * clone () const {
340 return new SGRawValueFunctions<T>(_getter,_setter);
350 * An indexed value bound to static functions.
352 * A read-only value will not have a setter; a write-only value will
353 * not have a getter. An indexed value is useful for binding one
354 * of a list of possible values (such as multiple engines for a
355 * plane). The index is hard-coded at creation time.
360 class SGRawValueFunctionsIndexed : public SGRawValue<T>
363 typedef T (*getter_t)(int);
364 typedef void (*setter_t)(int,T);
365 SGRawValueFunctionsIndexed (int index, getter_t getter = 0, setter_t setter = 0)
366 : _index(index), _getter(getter), _setter(setter) {}
367 virtual ~SGRawValueFunctionsIndexed () {}
368 virtual T getValue () const {
369 if (_getter) return (*_getter)(_index);
370 else return SGRawValue<T>::DefaultValue;
372 virtual bool setValue (T value) {
373 if (_setter) { (*_setter)(_index, value); return true; }
376 virtual SGRawValue<T> * clone () const {
377 return new SGRawValueFunctionsIndexed<T>(_index, _getter, _setter);
387 * A value managed through an object and access methods.
389 * A read-only value will not have a setter; a write-only value will
392 template <class C, class T>
393 class SGRawValueMethods : public SGRawValue<T>
396 typedef T (C::*getter_t)() const;
397 typedef void (C::*setter_t)(T);
398 SGRawValueMethods (C &obj, getter_t getter = 0, setter_t setter = 0)
399 : _obj(obj), _getter(getter), _setter(setter) {}
400 virtual ~SGRawValueMethods () {}
401 virtual T getValue () const {
402 if (_getter) { return (_obj.*_getter)(); }
403 else { return SGRawValue<T>::DefaultValue; }
405 virtual bool setValue (T value) {
406 if (_setter) { (_obj.*_setter)(value); return true; }
409 virtual SGRawValue<T> * clone () const {
410 return new SGRawValueMethods<C,T>(_obj, _getter, _setter);
420 * An indexed value managed through an object and access methods.
422 * A read-only value will not have a setter; a write-only value will
425 template <class C, class T>
426 class SGRawValueMethodsIndexed : public SGRawValue<T>
429 typedef T (C::*getter_t)(int) const;
430 typedef void (C::*setter_t)(int, T);
431 SGRawValueMethodsIndexed (C &obj, int index,
432 getter_t getter = 0, setter_t setter = 0)
433 : _obj(obj), _index(index), _getter(getter), _setter(setter) {}
434 virtual ~SGRawValueMethodsIndexed () {}
435 virtual T getValue () const {
436 if (_getter) { return (_obj.*_getter)(_index); }
437 else { return SGRawValue<T>::DefaultValue; }
439 virtual bool setValue (T value) {
440 if (_setter) { (_obj.*_setter)(_index, value); return true; }
443 virtual SGRawValue<T> * clone () const {
444 return new SGRawValueMethodsIndexed<C,T>(_obj, _index, _getter, _setter);
455 * The smart pointer that manage reference counting
457 class SGPropertyNode;
458 typedef SGSharedPtr<SGPropertyNode> SGPropertyNode_ptr;
459 typedef SGSharedPtr<const SGPropertyNode> SGConstPropertyNode_ptr;
463 * The property change listener interface.
465 * <p>Any class that needs to listen for property changes must implement
466 * this interface.</p>
468 class SGPropertyChangeListener
471 virtual ~SGPropertyChangeListener ();
472 virtual void valueChanged (SGPropertyNode * node);
473 virtual void childAdded (SGPropertyNode * parent, SGPropertyNode * child);
474 virtual void childRemoved (SGPropertyNode * parent, SGPropertyNode * child);
477 friend class SGPropertyNode;
478 virtual void register_property (SGPropertyNode * node);
479 virtual void unregister_property (SGPropertyNode * node);
482 vector<SGPropertyNode *> _properties;
488 * A node in a property tree.
490 class SGPropertyNode : public SGReferenced
498 MAX_STRING_LEN = 1024
502 * Property value types.
518 * Access mode attributes.
520 * <p>The ARCHIVE attribute is strictly advisory, and controls
521 * whether the property should normally be saved and restored.</p>
535 * Last used attribute
536 * Update as needed when enum Attribute is changed
538 static const int LAST_USED_ATTRIBUTE;
542 * Default constructor.
550 SGPropertyNode (const SGPropertyNode &node);
556 virtual ~SGPropertyNode ();
565 * Test whether this node contains a primitive leaf value.
567 bool hasValue () const { return (_type != NONE); }
571 * Get the node's simple (XML) name.
573 const char * getName () const { return _name.c_str(); }
577 * Get the node's pretty display name, with subscript when needed.
579 const char * getDisplayName (bool simplify = false) const;
583 * Get the node's integer index.
585 int getIndex () const { return _index; }
589 * Get a non-const pointer to the node's parent.
591 SGPropertyNode * getParent () { return _parent; }
595 * Get a const pointer to the node's parent.
597 const SGPropertyNode * getParent () const { return _parent; }
606 * Get the number of child nodes.
608 int nChildren () const { return _children.size(); }
612 * Get a child node by position (*NOT* index).
614 SGPropertyNode * getChild (int position);
618 * Get a const child node by position (*NOT* index).
620 const SGPropertyNode * getChild (int position) const;
624 * Test whether a named child exists.
626 bool hasChild (const char * name, int index = 0) const
628 return (getChild(name, index) != 0);
633 * Get a child node by name and index.
635 SGPropertyNode * getChild (const char * name, int index = 0,
636 bool create = false);
640 * Get a const child node by name and index.
642 const SGPropertyNode * getChild (const char * name, int index = 0) const;
646 * Get a vector of all children with the specified name.
648 vector<SGPropertyNode_ptr> getChildren (const char * name) const;
652 * Remove child by position.
654 SGPropertyNode_ptr removeChild (int pos, bool keep = true);
658 * Remove a child node
660 SGPropertyNode_ptr removeChild (const char * name, int index = 0,
664 * Remove all children with the specified name.
666 vector<SGPropertyNode_ptr> removeChildren (const char * name,
676 * Alias this node's leaf value to another's.
678 bool alias (SGPropertyNode * target);
682 * Alias this node's leaf value to another's by relative path.
684 bool alias (const char * path);
688 * Remove any alias for this node.
694 * Test whether the node's leaf value is aliased to another's.
696 bool isAlias () const { return (_type == ALIAS); }
700 * Get a non-const pointer to the current alias target, if any.
702 SGPropertyNode * getAliasTarget ();
706 * Get a const pointer to the current alias target, if any.
708 const SGPropertyNode * getAliasTarget () const;
717 * Get the path to this node from the root.
719 const char * getPath (bool simplify = false) const;
723 * Get a pointer to the root node.
725 SGPropertyNode * getRootNode ();
729 * Get a const pointer to the root node.
731 const SGPropertyNode * getRootNode () const;
735 * Get a pointer to another node by relative path.
737 SGPropertyNode * getNode (const char * relative_path, bool create = false);
741 * Get a pointer to another node by relative path.
743 * This method leaves the index off the last member of the path,
744 * so that the user can specify it separately (and save some
745 * string building). For example, getNode("/bar[1]/foo", 3) is
746 * exactly equivalent to getNode("bar[1]/foo[3]"). The index
747 * provided overrides any given in the path itself for the last
750 SGPropertyNode * getNode (const char * relative_path, int index,
751 bool create = false);
755 * Get a const pointer to another node by relative path.
757 const SGPropertyNode * getNode (const char * relative_path) const;
761 * Get a const pointer to another node by relative path.
763 * This method leaves the index off the last member of the path,
764 * so that the user can specify it separate.
766 const SGPropertyNode * getNode (const char * relative_path,
775 * Check a single mode attribute for the property node.
777 bool getAttribute (Attribute attr) const { return ((_attr & attr) != 0); }
781 * Set a single mode attribute for the property node.
783 void setAttribute (Attribute attr, bool state) {
784 (state ? _attr |= attr : _attr &= ~attr);
789 * Get all of the mode attributes for the property node.
791 int getAttributes () const { return _attr; }
795 * Set all of the mode attributes for the property node.
797 void setAttributes (int attr) { _attr = attr; }
801 // Leaf Value (primitive).
806 * Get the type of leaf value, if any, for this node.
808 Type getType () const;
812 * Get a bool value for this node.
814 bool getBoolValue () const;
818 * Get an int value for this node.
820 int getIntValue () const;
824 * Get a long int value for this node.
826 long getLongValue () const;
830 * Get a float value for this node.
832 float getFloatValue () const;
836 * Get a double value for this node.
838 double getDoubleValue () const;
842 * Get a string value for this node.
844 const char * getStringValue () const;
849 * Set a bool value for this node.
851 bool setBoolValue (bool value);
855 * Set an int value for this node.
857 bool setIntValue (int value);
861 * Set a long int value for this node.
863 bool setLongValue (long value);
867 * Set a float value for this node.
869 bool setFloatValue (float value);
873 * Set a double value for this node.
875 bool setDoubleValue (double value);
879 * Set a string value for this node.
881 bool setStringValue (const char * value);
885 * Set a value of unspecified type for this node.
887 bool setUnspecifiedValue (const char * value);
896 * Test whether this node is bound to an external data source.
898 bool isTied () const { return _tied; }
902 * Bind this node to an external bool source.
904 bool tie (const SGRawValue<bool> &rawValue, bool useDefault = true);
908 * Bind this node to an external int source.
910 bool tie (const SGRawValue<int> &rawValue, bool useDefault = true);
914 * Bind this node to an external long int source.
916 bool tie (const SGRawValue<long> &rawValue, bool useDefault = true);
920 * Bind this node to an external float source.
922 bool tie (const SGRawValue<float> &rawValue, bool useDefault = true);
926 * Bind this node to an external double source.
928 bool tie (const SGRawValue<double> &rawValue, bool useDefault = true);
932 * Bind this node to an external string source.
934 bool tie (const SGRawValue<const char *> &rawValue, bool useDefault = true);
938 * Unbind this node from any external data source.
944 // Convenience methods using paths.
945 // TODO: add attribute methods
950 * Get another node's type.
952 Type getType (const char * relative_path) const;
956 * Test whether another node has a leaf value.
958 bool hasValue (const char * relative_path) const;
962 * Get another node's value as a bool.
964 bool getBoolValue (const char * relative_path,
965 bool defaultValue = false) const;
969 * Get another node's value as an int.
971 int getIntValue (const char * relative_path,
972 int defaultValue = 0) const;
976 * Get another node's value as a long int.
978 long getLongValue (const char * relative_path,
979 long defaultValue = 0L) const;
983 * Get another node's value as a float.
985 float getFloatValue (const char * relative_path,
986 float defaultValue = 0.0) const;
990 * Get another node's value as a double.
992 double getDoubleValue (const char * relative_path,
993 double defaultValue = 0.0L) const;
997 * Get another node's value as a string.
999 const char * getStringValue (const char * relative_path,
1000 const char * defaultValue = "") const;
1004 * Set another node's value as a bool.
1006 bool setBoolValue (const char * relative_path, bool value);
1010 * Set another node's value as an int.
1012 bool setIntValue (const char * relative_path, int value);
1016 * Set another node's value as a long int.
1018 bool setLongValue (const char * relative_path, long value);
1022 * Set another node's value as a float.
1024 bool setFloatValue (const char * relative_path, float value);
1028 * Set another node's value as a double.
1030 bool setDoubleValue (const char * relative_path, double value);
1034 * Set another node's value as a string.
1036 bool setStringValue (const char * relative_path, const char * value);
1040 * Set another node's value with no specified type.
1042 bool setUnspecifiedValue (const char * relative_path, const char * value);
1046 * Test whether another node is bound to an external data source.
1048 bool isTied (const char * relative_path) const;
1052 * Bind another node to an external bool source.
1054 bool tie (const char * relative_path, const SGRawValue<bool> &rawValue,
1055 bool useDefault = true);
1059 * Bind another node to an external int source.
1061 bool tie (const char * relative_path, const SGRawValue<int> &rawValue,
1062 bool useDefault = true);
1066 * Bind another node to an external long int source.
1068 bool tie (const char * relative_path, const SGRawValue<long> &rawValue,
1069 bool useDefault = true);
1073 * Bind another node to an external float source.
1075 bool tie (const char * relative_path, const SGRawValue<float> &rawValue,
1076 bool useDefault = true);
1080 * Bind another node to an external double source.
1082 bool tie (const char * relative_path, const SGRawValue<double> &rawValue,
1083 bool useDefault = true);
1087 * Bind another node to an external string source.
1089 bool tie (const char * relative_path, const SGRawValue<const char *> &rawValue,
1090 bool useDefault = true);
1094 * Unbind another node from any external data source.
1096 bool untie (const char * relative_path);
1100 * Add a change listener to the property. If "initial" is set call the
1101 * listener initially.
1103 void addChangeListener (SGPropertyChangeListener * listener,
1104 bool initial = false);
1108 * Remove a change listener from the property.
1110 void removeChangeListener (SGPropertyChangeListener * listener);
1114 * Fire a value change event to all listeners.
1116 void fireValueChanged ();
1120 * Fire a child-added event to all listeners.
1122 void fireChildAdded (SGPropertyNode * child);
1126 * Fire a child-removed event to all listeners.
1128 void fireChildRemoved (SGPropertyNode * child);
1132 * Clear any existing value and set the type to NONE.
1139 void fireValueChanged (SGPropertyNode * node);
1140 void fireChildAdded (SGPropertyNode * parent, SGPropertyNode * child);
1141 void fireChildRemoved (SGPropertyNode * parent, SGPropertyNode * child);
1144 * Protected constructor for making new nodes on demand.
1146 SGPropertyNode (const char * name, int index, SGPropertyNode * parent);
1151 // Get the raw value
1152 bool get_bool () const;
1153 int get_int () const;
1154 long get_long () const;
1155 float get_float () const;
1156 double get_double () const;
1157 const char * get_string () const;
1159 // Set the raw value
1160 bool set_bool (bool value);
1161 bool set_int (int value);
1162 bool set_long (long value);
1163 bool set_float (float value);
1164 bool set_double (double value);
1165 bool set_string (const char * value);
1169 * Get the value as a string.
1171 const char * make_string () const;
1175 * Trace a read access.
1177 void trace_read () const;
1181 * Trace a write access.
1183 void trace_write () const;
1190 mutable string _display_name;
1191 /// To avoid cyclic reference counting loops this shall not be a reference
1193 SGPropertyNode * _parent;
1194 vector<SGPropertyNode_ptr> _children;
1195 vector<SGPropertyNode_ptr> _removedChildren;
1196 mutable string _path;
1197 mutable string _buffer;
1198 hash_table * _path_cache;
1203 // The right kind of pointer...
1205 SGPropertyNode * alias;
1206 SGRawValue<bool> * bool_val;
1207 SGRawValue<int> * int_val;
1208 SGRawValue<long> * long_val;
1209 SGRawValue<float> * float_val;
1210 SGRawValue<double> * double_val;
1211 SGRawValue<const char *> * string_val;
1223 vector <SGPropertyChangeListener *> * _listeners;
1228 * A very simple hash table with no remove functionality.
1234 * An entry in a bucket in a hash table.
1240 const char * get_key () { return _key.c_str(); }
1241 void set_key (const char * key);
1242 SGPropertyNode * get_value () { return _value; }
1243 void set_value (SGPropertyNode * value);
1246 SGSharedPtr<SGPropertyNode> _value;
1251 * A bucket in a hash table.
1257 entry * get_entry (const char * key, bool create = false);
1258 void erase(const char * key);
1264 friend class bucket;
1268 SGPropertyNode * get (const char * key);
1269 void put (const char * key, SGPropertyNode * value);
1270 void erase(const char * key);
1273 unsigned int hashcode (const char * key);
1274 unsigned int _data_length;
1280 #endif // __PROPS_HXX