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