]> git.mxchange.org Git - simgear.git/blob - simgear/props/props.hxx
change return value of SGPropertyNode::getPath to std::string
[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 // Extended properties
67 template<>
68 std::istream& readFrom<SGVec3d>(std::istream& stream, SGVec3d& result);
69 template<>
70 std::istream& readFrom<SGVec4d>(std::istream& stream, SGVec4d& result);
71
72     
73 /**
74  * Property value types.
75  */
76
77 #ifdef NONE
78 #pragma warn A sloppy coder has defined NONE as a macro!
79 #undef NONE
80 #endif
81
82 #ifdef ALIAS
83 #pragma warn A sloppy coder has defined ALIAS as a macro!
84 #undef ALIAS
85 #endif
86
87 #ifdef UNSPECIFIED
88 #pragma warn A sloppy coder has defined UNSPECIFIED as a macro!
89 #undef UNSPECIFIED
90 #endif
91
92 #ifdef BOOL
93 #pragma warn A sloppy coder has defined BOOL as a macro!
94 #undef BOOL
95 #endif
96
97 #ifdef INT
98 #pragma warn A sloppy coder has defined INT as a macro!
99 #undef INT
100 #endif
101
102 #ifdef LONG
103 #pragma warn A sloppy coder has defined LONG as a macro!
104 #undef LONG
105 #endif
106
107 #ifdef FLOAT
108 #pragma warn A sloppy coder has defined FLOAT as a macro!
109 #undef FLOAT
110 #endif
111
112 #ifdef DOUBLE
113 #pragma warn A sloppy coder has defined DOUBLE as a macro!
114 #undef DOUBLE
115 #endif
116
117 #ifdef STRING
118 #pragma warn A sloppy coder has defined STRING as a macro!
119 #undef STRING
120 #endif
121
122 namespace props
123 {
124 /**
125  * The possible types of an SGPropertyNode. Types that appear after
126  * EXTENDED are not stored in the SGPropertyNode itself.
127  */
128 enum Type {
129     NONE = 0, /**< The node hasn't been assigned a value yet. */
130     ALIAS, /**< The node "points" to another node. */
131     BOOL,
132     INT,
133     LONG,
134     FLOAT,
135     DOUBLE,
136     STRING,
137     UNSPECIFIED,
138     EXTENDED, /**< The node's value is not stored in the property;
139                * the actual value and type is retrieved from an
140                * SGRawValue node. This type is never returned by @see
141                * SGPropertyNode::getType.
142                */
143     // Extended properties
144     VEC3D,
145     VEC4D
146 };
147
148 template<typename T> struct PropertyTraits;
149
150 #define DEFINTERNALPROP(TYPE, PROP) \
151 template<> \
152 struct PropertyTraits<TYPE> \
153 { \
154     static const Type type_tag = PROP; \
155     enum  { Internal = 1 }; \
156 }
157
158 DEFINTERNALPROP(bool, BOOL);
159 DEFINTERNALPROP(int, INT);
160 DEFINTERNALPROP(long, LONG);
161 DEFINTERNALPROP(float, FLOAT);
162 DEFINTERNALPROP(double, DOUBLE);
163 DEFINTERNALPROP(const char *, STRING);
164 DEFINTERNALPROP(const char[], STRING);
165 #undef DEFINTERNALPROP
166
167 template<>
168 struct PropertyTraits<SGVec3d>
169 {
170     static const Type type_tag = VEC3D;
171     enum  { Internal = 0 };
172 };
173
174 template<>
175 struct PropertyTraits<SGVec4d>
176 {
177     static const Type type_tag = VEC4D;
178     enum  { Internal = 0 };
179 };
180 }
181 }
182
183
184
185 \f
186 ////////////////////////////////////////////////////////////////////////
187 // A raw value.
188 //
189 // This is the mechanism that information-providing routines can
190 // use to link their own values to the property manager.  Any
191 // SGValue can be tied to a raw value and then untied again.
192 //
193 // Note: we are forced to use inlined methods here to ensure
194 // that the templates will be instantiated.  We're probably taking
195 // a small performance hit for that.
196 ////////////////////////////////////////////////////////////////////////
197
198 /**
199  * Base class for SGRawValue classes that holds no type
200  * information. This allows some generic manipulation of the
201  * SGRawValue object.
202  */
203 class SGRaw
204 {
205 public:
206     /**
207      * Get the type enumeration for the raw value.
208      *
209      * @return the type.
210      */
211     virtual simgear::props::Type getType() const = 0;
212     virtual ~SGRaw() {}
213     
214     /**
215      * Create a new deep copy of this raw value.
216      *
217      * The copy will contain its own version of the underlying value
218      * as well, and will be the same type.
219      *
220      * @return A deep copy of the current object.
221      */
222     virtual SGRaw* clone() const = 0;
223
224 };
225
226 class SGRawExtended : public SGRaw
227 {
228 public:
229     /**    
230      * Make an SGRawValueContainer from the SGRawValue.
231      *
232      * This is a virtual function of SGRawExtended so that
233      * SGPropertyNode::untie doesn't need to know the type of an
234      * extended property.
235      */
236     virtual SGRawExtended* makeContainer() const = 0;
237     /**
238      * Write value out to a stream
239      */
240     virtual std::ostream& printOn(std::ostream& stream) const = 0;
241     /**
242      * Read value from a stream and store it.
243      */
244     virtual std::istream& readFrom(std::istream& stream) = 0;
245 };
246
247 // Choose between different base classes based on whether the value is
248 // stored internal to the property node. This frees us from defining
249 // the virtual functions in the SGRawExtended interface where they
250 // don't make sense, e.g. readFrom for the const char* type.
251 template<typename T, int internal = simgear::props::PropertyTraits<T>::Internal>
252 class SGRawBase;
253
254 template<typename T>
255 class SGRawBase<T, 1> : public SGRaw
256 {
257 };
258
259 template<typename T>
260 class SGRawBase<T, 0> : public SGRawExtended
261 {
262     virtual SGRawExtended* makeContainer() const;
263     virtual std::ostream& printOn(std::ostream& stream) const;
264     virtual std::istream& readFrom(std::istream& stream);
265 };
266
267 /**
268  * Abstract base class for a raw value.
269  *
270  * <p>The property manager is implemented in two layers.  The {@link
271  * SGPropertyNode} is the highest and most abstract layer,
272  * representing an LValue/RValue pair: it records the position of the
273  * property in the property tree and contains facilities for
274  * navigation to other nodes.  It is guaranteed to be persistent: the
275  * {@link SGPropertyNode} will not change during a session, even if
276  * the property is bound and unbound multiple times.</p>
277  *
278  * <p>When the property value is not managed internally in the
279  * SGPropertyNode, the SGPropertyNode will contain a reference to an
280  * SGRawValue (this class), which provides an abstract way to get,
281  * set, and clone the underlying value.  The SGRawValue may change
282  * frequently during a session as a value is retyped or bound and
283  * unbound to various data source, but the abstract SGPropertyNode
284  * layer insulates the application from those changes.
285  *
286  * <p>The SGPropertyNode class always keeps a *copy* of a raw value,
287  * not the original one passed to it; if you override a derived class
288  * but do not replace the {@link #clone} method, strange things will
289  * happen.</p>
290  *
291  * <p>All derived SGRawValue classes must implement {@link #getValue},
292  * {@link #setValue}, and {@link #clone} for the appropriate type.</p>
293  *
294  * @see SGPropertyNode
295  * @see SGRawValuePointer
296  * @see SGRawValueFunctions
297  * @see SGRawValueFunctionsIndexed
298  * @see SGRawValueMethods
299  * @see SGRawValueMethodsIndexed
300  * @see SGRawValueContainer
301  */
302 template <class T>
303 class SGRawValue : public SGRawBase<T>
304 {
305 public:
306
307   /**
308    * The default underlying value for this type.
309    *
310    * Every raw value has a default; the default is false for a
311    * boolean, 0 for the various numeric values, and "" for a string.
312    * If additional types of raw values are added in the future, they
313    * may need different kinds of default values (such as epoch for a
314    * date type).  The default value is used when creating new values.
315    */
316   static T DefaultValue()
317   {
318     return T();
319   }
320
321
322   /**
323    * Constructor.
324    *
325    * Use the default value for this type.
326    */
327   SGRawValue () {}
328
329
330   /**
331    * Destructor.
332    */
333   virtual ~SGRawValue () {}
334
335
336   /**
337    * Return the underlying value.
338    *
339    * @return The actual value for the property.
340    * @see #setValue
341    */
342   virtual T getValue () const = 0;
343
344
345   /**
346    * Assign a new underlying value.
347    *
348    * If the new value cannot be set (because this is a read-only
349    * raw value, or because the new value is not acceptable for
350    * some reason) this method returns false and leaves the original
351    * value unchanged.
352    *
353    * @param value The actual value for the property.
354    * @return true if the value was set successfully, false otherwise.
355    * @see #getValue
356    */
357   virtual bool setValue (T value) = 0;
358
359
360   /**
361    * Return the type tag for this raw value type.
362    */
363   virtual simgear::props::Type getType() const
364   {
365     return simgear::props::PropertyTraits<T>::type_tag;
366   }
367 };
368
369
370
371 ////////////////////////////////////////////////////////////////////////
372 // Default values for every type.
373 ////////////////////////////////////////////////////////////////////////
374
375 template<> inline bool SGRawValue<bool>::DefaultValue()
376 {
377   return false;
378 }
379
380 template<> inline const char * SGRawValue<const char *>::DefaultValue()
381 {
382   return "";
383 }
384
385 /**
386  * A raw value bound to a pointer.
387  *
388  * This is the most efficient way to tie an external value, but also
389  * the most dangerous, because there is no way for the supplier to
390  * perform bounds checking and derived calculations except by polling
391  * the variable to see if it has changed.  There is no default
392  * constructor, because this class would be meaningless without a
393  * pointer.
394  */
395 template <class T>
396 class SGRawValuePointer : public SGRawValue<T>
397 {
398 public:
399
400   /**
401    * Explicit pointer constructor.
402    *
403    * Create a new raw value bound to the value of the variable
404    * referenced by the pointer.
405    *
406    * @param ptr The pointer to the variable to which this raw value
407    * will be bound.
408    */
409   SGRawValuePointer (T * ptr) : _ptr(ptr) {}
410
411   /**
412    * Destructor.
413    */
414   virtual ~SGRawValuePointer () {}
415
416   /**
417    * Get the underlying value.
418    *
419    * This method will dereference the pointer and return the
420    * variable's value.
421    */
422   virtual T getValue () const { return *_ptr; }
423
424   /**
425    * Set the underlying value.
426    *
427    * This method will dereference the pointer and change the
428    * variable's value.
429    */
430   virtual bool setValue (T value) { *_ptr = value; return true; }
431
432   /**
433    * Create a copy of this raw value.
434    *
435    * The copy will use the same external pointer as the original.
436    */
437   virtual SGRaw* clone () const {
438     return new SGRawValuePointer(_ptr);
439   }
440
441 private:
442   T * _ptr;
443 };
444
445
446 /**
447  * A value managed through static functions.
448  *
449  * A read-only value will not have a setter; a write-only value will
450  * not have a getter.
451  */
452 template <class T>
453 class SGRawValueFunctions : public SGRawValue<T>
454 {
455 public:
456
457   /**
458    * The template type of a static getter function.
459    */
460   typedef T (*getter_t)();
461
462   /**
463    * The template type of a static setter function.
464    */
465   typedef void (*setter_t)(T);
466
467   /**
468    * Explicit constructor.
469    *
470    * Create a new raw value bound to the getter and setter supplied.
471    *
472    * @param getter A static function for getting a value, or 0
473    * to read-disable the value.
474    * @param setter A static function for setting a value, or 0
475    * to write-disable the value.
476    */
477   SGRawValueFunctions (getter_t getter = 0, setter_t setter = 0)
478     : _getter(getter), _setter(setter) {}
479
480   /**
481    * Destructor.
482    */
483   virtual ~SGRawValueFunctions () {}
484
485   /**
486    * Get the underlying value.
487    *
488    * This method will invoke the getter function to get a value.
489    * If no getter function was supplied, this method will always
490    * return the default value for the type.
491    */
492   virtual T getValue () const {
493     if (_getter) return (*_getter)();
494     else return SGRawValue<T>::DefaultValue();
495   }
496
497   /**
498    * Set the underlying value.
499    *
500    * This method will invoke the setter function to change the
501    * underlying value.  If no setter function was supplied, this
502    * method will return false.
503    */
504   virtual bool setValue (T value) {
505     if (_setter) { (*_setter)(value); return true; }
506     else return false;
507   }
508
509   /**
510    * Create a copy of this raw value, bound to the same functions.
511    */
512   virtual SGRaw* clone () const {
513     return new SGRawValueFunctions(_getter,_setter);
514   }
515
516 private:
517   getter_t _getter;
518   setter_t _setter;
519 };
520
521
522 /**
523  * An indexed value bound to static functions.
524  *
525  * A read-only value will not have a setter; a write-only value will
526  * not have a getter.  An indexed value is useful for binding one
527  * of a list of possible values (such as multiple engines for a
528  * plane).  The index is hard-coded at creation time.
529  *
530  * @see SGRawValue
531  */
532 template <class T>
533 class SGRawValueFunctionsIndexed : public SGRawValue<T>
534 {
535 public:
536   typedef T (*getter_t)(int);
537   typedef void (*setter_t)(int,T);
538   SGRawValueFunctionsIndexed (int index, getter_t getter = 0, setter_t setter = 0)
539     : _index(index), _getter(getter), _setter(setter) {}
540   virtual ~SGRawValueFunctionsIndexed () {}
541   virtual T getValue () const {
542     if (_getter) return (*_getter)(_index);
543     else return SGRawValue<T>::DefaultValue();
544   }
545   virtual bool setValue (T value) {
546     if (_setter) { (*_setter)(_index, value); return true; }
547     else return false;
548   }
549   virtual SGRaw* clone () const {
550     return new SGRawValueFunctionsIndexed(_index, _getter, _setter);
551   }
552 private:
553   int _index;
554   getter_t _getter;
555   setter_t _setter;
556 };
557
558
559 /**
560  * A value managed through an object and access methods.
561  *
562  * A read-only value will not have a setter; a write-only value will
563  * not have a getter.
564  */
565 template <class C, class T>
566 class SGRawValueMethods : public SGRawValue<T>
567 {
568 public:
569   typedef T (C::*getter_t)() const;
570   typedef void (C::*setter_t)(T);
571   SGRawValueMethods (C &obj, getter_t getter = 0, setter_t setter = 0)
572     : _obj(obj), _getter(getter), _setter(setter) {}
573   virtual ~SGRawValueMethods () {}
574   virtual T getValue () const {
575     if (_getter) { return (_obj.*_getter)(); }
576     else { return SGRawValue<T>::DefaultValue(); }
577   }
578   virtual bool setValue (T value) {
579     if (_setter) { (_obj.*_setter)(value); return true; }
580     else return false;
581   }
582   virtual SGRaw* clone () const {
583     return new SGRawValueMethods(_obj, _getter, _setter);
584   }
585 private:
586   C &_obj;
587   getter_t _getter;
588   setter_t _setter;
589 };
590
591
592 /**
593  * An indexed value managed through an object and access methods.
594  *
595  * A read-only value will not have a setter; a write-only value will
596  * not have a getter.
597  */
598 template <class C, class T>
599 class SGRawValueMethodsIndexed : public SGRawValue<T>
600 {
601 public:
602   typedef T (C::*getter_t)(int) const;
603   typedef void (C::*setter_t)(int, T);
604   SGRawValueMethodsIndexed (C &obj, int index,
605                      getter_t getter = 0, setter_t setter = 0)
606     : _obj(obj), _index(index), _getter(getter), _setter(setter) {}
607   virtual ~SGRawValueMethodsIndexed () {}
608   virtual T getValue () const {
609     if (_getter) { return (_obj.*_getter)(_index); }
610     else { return SGRawValue<T>::DefaultValue(); }
611   }
612   virtual bool setValue (T value) {
613     if (_setter) { (_obj.*_setter)(_index, value); return true; }
614     else return false;
615   }
616   virtual SGRaw* clone () const {
617     return new SGRawValueMethodsIndexed(_obj, _index, _getter, _setter);
618   }
619 private:
620   C &_obj;
621   int _index;
622   getter_t _getter;
623   setter_t _setter;
624 };
625
626 /**
627  * A raw value that contains its value. This provides a way for
628  * property nodes to contain values that shouldn't be stored in the
629  * property node itself.
630  */
631 template <class T>
632 class SGRawValueContainer : public SGRawValue<T>
633 {
634 public:
635
636     /**
637      * Explicit constructor.
638      */
639     SGRawValueContainer(const T& obj) : _obj(obj) {}
640
641     /**
642      * Destructor.
643      */
644     virtual ~SGRawValueContainer() {}
645
646     /**
647      * Get the underlying value.
648      */
649     virtual T getValue() const { return _obj; }
650
651     /**
652      * Set the underlying value.
653      *
654      * This method will dereference the pointer and change the
655      * variable's value.
656      */
657     virtual bool setValue (T value) { _obj = value; return true; }
658
659     /**
660      * Create a copy of this raw value.
661      */
662     virtual SGRaw* clone () const {
663         return new SGRawValueContainer(_obj);
664     }
665
666 private:
667     T _obj;
668 };
669
670 template<typename T>
671 SGRawExtended* SGRawBase<T, 0>::makeContainer() const
672 {
673     return new SGRawValueContainer<T>(static_cast<const SGRawValue<T>*>(this)
674                                       ->getValue());
675 }
676
677 template<typename T>
678 std::ostream& SGRawBase<T, 0>::printOn(std::ostream& stream) const
679 {
680     return stream << static_cast<SGRawValue<T>*>(this)->getValue();
681 }
682
683 template<typename T>
684 std::istream& SGRawBase<T, 0>::readFrom(std::istream& stream)
685 {
686     T value;
687     simgear::readFrom(stream, value);
688     static_cast<SGRawValue<T>*>(this)->setValue(value);
689     return stream;
690 }
691
692 template<>
693 std::ostream& SGRawBase<SGVec3d>::printOn(std::ostream& stream) const;
694 template<>
695 std::ostream& SGRawBase<SGVec4d>::printOn(std::ostream& stream) const;
696
697 \f
698 /**
699  * The smart pointer that manage reference counting
700  */
701 class SGPropertyNode;
702 typedef SGSharedPtr<SGPropertyNode> SGPropertyNode_ptr;
703 typedef SGSharedPtr<const SGPropertyNode> SGConstPropertyNode_ptr;
704
705 namespace simgear
706 {
707 typedef std::vector<SGPropertyNode_ptr> PropertyList;
708 }
709
710 \f
711 /**
712  * The property change listener interface.
713  *
714  * <p>Any class that needs to listen for property changes must implement
715  * this interface.</p>
716  */
717 class SGPropertyChangeListener
718 {
719 public:
720   virtual ~SGPropertyChangeListener ();
721   virtual void valueChanged (SGPropertyNode * node);
722   virtual void childAdded (SGPropertyNode * parent, SGPropertyNode * child);
723   virtual void childRemoved (SGPropertyNode * parent, SGPropertyNode * child);
724
725 protected:
726   friend class SGPropertyNode;
727   virtual void register_property (SGPropertyNode * node);
728   virtual void unregister_property (SGPropertyNode * node);
729
730 private:
731   std::vector<SGPropertyNode *> _properties;
732 };
733
734
735 \f
736 /**
737  * A node in a property tree.
738  */
739 class SGPropertyNode : public SGReferenced
740 {
741 public:
742
743   /**
744    * Public constants.
745    */
746   enum {
747     MAX_STRING_LEN = 1024
748   };
749
750   /**
751    * Access mode attributes.
752    *
753    * <p>The ARCHIVE attribute is strictly advisory, and controls
754    * whether the property should normally be saved and restored.</p>
755    */
756   enum Attribute {
757     READ = 1,
758     WRITE = 2,
759     ARCHIVE = 4,
760     REMOVED = 8,
761     TRACE_READ = 16,
762     TRACE_WRITE = 32,
763     USERARCHIVE = 64
764   };
765
766
767   /**
768    * Last used attribute
769    * Update as needed when enum Attribute is changed
770    */
771   static const int LAST_USED_ATTRIBUTE;
772
773
774   /**
775    * Default constructor.
776    */
777   SGPropertyNode ();
778
779
780   /**
781    * Copy constructor.
782    */
783   SGPropertyNode (const SGPropertyNode &node);
784
785
786   /**
787    * Destructor.
788    */
789   virtual ~SGPropertyNode ();
790
791
792
793   //
794   // Basic properties.
795   //
796
797   /**
798    * Test whether this node contains a primitive leaf value.
799    */
800     bool hasValue () const { return (_type != simgear::props::NONE); }
801
802
803   /**
804    * Get the node's simple (XML) name.
805    */
806   const char * getName () const { return _name.c_str(); }
807
808   /**
809    * Get the node's simple name as a string.
810    */
811   const std::string& getNameString () const { return _name; }
812
813   /**
814    * Get the node's pretty display name, with subscript when needed.
815    */
816     std::string getDisplayName (bool simplify = false) const;
817
818
819   /**
820    * Get the node's integer index.
821    */
822   int getIndex () const { return _index; }
823
824
825   /**
826    * Get a non-const pointer to the node's parent.
827    */
828   SGPropertyNode * getParent () { return _parent; }
829
830
831   /**
832    * Get a const pointer to the node's parent.
833    */
834   const SGPropertyNode * getParent () const { return _parent; }
835
836
837   //
838   // Children.
839   //
840
841
842   /**
843    * Get the number of child nodes.
844    */
845   int nChildren () const { return (int)_children.size(); }
846
847
848   /**
849    * Get a child node by position (*NOT* index).
850    */
851   SGPropertyNode * getChild (int position);
852
853
854   /**
855    * Get a const child node by position (*NOT* index).
856    */
857   const SGPropertyNode * getChild (int position) const;
858
859
860   /**
861    * Test whether a named child exists.
862    */
863   bool hasChild (const char * name, int index = 0) const
864   {
865     return (getChild(name, index) != 0);
866   }
867
868   /**
869    * Test whether a named child exists.
870    */
871   bool hasChild (const std::string& name, int index = 0) const
872   {
873     return (getChild(name, index) != 0);
874   }
875
876   /**
877    * Create a child node after the last node with the same name.
878    */
879   SGPropertyNode * addChild (const char * name);
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
1662   /**
1663    * Remove this node from all nodes that link to it in their path cache.
1664    */
1665   void remove_from_path_caches();
1666
1667
1668   class hash_table;
1669
1670   int _index;
1671   std::string _name;
1672   /// To avoid cyclic reference counting loops this shall not be a reference
1673   /// counted pointer
1674   SGPropertyNode * _parent;
1675   simgear::PropertyList _children;
1676   simgear::PropertyList _removedChildren;
1677   std::vector<hash_table *> _linkedNodes;
1678   mutable std::string _buffer;
1679   hash_table * _path_cache;
1680   simgear::props::Type _type;
1681   bool _tied;
1682   int _attr;
1683
1684   // The right kind of pointer...
1685   union {
1686     SGPropertyNode * alias;
1687     SGRaw* val;
1688   } _value;
1689
1690   union {
1691     bool bool_val;
1692     int int_val;
1693     long long_val;
1694     float float_val;
1695     double double_val;
1696     char * string_val;
1697   } _local_val;
1698
1699   std::vector<SGPropertyChangeListener *> * _listeners;
1700
1701
1702   /**
1703    * Register/unregister node that links to this node in its path cache.
1704    */
1705   void add_linked_node (hash_table * node) { _linkedNodes.push_back(node); }
1706   bool remove_linked_node (hash_table * node);
1707
1708
1709 \f
1710   /**
1711    * A very simple hash table.
1712    */
1713   class hash_table {
1714   public:
1715
1716     /**
1717      * An entry in a bucket in a hash table.
1718      */
1719     class entry {
1720     public:
1721       entry ();
1722       ~entry ();
1723       const char * get_key () { return _key.c_str(); }
1724       void set_key (const char * key);
1725       SGPropertyNode * get_value () { return _value; }
1726       void set_value (SGPropertyNode * value);
1727     private:
1728       std::string _key;
1729       SGSharedPtr<SGPropertyNode> _value;
1730     };
1731
1732
1733     /**
1734      * A bucket in a hash table.
1735      */
1736     class bucket {
1737     public:
1738       bucket ();
1739       ~bucket ();
1740       entry * get_entry (const char * key, bool create = false);
1741       bool erase (SGPropertyNode * node);
1742       void clear (hash_table * owner);
1743     private:
1744       int _length;
1745       entry ** _entries;
1746     };
1747
1748     friend class bucket;
1749
1750     hash_table ();
1751     ~hash_table ();
1752     SGPropertyNode * get (const char * key);
1753     void put (const char * key, SGPropertyNode * value);
1754     bool erase (SGPropertyNode * node);
1755
1756   private:
1757     unsigned int hashcode (const char * key);
1758     unsigned int _data_length;
1759     bucket ** _data;
1760   };
1761   // Pass name as a pair of iterators
1762   template<typename Itr>
1763   SGPropertyNode * getChildImpl (Itr begin, Itr end, int index = 0, bool create = false);
1764   // very internal method
1765   template<typename Itr>
1766   SGPropertyNode* getExistingChild (Itr begin, Itr end, int index, bool create);
1767   // very internal path parsing function
1768   template<typename SplitItr>
1769   friend SGPropertyNode* find_node_aux(SGPropertyNode * current, SplitItr& itr,
1770                                        bool create, int last_index);
1771   // For boost
1772   friend size_t hash_value(const SGPropertyNode& node);
1773 };
1774
1775 // Convenience functions for use in templates
1776 template<typename T>
1777 T getValue(const SGPropertyNode*);
1778
1779 template<>
1780 inline bool getValue<bool>(const SGPropertyNode* node) { return node->getBoolValue(); }
1781
1782 template<>
1783 inline int getValue<int>(const SGPropertyNode* node) { return node->getIntValue(); }
1784
1785 template<>
1786 inline long getValue<long>(const SGPropertyNode* node) { return node->getLongValue(); }
1787
1788 template<>
1789 inline float getValue<float>(const SGPropertyNode* node)
1790 {
1791     return node->getFloatValue();
1792 }
1793
1794 template<>
1795 inline double getValue<double>(const SGPropertyNode* node)
1796 {
1797     return node->getDoubleValue();
1798 }
1799
1800 template<>
1801 inline const char * getValue<const char*>(const SGPropertyNode* node)
1802 {
1803     return node->getStringValue ();
1804 }
1805
1806 inline bool setValue(SGPropertyNode* node, bool value)
1807 {
1808     return node->setBoolValue(value);
1809 }
1810
1811 inline bool setValue(SGPropertyNode* node, int value)
1812 {
1813     return node->setIntValue(value);
1814 }
1815
1816 inline bool setValue(SGPropertyNode* node, long value)
1817 {
1818     return node->setLongValue(value);
1819 }
1820
1821 inline bool setValue(SGPropertyNode* node, float value)
1822 {
1823     return node->setFloatValue(value);
1824 }
1825
1826 inline bool setValue(SGPropertyNode* node, double value)
1827 {
1828     return node->setDoubleValue(value);
1829 }
1830
1831 inline bool setValue(SGPropertyNode* node, const char* value)
1832 {
1833     return node->setStringValue(value);
1834 }
1835
1836 inline bool setValue (SGPropertyNode* node, const std::string& value)
1837 {
1838     return node->setStringValue(value.c_str());
1839 }
1840
1841 template<typename T>
1842 bool SGPropertyNode::tie(const SGRawValue<T> &rawValue, bool useDefault)
1843 {
1844     using namespace simgear::props;
1845     if (_type == ALIAS || _tied)
1846         return false;
1847
1848     useDefault = useDefault && hasValue();
1849     T old_val = SGRawValue<T>::DefaultValue();
1850     if (useDefault)
1851         old_val = getValue<T>(this);
1852     clearValue();
1853     if (PropertyTraits<T>::Internal)
1854         _type = PropertyTraits<T>::type_tag;
1855     else
1856         _type = EXTENDED;
1857     _tied = true;
1858     _value.val = rawValue.clone();
1859     if (useDefault)
1860         setValue(old_val);
1861     return true;
1862 }
1863
1864 template<>
1865 bool SGPropertyNode::tie (const SGRawValue<const char *> &rawValue,
1866                           bool useDefault);
1867
1868 template<typename T>
1869 T SGPropertyNode::getValue(typename boost::disable_if_c<simgear::props
1870                            ::PropertyTraits<T>::Internal>::type* dummy) const
1871 {
1872     using namespace simgear::props;
1873     if (_attr == (READ|WRITE) && _type == EXTENDED
1874         && _value.val->getType() == PropertyTraits<T>::type_tag) {
1875         return static_cast<SGRawValue<T>*>(_value.val)->getValue();
1876     }
1877     if (getAttribute(TRACE_READ))
1878         trace_read();
1879     if (!getAttribute(READ))
1880       return SGRawValue<T>::DefaultValue();
1881     switch (_type) {
1882     case EXTENDED:
1883         if (_value.val->getType() == PropertyTraits<T>::type_tag)
1884             return static_cast<SGRawValue<T>*>(_value.val)->getValue();
1885         break;
1886     case STRING:
1887     case UNSPECIFIED:
1888         return simgear::parseString<T>(make_string());
1889         break;
1890     default: // avoid compiler warning
1891         break;
1892     }
1893     return SGRawValue<T>::DefaultValue();
1894 }
1895
1896 template<typename T>
1897 inline T SGPropertyNode::getValue(typename boost::enable_if_c<simgear::props
1898                                   ::PropertyTraits<T>::Internal>::type* dummy) const
1899 {
1900   return ::getValue<T>(this);
1901 }
1902
1903 template<typename T>
1904 bool SGPropertyNode::setValue(const T& val,
1905                               typename boost::disable_if_c<simgear::props
1906                               ::PropertyTraits<T>::Internal>::type* dummy)
1907 {
1908     using namespace simgear::props;
1909     if (_attr == (READ|WRITE) && _type == EXTENDED
1910         && _value.val->getType() == PropertyTraits<T>::type_tag) {
1911         static_cast<SGRawValue<T>*>(_value.val)->setValue(val);
1912         return true;
1913     }
1914     if (getAttribute(WRITE)
1915         && ((_type == EXTENDED
1916             && _value.val->getType() == PropertyTraits<T>::type_tag)
1917             || _type == NONE || _type == UNSPECIFIED)) {
1918         if (_type == NONE || _type == UNSPECIFIED) {
1919             clearValue();
1920             _type = EXTENDED;
1921             _value.val = new SGRawValueContainer<T>(val);
1922         } else {
1923             static_cast<SGRawValue<T>*>(_value.val)->setValue(val);
1924         }
1925         if (getAttribute(TRACE_WRITE))
1926             trace_write();
1927         return true;
1928     }
1929     return false;
1930 }
1931
1932 template<typename T>
1933 inline bool SGPropertyNode::setValue(const T& val,
1934                                      typename boost::enable_if_c<simgear::props
1935                                      ::PropertyTraits<T>::Internal>::type* dummy)
1936 {
1937   return ::setValue(this, val);
1938 }
1939
1940 /**
1941  * Utility function for creation of a child property node.
1942  */
1943 inline SGPropertyNode* makeChild(SGPropertyNode* parent, const char* name,
1944                                  int index = 0)
1945 {
1946     return parent->getChild(name, index, true);
1947 }
1948
1949 /**
1950  * Utility function for creation of a child property node using a
1951  * relative path.
1952  */
1953 namespace simgear
1954 {
1955 template<typename StringType>
1956 inline SGPropertyNode* makeNode(SGPropertyNode* parent, const StringType& name)
1957 {
1958     return parent->getNode(name, true);
1959 }
1960 }
1961
1962 // For boost::hash
1963 size_t hash_value(const SGPropertyNode& node);
1964
1965 // Helper comparison and hash functions for common cases
1966
1967 namespace simgear
1968 {
1969 namespace props
1970 {
1971 struct Compare
1972 {
1973     bool operator()(const SGPropertyNode* lhs, const SGPropertyNode* rhs) const
1974     {
1975         return SGPropertyNode::compare(*lhs, *rhs);
1976     }
1977     bool operator()(SGPropertyNode_ptr lhs, const SGPropertyNode* rhs) const
1978     {
1979         return SGPropertyNode::compare(*lhs, *rhs);
1980     }
1981     bool operator()(const SGPropertyNode* lhs, SGPropertyNode_ptr rhs) const
1982     {
1983         return SGPropertyNode::compare(*lhs, *rhs);
1984     }
1985     bool operator()(SGPropertyNode_ptr lhs, SGPropertyNode_ptr rhs) const
1986     {
1987         return SGPropertyNode::compare(*lhs, *rhs);
1988     }
1989 };
1990
1991 struct Hash
1992 {
1993     size_t operator()(const SGPropertyNode* node) const
1994     {
1995         return hash_value(*node);
1996     }
1997     size_t operator()(SGPropertyNode_ptr node) const
1998     {
1999         return hash_value(*node);
2000     }
2001 };
2002 }
2003 }
2004 #endif // __PROPS_HXX
2005
2006 // end of props.hxx