]> git.mxchange.org Git - simgear.git/blob - simgear/props/props.hxx
Lots of (mostly) doxygen fixes/cleanup.
[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 #include <typeinfo>
24
25 #include <boost/utility.hpp>
26 #include <boost/type_traits/is_enum.hpp>
27
28 #if PROPS_STANDALONE
29 #else
30 #include <simgear/compiler.h>
31 #include <simgear/debug/logstream.hxx>
32 #endif
33
34
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>
39
40 // XXX This whole file should be in the simgear namespace, but I don't
41 // have the guts yet...
42
43 namespace simgear
44 {
45
46   class PropertyInterpolationMgr;
47
48 template<typename T>
49 std::istream& readFrom(std::istream& stream, T& result)
50 {
51     stream >> result;
52     return stream;
53 }
54
55 /**
56  * Parse a string as an object of a given type.
57  * XXX no error behavior yet.
58  *
59  * @tparam T the return type
60  * @param str the string
61  * @return the object.
62  */
63 template<typename T>
64 inline T parseString(const std::string& str)
65 {
66     std::istringstream stream(str);
67     T result;
68     readFrom(stream, result);
69     return result;
70 }
71
72 /**
73  * Property value types.
74  */
75
76 #ifdef NONE
77 #pragma warn A sloppy coder has defined NONE as a macro!
78 #undef NONE
79 #endif
80
81 #ifdef ALIAS
82 #pragma warn A sloppy coder has defined ALIAS as a macro!
83 #undef ALIAS
84 #endif
85
86 #ifdef UNSPECIFIED
87 #pragma warn A sloppy coder has defined UNSPECIFIED as a macro!
88 #undef UNSPECIFIED
89 #endif
90
91 #ifdef BOOL
92 #pragma warn A sloppy coder has defined BOOL as a macro!
93 #undef BOOL
94 #endif
95
96 #ifdef INT
97 #pragma warn A sloppy coder has defined INT as a macro!
98 #undef INT
99 #endif
100
101 #ifdef LONG
102 #pragma warn A sloppy coder has defined LONG as a macro!
103 #undef LONG
104 #endif
105
106 #ifdef FLOAT
107 #pragma warn A sloppy coder has defined FLOAT as a macro!
108 #undef FLOAT
109 #endif
110
111 #ifdef DOUBLE
112 #pragma warn A sloppy coder has defined DOUBLE as a macro!
113 #undef DOUBLE
114 #endif
115
116 #ifdef STRING
117 #pragma warn A sloppy coder has defined STRING as a macro!
118 #undef STRING
119 #endif
120
121 namespace props
122 {
123 /**
124  * The possible types of an SGPropertyNode. Types that appear after
125  * EXTENDED are not stored in the SGPropertyNode itself.
126  */
127 enum Type {
128     NONE = 0, /**< The node hasn't been assigned a value yet. */
129     ALIAS, /**< The node "points" to another node. */
130     BOOL,
131     INT,
132     LONG,
133     FLOAT,
134     DOUBLE,
135     STRING,
136     UNSPECIFIED,
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.
141                */
142     // Extended properties
143     VEC3D,
144     VEC4D
145 };
146
147 template<typename T> struct PropertyTraits;
148
149 #define DEFINTERNALPROP(TYPE, PROP) \
150 template<> \
151 struct PropertyTraits<TYPE> \
152 { \
153     static const Type type_tag = PROP; \
154     enum  { Internal = 1 }; \
155 }
156
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
165
166 }
167 }
168
169
170
171 ////////////////////////////////////////////////////////////////////////
172 // A raw value.
173 //
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.
177 //
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 ////////////////////////////////////////////////////////////////////////
182
183 /**
184  * Base class for SGRawValue classes that holds no type
185  * information. This allows some generic manipulation of the
186  * SGRawValue object.
187  */
188 class SGRaw
189 {
190 public:
191     /**
192      * Get the type enumeration for the raw value.
193      *
194      * @return the type.
195      */
196     virtual simgear::props::Type getType() const = 0;
197     virtual ~SGRaw() {}
198     
199     /**
200      * Create a new deep copy of this raw value.
201      *
202      * The copy will contain its own version of the underlying value
203      * as well, and will be the same type.
204      *
205      * @return A deep copy of the current object.
206      */
207     virtual SGRaw* clone() const = 0;
208
209 };
210
211 class SGRawExtended : public SGRaw
212 {
213 public:
214     /**    
215      * Make an SGRawValueContainer from the SGRawValue.
216      *
217      * This is a virtual function of SGRawExtended so that
218      * SGPropertyNode::untie doesn't need to know the type of an
219      * extended property.
220      */
221     virtual SGRawExtended* makeContainer() const = 0;
222     /**
223      * Write value out to a stream
224      */
225     virtual std::ostream& printOn(std::ostream& stream) const = 0;
226     /**
227      * Read value from a stream and store it.
228      */
229     virtual std::istream& readFrom(std::istream& stream) = 0;
230 };
231
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>
237 class SGRawBase;
238
239 template<typename T>
240 class SGRawBase<T, 1> : public SGRaw
241 {
242 };
243
244 template<typename T>
245 class SGRawBase<T, 0> : public SGRawExtended
246 {
247     virtual SGRawExtended* makeContainer() const;
248     virtual std::ostream& printOn(std::ostream& stream) const;
249     virtual std::istream& readFrom(std::istream& stream);
250 };
251
252 /**
253  * Abstract base class for a raw value.
254  *
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.
261  *
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.
269  *
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.
273  *
274  * All derived SGRawValue classes must implement getValue(), setValue(), and
275  * {@link SGRaw::clone clone()} for the appropriate type.
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
698   /// Called if value of \a node has changed.
699   virtual void valueChanged(SGPropertyNode * node);
700
701   /// Called if \a child has been added to the given \a parent.
702   virtual void childAdded(SGPropertyNode * parent, SGPropertyNode * child);
703
704   /// Called if \a child has been removed from its \a parent.
705   virtual void childRemoved(SGPropertyNode * parent, SGPropertyNode * child);
706
707 protected:
708   friend class SGPropertyNode;
709   virtual void register_property (SGPropertyNode * node);
710   virtual void unregister_property (SGPropertyNode * node);
711
712 private:
713   std::vector<SGPropertyNode *> _properties;
714 };
715
716
717 /**
718  * A node in a property tree.
719  */
720 class SGPropertyNode : public SGReferenced
721 {
722 public:
723
724   /**
725    * Public constants.
726    */
727   enum {
728     MAX_STRING_LEN = 1024
729   };
730
731   /**
732    * Access mode attributes.
733    *
734    * <p>The ARCHIVE attribute is strictly advisory, and controls
735    * whether the property should normally be saved and restored.</p>
736    */
737   enum Attribute {
738     NO_ATTR = 0,
739     READ = 1,
740     WRITE = 2,
741     ARCHIVE = 4,
742     REMOVED = 8,
743     TRACE_READ = 16,
744     TRACE_WRITE = 32,
745     USERARCHIVE = 64,
746     PRESERVE = 128
747     // beware: if you add another attribute here,
748     // also update value of "LAST_USED_ATTRIBUTE".
749   };
750
751
752   /**
753    * Last used attribute
754    * Update as needed when enum Attribute is changed
755    */
756   static const int LAST_USED_ATTRIBUTE;
757
758   /**
759    * Default constructor.
760    */
761   SGPropertyNode ();
762
763
764   /**
765    * Copy constructor.
766    */
767   SGPropertyNode (const SGPropertyNode &node);
768
769
770   /**
771    * Destructor.
772    */
773   virtual ~SGPropertyNode ();
774
775
776
777   //
778   // Basic properties.
779   //
780
781   /**
782    * Test whether this node contains a primitive leaf value.
783    */
784     bool hasValue () const { return (_type != simgear::props::NONE); }
785
786
787   /**
788    * Get the node's simple (XML) name.
789    */
790   const char * getName () const { return _name.c_str(); }
791
792   /**
793    * Get the node's simple name as a string.
794    */
795   const std::string& getNameString () const { return _name; }
796
797   /**
798    * Get the node's pretty display name, with subscript when needed.
799    */
800     std::string getDisplayName (bool simplify = false) const;
801
802
803   /**
804    * Get the node's integer index.
805    */
806   int getIndex () const { return _index; }
807
808
809   /**
810    * Get a non-const pointer to the node's parent.
811    */
812   SGPropertyNode * getParent () { return _parent; }
813
814
815   /**
816    * Get a const pointer to the node's parent.
817    */
818   const SGPropertyNode * getParent () const { return _parent; }
819
820
821   //
822   // Children.
823   //
824
825
826   /**
827    * Get the number of child nodes.
828    */
829   int nChildren () const { return (int)_children.size(); }
830
831
832   /**
833    * Get a child node by position (*NOT* index).
834    */
835   SGPropertyNode * getChild (int position);
836
837
838   /**
839    * Get a const child node by position (*NOT* index).
840    */
841   const SGPropertyNode * getChild (int position) const;
842
843
844   /**
845    * Test whether a named child exists.
846    */
847   bool hasChild (const char * name, int index = 0) const
848   {
849     return (getChild(name, index) != 0);
850   }
851
852   /**
853    * Test whether a named child exists.
854    */
855   bool hasChild (const std::string& name, int index = 0) const
856   {
857     return (getChild(name, index) != 0);
858   }
859
860   /**
861    * Create a new child node with the given name and an unused index
862    *
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
866    */
867   SGPropertyNode * addChild ( const char* name,
868                               int min_index = 0,
869                               bool append = true );
870   SGPropertyNode * addChild ( const std::string& name,
871                               int min_index = 0,
872                               bool append = true )
873   { return addChild(name.c_str(), min_index, append); }
874
875   /**
876    * Create multiple child nodes with the given name an unused indices
877    *
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
882    */
883   simgear::PropertyList addChildren ( const std::string& name,
884                                       size_t count,
885                                       int min_index = 0,
886                                       bool append = true );
887
888   /**
889    * Get a child node by name and index.
890    */
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);
895   /**
896    * Get a const child node by name and index.
897    */
898   const SGPropertyNode * getChild (const char * name, int index = 0) const;
899
900   /**
901    * Get a const child node by name and index.
902    */
903   const SGPropertyNode * getChild (const std::string& name, int index = 0) const
904   { return getChild(name.c_str(), index); }
905
906
907   /**
908    * Get a vector of all children with the specified name.
909    */
910   simgear::PropertyList getChildren (const char * name) const;
911
912   /**
913    * Get a vector of all children with the specified name.
914    */
915   simgear::PropertyList getChildren (const std::string& name) const
916   { return getChildren(name.c_str()); }
917
918   /**
919    * Remove child by pointer (if it is a child of this node).
920    *
921    * @return true, if the node was deleted.
922    */
923   bool removeChild(SGPropertyNode* node);
924
925   // TODO do we need the removeXXX methods to return the deleted nodes?
926   /**
927    * Remove child by position.
928    */
929   SGPropertyNode_ptr removeChild(int pos);
930
931
932   /**
933    * Remove a child node
934    */
935   SGPropertyNode_ptr removeChild(const char * name, int index = 0);
936
937   /**
938    * Remove a child node
939    */
940   SGPropertyNode_ptr removeChild(const std::string& name, int index = 0)
941   { return removeChild(name.c_str(), index); }
942
943   /**
944    * Remove all children with the specified name.
945    */
946   simgear::PropertyList removeChildren(const char * name);
947
948   /**
949    * Remove all children with the specified name.
950    */
951   simgear::PropertyList removeChildren(const std::string& name)
952   { return removeChildren(name.c_str()); }
953
954   /**
955    * Remove all children (does not change the value of the node)
956    */
957   void removeAllChildren();
958
959   //
960   // Alias support.
961   //
962
963
964   /**
965    * Alias this node's leaf value to another's.
966    */
967   bool alias (SGPropertyNode * target);
968
969
970   /**
971    * Alias this node's leaf value to another's by relative path.
972    */
973   bool alias (const char * path);
974
975   /**
976    * Alias this node's leaf value to another's by relative path.
977    */
978   bool alias (const std::string& path)
979   { return alias(path.c_str()); }
980
981
982   /**
983    * Remove any alias for this node.
984    */
985   bool unalias ();
986
987
988   /**
989    * Test whether the node's leaf value is aliased to another's.
990    */
991   bool isAlias () const { return (_type == simgear::props::ALIAS); }
992
993
994   /**
995    * Get a non-const pointer to the current alias target, if any.
996    */
997   SGPropertyNode * getAliasTarget ();
998
999
1000   /**
1001    * Get a const pointer to the current alias target, if any.
1002    */
1003   const SGPropertyNode * getAliasTarget () const;
1004
1005
1006   //
1007   // Path information.
1008   //
1009
1010
1011   /**
1012    * Get the path to this node from the root.
1013    */
1014   std::string getPath (bool simplify = false) const;
1015
1016
1017   /**
1018    * Get a pointer to the root node.
1019    */
1020   SGPropertyNode * getRootNode ();
1021
1022
1023   /**
1024    * Get a const pointer to the root node.
1025    */
1026   const SGPropertyNode * getRootNode () const;
1027
1028
1029   /**
1030    * Get a pointer to another node by relative path.
1031    */
1032   SGPropertyNode * getNode (const char * relative_path, bool create = false);
1033
1034   /**
1035    * Get a pointer to another node by relative path.
1036    */
1037   SGPropertyNode * getNode (const std::string& relative_path, bool create = false)
1038   { return getNode(relative_path.c_str(), create); }
1039
1040   /**
1041    * Get a pointer to another node by relative path.
1042    *
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
1048    * component.
1049    */
1050   SGPropertyNode * getNode (const char * relative_path, int index,
1051                             bool create = false);
1052
1053   /**
1054    * Get a pointer to another node by relative path.
1055    *
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
1061    * component.
1062    */
1063   SGPropertyNode * getNode (const std::string& relative_path, int index,
1064                             bool create = false)
1065   { return getNode(relative_path.c_str(), index, create); }
1066
1067   /**
1068    * Get a const pointer to another node by relative path.
1069    */
1070   const SGPropertyNode * getNode (const char * relative_path) const;
1071
1072   /**
1073    * Get a const pointer to another node by relative path.
1074    */
1075   const SGPropertyNode * getNode (const std::string& relative_path) const
1076   { return getNode(relative_path.c_str()); }
1077
1078
1079   /**
1080    * Get a const pointer to another node by relative path.
1081    *
1082    * This method leaves the index off the last member of the path,
1083    * so that the user can specify it separate.
1084    */
1085   const SGPropertyNode * getNode (const char * relative_path,
1086                                   int index) const;
1087
1088   /**
1089    * Get a const pointer to another node by relative path.
1090    *
1091    * This method leaves the index off the last member of the path,
1092    * so that the user can specify it separate.
1093    */
1094   const SGPropertyNode * getNode (const std::string& relative_path,
1095                                   int index) const
1096   { return getNode(relative_path.c_str(), index); }
1097
1098   //
1099   // Access Mode.
1100   //
1101
1102   /**
1103    * Check a single mode attribute for the property node.
1104    */
1105   bool getAttribute (Attribute attr) const { return ((_attr & attr) != 0); }
1106
1107
1108   /**
1109    * Set a single mode attribute for the property node.
1110    */
1111   void setAttribute (Attribute attr, bool state) {
1112     (state ? _attr |= attr : _attr &= ~attr);
1113   }
1114
1115
1116   /**
1117    * Get all of the mode attributes for the property node.
1118    */
1119   int getAttributes () const { return _attr; }
1120
1121
1122   /**
1123    * Set all of the mode attributes for the property node.
1124    */
1125   void setAttributes (int attr) { _attr = attr; }
1126   
1127
1128   //
1129   // Leaf Value (primitive).
1130   //
1131
1132
1133   /**
1134    * Get the type of leaf value, if any, for this node.
1135    */
1136   simgear::props::Type getType () const;
1137
1138
1139   /**
1140    * Get a bool value for this node.
1141    */
1142   bool getBoolValue () const;
1143
1144
1145   /**
1146    * Get an int value for this node.
1147    */
1148   int getIntValue () const;
1149
1150
1151   /**
1152    * Get a long int value for this node.
1153    */
1154   long getLongValue () const;
1155
1156
1157   /**
1158    * Get a float value for this node.
1159    */
1160   float getFloatValue () const;
1161
1162
1163   /**
1164    * Get a double value for this node.
1165    */
1166   double getDoubleValue () const;
1167
1168
1169   /**
1170    * Get a string value for this node.
1171    */
1172   const char * getStringValue () const;
1173
1174   /**
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.
1177    */
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;
1185
1186   /**
1187    * Get a list of values from all children with the given name
1188    */
1189   template<typename T, typename T_get /* = T */> // TODO use C++11 or traits
1190   std::vector<T> getChildValues(const std::string& name) const;
1191
1192   /**
1193    * Get a list of values from all children with the given name
1194    */
1195   template<typename T>
1196   std::vector<T> getChildValues(const std::string& name) const;
1197
1198   /**
1199    * Set a bool value for this node.
1200    */
1201   bool setBoolValue (bool value);
1202
1203
1204   /**
1205    * Set an int value for this node.
1206    */
1207   bool setIntValue (int value);
1208
1209
1210   /**
1211    * Set a long int value for this node.
1212    */
1213   bool setLongValue (long value);
1214
1215
1216   /**
1217    * Set a float value for this node.
1218    */
1219   bool setFloatValue (float value);
1220
1221
1222   /**
1223    * Set a double value for this node.
1224    */
1225   bool setDoubleValue (double value);
1226
1227
1228   /**
1229    * Set a string value for this node.
1230    */
1231   bool setStringValue (const char * value);
1232
1233   /**
1234    * Set a string value for this node.
1235    */
1236   bool setStringValue (const std::string& value)
1237   { return setStringValue(value.c_str()); }
1238
1239
1240   /**
1241    * Set a value of unspecified type for this node.
1242    */
1243   bool setUnspecifiedValue (const char * value);
1244
1245   template<typename T>
1246   bool setValue(const T& val,
1247                 typename boost::enable_if_c<simgear::props::PropertyTraits<T>::Internal>
1248                 ::type* dummy = 0);
1249
1250   template<typename T>
1251   bool setValue(const T& val,
1252                 typename boost::disable_if_c<simgear::props::PropertyTraits<T>::Internal>
1253                 ::type* dummy = 0);
1254
1255   template<int N>
1256   bool setValue(const char (&val)[N])
1257   {
1258     return setValue(&val[0]);
1259   }
1260   
1261   /**
1262    * Set relative node to given value and afterwards make read only.
1263    *
1264    * @param relative_path   Path to node
1265    * @param value           Value to set
1266    *
1267    * @return whether value could be set
1268    */
1269   template<typename T>
1270   bool setValueReadOnly(const std::string& relative_path, const T& value)
1271   {
1272     SGPropertyNode* node = getNode(relative_path, true);
1273     bool ret = node->setValue(value);
1274     node->setAttributes(READ);
1275     return ret;
1276   }
1277
1278   /**
1279    * Interpolate current value to target value within given time.
1280    *
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/)
1285    */
1286   bool interpolate( const std::string& type,
1287                     const SGPropertyNode& target,
1288                     double duration = 0.6,
1289                     const std::string& easing = "swing" );
1290
1291   /**
1292    * Interpolate current value to a series of values within given durations.
1293    *
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/)
1298    */
1299   bool interpolate( const std::string& type,
1300                     const simgear::PropertyList& values,
1301                     const double_list& deltas,
1302                     const std::string& easing = "swing" );
1303
1304   /**
1305    * Set the interpolation manager used by the interpolate methods.
1306    */
1307   static void setInterpolationMgr(simgear::PropertyInterpolationMgr* mgr);
1308
1309   /**
1310    * Get the interpolation manager
1311    */
1312   static simgear::PropertyInterpolationMgr* getInterpolationMgr();
1313
1314   /**
1315    * Print the value of the property to a stream.
1316    */
1317   std::ostream& printOn(std::ostream& stream) const;
1318   
1319   //
1320   // Data binding.
1321   //
1322
1323
1324   /**
1325    * Test whether this node is bound to an external data source.
1326    */
1327   bool isTied () const { return _tied; }
1328
1329     /**
1330      * Bind this node to an external source.
1331      */
1332     template<typename T>
1333     bool tie(const SGRawValue<T> &rawValue, bool useDefault = true);
1334
1335   /**
1336    * Unbind this node from any external data source.
1337    */
1338   bool untie ();
1339
1340
1341   //
1342   // Convenience methods using paths.
1343   // TODO: add attribute methods
1344   //
1345
1346
1347   /**
1348    * Get another node's type.
1349    */
1350   simgear::props::Type getType (const char * relative_path) const;
1351
1352   /**
1353    * Get another node's type.
1354    */
1355   simgear::props::Type getType (const std::string& relative_path) const
1356   { return getType(relative_path.c_str()); }
1357
1358   /**
1359    * Test whether another node has a leaf value.
1360    */
1361   bool hasValue (const char * relative_path) const;
1362
1363   /**
1364    * Test whether another node has a leaf value.
1365    */
1366   bool hasValue (const std::string& relative_path) const
1367   { return hasValue(relative_path.c_str()); }
1368
1369   /**
1370    * Get another node's value as a bool.
1371    */
1372   bool getBoolValue (const char * relative_path,
1373                      bool defaultValue = false) const;
1374
1375   /**
1376    * Get another node's value as a bool.
1377    */
1378   bool getBoolValue (const std::string& relative_path,
1379                      bool defaultValue = false) const
1380   { return getBoolValue(relative_path.c_str(), defaultValue); }
1381
1382   /**
1383    * Get another node's value as an int.
1384    */
1385   int getIntValue (const char * relative_path,
1386                    int defaultValue = 0) const;
1387
1388   /**
1389    * Get another node's value as an int.
1390    */
1391   int getIntValue (const std::string& relative_path,
1392                    int defaultValue = 0) const
1393   { return getIntValue(relative_path.c_str(), defaultValue); }
1394
1395
1396   /**
1397    * Get another node's value as a long int.
1398    */
1399   long getLongValue (const char * relative_path,
1400                      long defaultValue = 0L) const;
1401
1402   /**
1403    * Get another node's value as a long int.
1404    */
1405   long getLongValue (const std::string& relative_path,
1406                      long defaultValue = 0L) const
1407   { return getLongValue(relative_path.c_str(), defaultValue); }
1408
1409   /**
1410    * Get another node's value as a float.
1411    */
1412   float getFloatValue (const char * relative_path,
1413                        float defaultValue = 0.0f) const;
1414
1415   /**
1416    * Get another node's value as a float.
1417    */
1418   float getFloatValue (const std::string& relative_path,
1419                        float defaultValue = 0.0f) const
1420   { return getFloatValue(relative_path.c_str(), defaultValue); }
1421
1422
1423   /**
1424    * Get another node's value as a double.
1425    */
1426   double getDoubleValue (const char * relative_path,
1427                          double defaultValue = 0.0) const;
1428
1429   /**
1430    * Get another node's value as a double.
1431    */
1432   double getDoubleValue (const std::string& relative_path,
1433                          double defaultValue = 0.0) const
1434   { return getDoubleValue(relative_path.c_str(), defaultValue); }
1435
1436   /**
1437    * Get another node's value as a string.
1438    */
1439   const char * getStringValue (const char * relative_path,
1440                                const char * defaultValue = "") const;
1441
1442
1443   /**
1444    * Get another node's value as a string.
1445    */
1446   const char * getStringValue (const std::string& relative_path,
1447                                const char * defaultValue = "") const
1448   { return getStringValue(relative_path.c_str(), defaultValue); }
1449
1450
1451   /**
1452    * Set another node's value as a bool.
1453    */
1454   bool setBoolValue (const char * relative_path, bool value);
1455
1456   /**
1457    * Set another node's value as a bool.
1458    */
1459   bool setBoolValue (const std::string& relative_path, bool value)
1460   { return setBoolValue(relative_path.c_str(), value); }
1461
1462
1463   /**
1464    * Set another node's value as an int.
1465    */
1466   bool setIntValue (const char * relative_path, int value);
1467
1468   /**
1469    * Set another node's value as an int.
1470    */
1471   bool setIntValue (const std::string& relative_path, int value)
1472   { return setIntValue(relative_path.c_str(), value); }
1473
1474
1475   /**
1476    * Set another node's value as a long int.
1477    */
1478   bool setLongValue (const char * relative_path, long value);
1479
1480   /**
1481    * Set another node's value as a long int.
1482    */
1483   bool setLongValue (const std::string& relative_path, long value)
1484   { return setLongValue(relative_path.c_str(), value); }
1485
1486
1487   /**
1488    * Set another node's value as a float.
1489    */
1490   bool setFloatValue (const char * relative_path, float value);
1491
1492   /**
1493    * Set another node's value as a float.
1494    */
1495   bool setFloatValue (const std::string& relative_path, float value)
1496   { return setFloatValue(relative_path.c_str(), value); }
1497
1498
1499   /**
1500    * Set another node's value as a double.
1501    */
1502   bool setDoubleValue (const char * relative_path, double value);
1503
1504   /**
1505    * Set another node's value as a double.
1506    */
1507   bool setDoubleValue (const std::string& relative_path, double value)
1508   { return setDoubleValue(relative_path.c_str(), value); }
1509
1510
1511   /**
1512    * Set another node's value as a string.
1513    */
1514   bool setStringValue (const char * relative_path, const char * value);
1515
1516   bool setStringValue(const char * relative_path, const std::string& value)
1517   { return setStringValue(relative_path, value.c_str()); }
1518   /**
1519    * Set another node's value as a string.
1520    */
1521   bool setStringValue (const std::string& relative_path, const char * value)
1522   { return setStringValue(relative_path.c_str(), value); }
1523
1524   bool setStringValue (const std::string& relative_path,
1525                        const std::string& value)
1526   { return setStringValue(relative_path.c_str(), value.c_str()); }
1527
1528   /**
1529    * Set another node's value with no specified type.
1530    */
1531   bool setUnspecifiedValue (const char * relative_path, const char * value);
1532
1533
1534   /**
1535    * Test whether another node is bound to an external data source.
1536    */
1537   bool isTied (const char * relative_path) const;
1538
1539   /**
1540    * Test whether another node is bound to an external data source.
1541    */
1542   bool isTied (const std::string& relative_path) const
1543   { return isTied(relative_path.c_str()); }
1544
1545   /**
1546    * Bind another node to an external bool source.
1547    */
1548   bool tie (const char * relative_path, const SGRawValue<bool> &rawValue,
1549             bool useDefault = true);
1550
1551   /**
1552    * Bind another node to an external bool source.
1553    */
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); }
1557
1558
1559   /**
1560    * Bind another node to an external int source.
1561    */
1562   bool tie (const char * relative_path, const SGRawValue<int> &rawValue,
1563             bool useDefault = true);
1564
1565   /**
1566    * Bind another node to an external int source.
1567    */
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); }
1571
1572
1573   /**
1574    * Bind another node to an external long int source.
1575    */
1576   bool tie (const char * relative_path, const SGRawValue<long> &rawValue,
1577             bool useDefault = true);
1578
1579   /**
1580    * Bind another node to an external long int source.
1581    */
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); }
1585
1586
1587   /**
1588    * Bind another node to an external float source.
1589    */
1590   bool tie (const char * relative_path, const SGRawValue<float> &rawValue,
1591             bool useDefault = true);
1592
1593   /**
1594    * Bind another node to an external float source.
1595    */
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); }
1599
1600
1601   /**
1602    * Bind another node to an external double source.
1603    */
1604   bool tie (const char * relative_path, const SGRawValue<double> &rawValue,
1605             bool useDefault = true);
1606
1607   /**
1608    * Bind another node to an external double source.
1609    */
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); }
1613
1614
1615   /**
1616    * Bind another node to an external string source.
1617    */
1618   bool tie (const char * relative_path, const SGRawValue<const char *> &rawValue,
1619             bool useDefault = true);
1620
1621   /**
1622    * Bind another node to an external string source.
1623    */
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); }
1627
1628
1629   /**
1630    * Unbind another node from any external data source.
1631    */
1632   bool untie (const char * relative_path);
1633
1634   /**
1635    * Unbind another node from any external data source.
1636    */
1637   bool untie (const std::string& relative_path)
1638   { return untie(relative_path.c_str()); }
1639
1640
1641   /**
1642    * Add a change listener to the property. If "initial" is set call the
1643    * listener initially.
1644    */
1645   void addChangeListener (SGPropertyChangeListener * listener,
1646                           bool initial = false);
1647
1648
1649   /**
1650    * Remove a change listener from the property.
1651    */
1652   void removeChangeListener (SGPropertyChangeListener * listener);
1653
1654
1655   /**
1656    * Get the number of listeners.
1657    */
1658   int nListeners () const { return _listeners ? (int)_listeners->size() : 0; }
1659
1660
1661   /**
1662    * Fire a value change event to all listeners.
1663    */
1664   void fireValueChanged ();
1665
1666
1667   /**
1668    * Fire a child-added event to all listeners.
1669    */
1670   void fireChildAdded (SGPropertyNode * child);
1671
1672   /**
1673    * Trigger a child-added and value-changed event for every child (Unlimited
1674    * depth).
1675    *
1676    * @param fire_self   Whether to trigger the events also for the node itself.
1677    *
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.
1680    */
1681   void fireCreatedRecursive(bool fire_self = false);
1682
1683   /**
1684    * Fire a child-removed event to all listeners.
1685    */
1686   void fireChildRemoved (SGPropertyNode * child);
1687
1688   /**
1689    * Fire a child-removed event for every child of this node (Unlimited depth)
1690    *
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.
1694    */
1695   void fireChildrenRemovedRecursive();
1696
1697
1698   /**
1699    * Clear any existing value and set the type to NONE.
1700    */
1701   void clearValue ();
1702
1703   /**
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.
1709    *
1710    * Attributes, removed children, and aliases aren't considered.
1711    */
1712   static bool compare (const SGPropertyNode& lhs, const SGPropertyNode& rhs);
1713
1714 protected:
1715
1716   void fireValueChanged (SGPropertyNode * node);
1717   void fireChildAdded (SGPropertyNode * parent, SGPropertyNode * child);
1718   void fireChildRemoved (SGPropertyNode * parent, SGPropertyNode * child);
1719
1720   SGPropertyNode_ptr eraseChild(simgear::PropertyList::iterator child);
1721
1722   /**
1723    * Protected constructor for making new nodes on demand.
1724    */
1725   SGPropertyNode (const std::string& name, int index, SGPropertyNode * parent);
1726   template<typename Itr>
1727   SGPropertyNode (Itr begin, Itr end, int index, SGPropertyNode * parent);
1728
1729   static simgear::PropertyInterpolationMgr* _interpolation_mgr;
1730
1731 private:
1732
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;
1740
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);
1748
1749
1750   /**
1751    * Get the value as a string.
1752    */
1753   const char * make_string () const;
1754
1755   /**
1756    * Trace a read access.
1757    */
1758   void trace_read () const;
1759
1760
1761   /**
1762    * Trace a write access.
1763    */
1764   void trace_write () const;
1765
1766   int _index;
1767   std::string _name;
1768   /// To avoid cyclic reference counting loops this shall not be a reference
1769   /// counted pointer
1770   SGPropertyNode * _parent;
1771   simgear::PropertyList _children;
1772   mutable std::string _buffer;
1773   simgear::props::Type _type;
1774   bool _tied;
1775   int _attr;
1776
1777   // The right kind of pointer...
1778   union {
1779     SGPropertyNode * alias;
1780     SGRaw* val;
1781   } _value;
1782
1783   union {
1784     bool bool_val;
1785     int int_val;
1786     long long_val;
1787     float float_val;
1788     double double_val;
1789     char * string_val;
1790   } _local_val;
1791
1792   std::vector<SGPropertyChangeListener *> * _listeners;
1793
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);
1804   // For boost
1805   friend size_t hash_value(const SGPropertyNode& node);
1806 };
1807
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*);
1812
1813 template<>
1814 inline bool getValue<bool>(const SGPropertyNode* node) { return node->getBoolValue(); }
1815
1816 template<>
1817 inline int getValue<int>(const SGPropertyNode* node) { return node->getIntValue(); }
1818
1819 template<>
1820 inline long getValue<long>(const SGPropertyNode* node) { return node->getLongValue(); }
1821
1822 template<>
1823 inline float getValue<float>(const SGPropertyNode* node)
1824 {
1825     return node->getFloatValue();
1826 }
1827
1828 template<>
1829 inline double getValue<double>(const SGPropertyNode* node)
1830 {
1831     return node->getDoubleValue();
1832 }
1833
1834 template<>
1835 inline const char * getValue<const char*>(const SGPropertyNode* node)
1836 {
1837     return node->getStringValue ();
1838 }
1839
1840 template<>
1841 inline std::string getValue<std::string>(const SGPropertyNode* node)
1842 {
1843     return node->getStringValue();
1844 }
1845
1846 namespace simgear
1847 {
1848   /**
1849    * Default trait for extracting enum values from SGPropertyNode. Create your
1850    * own specialization for specific enum types to enable validation of values.
1851    */
1852   template<class T>
1853   struct enum_traits
1854   {
1855     /**
1856      * Typename of the enum
1857      */
1858     static const char* name() { return typeid(T).name(); }
1859
1860     /**
1861      * @return Default value (will be used if validation fails)
1862      */
1863     static T defVal() { return T(); }
1864
1865     /**
1866      * @return Whether the given integer value has an enum value defined
1867      */
1868     static bool validate(int) { return true; }
1869   };
1870 } // namespace simgear
1871
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)
1876 {
1877   typedef simgear::enum_traits<T> Traits;
1878   int val = node->getIntValue();
1879   if( !Traits::validate(val) )
1880   {
1881     SG_LOG
1882     (
1883       SG_GENERAL,
1884       SG_WARN,
1885       "Invalid value for enum (" << Traits::name() << ", val = " << val << ")"
1886     );
1887     return Traits::defVal();
1888   }
1889   return static_cast<T>(node->getIntValue());
1890 }
1891
1892 inline bool setValue(SGPropertyNode* node, bool value)
1893 {
1894     return node->setBoolValue(value);
1895 }
1896
1897 inline bool setValue(SGPropertyNode* node, int value)
1898 {
1899     return node->setIntValue(value);
1900 }
1901
1902 inline bool setValue(SGPropertyNode* node, long value)
1903 {
1904     return node->setLongValue(value);
1905 }
1906
1907 inline bool setValue(SGPropertyNode* node, float value)
1908 {
1909     return node->setFloatValue(value);
1910 }
1911
1912 inline bool setValue(SGPropertyNode* node, double value)
1913 {
1914     return node->setDoubleValue(value);
1915 }
1916
1917 inline bool setValue(SGPropertyNode* node, const char* value)
1918 {
1919     return node->setStringValue(value);
1920 }
1921
1922 inline bool setValue (SGPropertyNode* node, const std::string& value)
1923 {
1924     return node->setStringValue(value.c_str());
1925 }
1926
1927 template<typename T>
1928 bool SGPropertyNode::tie(const SGRawValue<T> &rawValue, bool useDefault)
1929 {
1930     using namespace simgear::props;
1931     if (_type == ALIAS || _tied)
1932         return false;
1933
1934     useDefault = useDefault && hasValue();
1935     T old_val = SGRawValue<T>::DefaultValue();
1936     if (useDefault)
1937         old_val = getValue<T>(this);
1938     clearValue();
1939     if (PropertyTraits<T>::Internal)
1940         _type = PropertyTraits<T>::type_tag;
1941     else
1942         _type = EXTENDED;
1943     _tied = true;
1944     _value.val = rawValue.clone();
1945     if (useDefault) {
1946         int save_attributes = getAttributes();
1947         setAttribute( WRITE, true );
1948         setValue(old_val);
1949         setAttributes( save_attributes );
1950     }
1951     return true;
1952 }
1953
1954 template<>
1955 bool SGPropertyNode::tie (const SGRawValue<const char *> &rawValue,
1956                           bool useDefault);
1957
1958 template<typename T>
1959 T SGPropertyNode::getValue(typename boost::disable_if_c<simgear::props
1960                            ::PropertyTraits<T>::Internal>::type* dummy) const
1961 {
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();
1966     }
1967     if (getAttribute(TRACE_READ))
1968         trace_read();
1969     if (!getAttribute(READ))
1970       return SGRawValue<T>::DefaultValue();
1971     switch (_type) {
1972     case EXTENDED:
1973         if (_value.val->getType() == PropertyTraits<T>::type_tag)
1974             return static_cast<SGRawValue<T>*>(_value.val)->getValue();
1975         break;
1976     case STRING:
1977     case UNSPECIFIED:
1978         return simgear::parseString<T>(make_string());
1979         break;
1980     default: // avoid compiler warning
1981         break;
1982     }
1983     return SGRawValue<T>::DefaultValue();
1984 }
1985
1986 template<typename T>
1987 inline T SGPropertyNode::getValue(typename boost::enable_if_c<simgear::props
1988                                   ::PropertyTraits<T>::Internal>::type* dummy) const
1989 {
1990   return ::getValue<T>(this);
1991 }
1992
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
1995 {
1996   const simgear::PropertyList& props = getChildren(name);
1997   std::vector<T> values( props.size() );
1998
1999   for( size_t i = 0; i < props.size(); ++i )
2000     values[i] = props[i]->getValue<T_get>();
2001
2002   return values;
2003 }
2004
2005 template<typename T>
2006 inline
2007 std::vector<T> SGPropertyNode::getChildValues(const std::string& name) const
2008 {
2009   return getChildValues<T, T>(name);
2010 }
2011
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)
2016 {
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);
2021         return true;
2022     }
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) {
2028             clearValue();
2029             _type = EXTENDED;
2030             _value.val = new SGRawValueContainer<T>(val);
2031         } else {
2032             static_cast<SGRawValue<T>*>(_value.val)->setValue(val);
2033         }
2034         if (getAttribute(TRACE_WRITE))
2035             trace_write();
2036         return true;
2037     }
2038     return false;
2039 }
2040
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)
2045 {
2046   return ::setValue(this, val);
2047 }
2048
2049 /**
2050  * Utility function for creation of a child property node.
2051  */
2052 inline SGPropertyNode* makeChild(SGPropertyNode* parent, const char* name,
2053                                  int index = 0)
2054 {
2055     return parent->getChild(name, index, true);
2056 }
2057
2058 /**
2059  * Utility function for creation of a child property node using a
2060  * relative path.
2061  */
2062 namespace simgear
2063 {
2064 template<typename StringType>
2065 inline SGPropertyNode* makeNode(SGPropertyNode* parent, const StringType& name)
2066 {
2067     return parent->getNode(name, true);
2068 }
2069 }
2070
2071 // For boost::hash
2072 size_t hash_value(const SGPropertyNode& node);
2073
2074 // Helper comparison and hash functions for common cases
2075
2076 namespace simgear
2077 {
2078 namespace props
2079 {
2080 struct Compare
2081 {
2082     bool operator()(const SGPropertyNode* lhs, const SGPropertyNode* rhs) const
2083     {
2084         return SGPropertyNode::compare(*lhs, *rhs);
2085     }
2086     bool operator()(SGPropertyNode_ptr lhs, const SGPropertyNode* rhs) const
2087     {
2088         return SGPropertyNode::compare(*lhs, *rhs);
2089     }
2090     bool operator()(const SGPropertyNode* lhs, SGPropertyNode_ptr rhs) const
2091     {
2092         return SGPropertyNode::compare(*lhs, *rhs);
2093     }
2094     bool operator()(SGPropertyNode_ptr lhs, SGPropertyNode_ptr rhs) const
2095     {
2096         return SGPropertyNode::compare(*lhs, *rhs);
2097     }
2098 };
2099
2100 struct Hash
2101 {
2102     size_t operator()(const SGPropertyNode* node) const
2103     {
2104         return hash_value(*node);
2105     }
2106     size_t operator()(SGPropertyNode_ptr node) const
2107     {
2108         return hash_value(*node);
2109     }
2110 };
2111 }
2112 }
2113
2114 /** Convenience class for change listener callbacks without
2115  * creating a derived class implementing a "valueChanged" method.
2116  * Also removes listener on destruction automatically.
2117  */
2118 template<class T>
2119 class SGPropertyChangeCallback
2120     : public SGPropertyChangeListener
2121 {
2122 public:
2123     SGPropertyChangeCallback(T* obj, void (T::*method)(SGPropertyNode*),
2124                              SGPropertyNode_ptr property,bool initial=false)
2125         : _obj(obj), _callback(method), _property(property)
2126     {
2127         _property->addChangeListener(this,initial);
2128     }
2129
2130         SGPropertyChangeCallback(const SGPropertyChangeCallback<T>& other) :
2131                 _obj(other._obj), _callback(other._callback), _property(other._property)
2132         {
2133                  _property->addChangeListener(this,false);
2134         }
2135
2136     virtual ~SGPropertyChangeCallback()
2137     {
2138         _property->removeChangeListener(this);
2139     }
2140     void valueChanged (SGPropertyNode * node)
2141     {
2142         (_obj->*_callback)(node);
2143     }
2144 private:
2145     T* _obj;
2146     void (T::*_callback)(SGPropertyNode*);
2147     SGPropertyNode_ptr _property;
2148 };
2149
2150 #endif // __PROPS_HXX
2151
2152 // end of props.hxx