]> git.mxchange.org Git - simgear.git/blob - simgear/misc/props.hxx
Patch from Cameron Moore:
[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 pretty display name, with subscript when needed.
638    */
639   const char * getDisplayName (bool simplify = false) const;
640
641
642   /**
643    * Get the node's integer index.
644    */
645   int getIndex () const { return _index; }
646
647
648   /**
649    * Get a non-const pointer to the node's parent.
650    */
651   SGPropertyNode * getParent () { return _parent; }
652
653
654   /**
655    * Get a const pointer to the node's parent.
656    */
657   const SGPropertyNode * getParent () const { return _parent; }
658
659
660   //
661   // Children.
662   //
663
664
665   /**
666    * Get the number of child nodes.
667    */
668   int nChildren () const { return _children.size(); }
669
670
671   /**
672    * Get a child node by position (*NOT* index).
673    */
674   SGPropertyNode * getChild (int position);
675
676
677   /**
678    * Get a const child node by position (*NOT* index).
679    */
680   const SGPropertyNode * getChild (int position) const;
681
682
683   /**
684    * Test whether a named child exists.
685    */
686   bool hasChild (const char * name, int index = 0) const
687   {
688     return (getChild(name, index) != 0);
689   }
690
691
692   /**
693    * Get a child node by name and index.
694    */
695   SGPropertyNode * getChild (const char * name, int index = 0,
696                              bool create = false);
697
698
699   /**
700    * Get a const child node by name and index.
701    */
702   const SGPropertyNode * getChild (const char * name, int index = 0) const;
703
704
705   /**
706    * Get a vector of all children with the specified name.
707    */
708   vector<SGPropertyNode_ptr> getChildren (const char * name) const;
709
710
711   /**
712    * Remove a child node
713    */
714   SGPropertyNode_ptr removeChild (const char * name, int index = 0,
715                                   bool keep = true);
716
717
718   //
719   // Alias support.
720   //
721
722
723   /**
724    * Alias this node's leaf value to another's.
725    */
726   bool alias (SGPropertyNode * target);
727
728
729   /**
730    * Alias this node's leaf value to another's by relative path.
731    */
732   bool alias (const char * path);
733
734
735   /**
736    * Remove any alias for this node.
737    */
738   bool unalias ();
739
740
741   /**
742    * Test whether the node's leaf value is aliased to another's.
743    */
744   bool isAlias () const { return (_type == ALIAS); }
745
746
747   /**
748    * Get a non-const pointer to the current alias target, if any.
749    */
750   SGPropertyNode * getAliasTarget ();
751
752
753   /**
754    * Get a const pointer to the current alias target, if any.
755    */
756   const SGPropertyNode * getAliasTarget () const;
757
758
759   //
760   // Path information.
761   //
762
763
764   /**
765    * Get the path to this node from the root.
766    */
767   const char * getPath (bool simplify = false) const;
768
769
770   /**
771    * Get a pointer to the root node.
772    */
773   SGPropertyNode * getRootNode ();
774
775
776   /**
777    * Get a const pointer to the root node.
778    */
779   const SGPropertyNode * getRootNode () const;
780
781
782   /**
783    * Get a pointer to another node by relative path.
784    */
785   SGPropertyNode * getNode (const char * relative_path, bool create = false);
786
787
788   /**
789    * Get a pointer to another node by relative path.
790    *
791    * This method leaves the index off the last member of the path,
792    * so that the user can specify it separately (and save some
793    * string building).  For example, getNode("/bar[1]/foo", 3) is
794    * exactly equivalent to getNode("bar[1]/foo[3]").  The index
795    * provided overrides any given in the path itself for the last
796    * component.
797    */
798   SGPropertyNode * getNode (const char * relative_path, int index,
799                             bool create = false);
800
801
802   /**
803    * Get a const pointer to another node by relative path.
804    */
805   const SGPropertyNode * getNode (const char * relative_path) const;
806
807
808   /**
809    * Get a const pointer to another node by relative path.
810    *
811    * This method leaves the index off the last member of the path,
812    * so that the user can specify it separate.
813    */
814   const SGPropertyNode * getNode (const char * relative_path,
815                                   int index) const;
816
817
818   //
819   // Access Mode.
820   //
821
822   /**
823    * Check a single mode attribute for the property node.
824    */
825   bool getAttribute (Attribute attr) const { return ((_attr & attr) != 0); }
826
827
828   /**
829    * Set a single mode attribute for the property node.
830    */
831   void setAttribute (Attribute attr, bool state) {
832     (state ? _attr |= attr : _attr &= ~attr);
833   }
834
835
836   /**
837    * Get all of the mode attributes for the property node.
838    */
839   int getAttributes () const { return _attr; }
840
841
842   /**
843    * Set all of the mode attributes for the property node.
844    */
845   void setAttributes (int attr) { _attr = attr; }
846   
847
848   //
849   // Leaf Value (primitive).
850   //
851
852
853   /**
854    * Get the type of leaf value, if any, for this node.
855    */
856   Type getType () const;
857
858
859   /**
860    * Get a bool value for this node.
861    */
862   bool getBoolValue () const;
863
864
865   /**
866    * Get an int value for this node.
867    */
868   int getIntValue () const;
869
870
871   /**
872    * Get a long int value for this node.
873    */
874   long getLongValue () const;
875
876
877   /**
878    * Get a float value for this node.
879    */
880   float getFloatValue () const;
881
882
883   /**
884    * Get a double value for this node.
885    */
886   double getDoubleValue () const;
887
888
889   /**
890    * Get a string value for this node.
891    */
892   const char * getStringValue () const;
893
894
895
896   /**
897    * Set a bool value for this node.
898    */
899   bool setBoolValue (bool value);
900
901
902   /**
903    * Set an int value for this node.
904    */
905   bool setIntValue (int value);
906
907
908   /**
909    * Set a long int value for this node.
910    */
911   bool setLongValue (long value);
912
913
914   /**
915    * Set a float value for this node.
916    */
917   bool setFloatValue (float value);
918
919
920   /**
921    * Set a double value for this node.
922    */
923   bool setDoubleValue (double value);
924
925
926   /**
927    * Set a string value for this node.
928    */
929   bool setStringValue (const char * value);
930
931
932   /**
933    * Set a value of unspecified type for this node.
934    */
935   bool setUnspecifiedValue (const char * value);
936
937
938   //
939   // Data binding.
940   //
941
942
943   /**
944    * Test whether this node is bound to an external data source.
945    */
946   bool isTied () const { return _tied; }
947
948
949   /**
950    * Bind this node to an external bool source.
951    */
952   bool tie (const SGRawValue<bool> &rawValue, bool useDefault = true);
953
954
955   /**
956    * Bind this node to an external int source.
957    */
958   bool tie (const SGRawValue<int> &rawValue, bool useDefault = true);
959
960
961   /**
962    * Bind this node to an external long int source.
963    */
964   bool tie (const SGRawValue<long> &rawValue, bool useDefault = true);
965
966
967   /**
968    * Bind this node to an external float source.
969    */
970   bool tie (const SGRawValue<float> &rawValue, bool useDefault = true);
971
972
973   /**
974    * Bind this node to an external double source.
975    */
976   bool tie (const SGRawValue<double> &rawValue, bool useDefault = true);
977
978
979   /**
980    * Bind this node to an external string source.
981    */
982   bool tie (const SGRawValue<const char *> &rawValue, bool useDefault = true);
983
984
985   /**
986    * Unbind this node from any external data source.
987    */
988   bool untie ();
989
990
991   //
992   // Convenience methods using paths.
993   // TODO: add attribute methods
994   //
995
996
997   /**
998    * Get another node's type.
999    */
1000   Type getType (const char * relative_path) const;
1001
1002
1003   /**
1004    * Test whether another node has a leaf value.
1005    */
1006   bool hasValue (const char * relative_path) const;
1007
1008
1009   /**
1010    * Get another node's value as a bool.
1011    */
1012   bool getBoolValue (const char * relative_path,
1013                      bool defaultValue = false) const;
1014
1015
1016   /**
1017    * Get another node's value as an int.
1018    */
1019   int getIntValue (const char * relative_path,
1020                    int defaultValue = 0) const;
1021
1022
1023   /**
1024    * Get another node's value as a long int.
1025    */
1026   long getLongValue (const char * relative_path,
1027                      long defaultValue = 0L) const;
1028
1029
1030   /**
1031    * Get another node's value as a float.
1032    */
1033   float getFloatValue (const char * relative_path,
1034                        float defaultValue = 0.0) const;
1035
1036
1037   /**
1038    * Get another node's value as a double.
1039    */
1040   double getDoubleValue (const char * relative_path,
1041                          double defaultValue = 0.0L) const;
1042
1043
1044   /**
1045    * Get another node's value as a string.
1046    */
1047   const char * getStringValue (const char * relative_path,
1048                                const char * defaultValue = "") const;
1049
1050
1051   /**
1052    * Set another node's value as a bool.
1053    */
1054   bool setBoolValue (const char * relative_path, bool value);
1055
1056
1057   /**
1058    * Set another node's value as an int.
1059    */
1060   bool setIntValue (const char * relative_path, int value);
1061
1062
1063   /**
1064    * Set another node's value as a long int.
1065    */
1066   bool setLongValue (const char * relative_path, long value);
1067
1068
1069   /**
1070    * Set another node's value as a float.
1071    */
1072   bool setFloatValue (const char * relative_path, float value);
1073
1074
1075   /**
1076    * Set another node's value as a double.
1077    */
1078   bool setDoubleValue (const char * relative_path, double value);
1079
1080
1081   /**
1082    * Set another node's value as a string.
1083    */
1084   bool setStringValue (const char * relative_path, const char * value);
1085
1086
1087   /**
1088    * Set another node's value with no specified type.
1089    */
1090   bool setUnspecifiedValue (const char * relative_path, const char * value);
1091
1092
1093   /**
1094    * Test whether another node is bound to an external data source.
1095    */
1096   bool isTied (const char * relative_path) const;
1097
1098
1099   /**
1100    * Bind another node to an external bool source.
1101    */
1102   bool tie (const char * relative_path, const SGRawValue<bool> &rawValue,
1103             bool useDefault = true);
1104
1105
1106   /**
1107    * Bind another node to an external int source.
1108    */
1109   bool tie (const char * relative_path, const SGRawValue<int> &rawValue,
1110             bool useDefault = true);
1111
1112
1113   /**
1114    * Bind another node to an external long int source.
1115    */
1116   bool tie (const char * relative_path, const SGRawValue<long> &rawValue,
1117             bool useDefault = true);
1118
1119
1120   /**
1121    * Bind another node to an external float source.
1122    */
1123   bool tie (const char * relative_path, const SGRawValue<float> &rawValue,
1124             bool useDefault = true);
1125
1126
1127   /**
1128    * Bind another node to an external double source.
1129    */
1130   bool tie (const char * relative_path, const SGRawValue<double> &rawValue,
1131             bool useDefault = true);
1132
1133
1134   /**
1135    * Bind another node to an external string source.
1136    */
1137   bool tie (const char * relative_path, const SGRawValue<const char *> &rawValue,
1138             bool useDefault = true);
1139
1140
1141   /**
1142    * Unbind another node from any external data source.
1143    */
1144   bool untie (const char * relative_path);
1145
1146
1147   /**
1148    * Add a change listener to the property.
1149    */
1150   void addChangeListener (SGPropertyChangeListener * listener);
1151
1152
1153   /**
1154    * Remove a change listener from the property.
1155    */
1156   void removeChangeListener (SGPropertyChangeListener * listener);
1157
1158
1159   /**
1160    * Fire a value change event to all listeners.
1161    */
1162   void fireValueChanged ();
1163
1164
1165   /**
1166    * Fire a child-added event to all listeners.
1167    */
1168   void fireChildAdded (SGPropertyNode * child);
1169
1170
1171   /**
1172    * Fire a child-removed event to all listeners.
1173    */
1174   void fireChildRemoved (SGPropertyNode * child);
1175
1176
1177 protected:
1178
1179   void fireValueChanged (SGPropertyNode * node);
1180   void fireChildAdded (SGPropertyNode * parent, SGPropertyNode * child);
1181   void fireChildRemoved (SGPropertyNode * parent, SGPropertyNode * child);
1182
1183   /**
1184    * Protected constructor for making new nodes on demand.
1185    */
1186   SGPropertyNode (const char * name, int index, SGPropertyNode * parent);
1187
1188
1189 private:
1190
1191                                 // Get the raw value
1192   bool get_bool () const;
1193   int get_int () const;
1194   long get_long () const;
1195   float get_float () const;
1196   double get_double () const;
1197   const char * get_string () const;
1198
1199                                 // Set the raw value
1200   bool set_bool (bool value);
1201   bool set_int (int value);
1202   bool set_long (long value);
1203   bool set_float (float value);
1204   bool set_double (double value);
1205   bool set_string (const char * value);
1206
1207
1208   /**
1209    * Clear any existing value and set the type to NONE.
1210    */
1211   void clear_value ();
1212
1213
1214   /**
1215    * Get the value as a string.
1216    */
1217   const char * make_string () const;
1218
1219
1220   /**
1221    * Trace a read access.
1222    */
1223   void trace_read () const;
1224
1225
1226   /**
1227    * Trace a write access.
1228    */
1229   void trace_write () const;
1230
1231
1232   /**
1233    * Increment reference counter
1234    */
1235   void incrementRef();
1236
1237   /**
1238    * Decrement reference counter
1239    */
1240   int decrementRef();
1241
1242   friend class SGPropertyNode_ptr;
1243
1244
1245   mutable char _buffer[MAX_STRING_LEN+1];
1246
1247   class hash_table;
1248
1249   char * _name;
1250   mutable char * _display_name;
1251   int _index;
1252   SGPropertyNode * _parent;
1253   vector<SGPropertyNode_ptr> _children;
1254   vector<SGPropertyNode_ptr> _removedChildren;
1255   mutable char * _path;
1256   hash_table * _path_cache;
1257   Type _type;
1258   bool _tied;
1259   int _attr;
1260   int _count;
1261
1262                                 // The right kind of pointer...
1263   union {
1264     SGPropertyNode * alias;
1265     SGRawValue<bool> * bool_val;
1266     SGRawValue<int> * int_val;
1267     SGRawValue<long> * long_val;
1268     SGRawValue<float> * float_val;
1269     SGRawValue<double> * double_val;
1270     SGRawValue<const char *> * string_val;
1271   } _value;
1272
1273   union {
1274     bool bool_val;
1275     int int_val;
1276     long long_val;
1277     float float_val;
1278     double double_val;
1279     char * string_val;
1280   } _local_val;
1281
1282   vector <SGPropertyChangeListener *> * _listeners;
1283
1284
1285 \f
1286   /**
1287    * A very simple hash table with no remove functionality.
1288    */
1289   class hash_table {
1290   public:
1291
1292     /**
1293      * An entry in a bucket in a hash table.
1294      */
1295     class entry {
1296     public:
1297       entry ();
1298       virtual ~entry ();
1299       virtual const char * get_key () { return _key; }
1300       virtual void set_key (const char * key);
1301       virtual SGPropertyNode * get_value () { return _value; }
1302       virtual void set_value (SGPropertyNode * value);
1303     private:
1304       char * _key;
1305       SGPropertyNode * _value;
1306     };
1307
1308
1309     /**
1310      * A bucket in a hash table.
1311      */
1312     class bucket {
1313     public:
1314       bucket ();
1315       virtual ~bucket ();
1316       virtual entry * get_entry (const char * key, bool create = false);
1317     private:
1318       int _length;
1319       entry ** _entries;
1320     };
1321
1322     friend class bucket;
1323
1324     hash_table ();
1325     virtual ~hash_table ();
1326     virtual SGPropertyNode * get (const char * key);
1327     virtual void put (const char * key, SGPropertyNode * value);
1328
1329   private:
1330     unsigned int hashcode (const char * key);
1331     unsigned int _data_length;
1332     bucket ** _data;
1333   };
1334
1335 };
1336
1337 #endif // __PROPS_HXX
1338
1339 // end of props.hxx