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