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);
67 * Property value types.
71 #pragma warn A sloppy coder has defined NONE as a macro!
76 #pragma warn A sloppy coder has defined ALIAS as a macro!
81 #pragma warn A sloppy coder has defined UNSPECIFIED as a macro!
86 #pragma warn A sloppy coder has defined BOOL as a macro!
91 #pragma warn A sloppy coder has defined INT as a macro!
96 #pragma warn A sloppy coder has defined LONG as a macro!
101 #pragma warn A sloppy coder has defined FLOAT as a macro!
106 #pragma warn A sloppy coder has defined DOUBLE as a macro!
111 #pragma warn A sloppy coder has defined STRING as a macro!
118 * The possible types of an SGPropertyNode. Types that appear after
119 * EXTENDED are not stored in the SGPropertyNode itself.
122 NONE = 0, /**< The node hasn't been assigned a value yet. */
123 ALIAS, /**< The node "points" to another node. */
131 EXTENDED, /**< The node's value is not stored in the property;
132 * the actual value and type is retrieved from an
133 * SGRawValue node. This type is never returned by @see
134 * SGPropertyNode::getType.
136 // Extended properties
141 template<typename T> struct PropertyTraits;
143 #define DEFINTERNALPROP(TYPE, PROP) \
145 struct PropertyTraits<TYPE> \
147 static const Type type_tag = PROP; \
148 enum { Internal = 1 }; \
151 DEFINTERNALPROP(bool, BOOL);
152 DEFINTERNALPROP(int, INT);
153 DEFINTERNALPROP(long, LONG);
154 DEFINTERNALPROP(float, FLOAT);
155 DEFINTERNALPROP(double, DOUBLE);
156 DEFINTERNALPROP(const char *, STRING);
157 DEFINTERNALPROP(const char[], STRING);
158 #undef DEFINTERNALPROP
166 ////////////////////////////////////////////////////////////////////////
169 // This is the mechanism that information-providing routines can
170 // use to link their own values to the property manager. Any
171 // SGValue can be tied to a raw value and then untied again.
173 // Note: we are forced to use inlined methods here to ensure
174 // that the templates will be instantiated. We're probably taking
175 // a small performance hit for that.
176 ////////////////////////////////////////////////////////////////////////
179 * Base class for SGRawValue classes that holds no type
180 * information. This allows some generic manipulation of the
187 * Get the type enumeration for the raw value.
191 virtual simgear::props::Type getType() const = 0;
195 * Create a new deep copy of this raw value.
197 * The copy will contain its own version of the underlying value
198 * as well, and will be the same type.
200 * @return A deep copy of the current object.
202 virtual SGRaw* clone() const = 0;
206 class SGRawExtended : public SGRaw
210 * Make an SGRawValueContainer from the SGRawValue.
212 * This is a virtual function of SGRawExtended so that
213 * SGPropertyNode::untie doesn't need to know the type of an
216 virtual SGRawExtended* makeContainer() const = 0;
218 * Write value out to a stream
220 virtual std::ostream& printOn(std::ostream& stream) const = 0;
222 * Read value from a stream and store it.
224 virtual std::istream& readFrom(std::istream& stream) = 0;
227 // Choose between different base classes based on whether the value is
228 // stored internal to the property node. This frees us from defining
229 // the virtual functions in the SGRawExtended interface where they
230 // don't make sense, e.g. readFrom for the const char* type.
231 template<typename T, int internal = simgear::props::PropertyTraits<T>::Internal>
235 class SGRawBase<T, 1> : public SGRaw
240 class SGRawBase<T, 0> : public SGRawExtended
242 virtual SGRawExtended* makeContainer() const;
243 virtual std::ostream& printOn(std::ostream& stream) const;
244 virtual std::istream& readFrom(std::istream& stream);
248 * Abstract base class for a raw value.
250 * <p>The property manager is implemented in two layers. The {@link
251 * SGPropertyNode} is the highest and most abstract layer,
252 * representing an LValue/RValue pair: it records the position of the
253 * property in the property tree and contains facilities for
254 * navigation to other nodes. It is guaranteed to be persistent: the
255 * {@link SGPropertyNode} will not change during a session, even if
256 * the property is bound and unbound multiple times.</p>
258 * <p>When the property value is not managed internally in the
259 * SGPropertyNode, the SGPropertyNode will contain a reference to an
260 * SGRawValue (this class), which provides an abstract way to get,
261 * set, and clone the underlying value. The SGRawValue may change
262 * frequently during a session as a value is retyped or bound and
263 * unbound to various data source, but the abstract SGPropertyNode
264 * layer insulates the application from those changes.
266 * <p>The SGPropertyNode class always keeps a *copy* of a raw value,
267 * not the original one passed to it; if you override a derived class
268 * but do not replace the {@link #clone} method, strange things will
271 * <p>All derived SGRawValue classes must implement {@link #getValue},
272 * {@link #setValue}, and {@link #clone} for the appropriate type.</p>
274 * @see SGPropertyNode
275 * @see SGRawValuePointer
276 * @see SGRawValueFunctions
277 * @see SGRawValueFunctionsIndexed
278 * @see SGRawValueMethods
279 * @see SGRawValueMethodsIndexed
280 * @see SGRawValueContainer
283 class SGRawValue : public SGRawBase<T>
288 * The default underlying value for this type.
290 * Every raw value has a default; the default is false for a
291 * boolean, 0 for the various numeric values, and "" for a string.
292 * If additional types of raw values are added in the future, they
293 * may need different kinds of default values (such as epoch for a
294 * date type). The default value is used when creating new values.
296 static T DefaultValue()
305 * Use the default value for this type.
313 virtual ~SGRawValue () {}
317 * Return the underlying value.
319 * @return The actual value for the property.
322 virtual T getValue () const = 0;
326 * Assign a new underlying value.
328 * If the new value cannot be set (because this is a read-only
329 * raw value, or because the new value is not acceptable for
330 * some reason) this method returns false and leaves the original
333 * @param value The actual value for the property.
334 * @return true if the value was set successfully, false otherwise.
337 virtual bool setValue (T value) = 0;
341 * Return the type tag for this raw value type.
343 virtual simgear::props::Type getType() const
345 return simgear::props::PropertyTraits<T>::type_tag;
351 ////////////////////////////////////////////////////////////////////////
352 // Default values for every type.
353 ////////////////////////////////////////////////////////////////////////
355 template<> inline bool SGRawValue<bool>::DefaultValue()
360 template<> inline const char * SGRawValue<const char *>::DefaultValue()
366 * A raw value bound to a pointer.
368 * This is the most efficient way to tie an external value, but also
369 * the most dangerous, because there is no way for the supplier to
370 * perform bounds checking and derived calculations except by polling
371 * the variable to see if it has changed. There is no default
372 * constructor, because this class would be meaningless without a
376 class SGRawValuePointer : public SGRawValue<T>
381 * Explicit pointer constructor.
383 * Create a new raw value bound to the value of the variable
384 * referenced by the pointer.
386 * @param ptr The pointer to the variable to which this raw value
389 SGRawValuePointer (T * ptr) : _ptr(ptr) {}
394 virtual ~SGRawValuePointer () {}
397 * Get the underlying value.
399 * This method will dereference the pointer and return the
402 virtual T getValue () const { return *_ptr; }
405 * Set the underlying value.
407 * This method will dereference the pointer and change the
410 virtual bool setValue (T value) { *_ptr = value; return true; }
413 * Create a copy of this raw value.
415 * The copy will use the same external pointer as the original.
417 virtual SGRaw* clone () const {
418 return new SGRawValuePointer(_ptr);
427 * A value managed through static functions.
429 * A read-only value will not have a setter; a write-only value will
433 class SGRawValueFunctions : public SGRawValue<T>
438 * The template type of a static getter function.
440 typedef T (*getter_t)();
443 * The template type of a static setter function.
445 typedef void (*setter_t)(T);
448 * Explicit constructor.
450 * Create a new raw value bound to the getter and setter supplied.
452 * @param getter A static function for getting a value, or 0
453 * to read-disable the value.
454 * @param setter A static function for setting a value, or 0
455 * to write-disable the value.
457 SGRawValueFunctions (getter_t getter = 0, setter_t setter = 0)
458 : _getter(getter), _setter(setter) {}
463 virtual ~SGRawValueFunctions () {}
466 * Get the underlying value.
468 * This method will invoke the getter function to get a value.
469 * If no getter function was supplied, this method will always
470 * return the default value for the type.
472 virtual T getValue () const {
473 if (_getter) return (*_getter)();
474 else return SGRawValue<T>::DefaultValue();
478 * Set the underlying value.
480 * This method will invoke the setter function to change the
481 * underlying value. If no setter function was supplied, this
482 * method will return false.
484 virtual bool setValue (T value) {
485 if (_setter) { (*_setter)(value); return true; }
490 * Create a copy of this raw value, bound to the same functions.
492 virtual SGRaw* clone () const {
493 return new SGRawValueFunctions(_getter,_setter);
503 * An indexed value bound to static functions.
505 * A read-only value will not have a setter; a write-only value will
506 * not have a getter. An indexed value is useful for binding one
507 * of a list of possible values (such as multiple engines for a
508 * plane). The index is hard-coded at creation time.
513 class SGRawValueFunctionsIndexed : public SGRawValue<T>
516 typedef T (*getter_t)(int);
517 typedef void (*setter_t)(int,T);
518 SGRawValueFunctionsIndexed (int index, getter_t getter = 0, setter_t setter = 0)
519 : _index(index), _getter(getter), _setter(setter) {}
520 virtual ~SGRawValueFunctionsIndexed () {}
521 virtual T getValue () const {
522 if (_getter) return (*_getter)(_index);
523 else return SGRawValue<T>::DefaultValue();
525 virtual bool setValue (T value) {
526 if (_setter) { (*_setter)(_index, value); return true; }
529 virtual SGRaw* clone () const {
530 return new SGRawValueFunctionsIndexed(_index, _getter, _setter);
540 * A value managed through an object and access methods.
542 * A read-only value will not have a setter; a write-only value will
545 template <class C, class T>
546 class SGRawValueMethods : public SGRawValue<T>
549 typedef T (C::*getter_t)() const;
550 typedef void (C::*setter_t)(T);
551 SGRawValueMethods (C &obj, getter_t getter = 0, setter_t setter = 0)
552 : _obj(obj), _getter(getter), _setter(setter) {}
553 virtual ~SGRawValueMethods () {}
554 virtual T getValue () const {
555 if (_getter) { return (_obj.*_getter)(); }
556 else { return SGRawValue<T>::DefaultValue(); }
558 virtual bool setValue (T value) {
559 if (_setter) { (_obj.*_setter)(value); return true; }
562 virtual SGRaw* clone () const {
563 return new SGRawValueMethods(_obj, _getter, _setter);
573 * An indexed value managed through an object and access methods.
575 * A read-only value will not have a setter; a write-only value will
578 template <class C, class T>
579 class SGRawValueMethodsIndexed : public SGRawValue<T>
582 typedef T (C::*getter_t)(int) const;
583 typedef void (C::*setter_t)(int, T);
584 SGRawValueMethodsIndexed (C &obj, int index,
585 getter_t getter = 0, setter_t setter = 0)
586 : _obj(obj), _index(index), _getter(getter), _setter(setter) {}
587 virtual ~SGRawValueMethodsIndexed () {}
588 virtual T getValue () const {
589 if (_getter) { return (_obj.*_getter)(_index); }
590 else { return SGRawValue<T>::DefaultValue(); }
592 virtual bool setValue (T value) {
593 if (_setter) { (_obj.*_setter)(_index, value); return true; }
596 virtual SGRaw* clone () const {
597 return new SGRawValueMethodsIndexed(_obj, _index, _getter, _setter);
607 * A raw value that contains its value. This provides a way for
608 * property nodes to contain values that shouldn't be stored in the
609 * property node itself.
612 class SGRawValueContainer : public SGRawValue<T>
617 * Explicit constructor.
619 SGRawValueContainer(const T& obj) : _obj(obj) {}
624 virtual ~SGRawValueContainer() {}
627 * Get the underlying value.
629 virtual T getValue() const { return _obj; }
632 * Set the underlying value.
634 * This method will dereference the pointer and change the
637 virtual bool setValue (T value) { _obj = value; return true; }
640 * Create a copy of this raw value.
642 virtual SGRaw* clone () const {
643 return new SGRawValueContainer(_obj);
651 SGRawExtended* SGRawBase<T, 0>::makeContainer() const
653 return new SGRawValueContainer<T>(static_cast<const SGRawValue<T>*>(this)
658 std::ostream& SGRawBase<T, 0>::printOn(std::ostream& stream) const
660 return stream << static_cast<SGRawValue<T>*>(this)->getValue();
664 std::istream& SGRawBase<T, 0>::readFrom(std::istream& stream)
667 simgear::readFrom(stream, value);
668 static_cast<SGRawValue<T>*>(this)->setValue(value);
674 * The smart pointer that manage reference counting
676 class SGPropertyNode;
677 typedef SGSharedPtr<SGPropertyNode> SGPropertyNode_ptr;
678 typedef SGSharedPtr<const SGPropertyNode> SGConstPropertyNode_ptr;
682 typedef std::vector<SGPropertyNode_ptr> PropertyList;
687 * The property change listener interface.
689 * <p>Any class that needs to listen for property changes must implement
690 * this interface.</p>
692 class SGPropertyChangeListener
695 virtual ~SGPropertyChangeListener ();
696 virtual void valueChanged (SGPropertyNode * node);
697 virtual void childAdded (SGPropertyNode * parent, SGPropertyNode * child);
698 virtual void childRemoved (SGPropertyNode * parent, SGPropertyNode * child);
701 friend class SGPropertyNode;
702 virtual void register_property (SGPropertyNode * node);
703 virtual void unregister_property (SGPropertyNode * node);
706 std::vector<SGPropertyNode *> _properties;
712 * A node in a property tree.
714 class SGPropertyNode : public SGReferenced
722 MAX_STRING_LEN = 1024
726 * Access mode attributes.
728 * <p>The ARCHIVE attribute is strictly advisory, and controls
729 * whether the property should normally be saved and restored.</p>
740 // beware: if you add another attribute here,
741 // also update value of "LAST_USED_ATTRIBUTE".
746 * Last used attribute
747 * Update as needed when enum Attribute is changed
749 static const int LAST_USED_ATTRIBUTE;
752 * Default constructor.
760 SGPropertyNode (const SGPropertyNode &node);
766 virtual ~SGPropertyNode ();
775 * Test whether this node contains a primitive leaf value.
777 bool hasValue () const { return (_type != simgear::props::NONE); }
781 * Get the node's simple (XML) name.
783 const char * getName () const { return _name.c_str(); }
786 * Get the node's simple name as a string.
788 const std::string& getNameString () const { return _name; }
791 * Get the node's pretty display name, with subscript when needed.
793 std::string getDisplayName (bool simplify = false) const;
797 * Get the node's integer index.
799 int getIndex () const { return _index; }
803 * Get a non-const pointer to the node's parent.
805 SGPropertyNode * getParent () { return _parent; }
809 * Get a const pointer to the node's parent.
811 const SGPropertyNode * getParent () const { return _parent; }
820 * Get the number of child nodes.
822 int nChildren () const { return (int)_children.size(); }
826 * Get a child node by position (*NOT* index).
828 SGPropertyNode * getChild (int position);
832 * Get a const child node by position (*NOT* index).
834 const SGPropertyNode * getChild (int position) const;
838 * Test whether a named child exists.
840 bool hasChild (const char * name, int index = 0) const
842 return (getChild(name, index) != 0);
846 * Test whether a named child exists.
848 bool hasChild (const std::string& name, int index = 0) const
850 return (getChild(name, index) != 0);
854 * Create a new child node with the given name and an unused index
856 * @param min_index Minimal index for new node (skips lower indices)
857 * @param append Whether to simply use the index after the last used index
858 * or use a lower, unused index if it exists
860 SGPropertyNode * addChild ( const char* name,
862 bool append = true );
863 SGPropertyNode * addChild ( const std::string& name,
866 { return addChild(name.c_str(), min_index, append); }
869 * Create multiple child nodes with the given name an unused indices
871 * @param count The number of nodes create
872 * @param min_index Minimal index for new nodes (skips lower indices)
873 * @param append Whether to simply use the index after the last used index
874 * or use a lower, unused index if it exists
876 simgear::PropertyList addChildren ( const std::string& name,
879 bool append = true );
882 * Get a child node by name and index.
884 SGPropertyNode * getChild (const char* name, int index = 0,
885 bool create = false);
886 SGPropertyNode * getChild (const std::string& name, int index = 0,
887 bool create = false);
889 * Get a const child node by name and index.
891 const SGPropertyNode * getChild (const char * name, int index = 0) const;
894 * Get a const child node by name and index.
896 const SGPropertyNode * getChild (const std::string& name, int index = 0) const
897 { return getChild(name.c_str(), index); }
901 * Get a vector of all children with the specified name.
903 simgear::PropertyList getChildren (const char * name) const;
906 * Get a vector of all children with the specified name.
908 simgear::PropertyList getChildren (const std::string& name) const
909 { return getChildren(name.c_str()); }
912 * Remove child by position.
914 SGPropertyNode_ptr removeChild (int pos, bool keep = true);
918 * Remove a child node
920 SGPropertyNode_ptr removeChild (const char * name, int index = 0,
924 * Remove a child node
926 SGPropertyNode_ptr removeChild (const std::string& name, int index = 0,
928 { return removeChild(name.c_str(), index, keep); }
931 * Remove all children with the specified name.
933 simgear::PropertyList removeChildren (const char * name, bool keep = true);
936 * Remove all children with the specified name.
938 simgear::PropertyList removeChildren (const std::string& name,
940 { return removeChildren(name.c_str(), keep); }
948 * Alias this node's leaf value to another's.
950 bool alias (SGPropertyNode * target);
954 * Alias this node's leaf value to another's by relative path.
956 bool alias (const char * path);
959 * Alias this node's leaf value to another's by relative path.
961 bool alias (const std::string& path)
962 { return alias(path.c_str()); }
966 * Remove any alias for this node.
972 * Test whether the node's leaf value is aliased to another's.
974 bool isAlias () const { return (_type == simgear::props::ALIAS); }
978 * Get a non-const pointer to the current alias target, if any.
980 SGPropertyNode * getAliasTarget ();
984 * Get a const pointer to the current alias target, if any.
986 const SGPropertyNode * getAliasTarget () const;
995 * Get the path to this node from the root.
997 std::string getPath (bool simplify = false) const;
1001 * Get a pointer to the root node.
1003 SGPropertyNode * getRootNode ();
1007 * Get a const pointer to the root node.
1009 const SGPropertyNode * getRootNode () const;
1013 * Get a pointer to another node by relative path.
1015 SGPropertyNode * getNode (const char * relative_path, bool create = false);
1018 * Get a pointer to another node by relative path.
1020 SGPropertyNode * getNode (const std::string& relative_path, bool create = false)
1021 { return getNode(relative_path.c_str(), create); }
1024 * Get a pointer to another node by relative path.
1026 * This method leaves the index off the last member of the path,
1027 * so that the user can specify it separately (and save some
1028 * string building). For example, getNode("/bar[1]/foo", 3) is
1029 * exactly equivalent to getNode("bar[1]/foo[3]"). The index
1030 * provided overrides any given in the path itself for the last
1033 SGPropertyNode * getNode (const char * relative_path, int index,
1034 bool create = false);
1037 * Get a pointer to another node by relative path.
1039 * This method leaves the index off the last member of the path,
1040 * so that the user can specify it separately (and save some
1041 * string building). For example, getNode("/bar[1]/foo", 3) is
1042 * exactly equivalent to getNode("bar[1]/foo[3]"). The index
1043 * provided overrides any given in the path itself for the last
1046 SGPropertyNode * getNode (const std::string& relative_path, int index,
1047 bool create = false)
1048 { return getNode(relative_path.c_str(), index, create); }
1051 * Get a const pointer to another node by relative path.
1053 const SGPropertyNode * getNode (const char * relative_path) const;
1056 * Get a const pointer to another node by relative path.
1058 const SGPropertyNode * getNode (const std::string& relative_path) const
1059 { return getNode(relative_path.c_str()); }
1063 * Get a const pointer to another node by relative path.
1065 * This method leaves the index off the last member of the path,
1066 * so that the user can specify it separate.
1068 const SGPropertyNode * getNode (const char * relative_path,
1072 * Get a const pointer to another node by relative path.
1074 * This method leaves the index off the last member of the path,
1075 * so that the user can specify it separate.
1077 const SGPropertyNode * getNode (const std::string& relative_path,
1079 { return getNode(relative_path.c_str(), index); }
1086 * Check a single mode attribute for the property node.
1088 bool getAttribute (Attribute attr) const { return ((_attr & attr) != 0); }
1092 * Set a single mode attribute for the property node.
1094 void setAttribute (Attribute attr, bool state) {
1095 (state ? _attr |= attr : _attr &= ~attr);
1100 * Get all of the mode attributes for the property node.
1102 int getAttributes () const { return _attr; }
1106 * Set all of the mode attributes for the property node.
1108 void setAttributes (int attr) { _attr = attr; }
1112 // Leaf Value (primitive).
1117 * Get the type of leaf value, if any, for this node.
1119 simgear::props::Type getType () const;
1123 * Get a bool value for this node.
1125 bool getBoolValue () const;
1129 * Get an int value for this node.
1131 int getIntValue () const;
1135 * Get a long int value for this node.
1137 long getLongValue () const;
1141 * Get a float value for this node.
1143 float getFloatValue () const;
1147 * Get a double value for this node.
1149 double getDoubleValue () const;
1153 * Get a string value for this node.
1155 const char * getStringValue () const;
1158 * Get a value from a node. If the actual type of the node doesn't
1159 * match the desired type, a conversion isn't guaranteed.
1161 template<typename T>
1162 T getValue(typename boost::enable_if_c<simgear::props::PropertyTraits<T>::Internal>
1163 ::type* dummy = 0) const;
1164 // Getter for extended property
1165 template<typename T>
1166 T getValue(typename boost::disable_if_c<simgear::props::PropertyTraits<T>::Internal>
1167 ::type* dummy = 0) const;
1170 * Set a bool value for this node.
1172 bool setBoolValue (bool value);
1176 * Set an int value for this node.
1178 bool setIntValue (int value);
1182 * Set a long int value for this node.
1184 bool setLongValue (long value);
1188 * Set a float value for this node.
1190 bool setFloatValue (float value);
1194 * Set a double value for this node.
1196 bool setDoubleValue (double value);
1200 * Set a string value for this node.
1202 bool setStringValue (const char * value);
1205 * Set a string value for this node.
1207 bool setStringValue (const std::string& value)
1208 { return setStringValue(value.c_str()); }
1212 * Set a value of unspecified type for this node.
1214 bool setUnspecifiedValue (const char * value);
1216 template<typename T>
1217 bool setValue(const T& val,
1218 typename boost::enable_if_c<simgear::props::PropertyTraits<T>::Internal>
1221 template<typename T>
1222 bool setValue(const T& val,
1223 typename boost::disable_if_c<simgear::props::PropertyTraits<T>::Internal>
1227 bool setValue(const char (&val)[N])
1229 return setValue(&val[0]);
1233 * Print the value of the property to a stream.
1235 std::ostream& printOn(std::ostream& stream) const;
1243 * Test whether this node is bound to an external data source.
1245 bool isTied () const { return _tied; }
1248 * Bind this node to an external source.
1250 template<typename T>
1251 bool tie(const SGRawValue<T> &rawValue, bool useDefault = true);
1254 * Unbind this node from any external data source.
1260 // Convenience methods using paths.
1261 // TODO: add attribute methods
1266 * Get another node's type.
1268 simgear::props::Type getType (const char * relative_path) const;
1271 * Get another node's type.
1273 simgear::props::Type getType (const std::string& relative_path) const
1274 { return getType(relative_path.c_str()); }
1277 * Test whether another node has a leaf value.
1279 bool hasValue (const char * relative_path) const;
1282 * Test whether another node has a leaf value.
1284 bool hasValue (const std::string& relative_path) const
1285 { return hasValue(relative_path.c_str()); }
1288 * Get another node's value as a bool.
1290 bool getBoolValue (const char * relative_path,
1291 bool defaultValue = false) const;
1294 * Get another node's value as a bool.
1296 bool getBoolValue (const std::string& relative_path,
1297 bool defaultValue = false) const
1298 { return getBoolValue(relative_path.c_str(), defaultValue); }
1301 * Get another node's value as an int.
1303 int getIntValue (const char * relative_path,
1304 int defaultValue = 0) const;
1307 * Get another node's value as an int.
1309 int getIntValue (const std::string& relative_path,
1310 int defaultValue = 0) const
1311 { return getIntValue(relative_path.c_str(), defaultValue); }
1315 * Get another node's value as a long int.
1317 long getLongValue (const char * relative_path,
1318 long defaultValue = 0L) const;
1321 * Get another node's value as a long int.
1323 long getLongValue (const std::string& relative_path,
1324 long defaultValue = 0L) const
1325 { return getLongValue(relative_path.c_str(), defaultValue); }
1328 * Get another node's value as a float.
1330 float getFloatValue (const char * relative_path,
1331 float defaultValue = 0.0f) const;
1334 * Get another node's value as a float.
1336 float getFloatValue (const std::string& relative_path,
1337 float defaultValue = 0.0f) const
1338 { return getFloatValue(relative_path.c_str(), defaultValue); }
1342 * Get another node's value as a double.
1344 double getDoubleValue (const char * relative_path,
1345 double defaultValue = 0.0) const;
1348 * Get another node's value as a double.
1350 double getDoubleValue (const std::string& relative_path,
1351 double defaultValue = 0.0) const
1352 { return getDoubleValue(relative_path.c_str(), defaultValue); }
1355 * Get another node's value as a string.
1357 const char * getStringValue (const char * relative_path,
1358 const char * defaultValue = "") const;
1362 * Get another node's value as a string.
1364 const char * getStringValue (const std::string& relative_path,
1365 const char * defaultValue = "") const
1366 { return getStringValue(relative_path.c_str(), defaultValue); }
1370 * Set another node's value as a bool.
1372 bool setBoolValue (const char * relative_path, bool value);
1375 * Set another node's value as a bool.
1377 bool setBoolValue (const std::string& relative_path, bool value)
1378 { return setBoolValue(relative_path.c_str(), value); }
1382 * Set another node's value as an int.
1384 bool setIntValue (const char * relative_path, int value);
1387 * Set another node's value as an int.
1389 bool setIntValue (const std::string& relative_path, int value)
1390 { return setIntValue(relative_path.c_str(), value); }
1394 * Set another node's value as a long int.
1396 bool setLongValue (const char * relative_path, long value);
1399 * Set another node's value as a long int.
1401 bool setLongValue (const std::string& relative_path, long value)
1402 { return setLongValue(relative_path.c_str(), value); }
1406 * Set another node's value as a float.
1408 bool setFloatValue (const char * relative_path, float value);
1411 * Set another node's value as a float.
1413 bool setFloatValue (const std::string& relative_path, float value)
1414 { return setFloatValue(relative_path.c_str(), value); }
1418 * Set another node's value as a double.
1420 bool setDoubleValue (const char * relative_path, double value);
1423 * Set another node's value as a double.
1425 bool setDoubleValue (const std::string& relative_path, double value)
1426 { return setDoubleValue(relative_path.c_str(), value); }
1430 * Set another node's value as a string.
1432 bool setStringValue (const char * relative_path, const char * value);
1434 bool setStringValue(const char * relative_path, const std::string& value)
1435 { return setStringValue(relative_path, value.c_str()); }
1437 * Set another node's value as a string.
1439 bool setStringValue (const std::string& relative_path, const char * value)
1440 { return setStringValue(relative_path.c_str(), value); }
1442 bool setStringValue (const std::string& relative_path,
1443 const std::string& value)
1444 { return setStringValue(relative_path.c_str(), value.c_str()); }
1447 * Set another node's value with no specified type.
1449 bool setUnspecifiedValue (const char * relative_path, const char * value);
1453 * Test whether another node is bound to an external data source.
1455 bool isTied (const char * relative_path) const;
1458 * Test whether another node is bound to an external data source.
1460 bool isTied (const std::string& relative_path) const
1461 { return isTied(relative_path.c_str()); }
1464 * Bind another node to an external bool source.
1466 bool tie (const char * relative_path, const SGRawValue<bool> &rawValue,
1467 bool useDefault = true);
1470 * Bind another node to an external bool source.
1472 bool tie (const std::string& relative_path, const SGRawValue<bool> &rawValue,
1473 bool useDefault = true)
1474 { return tie(relative_path.c_str(), rawValue, useDefault); }
1478 * Bind another node to an external int source.
1480 bool tie (const char * relative_path, const SGRawValue<int> &rawValue,
1481 bool useDefault = true);
1484 * Bind another node to an external int source.
1486 bool tie (const std::string& relative_path, const SGRawValue<int> &rawValue,
1487 bool useDefault = true)
1488 { return tie(relative_path.c_str(), rawValue, useDefault); }
1492 * Bind another node to an external long int source.
1494 bool tie (const char * relative_path, const SGRawValue<long> &rawValue,
1495 bool useDefault = true);
1498 * Bind another node to an external long int source.
1500 bool tie (const std::string& relative_path, const SGRawValue<long> &rawValue,
1501 bool useDefault = true)
1502 { return tie(relative_path.c_str(), rawValue, useDefault); }
1506 * Bind another node to an external float source.
1508 bool tie (const char * relative_path, const SGRawValue<float> &rawValue,
1509 bool useDefault = true);
1512 * Bind another node to an external float source.
1514 bool tie (const std::string& relative_path, const SGRawValue<float> &rawValue,
1515 bool useDefault = true)
1516 { return tie(relative_path.c_str(), rawValue, useDefault); }
1520 * Bind another node to an external double source.
1522 bool tie (const char * relative_path, const SGRawValue<double> &rawValue,
1523 bool useDefault = true);
1526 * Bind another node to an external double source.
1528 bool tie (const std::string& relative_path, const SGRawValue<double> &rawValue,
1529 bool useDefault = true)
1530 { return tie(relative_path.c_str(), rawValue, useDefault); }
1534 * Bind another node to an external string source.
1536 bool tie (const char * relative_path, const SGRawValue<const char *> &rawValue,
1537 bool useDefault = true);
1540 * Bind another node to an external string source.
1542 bool tie (const std::string& relative_path, const SGRawValue<const char*> &rawValue,
1543 bool useDefault = true)
1544 { return tie(relative_path.c_str(), rawValue, useDefault); }
1548 * Unbind another node from any external data source.
1550 bool untie (const char * relative_path);
1553 * Unbind another node from any external data source.
1555 bool untie (const std::string& relative_path)
1556 { return untie(relative_path.c_str()); }
1560 * Add a change listener to the property. If "initial" is set call the
1561 * listener initially.
1563 void addChangeListener (SGPropertyChangeListener * listener,
1564 bool initial = false);
1568 * Remove a change listener from the property.
1570 void removeChangeListener (SGPropertyChangeListener * listener);
1574 * Get the number of listeners.
1576 int nListeners () const { return _listeners ? (int)_listeners->size() : 0; }
1580 * Fire a value change event to all listeners.
1582 void fireValueChanged ();
1586 * Fire a child-added event to all listeners.
1588 void fireChildAdded (SGPropertyNode * child);
1592 * Fire a child-removed event to all listeners.
1594 void fireChildRemoved (SGPropertyNode * child);
1598 * Clear any existing value and set the type to NONE.
1603 * Compare two property trees. The property trees are equal if: 1)
1604 * They have no children, and have the same type and the values are
1605 * equal, or 2) have the same number of children, and the
1606 * corresponding children in each tree are equal. "corresponding"
1607 * means have the same name and index.
1609 * Attributes, removed children, and aliases aren't considered.
1611 static bool compare (const SGPropertyNode& lhs, const SGPropertyNode& rhs);
1615 void fireValueChanged (SGPropertyNode * node);
1616 void fireChildAdded (SGPropertyNode * parent, SGPropertyNode * child);
1617 void fireChildRemoved (SGPropertyNode * parent, SGPropertyNode * child);
1620 * Protected constructor for making new nodes on demand.
1622 SGPropertyNode (const std::string& name, int index, SGPropertyNode * parent);
1623 template<typename Itr>
1624 SGPropertyNode (Itr begin, Itr end, int index, SGPropertyNode * parent);
1628 // Get the raw value
1629 bool get_bool () const;
1630 int get_int () const;
1631 long get_long () const;
1632 float get_float () const;
1633 double get_double () const;
1634 const char * get_string () const;
1636 // Set the raw value
1637 bool set_bool (bool value);
1638 bool set_int (int value);
1639 bool set_long (long value);
1640 bool set_float (float value);
1641 bool set_double (double value);
1642 bool set_string (const char * value);
1646 * Get the value as a string.
1648 const char * make_string () const;
1651 * Trace a read access.
1653 void trace_read () const;
1657 * Trace a write access.
1659 void trace_write () const;
1663 /// To avoid cyclic reference counting loops this shall not be a reference
1665 SGPropertyNode * _parent;
1666 simgear::PropertyList _children;
1667 simgear::PropertyList _removedChildren;
1668 mutable std::string _buffer;
1669 simgear::props::Type _type;
1673 // The right kind of pointer...
1675 SGPropertyNode * alias;
1688 std::vector<SGPropertyChangeListener *> * _listeners;
1691 // Pass name as a pair of iterators
1692 template<typename Itr>
1693 SGPropertyNode * getChildImpl (Itr begin, Itr end, int index = 0, bool create = false);
1694 // very internal method
1695 template<typename Itr>
1696 SGPropertyNode* getExistingChild (Itr begin, Itr end, int index, bool create);
1697 // very internal path parsing function
1698 template<typename SplitItr>
1699 friend SGPropertyNode* find_node_aux(SGPropertyNode * current, SplitItr& itr,
1700 bool create, int last_index);
1702 friend size_t hash_value(const SGPropertyNode& node);
1705 // Convenience functions for use in templates
1706 template<typename T>
1707 T getValue(const SGPropertyNode*);
1710 inline bool getValue<bool>(const SGPropertyNode* node) { return node->getBoolValue(); }
1713 inline int getValue<int>(const SGPropertyNode* node) { return node->getIntValue(); }
1716 inline long getValue<long>(const SGPropertyNode* node) { return node->getLongValue(); }
1719 inline float getValue<float>(const SGPropertyNode* node)
1721 return node->getFloatValue();
1725 inline double getValue<double>(const SGPropertyNode* node)
1727 return node->getDoubleValue();
1731 inline const char * getValue<const char*>(const SGPropertyNode* node)
1733 return node->getStringValue ();
1736 inline bool setValue(SGPropertyNode* node, bool value)
1738 return node->setBoolValue(value);
1741 inline bool setValue(SGPropertyNode* node, int value)
1743 return node->setIntValue(value);
1746 inline bool setValue(SGPropertyNode* node, long value)
1748 return node->setLongValue(value);
1751 inline bool setValue(SGPropertyNode* node, float value)
1753 return node->setFloatValue(value);
1756 inline bool setValue(SGPropertyNode* node, double value)
1758 return node->setDoubleValue(value);
1761 inline bool setValue(SGPropertyNode* node, const char* value)
1763 return node->setStringValue(value);
1766 inline bool setValue (SGPropertyNode* node, const std::string& value)
1768 return node->setStringValue(value.c_str());
1771 template<typename T>
1772 bool SGPropertyNode::tie(const SGRawValue<T> &rawValue, bool useDefault)
1774 using namespace simgear::props;
1775 if (_type == ALIAS || _tied)
1778 useDefault = useDefault && hasValue();
1779 T old_val = SGRawValue<T>::DefaultValue();
1781 old_val = getValue<T>(this);
1783 if (PropertyTraits<T>::Internal)
1784 _type = PropertyTraits<T>::type_tag;
1788 _value.val = rawValue.clone();
1790 int save_attributes = getAttributes();
1791 setAttribute( WRITE, true );
1793 setAttributes( save_attributes );
1799 bool SGPropertyNode::tie (const SGRawValue<const char *> &rawValue,
1802 template<typename T>
1803 T SGPropertyNode::getValue(typename boost::disable_if_c<simgear::props
1804 ::PropertyTraits<T>::Internal>::type* dummy) const
1806 using namespace simgear::props;
1807 if (_attr == (READ|WRITE) && _type == EXTENDED
1808 && _value.val->getType() == PropertyTraits<T>::type_tag) {
1809 return static_cast<SGRawValue<T>*>(_value.val)->getValue();
1811 if (getAttribute(TRACE_READ))
1813 if (!getAttribute(READ))
1814 return SGRawValue<T>::DefaultValue();
1817 if (_value.val->getType() == PropertyTraits<T>::type_tag)
1818 return static_cast<SGRawValue<T>*>(_value.val)->getValue();
1822 return simgear::parseString<T>(make_string());
1824 default: // avoid compiler warning
1827 return SGRawValue<T>::DefaultValue();
1830 template<typename T>
1831 inline T SGPropertyNode::getValue(typename boost::enable_if_c<simgear::props
1832 ::PropertyTraits<T>::Internal>::type* dummy) const
1834 return ::getValue<T>(this);
1837 template<typename T>
1838 bool SGPropertyNode::setValue(const T& val,
1839 typename boost::disable_if_c<simgear::props
1840 ::PropertyTraits<T>::Internal>::type* dummy)
1842 using namespace simgear::props;
1843 if (_attr == (READ|WRITE) && _type == EXTENDED
1844 && _value.val->getType() == PropertyTraits<T>::type_tag) {
1845 static_cast<SGRawValue<T>*>(_value.val)->setValue(val);
1848 if (getAttribute(WRITE)
1849 && ((_type == EXTENDED
1850 && _value.val->getType() == PropertyTraits<T>::type_tag)
1851 || _type == NONE || _type == UNSPECIFIED)) {
1852 if (_type == NONE || _type == UNSPECIFIED) {
1855 _value.val = new SGRawValueContainer<T>(val);
1857 static_cast<SGRawValue<T>*>(_value.val)->setValue(val);
1859 if (getAttribute(TRACE_WRITE))
1866 template<typename T>
1867 inline bool SGPropertyNode::setValue(const T& val,
1868 typename boost::enable_if_c<simgear::props
1869 ::PropertyTraits<T>::Internal>::type* dummy)
1871 return ::setValue(this, val);
1875 * Utility function for creation of a child property node.
1877 inline SGPropertyNode* makeChild(SGPropertyNode* parent, const char* name,
1880 return parent->getChild(name, index, true);
1884 * Utility function for creation of a child property node using a
1889 template<typename StringType>
1890 inline SGPropertyNode* makeNode(SGPropertyNode* parent, const StringType& name)
1892 return parent->getNode(name, true);
1897 size_t hash_value(const SGPropertyNode& node);
1899 // Helper comparison and hash functions for common cases
1907 bool operator()(const SGPropertyNode* lhs, const SGPropertyNode* rhs) const
1909 return SGPropertyNode::compare(*lhs, *rhs);
1911 bool operator()(SGPropertyNode_ptr lhs, const SGPropertyNode* rhs) const
1913 return SGPropertyNode::compare(*lhs, *rhs);
1915 bool operator()(const SGPropertyNode* lhs, SGPropertyNode_ptr rhs) const
1917 return SGPropertyNode::compare(*lhs, *rhs);
1919 bool operator()(SGPropertyNode_ptr lhs, SGPropertyNode_ptr rhs) const
1921 return SGPropertyNode::compare(*lhs, *rhs);
1927 size_t operator()(const SGPropertyNode* node) const
1929 return hash_value(*node);
1931 size_t operator()(SGPropertyNode_ptr node) const
1933 return hash_value(*node);
1939 /** Convenience class for change listener callbacks without
1940 * creating a derived class implementing a "valueChanged" method.
1941 * Also removes listener on destruction automatically.
1944 class SGPropertyChangeCallback
1945 : public SGPropertyChangeListener
1948 SGPropertyChangeCallback(T* obj, void (T::*method)(SGPropertyNode*),
1949 SGPropertyNode_ptr property,bool initial=false)
1950 : _obj(obj), _callback(method), _property(property)
1952 _property->addChangeListener(this,initial);
1954 virtual ~SGPropertyChangeCallback()
1956 _property->removeChangeListener(this);
1958 void valueChanged (SGPropertyNode * node)
1960 (_obj->*_callback)(node);
1964 void (T::*_callback)(SGPropertyNode*);
1965 SGPropertyNode_ptr _property;
1968 #endif // __PROPS_HXX