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
24 #include <simgear/compiler.h>
25 #include <simgear/debug/logstream.hxx>
30 #include <simgear/structure/SGReferenced.hxx>
31 #include <simgear/structure/SGSharedPtr.hxx>
35 #pragma warn A sloppy coder has defined NONE as a macro!
40 #pragma warn A sloppy coder has defined ALIAS as a macro!
45 #pragma warn A sloppy coder has defined UNSPECIFIED as a macro!
50 #pragma warn A sloppy coder has defined BOOL as a macro!
55 #pragma warn A sloppy coder has defined INT as a macro!
60 #pragma warn A sloppy coder has defined LONG as a macro!
65 #pragma warn A sloppy coder has defined FLOAT as a macro!
70 #pragma warn A sloppy coder has defined DOUBLE as a macro!
75 #pragma warn A sloppy coder has defined STRING as a macro!
81 ////////////////////////////////////////////////////////////////////////
84 // This is the mechanism that information-providing routines can
85 // use to link their own values to the property manager. Any
86 // SGValue can be tied to a raw value and then untied again.
88 // Note: we are forced to use inlined methods here to ensure
89 // that the templates will be instantiated. We're probably taking
90 // a small performance hit for that.
91 ////////////////////////////////////////////////////////////////////////
95 * Abstract base class for a raw value.
97 * <p>The property manager is implemented in two layers. The {@link
98 * SGPropertyNode} is the highest and most abstract layer,
99 * representing an LValue/RValue pair: it records the position of the
100 * property in the property tree and contains facilities for
101 * navigation to other nodes. It is guaranteed to be persistent: the
102 * {@link SGPropertyNode} will not change during a session, even if
103 * the property is bound and unbound multiple times.</p>
105 * <p>When the property value is not managed internally in the
106 * SGPropertyNode, the SGPropertyNode will contain a reference to an
107 * SGRawValue (this class), which provides an abstract way to get,
108 * set, and clone the underlying value. The SGRawValue may change
109 * frequently during a session as a value is retyped or bound and
110 * unbound to various data source, but the abstract SGPropertyNode
111 * layer insulates the application from those changes. The raw value
112 * contains no facilities for data binding or for type conversion: it
113 * is simply the abstraction of a primitive data type (or a compound
114 * data type, in the case of a string).</p>
116 * <p>The SGPropertyNode class always keeps a *copy* of a raw value,
117 * not the original one passed to it; if you override a derived class
118 * but do not replace the {@link #clone} method, strange things will
121 * <p>All derived SGRawValue classes must implement {@link #getValue},
122 * {@link #setValue}, and {@link #clone} for the appropriate type.</p>
124 * @see SGPropertyNode
125 * @see SGRawValuePointer
126 * @see SGRawValueFunctions
127 * @see SGRawValueFunctionsIndexed
128 * @see SGRawValueMethods
129 * @see SGRawValueMethodsIndexed
137 * The default underlying value for this type.
139 * Every raw value has a default; the default is false for a
140 * boolean, 0 for the various numeric values, and "" for a string.
141 * If additional types of raw values are added in the future, they
142 * may need different kinds of default values (such as epoch for a
143 * date type). The default value is used when creating new values.
145 static const T DefaultValue; // Default for this kind of raw value.
151 * Use the default value for this type.
159 virtual ~SGRawValue () {}
163 * Return the underlying value.
165 * @return The actual value for the property.
168 virtual T getValue () const = 0;
172 * Assign a new underlying value.
174 * If the new value cannot be set (because this is a read-only
175 * raw value, or because the new value is not acceptable for
176 * some reason) this method returns false and leaves the original
179 * @param value The actual value for the property.
180 * @return true if the value was set successfully, false otherwise.
183 virtual bool setValue (T value) = 0;
187 * Create a new deep copy of this raw value.
189 * The copy will contain its own version of the underlying value
192 * @return A deep copy of the current object.
194 virtual SGRawValue * clone () const = 0;
199 * A raw value bound to a pointer.
201 * This is the most efficient way to tie an external value, but also
202 * the most dangerous, because there is no way for the supplier to
203 * perform bounds checking and derived calculations except by polling
204 * the variable to see if it has changed. There is no default
205 * constructor, because this class would be meaningless without a
209 class SGRawValuePointer : public SGRawValue<T>
214 * Explicit pointer constructor.
216 * Create a new raw value bound to the value of the variable
217 * referenced by the pointer.
219 * @param ptr The pointer to the variable to which this raw value
222 SGRawValuePointer (T * ptr) : _ptr(ptr) {}
227 virtual ~SGRawValuePointer () {}
230 * Get the underlying value.
232 * This method will dereference the pointer and return the
235 virtual T getValue () const { return *_ptr; }
238 * Set the underlying value.
240 * This method will dereference the pointer and change the
243 virtual bool setValue (T value) { *_ptr = value; return true; }
246 * Create a copy of this raw value.
248 * The copy will use the same external pointer as the original.
250 virtual SGRawValue<T> * clone () const {
251 return new SGRawValuePointer<T>(_ptr);
260 * A value managed through static functions.
262 * A read-only value will not have a setter; a write-only value will
266 class SGRawValueFunctions : public SGRawValue<T>
271 * The template type of a static getter function.
273 typedef T (*getter_t)();
276 * The template type of a static setter function.
278 typedef void (*setter_t)(T);
281 * Explicit constructor.
283 * Create a new raw value bound to the getter and setter supplied.
285 * @param getter A static function for getting a value, or 0
286 * to read-disable the value.
287 * @param setter A static function for setting a value, or 0
288 * to write-disable the value.
290 SGRawValueFunctions (getter_t getter = 0, setter_t setter = 0)
291 : _getter(getter), _setter(setter) {}
296 virtual ~SGRawValueFunctions () {}
299 * Get the underlying value.
301 * This method will invoke the getter function to get a value.
302 * If no getter function was supplied, this method will always
303 * return the default value for the type.
305 virtual T getValue () const {
306 if (_getter) return (*_getter)();
307 else return SGRawValue<T>::DefaultValue;
311 * Set the underlying value.
313 * This method will invoke the setter function to change the
314 * underlying value. If no setter function was supplied, this
315 * method will return false.
317 virtual bool setValue (T value) {
318 if (_setter) { (*_setter)(value); return true; }
323 * Create a copy of this raw value, bound to the same functions.
325 virtual SGRawValue<T> * clone () const {
326 return new SGRawValueFunctions<T>(_getter,_setter);
336 * An indexed value bound to static functions.
338 * A read-only value will not have a setter; a write-only value will
339 * not have a getter. An indexed value is useful for binding one
340 * of a list of possible values (such as multiple engines for a
341 * plane). The index is hard-coded at creation time.
346 class SGRawValueFunctionsIndexed : public SGRawValue<T>
349 typedef T (*getter_t)(int);
350 typedef void (*setter_t)(int,T);
351 SGRawValueFunctionsIndexed (int index, getter_t getter = 0, setter_t setter = 0)
352 : _index(index), _getter(getter), _setter(setter) {}
353 virtual ~SGRawValueFunctionsIndexed () {}
354 virtual T getValue () const {
355 if (_getter) return (*_getter)(_index);
356 else return SGRawValue<T>::DefaultValue;
358 virtual bool setValue (T value) {
359 if (_setter) { (*_setter)(_index, value); return true; }
362 virtual SGRawValue<T> * clone () const {
363 return new SGRawValueFunctionsIndexed<T>(_index, _getter, _setter);
373 * A value managed through an object and access methods.
375 * A read-only value will not have a setter; a write-only value will
378 template <class C, class T>
379 class SGRawValueMethods : public SGRawValue<T>
382 typedef T (C::*getter_t)() const;
383 typedef void (C::*setter_t)(T);
384 SGRawValueMethods (C &obj, getter_t getter = 0, setter_t setter = 0)
385 : _obj(obj), _getter(getter), _setter(setter) {}
386 virtual ~SGRawValueMethods () {}
387 virtual T getValue () const {
388 if (_getter) { return (_obj.*_getter)(); }
389 else { return SGRawValue<T>::DefaultValue; }
391 virtual bool setValue (T value) {
392 if (_setter) { (_obj.*_setter)(value); return true; }
395 virtual SGRawValue<T> * clone () const {
396 return new SGRawValueMethods<C,T>(_obj, _getter, _setter);
406 * An indexed value managed through an object and access methods.
408 * A read-only value will not have a setter; a write-only value will
411 template <class C, class T>
412 class SGRawValueMethodsIndexed : public SGRawValue<T>
415 typedef T (C::*getter_t)(int) const;
416 typedef void (C::*setter_t)(int, T);
417 SGRawValueMethodsIndexed (C &obj, int index,
418 getter_t getter = 0, setter_t setter = 0)
419 : _obj(obj), _index(index), _getter(getter), _setter(setter) {}
420 virtual ~SGRawValueMethodsIndexed () {}
421 virtual T getValue () const {
422 if (_getter) { return (_obj.*_getter)(_index); }
423 else { return SGRawValue<T>::DefaultValue; }
425 virtual bool setValue (T value) {
426 if (_setter) { (_obj.*_setter)(_index, value); return true; }
429 virtual SGRawValue<T> * clone () const {
430 return new SGRawValueMethodsIndexed<C,T>(_obj, _index, _getter, _setter);
441 * The smart pointer that manage reference counting
443 class SGPropertyNode;
444 typedef SGSharedPtr<SGPropertyNode> SGPropertyNode_ptr;
445 typedef SGSharedPtr<const SGPropertyNode> SGConstPropertyNode_ptr;
449 * The property change listener interface.
451 * <p>Any class that needs to listen for property changes must implement
452 * this interface.</p>
454 class SGPropertyChangeListener
457 virtual ~SGPropertyChangeListener ();
458 virtual void valueChanged (SGPropertyNode * node);
459 virtual void childAdded (SGPropertyNode * parent, SGPropertyNode * child);
460 virtual void childRemoved (SGPropertyNode * parent, SGPropertyNode * child);
463 friend class SGPropertyNode;
464 virtual void register_property (SGPropertyNode * node);
465 virtual void unregister_property (SGPropertyNode * node);
468 std::vector<SGPropertyNode *> _properties;
474 * A node in a property tree.
476 class SGPropertyNode : public SGReferenced
484 MAX_STRING_LEN = 1024
488 * Property value types.
504 * Access mode attributes.
506 * <p>The ARCHIVE attribute is strictly advisory, and controls
507 * whether the property should normally be saved and restored.</p>
521 * Last used attribute
522 * Update as needed when enum Attribute is changed
524 static const int LAST_USED_ATTRIBUTE;
528 * Default constructor.
536 SGPropertyNode (const SGPropertyNode &node);
542 virtual ~SGPropertyNode ();
551 * Test whether this node contains a primitive leaf value.
553 bool hasValue () const { return (_type != NONE); }
557 * Get the node's simple (XML) name.
559 const char * getName () const { return _name.c_str(); }
563 * Get the node's pretty display name, with subscript when needed.
565 const char * getDisplayName (bool simplify = false) const;
569 * Get the node's integer index.
571 int getIndex () const { return _index; }
575 * Get a non-const pointer to the node's parent.
577 SGPropertyNode * getParent () { return _parent; }
581 * Get a const pointer to the node's parent.
583 const SGPropertyNode * getParent () const { return _parent; }
592 * Get the number of child nodes.
594 int nChildren () const { return _children.size(); }
598 * Get a child node by position (*NOT* index).
600 SGPropertyNode * getChild (int position);
604 * Get a const child node by position (*NOT* index).
606 const SGPropertyNode * getChild (int position) const;
610 * Test whether a named child exists.
612 bool hasChild (const char * name, int index = 0) const
614 return (getChild(name, index) != 0);
618 * Test whether a named child exists.
620 bool hasChild (const std::string& name, int index = 0) const
622 return (getChild(name, index) != 0);
626 * Create a child node after the last node with the same name.
628 SGPropertyNode * addChild (const char * name);
631 * Get a child node by name and index.
633 SGPropertyNode * getChild (const char * name, int index = 0,
634 bool create = false);
637 * Get a child node by name and index.
639 SGPropertyNode * getChild (const std::string& name, int index = 0,
641 { return getChild(name.c_str(), index, create); }
645 * Get a const child node by name and index.
647 const SGPropertyNode * getChild (const char * name, int index = 0) const;
650 * Get a const child node by name and index.
652 const SGPropertyNode * getChild (const std::string& name, int index = 0) const
653 { return getChild(name.c_str(), index); }
657 * Get a vector of all children with the specified name.
659 std::vector<SGPropertyNode_ptr> getChildren (const char * name) const;
662 * Get a vector of all children with the specified name.
664 std::vector<SGPropertyNode_ptr> getChildren (const std::string& name) const
665 { return getChildren(name.c_str()); }
668 * Remove child by position.
670 SGPropertyNode_ptr removeChild (int pos, bool keep = true);
674 * Remove a child node
676 SGPropertyNode_ptr removeChild (const char * name, int index = 0,
680 * Remove a child node
682 SGPropertyNode_ptr removeChild (const std::string& name, int index = 0,
684 { return removeChild(name.c_str(), index, keep); }
687 * Remove all children with the specified name.
689 std::vector<SGPropertyNode_ptr> removeChildren (const char * name,
694 * Remove all children with the specified name.
696 std::vector<SGPropertyNode_ptr> removeChildren (const std::string& name,
698 { return removeChildren(name.c_str(), keep); }
706 * Alias this node's leaf value to another's.
708 bool alias (SGPropertyNode * target);
712 * Alias this node's leaf value to another's by relative path.
714 bool alias (const char * path);
717 * Alias this node's leaf value to another's by relative path.
719 bool alias (const std::string& path)
720 { return alias(path.c_str()); }
724 * Remove any alias for this node.
730 * Test whether the node's leaf value is aliased to another's.
732 bool isAlias () const { return (_type == ALIAS); }
736 * Get a non-const pointer to the current alias target, if any.
738 SGPropertyNode * getAliasTarget ();
742 * Get a const pointer to the current alias target, if any.
744 const SGPropertyNode * getAliasTarget () const;
753 * Get the path to this node from the root.
755 const char * getPath (bool simplify = false) const;
759 * Get a pointer to the root node.
761 SGPropertyNode * getRootNode ();
765 * Get a const pointer to the root node.
767 const SGPropertyNode * getRootNode () const;
771 * Get a pointer to another node by relative path.
773 SGPropertyNode * getNode (const char * relative_path, bool create = false);
776 * Get a pointer to another node by relative path.
778 SGPropertyNode * getNode (const std::string& relative_path, bool create = false)
779 { return getNode(relative_path.c_str(), create); }
782 * Get a pointer to another node by relative path.
784 * This method leaves the index off the last member of the path,
785 * so that the user can specify it separately (and save some
786 * string building). For example, getNode("/bar[1]/foo", 3) is
787 * exactly equivalent to getNode("bar[1]/foo[3]"). The index
788 * provided overrides any given in the path itself for the last
791 SGPropertyNode * getNode (const char * relative_path, int index,
792 bool create = false);
795 * Get a pointer to another node by relative path.
797 * This method leaves the index off the last member of the path,
798 * so that the user can specify it separately (and save some
799 * string building). For example, getNode("/bar[1]/foo", 3) is
800 * exactly equivalent to getNode("bar[1]/foo[3]"). The index
801 * provided overrides any given in the path itself for the last
804 SGPropertyNode * getNode (const std::string& relative_path, int index,
806 { return getNode(relative_path.c_str(), index, create); }
809 * Get a const pointer to another node by relative path.
811 const SGPropertyNode * getNode (const char * relative_path) const;
814 * Get a const pointer to another node by relative path.
816 const SGPropertyNode * getNode (const std::string& relative_path) const
817 { return getNode(relative_path.c_str()); }
821 * Get a const pointer to another node by relative path.
823 * This method leaves the index off the last member of the path,
824 * so that the user can specify it separate.
826 const SGPropertyNode * getNode (const char * relative_path,
830 * Get a const pointer to another node by relative path.
832 * This method leaves the index off the last member of the path,
833 * so that the user can specify it separate.
835 const SGPropertyNode * getNode (const std::string& relative_path,
837 { return getNode(relative_path.c_str(), index); }
844 * Check a single mode attribute for the property node.
846 bool getAttribute (Attribute attr) const { return ((_attr & attr) != 0); }
850 * Set a single mode attribute for the property node.
852 void setAttribute (Attribute attr, bool state) {
853 (state ? _attr |= attr : _attr &= ~attr);
858 * Get all of the mode attributes for the property node.
860 int getAttributes () const { return _attr; }
864 * Set all of the mode attributes for the property node.
866 void setAttributes (int attr) { _attr = attr; }
870 // Leaf Value (primitive).
875 * Get the type of leaf value, if any, for this node.
877 Type getType () const;
881 * Get a bool value for this node.
883 bool getBoolValue () const;
887 * Get an int value for this node.
889 int getIntValue () const;
893 * Get a long int value for this node.
895 long getLongValue () const;
899 * Get a float value for this node.
901 float getFloatValue () const;
905 * Get a double value for this node.
907 double getDoubleValue () const;
911 * Get a string value for this node.
913 const char * getStringValue () const;
918 * Set a bool value for this node.
920 bool setBoolValue (bool value);
924 * Set an int value for this node.
926 bool setIntValue (int value);
930 * Set a long int value for this node.
932 bool setLongValue (long value);
936 * Set a float value for this node.
938 bool setFloatValue (float value);
942 * Set a double value for this node.
944 bool setDoubleValue (double value);
948 * Set a string value for this node.
950 bool setStringValue (const char * value);
953 * Set a string value for this node.
955 bool setStringValue (const std::string& value)
956 { return setStringValue(value.c_str()); }
960 * Set a value of unspecified type for this node.
962 bool setUnspecifiedValue (const char * value);
971 * Test whether this node is bound to an external data source.
973 bool isTied () const { return _tied; }
977 * Bind this node to an external bool source.
979 bool tie (const SGRawValue<bool> &rawValue, bool useDefault = true);
983 * Bind this node to an external int source.
985 bool tie (const SGRawValue<int> &rawValue, bool useDefault = true);
989 * Bind this node to an external long int source.
991 bool tie (const SGRawValue<long> &rawValue, bool useDefault = true);
995 * Bind this node to an external float source.
997 bool tie (const SGRawValue<float> &rawValue, bool useDefault = true);
1001 * Bind this node to an external double source.
1003 bool tie (const SGRawValue<double> &rawValue, bool useDefault = true);
1007 * Bind this node to an external string source.
1009 bool tie (const SGRawValue<const char *> &rawValue, bool useDefault = true);
1013 * Unbind this node from any external data source.
1019 // Convenience methods using paths.
1020 // TODO: add attribute methods
1025 * Get another node's type.
1027 Type getType (const char * relative_path) const;
1030 * Get another node's type.
1032 Type getType (const std::string& relative_path) const
1033 { return getType(relative_path.c_str()); }
1036 * Test whether another node has a leaf value.
1038 bool hasValue (const char * relative_path) const;
1041 * Test whether another node has a leaf value.
1043 bool hasValue (const std::string& relative_path) const
1044 { return hasValue(relative_path.c_str()); }
1047 * Get another node's value as a bool.
1049 bool getBoolValue (const char * relative_path,
1050 bool defaultValue = false) const;
1053 * Get another node's value as a bool.
1055 bool getBoolValue (const std::string& relative_path,
1056 bool defaultValue = false) const
1057 { return getBoolValue(relative_path.c_str(), defaultValue); }
1060 * Get another node's value as an int.
1062 int getIntValue (const char * relative_path,
1063 int defaultValue = 0) const;
1066 * Get another node's value as an int.
1068 int getIntValue (const std::string& relative_path,
1069 int defaultValue = 0) const
1070 { return getIntValue(relative_path.c_str(), defaultValue); }
1074 * Get another node's value as a long int.
1076 long getLongValue (const char * relative_path,
1077 long defaultValue = 0L) const;
1080 * Get another node's value as a long int.
1082 long getLongValue (const std::string& relative_path,
1083 long defaultValue = 0L) const
1084 { return getLongValue(relative_path.c_str(), defaultValue); }
1087 * Get another node's value as a float.
1089 float getFloatValue (const char * relative_path,
1090 float defaultValue = 0.0f) const;
1093 * Get another node's value as a float.
1095 float getFloatValue (const std::string& relative_path,
1096 float defaultValue = 0.0f) const
1097 { return getFloatValue(relative_path.c_str(), defaultValue); }
1101 * Get another node's value as a double.
1103 double getDoubleValue (const char * relative_path,
1104 double defaultValue = 0.0) const;
1107 * Get another node's value as a double.
1109 double getDoubleValue (const std::string& relative_path,
1110 double defaultValue = 0.0) const
1111 { return getDoubleValue(relative_path.c_str(), defaultValue); }
1114 * Get another node's value as a string.
1116 const char * getStringValue (const char * relative_path,
1117 const char * defaultValue = "") const;
1121 * Get another node's value as a string.
1123 const char * getStringValue (const std::string& relative_path,
1124 const char * defaultValue = "") const
1125 { return getStringValue(relative_path.c_str(), defaultValue); }
1129 * Set another node's value as a bool.
1131 bool setBoolValue (const char * relative_path, bool value);
1134 * Set another node's value as a bool.
1136 bool setBoolValue (const std::string& relative_path, bool value)
1137 { return setBoolValue(relative_path.c_str(), value); }
1141 * Set another node's value as an int.
1143 bool setIntValue (const char * relative_path, int value);
1146 * Set another node's value as an int.
1148 bool setIntValue (const std::string& relative_path, int value)
1149 { return setIntValue(relative_path.c_str(), value); }
1153 * Set another node's value as a long int.
1155 bool setLongValue (const char * relative_path, long value);
1158 * Set another node's value as a long int.
1160 bool setLongValue (const std::string& relative_path, long value)
1161 { return setLongValue(relative_path.c_str(), value); }
1165 * Set another node's value as a float.
1167 bool setFloatValue (const char * relative_path, float value);
1170 * Set another node's value as a float.
1172 bool setFloatValue (const std::string& relative_path, float value)
1173 { return setFloatValue(relative_path.c_str(), value); }
1177 * Set another node's value as a double.
1179 bool setDoubleValue (const char * relative_path, double value);
1182 * Set another node's value as a double.
1184 bool setDoubleValue (const std::string& relative_path, double value)
1185 { return setDoubleValue(relative_path.c_str(), value); }
1189 * Set another node's value as a string.
1191 bool setStringValue (const char * relative_path, const char * value);
1194 * Set another node's value as a string.
1196 bool setStringValue (const std::string& relative_path, const char * value)
1197 { return setStringValue(relative_path.c_str(), value); }
1201 * Set another node's value with no specified type.
1203 bool setUnspecifiedValue (const char * relative_path, const char * value);
1207 * Test whether another node is bound to an external data source.
1209 bool isTied (const char * relative_path) const;
1212 * Test whether another node is bound to an external data source.
1214 bool isTied (const std::string& relative_path) const
1215 { return isTied(relative_path.c_str()); }
1218 * Bind another node to an external bool source.
1220 bool tie (const char * relative_path, const SGRawValue<bool> &rawValue,
1221 bool useDefault = true);
1224 * Bind another node to an external bool source.
1226 bool tie (const std::string& relative_path, const SGRawValue<bool> &rawValue,
1227 bool useDefault = true)
1228 { return tie(relative_path.c_str(), rawValue, useDefault); }
1232 * Bind another node to an external int source.
1234 bool tie (const char * relative_path, const SGRawValue<int> &rawValue,
1235 bool useDefault = true);
1238 * Bind another node to an external int source.
1240 bool tie (const std::string& relative_path, const SGRawValue<int> &rawValue,
1241 bool useDefault = true)
1242 { return tie(relative_path.c_str(), rawValue, useDefault); }
1246 * Bind another node to an external long int source.
1248 bool tie (const char * relative_path, const SGRawValue<long> &rawValue,
1249 bool useDefault = true);
1252 * Bind another node to an external long int source.
1254 bool tie (const std::string& relative_path, const SGRawValue<long> &rawValue,
1255 bool useDefault = true)
1256 { return tie(relative_path.c_str(), rawValue, useDefault); }
1260 * Bind another node to an external float source.
1262 bool tie (const char * relative_path, const SGRawValue<float> &rawValue,
1263 bool useDefault = true);
1266 * Bind another node to an external float source.
1268 bool tie (const std::string& relative_path, const SGRawValue<float> &rawValue,
1269 bool useDefault = true)
1270 { return tie(relative_path.c_str(), rawValue, useDefault); }
1274 * Bind another node to an external double source.
1276 bool tie (const char * relative_path, const SGRawValue<double> &rawValue,
1277 bool useDefault = true);
1280 * Bind another node to an external double source.
1282 bool tie (const std::string& relative_path, const SGRawValue<double> &rawValue,
1283 bool useDefault = true)
1284 { return tie(relative_path.c_str(), rawValue, useDefault); }
1288 * Bind another node to an external string source.
1290 bool tie (const char * relative_path, const SGRawValue<const char *> &rawValue,
1291 bool useDefault = true);
1294 * Bind another node to an external string source.
1296 bool tie (const std::string& relative_path, const SGRawValue<const char*> &rawValue,
1297 bool useDefault = true)
1298 { return tie(relative_path.c_str(), rawValue, useDefault); }
1302 * Unbind another node from any external data source.
1304 bool untie (const char * relative_path);
1307 * Unbind another node from any external data source.
1309 bool untie (const std::string& relative_path)
1310 { return untie(relative_path.c_str()); }
1314 * Add a change listener to the property. If "initial" is set call the
1315 * listener initially.
1317 void addChangeListener (SGPropertyChangeListener * listener,
1318 bool initial = false);
1322 * Remove a change listener from the property.
1324 void removeChangeListener (SGPropertyChangeListener * listener);
1328 * Get the number of listeners.
1330 int nListeners () const { return _listeners ? _listeners->size() : 0; }
1334 * Fire a value change event to all listeners.
1336 void fireValueChanged ();
1340 * Fire a child-added event to all listeners.
1342 void fireChildAdded (SGPropertyNode * child);
1346 * Fire a child-removed event to all listeners.
1348 void fireChildRemoved (SGPropertyNode * child);
1352 * Clear any existing value and set the type to NONE.
1359 void fireValueChanged (SGPropertyNode * node);
1360 void fireChildAdded (SGPropertyNode * parent, SGPropertyNode * child);
1361 void fireChildRemoved (SGPropertyNode * parent, SGPropertyNode * child);
1364 * Protected constructor for making new nodes on demand.
1366 SGPropertyNode (const char * name, int index, SGPropertyNode * parent);
1371 // Get the raw value
1372 bool get_bool () const;
1373 int get_int () const;
1374 long get_long () const;
1375 float get_float () const;
1376 double get_double () const;
1377 const char * get_string () const;
1379 // Set the raw value
1380 bool set_bool (bool value);
1381 bool set_int (int value);
1382 bool set_long (long value);
1383 bool set_float (float value);
1384 bool set_double (double value);
1385 bool set_string (const char * value);
1389 * Get the value as a string.
1391 const char * make_string () const;
1395 * Trace a read access.
1397 void trace_read () const;
1401 * Trace a write access.
1403 void trace_write () const;
1407 * Remove this node from all nodes that link to it in their path cache.
1409 void remove_from_path_caches();
1416 mutable std::string _display_name;
1417 /// To avoid cyclic reference counting loops this shall not be a reference
1419 SGPropertyNode * _parent;
1420 std::vector<SGPropertyNode_ptr> _children;
1421 std::vector<SGPropertyNode_ptr> _removedChildren;
1422 std::vector<hash_table *> _linkedNodes;
1423 mutable std::string _path;
1424 mutable std::string _buffer;
1425 hash_table * _path_cache;
1430 // The right kind of pointer...
1432 SGPropertyNode * alias;
1433 SGRawValue<bool> * bool_val;
1434 SGRawValue<int> * int_val;
1435 SGRawValue<long> * long_val;
1436 SGRawValue<float> * float_val;
1437 SGRawValue<double> * double_val;
1438 SGRawValue<const char *> * string_val;
1450 std::vector<SGPropertyChangeListener *> * _listeners;
1454 * Register/unregister node that links to this node in its path cache.
1456 void add_linked_node (hash_table * node) { _linkedNodes.push_back(node); }
1457 bool remove_linked_node (hash_table * node);
1462 * A very simple hash table.
1468 * An entry in a bucket in a hash table.
1474 const char * get_key () { return _key.c_str(); }
1475 void set_key (const char * key);
1476 SGPropertyNode * get_value () { return _value; }
1477 void set_value (SGPropertyNode * value);
1480 SGSharedPtr<SGPropertyNode> _value;
1485 * A bucket in a hash table.
1491 entry * get_entry (const char * key, bool create = false);
1492 bool erase (SGPropertyNode * node);
1493 void clear (hash_table * owner);
1499 friend class bucket;
1503 SGPropertyNode * get (const char * key);
1504 void put (const char * key, SGPropertyNode * value);
1505 bool erase (SGPropertyNode * node);
1508 unsigned int hashcode (const char * key);
1509 unsigned int _data_length;
1515 // Convenice functions for use in templates
1516 template<typename T>
1517 T getValue(const SGPropertyNode*);
1520 inline bool getValue<bool>(const SGPropertyNode* node) { return node->getBoolValue(); }
1523 inline int getValue<int>(const SGPropertyNode* node) { return node->getIntValue(); }
1526 inline long getValue<long>(const SGPropertyNode* node) { return node->getLongValue(); }
1529 inline float getValue<float>(const SGPropertyNode* node)
1531 return node->getFloatValue();
1535 inline double getValue<double>(const SGPropertyNode* node)
1537 return node->getDoubleValue();
1541 inline const char * getValue<const char*>(const SGPropertyNode* node)
1543 return node->getStringValue ();
1546 inline bool setValue(SGPropertyNode* node, bool value)
1548 return node->setBoolValue(value);
1551 inline bool setValue(SGPropertyNode* node, int value)
1553 return node->setIntValue(value);
1556 inline bool setValue(SGPropertyNode* node, long value)
1558 return node->setLongValue(value);
1561 inline bool setValue(SGPropertyNode* node, float value)
1563 return node->setFloatValue(value);
1566 inline bool setValue(SGPropertyNode* node, double value)
1568 return node->setDoubleValue(value);
1571 inline bool setValue(SGPropertyNode* node, const char* value)
1573 return node->setStringValue(value);
1576 inline bool setValue (SGPropertyNode* node, const std::string& value)
1578 return node->setStringValue(value.c_str());
1581 #endif // __PROPS_HXX