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