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 DEFINTERNALPROP(const char[], STRING);
165 #undef DEFINTERNALPROP
168 struct PropertyTraits<SGVec3d>
170 static const Type type_tag = VEC3D;
171 enum { Internal = 0 };
175 struct PropertyTraits<SGVec4d>
177 static const Type type_tag = VEC4D;
178 enum { Internal = 0 };
186 ////////////////////////////////////////////////////////////////////////
189 // This is the mechanism that information-providing routines can
190 // use to link their own values to the property manager. Any
191 // SGValue can be tied to a raw value and then untied again.
193 // Note: we are forced to use inlined methods here to ensure
194 // that the templates will be instantiated. We're probably taking
195 // a small performance hit for that.
196 ////////////////////////////////////////////////////////////////////////
199 * Base class for SGRawValue classes that holds no type
200 * information. This allows some generic manipulation of the
207 * Get the type enumeration for the raw value.
211 virtual simgear::props::Type getType() const = 0;
215 * Create a new deep copy of this raw value.
217 * The copy will contain its own version of the underlying value
218 * as well, and will be the same type.
220 * @return A deep copy of the current object.
222 virtual SGRaw* clone() const = 0;
226 class SGRawExtended : public SGRaw
230 * Make an SGRawValueContainer from the SGRawValue.
232 * This is a virtual function of SGRawExtended so that
233 * SGPropertyNode::untie doesn't need to know the type of an
236 virtual SGRawExtended* makeContainer() const = 0;
238 * Write value out to a stream
240 virtual std::ostream& printOn(std::ostream& stream) const = 0;
242 * Read value from a stream and store it.
244 virtual std::istream& readFrom(std::istream& stream) = 0;
247 // Choose between different base classes based on whether the value is
248 // stored internal to the property node. This frees us from defining
249 // the virtual functions in the SGRawExtended interface where they
250 // don't make sense, e.g. readFrom for the const char* type.
251 template<typename T, int internal = simgear::props::PropertyTraits<T>::Internal>
255 class SGRawBase<T, 1> : public SGRaw
260 class SGRawBase<T, 0> : public SGRawExtended
262 virtual SGRawExtended* makeContainer() const;
263 virtual std::ostream& printOn(std::ostream& stream) const;
264 virtual std::istream& readFrom(std::istream& stream);
268 * Abstract base class for a raw value.
270 * <p>The property manager is implemented in two layers. The {@link
271 * SGPropertyNode} is the highest and most abstract layer,
272 * representing an LValue/RValue pair: it records the position of the
273 * property in the property tree and contains facilities for
274 * navigation to other nodes. It is guaranteed to be persistent: the
275 * {@link SGPropertyNode} will not change during a session, even if
276 * the property is bound and unbound multiple times.</p>
278 * <p>When the property value is not managed internally in the
279 * SGPropertyNode, the SGPropertyNode will contain a reference to an
280 * SGRawValue (this class), which provides an abstract way to get,
281 * set, and clone the underlying value. The SGRawValue may change
282 * frequently during a session as a value is retyped or bound and
283 * unbound to various data source, but the abstract SGPropertyNode
284 * layer insulates the application from those changes.
286 * <p>The SGPropertyNode class always keeps a *copy* of a raw value,
287 * not the original one passed to it; if you override a derived class
288 * but do not replace the {@link #clone} method, strange things will
291 * <p>All derived SGRawValue classes must implement {@link #getValue},
292 * {@link #setValue}, and {@link #clone} for the appropriate type.</p>
294 * @see SGPropertyNode
295 * @see SGRawValuePointer
296 * @see SGRawValueFunctions
297 * @see SGRawValueFunctionsIndexed
298 * @see SGRawValueMethods
299 * @see SGRawValueMethodsIndexed
300 * @see SGRawValueContainer
303 class SGRawValue : public SGRawBase<T>
308 * The default underlying value for this type.
310 * Every raw value has a default; the default is false for a
311 * boolean, 0 for the various numeric values, and "" for a string.
312 * If additional types of raw values are added in the future, they
313 * may need different kinds of default values (such as epoch for a
314 * date type). The default value is used when creating new values.
316 static T DefaultValue()
325 * Use the default value for this type.
333 virtual ~SGRawValue () {}
337 * Return the underlying value.
339 * @return The actual value for the property.
342 virtual T getValue () const = 0;
346 * Assign a new underlying value.
348 * If the new value cannot be set (because this is a read-only
349 * raw value, or because the new value is not acceptable for
350 * some reason) this method returns false and leaves the original
353 * @param value The actual value for the property.
354 * @return true if the value was set successfully, false otherwise.
357 virtual bool setValue (T value) = 0;
361 * Return the type tag for this raw value type.
363 virtual simgear::props::Type getType() const
365 return simgear::props::PropertyTraits<T>::type_tag;
371 ////////////////////////////////////////////////////////////////////////
372 // Default values for every type.
373 ////////////////////////////////////////////////////////////////////////
375 template<> inline bool SGRawValue<bool>::DefaultValue()
380 template<> inline const char * SGRawValue<const char *>::DefaultValue()
386 * A raw value bound to a pointer.
388 * This is the most efficient way to tie an external value, but also
389 * the most dangerous, because there is no way for the supplier to
390 * perform bounds checking and derived calculations except by polling
391 * the variable to see if it has changed. There is no default
392 * constructor, because this class would be meaningless without a
396 class SGRawValuePointer : public SGRawValue<T>
401 * Explicit pointer constructor.
403 * Create a new raw value bound to the value of the variable
404 * referenced by the pointer.
406 * @param ptr The pointer to the variable to which this raw value
409 SGRawValuePointer (T * ptr) : _ptr(ptr) {}
414 virtual ~SGRawValuePointer () {}
417 * Get the underlying value.
419 * This method will dereference the pointer and return the
422 virtual T getValue () const { return *_ptr; }
425 * Set the underlying value.
427 * This method will dereference the pointer and change the
430 virtual bool setValue (T value) { *_ptr = value; return true; }
433 * Create a copy of this raw value.
435 * The copy will use the same external pointer as the original.
437 virtual SGRaw* clone () const {
438 return new SGRawValuePointer(_ptr);
447 * A value managed through static functions.
449 * A read-only value will not have a setter; a write-only value will
453 class SGRawValueFunctions : public SGRawValue<T>
458 * The template type of a static getter function.
460 typedef T (*getter_t)();
463 * The template type of a static setter function.
465 typedef void (*setter_t)(T);
468 * Explicit constructor.
470 * Create a new raw value bound to the getter and setter supplied.
472 * @param getter A static function for getting a value, or 0
473 * to read-disable the value.
474 * @param setter A static function for setting a value, or 0
475 * to write-disable the value.
477 SGRawValueFunctions (getter_t getter = 0, setter_t setter = 0)
478 : _getter(getter), _setter(setter) {}
483 virtual ~SGRawValueFunctions () {}
486 * Get the underlying value.
488 * This method will invoke the getter function to get a value.
489 * If no getter function was supplied, this method will always
490 * return the default value for the type.
492 virtual T getValue () const {
493 if (_getter) return (*_getter)();
494 else return SGRawValue<T>::DefaultValue();
498 * Set the underlying value.
500 * This method will invoke the setter function to change the
501 * underlying value. If no setter function was supplied, this
502 * method will return false.
504 virtual bool setValue (T value) {
505 if (_setter) { (*_setter)(value); return true; }
510 * Create a copy of this raw value, bound to the same functions.
512 virtual SGRaw* clone () const {
513 return new SGRawValueFunctions(_getter,_setter);
523 * An indexed value bound to static functions.
525 * A read-only value will not have a setter; a write-only value will
526 * not have a getter. An indexed value is useful for binding one
527 * of a list of possible values (such as multiple engines for a
528 * plane). The index is hard-coded at creation time.
533 class SGRawValueFunctionsIndexed : public SGRawValue<T>
536 typedef T (*getter_t)(int);
537 typedef void (*setter_t)(int,T);
538 SGRawValueFunctionsIndexed (int index, getter_t getter = 0, setter_t setter = 0)
539 : _index(index), _getter(getter), _setter(setter) {}
540 virtual ~SGRawValueFunctionsIndexed () {}
541 virtual T getValue () const {
542 if (_getter) return (*_getter)(_index);
543 else return SGRawValue<T>::DefaultValue();
545 virtual bool setValue (T value) {
546 if (_setter) { (*_setter)(_index, value); return true; }
549 virtual SGRaw* clone () const {
550 return new SGRawValueFunctionsIndexed(_index, _getter, _setter);
560 * A value managed through an object and access methods.
562 * A read-only value will not have a setter; a write-only value will
565 template <class C, class T>
566 class SGRawValueMethods : public SGRawValue<T>
569 typedef T (C::*getter_t)() const;
570 typedef void (C::*setter_t)(T);
571 SGRawValueMethods (C &obj, getter_t getter = 0, setter_t setter = 0)
572 : _obj(obj), _getter(getter), _setter(setter) {}
573 virtual ~SGRawValueMethods () {}
574 virtual T getValue () const {
575 if (_getter) { return (_obj.*_getter)(); }
576 else { return SGRawValue<T>::DefaultValue(); }
578 virtual bool setValue (T value) {
579 if (_setter) { (_obj.*_setter)(value); return true; }
582 virtual SGRaw* clone () const {
583 return new SGRawValueMethods(_obj, _getter, _setter);
593 * An indexed value managed through an object and access methods.
595 * A read-only value will not have a setter; a write-only value will
598 template <class C, class T>
599 class SGRawValueMethodsIndexed : public SGRawValue<T>
602 typedef T (C::*getter_t)(int) const;
603 typedef void (C::*setter_t)(int, T);
604 SGRawValueMethodsIndexed (C &obj, int index,
605 getter_t getter = 0, setter_t setter = 0)
606 : _obj(obj), _index(index), _getter(getter), _setter(setter) {}
607 virtual ~SGRawValueMethodsIndexed () {}
608 virtual T getValue () const {
609 if (_getter) { return (_obj.*_getter)(_index); }
610 else { return SGRawValue<T>::DefaultValue(); }
612 virtual bool setValue (T value) {
613 if (_setter) { (_obj.*_setter)(_index, value); return true; }
616 virtual SGRaw* clone () const {
617 return new SGRawValueMethodsIndexed(_obj, _index, _getter, _setter);
627 * A raw value that contains its value. This provides a way for
628 * property nodes to contain values that shouldn't be stored in the
629 * property node itself.
632 class SGRawValueContainer : public SGRawValue<T>
637 * Explicit constructor.
639 SGRawValueContainer(const T& obj) : _obj(obj) {}
644 virtual ~SGRawValueContainer() {}
647 * Get the underlying value.
649 virtual T getValue() const { return _obj; }
652 * Set the underlying value.
654 * This method will dereference the pointer and change the
657 virtual bool setValue (T value) { _obj = value; return true; }
660 * Create a copy of this raw value.
662 virtual SGRaw* clone () const {
663 return new SGRawValueContainer(_obj);
671 SGRawExtended* SGRawBase<T, 0>::makeContainer() const
673 return new SGRawValueContainer<T>(static_cast<const SGRawValue<T>*>(this)
678 std::ostream& SGRawBase<T, 0>::printOn(std::ostream& stream) const
680 return stream << static_cast<SGRawValue<T>*>(this)->getValue();
684 std::istream& SGRawBase<T, 0>::readFrom(std::istream& stream)
687 simgear::readFrom(stream, value);
688 static_cast<SGRawValue<T>*>(this)->setValue(value);
693 std::ostream& SGRawBase<SGVec3d>::printOn(std::ostream& stream) const;
695 std::ostream& SGRawBase<SGVec4d>::printOn(std::ostream& stream) const;
699 * The smart pointer that manage reference counting
701 class SGPropertyNode;
702 typedef SGSharedPtr<SGPropertyNode> SGPropertyNode_ptr;
703 typedef SGSharedPtr<const SGPropertyNode> SGConstPropertyNode_ptr;
707 typedef std::vector<SGPropertyNode_ptr> PropertyList;
712 * The property change listener interface.
714 * <p>Any class that needs to listen for property changes must implement
715 * this interface.</p>
717 class SGPropertyChangeListener
720 virtual ~SGPropertyChangeListener ();
721 virtual void valueChanged (SGPropertyNode * node);
722 virtual void childAdded (SGPropertyNode * parent, SGPropertyNode * child);
723 virtual void childRemoved (SGPropertyNode * parent, SGPropertyNode * child);
726 friend class SGPropertyNode;
727 virtual void register_property (SGPropertyNode * node);
728 virtual void unregister_property (SGPropertyNode * node);
731 std::vector<SGPropertyNode *> _properties;
737 * A node in a property tree.
739 class SGPropertyNode : public SGReferenced
747 MAX_STRING_LEN = 1024
751 * Access mode attributes.
753 * <p>The ARCHIVE attribute is strictly advisory, and controls
754 * whether the property should normally be saved and restored.</p>
765 // beware: if you add another attribute here,
766 // also update value of "LAST_USED_ATTRIBUTE".
771 * Last used attribute
772 * Update as needed when enum Attribute is changed
774 static const int LAST_USED_ATTRIBUTE;
778 * Default constructor.
786 SGPropertyNode (const SGPropertyNode &node);
792 virtual ~SGPropertyNode ();
801 * Test whether this node contains a primitive leaf value.
803 bool hasValue () const { return (_type != simgear::props::NONE); }
807 * Get the node's simple (XML) name.
809 const char * getName () const { return _name.c_str(); }
812 * Get the node's simple name as a string.
814 const std::string& getNameString () const { return _name; }
817 * Get the node's pretty display name, with subscript when needed.
819 std::string getDisplayName (bool simplify = false) const;
823 * Get the node's integer index.
825 int getIndex () const { return _index; }
829 * Get a non-const pointer to the node's parent.
831 SGPropertyNode * getParent () { return _parent; }
835 * Get a const pointer to the node's parent.
837 const SGPropertyNode * getParent () const { return _parent; }
846 * Get the number of child nodes.
848 int nChildren () const { return (int)_children.size(); }
852 * Get a child node by position (*NOT* index).
854 SGPropertyNode * getChild (int position);
858 * Get a const child node by position (*NOT* index).
860 const SGPropertyNode * getChild (int position) const;
864 * Test whether a named child exists.
866 bool hasChild (const char * name, int index = 0) const
868 return (getChild(name, index) != 0);
872 * Test whether a named child exists.
874 bool hasChild (const std::string& name, int index = 0) const
876 return (getChild(name, index) != 0);
880 * Create a child node after the last node with the same name.
882 SGPropertyNode * addChild (const char * name);
885 * Get a child node by name and index.
887 SGPropertyNode * getChild (const char* name, int index = 0,
888 bool create = false);
889 SGPropertyNode * getChild (const std::string& name, int index = 0,
890 bool create = false);
892 * Get a const child node by name and index.
894 const SGPropertyNode * getChild (const char * name, int index = 0) const;
897 * Get a const child node by name and index.
899 const SGPropertyNode * getChild (const std::string& name, int index = 0) const
900 { return getChild(name.c_str(), index); }
904 * Get a vector of all children with the specified name.
906 simgear::PropertyList getChildren (const char * name) const;
909 * Get a vector of all children with the specified name.
911 simgear::PropertyList getChildren (const std::string& name) const
912 { return getChildren(name.c_str()); }
915 * Remove child by position.
917 SGPropertyNode_ptr removeChild (int pos, bool keep = true);
921 * Remove a child node
923 SGPropertyNode_ptr removeChild (const char * name, int index = 0,
927 * Remove a child node
929 SGPropertyNode_ptr removeChild (const std::string& name, int index = 0,
931 { return removeChild(name.c_str(), index, keep); }
934 * Remove all children with the specified name.
936 simgear::PropertyList removeChildren (const char * name, bool keep = true);
939 * Remove all children with the specified name.
941 simgear::PropertyList removeChildren (const std::string& name,
943 { return removeChildren(name.c_str(), keep); }
951 * Alias this node's leaf value to another's.
953 bool alias (SGPropertyNode * target);
957 * Alias this node's leaf value to another's by relative path.
959 bool alias (const char * path);
962 * Alias this node's leaf value to another's by relative path.
964 bool alias (const std::string& path)
965 { return alias(path.c_str()); }
969 * Remove any alias for this node.
975 * Test whether the node's leaf value is aliased to another's.
977 bool isAlias () const { return (_type == simgear::props::ALIAS); }
981 * Get a non-const pointer to the current alias target, if any.
983 SGPropertyNode * getAliasTarget ();
987 * Get a const pointer to the current alias target, if any.
989 const SGPropertyNode * getAliasTarget () const;
998 * Get the path to this node from the root.
1000 std::string getPath (bool simplify = false) const;
1004 * Get a pointer to the root node.
1006 SGPropertyNode * getRootNode ();
1010 * Get a const pointer to the root node.
1012 const SGPropertyNode * getRootNode () const;
1016 * Get a pointer to another node by relative path.
1018 SGPropertyNode * getNode (const char * relative_path, bool create = false);
1021 * Get a pointer to another node by relative path.
1023 SGPropertyNode * getNode (const std::string& relative_path, bool create = false)
1024 { return getNode(relative_path.c_str(), create); }
1027 * Get a pointer to another node by relative path.
1029 * This method leaves the index off the last member of the path,
1030 * so that the user can specify it separately (and save some
1031 * string building). For example, getNode("/bar[1]/foo", 3) is
1032 * exactly equivalent to getNode("bar[1]/foo[3]"). The index
1033 * provided overrides any given in the path itself for the last
1036 SGPropertyNode * getNode (const char * relative_path, int index,
1037 bool create = false);
1040 * Get a pointer to another node by relative path.
1042 * This method leaves the index off the last member of the path,
1043 * so that the user can specify it separately (and save some
1044 * string building). For example, getNode("/bar[1]/foo", 3) is
1045 * exactly equivalent to getNode("bar[1]/foo[3]"). The index
1046 * provided overrides any given in the path itself for the last
1049 SGPropertyNode * getNode (const std::string& relative_path, int index,
1050 bool create = false)
1051 { return getNode(relative_path.c_str(), index, create); }
1054 * Get a const pointer to another node by relative path.
1056 const SGPropertyNode * getNode (const char * relative_path) const;
1059 * Get a const pointer to another node by relative path.
1061 const SGPropertyNode * getNode (const std::string& relative_path) const
1062 { return getNode(relative_path.c_str()); }
1066 * Get a const pointer to another node by relative path.
1068 * This method leaves the index off the last member of the path,
1069 * so that the user can specify it separate.
1071 const SGPropertyNode * getNode (const char * relative_path,
1075 * Get a const pointer to another node by relative path.
1077 * This method leaves the index off the last member of the path,
1078 * so that the user can specify it separate.
1080 const SGPropertyNode * getNode (const std::string& relative_path,
1082 { return getNode(relative_path.c_str(), index); }
1089 * Check a single mode attribute for the property node.
1091 bool getAttribute (Attribute attr) const { return ((_attr & attr) != 0); }
1095 * Set a single mode attribute for the property node.
1097 void setAttribute (Attribute attr, bool state) {
1098 (state ? _attr |= attr : _attr &= ~attr);
1103 * Get all of the mode attributes for the property node.
1105 int getAttributes () const { return _attr; }
1109 * Set all of the mode attributes for the property node.
1111 void setAttributes (int attr) { _attr = attr; }
1115 // Leaf Value (primitive).
1120 * Get the type of leaf value, if any, for this node.
1122 simgear::props::Type getType () const;
1126 * Get a bool value for this node.
1128 bool getBoolValue () const;
1132 * Get an int value for this node.
1134 int getIntValue () const;
1138 * Get a long int value for this node.
1140 long getLongValue () const;
1144 * Get a float value for this node.
1146 float getFloatValue () const;
1150 * Get a double value for this node.
1152 double getDoubleValue () const;
1156 * Get a string value for this node.
1158 const char * getStringValue () const;
1161 * Get a value from a node. If the actual type of the node doesn't
1162 * match the desired type, a conversion isn't guaranteed.
1164 template<typename T>
1165 T getValue(typename boost::enable_if_c<simgear::props::PropertyTraits<T>::Internal>
1166 ::type* dummy = 0) const;
1167 // Getter for extended property
1168 template<typename T>
1169 T getValue(typename boost::disable_if_c<simgear::props::PropertyTraits<T>::Internal>
1170 ::type* dummy = 0) const;
1173 * Set a bool value for this node.
1175 bool setBoolValue (bool value);
1179 * Set an int value for this node.
1181 bool setIntValue (int value);
1185 * Set a long int value for this node.
1187 bool setLongValue (long value);
1191 * Set a float value for this node.
1193 bool setFloatValue (float value);
1197 * Set a double value for this node.
1199 bool setDoubleValue (double value);
1203 * Set a string value for this node.
1205 bool setStringValue (const char * value);
1208 * Set a string value for this node.
1210 bool setStringValue (const std::string& value)
1211 { return setStringValue(value.c_str()); }
1215 * Set a value of unspecified type for this node.
1217 bool setUnspecifiedValue (const char * value);
1219 template<typename T>
1220 bool setValue(const T& val,
1221 typename boost::enable_if_c<simgear::props::PropertyTraits<T>::Internal>
1224 template<typename T>
1225 bool setValue(const T& val,
1226 typename boost::disable_if_c<simgear::props::PropertyTraits<T>::Internal>
1230 bool setValue(const char (&val)[N])
1232 return setValue(&val[0]);
1236 * Print the value of the property to a stream.
1238 std::ostream& printOn(std::ostream& stream) const;
1246 * Test whether this node is bound to an external data source.
1248 bool isTied () const { return _tied; }
1251 * Bind this node to an external source.
1253 template<typename T>
1254 bool tie(const SGRawValue<T> &rawValue, bool useDefault = true);
1257 * Unbind this node from any external data source.
1263 // Convenience methods using paths.
1264 // TODO: add attribute methods
1269 * Get another node's type.
1271 simgear::props::Type getType (const char * relative_path) const;
1274 * Get another node's type.
1276 simgear::props::Type getType (const std::string& relative_path) const
1277 { return getType(relative_path.c_str()); }
1280 * Test whether another node has a leaf value.
1282 bool hasValue (const char * relative_path) const;
1285 * Test whether another node has a leaf value.
1287 bool hasValue (const std::string& relative_path) const
1288 { return hasValue(relative_path.c_str()); }
1291 * Get another node's value as a bool.
1293 bool getBoolValue (const char * relative_path,
1294 bool defaultValue = false) const;
1297 * Get another node's value as a bool.
1299 bool getBoolValue (const std::string& relative_path,
1300 bool defaultValue = false) const
1301 { return getBoolValue(relative_path.c_str(), defaultValue); }
1304 * Get another node's value as an int.
1306 int getIntValue (const char * relative_path,
1307 int defaultValue = 0) const;
1310 * Get another node's value as an int.
1312 int getIntValue (const std::string& relative_path,
1313 int defaultValue = 0) const
1314 { return getIntValue(relative_path.c_str(), defaultValue); }
1318 * Get another node's value as a long int.
1320 long getLongValue (const char * relative_path,
1321 long defaultValue = 0L) const;
1324 * Get another node's value as a long int.
1326 long getLongValue (const std::string& relative_path,
1327 long defaultValue = 0L) const
1328 { return getLongValue(relative_path.c_str(), defaultValue); }
1331 * Get another node's value as a float.
1333 float getFloatValue (const char * relative_path,
1334 float defaultValue = 0.0f) const;
1337 * Get another node's value as a float.
1339 float getFloatValue (const std::string& relative_path,
1340 float defaultValue = 0.0f) const
1341 { return getFloatValue(relative_path.c_str(), defaultValue); }
1345 * Get another node's value as a double.
1347 double getDoubleValue (const char * relative_path,
1348 double defaultValue = 0.0) const;
1351 * Get another node's value as a double.
1353 double getDoubleValue (const std::string& relative_path,
1354 double defaultValue = 0.0) const
1355 { return getDoubleValue(relative_path.c_str(), defaultValue); }
1358 * Get another node's value as a string.
1360 const char * getStringValue (const char * relative_path,
1361 const char * defaultValue = "") const;
1365 * Get another node's value as a string.
1367 const char * getStringValue (const std::string& relative_path,
1368 const char * defaultValue = "") const
1369 { return getStringValue(relative_path.c_str(), defaultValue); }
1373 * Set another node's value as a bool.
1375 bool setBoolValue (const char * relative_path, bool value);
1378 * Set another node's value as a bool.
1380 bool setBoolValue (const std::string& relative_path, bool value)
1381 { return setBoolValue(relative_path.c_str(), value); }
1385 * Set another node's value as an int.
1387 bool setIntValue (const char * relative_path, int value);
1390 * Set another node's value as an int.
1392 bool setIntValue (const std::string& relative_path, int value)
1393 { return setIntValue(relative_path.c_str(), value); }
1397 * Set another node's value as a long int.
1399 bool setLongValue (const char * relative_path, long value);
1402 * Set another node's value as a long int.
1404 bool setLongValue (const std::string& relative_path, long value)
1405 { return setLongValue(relative_path.c_str(), value); }
1409 * Set another node's value as a float.
1411 bool setFloatValue (const char * relative_path, float value);
1414 * Set another node's value as a float.
1416 bool setFloatValue (const std::string& relative_path, float value)
1417 { return setFloatValue(relative_path.c_str(), value); }
1421 * Set another node's value as a double.
1423 bool setDoubleValue (const char * relative_path, double value);
1426 * Set another node's value as a double.
1428 bool setDoubleValue (const std::string& relative_path, double value)
1429 { return setDoubleValue(relative_path.c_str(), value); }
1433 * Set another node's value as a string.
1435 bool setStringValue (const char * relative_path, const char * value);
1437 bool setStringValue(const char * relative_path, const std::string& value)
1438 { return setStringValue(relative_path, value.c_str()); }
1440 * Set another node's value as a string.
1442 bool setStringValue (const std::string& relative_path, const char * value)
1443 { return setStringValue(relative_path.c_str(), value); }
1445 bool setStringValue (const std::string& relative_path,
1446 const std::string& value)
1447 { return setStringValue(relative_path.c_str(), value.c_str()); }
1450 * Set another node's value with no specified type.
1452 bool setUnspecifiedValue (const char * relative_path, const char * value);
1456 * Test whether another node is bound to an external data source.
1458 bool isTied (const char * relative_path) const;
1461 * Test whether another node is bound to an external data source.
1463 bool isTied (const std::string& relative_path) const
1464 { return isTied(relative_path.c_str()); }
1467 * Bind another node to an external bool source.
1469 bool tie (const char * relative_path, const SGRawValue<bool> &rawValue,
1470 bool useDefault = true);
1473 * Bind another node to an external bool source.
1475 bool tie (const std::string& relative_path, const SGRawValue<bool> &rawValue,
1476 bool useDefault = true)
1477 { return tie(relative_path.c_str(), rawValue, useDefault); }
1481 * Bind another node to an external int source.
1483 bool tie (const char * relative_path, const SGRawValue<int> &rawValue,
1484 bool useDefault = true);
1487 * Bind another node to an external int source.
1489 bool tie (const std::string& relative_path, const SGRawValue<int> &rawValue,
1490 bool useDefault = true)
1491 { return tie(relative_path.c_str(), rawValue, useDefault); }
1495 * Bind another node to an external long int source.
1497 bool tie (const char * relative_path, const SGRawValue<long> &rawValue,
1498 bool useDefault = true);
1501 * Bind another node to an external long int source.
1503 bool tie (const std::string& relative_path, const SGRawValue<long> &rawValue,
1504 bool useDefault = true)
1505 { return tie(relative_path.c_str(), rawValue, useDefault); }
1509 * Bind another node to an external float source.
1511 bool tie (const char * relative_path, const SGRawValue<float> &rawValue,
1512 bool useDefault = true);
1515 * Bind another node to an external float source.
1517 bool tie (const std::string& relative_path, const SGRawValue<float> &rawValue,
1518 bool useDefault = true)
1519 { return tie(relative_path.c_str(), rawValue, useDefault); }
1523 * Bind another node to an external double source.
1525 bool tie (const char * relative_path, const SGRawValue<double> &rawValue,
1526 bool useDefault = true);
1529 * Bind another node to an external double source.
1531 bool tie (const std::string& relative_path, const SGRawValue<double> &rawValue,
1532 bool useDefault = true)
1533 { return tie(relative_path.c_str(), rawValue, useDefault); }
1537 * Bind another node to an external string source.
1539 bool tie (const char * relative_path, const SGRawValue<const char *> &rawValue,
1540 bool useDefault = true);
1543 * Bind another node to an external string source.
1545 bool tie (const std::string& relative_path, const SGRawValue<const char*> &rawValue,
1546 bool useDefault = true)
1547 { return tie(relative_path.c_str(), rawValue, useDefault); }
1551 * Unbind another node from any external data source.
1553 bool untie (const char * relative_path);
1556 * Unbind another node from any external data source.
1558 bool untie (const std::string& relative_path)
1559 { return untie(relative_path.c_str()); }
1563 * Add a change listener to the property. If "initial" is set call the
1564 * listener initially.
1566 void addChangeListener (SGPropertyChangeListener * listener,
1567 bool initial = false);
1571 * Remove a change listener from the property.
1573 void removeChangeListener (SGPropertyChangeListener * listener);
1577 * Get the number of listeners.
1579 int nListeners () const { return _listeners ? (int)_listeners->size() : 0; }
1583 * Fire a value change event to all listeners.
1585 void fireValueChanged ();
1589 * Fire a child-added event to all listeners.
1591 void fireChildAdded (SGPropertyNode * child);
1595 * Fire a child-removed event to all listeners.
1597 void fireChildRemoved (SGPropertyNode * child);
1601 * Clear any existing value and set the type to NONE.
1606 * Compare two property trees. The property trees are equal if: 1)
1607 * They have no children, and have the same type and the values are
1608 * equal, or 2) have the same number of children, and the
1609 * corresponding children in each tree are equal. "corresponding"
1610 * means have the same name and index.
1612 * Attributes, removed children, and aliases aren't considered.
1614 static bool compare (const SGPropertyNode& lhs, const SGPropertyNode& rhs);
1618 void fireValueChanged (SGPropertyNode * node);
1619 void fireChildAdded (SGPropertyNode * parent, SGPropertyNode * child);
1620 void fireChildRemoved (SGPropertyNode * parent, SGPropertyNode * child);
1623 * Protected constructor for making new nodes on demand.
1625 SGPropertyNode (const std::string& name, int index, SGPropertyNode * parent);
1626 template<typename Itr>
1627 SGPropertyNode (Itr begin, Itr end, int index, SGPropertyNode * parent);
1631 // Get the raw value
1632 bool get_bool () const;
1633 int get_int () const;
1634 long get_long () const;
1635 float get_float () const;
1636 double get_double () const;
1637 const char * get_string () const;
1639 // Set the raw value
1640 bool set_bool (bool value);
1641 bool set_int (int value);
1642 bool set_long (long value);
1643 bool set_float (float value);
1644 bool set_double (double value);
1645 bool set_string (const char * value);
1649 * Get the value as a string.
1651 const char * make_string () const;
1654 * Trace a read access.
1656 void trace_read () const;
1660 * Trace a write access.
1662 void trace_write () const;
1666 /// To avoid cyclic reference counting loops this shall not be a reference
1668 SGPropertyNode * _parent;
1669 simgear::PropertyList _children;
1670 simgear::PropertyList _removedChildren;
1671 mutable std::string _buffer;
1672 simgear::props::Type _type;
1676 // The right kind of pointer...
1678 SGPropertyNode * alias;
1691 std::vector<SGPropertyChangeListener *> * _listeners;
1694 // Pass name as a pair of iterators
1695 template<typename Itr>
1696 SGPropertyNode * getChildImpl (Itr begin, Itr end, int index = 0, bool create = false);
1697 // very internal method
1698 template<typename Itr>
1699 SGPropertyNode* getExistingChild (Itr begin, Itr end, int index, bool create);
1700 // very internal path parsing function
1701 template<typename SplitItr>
1702 friend SGPropertyNode* find_node_aux(SGPropertyNode * current, SplitItr& itr,
1703 bool create, int last_index);
1705 friend size_t hash_value(const SGPropertyNode& node);
1708 // Convenience functions for use in templates
1709 template<typename T>
1710 T getValue(const SGPropertyNode*);
1713 inline bool getValue<bool>(const SGPropertyNode* node) { return node->getBoolValue(); }
1716 inline int getValue<int>(const SGPropertyNode* node) { return node->getIntValue(); }
1719 inline long getValue<long>(const SGPropertyNode* node) { return node->getLongValue(); }
1722 inline float getValue<float>(const SGPropertyNode* node)
1724 return node->getFloatValue();
1728 inline double getValue<double>(const SGPropertyNode* node)
1730 return node->getDoubleValue();
1734 inline const char * getValue<const char*>(const SGPropertyNode* node)
1736 return node->getStringValue ();
1739 inline bool setValue(SGPropertyNode* node, bool value)
1741 return node->setBoolValue(value);
1744 inline bool setValue(SGPropertyNode* node, int value)
1746 return node->setIntValue(value);
1749 inline bool setValue(SGPropertyNode* node, long value)
1751 return node->setLongValue(value);
1754 inline bool setValue(SGPropertyNode* node, float value)
1756 return node->setFloatValue(value);
1759 inline bool setValue(SGPropertyNode* node, double value)
1761 return node->setDoubleValue(value);
1764 inline bool setValue(SGPropertyNode* node, const char* value)
1766 return node->setStringValue(value);
1769 inline bool setValue (SGPropertyNode* node, const std::string& value)
1771 return node->setStringValue(value.c_str());
1774 template<typename T>
1775 bool SGPropertyNode::tie(const SGRawValue<T> &rawValue, bool useDefault)
1777 using namespace simgear::props;
1778 if (_type == ALIAS || _tied)
1781 useDefault = useDefault && hasValue();
1782 T old_val = SGRawValue<T>::DefaultValue();
1784 old_val = getValue<T>(this);
1786 if (PropertyTraits<T>::Internal)
1787 _type = PropertyTraits<T>::type_tag;
1791 _value.val = rawValue.clone();
1793 int save_attributes = getAttributes();
1794 setAttribute( WRITE, true );
1796 setAttributes( save_attributes );
1802 bool SGPropertyNode::tie (const SGRawValue<const char *> &rawValue,
1805 template<typename T>
1806 T SGPropertyNode::getValue(typename boost::disable_if_c<simgear::props
1807 ::PropertyTraits<T>::Internal>::type* dummy) const
1809 using namespace simgear::props;
1810 if (_attr == (READ|WRITE) && _type == EXTENDED
1811 && _value.val->getType() == PropertyTraits<T>::type_tag) {
1812 return static_cast<SGRawValue<T>*>(_value.val)->getValue();
1814 if (getAttribute(TRACE_READ))
1816 if (!getAttribute(READ))
1817 return SGRawValue<T>::DefaultValue();
1820 if (_value.val->getType() == PropertyTraits<T>::type_tag)
1821 return static_cast<SGRawValue<T>*>(_value.val)->getValue();
1825 return simgear::parseString<T>(make_string());
1827 default: // avoid compiler warning
1830 return SGRawValue<T>::DefaultValue();
1833 template<typename T>
1834 inline T SGPropertyNode::getValue(typename boost::enable_if_c<simgear::props
1835 ::PropertyTraits<T>::Internal>::type* dummy) const
1837 return ::getValue<T>(this);
1840 template<typename T>
1841 bool SGPropertyNode::setValue(const T& val,
1842 typename boost::disable_if_c<simgear::props
1843 ::PropertyTraits<T>::Internal>::type* dummy)
1845 using namespace simgear::props;
1846 if (_attr == (READ|WRITE) && _type == EXTENDED
1847 && _value.val->getType() == PropertyTraits<T>::type_tag) {
1848 static_cast<SGRawValue<T>*>(_value.val)->setValue(val);
1851 if (getAttribute(WRITE)
1852 && ((_type == EXTENDED
1853 && _value.val->getType() == PropertyTraits<T>::type_tag)
1854 || _type == NONE || _type == UNSPECIFIED)) {
1855 if (_type == NONE || _type == UNSPECIFIED) {
1858 _value.val = new SGRawValueContainer<T>(val);
1860 static_cast<SGRawValue<T>*>(_value.val)->setValue(val);
1862 if (getAttribute(TRACE_WRITE))
1869 template<typename T>
1870 inline bool SGPropertyNode::setValue(const T& val,
1871 typename boost::enable_if_c<simgear::props
1872 ::PropertyTraits<T>::Internal>::type* dummy)
1874 return ::setValue(this, val);
1878 * Utility function for creation of a child property node.
1880 inline SGPropertyNode* makeChild(SGPropertyNode* parent, const char* name,
1883 return parent->getChild(name, index, true);
1887 * Utility function for creation of a child property node using a
1892 template<typename StringType>
1893 inline SGPropertyNode* makeNode(SGPropertyNode* parent, const StringType& name)
1895 return parent->getNode(name, true);
1900 size_t hash_value(const SGPropertyNode& node);
1902 // Helper comparison and hash functions for common cases
1910 bool operator()(const SGPropertyNode* lhs, const SGPropertyNode* rhs) const
1912 return SGPropertyNode::compare(*lhs, *rhs);
1914 bool operator()(SGPropertyNode_ptr lhs, const SGPropertyNode* rhs) const
1916 return SGPropertyNode::compare(*lhs, *rhs);
1918 bool operator()(const SGPropertyNode* lhs, SGPropertyNode_ptr rhs) const
1920 return SGPropertyNode::compare(*lhs, *rhs);
1922 bool operator()(SGPropertyNode_ptr lhs, SGPropertyNode_ptr rhs) const
1924 return SGPropertyNode::compare(*lhs, *rhs);
1930 size_t operator()(const SGPropertyNode* node) const
1932 return hash_value(*node);
1934 size_t operator()(SGPropertyNode_ptr node) const
1936 return hash_value(*node);
1941 #endif // __PROPS_HXX