]> git.mxchange.org Git - simgear.git/blob - simgear/misc/props.hxx
Significant improvements to the change-listener support. There are
[simgear.git] / simgear / misc / 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
21 #if PROPS_STANDALONE
22
23 #include <string>
24 #include <iostream>
25
26 using std::string;
27 using std::vector;
28 using std::istream;
29 using std::ostream;
30
31 #else
32
33 #include <simgear/compiler.h>
34 #include <simgear/debug/logstream.hxx>
35 #include STL_STRING
36 #include STL_IOSTREAM
37 SG_USING_STD(string);
38 SG_USING_STD(vector);
39 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
40 SG_USING_STD(istream);
41 SG_USING_STD(ostream);
42 #endif
43
44 #endif
45
46
47 #ifdef NONE
48 #pragma warn A sloppy coder has defined NONE as a macro!
49 #undef NONE
50 #endif
51
52 #ifdef ALIAS
53 #pragma warn A sloppy coder has defined ALIAS as a macro!
54 #undef ALIAS
55 #endif
56
57 #ifdef UNSPECIFIED
58 #pragma warn A sloppy coder has defined UNSPECIFIED as a macro!
59 #undef UNSPECIFIED
60 #endif
61
62 #ifdef BOOL
63 #pragma warn A sloppy coder has defined BOOL as a macro!
64 #undef BOOL
65 #endif
66
67 #ifdef INT
68 #pragma warn A sloppy coder has defined INT as a macro!
69 #undef INT
70 #endif
71
72 #ifdef LONG
73 #pragma warn A sloppy coder has defined LONG as a macro!
74 #undef LONG
75 #endif
76
77 #ifdef FLOAT
78 #pragma warn A sloppy coder has defined FLOAT as a macro!
79 #undef FLOAT
80 #endif
81
82 #ifdef DOUBLE
83 #pragma warn A sloppy coder has defined DOUBLE as a macro!
84 #undef DOUBLE
85 #endif
86
87 #ifdef STRING
88 #pragma warn A sloppy coder has defined STRING as a macro!
89 #undef STRING
90 #endif
91
92
93 \f
94 ////////////////////////////////////////////////////////////////////////
95 // A raw value.
96 //
97 // This is the mechanism that information-providing routines can
98 // use to link their own values to the property manager.  Any
99 // SGValue can be tied to a raw value and then untied again.
100 //
101 // Note: we are forced to use inlined methods here to ensure
102 // that the templates will be instantiated.  We're probably taking
103 // a small performance hit for that.
104 ////////////////////////////////////////////////////////////////////////
105
106
107 /**
108  * Abstract base class for a raw value.
109  *
110  * <p>The property manager is implemented in two layers.  The {@link
111  * SGPropertyNode} is the highest and most abstract layer,
112  * representing an LValue/RValue pair: it records the position of the
113  * property in the property tree and contains facilities for
114  * navigation to other nodes.  It is guaranteed to be persistent: the
115  * {@link SGPropertyNode} will not change during a session, even if
116  * the property is bound and unbound multiple times.</p>
117  *
118  * <p>When the property value is not managed internally in the
119  * SGPropertyNode, the SGPropertyNode will contain a reference to an
120  * SGRawValue (this class), which provides an abstract way to get,
121  * set, and clone the underlying value.  The SGRawValue may change
122  * frequently during a session as a value is retyped or bound and
123  * unbound to various data source, but the abstract SGPropertyNode
124  * layer insulates the application from those changes.  The raw value
125  * contains no facilities for data binding or for type conversion: it
126  * is simply the abstraction of a primitive data type (or a compound
127  * data type, in the case of a string).</p>
128  *
129  * <p>The SGPropertyNode class always keeps a *copy* of a raw value,
130  * not the original one passed to it; if you override a derived class
131  * but do not replace the {@link #clone} method, strange things will
132  * happen.</p>
133  *
134  * <p>All derived SGRawValue classes must implement {@link #getValue},
135  * {@link #setValue}, and {@link #clone} for the appropriate type.</p>
136  *
137  * @see SGPropertyNode
138  * @see SGRawValuePointer
139  * @see SGRawValueFunctions
140  * @see SGRawValueFunctionsIndexed
141  * @see SGRawValueMethods
142  * @see SGRawValueMethodsIndexed
143  */
144 template <class T>
145 class SGRawValue
146 {
147 public:
148
149   /**
150    * The default underlying value for this type.
151    *
152    * Every raw value has a default; the default is false for a
153    * boolean, 0 for the various numeric values, and "" for a string.
154    * If additional types of raw values are added in the future, they
155    * may need different kinds of default values (such as epoch for a
156    * date type).  The default value is used when creating new values.
157    */
158   static const T DefaultValue;  // Default for this kind of raw value.
159
160
161   /**
162    * Constructor.
163    *
164    * Use the default value for this type.
165    */
166   SGRawValue () {}
167
168
169   /**
170    * Destructor.
171    */
172   virtual ~SGRawValue () {}
173
174
175   /**
176    * Return the underlying value.
177    *
178    * @return The actual value for the property.
179    * @see #setValue
180    */
181   virtual T getValue () const = 0;
182
183
184   /**
185    * Assign a new underlying value.
186    *
187    * If the new value cannot be set (because this is a read-only
188    * raw value, or because the new value is not acceptable for
189    * some reason) this method returns false and leaves the original
190    * value unchanged.
191    *
192    * @param value The actual value for the property.
193    * @return true if the value was set successfully, false otherwise.
194    * @see #getValue
195    */
196   virtual bool setValue (T value) = 0;
197
198
199   /**
200    * Create a new deep copy of this raw value.
201    *
202    * The copy will contain its own version of the underlying value
203    * as well.
204    *
205    * @return A deep copy of the current object.
206    */
207   virtual SGRawValue * clone () const = 0;
208 };
209
210
211 /**
212  * A raw value bound to a pointer.
213  *
214  * This is the most efficient way to tie an external value, but also
215  * the most dangerous, because there is no way for the supplier to
216  * perform bounds checking and derived calculations except by polling
217  * the variable to see if it has changed.  There is no default
218  * constructor, because this class would be meaningless without a
219  * pointer.
220  */
221 template <class T>
222 class SGRawValuePointer : public SGRawValue<T>
223 {
224 public:
225
226   /**
227    * Explicit pointer constructor.
228    *
229    * Create a new raw value bound to the value of the variable
230    * referenced by the pointer.
231    *
232    * @param ptr The pointer to the variable to which this raw value
233    * will be bound.
234    */
235   SGRawValuePointer (T * ptr) : _ptr(ptr) {}
236
237   /**
238    * Destructor.
239    */
240   virtual ~SGRawValuePointer () {}
241
242   /**
243    * Get the underlying value.
244    *
245    * This method will dereference the pointer and return the
246    * variable's value.
247    */
248   virtual T getValue () const { return *_ptr; }
249
250   /**
251    * Set the underlying value.
252    *
253    * This method will dereference the pointer and change the
254    * variable's value.
255    */
256   virtual bool setValue (T value) { *_ptr = value; return true; }
257
258   /**
259    * Create a copy of this raw value.
260    *
261    * The copy will use the same external pointer as the original.
262    */
263   virtual SGRawValue<T> * clone () const {
264     return new SGRawValuePointer<T>(_ptr);
265   }
266
267 private:
268   T * _ptr;
269 };
270
271
272 /**
273  * A value managed through static functions.
274  *
275  * A read-only value will not have a setter; a write-only value will
276  * not have a getter.
277  */
278 template <class T>
279 class SGRawValueFunctions : public SGRawValue<T>
280 {
281 public:
282
283   /**
284    * The template type of a static getter function.
285    */
286   typedef T (*getter_t)();
287
288   /**
289    * The template type of a static setter function.
290    */
291   typedef void (*setter_t)(T);
292
293   /**
294    * Explicit constructor.
295    *
296    * Create a new raw value bound to the getter and setter supplied.
297    *
298    * @param getter A static function for getting a value, or 0
299    * to read-disable the value.
300    * @param setter A static function for setting a value, or 0
301    * to write-disable the value.
302    */
303   SGRawValueFunctions (getter_t getter = 0, setter_t setter = 0)
304     : _getter(getter), _setter(setter) {}
305
306   /**
307    * Destructor.
308    */
309   virtual ~SGRawValueFunctions () {}
310
311   /**
312    * Get the underlying value.
313    *
314    * This method will invoke the getter function to get a value.
315    * If no getter function was supplied, this method will always
316    * return the default value for the type.
317    */
318   virtual T getValue () const {
319     if (_getter) return (*_getter)();
320     else return SGRawValue<T>::DefaultValue;
321   }
322
323   /**
324    * Set the underlying value.
325    *
326    * This method will invoke the setter function to change the
327    * underlying value.  If no setter function was supplied, this
328    * method will return false.
329    */
330   virtual bool setValue (T value) {
331     if (_setter) { (*_setter)(value); return true; }
332     else return false;
333   }
334
335   /**
336    * Create a copy of this raw value, bound to the same functions.
337    */
338   virtual SGRawValue<T> * clone () const {
339     return new SGRawValueFunctions<T>(_getter,_setter);
340   }
341
342 private:
343   getter_t _getter;
344   setter_t _setter;
345 };
346
347
348 /**
349  * An indexed value bound to static functions.
350  *
351  * A read-only value will not have a setter; a write-only value will
352  * not have a getter.  An indexed value is useful for binding one
353  * of a list of possible values (such as multiple engines for a
354  * plane).  The index is hard-coded at creation time.
355  *
356  * @see SGRawValue
357  */
358 template <class T>
359 class SGRawValueFunctionsIndexed : public SGRawValue<T>
360 {
361 public:
362   typedef T (*getter_t)(int);
363   typedef void (*setter_t)(int,T);
364   SGRawValueFunctionsIndexed (int index, getter_t getter = 0, setter_t setter = 0)
365     : _index(index), _getter(getter), _setter(setter) {}
366   virtual ~SGRawValueFunctionsIndexed () {}
367   virtual T getValue () const {
368     if (_getter) return (*_getter)(_index);
369     else return SGRawValue<T>::DefaultValue;
370   }
371   virtual bool setValue (T value) {
372     if (_setter) { (*_setter)(_index, value); return true; }
373     else return false;
374   }
375   virtual SGRawValue<T> * clone () const {
376     return new SGRawValueFunctionsIndexed<T>(_index, _getter, _setter);
377   }
378 private:
379   int _index;
380   getter_t _getter;
381   setter_t _setter;
382 };
383
384
385 /**
386  * A value managed through an object and access methods.
387  *
388  * A read-only value will not have a setter; a write-only value will
389  * not have a getter.
390  */
391 template <class C, class T>
392 class SGRawValueMethods : public SGRawValue<T>
393 {
394 public:
395   typedef T (C::*getter_t)() const;
396   typedef void (C::*setter_t)(T);
397   SGRawValueMethods (C &obj, getter_t getter = 0, setter_t setter = 0)
398     : _obj(obj), _getter(getter), _setter(setter) {}
399   virtual ~SGRawValueMethods () {}
400   virtual T getValue () const {
401     if (_getter) { return (_obj.*_getter)(); }
402     else { return SGRawValue<T>::DefaultValue; }
403   }
404   virtual bool setValue (T value) {
405     if (_setter) { (_obj.*_setter)(value); return true; }
406     else return false;
407   }
408   virtual SGRawValue<T> * clone () const {
409     return new SGRawValueMethods<C,T>(_obj, _getter, _setter);
410   }
411 private:
412   C &_obj;
413   getter_t _getter;
414   setter_t _setter;
415 };
416
417
418 /**
419  * An indexed value managed through an object and access methods.
420  *
421  * A read-only value will not have a setter; a write-only value will
422  * not have a getter.
423  */
424 template <class C, class T>
425 class SGRawValueMethodsIndexed : public SGRawValue<T>
426 {
427 public:
428   typedef T (C::*getter_t)(int) const;
429   typedef void (C::*setter_t)(int, T);
430   SGRawValueMethodsIndexed (C &obj, int index,
431                      getter_t getter = 0, setter_t setter = 0)
432     : _obj(obj), _index(index), _getter(getter), _setter(setter) {}
433   virtual ~SGRawValueMethodsIndexed () {}
434   virtual T getValue () const {
435     if (_getter) { return (_obj.*_getter)(_index); }
436     else { return SGRawValue<T>::DefaultValue; }
437   }
438   virtual bool setValue (T value) {
439     if (_setter) { (_obj.*_setter)(_index, value); return true; }
440     else return false;
441   }
442   virtual SGRawValue<T> * clone () const {
443     return new SGRawValueMethodsIndexed<C,T>(_obj, _index, _getter, _setter);
444   }
445 private:
446   C &_obj;
447   int _index;
448   getter_t _getter;
449   setter_t _setter;
450 };
451
452 \f
453 /**
454  * The smart pointer that manage reference counting
455  */
456 class SGPropertyNode;
457 class SGPropertyNode_ptr
458 {
459 public:
460
461   /**
462    * Default constructor
463    */
464   SGPropertyNode_ptr();
465
466   /**
467    * Copy constructor
468    */
469   SGPropertyNode_ptr( const SGPropertyNode_ptr &r );
470
471   /**
472    * Constructor from a pointer to a node
473    */
474   SGPropertyNode_ptr( SGPropertyNode *p );
475
476   /**
477    * Destructor
478    */
479   ~SGPropertyNode_ptr();
480
481   /**
482    * Assignement operator
483    */
484   SGPropertyNode_ptr &operator=( const SGPropertyNode_ptr &r );
485
486   /**
487    * Pointer access operator
488    */
489   SGPropertyNode *operator->();
490
491   /**
492    * Pointer access operator (const)
493    */
494   const SGPropertyNode *operator->() const;
495
496   /**
497    * Conversion to SGPropertyNode * operator
498    */
499   operator SGPropertyNode *();
500
501   /**
502    * Conversion to const SGPropertyNode * operator
503    */
504   operator const SGPropertyNode *() const;
505
506   /**
507    * Return the pointer.
508    */
509   SGPropertyNode * ptr () { return _ptr; }
510
511   /**
512    * Validity test
513    */
514   bool valid() const;
515
516 private:
517
518   SGPropertyNode *_ptr;
519 };
520
521
522 \f
523 /**
524  * The property change listener interface.
525  *
526  * <p>Any class that needs to listen for property changes must implement
527  * this interface.</p>
528  */
529 class SGPropertyChangeListener
530 {
531 public:
532   virtual ~SGPropertyChangeListener ();
533   virtual void valueChanged (SGPropertyNode * node);
534   virtual void childAdded (SGPropertyNode * parent, SGPropertyNode * child);
535   virtual void childRemoved (SGPropertyNode * parent, SGPropertyNode * child);
536
537 protected:
538   friend class SGPropertyNode;
539   virtual void register_property (SGPropertyNode * node);
540   virtual void unregister_property (SGPropertyNode * node);
541
542 private:
543   vector<SGPropertyNode *> _properties;
544 };
545
546
547 \f
548 /**
549  * A node in a property tree.
550  */
551 class SGPropertyNode
552 {
553 public:
554
555   /**
556    * Public constants.
557    */
558   enum {
559     MAX_STRING_LEN = 1024
560   };
561
562   /**
563    * Property value types.
564    */
565   enum Type {
566     NONE,
567     ALIAS,
568     BOOL,
569     INT,
570     LONG,
571     FLOAT,
572     DOUBLE,
573     STRING,
574     UNSPECIFIED
575   };
576
577
578   /**
579    * Access mode attributes.
580    *
581    * <p>The ARCHIVE attribute is strictly advisory, and controls
582    * whether the property should normally be saved and restored.</p>
583    */
584   enum Attribute {
585     READ = 1,
586     WRITE = 2,
587     ARCHIVE = 4,
588     REMOVED = 8,
589     TRACE_READ = 16,
590     TRACE_WRITE = 32
591   };
592
593
594   /**
595    * Last used attribute
596    * Update as needed when enum Attribute is changed
597    */
598   static const int LAST_USED_ATTRIBUTE;
599
600
601   /**
602    * Default constructor.
603    */
604   SGPropertyNode ();
605
606
607   /**
608    * Copy constructor.
609    */
610   SGPropertyNode (const SGPropertyNode &node);
611
612
613   /**
614    * Destructor.
615    */
616   virtual ~SGPropertyNode ();
617
618
619
620   //
621   // Basic properties.
622   //
623
624   /**
625    * Test whether this node contains a primitive leaf value.
626    */
627   bool hasValue () const { return (_type != NONE); }
628
629
630   /**
631    * Get the node's simple (XML) name.
632    */
633   const char * getName () const { return _name; }
634
635
636   /**
637    * Get the node's integer index.
638    */
639   int getIndex () const { return _index; }
640
641
642   /**
643    * Get a non-const pointer to the node's parent.
644    */
645   SGPropertyNode * getParent () { return _parent; }
646
647
648   /**
649    * Get a const pointer to the node's parent.
650    */
651   const SGPropertyNode * getParent () const { return _parent; }
652
653
654   //
655   // Children.
656   //
657
658
659   /**
660    * Get the number of child nodes.
661    */
662   int nChildren () const { return _children.size(); }
663
664
665   /**
666    * Get a child node by position (*NOT* index).
667    */
668   SGPropertyNode * getChild (int position);
669
670
671   /**
672    * Get a const child node by position (*NOT* index).
673    */
674   const SGPropertyNode * getChild (int position) const;
675
676
677   /**
678    * Get a child node by name and index.
679    */
680   SGPropertyNode * getChild (const char * name, int index = 0,
681                              bool create = false);
682
683
684   /**
685    * Get a const child node by name and index.
686    */
687   const SGPropertyNode * getChild (const char * name, int index = 0) const;
688
689
690   /**
691    * Get a vector of all children with the specified name.
692    */
693   vector<SGPropertyNode_ptr> getChildren (const char * name) const;
694
695
696   /**
697    * Remove a child node
698    */
699   SGPropertyNode_ptr removeChild (const char * name, int index = 0,
700                                   bool keep = true);
701
702
703   //
704   // Alias support.
705   //
706
707
708   /**
709    * Alias this node's leaf value to another's.
710    */
711   bool alias (SGPropertyNode * target);
712
713
714   /**
715    * Alias this node's leaf value to another's by relative path.
716    */
717   bool alias (const char * path);
718
719
720   /**
721    * Remove any alias for this node.
722    */
723   bool unalias ();
724
725
726   /**
727    * Test whether the node's leaf value is aliased to another's.
728    */
729   bool isAlias () const { return (_type == ALIAS); }
730
731
732   /**
733    * Get a non-const pointer to the current alias target, if any.
734    */
735   SGPropertyNode * getAliasTarget ();
736
737
738   /**
739    * Get a const pointer to the current alias target, if any.
740    */
741   const SGPropertyNode * getAliasTarget () const;
742
743
744   //
745   // Path information.
746   //
747
748
749   /**
750    * Get the path to this node from the root.
751    */
752   const char * getPath (bool simplify = false) const;
753
754
755   /**
756    * Get a pointer to the root node.
757    */
758   SGPropertyNode * getRootNode ();
759
760
761   /**
762    * Get a const pointer to the root node.
763    */
764   const SGPropertyNode * getRootNode () const;
765
766
767   /**
768    * Get a pointer to another node by relative path.
769    */
770   SGPropertyNode * getNode (const char * relative_path, bool create = false);
771
772
773   /**
774    * Get a pointer to another node by relative path.
775    *
776    * This method leaves the index off the last member of the path,
777    * so that the user can specify it separately (and save some
778    * string building).  For example, getNode("/bar[1]/foo", 3) is
779    * exactly equivalent to getNode("bar[1]/foo[3]").  The index
780    * provided overrides any given in the path itself for the last
781    * component.
782    */
783   SGPropertyNode * getNode (const char * relative_path, int index,
784                             bool create = false);
785
786
787   /**
788    * Get a const pointer to another node by relative path.
789    */
790   const SGPropertyNode * getNode (const char * relative_path) const;
791
792
793   /**
794    * Get a const pointer to another node by relative path.
795    *
796    * This method leaves the index off the last member of the path,
797    * so that the user can specify it separate.
798    */
799   const SGPropertyNode * getNode (const char * relative_path,
800                                   int index) const;
801
802
803   //
804   // Access Mode.
805   //
806
807   /**
808    * Check a single mode attribute for the property node.
809    */
810   bool getAttribute (Attribute attr) const { return ((_attr & attr) != 0); }
811
812
813   /**
814    * Set a single mode attribute for the property node.
815    */
816   void setAttribute (Attribute attr, bool state) {
817     (state ? _attr |= attr : _attr &= ~attr);
818   }
819
820
821   /**
822    * Get all of the mode attributes for the property node.
823    */
824   int getAttributes () const { return _attr; }
825
826
827   /**
828    * Set all of the mode attributes for the property node.
829    */
830   void setAttributes (int attr) { _attr = attr; }
831   
832
833   //
834   // Leaf Value (primitive).
835   //
836
837
838   /**
839    * Get the type of leaf value, if any, for this node.
840    */
841   Type getType () const;
842
843
844   /**
845    * Get a bool value for this node.
846    */
847   bool getBoolValue () const;
848
849
850   /**
851    * Get an int value for this node.
852    */
853   int getIntValue () const;
854
855
856   /**
857    * Get a long int value for this node.
858    */
859   long getLongValue () const;
860
861
862   /**
863    * Get a float value for this node.
864    */
865   float getFloatValue () const;
866
867
868   /**
869    * Get a double value for this node.
870    */
871   double getDoubleValue () const;
872
873
874   /**
875    * Get a string value for this node.
876    */
877   const char * getStringValue () const;
878
879
880
881   /**
882    * Set a bool value for this node.
883    */
884   bool setBoolValue (bool value);
885
886
887   /**
888    * Set an int value for this node.
889    */
890   bool setIntValue (int value);
891
892
893   /**
894    * Set a long int value for this node.
895    */
896   bool setLongValue (long value);
897
898
899   /**
900    * Set a float value for this node.
901    */
902   bool setFloatValue (float value);
903
904
905   /**
906    * Set a double value for this node.
907    */
908   bool setDoubleValue (double value);
909
910
911   /**
912    * Set a string value for this node.
913    */
914   bool setStringValue (const char * value);
915
916
917   /**
918    * Set a value of unspecified type for this node.
919    */
920   bool setUnspecifiedValue (const char * value);
921
922
923   //
924   // Data binding.
925   //
926
927
928   /**
929    * Test whether this node is bound to an external data source.
930    */
931   bool isTied () const { return _tied; }
932
933
934   /**
935    * Bind this node to an external bool source.
936    */
937   bool tie (const SGRawValue<bool> &rawValue, bool useDefault = true);
938
939
940   /**
941    * Bind this node to an external int source.
942    */
943   bool tie (const SGRawValue<int> &rawValue, bool useDefault = true);
944
945
946   /**
947    * Bind this node to an external long int source.
948    */
949   bool tie (const SGRawValue<long> &rawValue, bool useDefault = true);
950
951
952   /**
953    * Bind this node to an external float source.
954    */
955   bool tie (const SGRawValue<float> &rawValue, bool useDefault = true);
956
957
958   /**
959    * Bind this node to an external double source.
960    */
961   bool tie (const SGRawValue<double> &rawValue, bool useDefault = true);
962
963
964   /**
965    * Bind this node to an external string source.
966    */
967   bool tie (const SGRawValue<const char *> &rawValue, bool useDefault = true);
968
969
970   /**
971    * Unbind this node from any external data source.
972    */
973   bool untie ();
974
975
976   //
977   // Convenience methods using paths.
978   // TODO: add attribute methods
979   //
980
981
982   /**
983    * Get another node's type.
984    */
985   Type getType (const char * relative_path) const;
986
987
988   /**
989    * Test whether another node has a leaf value.
990    */
991   bool hasValue (const char * relative_path) const;
992
993
994   /**
995    * Get another node's value as a bool.
996    */
997   bool getBoolValue (const char * relative_path,
998                      bool defaultValue = false) const;
999
1000
1001   /**
1002    * Get another node's value as an int.
1003    */
1004   int getIntValue (const char * relative_path,
1005                    int defaultValue = 0) const;
1006
1007
1008   /**
1009    * Get another node's value as a long int.
1010    */
1011   long getLongValue (const char * relative_path,
1012                      long defaultValue = 0L) const;
1013
1014
1015   /**
1016    * Get another node's value as a float.
1017    */
1018   float getFloatValue (const char * relative_path,
1019                        float defaultValue = 0.0) const;
1020
1021
1022   /**
1023    * Get another node's value as a double.
1024    */
1025   double getDoubleValue (const char * relative_path,
1026                          double defaultValue = 0.0L) const;
1027
1028
1029   /**
1030    * Get another node's value as a string.
1031    */
1032   const char * getStringValue (const char * relative_path,
1033                                const char * defaultValue = "") const;
1034
1035
1036   /**
1037    * Set another node's value as a bool.
1038    */
1039   bool setBoolValue (const char * relative_path, bool value);
1040
1041
1042   /**
1043    * Set another node's value as an int.
1044    */
1045   bool setIntValue (const char * relative_path, int value);
1046
1047
1048   /**
1049    * Set another node's value as a long int.
1050    */
1051   bool setLongValue (const char * relative_path, long value);
1052
1053
1054   /**
1055    * Set another node's value as a float.
1056    */
1057   bool setFloatValue (const char * relative_path, float value);
1058
1059
1060   /**
1061    * Set another node's value as a double.
1062    */
1063   bool setDoubleValue (const char * relative_path, double value);
1064
1065
1066   /**
1067    * Set another node's value as a string.
1068    */
1069   bool setStringValue (const char * relative_path, const char * value);
1070
1071
1072   /**
1073    * Set another node's value with no specified type.
1074    */
1075   bool setUnspecifiedValue (const char * relative_path, const char * value);
1076
1077
1078   /**
1079    * Test whether another node is bound to an external data source.
1080    */
1081   bool isTied (const char * relative_path) const;
1082
1083
1084   /**
1085    * Bind another node to an external bool source.
1086    */
1087   bool tie (const char * relative_path, const SGRawValue<bool> &rawValue,
1088             bool useDefault = true);
1089
1090
1091   /**
1092    * Bind another node to an external int source.
1093    */
1094   bool tie (const char * relative_path, const SGRawValue<int> &rawValue,
1095             bool useDefault = true);
1096
1097
1098   /**
1099    * Bind another node to an external long int source.
1100    */
1101   bool tie (const char * relative_path, const SGRawValue<long> &rawValue,
1102             bool useDefault = true);
1103
1104
1105   /**
1106    * Bind another node to an external float source.
1107    */
1108   bool tie (const char * relative_path, const SGRawValue<float> &rawValue,
1109             bool useDefault = true);
1110
1111
1112   /**
1113    * Bind another node to an external double source.
1114    */
1115   bool tie (const char * relative_path, const SGRawValue<double> &rawValue,
1116             bool useDefault = true);
1117
1118
1119   /**
1120    * Bind another node to an external string source.
1121    */
1122   bool tie (const char * relative_path, const SGRawValue<const char *> &rawValue,
1123             bool useDefault = true);
1124
1125
1126   /**
1127    * Unbind another node from any external data source.
1128    */
1129   bool untie (const char * relative_path);
1130
1131
1132   /**
1133    * Add a change listener to the property.
1134    */
1135   void addChangeListener (SGPropertyChangeListener * listener);
1136
1137
1138   /**
1139    * Remove a change listener from the property.
1140    */
1141   void removeChangeListener (SGPropertyChangeListener * listener);
1142
1143
1144   /**
1145    * Fire a value change event to all listeners.
1146    */
1147   void fireValueChanged ();
1148
1149
1150   /**
1151    * Fire a child-added event to all listeners.
1152    */
1153   void fireChildAdded (SGPropertyNode * child);
1154
1155
1156   /**
1157    * Fire a child-removed event to all listeners.
1158    */
1159   void fireChildRemoved (SGPropertyNode * child);
1160
1161
1162 protected:
1163
1164   void fireValueChanged (SGPropertyNode * node);
1165   void fireChildAdded (SGPropertyNode * parent, SGPropertyNode * child);
1166   void fireChildRemoved (SGPropertyNode * parent, SGPropertyNode * child);
1167
1168   /**
1169    * Protected constructor for making new nodes on demand.
1170    */
1171   SGPropertyNode (const char * name, int index, SGPropertyNode * parent);
1172
1173
1174 private:
1175
1176                                 // Get the raw value
1177   bool get_bool () const;
1178   int get_int () const;
1179   long get_long () const;
1180   float get_float () const;
1181   double get_double () const;
1182   const char * get_string () const;
1183
1184                                 // Set the raw value
1185   bool set_bool (bool value);
1186   bool set_int (int value);
1187   bool set_long (long value);
1188   bool set_float (float value);
1189   bool set_double (double value);
1190   bool set_string (const char * value);
1191
1192
1193   /**
1194    * Clear any existing value and set the type to NONE.
1195    */
1196   void clear_value ();
1197
1198
1199   /**
1200    * Get the value as a string.
1201    */
1202   const char * make_string () const;
1203
1204
1205   /**
1206    * Trace a read access.
1207    */
1208   void trace_read () const;
1209
1210
1211   /**
1212    * Trace a write access.
1213    */
1214   void trace_write () const;
1215
1216
1217   /**
1218    * Increment reference counter
1219    */
1220   void incrementRef();
1221
1222   /**
1223    * Decrement reference counter
1224    */
1225   int decrementRef();
1226
1227   friend class SGPropertyNode_ptr;
1228
1229
1230   mutable char _buffer[MAX_STRING_LEN+1];
1231
1232   class hash_table;
1233
1234   char * _name;
1235   int _index;
1236   SGPropertyNode * _parent;
1237   vector<SGPropertyNode_ptr> _children;
1238   vector<SGPropertyNode_ptr> _removedChildren;
1239   hash_table * _path_cache;
1240   Type _type;
1241   bool _tied;
1242   int _attr;
1243   int _count;
1244
1245                                 // The right kind of pointer...
1246   union {
1247     SGPropertyNode * alias;
1248     SGRawValue<bool> * bool_val;
1249     SGRawValue<int> * int_val;
1250     SGRawValue<long> * long_val;
1251     SGRawValue<float> * float_val;
1252     SGRawValue<double> * double_val;
1253     SGRawValue<const char *> * string_val;
1254   } _value;
1255
1256   union {
1257     bool bool_val;
1258     int int_val;
1259     long long_val;
1260     float float_val;
1261     double double_val;
1262     char * string_val;
1263   } _local_val;
1264
1265   vector <SGPropertyChangeListener *> * _listeners;
1266
1267
1268 \f
1269   /**
1270    * A very simple hash table with no remove functionality.
1271    */
1272   class hash_table {
1273   public:
1274
1275     /**
1276      * An entry in a bucket in a hash table.
1277      */
1278     class entry {
1279     public:
1280       entry ();
1281       virtual ~entry ();
1282       virtual const char * get_key () { return _key; }
1283       virtual void set_key (const char * key);
1284       virtual SGPropertyNode * get_value () { return _value; }
1285       virtual void set_value (SGPropertyNode * value);
1286     private:
1287       char * _key;
1288       SGPropertyNode * _value;
1289     };
1290
1291
1292     /**
1293      * A bucket in a hash table.
1294      */
1295     class bucket {
1296     public:
1297       bucket ();
1298       virtual ~bucket ();
1299       virtual entry * get_entry (const char * key, bool create = false);
1300     private:
1301       int _length;
1302       entry ** _entries;
1303     };
1304
1305     friend class bucket;
1306
1307     hash_table ();
1308     virtual ~hash_table ();
1309     virtual SGPropertyNode * get (const char * key);
1310     virtual void put (const char * key, SGPropertyNode * value);
1311
1312   private:
1313     unsigned int hashcode (const char * key);
1314     unsigned int _data_length;
1315     bucket ** _data;
1316   };
1317
1318 };
1319
1320 #endif // __PROPS_HXX
1321
1322 // end of props.hxx