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);
627 * Get a child node by name and index.
629 SGPropertyNode * getChild (const char * name, int index = 0,
630 bool create = false);
633 * Get a child node by name and index.
635 SGPropertyNode * getChild (const std::string& name, int index = 0,
637 { return getChild(name.c_str(), index, create); }
641 * Get a const child node by name and index.
643 const SGPropertyNode * getChild (const char * name, int index = 0) const;
646 * Get a const child node by name and index.
648 const SGPropertyNode * getChild (const std::string& name, int index = 0) const
649 { return getChild(name.c_str(), index); }
653 * Get a vector of all children with the specified name.
655 std::vector<SGPropertyNode_ptr> getChildren (const char * name) const;
658 * Get a vector of all children with the specified name.
660 std::vector<SGPropertyNode_ptr> getChildren (const std::string& name) const
661 { return getChildren(name.c_str()); }
664 * Remove child by position.
666 SGPropertyNode_ptr removeChild (int pos, bool keep = true);
670 * Remove a child node
672 SGPropertyNode_ptr removeChild (const char * name, int index = 0,
676 * Remove a child node
678 SGPropertyNode_ptr removeChild (const std::string& name, int index = 0,
680 { return removeChild(name.c_str(), index, keep); }
683 * Remove all children with the specified name.
685 std::vector<SGPropertyNode_ptr> removeChildren (const char * name,
690 * Remove all children with the specified name.
692 std::vector<SGPropertyNode_ptr> removeChildren (const std::string& name,
694 { return removeChildren(name.c_str(), keep); }
702 * Alias this node's leaf value to another's.
704 bool alias (SGPropertyNode * target);
708 * Alias this node's leaf value to another's by relative path.
710 bool alias (const char * path);
713 * Alias this node's leaf value to another's by relative path.
715 bool alias (const std::string& path)
716 { return alias(path.c_str()); }
720 * Remove any alias for this node.
726 * Test whether the node's leaf value is aliased to another's.
728 bool isAlias () const { return (_type == ALIAS); }
732 * Get a non-const pointer to the current alias target, if any.
734 SGPropertyNode * getAliasTarget ();
738 * Get a const pointer to the current alias target, if any.
740 const SGPropertyNode * getAliasTarget () const;
749 * Get the path to this node from the root.
751 const char * getPath (bool simplify = false) const;
755 * Get a pointer to the root node.
757 SGPropertyNode * getRootNode ();
761 * Get a const pointer to the root node.
763 const SGPropertyNode * getRootNode () const;
767 * Get a pointer to another node by relative path.
769 SGPropertyNode * getNode (const char * relative_path, bool create = false);
772 * Get a pointer to another node by relative path.
774 SGPropertyNode * getNode (const std::string& relative_path, bool create = false)
775 { return getNode(relative_path.c_str(), create); }
778 * Get a pointer to another node by relative path.
780 * This method leaves the index off the last member of the path,
781 * so that the user can specify it separately (and save some
782 * string building). For example, getNode("/bar[1]/foo", 3) is
783 * exactly equivalent to getNode("bar[1]/foo[3]"). The index
784 * provided overrides any given in the path itself for the last
787 SGPropertyNode * getNode (const char * relative_path, int index,
788 bool create = false);
791 * Get a pointer to another node by relative path.
793 * This method leaves the index off the last member of the path,
794 * so that the user can specify it separately (and save some
795 * string building). For example, getNode("/bar[1]/foo", 3) is
796 * exactly equivalent to getNode("bar[1]/foo[3]"). The index
797 * provided overrides any given in the path itself for the last
800 SGPropertyNode * getNode (const std::string& relative_path, int index,
802 { return getNode(relative_path.c_str(), index, create); }
805 * Get a const pointer to another node by relative path.
807 const SGPropertyNode * getNode (const char * relative_path) const;
810 * Get a const pointer to another node by relative path.
812 const SGPropertyNode * getNode (const std::string& relative_path) const
813 { return getNode(relative_path.c_str()); }
817 * Get a const pointer to another node by relative path.
819 * This method leaves the index off the last member of the path,
820 * so that the user can specify it separate.
822 const SGPropertyNode * getNode (const char * relative_path,
826 * Get a const pointer to another node by relative path.
828 * This method leaves the index off the last member of the path,
829 * so that the user can specify it separate.
831 const SGPropertyNode * getNode (const std::string& relative_path,
833 { return getNode(relative_path.c_str(), index); }
840 * Check a single mode attribute for the property node.
842 bool getAttribute (Attribute attr) const { return ((_attr & attr) != 0); }
846 * Set a single mode attribute for the property node.
848 void setAttribute (Attribute attr, bool state) {
849 (state ? _attr |= attr : _attr &= ~attr);
854 * Get all of the mode attributes for the property node.
856 int getAttributes () const { return _attr; }
860 * Set all of the mode attributes for the property node.
862 void setAttributes (int attr) { _attr = attr; }
866 // Leaf Value (primitive).
871 * Get the type of leaf value, if any, for this node.
873 Type getType () const;
877 * Get a bool value for this node.
879 bool getBoolValue () const;
883 * Get an int value for this node.
885 int getIntValue () const;
889 * Get a long int value for this node.
891 long getLongValue () const;
895 * Get a float value for this node.
897 float getFloatValue () const;
901 * Get a double value for this node.
903 double getDoubleValue () const;
907 * Get a string value for this node.
909 const char * getStringValue () const;
914 * Set a bool value for this node.
916 bool setBoolValue (bool value);
920 * Set an int value for this node.
922 bool setIntValue (int value);
926 * Set a long int value for this node.
928 bool setLongValue (long value);
932 * Set a float value for this node.
934 bool setFloatValue (float value);
938 * Set a double value for this node.
940 bool setDoubleValue (double value);
944 * Set a string value for this node.
946 bool setStringValue (const char * value);
949 * Set a string value for this node.
951 bool setStringValue (const std::string& value)
952 { return setStringValue(value.c_str()); }
956 * Set a value of unspecified type for this node.
958 bool setUnspecifiedValue (const char * value);
967 * Test whether this node is bound to an external data source.
969 bool isTied () const { return _tied; }
973 * Bind this node to an external bool source.
975 bool tie (const SGRawValue<bool> &rawValue, bool useDefault = true);
979 * Bind this node to an external int source.
981 bool tie (const SGRawValue<int> &rawValue, bool useDefault = true);
985 * Bind this node to an external long int source.
987 bool tie (const SGRawValue<long> &rawValue, bool useDefault = true);
991 * Bind this node to an external float source.
993 bool tie (const SGRawValue<float> &rawValue, bool useDefault = true);
997 * Bind this node to an external double source.
999 bool tie (const SGRawValue<double> &rawValue, bool useDefault = true);
1003 * Bind this node to an external string source.
1005 bool tie (const SGRawValue<const char *> &rawValue, bool useDefault = true);
1009 * Unbind this node from any external data source.
1015 // Convenience methods using paths.
1016 // TODO: add attribute methods
1021 * Get another node's type.
1023 Type getType (const char * relative_path) const;
1026 * Get another node's type.
1028 Type getType (const std::string& relative_path) const
1029 { return getType(relative_path.c_str()); }
1032 * Test whether another node has a leaf value.
1034 bool hasValue (const char * relative_path) const;
1037 * Test whether another node has a leaf value.
1039 bool hasValue (const std::string& relative_path) const
1040 { return hasValue(relative_path.c_str()); }
1043 * Get another node's value as a bool.
1045 bool getBoolValue (const char * relative_path,
1046 bool defaultValue = false) const;
1049 * Get another node's value as a bool.
1051 bool getBoolValue (const std::string& relative_path,
1052 bool defaultValue = false) const
1053 { return getBoolValue(relative_path.c_str(), defaultValue); }
1056 * Get another node's value as an int.
1058 int getIntValue (const char * relative_path,
1059 int defaultValue = 0) const;
1062 * Get another node's value as an int.
1064 int getIntValue (const std::string& relative_path,
1065 int defaultValue = 0) const
1066 { return getIntValue(relative_path.c_str(), defaultValue); }
1070 * Get another node's value as a long int.
1072 long getLongValue (const char * relative_path,
1073 long defaultValue = 0L) const;
1076 * Get another node's value as a long int.
1078 long getLongValue (const std::string& relative_path,
1079 long defaultValue = 0L) const
1080 { return getLongValue(relative_path.c_str(), defaultValue); }
1083 * Get another node's value as a float.
1085 float getFloatValue (const char * relative_path,
1086 float defaultValue = 0.0f) const;
1089 * Get another node's value as a float.
1091 float getFloatValue (const std::string& relative_path,
1092 float defaultValue = 0.0f) const
1093 { return getFloatValue(relative_path.c_str(), defaultValue); }
1097 * Get another node's value as a double.
1099 double getDoubleValue (const char * relative_path,
1100 double defaultValue = 0.0) const;
1103 * Get another node's value as a double.
1105 double getDoubleValue (const std::string& relative_path,
1106 double defaultValue = 0.0) const
1107 { return getDoubleValue(relative_path.c_str(), defaultValue); }
1110 * Get another node's value as a string.
1112 const char * getStringValue (const char * relative_path,
1113 const char * defaultValue = "") const;
1117 * Get another node's value as a string.
1119 const char * getStringValue (const std::string& relative_path,
1120 const char * defaultValue = "") const
1121 { return getStringValue(relative_path.c_str(), defaultValue); }
1125 * Set another node's value as a bool.
1127 bool setBoolValue (const char * relative_path, bool value);
1130 * Set another node's value as a bool.
1132 bool setBoolValue (const std::string& relative_path, bool value)
1133 { return setBoolValue(relative_path.c_str(), value); }
1137 * Set another node's value as an int.
1139 bool setIntValue (const char * relative_path, int value);
1142 * Set another node's value as an int.
1144 bool setIntValue (const std::string& relative_path, int value)
1145 { return setIntValue(relative_path.c_str(), value); }
1149 * Set another node's value as a long int.
1151 bool setLongValue (const char * relative_path, long value);
1154 * Set another node's value as a long int.
1156 bool setLongValue (const std::string& relative_path, long value)
1157 { return setLongValue(relative_path.c_str(), value); }
1161 * Set another node's value as a float.
1163 bool setFloatValue (const char * relative_path, float value);
1166 * Set another node's value as a float.
1168 bool setFloatValue (const std::string& relative_path, float value)
1169 { return setFloatValue(relative_path.c_str(), value); }
1173 * Set another node's value as a double.
1175 bool setDoubleValue (const char * relative_path, double value);
1178 * Set another node's value as a double.
1180 bool setDoubleValue (const std::string& relative_path, double value)
1181 { return setDoubleValue(relative_path.c_str(), value); }
1185 * Set another node's value as a string.
1187 bool setStringValue (const char * relative_path, const char * value);
1190 * Set another node's value as a string.
1192 bool setStringValue (const std::string& relative_path, const char * value)
1193 { return setStringValue(relative_path.c_str(), value); }
1197 * Set another node's value with no specified type.
1199 bool setUnspecifiedValue (const char * relative_path, const char * value);
1203 * Test whether another node is bound to an external data source.
1205 bool isTied (const char * relative_path) const;
1208 * Test whether another node is bound to an external data source.
1210 bool isTied (const std::string& relative_path) const
1211 { return isTied(relative_path.c_str()); }
1214 * Bind another node to an external bool source.
1216 bool tie (const char * relative_path, const SGRawValue<bool> &rawValue,
1217 bool useDefault = true);
1220 * Bind another node to an external bool source.
1222 bool tie (const std::string& relative_path, const SGRawValue<bool> &rawValue,
1223 bool useDefault = true)
1224 { return tie(relative_path.c_str(), rawValue, useDefault); }
1228 * Bind another node to an external int source.
1230 bool tie (const char * relative_path, const SGRawValue<int> &rawValue,
1231 bool useDefault = true);
1234 * Bind another node to an external int source.
1236 bool tie (const std::string& relative_path, const SGRawValue<int> &rawValue,
1237 bool useDefault = true)
1238 { return tie(relative_path.c_str(), rawValue, useDefault); }
1242 * Bind another node to an external long int source.
1244 bool tie (const char * relative_path, const SGRawValue<long> &rawValue,
1245 bool useDefault = true);
1248 * Bind another node to an external long int source.
1250 bool tie (const std::string& relative_path, const SGRawValue<long> &rawValue,
1251 bool useDefault = true)
1252 { return tie(relative_path.c_str(), rawValue, useDefault); }
1256 * Bind another node to an external float source.
1258 bool tie (const char * relative_path, const SGRawValue<float> &rawValue,
1259 bool useDefault = true);
1262 * Bind another node to an external float source.
1264 bool tie (const std::string& relative_path, const SGRawValue<float> &rawValue,
1265 bool useDefault = true)
1266 { return tie(relative_path.c_str(), rawValue, useDefault); }
1270 * Bind another node to an external double source.
1272 bool tie (const char * relative_path, const SGRawValue<double> &rawValue,
1273 bool useDefault = true);
1276 * Bind another node to an external double source.
1278 bool tie (const std::string& relative_path, const SGRawValue<double> &rawValue,
1279 bool useDefault = true)
1280 { return tie(relative_path.c_str(), rawValue, useDefault); }
1284 * Bind another node to an external string source.
1286 bool tie (const char * relative_path, const SGRawValue<const char *> &rawValue,
1287 bool useDefault = true);
1290 * Bind another node to an external string source.
1292 bool tie (const std::string& relative_path, const SGRawValue<const char*> &rawValue,
1293 bool useDefault = true)
1294 { return tie(relative_path.c_str(), rawValue, useDefault); }
1298 * Unbind another node from any external data source.
1300 bool untie (const char * relative_path);
1303 * Unbind another node from any external data source.
1305 bool untie (const std::string& relative_path)
1306 { return untie(relative_path.c_str()); }
1310 * Add a change listener to the property. If "initial" is set call the
1311 * listener initially.
1313 void addChangeListener (SGPropertyChangeListener * listener,
1314 bool initial = false);
1318 * Remove a change listener from the property.
1320 void removeChangeListener (SGPropertyChangeListener * listener);
1324 * Get the number of listeners.
1326 int nListeners () const { return _listeners ? _listeners->size() : 0; }
1330 * Fire a value change event to all listeners.
1332 void fireValueChanged ();
1336 * Fire a child-added event to all listeners.
1338 void fireChildAdded (SGPropertyNode * child);
1342 * Fire a child-removed event to all listeners.
1344 void fireChildRemoved (SGPropertyNode * child);
1348 * Clear any existing value and set the type to NONE.
1355 void fireValueChanged (SGPropertyNode * node);
1356 void fireChildAdded (SGPropertyNode * parent, SGPropertyNode * child);
1357 void fireChildRemoved (SGPropertyNode * parent, SGPropertyNode * child);
1360 * Protected constructor for making new nodes on demand.
1362 SGPropertyNode (const char * name, int index, SGPropertyNode * parent);
1367 // Get the raw value
1368 bool get_bool () const;
1369 int get_int () const;
1370 long get_long () const;
1371 float get_float () const;
1372 double get_double () const;
1373 const char * get_string () const;
1375 // Set the raw value
1376 bool set_bool (bool value);
1377 bool set_int (int value);
1378 bool set_long (long value);
1379 bool set_float (float value);
1380 bool set_double (double value);
1381 bool set_string (const char * value);
1385 * Get the value as a string.
1387 const char * make_string () const;
1391 * Trace a read access.
1393 void trace_read () const;
1397 * Trace a write access.
1399 void trace_write () const;
1403 * Remove this node from all nodes that link to it in their path cache.
1405 void remove_from_path_caches();
1412 mutable std::string _display_name;
1413 /// To avoid cyclic reference counting loops this shall not be a reference
1415 SGPropertyNode * _parent;
1416 std::vector<SGPropertyNode_ptr> _children;
1417 std::vector<SGPropertyNode_ptr> _removedChildren;
1418 std::vector<hash_table *> _linkedNodes;
1419 mutable std::string _path;
1420 mutable std::string _buffer;
1421 hash_table * _path_cache;
1426 // The right kind of pointer...
1428 SGPropertyNode * alias;
1429 SGRawValue<bool> * bool_val;
1430 SGRawValue<int> * int_val;
1431 SGRawValue<long> * long_val;
1432 SGRawValue<float> * float_val;
1433 SGRawValue<double> * double_val;
1434 SGRawValue<const char *> * string_val;
1446 std::vector<SGPropertyChangeListener *> * _listeners;
1450 * Register/unregister node that links to this node in its path cache.
1452 void add_linked_node (hash_table * node) { _linkedNodes.push_back(node); }
1453 bool remove_linked_node (hash_table * node);
1458 * A very simple hash table.
1464 * An entry in a bucket in a hash table.
1470 const char * get_key () { return _key.c_str(); }
1471 void set_key (const char * key);
1472 SGPropertyNode * get_value () { return _value; }
1473 void set_value (SGPropertyNode * value);
1476 SGSharedPtr<SGPropertyNode> _value;
1481 * A bucket in a hash table.
1487 entry * get_entry (const char * key, bool create = false);
1488 bool erase (SGPropertyNode * node);
1489 void clear (hash_table * owner);
1495 friend class bucket;
1499 SGPropertyNode * get (const char * key);
1500 void put (const char * key, SGPropertyNode * value);
1501 bool erase (SGPropertyNode * node);
1504 unsigned int hashcode (const char * key);
1505 unsigned int _data_length;
1511 // Convenice functions for use in templates
1512 template<typename T>
1513 T getValue(const SGPropertyNode*);
1516 inline bool getValue<bool>(const SGPropertyNode* node) { return node->getBoolValue(); }
1519 inline int getValue<int>(const SGPropertyNode* node) { return node->getIntValue(); }
1522 inline long getValue<long>(const SGPropertyNode* node) { return node->getLongValue(); }
1525 inline float getValue<float>(const SGPropertyNode* node)
1527 return node->getFloatValue();
1531 inline double getValue<double>(const SGPropertyNode* node)
1533 return node->getDoubleValue();
1537 inline const char * getValue<const char*>(const SGPropertyNode* node)
1539 return node->getStringValue ();
1542 inline bool setValue(SGPropertyNode* node, bool value)
1544 return node->setBoolValue(value);
1547 inline bool setValue(SGPropertyNode* node, int value)
1549 return node->setIntValue(value);
1552 inline bool setValue(SGPropertyNode* node, long value)
1554 return node->setLongValue(value);
1557 inline bool setValue(SGPropertyNode* node, float value)
1559 return node->setFloatValue(value);
1562 inline bool setValue(SGPropertyNode* node, double value)
1564 return node->setDoubleValue(value);
1567 inline bool setValue(SGPropertyNode* node, const char* value)
1569 return node->setStringValue(value);
1572 inline bool setValue (SGPropertyNode* node, const std::string& value)
1574 return node->setStringValue(value.c_str());
1577 #endif // __PROPS_HXX