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
25 #include <boost/utility.hpp>
26 #include <boost/type_traits/is_enum.hpp>
30 #include <simgear/compiler.h>
31 #include <simgear/debug/logstream.hxx>
35 #include <simgear/math/SGMathFwd.hxx>
36 #include <simgear/math/sg_types.hxx>
37 #include <simgear/structure/SGReferenced.hxx>
38 #include <simgear/structure/SGSharedPtr.hxx>
40 // XXX This whole file should be in the simgear namespace, but I don't
41 // have the guts yet...
46 class PropertyInterpolationMgr;
49 std::istream& readFrom(std::istream& stream, T& result)
56 * Parse a string as an object of a given type.
57 * XXX no error behavior yet.
59 * @tparam T the return type
60 * @param str the string
64 inline T parseString(const std::string& str)
66 std::istringstream stream(str);
68 readFrom(stream, result);
73 * Property value types.
77 #pragma warn A sloppy coder has defined NONE as a macro!
82 #pragma warn A sloppy coder has defined ALIAS as a macro!
87 #pragma warn A sloppy coder has defined UNSPECIFIED as a macro!
92 #pragma warn A sloppy coder has defined BOOL as a macro!
97 #pragma warn A sloppy coder has defined INT as a macro!
102 #pragma warn A sloppy coder has defined LONG as a macro!
107 #pragma warn A sloppy coder has defined FLOAT as a macro!
112 #pragma warn A sloppy coder has defined DOUBLE as a macro!
117 #pragma warn A sloppy coder has defined STRING as a macro!
124 * The possible types of an SGPropertyNode. Types that appear after
125 * EXTENDED are not stored in the SGPropertyNode itself.
128 NONE = 0, /**< The node hasn't been assigned a value yet. */
129 ALIAS, /**< The node "points" to another node. */
137 EXTENDED, /**< The node's value is not stored in the property;
138 * the actual value and type is retrieved from an
139 * SGRawValue node. This type is never returned by @see
140 * SGPropertyNode::getType.
142 // Extended properties
147 template<typename T> struct PropertyTraits;
149 #define DEFINTERNALPROP(TYPE, PROP) \
151 struct PropertyTraits<TYPE> \
153 static const Type type_tag = PROP; \
154 enum { Internal = 1 }; \
157 DEFINTERNALPROP(bool, BOOL);
158 DEFINTERNALPROP(int, INT);
159 DEFINTERNALPROP(long, LONG);
160 DEFINTERNALPROP(float, FLOAT);
161 DEFINTERNALPROP(double, DOUBLE);
162 DEFINTERNALPROP(const char *, STRING);
163 DEFINTERNALPROP(const char[], STRING);
164 #undef DEFINTERNALPROP
171 ////////////////////////////////////////////////////////////////////////
174 // This is the mechanism that information-providing routines can
175 // use to link their own values to the property manager. Any
176 // SGValue can be tied to a raw value and then untied again.
178 // Note: we are forced to use inlined methods here to ensure
179 // that the templates will be instantiated. We're probably taking
180 // a small performance hit for that.
181 ////////////////////////////////////////////////////////////////////////
184 * Base class for SGRawValue classes that holds no type
185 * information. This allows some generic manipulation of the
192 * Get the type enumeration for the raw value.
196 virtual simgear::props::Type getType() const = 0;
200 * Create a new deep copy of this raw value.
202 * The copy will contain its own version of the underlying value
203 * as well, and will be the same type.
205 * @return A deep copy of the current object.
207 virtual SGRaw* clone() const = 0;
211 class SGRawExtended : public SGRaw
215 * Make an SGRawValueContainer from the SGRawValue.
217 * This is a virtual function of SGRawExtended so that
218 * SGPropertyNode::untie doesn't need to know the type of an
221 virtual SGRawExtended* makeContainer() const = 0;
223 * Write value out to a stream
225 virtual std::ostream& printOn(std::ostream& stream) const = 0;
227 * Read value from a stream and store it.
229 virtual std::istream& readFrom(std::istream& stream) = 0;
232 // Choose between different base classes based on whether the value is
233 // stored internal to the property node. This frees us from defining
234 // the virtual functions in the SGRawExtended interface where they
235 // don't make sense, e.g. readFrom for the const char* type.
236 template<typename T, int internal = simgear::props::PropertyTraits<T>::Internal>
240 class SGRawBase<T, 1> : public SGRaw
245 class SGRawBase<T, 0> : public SGRawExtended
247 virtual SGRawExtended* makeContainer() const;
248 virtual std::ostream& printOn(std::ostream& stream) const;
249 virtual std::istream& readFrom(std::istream& stream);
253 * Abstract base class for a raw value.
255 * The property manager is implemented in two layers. The SGPropertyNode is the
256 * highest and most abstract layer, representing an LValue/RValue pair: it
257 * records the position of the property in the property tree and contains
258 * facilities for navigation to other nodes. It is guaranteed to be persistent:
259 * the SGPropertyNode will not change during a session, even if the property is
260 * bound and unbound multiple times.
262 * When the property value is not managed internally in the
263 * SGPropertyNode, the SGPropertyNode will contain a reference to an
264 * SGRawValue (this class), which provides an abstract way to get,
265 * set, and clone the underlying value. The SGRawValue may change
266 * frequently during a session as a value is retyped or bound and
267 * unbound to various data source, but the abstract SGPropertyNode
268 * layer insulates the application from those changes.
270 * The SGPropertyNode class always keeps a *copy* of a raw value, not the
271 * original one passed to it; if you override a derived class but do not replace
272 * the {@link SGRaw::clone clone()} method, strange things will happen.
274 * All derived SGRawValue classes must implement getValue(), setValue(), and
275 * {@link SGRaw::clone clone()} for the appropriate type.
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 ();
698 /// Called if value of \a node has changed.
699 virtual void valueChanged(SGPropertyNode * node);
701 /// Called if \a child has been added to the given \a parent.
702 virtual void childAdded(SGPropertyNode * parent, SGPropertyNode * child);
704 /// Called if \a child has been removed from its \a parent.
705 virtual void childRemoved(SGPropertyNode * parent, SGPropertyNode * child);
708 friend class SGPropertyNode;
709 virtual void register_property (SGPropertyNode * node);
710 virtual void unregister_property (SGPropertyNode * node);
713 std::vector<SGPropertyNode *> _properties;
718 * A node in a property tree.
720 class SGPropertyNode : public SGReferenced
728 MAX_STRING_LEN = 1024
732 * Access mode attributes.
734 * <p>The ARCHIVE attribute is strictly advisory, and controls
735 * whether the property should normally be saved and restored.</p>
747 // beware: if you add another attribute here,
748 // also update value of "LAST_USED_ATTRIBUTE".
753 * Last used attribute
754 * Update as needed when enum Attribute is changed
756 static const int LAST_USED_ATTRIBUTE;
759 * Default constructor.
767 SGPropertyNode (const SGPropertyNode &node);
773 virtual ~SGPropertyNode ();
782 * Test whether this node contains a primitive leaf value.
784 bool hasValue () const { return (_type != simgear::props::NONE); }
788 * Get the node's simple (XML) name.
790 const char * getName () const { return _name.c_str(); }
793 * Get the node's simple name as a string.
795 const std::string& getNameString () const { return _name; }
798 * Get the node's pretty display name, with subscript when needed.
800 std::string getDisplayName (bool simplify = false) const;
804 * Get the node's integer index.
806 int getIndex () const { return _index; }
810 * Get a non-const pointer to the node's parent.
812 SGPropertyNode * getParent () { return _parent; }
816 * Get a const pointer to the node's parent.
818 const SGPropertyNode * getParent () const { return _parent; }
827 * Get the number of child nodes.
829 int nChildren () const { return (int)_children.size(); }
833 * Get a child node by position (*NOT* index).
835 SGPropertyNode * getChild (int position);
839 * Get a const child node by position (*NOT* index).
841 const SGPropertyNode * getChild (int position) const;
845 * Test whether a named child exists.
847 bool hasChild (const char * name, int index = 0) const
849 return (getChild(name, index) != 0);
853 * Test whether a named child exists.
855 bool hasChild (const std::string& name, int index = 0) const
857 return (getChild(name, index) != 0);
861 * Create a new child node with the given name and an unused index
863 * @param min_index Minimal index for new node (skips lower indices)
864 * @param append Whether to simply use the index after the last used index
865 * or use a lower, unused index if it exists
867 SGPropertyNode * addChild ( const char* name,
869 bool append = true );
870 SGPropertyNode * addChild ( const std::string& name,
873 { return addChild(name.c_str(), min_index, append); }
876 * Create multiple child nodes with the given name an unused indices
878 * @param count The number of nodes create
879 * @param min_index Minimal index for new nodes (skips lower indices)
880 * @param append Whether to simply use the index after the last used index
881 * or use a lower, unused index if it exists
883 simgear::PropertyList addChildren ( const std::string& name,
886 bool append = true );
889 * Get a child node by name and index.
891 SGPropertyNode * getChild (const char* name, int index = 0,
892 bool create = false);
893 SGPropertyNode * getChild (const std::string& name, int index = 0,
894 bool create = false);
896 * Get a const child node by name and index.
898 const SGPropertyNode * getChild (const char * name, int index = 0) const;
901 * Get a const child node by name and index.
903 const SGPropertyNode * getChild (const std::string& name, int index = 0) const
904 { return getChild(name.c_str(), index); }
908 * Get a vector of all children with the specified name.
910 simgear::PropertyList getChildren (const char * name) const;
913 * Get a vector of all children with the specified name.
915 simgear::PropertyList getChildren (const std::string& name) const
916 { return getChildren(name.c_str()); }
919 * Remove child by pointer (if it is a child of this node).
921 * @return true, if the node was deleted.
923 bool removeChild(SGPropertyNode* node);
925 // TODO do we need the removeXXX methods to return the deleted nodes?
927 * Remove child by position.
929 SGPropertyNode_ptr removeChild(int pos);
933 * Remove a child node
935 SGPropertyNode_ptr removeChild(const char * name, int index = 0);
938 * Remove a child node
940 SGPropertyNode_ptr removeChild(const std::string& name, int index = 0)
941 { return removeChild(name.c_str(), index); }
944 * Remove all children with the specified name.
946 simgear::PropertyList removeChildren(const char * name);
949 * Remove all children with the specified name.
951 simgear::PropertyList removeChildren(const std::string& name)
952 { return removeChildren(name.c_str()); }
955 * Remove all children (does not change the value of the node)
957 void removeAllChildren();
965 * Alias this node's leaf value to another's.
967 bool alias (SGPropertyNode * target);
971 * Alias this node's leaf value to another's by relative path.
973 bool alias (const char * path);
976 * Alias this node's leaf value to another's by relative path.
978 bool alias (const std::string& path)
979 { return alias(path.c_str()); }
983 * Remove any alias for this node.
989 * Test whether the node's leaf value is aliased to another's.
991 bool isAlias () const { return (_type == simgear::props::ALIAS); }
995 * Get a non-const pointer to the current alias target, if any.
997 SGPropertyNode * getAliasTarget ();
1001 * Get a const pointer to the current alias target, if any.
1003 const SGPropertyNode * getAliasTarget () const;
1007 // Path information.
1012 * Get the path to this node from the root.
1014 std::string getPath (bool simplify = false) const;
1018 * Get a pointer to the root node.
1020 SGPropertyNode * getRootNode ();
1024 * Get a const pointer to the root node.
1026 const SGPropertyNode * getRootNode () const;
1030 * Get a pointer to another node by relative path.
1032 SGPropertyNode * getNode (const char * relative_path, bool create = false);
1035 * Get a pointer to another node by relative path.
1037 SGPropertyNode * getNode (const std::string& relative_path, bool create = false)
1038 { return getNode(relative_path.c_str(), create); }
1041 * Get a pointer to another node by relative path.
1043 * This method leaves the index off the last member of the path,
1044 * so that the user can specify it separately (and save some
1045 * string building). For example, getNode("/bar[1]/foo", 3) is
1046 * exactly equivalent to getNode("bar[1]/foo[3]"). The index
1047 * provided overrides any given in the path itself for the last
1050 SGPropertyNode * getNode (const char * relative_path, int index,
1051 bool create = false);
1054 * Get a pointer to another node by relative path.
1056 * This method leaves the index off the last member of the path,
1057 * so that the user can specify it separately (and save some
1058 * string building). For example, getNode("/bar[1]/foo", 3) is
1059 * exactly equivalent to getNode("bar[1]/foo[3]"). The index
1060 * provided overrides any given in the path itself for the last
1063 SGPropertyNode * getNode (const std::string& relative_path, int index,
1064 bool create = false)
1065 { return getNode(relative_path.c_str(), index, create); }
1068 * Get a const pointer to another node by relative path.
1070 const SGPropertyNode * getNode (const char * relative_path) const;
1073 * Get a const pointer to another node by relative path.
1075 const SGPropertyNode * getNode (const std::string& relative_path) const
1076 { return getNode(relative_path.c_str()); }
1080 * Get a const pointer to another node by relative path.
1082 * This method leaves the index off the last member of the path,
1083 * so that the user can specify it separate.
1085 const SGPropertyNode * getNode (const char * relative_path,
1089 * Get a const pointer to another node by relative path.
1091 * This method leaves the index off the last member of the path,
1092 * so that the user can specify it separate.
1094 const SGPropertyNode * getNode (const std::string& relative_path,
1096 { return getNode(relative_path.c_str(), index); }
1103 * Check a single mode attribute for the property node.
1105 bool getAttribute (Attribute attr) const { return ((_attr & attr) != 0); }
1109 * Set a single mode attribute for the property node.
1111 void setAttribute (Attribute attr, bool state) {
1112 (state ? _attr |= attr : _attr &= ~attr);
1117 * Get all of the mode attributes for the property node.
1119 int getAttributes () const { return _attr; }
1123 * Set all of the mode attributes for the property node.
1125 void setAttributes (int attr) { _attr = attr; }
1129 // Leaf Value (primitive).
1134 * Get the type of leaf value, if any, for this node.
1136 simgear::props::Type getType () const;
1140 * Get a bool value for this node.
1142 bool getBoolValue () const;
1146 * Get an int value for this node.
1148 int getIntValue () const;
1152 * Get a long int value for this node.
1154 long getLongValue () const;
1158 * Get a float value for this node.
1160 float getFloatValue () const;
1164 * Get a double value for this node.
1166 double getDoubleValue () const;
1170 * Get a string value for this node.
1172 const char * getStringValue () const;
1175 * Get a value from a node. If the actual type of the node doesn't
1176 * match the desired type, a conversion isn't guaranteed.
1178 template<typename T>
1179 T getValue(typename boost::enable_if_c<simgear::props::PropertyTraits<T>::Internal>
1180 ::type* dummy = 0) const;
1181 // Getter for extended property
1182 template<typename T>
1183 T getValue(typename boost::disable_if_c<simgear::props::PropertyTraits<T>::Internal>
1184 ::type* dummy = 0) const;
1187 * Get a list of values from all children with the given name
1189 template<typename T, typename T_get /* = T */> // TODO use C++11 or traits
1190 std::vector<T> getChildValues(const std::string& name) const;
1193 * Get a list of values from all children with the given name
1195 template<typename T>
1196 std::vector<T> getChildValues(const std::string& name) const;
1199 * Set a bool value for this node.
1201 bool setBoolValue (bool value);
1205 * Set an int value for this node.
1207 bool setIntValue (int value);
1211 * Set a long int value for this node.
1213 bool setLongValue (long value);
1217 * Set a float value for this node.
1219 bool setFloatValue (float value);
1223 * Set a double value for this node.
1225 bool setDoubleValue (double value);
1229 * Set a string value for this node.
1231 bool setStringValue (const char * value);
1234 * Set a string value for this node.
1236 bool setStringValue (const std::string& value)
1237 { return setStringValue(value.c_str()); }
1241 * Set a value of unspecified type for this node.
1243 bool setUnspecifiedValue (const char * value);
1245 template<typename T>
1246 bool setValue(const T& val,
1247 typename boost::enable_if_c<simgear::props::PropertyTraits<T>::Internal>
1250 template<typename T>
1251 bool setValue(const T& val,
1252 typename boost::disable_if_c<simgear::props::PropertyTraits<T>::Internal>
1256 bool setValue(const char (&val)[N])
1258 return setValue(&val[0]);
1262 * Set relative node to given value and afterwards make read only.
1264 * @param relative_path Path to node
1265 * @param value Value to set
1267 * @return whether value could be set
1269 template<typename T>
1270 bool setValueReadOnly(const std::string& relative_path, const T& value)
1272 SGPropertyNode* node = getNode(relative_path, true);
1273 bool ret = node->setValue(value);
1274 node->setAttributes(READ);
1279 * Interpolate current value to target value within given time.
1281 * @param type Type of interpolation ("numeric", "color", etc.)
1282 * @param target Node containing target value
1283 * @param duration Duration of interpolation (in seconds)
1284 * @param easing Easing function (http://easings.net/)
1286 bool interpolate( const std::string& type,
1287 const SGPropertyNode& target,
1288 double duration = 0.6,
1289 const std::string& easing = "swing" );
1292 * Interpolate current value to a series of values within given durations.
1294 * @param type Type of interpolation ("numeric", "color", etc.)
1295 * @param values Nodes containing intermediate and target values
1296 * @param deltas Durations for each interpolation step (in seconds)
1297 * @param easing Easing function (http://easings.net/)
1299 bool interpolate( const std::string& type,
1300 const simgear::PropertyList& values,
1301 const double_list& deltas,
1302 const std::string& easing = "swing" );
1305 * Set the interpolation manager used by the interpolate methods.
1307 static void setInterpolationMgr(simgear::PropertyInterpolationMgr* mgr);
1310 * Get the interpolation manager
1312 static simgear::PropertyInterpolationMgr* getInterpolationMgr();
1315 * Print the value of the property to a stream.
1317 std::ostream& printOn(std::ostream& stream) const;
1325 * Test whether this node is bound to an external data source.
1327 bool isTied () const { return _tied; }
1330 * Bind this node to an external source.
1332 template<typename T>
1333 bool tie(const SGRawValue<T> &rawValue, bool useDefault = true);
1336 * Unbind this node from any external data source.
1342 // Convenience methods using paths.
1343 // TODO: add attribute methods
1348 * Get another node's type.
1350 simgear::props::Type getType (const char * relative_path) const;
1353 * Get another node's type.
1355 simgear::props::Type getType (const std::string& relative_path) const
1356 { return getType(relative_path.c_str()); }
1359 * Test whether another node has a leaf value.
1361 bool hasValue (const char * relative_path) const;
1364 * Test whether another node has a leaf value.
1366 bool hasValue (const std::string& relative_path) const
1367 { return hasValue(relative_path.c_str()); }
1370 * Get another node's value as a bool.
1372 bool getBoolValue (const char * relative_path,
1373 bool defaultValue = false) const;
1376 * Get another node's value as a bool.
1378 bool getBoolValue (const std::string& relative_path,
1379 bool defaultValue = false) const
1380 { return getBoolValue(relative_path.c_str(), defaultValue); }
1383 * Get another node's value as an int.
1385 int getIntValue (const char * relative_path,
1386 int defaultValue = 0) const;
1389 * Get another node's value as an int.
1391 int getIntValue (const std::string& relative_path,
1392 int defaultValue = 0) const
1393 { return getIntValue(relative_path.c_str(), defaultValue); }
1397 * Get another node's value as a long int.
1399 long getLongValue (const char * relative_path,
1400 long defaultValue = 0L) const;
1403 * Get another node's value as a long int.
1405 long getLongValue (const std::string& relative_path,
1406 long defaultValue = 0L) const
1407 { return getLongValue(relative_path.c_str(), defaultValue); }
1410 * Get another node's value as a float.
1412 float getFloatValue (const char * relative_path,
1413 float defaultValue = 0.0f) const;
1416 * Get another node's value as a float.
1418 float getFloatValue (const std::string& relative_path,
1419 float defaultValue = 0.0f) const
1420 { return getFloatValue(relative_path.c_str(), defaultValue); }
1424 * Get another node's value as a double.
1426 double getDoubleValue (const char * relative_path,
1427 double defaultValue = 0.0) const;
1430 * Get another node's value as a double.
1432 double getDoubleValue (const std::string& relative_path,
1433 double defaultValue = 0.0) const
1434 { return getDoubleValue(relative_path.c_str(), defaultValue); }
1437 * Get another node's value as a string.
1439 const char * getStringValue (const char * relative_path,
1440 const char * defaultValue = "") const;
1444 * Get another node's value as a string.
1446 const char * getStringValue (const std::string& relative_path,
1447 const char * defaultValue = "") const
1448 { return getStringValue(relative_path.c_str(), defaultValue); }
1452 * Set another node's value as a bool.
1454 bool setBoolValue (const char * relative_path, bool value);
1457 * Set another node's value as a bool.
1459 bool setBoolValue (const std::string& relative_path, bool value)
1460 { return setBoolValue(relative_path.c_str(), value); }
1464 * Set another node's value as an int.
1466 bool setIntValue (const char * relative_path, int value);
1469 * Set another node's value as an int.
1471 bool setIntValue (const std::string& relative_path, int value)
1472 { return setIntValue(relative_path.c_str(), value); }
1476 * Set another node's value as a long int.
1478 bool setLongValue (const char * relative_path, long value);
1481 * Set another node's value as a long int.
1483 bool setLongValue (const std::string& relative_path, long value)
1484 { return setLongValue(relative_path.c_str(), value); }
1488 * Set another node's value as a float.
1490 bool setFloatValue (const char * relative_path, float value);
1493 * Set another node's value as a float.
1495 bool setFloatValue (const std::string& relative_path, float value)
1496 { return setFloatValue(relative_path.c_str(), value); }
1500 * Set another node's value as a double.
1502 bool setDoubleValue (const char * relative_path, double value);
1505 * Set another node's value as a double.
1507 bool setDoubleValue (const std::string& relative_path, double value)
1508 { return setDoubleValue(relative_path.c_str(), value); }
1512 * Set another node's value as a string.
1514 bool setStringValue (const char * relative_path, const char * value);
1516 bool setStringValue(const char * relative_path, const std::string& value)
1517 { return setStringValue(relative_path, value.c_str()); }
1519 * Set another node's value as a string.
1521 bool setStringValue (const std::string& relative_path, const char * value)
1522 { return setStringValue(relative_path.c_str(), value); }
1524 bool setStringValue (const std::string& relative_path,
1525 const std::string& value)
1526 { return setStringValue(relative_path.c_str(), value.c_str()); }
1529 * Set another node's value with no specified type.
1531 bool setUnspecifiedValue (const char * relative_path, const char * value);
1535 * Test whether another node is bound to an external data source.
1537 bool isTied (const char * relative_path) const;
1540 * Test whether another node is bound to an external data source.
1542 bool isTied (const std::string& relative_path) const
1543 { return isTied(relative_path.c_str()); }
1546 * Bind another node to an external bool source.
1548 bool tie (const char * relative_path, const SGRawValue<bool> &rawValue,
1549 bool useDefault = true);
1552 * Bind another node to an external bool source.
1554 bool tie (const std::string& relative_path, const SGRawValue<bool> &rawValue,
1555 bool useDefault = true)
1556 { return tie(relative_path.c_str(), rawValue, useDefault); }
1560 * Bind another node to an external int source.
1562 bool tie (const char * relative_path, const SGRawValue<int> &rawValue,
1563 bool useDefault = true);
1566 * Bind another node to an external int source.
1568 bool tie (const std::string& relative_path, const SGRawValue<int> &rawValue,
1569 bool useDefault = true)
1570 { return tie(relative_path.c_str(), rawValue, useDefault); }
1574 * Bind another node to an external long int source.
1576 bool tie (const char * relative_path, const SGRawValue<long> &rawValue,
1577 bool useDefault = true);
1580 * Bind another node to an external long int source.
1582 bool tie (const std::string& relative_path, const SGRawValue<long> &rawValue,
1583 bool useDefault = true)
1584 { return tie(relative_path.c_str(), rawValue, useDefault); }
1588 * Bind another node to an external float source.
1590 bool tie (const char * relative_path, const SGRawValue<float> &rawValue,
1591 bool useDefault = true);
1594 * Bind another node to an external float source.
1596 bool tie (const std::string& relative_path, const SGRawValue<float> &rawValue,
1597 bool useDefault = true)
1598 { return tie(relative_path.c_str(), rawValue, useDefault); }
1602 * Bind another node to an external double source.
1604 bool tie (const char * relative_path, const SGRawValue<double> &rawValue,
1605 bool useDefault = true);
1608 * Bind another node to an external double source.
1610 bool tie (const std::string& relative_path, const SGRawValue<double> &rawValue,
1611 bool useDefault = true)
1612 { return tie(relative_path.c_str(), rawValue, useDefault); }
1616 * Bind another node to an external string source.
1618 bool tie (const char * relative_path, const SGRawValue<const char *> &rawValue,
1619 bool useDefault = true);
1622 * Bind another node to an external string source.
1624 bool tie (const std::string& relative_path, const SGRawValue<const char*> &rawValue,
1625 bool useDefault = true)
1626 { return tie(relative_path.c_str(), rawValue, useDefault); }
1630 * Unbind another node from any external data source.
1632 bool untie (const char * relative_path);
1635 * Unbind another node from any external data source.
1637 bool untie (const std::string& relative_path)
1638 { return untie(relative_path.c_str()); }
1642 * Add a change listener to the property. If "initial" is set call the
1643 * listener initially.
1645 void addChangeListener (SGPropertyChangeListener * listener,
1646 bool initial = false);
1650 * Remove a change listener from the property.
1652 void removeChangeListener (SGPropertyChangeListener * listener);
1656 * Get the number of listeners.
1658 int nListeners () const { return _listeners ? (int)_listeners->size() : 0; }
1662 * Fire a value change event to all listeners.
1664 void fireValueChanged ();
1668 * Fire a child-added event to all listeners.
1670 void fireChildAdded (SGPropertyNode * child);
1673 * Trigger a child-added and value-changed event for every child (Unlimited
1676 * @param fire_self Whether to trigger the events also for the node itself.
1678 * It can be used to simulating the creation of a property tree, eg. for
1679 * (re)initializing a subsystem which is controlled through the property tree.
1681 void fireCreatedRecursive(bool fire_self = false);
1684 * Fire a child-removed event to all listeners.
1686 void fireChildRemoved (SGPropertyNode * child);
1689 * Fire a child-removed event for every child of this node (Unlimited depth)
1691 * Upon removal of a child node only for this single node a child-removed
1692 * event is triggered. If eg. resource cleanup relies on receiving a
1693 * child-removed event for every child this method can be used.
1695 void fireChildrenRemovedRecursive();
1699 * Clear any existing value and set the type to NONE.
1704 * Compare two property trees. The property trees are equal if: 1)
1705 * They have no children, and have the same type and the values are
1706 * equal, or 2) have the same number of children, and the
1707 * corresponding children in each tree are equal. "corresponding"
1708 * means have the same name and index.
1710 * Attributes, removed children, and aliases aren't considered.
1712 static bool compare (const SGPropertyNode& lhs, const SGPropertyNode& rhs);
1716 void fireValueChanged (SGPropertyNode * node);
1717 void fireChildAdded (SGPropertyNode * parent, SGPropertyNode * child);
1718 void fireChildRemoved (SGPropertyNode * parent, SGPropertyNode * child);
1720 SGPropertyNode_ptr eraseChild(simgear::PropertyList::iterator child);
1723 * Protected constructor for making new nodes on demand.
1725 SGPropertyNode (const std::string& name, int index, SGPropertyNode * parent);
1726 template<typename Itr>
1727 SGPropertyNode (Itr begin, Itr end, int index, SGPropertyNode * parent);
1729 static simgear::PropertyInterpolationMgr* _interpolation_mgr;
1733 // Get the raw value
1734 bool get_bool () const;
1735 int get_int () const;
1736 long get_long () const;
1737 float get_float () const;
1738 double get_double () const;
1739 const char * get_string () const;
1741 // Set the raw value
1742 bool set_bool (bool value);
1743 bool set_int (int value);
1744 bool set_long (long value);
1745 bool set_float (float value);
1746 bool set_double (double value);
1747 bool set_string (const char * value);
1751 * Get the value as a string.
1753 const char * make_string () const;
1756 * Trace a read access.
1758 void trace_read () const;
1762 * Trace a write access.
1764 void trace_write () const;
1768 /// To avoid cyclic reference counting loops this shall not be a reference
1770 SGPropertyNode * _parent;
1771 simgear::PropertyList _children;
1772 mutable std::string _buffer;
1773 simgear::props::Type _type;
1777 // The right kind of pointer...
1779 SGPropertyNode * alias;
1792 std::vector<SGPropertyChangeListener *> * _listeners;
1794 // Pass name as a pair of iterators
1795 template<typename Itr>
1796 SGPropertyNode * getChildImpl (Itr begin, Itr end, int index = 0, bool create = false);
1797 // very internal method
1798 template<typename Itr>
1799 SGPropertyNode* getExistingChild (Itr begin, Itr end, int index);
1800 // very internal path parsing function
1801 template<typename SplitItr>
1802 friend SGPropertyNode* find_node_aux(SGPropertyNode * current, SplitItr& itr,
1803 bool create, int last_index);
1805 friend size_t hash_value(const SGPropertyNode& node);
1808 // Convenience functions for use in templates
1809 template<typename T>
1810 typename boost::disable_if<boost::is_enum<T>, T>::type
1811 getValue(const SGPropertyNode*);
1814 inline bool getValue<bool>(const SGPropertyNode* node) { return node->getBoolValue(); }
1817 inline int getValue<int>(const SGPropertyNode* node) { return node->getIntValue(); }
1820 inline long getValue<long>(const SGPropertyNode* node) { return node->getLongValue(); }
1823 inline float getValue<float>(const SGPropertyNode* node)
1825 return node->getFloatValue();
1829 inline double getValue<double>(const SGPropertyNode* node)
1831 return node->getDoubleValue();
1835 inline const char * getValue<const char*>(const SGPropertyNode* node)
1837 return node->getStringValue ();
1841 inline std::string getValue<std::string>(const SGPropertyNode* node)
1843 return node->getStringValue();
1849 * Default trait for extracting enum values from SGPropertyNode. Create your
1850 * own specialization for specific enum types to enable validation of values.
1856 * Typename of the enum
1858 static const char* name() { return typeid(T).name(); }
1861 * @return Default value (will be used if validation fails)
1863 static T defVal() { return T(); }
1866 * @return Whether the given integer value has an enum value defined
1868 static bool validate(int) { return true; }
1870 } // namespace simgear
1872 /** Extract enum from SGPropertyNode */
1873 template<typename T>
1874 inline typename boost::enable_if<boost::is_enum<T>, T>::type
1875 getValue(const SGPropertyNode* node)
1877 typedef simgear::enum_traits<T> Traits;
1878 int val = node->getIntValue();
1879 if( !Traits::validate(val) )
1885 "Invalid value for enum (" << Traits::name() << ", val = " << val << ")"
1887 return Traits::defVal();
1889 return static_cast<T>(node->getIntValue());
1892 inline bool setValue(SGPropertyNode* node, bool value)
1894 return node->setBoolValue(value);
1897 inline bool setValue(SGPropertyNode* node, int value)
1899 return node->setIntValue(value);
1902 inline bool setValue(SGPropertyNode* node, long value)
1904 return node->setLongValue(value);
1907 inline bool setValue(SGPropertyNode* node, float value)
1909 return node->setFloatValue(value);
1912 inline bool setValue(SGPropertyNode* node, double value)
1914 return node->setDoubleValue(value);
1917 inline bool setValue(SGPropertyNode* node, const char* value)
1919 return node->setStringValue(value);
1922 inline bool setValue (SGPropertyNode* node, const std::string& value)
1924 return node->setStringValue(value.c_str());
1927 template<typename T>
1928 bool SGPropertyNode::tie(const SGRawValue<T> &rawValue, bool useDefault)
1930 using namespace simgear::props;
1931 if (_type == ALIAS || _tied)
1934 useDefault = useDefault && hasValue();
1935 T old_val = SGRawValue<T>::DefaultValue();
1937 old_val = getValue<T>(this);
1939 if (PropertyTraits<T>::Internal)
1940 _type = PropertyTraits<T>::type_tag;
1944 _value.val = rawValue.clone();
1946 int save_attributes = getAttributes();
1947 setAttribute( WRITE, true );
1949 setAttributes( save_attributes );
1955 bool SGPropertyNode::tie (const SGRawValue<const char *> &rawValue,
1958 template<typename T>
1959 T SGPropertyNode::getValue(typename boost::disable_if_c<simgear::props
1960 ::PropertyTraits<T>::Internal>::type* dummy) const
1962 using namespace simgear::props;
1963 if (_attr == (READ|WRITE) && _type == EXTENDED
1964 && _value.val->getType() == PropertyTraits<T>::type_tag) {
1965 return static_cast<SGRawValue<T>*>(_value.val)->getValue();
1967 if (getAttribute(TRACE_READ))
1969 if (!getAttribute(READ))
1970 return SGRawValue<T>::DefaultValue();
1973 if (_value.val->getType() == PropertyTraits<T>::type_tag)
1974 return static_cast<SGRawValue<T>*>(_value.val)->getValue();
1978 return simgear::parseString<T>(make_string());
1980 default: // avoid compiler warning
1983 return SGRawValue<T>::DefaultValue();
1986 template<typename T>
1987 inline T SGPropertyNode::getValue(typename boost::enable_if_c<simgear::props
1988 ::PropertyTraits<T>::Internal>::type* dummy) const
1990 return ::getValue<T>(this);
1993 template<typename T, typename T_get /* = T */> // TODO use C++11 or traits
1994 std::vector<T> SGPropertyNode::getChildValues(const std::string& name) const
1996 const simgear::PropertyList& props = getChildren(name);
1997 std::vector<T> values( props.size() );
1999 for( size_t i = 0; i < props.size(); ++i )
2000 values[i] = props[i]->getValue<T_get>();
2005 template<typename T>
2007 std::vector<T> SGPropertyNode::getChildValues(const std::string& name) const
2009 return getChildValues<T, T>(name);
2012 template<typename T>
2013 bool SGPropertyNode::setValue(const T& val,
2014 typename boost::disable_if_c<simgear::props
2015 ::PropertyTraits<T>::Internal>::type* dummy)
2017 using namespace simgear::props;
2018 if (_attr == (READ|WRITE) && _type == EXTENDED
2019 && _value.val->getType() == PropertyTraits<T>::type_tag) {
2020 static_cast<SGRawValue<T>*>(_value.val)->setValue(val);
2023 if (getAttribute(WRITE)
2024 && ((_type == EXTENDED
2025 && _value.val->getType() == PropertyTraits<T>::type_tag)
2026 || _type == NONE || _type == UNSPECIFIED)) {
2027 if (_type == NONE || _type == UNSPECIFIED) {
2030 _value.val = new SGRawValueContainer<T>(val);
2032 static_cast<SGRawValue<T>*>(_value.val)->setValue(val);
2034 if (getAttribute(TRACE_WRITE))
2041 template<typename T>
2042 inline bool SGPropertyNode::setValue(const T& val,
2043 typename boost::enable_if_c<simgear::props
2044 ::PropertyTraits<T>::Internal>::type* dummy)
2046 return ::setValue(this, val);
2050 * Utility function for creation of a child property node.
2052 inline SGPropertyNode* makeChild(SGPropertyNode* parent, const char* name,
2055 return parent->getChild(name, index, true);
2059 * Utility function for creation of a child property node using a
2064 template<typename StringType>
2065 inline SGPropertyNode* makeNode(SGPropertyNode* parent, const StringType& name)
2067 return parent->getNode(name, true);
2072 size_t hash_value(const SGPropertyNode& node);
2074 // Helper comparison and hash functions for common cases
2082 bool operator()(const SGPropertyNode* lhs, const SGPropertyNode* rhs) const
2084 return SGPropertyNode::compare(*lhs, *rhs);
2086 bool operator()(SGPropertyNode_ptr lhs, const SGPropertyNode* rhs) const
2088 return SGPropertyNode::compare(*lhs, *rhs);
2090 bool operator()(const SGPropertyNode* lhs, SGPropertyNode_ptr rhs) const
2092 return SGPropertyNode::compare(*lhs, *rhs);
2094 bool operator()(SGPropertyNode_ptr lhs, SGPropertyNode_ptr rhs) const
2096 return SGPropertyNode::compare(*lhs, *rhs);
2102 size_t operator()(const SGPropertyNode* node) const
2104 return hash_value(*node);
2106 size_t operator()(SGPropertyNode_ptr node) const
2108 return hash_value(*node);
2114 /** Convenience class for change listener callbacks without
2115 * creating a derived class implementing a "valueChanged" method.
2116 * Also removes listener on destruction automatically.
2119 class SGPropertyChangeCallback
2120 : public SGPropertyChangeListener
2123 SGPropertyChangeCallback(T* obj, void (T::*method)(SGPropertyNode*),
2124 SGPropertyNode_ptr property,bool initial=false)
2125 : _obj(obj), _callback(method), _property(property)
2127 _property->addChangeListener(this,initial);
2130 SGPropertyChangeCallback(const SGPropertyChangeCallback<T>& other) :
2131 _obj(other._obj), _callback(other._callback), _property(other._property)
2133 _property->addChangeListener(this,false);
2136 virtual ~SGPropertyChangeCallback()
2138 _property->removeChangeListener(this);
2140 void valueChanged (SGPropertyNode * node)
2142 (_obj->*_callback)(node);
2146 void (T::*_callback)(SGPropertyNode*);
2147 SGPropertyNode_ptr _property;
2150 #endif // __PROPS_HXX