]> git.mxchange.org Git - simgear.git/blob - simgear/props/props.hxx
d06f4855e317f5074fc83dfef8cb84f895ff78ca
[simgear.git] / simgear / props / props.hxx
1 /**
2  * \file props.hxx
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.
6  *
7  * See props.html for documentation [replace with URL when available].
8  *
9  * $Id$
10  */
11
12 #ifndef __PROPS_HXX
13 #define __PROPS_HXX
14
15 #ifndef PROPS_STANDALONE
16 #define PROPS_STANDALONE 0
17 #endif
18
19 #include <vector>
20 #include <string>
21 #include <iostream>
22 #include <sstream>
23
24 #include <boost/utility.hpp>
25
26 #if PROPS_STANDALONE
27 #else
28 #include <simgear/compiler.h>
29 #include <simgear/debug/logstream.hxx>
30 #endif
31
32
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>
37
38 // XXX This whole file should be in the simgear namespace, but I don't
39 // have the guts yet...
40
41 namespace simgear
42 {
43
44   class PropertyInterpolationMgr;
45
46 template<typename T>
47 std::istream& readFrom(std::istream& stream, T& result)
48 {
49     stream >> result;
50     return stream;
51 }
52
53 /**
54  * Parse a string as an object of a given type.
55  * XXX no error behavior yet.
56  *
57  * @tparam T the return type
58  * @param str the string
59  * @return the object.
60  */
61 template<typename T>
62 inline T parseString(const std::string& str)
63 {
64     std::istringstream stream(str);
65     T result;
66     readFrom(stream, result);
67     return result;
68 }
69
70 /**
71  * Property value types.
72  */
73
74 #ifdef NONE
75 #pragma warn A sloppy coder has defined NONE as a macro!
76 #undef NONE
77 #endif
78
79 #ifdef ALIAS
80 #pragma warn A sloppy coder has defined ALIAS as a macro!
81 #undef ALIAS
82 #endif
83
84 #ifdef UNSPECIFIED
85 #pragma warn A sloppy coder has defined UNSPECIFIED as a macro!
86 #undef UNSPECIFIED
87 #endif
88
89 #ifdef BOOL
90 #pragma warn A sloppy coder has defined BOOL as a macro!
91 #undef BOOL
92 #endif
93
94 #ifdef INT
95 #pragma warn A sloppy coder has defined INT as a macro!
96 #undef INT
97 #endif
98
99 #ifdef LONG
100 #pragma warn A sloppy coder has defined LONG as a macro!
101 #undef LONG
102 #endif
103
104 #ifdef FLOAT
105 #pragma warn A sloppy coder has defined FLOAT as a macro!
106 #undef FLOAT
107 #endif
108
109 #ifdef DOUBLE
110 #pragma warn A sloppy coder has defined DOUBLE as a macro!
111 #undef DOUBLE
112 #endif
113
114 #ifdef STRING
115 #pragma warn A sloppy coder has defined STRING as a macro!
116 #undef STRING
117 #endif
118
119 namespace props
120 {
121 /**
122  * The possible types of an SGPropertyNode. Types that appear after
123  * EXTENDED are not stored in the SGPropertyNode itself.
124  */
125 enum Type {
126     NONE = 0, /**< The node hasn't been assigned a value yet. */
127     ALIAS, /**< The node "points" to another node. */
128     BOOL,
129     INT,
130     LONG,
131     FLOAT,
132     DOUBLE,
133     STRING,
134     UNSPECIFIED,
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.
139                */
140     // Extended properties
141     VEC3D,
142     VEC4D
143 };
144
145 template<typename T> struct PropertyTraits;
146
147 #define DEFINTERNALPROP(TYPE, PROP) \
148 template<> \
149 struct PropertyTraits<TYPE> \
150 { \
151     static const Type type_tag = PROP; \
152     enum  { Internal = 1 }; \
153 }
154
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
163
164 }
165 }
166
167
168
169 ////////////////////////////////////////////////////////////////////////
170 // A raw value.
171 //
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.
175 //
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 ////////////////////////////////////////////////////////////////////////
180
181 /**
182  * Base class for SGRawValue classes that holds no type
183  * information. This allows some generic manipulation of the
184  * SGRawValue object.
185  */
186 class SGRaw
187 {
188 public:
189     /**
190      * Get the type enumeration for the raw value.
191      *
192      * @return the type.
193      */
194     virtual simgear::props::Type getType() const = 0;
195     virtual ~SGRaw() {}
196     
197     /**
198      * Create a new deep copy of this raw value.
199      *
200      * The copy will contain its own version of the underlying value
201      * as well, and will be the same type.
202      *
203      * @return A deep copy of the current object.
204      */
205     virtual SGRaw* clone() const = 0;
206
207 };
208
209 class SGRawExtended : public SGRaw
210 {
211 public:
212     /**    
213      * Make an SGRawValueContainer from the SGRawValue.
214      *
215      * This is a virtual function of SGRawExtended so that
216      * SGPropertyNode::untie doesn't need to know the type of an
217      * extended property.
218      */
219     virtual SGRawExtended* makeContainer() const = 0;
220     /**
221      * Write value out to a stream
222      */
223     virtual std::ostream& printOn(std::ostream& stream) const = 0;
224     /**
225      * Read value from a stream and store it.
226      */
227     virtual std::istream& readFrom(std::istream& stream) = 0;
228 };
229
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>
235 class SGRawBase;
236
237 template<typename T>
238 class SGRawBase<T, 1> : public SGRaw
239 {
240 };
241
242 template<typename T>
243 class SGRawBase<T, 0> : public SGRawExtended
244 {
245     virtual SGRawExtended* makeContainer() const;
246     virtual std::ostream& printOn(std::ostream& stream) const;
247     virtual std::istream& readFrom(std::istream& stream);
248 };
249
250 /**
251  * Abstract base class for a raw value.
252  *
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>
260  *
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.
268  *
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
272  * happen.</p>
273  *
274  * <p>All derived SGRawValue classes must implement {@link #getValue},
275  * {@link #setValue}, and {@link #clone} for the appropriate type.</p>
276  *
277  * @see SGPropertyNode
278  * @see SGRawValuePointer
279  * @see SGRawValueFunctions
280  * @see SGRawValueFunctionsIndexed
281  * @see SGRawValueMethods
282  * @see SGRawValueMethodsIndexed
283  * @see SGRawValueContainer
284  */
285 template <class T>
286 class SGRawValue : public SGRawBase<T>
287 {
288 public:
289
290   /**
291    * The default underlying value for this type.
292    *
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.
298    */
299   static T DefaultValue()
300   {
301     return T();
302   }
303
304
305   /**
306    * Constructor.
307    *
308    * Use the default value for this type.
309    */
310   SGRawValue () {}
311
312
313   /**
314    * Destructor.
315    */
316   virtual ~SGRawValue () {}
317
318
319   /**
320    * Return the underlying value.
321    *
322    * @return The actual value for the property.
323    * @see #setValue
324    */
325   virtual T getValue () const = 0;
326
327
328   /**
329    * Assign a new underlying value.
330    *
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
334    * value unchanged.
335    *
336    * @param value The actual value for the property.
337    * @return true if the value was set successfully, false otherwise.
338    * @see #getValue
339    */
340   virtual bool setValue (T value) = 0;
341
342
343   /**
344    * Return the type tag for this raw value type.
345    */
346   virtual simgear::props::Type getType() const
347   {
348     return simgear::props::PropertyTraits<T>::type_tag;
349   }
350 };
351
352
353
354 ////////////////////////////////////////////////////////////////////////
355 // Default values for every type.
356 ////////////////////////////////////////////////////////////////////////
357
358 template<> inline bool SGRawValue<bool>::DefaultValue()
359 {
360   return false;
361 }
362
363 template<> inline const char * SGRawValue<const char *>::DefaultValue()
364 {
365   return "";
366 }
367
368 /**
369  * A raw value bound to a pointer.
370  *
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
376  * pointer.
377  */
378 template <class T>
379 class SGRawValuePointer : public SGRawValue<T>
380 {
381 public:
382
383   /**
384    * Explicit pointer constructor.
385    *
386    * Create a new raw value bound to the value of the variable
387    * referenced by the pointer.
388    *
389    * @param ptr The pointer to the variable to which this raw value
390    * will be bound.
391    */
392   SGRawValuePointer (T * ptr) : _ptr(ptr) {}
393
394   /**
395    * Destructor.
396    */
397   virtual ~SGRawValuePointer () {}
398
399   /**
400    * Get the underlying value.
401    *
402    * This method will dereference the pointer and return the
403    * variable's value.
404    */
405   virtual T getValue () const { return *_ptr; }
406
407   /**
408    * Set the underlying value.
409    *
410    * This method will dereference the pointer and change the
411    * variable's value.
412    */
413   virtual bool setValue (T value) { *_ptr = value; return true; }
414
415   /**
416    * Create a copy of this raw value.
417    *
418    * The copy will use the same external pointer as the original.
419    */
420   virtual SGRaw* clone () const {
421     return new SGRawValuePointer(_ptr);
422   }
423
424 private:
425   T * _ptr;
426 };
427
428
429 /**
430  * A value managed through static functions.
431  *
432  * A read-only value will not have a setter; a write-only value will
433  * not have a getter.
434  */
435 template <class T>
436 class SGRawValueFunctions : public SGRawValue<T>
437 {
438 public:
439
440   /**
441    * The template type of a static getter function.
442    */
443   typedef T (*getter_t)();
444
445   /**
446    * The template type of a static setter function.
447    */
448   typedef void (*setter_t)(T);
449
450   /**
451    * Explicit constructor.
452    *
453    * Create a new raw value bound to the getter and setter supplied.
454    *
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.
459    */
460   SGRawValueFunctions (getter_t getter = 0, setter_t setter = 0)
461     : _getter(getter), _setter(setter) {}
462
463   /**
464    * Destructor.
465    */
466   virtual ~SGRawValueFunctions () {}
467
468   /**
469    * Get the underlying value.
470    *
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.
474    */
475   virtual T getValue () const {
476     if (_getter) return (*_getter)();
477     else return SGRawValue<T>::DefaultValue();
478   }
479
480   /**
481    * Set the underlying value.
482    *
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.
486    */
487   virtual bool setValue (T value) {
488     if (_setter) { (*_setter)(value); return true; }
489     else return false;
490   }
491
492   /**
493    * Create a copy of this raw value, bound to the same functions.
494    */
495   virtual SGRaw* clone () const {
496     return new SGRawValueFunctions(_getter,_setter);
497   }
498
499 private:
500   getter_t _getter;
501   setter_t _setter;
502 };
503
504
505 /**
506  * An indexed value bound to static functions.
507  *
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.
512  *
513  * @see SGRawValue
514  */
515 template <class T>
516 class SGRawValueFunctionsIndexed : public SGRawValue<T>
517 {
518 public:
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();
527   }
528   virtual bool setValue (T value) {
529     if (_setter) { (*_setter)(_index, value); return true; }
530     else return false;
531   }
532   virtual SGRaw* clone () const {
533     return new SGRawValueFunctionsIndexed(_index, _getter, _setter);
534   }
535 private:
536   int _index;
537   getter_t _getter;
538   setter_t _setter;
539 };
540
541
542 /**
543  * A value managed through an object and access methods.
544  *
545  * A read-only value will not have a setter; a write-only value will
546  * not have a getter.
547  */
548 template <class C, class T>
549 class SGRawValueMethods : public SGRawValue<T>
550 {
551 public:
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(); }
560   }
561   virtual bool setValue (T value) {
562     if (_setter) { (_obj.*_setter)(value); return true; }
563     else return false;
564   }
565   virtual SGRaw* clone () const {
566     return new SGRawValueMethods(_obj, _getter, _setter);
567   }
568 private:
569   C &_obj;
570   getter_t _getter;
571   setter_t _setter;
572 };
573
574
575 /**
576  * An indexed value managed through an object and access methods.
577  *
578  * A read-only value will not have a setter; a write-only value will
579  * not have a getter.
580  */
581 template <class C, class T>
582 class SGRawValueMethodsIndexed : public SGRawValue<T>
583 {
584 public:
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(); }
594   }
595   virtual bool setValue (T value) {
596     if (_setter) { (_obj.*_setter)(_index, value); return true; }
597     else return false;
598   }
599   virtual SGRaw* clone () const {
600     return new SGRawValueMethodsIndexed(_obj, _index, _getter, _setter);
601   }
602 private:
603   C &_obj;
604   int _index;
605   getter_t _getter;
606   setter_t _setter;
607 };
608
609 /**
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.
613  */
614 template <class T>
615 class SGRawValueContainer : public SGRawValue<T>
616 {
617 public:
618
619     /**
620      * Explicit constructor.
621      */
622     SGRawValueContainer(const T& obj) : _obj(obj) {}
623
624     /**
625      * Destructor.
626      */
627     virtual ~SGRawValueContainer() {}
628
629     /**
630      * Get the underlying value.
631      */
632     virtual T getValue() const { return _obj; }
633
634     /**
635      * Set the underlying value.
636      *
637      * This method will dereference the pointer and change the
638      * variable's value.
639      */
640     virtual bool setValue (T value) { _obj = value; return true; }
641
642     /**
643      * Create a copy of this raw value.
644      */
645     virtual SGRaw* clone () const {
646         return new SGRawValueContainer(_obj);
647     }
648
649 private:
650     T _obj;
651 };
652
653 template<typename T>
654 SGRawExtended* SGRawBase<T, 0>::makeContainer() const
655 {
656     return new SGRawValueContainer<T>(static_cast<const SGRawValue<T>*>(this)
657                                       ->getValue());
658 }
659
660 template<typename T>
661 std::ostream& SGRawBase<T, 0>::printOn(std::ostream& stream) const
662 {
663     return stream << static_cast<SGRawValue<T>*>(this)->getValue();
664 }
665
666 template<typename T>
667 std::istream& SGRawBase<T, 0>::readFrom(std::istream& stream)
668 {
669     T value;
670     simgear::readFrom(stream, value);
671     static_cast<SGRawValue<T>*>(this)->setValue(value);
672     return stream;
673 }
674
675 /**
676  * The smart pointer that manage reference counting
677  */
678 class SGPropertyNode;
679 typedef SGSharedPtr<SGPropertyNode> SGPropertyNode_ptr;
680 typedef SGSharedPtr<const SGPropertyNode> SGConstPropertyNode_ptr;
681
682 namespace simgear
683 {
684 typedef std::vector<SGPropertyNode_ptr> PropertyList;
685 }
686
687 /**
688  * The property change listener interface.
689  *
690  * <p>Any class that needs to listen for property changes must implement
691  * this interface.</p>
692  */
693 class SGPropertyChangeListener
694 {
695 public:
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);
700
701 protected:
702   friend class SGPropertyNode;
703   virtual void register_property (SGPropertyNode * node);
704   virtual void unregister_property (SGPropertyNode * node);
705
706 private:
707   std::vector<SGPropertyNode *> _properties;
708 };
709
710
711 /**
712  * A node in a property tree.
713  */
714 class SGPropertyNode : public SGReferenced
715 {
716 public:
717
718   /**
719    * Public constants.
720    */
721   enum {
722     MAX_STRING_LEN = 1024
723   };
724
725   /**
726    * Access mode attributes.
727    *
728    * <p>The ARCHIVE attribute is strictly advisory, and controls
729    * whether the property should normally be saved and restored.</p>
730    */
731   enum Attribute {
732     READ = 1,
733     WRITE = 2,
734     ARCHIVE = 4,
735     REMOVED = 8,
736     TRACE_READ = 16,
737     TRACE_WRITE = 32,
738     USERARCHIVE = 64,
739     PRESERVE = 128
740     // beware: if you add another attribute here,
741     // also update value of "LAST_USED_ATTRIBUTE".
742   };
743
744
745   /**
746    * Last used attribute
747    * Update as needed when enum Attribute is changed
748    */
749   static const int LAST_USED_ATTRIBUTE;
750
751   /**
752    * Default constructor.
753    */
754   SGPropertyNode ();
755
756
757   /**
758    * Copy constructor.
759    */
760   SGPropertyNode (const SGPropertyNode &node);
761
762
763   /**
764    * Destructor.
765    */
766   virtual ~SGPropertyNode ();
767
768
769
770   //
771   // Basic properties.
772   //
773
774   /**
775    * Test whether this node contains a primitive leaf value.
776    */
777     bool hasValue () const { return (_type != simgear::props::NONE); }
778
779
780   /**
781    * Get the node's simple (XML) name.
782    */
783   const char * getName () const { return _name.c_str(); }
784
785   /**
786    * Get the node's simple name as a string.
787    */
788   const std::string& getNameString () const { return _name; }
789
790   /**
791    * Get the node's pretty display name, with subscript when needed.
792    */
793     std::string getDisplayName (bool simplify = false) const;
794
795
796   /**
797    * Get the node's integer index.
798    */
799   int getIndex () const { return _index; }
800
801
802   /**
803    * Get a non-const pointer to the node's parent.
804    */
805   SGPropertyNode * getParent () { return _parent; }
806
807
808   /**
809    * Get a const pointer to the node's parent.
810    */
811   const SGPropertyNode * getParent () const { return _parent; }
812
813
814   //
815   // Children.
816   //
817
818
819   /**
820    * Get the number of child nodes.
821    */
822   int nChildren () const { return (int)_children.size(); }
823
824
825   /**
826    * Get a child node by position (*NOT* index).
827    */
828   SGPropertyNode * getChild (int position);
829
830
831   /**
832    * Get a const child node by position (*NOT* index).
833    */
834   const SGPropertyNode * getChild (int position) const;
835
836
837   /**
838    * Test whether a named child exists.
839    */
840   bool hasChild (const char * name, int index = 0) const
841   {
842     return (getChild(name, index) != 0);
843   }
844
845   /**
846    * Test whether a named child exists.
847    */
848   bool hasChild (const std::string& name, int index = 0) const
849   {
850     return (getChild(name, index) != 0);
851   }
852
853   /**
854    * Create a new child node with the given name and an unused index
855    *
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
859    */
860   SGPropertyNode * addChild ( const char* name,
861                               int min_index = 0,
862                               bool append = true );
863   SGPropertyNode * addChild ( const std::string& name,
864                               int min_index = 0,
865                               bool append = true )
866   { return addChild(name.c_str(), min_index, append); }
867
868   /**
869    * Create multiple child nodes with the given name an unused indices
870    *
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
875    */
876   simgear::PropertyList addChildren ( const std::string& name,
877                                       size_t count,
878                                       int min_index = 0,
879                                       bool append = true );
880
881   /**
882    * Get a child node by name and index.
883    */
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);
888   /**
889    * Get a const child node by name and index.
890    */
891   const SGPropertyNode * getChild (const char * name, int index = 0) const;
892
893   /**
894    * Get a const child node by name and index.
895    */
896   const SGPropertyNode * getChild (const std::string& name, int index = 0) const
897   { return getChild(name.c_str(), index); }
898
899
900   /**
901    * Get a vector of all children with the specified name.
902    */
903   simgear::PropertyList getChildren (const char * name) const;
904
905   /**
906    * Get a vector of all children with the specified name.
907    */
908   simgear::PropertyList getChildren (const std::string& name) const
909   { return getChildren(name.c_str()); }
910
911   /**
912    * Remove child by position.
913    */
914   SGPropertyNode_ptr removeChild (int pos, bool keep = true);
915
916
917   /**
918    * Remove a child node
919    */
920   SGPropertyNode_ptr removeChild (const char * name, int index = 0,
921                                   bool keep = true);
922
923   /**
924    * Remove a child node
925    */
926   SGPropertyNode_ptr removeChild (const std::string& name, int index = 0,
927                                   bool keep = true)
928   { return removeChild(name.c_str(), index, keep); }
929
930   /**
931    * Remove all children with the specified name.
932    */
933   simgear::PropertyList removeChildren (const char * name, bool keep = true);
934
935   /**
936    * Remove all children with the specified name.
937    */
938   simgear::PropertyList removeChildren (const std::string& name,
939                                         bool keep = true)
940   { return removeChildren(name.c_str(), keep); }
941
942   //
943   // Alias support.
944   //
945
946
947   /**
948    * Alias this node's leaf value to another's.
949    */
950   bool alias (SGPropertyNode * target);
951
952
953   /**
954    * Alias this node's leaf value to another's by relative path.
955    */
956   bool alias (const char * path);
957
958   /**
959    * Alias this node's leaf value to another's by relative path.
960    */
961   bool alias (const std::string& path)
962   { return alias(path.c_str()); }
963
964
965   /**
966    * Remove any alias for this node.
967    */
968   bool unalias ();
969
970
971   /**
972    * Test whether the node's leaf value is aliased to another's.
973    */
974   bool isAlias () const { return (_type == simgear::props::ALIAS); }
975
976
977   /**
978    * Get a non-const pointer to the current alias target, if any.
979    */
980   SGPropertyNode * getAliasTarget ();
981
982
983   /**
984    * Get a const pointer to the current alias target, if any.
985    */
986   const SGPropertyNode * getAliasTarget () const;
987
988
989   //
990   // Path information.
991   //
992
993
994   /**
995    * Get the path to this node from the root.
996    */
997   std::string getPath (bool simplify = false) const;
998
999
1000   /**
1001    * Get a pointer to the root node.
1002    */
1003   SGPropertyNode * getRootNode ();
1004
1005
1006   /**
1007    * Get a const pointer to the root node.
1008    */
1009   const SGPropertyNode * getRootNode () const;
1010
1011
1012   /**
1013    * Get a pointer to another node by relative path.
1014    */
1015   SGPropertyNode * getNode (const char * relative_path, bool create = false);
1016
1017   /**
1018    * Get a pointer to another node by relative path.
1019    */
1020   SGPropertyNode * getNode (const std::string& relative_path, bool create = false)
1021   { return getNode(relative_path.c_str(), create); }
1022
1023   /**
1024    * Get a pointer to another node by relative path.
1025    *
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
1031    * component.
1032    */
1033   SGPropertyNode * getNode (const char * relative_path, int index,
1034                             bool create = false);
1035
1036   /**
1037    * Get a pointer to another node by relative path.
1038    *
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
1044    * component.
1045    */
1046   SGPropertyNode * getNode (const std::string& relative_path, int index,
1047                             bool create = false)
1048   { return getNode(relative_path.c_str(), index, create); }
1049
1050   /**
1051    * Get a const pointer to another node by relative path.
1052    */
1053   const SGPropertyNode * getNode (const char * relative_path) const;
1054
1055   /**
1056    * Get a const pointer to another node by relative path.
1057    */
1058   const SGPropertyNode * getNode (const std::string& relative_path) const
1059   { return getNode(relative_path.c_str()); }
1060
1061
1062   /**
1063    * Get a const pointer to another node by relative path.
1064    *
1065    * This method leaves the index off the last member of the path,
1066    * so that the user can specify it separate.
1067    */
1068   const SGPropertyNode * getNode (const char * relative_path,
1069                                   int index) const;
1070
1071   /**
1072    * Get a const pointer to another node by relative path.
1073    *
1074    * This method leaves the index off the last member of the path,
1075    * so that the user can specify it separate.
1076    */
1077   const SGPropertyNode * getNode (const std::string& relative_path,
1078                                   int index) const
1079   { return getNode(relative_path.c_str(), index); }
1080
1081   //
1082   // Access Mode.
1083   //
1084
1085   /**
1086    * Check a single mode attribute for the property node.
1087    */
1088   bool getAttribute (Attribute attr) const { return ((_attr & attr) != 0); }
1089
1090
1091   /**
1092    * Set a single mode attribute for the property node.
1093    */
1094   void setAttribute (Attribute attr, bool state) {
1095     (state ? _attr |= attr : _attr &= ~attr);
1096   }
1097
1098
1099   /**
1100    * Get all of the mode attributes for the property node.
1101    */
1102   int getAttributes () const { return _attr; }
1103
1104
1105   /**
1106    * Set all of the mode attributes for the property node.
1107    */
1108   void setAttributes (int attr) { _attr = attr; }
1109   
1110
1111   //
1112   // Leaf Value (primitive).
1113   //
1114
1115
1116   /**
1117    * Get the type of leaf value, if any, for this node.
1118    */
1119   simgear::props::Type getType () const;
1120
1121
1122   /**
1123    * Get a bool value for this node.
1124    */
1125   bool getBoolValue () const;
1126
1127
1128   /**
1129    * Get an int value for this node.
1130    */
1131   int getIntValue () const;
1132
1133
1134   /**
1135    * Get a long int value for this node.
1136    */
1137   long getLongValue () const;
1138
1139
1140   /**
1141    * Get a float value for this node.
1142    */
1143   float getFloatValue () const;
1144
1145
1146   /**
1147    * Get a double value for this node.
1148    */
1149   double getDoubleValue () const;
1150
1151
1152   /**
1153    * Get a string value for this node.
1154    */
1155   const char * getStringValue () const;
1156
1157   /**
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.
1160    */
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;
1168
1169   /**
1170    * Get a list of values from all children with the given name
1171    */
1172   template<typename T, typename T_get /* = T */> // TODO use C++11 or traits
1173   std::vector<T> getChildValues(const std::string& name) const;
1174
1175   /**
1176    * Get a list of values from all children with the given name
1177    */
1178   template<typename T>
1179   std::vector<T> getChildValues(const std::string& name) const;
1180
1181   /**
1182    * Set a bool value for this node.
1183    */
1184   bool setBoolValue (bool value);
1185
1186
1187   /**
1188    * Set an int value for this node.
1189    */
1190   bool setIntValue (int value);
1191
1192
1193   /**
1194    * Set a long int value for this node.
1195    */
1196   bool setLongValue (long value);
1197
1198
1199   /**
1200    * Set a float value for this node.
1201    */
1202   bool setFloatValue (float value);
1203
1204
1205   /**
1206    * Set a double value for this node.
1207    */
1208   bool setDoubleValue (double value);
1209
1210
1211   /**
1212    * Set a string value for this node.
1213    */
1214   bool setStringValue (const char * value);
1215
1216   /**
1217    * Set a string value for this node.
1218    */
1219   bool setStringValue (const std::string& value)
1220   { return setStringValue(value.c_str()); }
1221
1222
1223   /**
1224    * Set a value of unspecified type for this node.
1225    */
1226   bool setUnspecifiedValue (const char * value);
1227
1228   template<typename T>
1229   bool setValue(const T& val,
1230                 typename boost::enable_if_c<simgear::props::PropertyTraits<T>::Internal>
1231                 ::type* dummy = 0);
1232
1233   template<typename T>
1234   bool setValue(const T& val,
1235                 typename boost::disable_if_c<simgear::props::PropertyTraits<T>::Internal>
1236                 ::type* dummy = 0);
1237
1238   template<int N>
1239   bool setValue(const char (&val)[N])
1240   {
1241     return setValue(&val[0]);
1242   }
1243   
1244   /**
1245    * Interpolate current value to target value within given time.
1246    *
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/)
1251    */
1252   bool interpolate( const std::string& type,
1253                     const SGPropertyNode& target,
1254                     double duration = 0.6,
1255                     const std::string& easing = "swing" );
1256
1257   /**
1258    * Interpolate current value to a series of values within given durations.
1259    *
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/)
1264    */
1265   bool interpolate( const std::string& type,
1266                     const simgear::PropertyList& values,
1267                     const double_list& deltas,
1268                     const std::string& easing = "swing" );
1269
1270   /**
1271    * Set the interpolation manager used by the interpolate methods.
1272    */
1273   static void setInterpolationMgr(simgear::PropertyInterpolationMgr* mgr);
1274
1275   /**
1276    * Print the value of the property to a stream.
1277    */
1278   std::ostream& printOn(std::ostream& stream) const;
1279   
1280   //
1281   // Data binding.
1282   //
1283
1284
1285   /**
1286    * Test whether this node is bound to an external data source.
1287    */
1288   bool isTied () const { return _tied; }
1289
1290     /**
1291      * Bind this node to an external source.
1292      */
1293     template<typename T>
1294     bool tie(const SGRawValue<T> &rawValue, bool useDefault = true);
1295
1296   /**
1297    * Unbind this node from any external data source.
1298    */
1299   bool untie ();
1300
1301
1302   //
1303   // Convenience methods using paths.
1304   // TODO: add attribute methods
1305   //
1306
1307
1308   /**
1309    * Get another node's type.
1310    */
1311   simgear::props::Type getType (const char * relative_path) const;
1312
1313   /**
1314    * Get another node's type.
1315    */
1316   simgear::props::Type getType (const std::string& relative_path) const
1317   { return getType(relative_path.c_str()); }
1318
1319   /**
1320    * Test whether another node has a leaf value.
1321    */
1322   bool hasValue (const char * relative_path) const;
1323
1324   /**
1325    * Test whether another node has a leaf value.
1326    */
1327   bool hasValue (const std::string& relative_path) const
1328   { return hasValue(relative_path.c_str()); }
1329
1330   /**
1331    * Get another node's value as a bool.
1332    */
1333   bool getBoolValue (const char * relative_path,
1334                      bool defaultValue = false) const;
1335
1336   /**
1337    * Get another node's value as a bool.
1338    */
1339   bool getBoolValue (const std::string& relative_path,
1340                      bool defaultValue = false) const
1341   { return getBoolValue(relative_path.c_str(), defaultValue); }
1342
1343   /**
1344    * Get another node's value as an int.
1345    */
1346   int getIntValue (const char * relative_path,
1347                    int defaultValue = 0) const;
1348
1349   /**
1350    * Get another node's value as an int.
1351    */
1352   int getIntValue (const std::string& relative_path,
1353                    int defaultValue = 0) const
1354   { return getIntValue(relative_path.c_str(), defaultValue); }
1355
1356
1357   /**
1358    * Get another node's value as a long int.
1359    */
1360   long getLongValue (const char * relative_path,
1361                      long defaultValue = 0L) const;
1362
1363   /**
1364    * Get another node's value as a long int.
1365    */
1366   long getLongValue (const std::string& relative_path,
1367                      long defaultValue = 0L) const
1368   { return getLongValue(relative_path.c_str(), defaultValue); }
1369
1370   /**
1371    * Get another node's value as a float.
1372    */
1373   float getFloatValue (const char * relative_path,
1374                        float defaultValue = 0.0f) const;
1375
1376   /**
1377    * Get another node's value as a float.
1378    */
1379   float getFloatValue (const std::string& relative_path,
1380                        float defaultValue = 0.0f) const
1381   { return getFloatValue(relative_path.c_str(), defaultValue); }
1382
1383
1384   /**
1385    * Get another node's value as a double.
1386    */
1387   double getDoubleValue (const char * relative_path,
1388                          double defaultValue = 0.0) const;
1389
1390   /**
1391    * Get another node's value as a double.
1392    */
1393   double getDoubleValue (const std::string& relative_path,
1394                          double defaultValue = 0.0) const
1395   { return getDoubleValue(relative_path.c_str(), defaultValue); }
1396
1397   /**
1398    * Get another node's value as a string.
1399    */
1400   const char * getStringValue (const char * relative_path,
1401                                const char * defaultValue = "") const;
1402
1403
1404   /**
1405    * Get another node's value as a string.
1406    */
1407   const char * getStringValue (const std::string& relative_path,
1408                                const char * defaultValue = "") const
1409   { return getStringValue(relative_path.c_str(), defaultValue); }
1410
1411
1412   /**
1413    * Set another node's value as a bool.
1414    */
1415   bool setBoolValue (const char * relative_path, bool value);
1416
1417   /**
1418    * Set another node's value as a bool.
1419    */
1420   bool setBoolValue (const std::string& relative_path, bool value)
1421   { return setBoolValue(relative_path.c_str(), value); }
1422
1423
1424   /**
1425    * Set another node's value as an int.
1426    */
1427   bool setIntValue (const char * relative_path, int value);
1428
1429   /**
1430    * Set another node's value as an int.
1431    */
1432   bool setIntValue (const std::string& relative_path, int value)
1433   { return setIntValue(relative_path.c_str(), value); }
1434
1435
1436   /**
1437    * Set another node's value as a long int.
1438    */
1439   bool setLongValue (const char * relative_path, long value);
1440
1441   /**
1442    * Set another node's value as a long int.
1443    */
1444   bool setLongValue (const std::string& relative_path, long value)
1445   { return setLongValue(relative_path.c_str(), value); }
1446
1447
1448   /**
1449    * Set another node's value as a float.
1450    */
1451   bool setFloatValue (const char * relative_path, float value);
1452
1453   /**
1454    * Set another node's value as a float.
1455    */
1456   bool setFloatValue (const std::string& relative_path, float value)
1457   { return setFloatValue(relative_path.c_str(), value); }
1458
1459
1460   /**
1461    * Set another node's value as a double.
1462    */
1463   bool setDoubleValue (const char * relative_path, double value);
1464
1465   /**
1466    * Set another node's value as a double.
1467    */
1468   bool setDoubleValue (const std::string& relative_path, double value)
1469   { return setDoubleValue(relative_path.c_str(), value); }
1470
1471
1472   /**
1473    * Set another node's value as a string.
1474    */
1475   bool setStringValue (const char * relative_path, const char * value);
1476
1477   bool setStringValue(const char * relative_path, const std::string& value)
1478   { return setStringValue(relative_path, value.c_str()); }
1479   /**
1480    * Set another node's value as a string.
1481    */
1482   bool setStringValue (const std::string& relative_path, const char * value)
1483   { return setStringValue(relative_path.c_str(), value); }
1484
1485   bool setStringValue (const std::string& relative_path,
1486                        const std::string& value)
1487   { return setStringValue(relative_path.c_str(), value.c_str()); }
1488
1489   /**
1490    * Set another node's value with no specified type.
1491    */
1492   bool setUnspecifiedValue (const char * relative_path, const char * value);
1493
1494
1495   /**
1496    * Test whether another node is bound to an external data source.
1497    */
1498   bool isTied (const char * relative_path) const;
1499
1500   /**
1501    * Test whether another node is bound to an external data source.
1502    */
1503   bool isTied (const std::string& relative_path) const
1504   { return isTied(relative_path.c_str()); }
1505
1506   /**
1507    * Bind another node to an external bool source.
1508    */
1509   bool tie (const char * relative_path, const SGRawValue<bool> &rawValue,
1510             bool useDefault = true);
1511
1512   /**
1513    * Bind another node to an external bool source.
1514    */
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); }
1518
1519
1520   /**
1521    * Bind another node to an external int source.
1522    */
1523   bool tie (const char * relative_path, const SGRawValue<int> &rawValue,
1524             bool useDefault = true);
1525
1526   /**
1527    * Bind another node to an external int source.
1528    */
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); }
1532
1533
1534   /**
1535    * Bind another node to an external long int source.
1536    */
1537   bool tie (const char * relative_path, const SGRawValue<long> &rawValue,
1538             bool useDefault = true);
1539
1540   /**
1541    * Bind another node to an external long int source.
1542    */
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); }
1546
1547
1548   /**
1549    * Bind another node to an external float source.
1550    */
1551   bool tie (const char * relative_path, const SGRawValue<float> &rawValue,
1552             bool useDefault = true);
1553
1554   /**
1555    * Bind another node to an external float source.
1556    */
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); }
1560
1561
1562   /**
1563    * Bind another node to an external double source.
1564    */
1565   bool tie (const char * relative_path, const SGRawValue<double> &rawValue,
1566             bool useDefault = true);
1567
1568   /**
1569    * Bind another node to an external double source.
1570    */
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); }
1574
1575
1576   /**
1577    * Bind another node to an external string source.
1578    */
1579   bool tie (const char * relative_path, const SGRawValue<const char *> &rawValue,
1580             bool useDefault = true);
1581
1582   /**
1583    * Bind another node to an external string source.
1584    */
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); }
1588
1589
1590   /**
1591    * Unbind another node from any external data source.
1592    */
1593   bool untie (const char * relative_path);
1594
1595   /**
1596    * Unbind another node from any external data source.
1597    */
1598   bool untie (const std::string& relative_path)
1599   { return untie(relative_path.c_str()); }
1600
1601
1602   /**
1603    * Add a change listener to the property. If "initial" is set call the
1604    * listener initially.
1605    */
1606   void addChangeListener (SGPropertyChangeListener * listener,
1607                           bool initial = false);
1608
1609
1610   /**
1611    * Remove a change listener from the property.
1612    */
1613   void removeChangeListener (SGPropertyChangeListener * listener);
1614
1615
1616   /**
1617    * Get the number of listeners.
1618    */
1619   int nListeners () const { return _listeners ? (int)_listeners->size() : 0; }
1620
1621
1622   /**
1623    * Fire a value change event to all listeners.
1624    */
1625   void fireValueChanged ();
1626
1627
1628   /**
1629    * Fire a child-added event to all listeners.
1630    */
1631   void fireChildAdded (SGPropertyNode * child);
1632
1633   /**
1634    * Trigger a child-added and value-changed event for every child (Unlimited
1635    * depth).
1636    *
1637    * @param fire_self   Whether to trigger the events also for the node itself.
1638    *
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.
1641    */
1642   void fireCreatedRecursive(bool fire_self = false);
1643
1644   /**
1645    * Fire a child-removed event to all listeners.
1646    */
1647   void fireChildRemoved (SGPropertyNode * child);
1648
1649   /**
1650    * Fire a child-removed event for every child of this node (Unlimited depth)
1651    *
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.
1655    */
1656   void fireChildrenRemovedRecursive();
1657
1658
1659   /**
1660    * Clear any existing value and set the type to NONE.
1661    */
1662   void clearValue ();
1663
1664   /**
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.
1670    *
1671    * Attributes, removed children, and aliases aren't considered.
1672    */
1673   static bool compare (const SGPropertyNode& lhs, const SGPropertyNode& rhs);
1674
1675 protected:
1676
1677   void fireValueChanged (SGPropertyNode * node);
1678   void fireChildAdded (SGPropertyNode * parent, SGPropertyNode * child);
1679   void fireChildRemoved (SGPropertyNode * parent, SGPropertyNode * child);
1680
1681   /**
1682    * Protected constructor for making new nodes on demand.
1683    */
1684   SGPropertyNode (const std::string& name, int index, SGPropertyNode * parent);
1685   template<typename Itr>
1686   SGPropertyNode (Itr begin, Itr end, int index, SGPropertyNode * parent);
1687
1688   static simgear::PropertyInterpolationMgr* _interpolation_mgr;
1689
1690 private:
1691
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;
1699
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);
1707
1708
1709   /**
1710    * Get the value as a string.
1711    */
1712   const char * make_string () const;
1713
1714   /**
1715    * Trace a read access.
1716    */
1717   void trace_read () const;
1718
1719
1720   /**
1721    * Trace a write access.
1722    */
1723   void trace_write () const;
1724
1725   int _index;
1726   std::string _name;
1727   /// To avoid cyclic reference counting loops this shall not be a reference
1728   /// counted pointer
1729   SGPropertyNode * _parent;
1730   simgear::PropertyList _children;
1731   simgear::PropertyList _removedChildren;
1732   mutable std::string _buffer;
1733   simgear::props::Type _type;
1734   bool _tied;
1735   int _attr;
1736
1737   // The right kind of pointer...
1738   union {
1739     SGPropertyNode * alias;
1740     SGRaw* val;
1741   } _value;
1742
1743   union {
1744     bool bool_val;
1745     int int_val;
1746     long long_val;
1747     float float_val;
1748     double double_val;
1749     char * string_val;
1750   } _local_val;
1751
1752   std::vector<SGPropertyChangeListener *> * _listeners;
1753
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);
1764   // For boost
1765   friend size_t hash_value(const SGPropertyNode& node);
1766 };
1767
1768 // Convenience functions for use in templates
1769 template<typename T>
1770 T getValue(const SGPropertyNode*);
1771
1772 template<>
1773 inline bool getValue<bool>(const SGPropertyNode* node) { return node->getBoolValue(); }
1774
1775 template<>
1776 inline int getValue<int>(const SGPropertyNode* node) { return node->getIntValue(); }
1777
1778 template<>
1779 inline long getValue<long>(const SGPropertyNode* node) { return node->getLongValue(); }
1780
1781 template<>
1782 inline float getValue<float>(const SGPropertyNode* node)
1783 {
1784     return node->getFloatValue();
1785 }
1786
1787 template<>
1788 inline double getValue<double>(const SGPropertyNode* node)
1789 {
1790     return node->getDoubleValue();
1791 }
1792
1793 template<>
1794 inline const char * getValue<const char*>(const SGPropertyNode* node)
1795 {
1796     return node->getStringValue ();
1797 }
1798
1799 inline bool setValue(SGPropertyNode* node, bool value)
1800 {
1801     return node->setBoolValue(value);
1802 }
1803
1804 inline bool setValue(SGPropertyNode* node, int value)
1805 {
1806     return node->setIntValue(value);
1807 }
1808
1809 inline bool setValue(SGPropertyNode* node, long value)
1810 {
1811     return node->setLongValue(value);
1812 }
1813
1814 inline bool setValue(SGPropertyNode* node, float value)
1815 {
1816     return node->setFloatValue(value);
1817 }
1818
1819 inline bool setValue(SGPropertyNode* node, double value)
1820 {
1821     return node->setDoubleValue(value);
1822 }
1823
1824 inline bool setValue(SGPropertyNode* node, const char* value)
1825 {
1826     return node->setStringValue(value);
1827 }
1828
1829 inline bool setValue (SGPropertyNode* node, const std::string& value)
1830 {
1831     return node->setStringValue(value.c_str());
1832 }
1833
1834 template<typename T>
1835 bool SGPropertyNode::tie(const SGRawValue<T> &rawValue, bool useDefault)
1836 {
1837     using namespace simgear::props;
1838     if (_type == ALIAS || _tied)
1839         return false;
1840
1841     useDefault = useDefault && hasValue();
1842     T old_val = SGRawValue<T>::DefaultValue();
1843     if (useDefault)
1844         old_val = getValue<T>(this);
1845     clearValue();
1846     if (PropertyTraits<T>::Internal)
1847         _type = PropertyTraits<T>::type_tag;
1848     else
1849         _type = EXTENDED;
1850     _tied = true;
1851     _value.val = rawValue.clone();
1852     if (useDefault) {
1853         int save_attributes = getAttributes();
1854         setAttribute( WRITE, true );
1855         setValue(old_val);
1856         setAttributes( save_attributes );
1857     }
1858     return true;
1859 }
1860
1861 template<>
1862 bool SGPropertyNode::tie (const SGRawValue<const char *> &rawValue,
1863                           bool useDefault);
1864
1865 template<typename T>
1866 T SGPropertyNode::getValue(typename boost::disable_if_c<simgear::props
1867                            ::PropertyTraits<T>::Internal>::type* dummy) const
1868 {
1869     using namespace simgear::props;
1870     if (_attr == (READ|WRITE) && _type == EXTENDED
1871         && _value.val->getType() == PropertyTraits<T>::type_tag) {
1872         return static_cast<SGRawValue<T>*>(_value.val)->getValue();
1873     }
1874     if (getAttribute(TRACE_READ))
1875         trace_read();
1876     if (!getAttribute(READ))
1877       return SGRawValue<T>::DefaultValue();
1878     switch (_type) {
1879     case EXTENDED:
1880         if (_value.val->getType() == PropertyTraits<T>::type_tag)
1881             return static_cast<SGRawValue<T>*>(_value.val)->getValue();
1882         break;
1883     case STRING:
1884     case UNSPECIFIED:
1885         return simgear::parseString<T>(make_string());
1886         break;
1887     default: // avoid compiler warning
1888         break;
1889     }
1890     return SGRawValue<T>::DefaultValue();
1891 }
1892
1893 template<typename T>
1894 inline T SGPropertyNode::getValue(typename boost::enable_if_c<simgear::props
1895                                   ::PropertyTraits<T>::Internal>::type* dummy) const
1896 {
1897   return ::getValue<T>(this);
1898 }
1899
1900 template<typename T, typename T_get /* = T */> // TODO use C++11 or traits
1901 std::vector<T> SGPropertyNode::getChildValues(const std::string& name) const
1902 {
1903   const simgear::PropertyList& props = getChildren(name);
1904   std::vector<T> values( props.size() );
1905
1906   for( size_t i = 0; i < props.size(); ++i )
1907     values[i] = props[i]->getValue<T_get>();
1908
1909   return values;
1910 }
1911
1912 template<typename T>
1913 inline
1914 std::vector<T> SGPropertyNode::getChildValues(const std::string& name) const
1915 {
1916   return getChildValues<T, T>(name);
1917 }
1918
1919 template<typename T>
1920 bool SGPropertyNode::setValue(const T& val,
1921                               typename boost::disable_if_c<simgear::props
1922                               ::PropertyTraits<T>::Internal>::type* dummy)
1923 {
1924     using namespace simgear::props;
1925     if (_attr == (READ|WRITE) && _type == EXTENDED
1926         && _value.val->getType() == PropertyTraits<T>::type_tag) {
1927         static_cast<SGRawValue<T>*>(_value.val)->setValue(val);
1928         return true;
1929     }
1930     if (getAttribute(WRITE)
1931         && ((_type == EXTENDED
1932             && _value.val->getType() == PropertyTraits<T>::type_tag)
1933             || _type == NONE || _type == UNSPECIFIED)) {
1934         if (_type == NONE || _type == UNSPECIFIED) {
1935             clearValue();
1936             _type = EXTENDED;
1937             _value.val = new SGRawValueContainer<T>(val);
1938         } else {
1939             static_cast<SGRawValue<T>*>(_value.val)->setValue(val);
1940         }
1941         if (getAttribute(TRACE_WRITE))
1942             trace_write();
1943         return true;
1944     }
1945     return false;
1946 }
1947
1948 template<typename T>
1949 inline bool SGPropertyNode::setValue(const T& val,
1950                                      typename boost::enable_if_c<simgear::props
1951                                      ::PropertyTraits<T>::Internal>::type* dummy)
1952 {
1953   return ::setValue(this, val);
1954 }
1955
1956 /**
1957  * Utility function for creation of a child property node.
1958  */
1959 inline SGPropertyNode* makeChild(SGPropertyNode* parent, const char* name,
1960                                  int index = 0)
1961 {
1962     return parent->getChild(name, index, true);
1963 }
1964
1965 /**
1966  * Utility function for creation of a child property node using a
1967  * relative path.
1968  */
1969 namespace simgear
1970 {
1971 template<typename StringType>
1972 inline SGPropertyNode* makeNode(SGPropertyNode* parent, const StringType& name)
1973 {
1974     return parent->getNode(name, true);
1975 }
1976 }
1977
1978 // For boost::hash
1979 size_t hash_value(const SGPropertyNode& node);
1980
1981 // Helper comparison and hash functions for common cases
1982
1983 namespace simgear
1984 {
1985 namespace props
1986 {
1987 struct Compare
1988 {
1989     bool operator()(const SGPropertyNode* lhs, const SGPropertyNode* rhs) const
1990     {
1991         return SGPropertyNode::compare(*lhs, *rhs);
1992     }
1993     bool operator()(SGPropertyNode_ptr lhs, const SGPropertyNode* rhs) const
1994     {
1995         return SGPropertyNode::compare(*lhs, *rhs);
1996     }
1997     bool operator()(const SGPropertyNode* lhs, SGPropertyNode_ptr rhs) const
1998     {
1999         return SGPropertyNode::compare(*lhs, *rhs);
2000     }
2001     bool operator()(SGPropertyNode_ptr lhs, SGPropertyNode_ptr rhs) const
2002     {
2003         return SGPropertyNode::compare(*lhs, *rhs);
2004     }
2005 };
2006
2007 struct Hash
2008 {
2009     size_t operator()(const SGPropertyNode* node) const
2010     {
2011         return hash_value(*node);
2012     }
2013     size_t operator()(SGPropertyNode_ptr node) const
2014     {
2015         return hash_value(*node);
2016     }
2017 };
2018 }
2019 }
2020
2021 /** Convenience class for change listener callbacks without
2022  * creating a derived class implementing a "valueChanged" method.
2023  * Also removes listener on destruction automatically.
2024  */
2025 template<class T>
2026 class SGPropertyChangeCallback
2027     : public SGPropertyChangeListener
2028 {
2029 public:
2030     SGPropertyChangeCallback(T* obj, void (T::*method)(SGPropertyNode*),
2031                              SGPropertyNode_ptr property,bool initial=false)
2032         : _obj(obj), _callback(method), _property(property)
2033     {
2034         _property->addChangeListener(this,initial);
2035     }
2036     virtual ~SGPropertyChangeCallback()
2037     {
2038         _property->removeChangeListener(this);
2039     }
2040     void valueChanged (SGPropertyNode * node)
2041     {
2042         (_obj->*_callback)(node);
2043     }
2044 private:
2045     T* _obj;
2046     void (T::*_callback)(SGPropertyNode*);
2047     SGPropertyNode_ptr _property;
2048 };
2049
2050 #endif // __PROPS_HXX
2051
2052 // end of props.hxx