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