]> git.mxchange.org Git - simgear.git/blob - simgear/props/props.hxx
bf0e563e99d3bdd81eef5e3d23596bf5792af0ed
[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    * Default constructor.
753    */
754   SGPropertyNode ();
755
756
757   /**
758    * Copy constructor.
759    */
760   SGPropertyNode (const SGPropertyNode &node);
761
762
763   /**
764    * Destructor.
765    */
766   virtual ~SGPropertyNode ();
767
768
769
770   //
771   // Basic properties.
772   //
773
774   /**
775    * Test whether this node contains a primitive leaf value.
776    */
777     bool hasValue () const { return (_type != simgear::props::NONE); }
778
779
780   /**
781    * Get the node's simple (XML) name.
782    */
783   const char * getName () const { return _name.c_str(); }
784
785   /**
786    * Get the node's simple name as a string.
787    */
788   const std::string& getNameString () const { return _name; }
789
790   /**
791    * Get the node's pretty display name, with subscript when needed.
792    */
793     std::string getDisplayName (bool simplify = false) const;
794
795
796   /**
797    * Get the node's integer index.
798    */
799   int getIndex () const { return _index; }
800
801
802   /**
803    * Get a non-const pointer to the node's parent.
804    */
805   SGPropertyNode * getParent () { return _parent; }
806
807
808   /**
809    * Get a const pointer to the node's parent.
810    */
811   const SGPropertyNode * getParent () const { return _parent; }
812
813
814   //
815   // Children.
816   //
817
818
819   /**
820    * Get the number of child nodes.
821    */
822   int nChildren () const { return (int)_children.size(); }
823
824
825   /**
826    * Get a child node by position (*NOT* index).
827    */
828   SGPropertyNode * getChild (int position);
829
830
831   /**
832    * Get a const child node by position (*NOT* index).
833    */
834   const SGPropertyNode * getChild (int position) const;
835
836
837   /**
838    * Test whether a named child exists.
839    */
840   bool hasChild (const char * name, int index = 0) const
841   {
842     return (getChild(name, index) != 0);
843   }
844
845   /**
846    * Test whether a named child exists.
847    */
848   bool hasChild (const std::string& name, int index = 0) const
849   {
850     return (getChild(name, index) != 0);
851   }
852
853   /**
854    * Create a child node after the last node with the same name.
855    *
856    * @param min_index Minimal index for new node (skips lower indices)
857    * @param append    Whether to simply use the index after the last used index
858    *                  or use a lower, unused index if it exists
859    */
860   SGPropertyNode * addChild ( const char* name,
861                               int min_index = 0,
862                               bool append = true );
863   SGPropertyNode * addChild ( const std::string& name,
864                               int min_index = 0,
865                               bool append = true )
866   { return addChild(name.c_str(), min_index, append); }
867
868   /**
869    * Get a child node by name and index.
870    */
871   SGPropertyNode * getChild (const char* name, int index = 0,
872                              bool create = false);
873   SGPropertyNode * getChild (const std::string& name, int index = 0,
874                              bool create = false);
875   /**
876    * Get a const child node by name and index.
877    */
878   const SGPropertyNode * getChild (const char * name, int index = 0) const;
879
880   /**
881    * Get a const child node by name and index.
882    */
883   const SGPropertyNode * getChild (const std::string& name, int index = 0) const
884   { return getChild(name.c_str(), index); }
885
886
887   /**
888    * Get a vector of all children with the specified name.
889    */
890   simgear::PropertyList getChildren (const char * name) const;
891
892   /**
893    * Get a vector of all children with the specified name.
894    */
895   simgear::PropertyList getChildren (const std::string& name) const
896   { return getChildren(name.c_str()); }
897
898   /**
899    * Remove child by position.
900    */
901   SGPropertyNode_ptr removeChild (int pos, bool keep = true);
902
903
904   /**
905    * Remove a child node
906    */
907   SGPropertyNode_ptr removeChild (const char * name, int index = 0,
908                                   bool keep = true);
909
910   /**
911    * Remove a child node
912    */
913   SGPropertyNode_ptr removeChild (const std::string& name, int index = 0,
914                                   bool keep = true)
915   { return removeChild(name.c_str(), index, keep); }
916
917   /**
918    * Remove all children with the specified name.
919    */
920   simgear::PropertyList removeChildren (const char * name, bool keep = true);
921
922   /**
923    * Remove all children with the specified name.
924    */
925   simgear::PropertyList removeChildren (const std::string& name,
926                                         bool keep = true)
927   { return removeChildren(name.c_str(), keep); }
928
929   //
930   // Alias support.
931   //
932
933
934   /**
935    * Alias this node's leaf value to another's.
936    */
937   bool alias (SGPropertyNode * target);
938
939
940   /**
941    * Alias this node's leaf value to another's by relative path.
942    */
943   bool alias (const char * path);
944
945   /**
946    * Alias this node's leaf value to another's by relative path.
947    */
948   bool alias (const std::string& path)
949   { return alias(path.c_str()); }
950
951
952   /**
953    * Remove any alias for this node.
954    */
955   bool unalias ();
956
957
958   /**
959    * Test whether the node's leaf value is aliased to another's.
960    */
961   bool isAlias () const { return (_type == simgear::props::ALIAS); }
962
963
964   /**
965    * Get a non-const pointer to the current alias target, if any.
966    */
967   SGPropertyNode * getAliasTarget ();
968
969
970   /**
971    * Get a const pointer to the current alias target, if any.
972    */
973   const SGPropertyNode * getAliasTarget () const;
974
975
976   //
977   // Path information.
978   //
979
980
981   /**
982    * Get the path to this node from the root.
983    */
984   std::string getPath (bool simplify = false) const;
985
986
987   /**
988    * Get a pointer to the root node.
989    */
990   SGPropertyNode * getRootNode ();
991
992
993   /**
994    * Get a const pointer to the root node.
995    */
996   const SGPropertyNode * getRootNode () const;
997
998
999   /**
1000    * Get a pointer to another node by relative path.
1001    */
1002   SGPropertyNode * getNode (const char * relative_path, bool create = false);
1003
1004   /**
1005    * Get a pointer to another node by relative path.
1006    */
1007   SGPropertyNode * getNode (const std::string& relative_path, bool create = false)
1008   { return getNode(relative_path.c_str(), create); }
1009
1010   /**
1011    * Get a pointer to another node by relative path.
1012    *
1013    * This method leaves the index off the last member of the path,
1014    * so that the user can specify it separately (and save some
1015    * string building).  For example, getNode("/bar[1]/foo", 3) is
1016    * exactly equivalent to getNode("bar[1]/foo[3]").  The index
1017    * provided overrides any given in the path itself for the last
1018    * component.
1019    */
1020   SGPropertyNode * getNode (const char * relative_path, int index,
1021                             bool create = false);
1022
1023   /**
1024    * Get a pointer to another node by relative path.
1025    *
1026    * This method leaves the index off the last member of the path,
1027    * so that the user can specify it separately (and save some
1028    * string building).  For example, getNode("/bar[1]/foo", 3) is
1029    * exactly equivalent to getNode("bar[1]/foo[3]").  The index
1030    * provided overrides any given in the path itself for the last
1031    * component.
1032    */
1033   SGPropertyNode * getNode (const std::string& relative_path, int index,
1034                             bool create = false)
1035   { return getNode(relative_path.c_str(), index, create); }
1036
1037   /**
1038    * Get a const pointer to another node by relative path.
1039    */
1040   const SGPropertyNode * getNode (const char * relative_path) const;
1041
1042   /**
1043    * Get a const pointer to another node by relative path.
1044    */
1045   const SGPropertyNode * getNode (const std::string& relative_path) const
1046   { return getNode(relative_path.c_str()); }
1047
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 char * relative_path,
1056                                   int index) const;
1057
1058   /**
1059    * Get a const pointer to another node by relative path.
1060    *
1061    * This method leaves the index off the last member of the path,
1062    * so that the user can specify it separate.
1063    */
1064   const SGPropertyNode * getNode (const std::string& relative_path,
1065                                   int index) const
1066   { return getNode(relative_path.c_str(), index); }
1067
1068   //
1069   // Access Mode.
1070   //
1071
1072   /**
1073    * Check a single mode attribute for the property node.
1074    */
1075   bool getAttribute (Attribute attr) const { return ((_attr & attr) != 0); }
1076
1077
1078   /**
1079    * Set a single mode attribute for the property node.
1080    */
1081   void setAttribute (Attribute attr, bool state) {
1082     (state ? _attr |= attr : _attr &= ~attr);
1083   }
1084
1085
1086   /**
1087    * Get all of the mode attributes for the property node.
1088    */
1089   int getAttributes () const { return _attr; }
1090
1091
1092   /**
1093    * Set all of the mode attributes for the property node.
1094    */
1095   void setAttributes (int attr) { _attr = attr; }
1096   
1097
1098   //
1099   // Leaf Value (primitive).
1100   //
1101
1102
1103   /**
1104    * Get the type of leaf value, if any, for this node.
1105    */
1106   simgear::props::Type getType () const;
1107
1108
1109   /**
1110    * Get a bool value for this node.
1111    */
1112   bool getBoolValue () const;
1113
1114
1115   /**
1116    * Get an int value for this node.
1117    */
1118   int getIntValue () const;
1119
1120
1121   /**
1122    * Get a long int value for this node.
1123    */
1124   long getLongValue () const;
1125
1126
1127   /**
1128    * Get a float value for this node.
1129    */
1130   float getFloatValue () const;
1131
1132
1133   /**
1134    * Get a double value for this node.
1135    */
1136   double getDoubleValue () const;
1137
1138
1139   /**
1140    * Get a string value for this node.
1141    */
1142   const char * getStringValue () const;
1143
1144   /**
1145    * Get a value from a node. If the actual type of the node doesn't
1146    * match the desired type, a conversion isn't guaranteed.
1147    */
1148   template<typename T>
1149   T getValue(typename boost::enable_if_c<simgear::props::PropertyTraits<T>::Internal>
1150              ::type* dummy = 0) const;
1151   // Getter for extended property
1152   template<typename T>
1153   T getValue(typename boost::disable_if_c<simgear::props::PropertyTraits<T>::Internal>
1154              ::type* dummy = 0) const;
1155
1156   /**
1157    * Set a bool value for this node.
1158    */
1159   bool setBoolValue (bool value);
1160
1161
1162   /**
1163    * Set an int value for this node.
1164    */
1165   bool setIntValue (int value);
1166
1167
1168   /**
1169    * Set a long int value for this node.
1170    */
1171   bool setLongValue (long value);
1172
1173
1174   /**
1175    * Set a float value for this node.
1176    */
1177   bool setFloatValue (float value);
1178
1179
1180   /**
1181    * Set a double value for this node.
1182    */
1183   bool setDoubleValue (double value);
1184
1185
1186   /**
1187    * Set a string value for this node.
1188    */
1189   bool setStringValue (const char * value);
1190
1191   /**
1192    * Set a string value for this node.
1193    */
1194   bool setStringValue (const std::string& value)
1195   { return setStringValue(value.c_str()); }
1196
1197
1198   /**
1199    * Set a value of unspecified type for this node.
1200    */
1201   bool setUnspecifiedValue (const char * value);
1202
1203   template<typename T>
1204   bool setValue(const T& val,
1205                 typename boost::enable_if_c<simgear::props::PropertyTraits<T>::Internal>
1206                 ::type* dummy = 0);
1207
1208   template<typename T>
1209   bool setValue(const T& val,
1210                 typename boost::disable_if_c<simgear::props::PropertyTraits<T>::Internal>
1211                 ::type* dummy = 0);
1212
1213   template<int N>
1214   bool setValue(const char (&val)[N])
1215   {
1216     return setValue(&val[0]);
1217   }
1218   
1219   /**
1220    * Print the value of the property to a stream.
1221    */
1222   std::ostream& printOn(std::ostream& stream) const;
1223   
1224   //
1225   // Data binding.
1226   //
1227
1228
1229   /**
1230    * Test whether this node is bound to an external data source.
1231    */
1232   bool isTied () const { return _tied; }
1233
1234     /**
1235      * Bind this node to an external source.
1236      */
1237     template<typename T>
1238     bool tie(const SGRawValue<T> &rawValue, bool useDefault = true);
1239
1240   /**
1241    * Unbind this node from any external data source.
1242    */
1243   bool untie ();
1244
1245
1246   //
1247   // Convenience methods using paths.
1248   // TODO: add attribute methods
1249   //
1250
1251
1252   /**
1253    * Get another node's type.
1254    */
1255   simgear::props::Type getType (const char * relative_path) const;
1256
1257   /**
1258    * Get another node's type.
1259    */
1260   simgear::props::Type getType (const std::string& relative_path) const
1261   { return getType(relative_path.c_str()); }
1262
1263   /**
1264    * Test whether another node has a leaf value.
1265    */
1266   bool hasValue (const char * relative_path) const;
1267
1268   /**
1269    * Test whether another node has a leaf value.
1270    */
1271   bool hasValue (const std::string& relative_path) const
1272   { return hasValue(relative_path.c_str()); }
1273
1274   /**
1275    * Get another node's value as a bool.
1276    */
1277   bool getBoolValue (const char * relative_path,
1278                      bool defaultValue = false) const;
1279
1280   /**
1281    * Get another node's value as a bool.
1282    */
1283   bool getBoolValue (const std::string& relative_path,
1284                      bool defaultValue = false) const
1285   { return getBoolValue(relative_path.c_str(), defaultValue); }
1286
1287   /**
1288    * Get another node's value as an int.
1289    */
1290   int getIntValue (const char * relative_path,
1291                    int defaultValue = 0) const;
1292
1293   /**
1294    * Get another node's value as an int.
1295    */
1296   int getIntValue (const std::string& relative_path,
1297                    int defaultValue = 0) const
1298   { return getIntValue(relative_path.c_str(), defaultValue); }
1299
1300
1301   /**
1302    * Get another node's value as a long int.
1303    */
1304   long getLongValue (const char * relative_path,
1305                      long defaultValue = 0L) const;
1306
1307   /**
1308    * Get another node's value as a long int.
1309    */
1310   long getLongValue (const std::string& relative_path,
1311                      long defaultValue = 0L) const
1312   { return getLongValue(relative_path.c_str(), defaultValue); }
1313
1314   /**
1315    * Get another node's value as a float.
1316    */
1317   float getFloatValue (const char * relative_path,
1318                        float defaultValue = 0.0f) const;
1319
1320   /**
1321    * Get another node's value as a float.
1322    */
1323   float getFloatValue (const std::string& relative_path,
1324                        float defaultValue = 0.0f) const
1325   { return getFloatValue(relative_path.c_str(), defaultValue); }
1326
1327
1328   /**
1329    * Get another node's value as a double.
1330    */
1331   double getDoubleValue (const char * relative_path,
1332                          double defaultValue = 0.0) const;
1333
1334   /**
1335    * Get another node's value as a double.
1336    */
1337   double getDoubleValue (const std::string& relative_path,
1338                          double defaultValue = 0.0) const
1339   { return getDoubleValue(relative_path.c_str(), defaultValue); }
1340
1341   /**
1342    * Get another node's value as a string.
1343    */
1344   const char * getStringValue (const char * relative_path,
1345                                const char * defaultValue = "") const;
1346
1347
1348   /**
1349    * Get another node's value as a string.
1350    */
1351   const char * getStringValue (const std::string& relative_path,
1352                                const char * defaultValue = "") const
1353   { return getStringValue(relative_path.c_str(), defaultValue); }
1354
1355
1356   /**
1357    * Set another node's value as a bool.
1358    */
1359   bool setBoolValue (const char * relative_path, bool value);
1360
1361   /**
1362    * Set another node's value as a bool.
1363    */
1364   bool setBoolValue (const std::string& relative_path, bool value)
1365   { return setBoolValue(relative_path.c_str(), value); }
1366
1367
1368   /**
1369    * Set another node's value as an int.
1370    */
1371   bool setIntValue (const char * relative_path, int value);
1372
1373   /**
1374    * Set another node's value as an int.
1375    */
1376   bool setIntValue (const std::string& relative_path, int value)
1377   { return setIntValue(relative_path.c_str(), value); }
1378
1379
1380   /**
1381    * Set another node's value as a long int.
1382    */
1383   bool setLongValue (const char * relative_path, long value);
1384
1385   /**
1386    * Set another node's value as a long int.
1387    */
1388   bool setLongValue (const std::string& relative_path, long value)
1389   { return setLongValue(relative_path.c_str(), value); }
1390
1391
1392   /**
1393    * Set another node's value as a float.
1394    */
1395   bool setFloatValue (const char * relative_path, float value);
1396
1397   /**
1398    * Set another node's value as a float.
1399    */
1400   bool setFloatValue (const std::string& relative_path, float value)
1401   { return setFloatValue(relative_path.c_str(), value); }
1402
1403
1404   /**
1405    * Set another node's value as a double.
1406    */
1407   bool setDoubleValue (const char * relative_path, double value);
1408
1409   /**
1410    * Set another node's value as a double.
1411    */
1412   bool setDoubleValue (const std::string& relative_path, double value)
1413   { return setDoubleValue(relative_path.c_str(), value); }
1414
1415
1416   /**
1417    * Set another node's value as a string.
1418    */
1419   bool setStringValue (const char * relative_path, const char * value);
1420
1421   bool setStringValue(const char * relative_path, const std::string& value)
1422   { return setStringValue(relative_path, value.c_str()); }
1423   /**
1424    * Set another node's value as a string.
1425    */
1426   bool setStringValue (const std::string& relative_path, const char * value)
1427   { return setStringValue(relative_path.c_str(), value); }
1428
1429   bool setStringValue (const std::string& relative_path,
1430                        const std::string& value)
1431   { return setStringValue(relative_path.c_str(), value.c_str()); }
1432
1433   /**
1434    * Set another node's value with no specified type.
1435    */
1436   bool setUnspecifiedValue (const char * relative_path, const char * value);
1437
1438
1439   /**
1440    * Test whether another node is bound to an external data source.
1441    */
1442   bool isTied (const char * relative_path) const;
1443
1444   /**
1445    * Test whether another node is bound to an external data source.
1446    */
1447   bool isTied (const std::string& relative_path) const
1448   { return isTied(relative_path.c_str()); }
1449
1450   /**
1451    * Bind another node to an external bool source.
1452    */
1453   bool tie (const char * relative_path, const SGRawValue<bool> &rawValue,
1454             bool useDefault = true);
1455
1456   /**
1457    * Bind another node to an external bool source.
1458    */
1459   bool tie (const std::string& relative_path, const SGRawValue<bool> &rawValue,
1460             bool useDefault = true)
1461   { return tie(relative_path.c_str(), rawValue, useDefault); }
1462
1463
1464   /**
1465    * Bind another node to an external int source.
1466    */
1467   bool tie (const char * relative_path, const SGRawValue<int> &rawValue,
1468             bool useDefault = true);
1469
1470   /**
1471    * Bind another node to an external int source.
1472    */
1473   bool tie (const std::string& relative_path, const SGRawValue<int> &rawValue,
1474             bool useDefault = true)
1475   { return tie(relative_path.c_str(), rawValue, useDefault); }
1476
1477
1478   /**
1479    * Bind another node to an external long int source.
1480    */
1481   bool tie (const char * relative_path, const SGRawValue<long> &rawValue,
1482             bool useDefault = true);
1483
1484   /**
1485    * Bind another node to an external long int source.
1486    */
1487   bool tie (const std::string& relative_path, const SGRawValue<long> &rawValue,
1488             bool useDefault = true)
1489   { return tie(relative_path.c_str(), rawValue, useDefault); }
1490
1491
1492   /**
1493    * Bind another node to an external float source.
1494    */
1495   bool tie (const char * relative_path, const SGRawValue<float> &rawValue,
1496             bool useDefault = true);
1497
1498   /**
1499    * Bind another node to an external float source.
1500    */
1501   bool tie (const std::string& relative_path, const SGRawValue<float> &rawValue,
1502             bool useDefault = true)
1503   { return tie(relative_path.c_str(), rawValue, useDefault); }
1504
1505
1506   /**
1507    * Bind another node to an external double source.
1508    */
1509   bool tie (const char * relative_path, const SGRawValue<double> &rawValue,
1510             bool useDefault = true);
1511
1512   /**
1513    * Bind another node to an external double source.
1514    */
1515   bool tie (const std::string& relative_path, const SGRawValue<double> &rawValue,
1516             bool useDefault = true)
1517   { return tie(relative_path.c_str(), rawValue, useDefault); }
1518
1519
1520   /**
1521    * Bind another node to an external string source.
1522    */
1523   bool tie (const char * relative_path, const SGRawValue<const char *> &rawValue,
1524             bool useDefault = true);
1525
1526   /**
1527    * Bind another node to an external string source.
1528    */
1529   bool tie (const std::string& relative_path, const SGRawValue<const char*> &rawValue,
1530             bool useDefault = true)
1531   { return tie(relative_path.c_str(), rawValue, useDefault); }
1532
1533
1534   /**
1535    * Unbind another node from any external data source.
1536    */
1537   bool untie (const char * relative_path);
1538
1539   /**
1540    * Unbind another node from any external data source.
1541    */
1542   bool untie (const std::string& relative_path)
1543   { return untie(relative_path.c_str()); }
1544
1545
1546   /**
1547    * Add a change listener to the property. If "initial" is set call the
1548    * listener initially.
1549    */
1550   void addChangeListener (SGPropertyChangeListener * listener,
1551                           bool initial = false);
1552
1553
1554   /**
1555    * Remove a change listener from the property.
1556    */
1557   void removeChangeListener (SGPropertyChangeListener * listener);
1558
1559
1560   /**
1561    * Get the number of listeners.
1562    */
1563   int nListeners () const { return _listeners ? (int)_listeners->size() : 0; }
1564
1565
1566   /**
1567    * Fire a value change event to all listeners.
1568    */
1569   void fireValueChanged ();
1570
1571
1572   /**
1573    * Fire a child-added event to all listeners.
1574    */
1575   void fireChildAdded (SGPropertyNode * child);
1576
1577
1578   /**
1579    * Fire a child-removed event to all listeners.
1580    */
1581   void fireChildRemoved (SGPropertyNode * child);
1582
1583
1584   /**
1585    * Clear any existing value and set the type to NONE.
1586    */
1587   void clearValue ();
1588
1589   /**
1590    * Compare two property trees. The property trees are equal if: 1)
1591    * They have no children, and have the same type and the values are
1592    * equal, or 2) have the same number of children, and the
1593    * corresponding children in each tree are equal. "corresponding"
1594    * means have the same name and index.
1595    *
1596    * Attributes, removed children, and aliases aren't considered.
1597    */
1598   static bool compare (const SGPropertyNode& lhs, const SGPropertyNode& rhs);
1599
1600 protected:
1601
1602   void fireValueChanged (SGPropertyNode * node);
1603   void fireChildAdded (SGPropertyNode * parent, SGPropertyNode * child);
1604   void fireChildRemoved (SGPropertyNode * parent, SGPropertyNode * child);
1605
1606   /**
1607    * Protected constructor for making new nodes on demand.
1608    */
1609   SGPropertyNode (const std::string& name, int index, SGPropertyNode * parent);
1610   template<typename Itr>
1611   SGPropertyNode (Itr begin, Itr end, int index, SGPropertyNode * parent);
1612
1613 private:
1614
1615   // Get the raw value
1616   bool get_bool () const;
1617   int get_int () const;
1618   long get_long () const;
1619   float get_float () const;
1620   double get_double () const;
1621   const char * get_string () const;
1622
1623   // Set the raw value
1624   bool set_bool (bool value);
1625   bool set_int (int value);
1626   bool set_long (long value);
1627   bool set_float (float value);
1628   bool set_double (double value);
1629   bool set_string (const char * value);
1630
1631
1632   /**
1633    * Get the value as a string.
1634    */
1635   const char * make_string () const;
1636
1637   /**
1638    * Trace a read access.
1639    */
1640   void trace_read () const;
1641
1642
1643   /**
1644    * Trace a write access.
1645    */
1646   void trace_write () const;
1647
1648   int _index;
1649   std::string _name;
1650   /// To avoid cyclic reference counting loops this shall not be a reference
1651   /// counted pointer
1652   SGPropertyNode * _parent;
1653   simgear::PropertyList _children;
1654   simgear::PropertyList _removedChildren;
1655   mutable std::string _buffer;
1656   simgear::props::Type _type;
1657   bool _tied;
1658   int _attr;
1659
1660   // The right kind of pointer...
1661   union {
1662     SGPropertyNode * alias;
1663     SGRaw* val;
1664   } _value;
1665
1666   union {
1667     bool bool_val;
1668     int int_val;
1669     long long_val;
1670     float float_val;
1671     double double_val;
1672     char * string_val;
1673   } _local_val;
1674
1675   std::vector<SGPropertyChangeListener *> * _listeners;
1676
1677 \f
1678   // Pass name as a pair of iterators
1679   template<typename Itr>
1680   SGPropertyNode * getChildImpl (Itr begin, Itr end, int index = 0, bool create = false);
1681   // very internal method
1682   template<typename Itr>
1683   SGPropertyNode* getExistingChild (Itr begin, Itr end, int index, bool create);
1684   // very internal path parsing function
1685   template<typename SplitItr>
1686   friend SGPropertyNode* find_node_aux(SGPropertyNode * current, SplitItr& itr,
1687                                        bool create, int last_index);
1688   // For boost
1689   friend size_t hash_value(const SGPropertyNode& node);
1690 };
1691
1692 // Convenience functions for use in templates
1693 template<typename T>
1694 T getValue(const SGPropertyNode*);
1695
1696 template<>
1697 inline bool getValue<bool>(const SGPropertyNode* node) { return node->getBoolValue(); }
1698
1699 template<>
1700 inline int getValue<int>(const SGPropertyNode* node) { return node->getIntValue(); }
1701
1702 template<>
1703 inline long getValue<long>(const SGPropertyNode* node) { return node->getLongValue(); }
1704
1705 template<>
1706 inline float getValue<float>(const SGPropertyNode* node)
1707 {
1708     return node->getFloatValue();
1709 }
1710
1711 template<>
1712 inline double getValue<double>(const SGPropertyNode* node)
1713 {
1714     return node->getDoubleValue();
1715 }
1716
1717 template<>
1718 inline const char * getValue<const char*>(const SGPropertyNode* node)
1719 {
1720     return node->getStringValue ();
1721 }
1722
1723 inline bool setValue(SGPropertyNode* node, bool value)
1724 {
1725     return node->setBoolValue(value);
1726 }
1727
1728 inline bool setValue(SGPropertyNode* node, int value)
1729 {
1730     return node->setIntValue(value);
1731 }
1732
1733 inline bool setValue(SGPropertyNode* node, long value)
1734 {
1735     return node->setLongValue(value);
1736 }
1737
1738 inline bool setValue(SGPropertyNode* node, float value)
1739 {
1740     return node->setFloatValue(value);
1741 }
1742
1743 inline bool setValue(SGPropertyNode* node, double value)
1744 {
1745     return node->setDoubleValue(value);
1746 }
1747
1748 inline bool setValue(SGPropertyNode* node, const char* value)
1749 {
1750     return node->setStringValue(value);
1751 }
1752
1753 inline bool setValue (SGPropertyNode* node, const std::string& value)
1754 {
1755     return node->setStringValue(value.c_str());
1756 }
1757
1758 template<typename T>
1759 bool SGPropertyNode::tie(const SGRawValue<T> &rawValue, bool useDefault)
1760 {
1761     using namespace simgear::props;
1762     if (_type == ALIAS || _tied)
1763         return false;
1764
1765     useDefault = useDefault && hasValue();
1766     T old_val = SGRawValue<T>::DefaultValue();
1767     if (useDefault)
1768         old_val = getValue<T>(this);
1769     clearValue();
1770     if (PropertyTraits<T>::Internal)
1771         _type = PropertyTraits<T>::type_tag;
1772     else
1773         _type = EXTENDED;
1774     _tied = true;
1775     _value.val = rawValue.clone();
1776     if (useDefault) {
1777         int save_attributes = getAttributes();
1778         setAttribute( WRITE, true );
1779         setValue(old_val);
1780         setAttributes( save_attributes );
1781     }
1782     return true;
1783 }
1784
1785 template<>
1786 bool SGPropertyNode::tie (const SGRawValue<const char *> &rawValue,
1787                           bool useDefault);
1788
1789 template<typename T>
1790 T SGPropertyNode::getValue(typename boost::disable_if_c<simgear::props
1791                            ::PropertyTraits<T>::Internal>::type* dummy) const
1792 {
1793     using namespace simgear::props;
1794     if (_attr == (READ|WRITE) && _type == EXTENDED
1795         && _value.val->getType() == PropertyTraits<T>::type_tag) {
1796         return static_cast<SGRawValue<T>*>(_value.val)->getValue();
1797     }
1798     if (getAttribute(TRACE_READ))
1799         trace_read();
1800     if (!getAttribute(READ))
1801       return SGRawValue<T>::DefaultValue();
1802     switch (_type) {
1803     case EXTENDED:
1804         if (_value.val->getType() == PropertyTraits<T>::type_tag)
1805             return static_cast<SGRawValue<T>*>(_value.val)->getValue();
1806         break;
1807     case STRING:
1808     case UNSPECIFIED:
1809         return simgear::parseString<T>(make_string());
1810         break;
1811     default: // avoid compiler warning
1812         break;
1813     }
1814     return SGRawValue<T>::DefaultValue();
1815 }
1816
1817 template<typename T>
1818 inline T SGPropertyNode::getValue(typename boost::enable_if_c<simgear::props
1819                                   ::PropertyTraits<T>::Internal>::type* dummy) const
1820 {
1821   return ::getValue<T>(this);
1822 }
1823
1824 template<typename T>
1825 bool SGPropertyNode::setValue(const T& val,
1826                               typename boost::disable_if_c<simgear::props
1827                               ::PropertyTraits<T>::Internal>::type* dummy)
1828 {
1829     using namespace simgear::props;
1830     if (_attr == (READ|WRITE) && _type == EXTENDED
1831         && _value.val->getType() == PropertyTraits<T>::type_tag) {
1832         static_cast<SGRawValue<T>*>(_value.val)->setValue(val);
1833         return true;
1834     }
1835     if (getAttribute(WRITE)
1836         && ((_type == EXTENDED
1837             && _value.val->getType() == PropertyTraits<T>::type_tag)
1838             || _type == NONE || _type == UNSPECIFIED)) {
1839         if (_type == NONE || _type == UNSPECIFIED) {
1840             clearValue();
1841             _type = EXTENDED;
1842             _value.val = new SGRawValueContainer<T>(val);
1843         } else {
1844             static_cast<SGRawValue<T>*>(_value.val)->setValue(val);
1845         }
1846         if (getAttribute(TRACE_WRITE))
1847             trace_write();
1848         return true;
1849     }
1850     return false;
1851 }
1852
1853 template<typename T>
1854 inline bool SGPropertyNode::setValue(const T& val,
1855                                      typename boost::enable_if_c<simgear::props
1856                                      ::PropertyTraits<T>::Internal>::type* dummy)
1857 {
1858   return ::setValue(this, val);
1859 }
1860
1861 /**
1862  * Utility function for creation of a child property node.
1863  */
1864 inline SGPropertyNode* makeChild(SGPropertyNode* parent, const char* name,
1865                                  int index = 0)
1866 {
1867     return parent->getChild(name, index, true);
1868 }
1869
1870 /**
1871  * Utility function for creation of a child property node using a
1872  * relative path.
1873  */
1874 namespace simgear
1875 {
1876 template<typename StringType>
1877 inline SGPropertyNode* makeNode(SGPropertyNode* parent, const StringType& name)
1878 {
1879     return parent->getNode(name, true);
1880 }
1881 }
1882
1883 // For boost::hash
1884 size_t hash_value(const SGPropertyNode& node);
1885
1886 // Helper comparison and hash functions for common cases
1887
1888 namespace simgear
1889 {
1890 namespace props
1891 {
1892 struct Compare
1893 {
1894     bool operator()(const SGPropertyNode* lhs, const SGPropertyNode* rhs) const
1895     {
1896         return SGPropertyNode::compare(*lhs, *rhs);
1897     }
1898     bool operator()(SGPropertyNode_ptr lhs, const SGPropertyNode* rhs) const
1899     {
1900         return SGPropertyNode::compare(*lhs, *rhs);
1901     }
1902     bool operator()(const SGPropertyNode* lhs, SGPropertyNode_ptr rhs) const
1903     {
1904         return SGPropertyNode::compare(*lhs, *rhs);
1905     }
1906     bool operator()(SGPropertyNode_ptr lhs, SGPropertyNode_ptr rhs) const
1907     {
1908         return SGPropertyNode::compare(*lhs, *rhs);
1909     }
1910 };
1911
1912 struct Hash
1913 {
1914     size_t operator()(const SGPropertyNode* node) const
1915     {
1916         return hash_value(*node);
1917     }
1918     size_t operator()(SGPropertyNode_ptr node) const
1919     {
1920         return hash_value(*node);
1921     }
1922 };
1923 }
1924 }
1925
1926 /** Convenience class for change listener callbacks without
1927  * creating a derived class implementing a "valueChanged" method.
1928  * Also removes listener on destruction automatically.
1929  */
1930 template<class T>
1931 class SGPropertyChangeCallback
1932     : public SGPropertyChangeListener
1933 {
1934 public:
1935     SGPropertyChangeCallback(T* obj, void (T::*method)(SGPropertyNode*),
1936                              SGPropertyNode_ptr property,bool initial=false)
1937         : _obj(obj), _callback(method), _property(property)
1938     {
1939         _property->addChangeListener(this,initial);
1940     }
1941     virtual ~SGPropertyChangeCallback()
1942     {
1943         _property->removeChangeListener(this);
1944     }
1945     void valueChanged (SGPropertyNode * node)
1946     {
1947         (_obj->*_callback)(node);
1948     }
1949 private:
1950     T* _obj;
1951     void (T::*_callback)(SGPropertyNode*);
1952     SGPropertyNode_ptr _property;
1953 };
1954
1955 #endif // __PROPS_HXX
1956
1957 // end of props.hxx