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/math/sg_types.hxx>
35 #include <simgear/structure/SGReferenced.hxx>
36 #include <simgear/structure/SGSharedPtr.hxx>
38 // XXX This whole file should be in the simgear namespace, but I don't
39 // have the guts yet...
44 class PropertyInterpolationMgr;
47 std::istream& readFrom(std::istream& stream, T& result)
54 * Parse a string as an object of a given type.
55 * XXX no error behavior yet.
57 * @tparam T the return type
58 * @param str the string
62 inline T parseString(const std::string& str)
64 std::istringstream stream(str);
66 readFrom(stream, result);
71 * Property value types.
75 #pragma warn A sloppy coder has defined NONE as a macro!
80 #pragma warn A sloppy coder has defined ALIAS as a macro!
85 #pragma warn A sloppy coder has defined UNSPECIFIED as a macro!
90 #pragma warn A sloppy coder has defined BOOL as a macro!
95 #pragma warn A sloppy coder has defined INT as a macro!
100 #pragma warn A sloppy coder has defined LONG as a macro!
105 #pragma warn A sloppy coder has defined FLOAT as a macro!
110 #pragma warn A sloppy coder has defined DOUBLE as a macro!
115 #pragma warn A sloppy coder has defined STRING as a macro!
122 * The possible types of an SGPropertyNode. Types that appear after
123 * EXTENDED are not stored in the SGPropertyNode itself.
126 NONE = 0, /**< The node hasn't been assigned a value yet. */
127 ALIAS, /**< The node "points" to another node. */
135 EXTENDED, /**< The node's value is not stored in the property;
136 * the actual value and type is retrieved from an
137 * SGRawValue node. This type is never returned by @see
138 * SGPropertyNode::getType.
140 // Extended properties
145 template<typename T> struct PropertyTraits;
147 #define DEFINTERNALPROP(TYPE, PROP) \
149 struct PropertyTraits<TYPE> \
151 static const Type type_tag = PROP; \
152 enum { Internal = 1 }; \
155 DEFINTERNALPROP(bool, BOOL);
156 DEFINTERNALPROP(int, INT);
157 DEFINTERNALPROP(long, LONG);
158 DEFINTERNALPROP(float, FLOAT);
159 DEFINTERNALPROP(double, DOUBLE);
160 DEFINTERNALPROP(const char *, STRING);
161 DEFINTERNALPROP(const char[], STRING);
162 #undef DEFINTERNALPROP
169 ////////////////////////////////////////////////////////////////////////
172 // This is the mechanism that information-providing routines can
173 // use to link their own values to the property manager. Any
174 // SGValue can be tied to a raw value and then untied again.
176 // Note: we are forced to use inlined methods here to ensure
177 // that the templates will be instantiated. We're probably taking
178 // a small performance hit for that.
179 ////////////////////////////////////////////////////////////////////////
182 * Base class for SGRawValue classes that holds no type
183 * information. This allows some generic manipulation of the
190 * Get the type enumeration for the raw value.
194 virtual simgear::props::Type getType() const = 0;
198 * Create a new deep copy of this raw value.
200 * The copy will contain its own version of the underlying value
201 * as well, and will be the same type.
203 * @return A deep copy of the current object.
205 virtual SGRaw* clone() const = 0;
209 class SGRawExtended : public SGRaw
213 * Make an SGRawValueContainer from the SGRawValue.
215 * This is a virtual function of SGRawExtended so that
216 * SGPropertyNode::untie doesn't need to know the type of an
219 virtual SGRawExtended* makeContainer() const = 0;
221 * Write value out to a stream
223 virtual std::ostream& printOn(std::ostream& stream) const = 0;
225 * Read value from a stream and store it.
227 virtual std::istream& readFrom(std::istream& stream) = 0;
230 // Choose between different base classes based on whether the value is
231 // stored internal to the property node. This frees us from defining
232 // the virtual functions in the SGRawExtended interface where they
233 // don't make sense, e.g. readFrom for the const char* type.
234 template<typename T, int internal = simgear::props::PropertyTraits<T>::Internal>
238 class SGRawBase<T, 1> : public SGRaw
243 class SGRawBase<T, 0> : public SGRawExtended
245 virtual SGRawExtended* makeContainer() const;
246 virtual std::ostream& printOn(std::ostream& stream) const;
247 virtual std::istream& readFrom(std::istream& stream);
251 * Abstract base class for a raw value.
253 * <p>The property manager is implemented in two layers. The {@link
254 * SGPropertyNode} is the highest and most abstract layer,
255 * representing an LValue/RValue pair: it records the position of the
256 * property in the property tree and contains facilities for
257 * navigation to other nodes. It is guaranteed to be persistent: the
258 * {@link SGPropertyNode} will not change during a session, even if
259 * the property is bound and unbound multiple times.</p>
261 * <p>When the property value is not managed internally in the
262 * SGPropertyNode, the SGPropertyNode will contain a reference to an
263 * SGRawValue (this class), which provides an abstract way to get,
264 * set, and clone the underlying value. The SGRawValue may change
265 * frequently during a session as a value is retyped or bound and
266 * unbound to various data source, but the abstract SGPropertyNode
267 * layer insulates the application from those changes.
269 * <p>The SGPropertyNode class always keeps a *copy* of a raw value,
270 * not the original one passed to it; if you override a derived class
271 * but do not replace the {@link #clone} method, strange things will
274 * <p>All derived SGRawValue classes must implement {@link #getValue},
275 * {@link #setValue}, and {@link #clone} for the appropriate type.</p>
277 * @see SGPropertyNode
278 * @see SGRawValuePointer
279 * @see SGRawValueFunctions
280 * @see SGRawValueFunctionsIndexed
281 * @see SGRawValueMethods
282 * @see SGRawValueMethodsIndexed
283 * @see SGRawValueContainer
286 class SGRawValue : public SGRawBase<T>
291 * The default underlying value for this type.
293 * Every raw value has a default; the default is false for a
294 * boolean, 0 for the various numeric values, and "" for a string.
295 * If additional types of raw values are added in the future, they
296 * may need different kinds of default values (such as epoch for a
297 * date type). The default value is used when creating new values.
299 static T DefaultValue()
308 * Use the default value for this type.
316 virtual ~SGRawValue () {}
320 * Return the underlying value.
322 * @return The actual value for the property.
325 virtual T getValue () const = 0;
329 * Assign a new underlying value.
331 * If the new value cannot be set (because this is a read-only
332 * raw value, or because the new value is not acceptable for
333 * some reason) this method returns false and leaves the original
336 * @param value The actual value for the property.
337 * @return true if the value was set successfully, false otherwise.
340 virtual bool setValue (T value) = 0;
344 * Return the type tag for this raw value type.
346 virtual simgear::props::Type getType() const
348 return simgear::props::PropertyTraits<T>::type_tag;
354 ////////////////////////////////////////////////////////////////////////
355 // Default values for every type.
356 ////////////////////////////////////////////////////////////////////////
358 template<> inline bool SGRawValue<bool>::DefaultValue()
363 template<> inline const char * SGRawValue<const char *>::DefaultValue()
369 * A raw value bound to a pointer.
371 * This is the most efficient way to tie an external value, but also
372 * the most dangerous, because there is no way for the supplier to
373 * perform bounds checking and derived calculations except by polling
374 * the variable to see if it has changed. There is no default
375 * constructor, because this class would be meaningless without a
379 class SGRawValuePointer : public SGRawValue<T>
384 * Explicit pointer constructor.
386 * Create a new raw value bound to the value of the variable
387 * referenced by the pointer.
389 * @param ptr The pointer to the variable to which this raw value
392 SGRawValuePointer (T * ptr) : _ptr(ptr) {}
397 virtual ~SGRawValuePointer () {}
400 * Get the underlying value.
402 * This method will dereference the pointer and return the
405 virtual T getValue () const { return *_ptr; }
408 * Set the underlying value.
410 * This method will dereference the pointer and change the
413 virtual bool setValue (T value) { *_ptr = value; return true; }
416 * Create a copy of this raw value.
418 * The copy will use the same external pointer as the original.
420 virtual SGRaw* clone () const {
421 return new SGRawValuePointer(_ptr);
430 * A value managed through static functions.
432 * A read-only value will not have a setter; a write-only value will
436 class SGRawValueFunctions : public SGRawValue<T>
441 * The template type of a static getter function.
443 typedef T (*getter_t)();
446 * The template type of a static setter function.
448 typedef void (*setter_t)(T);
451 * Explicit constructor.
453 * Create a new raw value bound to the getter and setter supplied.
455 * @param getter A static function for getting a value, or 0
456 * to read-disable the value.
457 * @param setter A static function for setting a value, or 0
458 * to write-disable the value.
460 SGRawValueFunctions (getter_t getter = 0, setter_t setter = 0)
461 : _getter(getter), _setter(setter) {}
466 virtual ~SGRawValueFunctions () {}
469 * Get the underlying value.
471 * This method will invoke the getter function to get a value.
472 * If no getter function was supplied, this method will always
473 * return the default value for the type.
475 virtual T getValue () const {
476 if (_getter) return (*_getter)();
477 else return SGRawValue<T>::DefaultValue();
481 * Set the underlying value.
483 * This method will invoke the setter function to change the
484 * underlying value. If no setter function was supplied, this
485 * method will return false.
487 virtual bool setValue (T value) {
488 if (_setter) { (*_setter)(value); return true; }
493 * Create a copy of this raw value, bound to the same functions.
495 virtual SGRaw* clone () const {
496 return new SGRawValueFunctions(_getter,_setter);
506 * An indexed value bound to static functions.
508 * A read-only value will not have a setter; a write-only value will
509 * not have a getter. An indexed value is useful for binding one
510 * of a list of possible values (such as multiple engines for a
511 * plane). The index is hard-coded at creation time.
516 class SGRawValueFunctionsIndexed : public SGRawValue<T>
519 typedef T (*getter_t)(int);
520 typedef void (*setter_t)(int,T);
521 SGRawValueFunctionsIndexed (int index, getter_t getter = 0, setter_t setter = 0)
522 : _index(index), _getter(getter), _setter(setter) {}
523 virtual ~SGRawValueFunctionsIndexed () {}
524 virtual T getValue () const {
525 if (_getter) return (*_getter)(_index);
526 else return SGRawValue<T>::DefaultValue();
528 virtual bool setValue (T value) {
529 if (_setter) { (*_setter)(_index, value); return true; }
532 virtual SGRaw* clone () const {
533 return new SGRawValueFunctionsIndexed(_index, _getter, _setter);
543 * A value managed through an object and access methods.
545 * A read-only value will not have a setter; a write-only value will
548 template <class C, class T>
549 class SGRawValueMethods : public SGRawValue<T>
552 typedef T (C::*getter_t)() const;
553 typedef void (C::*setter_t)(T);
554 SGRawValueMethods (C &obj, getter_t getter = 0, setter_t setter = 0)
555 : _obj(obj), _getter(getter), _setter(setter) {}
556 virtual ~SGRawValueMethods () {}
557 virtual T getValue () const {
558 if (_getter) { return (_obj.*_getter)(); }
559 else { return SGRawValue<T>::DefaultValue(); }
561 virtual bool setValue (T value) {
562 if (_setter) { (_obj.*_setter)(value); return true; }
565 virtual SGRaw* clone () const {
566 return new SGRawValueMethods(_obj, _getter, _setter);
576 * An indexed value managed through an object and access methods.
578 * A read-only value will not have a setter; a write-only value will
581 template <class C, class T>
582 class SGRawValueMethodsIndexed : public SGRawValue<T>
585 typedef T (C::*getter_t)(int) const;
586 typedef void (C::*setter_t)(int, T);
587 SGRawValueMethodsIndexed (C &obj, int index,
588 getter_t getter = 0, setter_t setter = 0)
589 : _obj(obj), _index(index), _getter(getter), _setter(setter) {}
590 virtual ~SGRawValueMethodsIndexed () {}
591 virtual T getValue () const {
592 if (_getter) { return (_obj.*_getter)(_index); }
593 else { return SGRawValue<T>::DefaultValue(); }
595 virtual bool setValue (T value) {
596 if (_setter) { (_obj.*_setter)(_index, value); return true; }
599 virtual SGRaw* clone () const {
600 return new SGRawValueMethodsIndexed(_obj, _index, _getter, _setter);
610 * A raw value that contains its value. This provides a way for
611 * property nodes to contain values that shouldn't be stored in the
612 * property node itself.
615 class SGRawValueContainer : public SGRawValue<T>
620 * Explicit constructor.
622 SGRawValueContainer(const T& obj) : _obj(obj) {}
627 virtual ~SGRawValueContainer() {}
630 * Get the underlying value.
632 virtual T getValue() const { return _obj; }
635 * Set the underlying value.
637 * This method will dereference the pointer and change the
640 virtual bool setValue (T value) { _obj = value; return true; }
643 * Create a copy of this raw value.
645 virtual SGRaw* clone () const {
646 return new SGRawValueContainer(_obj);
654 SGRawExtended* SGRawBase<T, 0>::makeContainer() const
656 return new SGRawValueContainer<T>(static_cast<const SGRawValue<T>*>(this)
661 std::ostream& SGRawBase<T, 0>::printOn(std::ostream& stream) const
663 return stream << static_cast<SGRawValue<T>*>(this)->getValue();
667 std::istream& SGRawBase<T, 0>::readFrom(std::istream& stream)
670 simgear::readFrom(stream, value);
671 static_cast<SGRawValue<T>*>(this)->setValue(value);
676 * The smart pointer that manage reference counting
678 class SGPropertyNode;
679 typedef SGSharedPtr<SGPropertyNode> SGPropertyNode_ptr;
680 typedef SGSharedPtr<const SGPropertyNode> SGConstPropertyNode_ptr;
684 typedef std::vector<SGPropertyNode_ptr> PropertyList;
688 * The property change listener interface.
690 * <p>Any class that needs to listen for property changes must implement
691 * this interface.</p>
693 class SGPropertyChangeListener
696 virtual ~SGPropertyChangeListener ();
697 virtual void valueChanged (SGPropertyNode * node);
698 virtual void childAdded (SGPropertyNode * parent, SGPropertyNode * child);
699 virtual void childRemoved (SGPropertyNode * parent, SGPropertyNode * child);
702 friend class SGPropertyNode;
703 virtual void register_property (SGPropertyNode * node);
704 virtual void unregister_property (SGPropertyNode * node);
707 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 * Get a list of values from all children with the given name
1172 template<typename T, typename T_get /* = T */> // TODO use C++11 or traits
1173 std::vector<T> getChildValues(const std::string& name) const;
1176 * Get a list of values from all children with the given name
1178 template<typename T>
1179 std::vector<T> getChildValues(const std::string& name) const;
1182 * Set a bool value for this node.
1184 bool setBoolValue (bool value);
1188 * Set an int value for this node.
1190 bool setIntValue (int value);
1194 * Set a long int value for this node.
1196 bool setLongValue (long value);
1200 * Set a float value for this node.
1202 bool setFloatValue (float value);
1206 * Set a double value for this node.
1208 bool setDoubleValue (double value);
1212 * Set a string value for this node.
1214 bool setStringValue (const char * value);
1217 * Set a string value for this node.
1219 bool setStringValue (const std::string& value)
1220 { return setStringValue(value.c_str()); }
1224 * Set a value of unspecified type for this node.
1226 bool setUnspecifiedValue (const char * value);
1228 template<typename T>
1229 bool setValue(const T& val,
1230 typename boost::enable_if_c<simgear::props::PropertyTraits<T>::Internal>
1233 template<typename T>
1234 bool setValue(const T& val,
1235 typename boost::disable_if_c<simgear::props::PropertyTraits<T>::Internal>
1239 bool setValue(const char (&val)[N])
1241 return setValue(&val[0]);
1245 * Interpolate current value to target value within given time.
1247 * @param type Type of interpolation ("numeric", "color", etc.)
1248 * @param target Node containing target value
1249 * @param duration Duration of interpolation (in seconds)
1250 * @param easing Easing function (http://easings.net/)
1252 bool interpolate( const std::string& type,
1253 const SGPropertyNode& target,
1254 double duration = 0.6,
1255 const std::string& easing = "swing" );
1258 * Interpolate current value to a series of values within given durations.
1260 * @param type Type of interpolation ("numeric", "color", etc.)
1261 * @param values Nodes containing intermediate and target values
1262 * @param duration Durations for each interpolation step (in seconds)
1263 * @param easing Easing function (http://easings.net/)
1265 bool interpolate( const std::string& type,
1266 const simgear::PropertyList& values,
1267 const double_list& deltas,
1268 const std::string& easing = "swing" );
1271 * Set the interpolation manager used by the interpolate methods.
1273 static void setInterpolationMgr(simgear::PropertyInterpolationMgr* mgr);
1276 * Print the value of the property to a stream.
1278 std::ostream& printOn(std::ostream& stream) const;
1286 * Test whether this node is bound to an external data source.
1288 bool isTied () const { return _tied; }
1291 * Bind this node to an external source.
1293 template<typename T>
1294 bool tie(const SGRawValue<T> &rawValue, bool useDefault = true);
1297 * Unbind this node from any external data source.
1303 // Convenience methods using paths.
1304 // TODO: add attribute methods
1309 * Get another node's type.
1311 simgear::props::Type getType (const char * relative_path) const;
1314 * Get another node's type.
1316 simgear::props::Type getType (const std::string& relative_path) const
1317 { return getType(relative_path.c_str()); }
1320 * Test whether another node has a leaf value.
1322 bool hasValue (const char * relative_path) const;
1325 * Test whether another node has a leaf value.
1327 bool hasValue (const std::string& relative_path) const
1328 { return hasValue(relative_path.c_str()); }
1331 * Get another node's value as a bool.
1333 bool getBoolValue (const char * relative_path,
1334 bool defaultValue = false) const;
1337 * Get another node's value as a bool.
1339 bool getBoolValue (const std::string& relative_path,
1340 bool defaultValue = false) const
1341 { return getBoolValue(relative_path.c_str(), defaultValue); }
1344 * Get another node's value as an int.
1346 int getIntValue (const char * relative_path,
1347 int defaultValue = 0) const;
1350 * Get another node's value as an int.
1352 int getIntValue (const std::string& relative_path,
1353 int defaultValue = 0) const
1354 { return getIntValue(relative_path.c_str(), defaultValue); }
1358 * Get another node's value as a long int.
1360 long getLongValue (const char * relative_path,
1361 long defaultValue = 0L) const;
1364 * Get another node's value as a long int.
1366 long getLongValue (const std::string& relative_path,
1367 long defaultValue = 0L) const
1368 { return getLongValue(relative_path.c_str(), defaultValue); }
1371 * Get another node's value as a float.
1373 float getFloatValue (const char * relative_path,
1374 float defaultValue = 0.0f) const;
1377 * Get another node's value as a float.
1379 float getFloatValue (const std::string& relative_path,
1380 float defaultValue = 0.0f) const
1381 { return getFloatValue(relative_path.c_str(), defaultValue); }
1385 * Get another node's value as a double.
1387 double getDoubleValue (const char * relative_path,
1388 double defaultValue = 0.0) const;
1391 * Get another node's value as a double.
1393 double getDoubleValue (const std::string& relative_path,
1394 double defaultValue = 0.0) const
1395 { return getDoubleValue(relative_path.c_str(), defaultValue); }
1398 * Get another node's value as a string.
1400 const char * getStringValue (const char * relative_path,
1401 const char * defaultValue = "") const;
1405 * Get another node's value as a string.
1407 const char * getStringValue (const std::string& relative_path,
1408 const char * defaultValue = "") const
1409 { return getStringValue(relative_path.c_str(), defaultValue); }
1413 * Set another node's value as a bool.
1415 bool setBoolValue (const char * relative_path, bool value);
1418 * Set another node's value as a bool.
1420 bool setBoolValue (const std::string& relative_path, bool value)
1421 { return setBoolValue(relative_path.c_str(), value); }
1425 * Set another node's value as an int.
1427 bool setIntValue (const char * relative_path, int value);
1430 * Set another node's value as an int.
1432 bool setIntValue (const std::string& relative_path, int value)
1433 { return setIntValue(relative_path.c_str(), value); }
1437 * Set another node's value as a long int.
1439 bool setLongValue (const char * relative_path, long value);
1442 * Set another node's value as a long int.
1444 bool setLongValue (const std::string& relative_path, long value)
1445 { return setLongValue(relative_path.c_str(), value); }
1449 * Set another node's value as a float.
1451 bool setFloatValue (const char * relative_path, float value);
1454 * Set another node's value as a float.
1456 bool setFloatValue (const std::string& relative_path, float value)
1457 { return setFloatValue(relative_path.c_str(), value); }
1461 * Set another node's value as a double.
1463 bool setDoubleValue (const char * relative_path, double value);
1466 * Set another node's value as a double.
1468 bool setDoubleValue (const std::string& relative_path, double value)
1469 { return setDoubleValue(relative_path.c_str(), value); }
1473 * Set another node's value as a string.
1475 bool setStringValue (const char * relative_path, const char * value);
1477 bool setStringValue(const char * relative_path, const std::string& value)
1478 { return setStringValue(relative_path, value.c_str()); }
1480 * Set another node's value as a string.
1482 bool setStringValue (const std::string& relative_path, const char * value)
1483 { return setStringValue(relative_path.c_str(), value); }
1485 bool setStringValue (const std::string& relative_path,
1486 const std::string& value)
1487 { return setStringValue(relative_path.c_str(), value.c_str()); }
1490 * Set another node's value with no specified type.
1492 bool setUnspecifiedValue (const char * relative_path, const char * value);
1496 * Test whether another node is bound to an external data source.
1498 bool isTied (const char * relative_path) const;
1501 * Test whether another node is bound to an external data source.
1503 bool isTied (const std::string& relative_path) const
1504 { return isTied(relative_path.c_str()); }
1507 * Bind another node to an external bool source.
1509 bool tie (const char * relative_path, const SGRawValue<bool> &rawValue,
1510 bool useDefault = true);
1513 * Bind another node to an external bool source.
1515 bool tie (const std::string& relative_path, const SGRawValue<bool> &rawValue,
1516 bool useDefault = true)
1517 { return tie(relative_path.c_str(), rawValue, useDefault); }
1521 * Bind another node to an external int source.
1523 bool tie (const char * relative_path, const SGRawValue<int> &rawValue,
1524 bool useDefault = true);
1527 * Bind another node to an external int source.
1529 bool tie (const std::string& relative_path, const SGRawValue<int> &rawValue,
1530 bool useDefault = true)
1531 { return tie(relative_path.c_str(), rawValue, useDefault); }
1535 * Bind another node to an external long int source.
1537 bool tie (const char * relative_path, const SGRawValue<long> &rawValue,
1538 bool useDefault = true);
1541 * Bind another node to an external long int source.
1543 bool tie (const std::string& relative_path, const SGRawValue<long> &rawValue,
1544 bool useDefault = true)
1545 { return tie(relative_path.c_str(), rawValue, useDefault); }
1549 * Bind another node to an external float source.
1551 bool tie (const char * relative_path, const SGRawValue<float> &rawValue,
1552 bool useDefault = true);
1555 * Bind another node to an external float source.
1557 bool tie (const std::string& relative_path, const SGRawValue<float> &rawValue,
1558 bool useDefault = true)
1559 { return tie(relative_path.c_str(), rawValue, useDefault); }
1563 * Bind another node to an external double source.
1565 bool tie (const char * relative_path, const SGRawValue<double> &rawValue,
1566 bool useDefault = true);
1569 * Bind another node to an external double source.
1571 bool tie (const std::string& relative_path, const SGRawValue<double> &rawValue,
1572 bool useDefault = true)
1573 { return tie(relative_path.c_str(), rawValue, useDefault); }
1577 * Bind another node to an external string source.
1579 bool tie (const char * relative_path, const SGRawValue<const char *> &rawValue,
1580 bool useDefault = true);
1583 * Bind another node to an external string source.
1585 bool tie (const std::string& relative_path, const SGRawValue<const char*> &rawValue,
1586 bool useDefault = true)
1587 { return tie(relative_path.c_str(), rawValue, useDefault); }
1591 * Unbind another node from any external data source.
1593 bool untie (const char * relative_path);
1596 * Unbind another node from any external data source.
1598 bool untie (const std::string& relative_path)
1599 { return untie(relative_path.c_str()); }
1603 * Add a change listener to the property. If "initial" is set call the
1604 * listener initially.
1606 void addChangeListener (SGPropertyChangeListener * listener,
1607 bool initial = false);
1611 * Remove a change listener from the property.
1613 void removeChangeListener (SGPropertyChangeListener * listener);
1617 * Get the number of listeners.
1619 int nListeners () const { return _listeners ? (int)_listeners->size() : 0; }
1623 * Fire a value change event to all listeners.
1625 void fireValueChanged ();
1629 * Fire a child-added event to all listeners.
1631 void fireChildAdded (SGPropertyNode * child);
1634 * Trigger a child-added and value-changed event for every child (Unlimited
1637 * @param fire_self Whether to trigger the events also for the node itself.
1639 * It can be used to simulating the creation of a property tree, eg. for
1640 * (re)initializing a subsystem which is controlled through the property tree.
1642 void fireCreatedRecursive(bool fire_self = false);
1645 * Fire a child-removed event to all listeners.
1647 void fireChildRemoved (SGPropertyNode * child);
1650 * Fire a child-removed event for every child of this node (Unlimited depth)
1652 * Upon removal of a child node only for this single node a child-removed
1653 * event is triggered. If eg. resource cleanup relies on receiving a
1654 * child-removed event for every child this method can be used.
1656 void fireChildrenRemovedRecursive();
1660 * Clear any existing value and set the type to NONE.
1665 * Compare two property trees. The property trees are equal if: 1)
1666 * They have no children, and have the same type and the values are
1667 * equal, or 2) have the same number of children, and the
1668 * corresponding children in each tree are equal. "corresponding"
1669 * means have the same name and index.
1671 * Attributes, removed children, and aliases aren't considered.
1673 static bool compare (const SGPropertyNode& lhs, const SGPropertyNode& rhs);
1677 void fireValueChanged (SGPropertyNode * node);
1678 void fireChildAdded (SGPropertyNode * parent, SGPropertyNode * child);
1679 void fireChildRemoved (SGPropertyNode * parent, SGPropertyNode * child);
1682 * Protected constructor for making new nodes on demand.
1684 SGPropertyNode (const std::string& name, int index, SGPropertyNode * parent);
1685 template<typename Itr>
1686 SGPropertyNode (Itr begin, Itr end, int index, SGPropertyNode * parent);
1688 static simgear::PropertyInterpolationMgr* _interpolation_mgr;
1692 // Get the raw value
1693 bool get_bool () const;
1694 int get_int () const;
1695 long get_long () const;
1696 float get_float () const;
1697 double get_double () const;
1698 const char * get_string () const;
1700 // Set the raw value
1701 bool set_bool (bool value);
1702 bool set_int (int value);
1703 bool set_long (long value);
1704 bool set_float (float value);
1705 bool set_double (double value);
1706 bool set_string (const char * value);
1710 * Get the value as a string.
1712 const char * make_string () const;
1715 * Trace a read access.
1717 void trace_read () const;
1721 * Trace a write access.
1723 void trace_write () const;
1727 /// To avoid cyclic reference counting loops this shall not be a reference
1729 SGPropertyNode * _parent;
1730 simgear::PropertyList _children;
1731 simgear::PropertyList _removedChildren;
1732 mutable std::string _buffer;
1733 simgear::props::Type _type;
1737 // The right kind of pointer...
1739 SGPropertyNode * alias;
1752 std::vector<SGPropertyChangeListener *> * _listeners;
1754 // Pass name as a pair of iterators
1755 template<typename Itr>
1756 SGPropertyNode * getChildImpl (Itr begin, Itr end, int index = 0, bool create = false);
1757 // very internal method
1758 template<typename Itr>
1759 SGPropertyNode* getExistingChild (Itr begin, Itr end, int index, bool create);
1760 // very internal path parsing function
1761 template<typename SplitItr>
1762 friend SGPropertyNode* find_node_aux(SGPropertyNode * current, SplitItr& itr,
1763 bool create, int last_index);
1765 friend size_t hash_value(const SGPropertyNode& node);
1768 // Convenience functions for use in templates
1769 template<typename T>
1770 T getValue(const SGPropertyNode*);
1773 inline bool getValue<bool>(const SGPropertyNode* node) { return node->getBoolValue(); }
1776 inline int getValue<int>(const SGPropertyNode* node) { return node->getIntValue(); }
1779 inline long getValue<long>(const SGPropertyNode* node) { return node->getLongValue(); }
1782 inline float getValue<float>(const SGPropertyNode* node)
1784 return node->getFloatValue();
1788 inline double getValue<double>(const SGPropertyNode* node)
1790 return node->getDoubleValue();
1794 inline const char * getValue<const char*>(const SGPropertyNode* node)
1796 return node->getStringValue ();
1800 inline std::string getValue<std::string>(const SGPropertyNode* node)
1802 return node->getStringValue();
1805 inline bool setValue(SGPropertyNode* node, bool value)
1807 return node->setBoolValue(value);
1810 inline bool setValue(SGPropertyNode* node, int value)
1812 return node->setIntValue(value);
1815 inline bool setValue(SGPropertyNode* node, long value)
1817 return node->setLongValue(value);
1820 inline bool setValue(SGPropertyNode* node, float value)
1822 return node->setFloatValue(value);
1825 inline bool setValue(SGPropertyNode* node, double value)
1827 return node->setDoubleValue(value);
1830 inline bool setValue(SGPropertyNode* node, const char* value)
1832 return node->setStringValue(value);
1835 inline bool setValue (SGPropertyNode* node, const std::string& value)
1837 return node->setStringValue(value.c_str());
1840 template<typename T>
1841 bool SGPropertyNode::tie(const SGRawValue<T> &rawValue, bool useDefault)
1843 using namespace simgear::props;
1844 if (_type == ALIAS || _tied)
1847 useDefault = useDefault && hasValue();
1848 T old_val = SGRawValue<T>::DefaultValue();
1850 old_val = getValue<T>(this);
1852 if (PropertyTraits<T>::Internal)
1853 _type = PropertyTraits<T>::type_tag;
1857 _value.val = rawValue.clone();
1859 int save_attributes = getAttributes();
1860 setAttribute( WRITE, true );
1862 setAttributes( save_attributes );
1868 bool SGPropertyNode::tie (const SGRawValue<const char *> &rawValue,
1871 template<typename T>
1872 T SGPropertyNode::getValue(typename boost::disable_if_c<simgear::props
1873 ::PropertyTraits<T>::Internal>::type* dummy) const
1875 using namespace simgear::props;
1876 if (_attr == (READ|WRITE) && _type == EXTENDED
1877 && _value.val->getType() == PropertyTraits<T>::type_tag) {
1878 return static_cast<SGRawValue<T>*>(_value.val)->getValue();
1880 if (getAttribute(TRACE_READ))
1882 if (!getAttribute(READ))
1883 return SGRawValue<T>::DefaultValue();
1886 if (_value.val->getType() == PropertyTraits<T>::type_tag)
1887 return static_cast<SGRawValue<T>*>(_value.val)->getValue();
1891 return simgear::parseString<T>(make_string());
1893 default: // avoid compiler warning
1896 return SGRawValue<T>::DefaultValue();
1899 template<typename T>
1900 inline T SGPropertyNode::getValue(typename boost::enable_if_c<simgear::props
1901 ::PropertyTraits<T>::Internal>::type* dummy) const
1903 return ::getValue<T>(this);
1906 template<typename T, typename T_get /* = T */> // TODO use C++11 or traits
1907 std::vector<T> SGPropertyNode::getChildValues(const std::string& name) const
1909 const simgear::PropertyList& props = getChildren(name);
1910 std::vector<T> values( props.size() );
1912 for( size_t i = 0; i < props.size(); ++i )
1913 values[i] = props[i]->getValue<T_get>();
1918 template<typename T>
1920 std::vector<T> SGPropertyNode::getChildValues(const std::string& name) const
1922 return getChildValues<T, T>(name);
1925 template<typename T>
1926 bool SGPropertyNode::setValue(const T& val,
1927 typename boost::disable_if_c<simgear::props
1928 ::PropertyTraits<T>::Internal>::type* dummy)
1930 using namespace simgear::props;
1931 if (_attr == (READ|WRITE) && _type == EXTENDED
1932 && _value.val->getType() == PropertyTraits<T>::type_tag) {
1933 static_cast<SGRawValue<T>*>(_value.val)->setValue(val);
1936 if (getAttribute(WRITE)
1937 && ((_type == EXTENDED
1938 && _value.val->getType() == PropertyTraits<T>::type_tag)
1939 || _type == NONE || _type == UNSPECIFIED)) {
1940 if (_type == NONE || _type == UNSPECIFIED) {
1943 _value.val = new SGRawValueContainer<T>(val);
1945 static_cast<SGRawValue<T>*>(_value.val)->setValue(val);
1947 if (getAttribute(TRACE_WRITE))
1954 template<typename T>
1955 inline bool SGPropertyNode::setValue(const T& val,
1956 typename boost::enable_if_c<simgear::props
1957 ::PropertyTraits<T>::Internal>::type* dummy)
1959 return ::setValue(this, val);
1963 * Utility function for creation of a child property node.
1965 inline SGPropertyNode* makeChild(SGPropertyNode* parent, const char* name,
1968 return parent->getChild(name, index, true);
1972 * Utility function for creation of a child property node using a
1977 template<typename StringType>
1978 inline SGPropertyNode* makeNode(SGPropertyNode* parent, const StringType& name)
1980 return parent->getNode(name, true);
1985 size_t hash_value(const SGPropertyNode& node);
1987 // Helper comparison and hash functions for common cases
1995 bool operator()(const SGPropertyNode* lhs, const SGPropertyNode* rhs) const
1997 return SGPropertyNode::compare(*lhs, *rhs);
1999 bool operator()(SGPropertyNode_ptr lhs, const SGPropertyNode* rhs) const
2001 return SGPropertyNode::compare(*lhs, *rhs);
2003 bool operator()(const SGPropertyNode* lhs, SGPropertyNode_ptr rhs) const
2005 return SGPropertyNode::compare(*lhs, *rhs);
2007 bool operator()(SGPropertyNode_ptr lhs, SGPropertyNode_ptr rhs) const
2009 return SGPropertyNode::compare(*lhs, *rhs);
2015 size_t operator()(const SGPropertyNode* node) const
2017 return hash_value(*node);
2019 size_t operator()(SGPropertyNode_ptr node) const
2021 return hash_value(*node);
2027 /** Convenience class for change listener callbacks without
2028 * creating a derived class implementing a "valueChanged" method.
2029 * Also removes listener on destruction automatically.
2032 class SGPropertyChangeCallback
2033 : public SGPropertyChangeListener
2036 SGPropertyChangeCallback(T* obj, void (T::*method)(SGPropertyNode*),
2037 SGPropertyNode_ptr property,bool initial=false)
2038 : _obj(obj), _callback(method), _property(property)
2040 _property->addChangeListener(this,initial);
2042 virtual ~SGPropertyChangeCallback()
2044 _property->removeChangeListener(this);
2046 void valueChanged (SGPropertyNode * node)
2048 (_obj->*_callback)(node);
2052 void (T::*_callback)(SGPropertyNode*);
2053 SGPropertyNode_ptr _property;
2056 #endif // __PROPS_HXX