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 <boost/utility.hpp>
28 #include <simgear/compiler.h>
29 #include <simgear/debug/logstream.hxx>
33 #include <simgear/math/SGMathFwd.hxx>
34 #include <simgear/structure/SGReferenced.hxx>
35 #include <simgear/structure/SGSharedPtr.hxx>
37 // XXX This whole file should be in the simgear namespace, but I don't
38 // have the guts yet...
43 std::istream& readFrom(std::istream& stream, T& result)
50 * Parse a string as an object of a given type.
51 * XXX no error behavior yet.
53 * @tparam T the return type
54 * @param str the string
58 inline T parseString(const std::string& str)
60 std::istringstream stream(str);
62 readFrom(stream, result);
66 // Extended properties
68 std::istream& readFrom<SGVec3d>(std::istream& stream, SGVec3d& result);
70 std::istream& readFrom<SGVec4d>(std::istream& stream, SGVec4d& result);
74 * Property value types.
78 #pragma warn A sloppy coder has defined NONE as a macro!
83 #pragma warn A sloppy coder has defined ALIAS as a macro!
88 #pragma warn A sloppy coder has defined UNSPECIFIED as a macro!
93 #pragma warn A sloppy coder has defined BOOL as a macro!
98 #pragma warn A sloppy coder has defined INT as a macro!
103 #pragma warn A sloppy coder has defined LONG as a macro!
108 #pragma warn A sloppy coder has defined FLOAT as a macro!
113 #pragma warn A sloppy coder has defined DOUBLE as a macro!
118 #pragma warn A sloppy coder has defined STRING as a macro!
125 * The possible types of an SGPropertyNode. Types that appear after
126 * EXTENDED are not stored in the SGPropertyNode itself.
129 NONE = 0, /**< The node hasn't been assigned a value yet. */
130 ALIAS, /**< The node "points" to another node. */
138 EXTENDED, /**< The node's value is not stored in the property;
139 * the actual value and type is retrieved from an
140 * SGRawValue node. This type is never returned by @see
141 * SGPropertyNode::getType.
143 // Extended properties
148 template<typename T> struct PropertyTraits;
150 #define DEFINTERNALPROP(TYPE, PROP) \
152 struct PropertyTraits<TYPE> \
154 static const Type type_tag = PROP; \
155 enum { Internal = 1 }; \
158 DEFINTERNALPROP(bool, BOOL);
159 DEFINTERNALPROP(int, INT);
160 DEFINTERNALPROP(long, LONG);
161 DEFINTERNALPROP(float, FLOAT);
162 DEFINTERNALPROP(double, DOUBLE);
163 DEFINTERNALPROP(const char *, STRING);
164 #undef DEFINTERNALPROP
167 struct PropertyTraits<SGVec3d>
169 static const Type type_tag = VEC3D;
170 enum { Internal = 0 };
174 struct PropertyTraits<SGVec4d>
176 static const Type type_tag = VEC4D;
177 enum { Internal = 0 };
185 ////////////////////////////////////////////////////////////////////////
188 // This is the mechanism that information-providing routines can
189 // use to link their own values to the property manager. Any
190 // SGValue can be tied to a raw value and then untied again.
192 // Note: we are forced to use inlined methods here to ensure
193 // that the templates will be instantiated. We're probably taking
194 // a small performance hit for that.
195 ////////////////////////////////////////////////////////////////////////
198 * Base class for SGRawValue classes that holds no type
199 * information. This allows some generic manipulation of the
206 * Get the type enumeration for the raw value.
210 virtual simgear::props::Type getType() const = 0;
214 * Create a new deep copy of this raw value.
216 * The copy will contain its own version of the underlying value
217 * as well, and will be the same type.
219 * @return A deep copy of the current object.
221 virtual SGRaw* clone() const = 0;
225 class SGRawExtended : public SGRaw
229 * Make an SGRawValueContainer from the SGRawValue.
231 * This is a virtual function of SGRawExtended so that
232 * SGPropertyNode::untie doesn't need to know the type of an
235 virtual SGRawExtended* makeContainer() const = 0;
237 * Write value out to a stream
239 virtual std::ostream& printOn(std::ostream& stream) const = 0;
241 * Read value from a stream and store it.
243 virtual std::istream& readFrom(std::istream& stream) = 0;
246 // Choose between different base classes based on whether the value is
247 // stored internal to the property node. This frees us from defining
248 // the virtual functions in the SGRawExtended interface where they
249 // don't make sense, e.g. readFrom for the const char* type.
250 template<typename T, int internal = simgear::props::PropertyTraits<T>::Internal>
254 class SGRawBase<T, 1> : public SGRaw
259 class SGRawBase<T, 0> : public SGRawExtended
261 virtual SGRawExtended* makeContainer() const;
262 virtual std::ostream& printOn(std::ostream& stream) const;
263 virtual std::istream& readFrom(std::istream& stream);
267 * Abstract base class for a raw value.
269 * <p>The property manager is implemented in two layers. The {@link
270 * SGPropertyNode} is the highest and most abstract layer,
271 * representing an LValue/RValue pair: it records the position of the
272 * property in the property tree and contains facilities for
273 * navigation to other nodes. It is guaranteed to be persistent: the
274 * {@link SGPropertyNode} will not change during a session, even if
275 * the property is bound and unbound multiple times.</p>
277 * <p>When the property value is not managed internally in the
278 * SGPropertyNode, the SGPropertyNode will contain a reference to an
279 * SGRawValue (this class), which provides an abstract way to get,
280 * set, and clone the underlying value. The SGRawValue may change
281 * frequently during a session as a value is retyped or bound and
282 * unbound to various data source, but the abstract SGPropertyNode
283 * layer insulates the application from those changes.
285 * <p>The SGPropertyNode class always keeps a *copy* of a raw value,
286 * not the original one passed to it; if you override a derived class
287 * but do not replace the {@link #clone} method, strange things will
290 * <p>All derived SGRawValue classes must implement {@link #getValue},
291 * {@link #setValue}, and {@link #clone} for the appropriate type.</p>
293 * @see SGPropertyNode
294 * @see SGRawValuePointer
295 * @see SGRawValueFunctions
296 * @see SGRawValueFunctionsIndexed
297 * @see SGRawValueMethods
298 * @see SGRawValueMethodsIndexed
299 * @see SGRawValueContainer
302 class SGRawValue : public SGRawBase<T>
307 * The default underlying value for this type.
309 * Every raw value has a default; the default is false for a
310 * boolean, 0 for the various numeric values, and "" for a string.
311 * If additional types of raw values are added in the future, they
312 * may need different kinds of default values (such as epoch for a
313 * date type). The default value is used when creating new values.
315 static const T DefaultValue; // Default for this kind of raw value.
321 * Use the default value for this type.
329 virtual ~SGRawValue () {}
333 * Return the underlying value.
335 * @return The actual value for the property.
338 virtual T getValue () const = 0;
342 * Assign a new underlying value.
344 * If the new value cannot be set (because this is a read-only
345 * raw value, or because the new value is not acceptable for
346 * some reason) this method returns false and leaves the original
349 * @param value The actual value for the property.
350 * @return true if the value was set successfully, false otherwise.
353 virtual bool setValue (T value) = 0;
357 * Return the type tag for this raw value type.
359 virtual simgear::props::Type getType() const
361 return simgear::props::PropertyTraits<T>::type_tag;
367 ////////////////////////////////////////////////////////////////////////
368 // Default values for every type.
369 ////////////////////////////////////////////////////////////////////////
371 template<> const bool SGRawValue<bool>::DefaultValue;
372 template<> const int SGRawValue<int>::DefaultValue;
373 template<> const long SGRawValue<long>::DefaultValue;
374 template<> const float SGRawValue<float>::DefaultValue;
375 template<> const double SGRawValue<double>::DefaultValue;
376 template<> const char * const SGRawValue<const char *>::DefaultValue;
377 template<> const SGVec3d SGRawValue<SGVec3d>::DefaultValue;
378 template<> const SGVec4d SGRawValue<SGVec4d>::DefaultValue;
381 * A raw value bound to a pointer.
383 * This is the most efficient way to tie an external value, but also
384 * the most dangerous, because there is no way for the supplier to
385 * perform bounds checking and derived calculations except by polling
386 * the variable to see if it has changed. There is no default
387 * constructor, because this class would be meaningless without a
391 class SGRawValuePointer : public SGRawValue<T>
396 * Explicit pointer constructor.
398 * Create a new raw value bound to the value of the variable
399 * referenced by the pointer.
401 * @param ptr The pointer to the variable to which this raw value
404 SGRawValuePointer (T * ptr) : _ptr(ptr) {}
409 virtual ~SGRawValuePointer () {}
412 * Get the underlying value.
414 * This method will dereference the pointer and return the
417 virtual T getValue () const { return *_ptr; }
420 * Set the underlying value.
422 * This method will dereference the pointer and change the
425 virtual bool setValue (T value) { *_ptr = value; return true; }
428 * Create a copy of this raw value.
430 * The copy will use the same external pointer as the original.
432 virtual SGRaw* clone () const {
433 return new SGRawValuePointer(_ptr);
442 * A value managed through static functions.
444 * A read-only value will not have a setter; a write-only value will
448 class SGRawValueFunctions : public SGRawValue<T>
453 * The template type of a static getter function.
455 typedef T (*getter_t)();
458 * The template type of a static setter function.
460 typedef void (*setter_t)(T);
463 * Explicit constructor.
465 * Create a new raw value bound to the getter and setter supplied.
467 * @param getter A static function for getting a value, or 0
468 * to read-disable the value.
469 * @param setter A static function for setting a value, or 0
470 * to write-disable the value.
472 SGRawValueFunctions (getter_t getter = 0, setter_t setter = 0)
473 : _getter(getter), _setter(setter) {}
478 virtual ~SGRawValueFunctions () {}
481 * Get the underlying value.
483 * This method will invoke the getter function to get a value.
484 * If no getter function was supplied, this method will always
485 * return the default value for the type.
487 virtual T getValue () const {
488 if (_getter) return (*_getter)();
489 else return SGRawValue<T>::DefaultValue;
493 * Set the underlying value.
495 * This method will invoke the setter function to change the
496 * underlying value. If no setter function was supplied, this
497 * method will return false.
499 virtual bool setValue (T value) {
500 if (_setter) { (*_setter)(value); return true; }
505 * Create a copy of this raw value, bound to the same functions.
507 virtual SGRaw* clone () const {
508 return new SGRawValueFunctions(_getter,_setter);
518 * An indexed value bound to static functions.
520 * A read-only value will not have a setter; a write-only value will
521 * not have a getter. An indexed value is useful for binding one
522 * of a list of possible values (such as multiple engines for a
523 * plane). The index is hard-coded at creation time.
528 class SGRawValueFunctionsIndexed : public SGRawValue<T>
531 typedef T (*getter_t)(int);
532 typedef void (*setter_t)(int,T);
533 SGRawValueFunctionsIndexed (int index, getter_t getter = 0, setter_t setter = 0)
534 : _index(index), _getter(getter), _setter(setter) {}
535 virtual ~SGRawValueFunctionsIndexed () {}
536 virtual T getValue () const {
537 if (_getter) return (*_getter)(_index);
538 else return SGRawValue<T>::DefaultValue;
540 virtual bool setValue (T value) {
541 if (_setter) { (*_setter)(_index, value); return true; }
544 virtual SGRaw* clone () const {
545 return new SGRawValueFunctionsIndexed(_index, _getter, _setter);
555 * A value managed through an object and access methods.
557 * A read-only value will not have a setter; a write-only value will
560 template <class C, class T>
561 class SGRawValueMethods : public SGRawValue<T>
564 typedef T (C::*getter_t)() const;
565 typedef void (C::*setter_t)(T);
566 SGRawValueMethods (C &obj, getter_t getter = 0, setter_t setter = 0)
567 : _obj(obj), _getter(getter), _setter(setter) {}
568 virtual ~SGRawValueMethods () {}
569 virtual T getValue () const {
570 if (_getter) { return (_obj.*_getter)(); }
571 else { return SGRawValue<T>::DefaultValue; }
573 virtual bool setValue (T value) {
574 if (_setter) { (_obj.*_setter)(value); return true; }
577 virtual SGRaw* clone () const {
578 return new SGRawValueMethods(_obj, _getter, _setter);
588 * An indexed value managed through an object and access methods.
590 * A read-only value will not have a setter; a write-only value will
593 template <class C, class T>
594 class SGRawValueMethodsIndexed : public SGRawValue<T>
597 typedef T (C::*getter_t)(int) const;
598 typedef void (C::*setter_t)(int, T);
599 SGRawValueMethodsIndexed (C &obj, int index,
600 getter_t getter = 0, setter_t setter = 0)
601 : _obj(obj), _index(index), _getter(getter), _setter(setter) {}
602 virtual ~SGRawValueMethodsIndexed () {}
603 virtual T getValue () const {
604 if (_getter) { return (_obj.*_getter)(_index); }
605 else { return SGRawValue<T>::DefaultValue; }
607 virtual bool setValue (T value) {
608 if (_setter) { (_obj.*_setter)(_index, value); return true; }
611 virtual SGRaw* clone () const {
612 return new SGRawValueMethodsIndexed(_obj, _index, _getter, _setter);
622 * A raw value that contains its value. This provides a way for
623 * property nodes to contain values that shouldn't be stored in the
624 * property node itself.
627 class SGRawValueContainer : public SGRawValue<T>
632 * Explicit constructor.
634 SGRawValueContainer(const T& obj) : _obj(obj) {}
639 virtual ~SGRawValueContainer() {}
642 * Get the underlying value.
644 virtual T getValue() const { return _obj; }
647 * Set the underlying value.
649 * This method will dereference the pointer and change the
652 virtual bool setValue (T value) { _obj = value; return true; }
655 * Create a copy of this raw value.
657 virtual SGRaw* clone () const {
658 return new SGRawValueContainer(_obj);
666 SGRawExtended* SGRawBase<T, 0>::makeContainer() const
668 return new SGRawValueContainer<T>(static_cast<const SGRawValue<T>*>(this)
673 std::ostream& SGRawBase<T, 0>::printOn(std::ostream& stream) const
675 return stream << static_cast<SGRawValue<T>*>(this)->getValue();
679 std::istream& SGRawBase<T, 0>::readFrom(std::istream& stream)
682 simgear::readFrom(stream, value);
683 static_cast<SGRawValue<T>*>(this)->setValue(value);
688 std::ostream& SGRawBase<SGVec3d>::printOn(std::ostream& stream) const;
690 std::ostream& SGRawBase<SGVec4d>::printOn(std::ostream& stream) const;
694 * The smart pointer that manage reference counting
696 class SGPropertyNode;
697 typedef SGSharedPtr<SGPropertyNode> SGPropertyNode_ptr;
698 typedef SGSharedPtr<const SGPropertyNode> SGConstPropertyNode_ptr;
702 * The property change listener interface.
704 * <p>Any class that needs to listen for property changes must implement
705 * this interface.</p>
707 class SGPropertyChangeListener
710 virtual ~SGPropertyChangeListener ();
711 virtual void valueChanged (SGPropertyNode * node);
712 virtual void childAdded (SGPropertyNode * parent, SGPropertyNode * child);
713 virtual void childRemoved (SGPropertyNode * parent, SGPropertyNode * child);
716 friend class SGPropertyNode;
717 virtual void register_property (SGPropertyNode * node);
718 virtual void unregister_property (SGPropertyNode * node);
721 std::vector<SGPropertyNode *> _properties;
727 * A node in a property tree.
729 class SGPropertyNode : public SGReferenced
737 MAX_STRING_LEN = 1024
741 * Access mode attributes.
743 * <p>The ARCHIVE attribute is strictly advisory, and controls
744 * whether the property should normally be saved and restored.</p>
758 * Last used attribute
759 * Update as needed when enum Attribute is changed
761 static const int LAST_USED_ATTRIBUTE;
765 * Default constructor.
773 SGPropertyNode (const SGPropertyNode &node);
779 virtual ~SGPropertyNode ();
788 * Test whether this node contains a primitive leaf value.
790 bool hasValue () const { return (_type != simgear::props::NONE); }
794 * Get the node's simple (XML) name.
796 const char * getName () const { return _name.c_str(); }
800 * Get the node's pretty display name, with subscript when needed.
802 const char * getDisplayName (bool simplify = false) const;
806 * Get the node's integer index.
808 int getIndex () const { return _index; }
812 * Get a non-const pointer to the node's parent.
814 SGPropertyNode * getParent () { return _parent; }
818 * Get a const pointer to the node's parent.
820 const SGPropertyNode * getParent () const { return _parent; }
829 * Get the number of child nodes.
831 int nChildren () const { return _children.size(); }
835 * Get a child node by position (*NOT* index).
837 SGPropertyNode * getChild (int position);
841 * Get a const child node by position (*NOT* index).
843 const SGPropertyNode * getChild (int position) const;
847 * Test whether a named child exists.
849 bool hasChild (const char * name, int index = 0) const
851 return (getChild(name, index) != 0);
855 * Test whether a named child exists.
857 bool hasChild (const std::string& name, int index = 0) const
859 return (getChild(name, index) != 0);
863 * Create a child node after the last node with the same name.
865 SGPropertyNode * addChild (const char * name);
868 * Get a child node by name and index.
870 SGPropertyNode * getChild (const char * name, int index = 0,
871 bool create = false);
874 * Get a child node by name and index.
876 SGPropertyNode * getChild (const std::string& name, int index = 0,
878 { return getChild(name.c_str(), index, create); }
882 * Get a const child node by name and index.
884 const SGPropertyNode * getChild (const char * name, int index = 0) const;
887 * Get a const child node by name and index.
889 const SGPropertyNode * getChild (const std::string& name, int index = 0) const
890 { return getChild(name.c_str(), index); }
894 * Get a vector of all children with the specified name.
896 std::vector<SGPropertyNode_ptr> getChildren (const char * name) const;
899 * Get a vector of all children with the specified name.
901 std::vector<SGPropertyNode_ptr> getChildren (const std::string& name) const
902 { return getChildren(name.c_str()); }
905 * Remove child by position.
907 SGPropertyNode_ptr removeChild (int pos, bool keep = true);
911 * Remove a child node
913 SGPropertyNode_ptr removeChild (const char * name, int index = 0,
917 * Remove a child node
919 SGPropertyNode_ptr removeChild (const std::string& name, int index = 0,
921 { return removeChild(name.c_str(), index, keep); }
924 * Remove all children with the specified name.
926 std::vector<SGPropertyNode_ptr> removeChildren (const char * name,
931 * Remove all children with the specified name.
933 std::vector<SGPropertyNode_ptr> removeChildren (const std::string& name,
935 { return removeChildren(name.c_str(), keep); }
943 * Alias this node's leaf value to another's.
945 bool alias (SGPropertyNode * target);
949 * Alias this node's leaf value to another's by relative path.
951 bool alias (const char * path);
954 * Alias this node's leaf value to another's by relative path.
956 bool alias (const std::string& path)
957 { return alias(path.c_str()); }
961 * Remove any alias for this node.
967 * Test whether the node's leaf value is aliased to another's.
969 bool isAlias () const { return (_type == simgear::props::ALIAS); }
973 * Get a non-const pointer to the current alias target, if any.
975 SGPropertyNode * getAliasTarget ();
979 * Get a const pointer to the current alias target, if any.
981 const SGPropertyNode * getAliasTarget () const;
990 * Get the path to this node from the root.
992 const char * getPath (bool simplify = false) const;
996 * Get a pointer to the root node.
998 SGPropertyNode * getRootNode ();
1002 * Get a const pointer to the root node.
1004 const SGPropertyNode * getRootNode () const;
1008 * Get a pointer to another node by relative path.
1010 SGPropertyNode * getNode (const char * relative_path, bool create = false);
1013 * Get a pointer to another node by relative path.
1015 SGPropertyNode * getNode (const std::string& relative_path, bool create = false)
1016 { return getNode(relative_path.c_str(), create); }
1019 * Get a pointer to another node by relative path.
1021 * This method leaves the index off the last member of the path,
1022 * so that the user can specify it separately (and save some
1023 * string building). For example, getNode("/bar[1]/foo", 3) is
1024 * exactly equivalent to getNode("bar[1]/foo[3]"). The index
1025 * provided overrides any given in the path itself for the last
1028 SGPropertyNode * getNode (const char * relative_path, int index,
1029 bool create = false);
1032 * Get a pointer to another node by relative path.
1034 * This method leaves the index off the last member of the path,
1035 * so that the user can specify it separately (and save some
1036 * string building). For example, getNode("/bar[1]/foo", 3) is
1037 * exactly equivalent to getNode("bar[1]/foo[3]"). The index
1038 * provided overrides any given in the path itself for the last
1041 SGPropertyNode * getNode (const std::string& relative_path, int index,
1042 bool create = false)
1043 { return getNode(relative_path.c_str(), index, create); }
1046 * Get a const pointer to another node by relative path.
1048 const SGPropertyNode * getNode (const char * relative_path) const;
1051 * Get a const pointer to another node by relative path.
1053 const SGPropertyNode * getNode (const std::string& relative_path) const
1054 { return getNode(relative_path.c_str()); }
1058 * Get a const pointer to another node by relative path.
1060 * This method leaves the index off the last member of the path,
1061 * so that the user can specify it separate.
1063 const SGPropertyNode * getNode (const char * relative_path,
1067 * Get a const pointer to another node by relative path.
1069 * This method leaves the index off the last member of the path,
1070 * so that the user can specify it separate.
1072 const SGPropertyNode * getNode (const std::string& relative_path,
1074 { return getNode(relative_path.c_str(), index); }
1081 * Check a single mode attribute for the property node.
1083 bool getAttribute (Attribute attr) const { return ((_attr & attr) != 0); }
1087 * Set a single mode attribute for the property node.
1089 void setAttribute (Attribute attr, bool state) {
1090 (state ? _attr |= attr : _attr &= ~attr);
1095 * Get all of the mode attributes for the property node.
1097 int getAttributes () const { return _attr; }
1101 * Set all of the mode attributes for the property node.
1103 void setAttributes (int attr) { _attr = attr; }
1107 // Leaf Value (primitive).
1112 * Get the type of leaf value, if any, for this node.
1114 simgear::props::Type getType () const;
1118 * Get a bool value for this node.
1120 bool getBoolValue () const;
1124 * Get an int value for this node.
1126 int getIntValue () const;
1130 * Get a long int value for this node.
1132 long getLongValue () const;
1136 * Get a float value for this node.
1138 float getFloatValue () const;
1142 * Get a double value for this node.
1144 double getDoubleValue () const;
1148 * Get a string value for this node.
1150 const char * getStringValue () const;
1153 * Get a value from a node. If the actual type of the node doesn't
1154 * match the desired type, a conversion isn't guaranteed.
1156 template<typename T>
1157 T getValue(typename boost::enable_if_c<simgear::props::PropertyTraits<T>::Internal>
1158 ::type* dummy = 0) const;
1159 // Getter for extended property
1160 template<typename T>
1161 T getValue(typename boost::disable_if_c<simgear::props::PropertyTraits<T>::Internal>
1162 ::type* dummy = 0) const;
1165 * Set a bool value for this node.
1167 bool setBoolValue (bool value);
1171 * Set an int value for this node.
1173 bool setIntValue (int value);
1177 * Set a long int value for this node.
1179 bool setLongValue (long value);
1183 * Set a float value for this node.
1185 bool setFloatValue (float value);
1189 * Set a double value for this node.
1191 bool setDoubleValue (double value);
1195 * Set a string value for this node.
1197 bool setStringValue (const char * value);
1200 * Set a string value for this node.
1202 bool setStringValue (const std::string& value)
1203 { return setStringValue(value.c_str()); }
1207 * Set a value of unspecified type for this node.
1209 bool setUnspecifiedValue (const char * value);
1211 template<typename T>
1212 bool setValue(const T& val,
1213 typename boost::enable_if_c<simgear::props::PropertyTraits<T>::Internal>
1216 template<typename T>
1217 bool setValue(const T& val,
1218 typename boost::disable_if_c<simgear::props::PropertyTraits<T>::Internal>
1222 * Print the value of the property to a stream.
1224 std::ostream& printOn(std::ostream& stream) const;
1232 * Test whether this node is bound to an external data source.
1234 bool isTied () const { return _tied; }
1237 * Bind this node to an external source.
1239 template<typename T>
1240 bool tie(const SGRawValue<T> &rawValue, bool useDefault = true);
1243 * Unbind this node from any external data source.
1249 // Convenience methods using paths.
1250 // TODO: add attribute methods
1255 * Get another node's type.
1257 simgear::props::Type getType (const char * relative_path) const;
1260 * Get another node's type.
1262 simgear::props::Type getType (const std::string& relative_path) const
1263 { return getType(relative_path.c_str()); }
1266 * Test whether another node has a leaf value.
1268 bool hasValue (const char * relative_path) const;
1271 * Test whether another node has a leaf value.
1273 bool hasValue (const std::string& relative_path) const
1274 { return hasValue(relative_path.c_str()); }
1277 * Get another node's value as a bool.
1279 bool getBoolValue (const char * relative_path,
1280 bool defaultValue = false) const;
1283 * Get another node's value as a bool.
1285 bool getBoolValue (const std::string& relative_path,
1286 bool defaultValue = false) const
1287 { return getBoolValue(relative_path.c_str(), defaultValue); }
1290 * Get another node's value as an int.
1292 int getIntValue (const char * relative_path,
1293 int defaultValue = 0) const;
1296 * Get another node's value as an int.
1298 int getIntValue (const std::string& relative_path,
1299 int defaultValue = 0) const
1300 { return getIntValue(relative_path.c_str(), defaultValue); }
1304 * Get another node's value as a long int.
1306 long getLongValue (const char * relative_path,
1307 long defaultValue = 0L) const;
1310 * Get another node's value as a long int.
1312 long getLongValue (const std::string& relative_path,
1313 long defaultValue = 0L) const
1314 { return getLongValue(relative_path.c_str(), defaultValue); }
1317 * Get another node's value as a float.
1319 float getFloatValue (const char * relative_path,
1320 float defaultValue = 0.0f) const;
1323 * Get another node's value as a float.
1325 float getFloatValue (const std::string& relative_path,
1326 float defaultValue = 0.0f) const
1327 { return getFloatValue(relative_path.c_str(), defaultValue); }
1331 * Get another node's value as a double.
1333 double getDoubleValue (const char * relative_path,
1334 double defaultValue = 0.0) const;
1337 * Get another node's value as a double.
1339 double getDoubleValue (const std::string& relative_path,
1340 double defaultValue = 0.0) const
1341 { return getDoubleValue(relative_path.c_str(), defaultValue); }
1344 * Get another node's value as a string.
1346 const char * getStringValue (const char * relative_path,
1347 const char * defaultValue = "") const;
1351 * Get another node's value as a string.
1353 const char * getStringValue (const std::string& relative_path,
1354 const char * defaultValue = "") const
1355 { return getStringValue(relative_path.c_str(), defaultValue); }
1359 * Set another node's value as a bool.
1361 bool setBoolValue (const char * relative_path, bool value);
1364 * Set another node's value as a bool.
1366 bool setBoolValue (const std::string& relative_path, bool value)
1367 { return setBoolValue(relative_path.c_str(), value); }
1371 * Set another node's value as an int.
1373 bool setIntValue (const char * relative_path, int value);
1376 * Set another node's value as an int.
1378 bool setIntValue (const std::string& relative_path, int value)
1379 { return setIntValue(relative_path.c_str(), value); }
1383 * Set another node's value as a long int.
1385 bool setLongValue (const char * relative_path, long value);
1388 * Set another node's value as a long int.
1390 bool setLongValue (const std::string& relative_path, long value)
1391 { return setLongValue(relative_path.c_str(), value); }
1395 * Set another node's value as a float.
1397 bool setFloatValue (const char * relative_path, float value);
1400 * Set another node's value as a float.
1402 bool setFloatValue (const std::string& relative_path, float value)
1403 { return setFloatValue(relative_path.c_str(), value); }
1407 * Set another node's value as a double.
1409 bool setDoubleValue (const char * relative_path, double value);
1412 * Set another node's value as a double.
1414 bool setDoubleValue (const std::string& relative_path, double value)
1415 { return setDoubleValue(relative_path.c_str(), value); }
1419 * Set another node's value as a string.
1421 bool setStringValue (const char * relative_path, const char * value);
1424 * Set another node's value as a string.
1426 bool setStringValue (const std::string& relative_path, const char * value)
1427 { return setStringValue(relative_path.c_str(), value); }
1431 * Set another node's value with no specified type.
1433 bool setUnspecifiedValue (const char * relative_path, const char * value);
1437 * Test whether another node is bound to an external data source.
1439 bool isTied (const char * relative_path) const;
1442 * Test whether another node is bound to an external data source.
1444 bool isTied (const std::string& relative_path) const
1445 { return isTied(relative_path.c_str()); }
1448 * Bind another node to an external bool source.
1450 bool tie (const char * relative_path, const SGRawValue<bool> &rawValue,
1451 bool useDefault = true);
1454 * Bind another node to an external bool source.
1456 bool tie (const std::string& relative_path, const SGRawValue<bool> &rawValue,
1457 bool useDefault = true)
1458 { return tie(relative_path.c_str(), rawValue, useDefault); }
1462 * Bind another node to an external int source.
1464 bool tie (const char * relative_path, const SGRawValue<int> &rawValue,
1465 bool useDefault = true);
1468 * Bind another node to an external int source.
1470 bool tie (const std::string& relative_path, const SGRawValue<int> &rawValue,
1471 bool useDefault = true)
1472 { return tie(relative_path.c_str(), rawValue, useDefault); }
1476 * Bind another node to an external long int source.
1478 bool tie (const char * relative_path, const SGRawValue<long> &rawValue,
1479 bool useDefault = true);
1482 * Bind another node to an external long int source.
1484 bool tie (const std::string& relative_path, const SGRawValue<long> &rawValue,
1485 bool useDefault = true)
1486 { return tie(relative_path.c_str(), rawValue, useDefault); }
1490 * Bind another node to an external float source.
1492 bool tie (const char * relative_path, const SGRawValue<float> &rawValue,
1493 bool useDefault = true);
1496 * Bind another node to an external float source.
1498 bool tie (const std::string& relative_path, const SGRawValue<float> &rawValue,
1499 bool useDefault = true)
1500 { return tie(relative_path.c_str(), rawValue, useDefault); }
1504 * Bind another node to an external double source.
1506 bool tie (const char * relative_path, const SGRawValue<double> &rawValue,
1507 bool useDefault = true);
1510 * Bind another node to an external double source.
1512 bool tie (const std::string& relative_path, const SGRawValue<double> &rawValue,
1513 bool useDefault = true)
1514 { return tie(relative_path.c_str(), rawValue, useDefault); }
1518 * Bind another node to an external string source.
1520 bool tie (const char * relative_path, const SGRawValue<const char *> &rawValue,
1521 bool useDefault = true);
1524 * Bind another node to an external string source.
1526 bool tie (const std::string& relative_path, const SGRawValue<const char*> &rawValue,
1527 bool useDefault = true)
1528 { return tie(relative_path.c_str(), rawValue, useDefault); }
1532 * Unbind another node from any external data source.
1534 bool untie (const char * relative_path);
1537 * Unbind another node from any external data source.
1539 bool untie (const std::string& relative_path)
1540 { return untie(relative_path.c_str()); }
1544 * Add a change listener to the property. If "initial" is set call the
1545 * listener initially.
1547 void addChangeListener (SGPropertyChangeListener * listener,
1548 bool initial = false);
1552 * Remove a change listener from the property.
1554 void removeChangeListener (SGPropertyChangeListener * listener);
1558 * Get the number of listeners.
1560 int nListeners () const { return _listeners ? _listeners->size() : 0; }
1564 * Fire a value change event to all listeners.
1566 void fireValueChanged ();
1570 * Fire a child-added event to all listeners.
1572 void fireChildAdded (SGPropertyNode * child);
1576 * Fire a child-removed event to all listeners.
1578 void fireChildRemoved (SGPropertyNode * child);
1582 * Clear any existing value and set the type to NONE.
1589 void fireValueChanged (SGPropertyNode * node);
1590 void fireChildAdded (SGPropertyNode * parent, SGPropertyNode * child);
1591 void fireChildRemoved (SGPropertyNode * parent, SGPropertyNode * child);
1594 * Protected constructor for making new nodes on demand.
1596 SGPropertyNode (const char * name, int index, SGPropertyNode * parent);
1601 // Get the raw value
1602 bool get_bool () const;
1603 int get_int () const;
1604 long get_long () const;
1605 float get_float () const;
1606 double get_double () const;
1607 const char * get_string () const;
1609 // Set the raw value
1610 bool set_bool (bool value);
1611 bool set_int (int value);
1612 bool set_long (long value);
1613 bool set_float (float value);
1614 bool set_double (double value);
1615 bool set_string (const char * value);
1619 * Get the value as a string.
1621 const char * make_string () const;
1624 * Trace a read access.
1626 void trace_read () const;
1630 * Trace a write access.
1632 void trace_write () const;
1636 * Remove this node from all nodes that link to it in their path cache.
1638 void remove_from_path_caches();
1645 mutable std::string _display_name;
1646 /// To avoid cyclic reference counting loops this shall not be a reference
1648 SGPropertyNode * _parent;
1649 std::vector<SGPropertyNode_ptr> _children;
1650 std::vector<SGPropertyNode_ptr> _removedChildren;
1651 std::vector<hash_table *> _linkedNodes;
1652 mutable std::string _path;
1653 mutable std::string _buffer;
1654 hash_table * _path_cache;
1655 simgear::props::Type _type;
1659 // The right kind of pointer...
1661 SGPropertyNode * alias;
1674 std::vector<SGPropertyChangeListener *> * _listeners;
1678 * Register/unregister node that links to this node in its path cache.
1680 void add_linked_node (hash_table * node) { _linkedNodes.push_back(node); }
1681 bool remove_linked_node (hash_table * node);
1686 * A very simple hash table.
1692 * An entry in a bucket in a hash table.
1698 const char * get_key () { return _key.c_str(); }
1699 void set_key (const char * key);
1700 SGPropertyNode * get_value () { return _value; }
1701 void set_value (SGPropertyNode * value);
1704 SGSharedPtr<SGPropertyNode> _value;
1709 * A bucket in a hash table.
1715 entry * get_entry (const char * key, bool create = false);
1716 bool erase (SGPropertyNode * node);
1717 void clear (hash_table * owner);
1723 friend class bucket;
1727 SGPropertyNode * get (const char * key);
1728 void put (const char * key, SGPropertyNode * value);
1729 bool erase (SGPropertyNode * node);
1732 unsigned int hashcode (const char * key);
1733 unsigned int _data_length;
1739 // Convenience functions for use in templates
1740 template<typename T>
1741 T getValue(const SGPropertyNode*);
1744 inline bool getValue<bool>(const SGPropertyNode* node) { return node->getBoolValue(); }
1747 inline int getValue<int>(const SGPropertyNode* node) { return node->getIntValue(); }
1750 inline long getValue<long>(const SGPropertyNode* node) { return node->getLongValue(); }
1753 inline float getValue<float>(const SGPropertyNode* node)
1755 return node->getFloatValue();
1759 inline double getValue<double>(const SGPropertyNode* node)
1761 return node->getDoubleValue();
1765 inline const char * getValue<const char*>(const SGPropertyNode* node)
1767 return node->getStringValue ();
1770 inline bool setValue(SGPropertyNode* node, bool value)
1772 return node->setBoolValue(value);
1775 inline bool setValue(SGPropertyNode* node, int value)
1777 return node->setIntValue(value);
1780 inline bool setValue(SGPropertyNode* node, long value)
1782 return node->setLongValue(value);
1785 inline bool setValue(SGPropertyNode* node, float value)
1787 return node->setFloatValue(value);
1790 inline bool setValue(SGPropertyNode* node, double value)
1792 return node->setDoubleValue(value);
1795 inline bool setValue(SGPropertyNode* node, const char* value)
1797 return node->setStringValue(value);
1800 inline bool setValue (SGPropertyNode* node, const std::string& value)
1802 return node->setStringValue(value.c_str());
1805 template<typename T>
1806 bool SGPropertyNode::tie(const SGRawValue<T> &rawValue, bool useDefault)
1808 using namespace simgear::props;
1809 if (_type == ALIAS || _tied)
1812 useDefault = useDefault && hasValue();
1813 T old_val = SGRawValue<T>::DefaultValue;
1815 old_val = getValue<T>(this);
1817 if (PropertyTraits<T>::Internal)
1818 _type = PropertyTraits<T>::type_tag;
1822 _value.val = rawValue.clone();
1829 bool SGPropertyNode::tie (const SGRawValue<const char *> &rawValue,
1832 template<typename T>
1833 T SGPropertyNode::getValue(typename boost::disable_if_c<simgear::props
1834 ::PropertyTraits<T>::Internal>::type* dummy) const
1836 using namespace simgear::props;
1837 if (_attr == (READ|WRITE) && _type == EXTENDED
1838 && _value.val->getType() == PropertyTraits<T>::type_tag) {
1839 return static_cast<SGRawValue<T>*>(_value.val)->getValue();
1841 if (getAttribute(TRACE_READ))
1843 if (!getAttribute(READ))
1844 return SGRawValue<T>::DefaultValue;
1847 if (_value.val->getType() == PropertyTraits<T>::type_tag)
1848 return static_cast<SGRawValue<T>*>(_value.val)->getValue();
1852 return simgear::parseString<T>(make_string());
1855 return SGRawValue<T>::DefaultValue;
1858 template<typename T>
1859 inline T SGPropertyNode::getValue(typename boost::enable_if_c<simgear::props
1860 ::PropertyTraits<T>::Internal>::type* dummy) const
1862 return ::getValue<T>(this);
1865 template<typename T>
1866 bool SGPropertyNode::setValue(const T& val,
1867 typename boost::disable_if_c<simgear::props
1868 ::PropertyTraits<T>::Internal>::type* dummy)
1870 using namespace simgear::props;
1871 if (_attr == (READ|WRITE) && _type == EXTENDED
1872 && _value.val->getType() == PropertyTraits<T>::type_tag) {
1873 static_cast<SGRawValue<T>*>(_value.val)->setValue(val);
1876 if (getAttribute(WRITE)
1877 && ((_type == EXTENDED
1878 && _value.val->getType() == PropertyTraits<T>::type_tag)
1879 || _type == NONE || _type == UNSPECIFIED)) {
1880 if (_type == NONE || _type == UNSPECIFIED) {
1883 _value.val = new SGRawValueContainer<T>(val);
1885 static_cast<SGRawValue<T>*>(_value.val)->setValue(val);
1887 if (getAttribute(TRACE_WRITE))
1894 template<typename T>
1895 inline bool SGPropertyNode::setValue(const T& val,
1896 typename boost::enable_if_c<simgear::props
1897 ::PropertyTraits<T>::Internal>::type* dummy)
1899 return ::setValue(this, val);
1901 #endif // __PROPS_HXX