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