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