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