]> git.mxchange.org Git - simgear.git/blob - simgear/misc/props.hxx
Patch from Frederic Bouvier:
[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
453 \f
454 /**
455  * A node in a property tree.
456  */
457 class SGPropertyNode
458 {
459 public:
460
461   /**
462    * Public constants.
463    */
464   enum {
465     MAX_STRING_LEN = 1024
466   };
467
468   /**
469    * Property value types.
470    */
471   enum Type {
472     NONE,
473     ALIAS,
474     BOOL,
475     INT,
476     LONG,
477     FLOAT,
478     DOUBLE,
479     STRING,
480     UNSPECIFIED
481   };
482
483
484   /**
485    * Access mode attributes.
486    *
487    * <p>The ARCHIVE attribute is strictly advisory, and controls
488    * whether the property should normally be saved and restored.</p>
489    */
490   enum Attribute {
491     READ = 1,
492     WRITE = 2,
493     ARCHIVE = 4,
494     TRACE_READ = 8,
495     TRACE_WRITE = 16
496   };
497
498
499   /**
500    * Default constructor.
501    */
502   SGPropertyNode ();
503
504
505   /**
506    * Copy constructor.
507    */
508   SGPropertyNode (const SGPropertyNode &node);
509
510
511   /**
512    * Destructor.
513    */
514   virtual ~SGPropertyNode ();
515
516
517
518   //
519   // Basic properties.
520   //
521
522   /**
523    * Test whether this node contains a primitive leaf value.
524    */
525   bool hasValue () const { return (_type != NONE); }
526
527
528   /**
529    * Get the node's simple (XML) name.
530    */
531   const char * getName () const { return _name; }
532
533
534   /**
535    * Get the node's integer index.
536    */
537   const int getIndex () const { return _index; }
538
539
540   /**
541    * Get a non-const pointer to the node's parent.
542    */
543   SGPropertyNode * getParent () { return _parent; }
544
545
546   /**
547    * Get a const pointer to the node's parent.
548    */
549   const SGPropertyNode * getParent () const { return _parent; }
550
551
552   //
553   // Children.
554   //
555
556
557   /**
558    * Get the number of child nodes.
559    */
560   const int nChildren () const { return _children.size(); }
561
562
563   /**
564    * Get a child node by position (*NOT* index).
565    */
566   SGPropertyNode * getChild (int position);
567
568
569   /**
570    * Get a const child node by position (*NOT* index).
571    */
572   const SGPropertyNode * getChild (int position) const;
573
574
575   /**
576    * Get a child node by name and index.
577    */
578   SGPropertyNode * getChild (const char * name, int index = 0,
579                              bool create = false);
580
581
582   /**
583    * Get a const child node by name and index.
584    */
585   const SGPropertyNode * getChild (const char * name, int index = 0) const;
586
587
588   /**
589    * Get a vector of all children with the specified name.
590    */
591   vector<SGPropertyNode *> getChildren (const char * name);
592
593
594   /**
595    * Get a vector all all children (const) with the specified name.
596    */
597   vector<const SGPropertyNode *> getChildren (const char * name) const;
598
599
600   //
601   // Alias support.
602   //
603
604
605   /**
606    * Alias this node's leaf value to another's.
607    */
608   bool alias (SGPropertyNode * target);
609
610
611   /**
612    * Alias this node's leaf value to another's by relative path.
613    */
614   bool alias (const char * path);
615
616
617   /**
618    * Remove any alias for this node.
619    */
620   bool unalias ();
621
622
623   /**
624    * Test whether the node's leaf value is aliased to another's.
625    */
626   bool isAlias () const { return (_type == ALIAS); }
627
628
629   /**
630    * Get a non-const pointer to the current alias target, if any.
631    */
632   SGPropertyNode * getAliasTarget ();
633
634
635   /**
636    * Get a const pointer to the current alias target, if any.
637    */
638   const SGPropertyNode * getAliasTarget () const;
639
640
641   //
642   // Path information.
643   //
644
645
646   /**
647    * Get the path to this node from the root.
648    */
649   const char * getPath (bool simplify = false) const;
650
651
652   /**
653    * Get a pointer to the root node.
654    */
655   SGPropertyNode * getRootNode ();
656
657
658   /**
659    * Get a const pointer to the root node.
660    */
661   const SGPropertyNode * getRootNode () const;
662
663
664   /**
665    * Get a pointer to another node by relative path.
666    */
667   SGPropertyNode * getNode (const char * relative_path, bool create = false);
668
669
670   /**
671    * Get a pointer to another node by relative path.
672    *
673    * This method leaves the index off the last member of the path,
674    * so that the user can specify it separately (and save some
675    * string building).  For example, getNode("/bar[1]/foo", 3) is
676    * exactly equivalent to getNode("bar[1]/foo[3]").  The index
677    * provided overrides any given in the path itself for the last
678    * component.
679    */
680   SGPropertyNode * getNode (const char * relative_path, int index,
681                             bool create = false);
682
683
684   /**
685    * Get a const pointer to another node by relative path.
686    */
687   const SGPropertyNode * getNode (const char * relative_path) const;
688
689
690   /**
691    * Get a const pointer to another node by relative path.
692    *
693    * This method leaves the index off the last member of the path,
694    * so that the user can specify it separate.
695    */
696   const SGPropertyNode * getNode (const char * relative_path,
697                                   int index) const;
698
699
700   //
701   // Access Mode.
702   //
703
704   /**
705    * Check a single mode attribute for the property node.
706    */
707   bool getAttribute (Attribute attr) const { return ((_attr & attr) != 0); }
708
709
710   /**
711    * Set a single mode attribute for the property node.
712    */
713   void setAttribute (Attribute attr, bool state) {
714     (state ? _attr |= attr : _attr &= ~attr);
715   }
716
717
718   /**
719    * Get all of the mode attributes for the property node.
720    */
721   int getAttributes () const { return _attr; }
722
723
724   /**
725    * Set all of the mode attributes for the property node.
726    */
727   void setAttributes (int attr) { _attr = attr; }
728   
729
730   //
731   // Leaf Value (primitive).
732   //
733
734
735   /**
736    * Get the type of leaf value, if any, for this node.
737    */
738   Type getType () const;
739
740
741   /**
742    * Get a bool value for this node.
743    */
744   bool getBoolValue () const;
745
746
747   /**
748    * Get an int value for this node.
749    */
750   int getIntValue () const;
751
752
753   /**
754    * Get a long int value for this node.
755    */
756   long getLongValue () const;
757
758
759   /**
760    * Get a float value for this node.
761    */
762   float getFloatValue () const;
763
764
765   /**
766    * Get a double value for this node.
767    */
768   double getDoubleValue () const;
769
770
771   /**
772    * Get a string value for this node.
773    */
774   const char * getStringValue () const;
775
776
777
778   /**
779    * Set a bool value for this node.
780    */
781   bool setBoolValue (bool value);
782
783
784   /**
785    * Set an int value for this node.
786    */
787   bool setIntValue (int value);
788
789
790   /**
791    * Set a long int value for this node.
792    */
793   bool setLongValue (long value);
794
795
796   /**
797    * Set a float value for this node.
798    */
799   bool setFloatValue (float value);
800
801
802   /**
803    * Set a double value for this node.
804    */
805   bool setDoubleValue (double value);
806
807
808   /**
809    * Set a string value for this node.
810    */
811   bool setStringValue (const char * value);
812
813
814   /**
815    * Set a value of unspecified type for this node.
816    */
817   bool setUnspecifiedValue (const char * value);
818
819
820   //
821   // Data binding.
822   //
823
824
825   /**
826    * Test whether this node is bound to an external data source.
827    */
828   bool isTied () const { return _tied; }
829
830
831   /**
832    * Bind this node to an external bool source.
833    */
834   bool tie (const SGRawValue<bool> &rawValue, bool useDefault = true);
835
836
837   /**
838    * Bind this node to an external int source.
839    */
840   bool tie (const SGRawValue<int> &rawValue, bool useDefault = true);
841
842
843   /**
844    * Bind this node to an external long int source.
845    */
846   bool tie (const SGRawValue<long> &rawValue, bool useDefault = true);
847
848
849   /**
850    * Bind this node to an external float source.
851    */
852   bool tie (const SGRawValue<float> &rawValue, bool useDefault = true);
853
854
855   /**
856    * Bind this node to an external double source.
857    */
858   bool tie (const SGRawValue<double> &rawValue, bool useDefault = true);
859
860
861   /**
862    * Bind this node to an external string source.
863    */
864   bool tie (const SGRawValue<const char *> &rawValue, bool useDefault = true);
865
866
867   /**
868    * Unbind this node from any external data source.
869    */
870   bool untie ();
871
872
873   //
874   // Convenience methods using paths.
875   // TODO: add attribute methods
876   //
877
878
879   /**
880    * Get another node's type.
881    */
882   Type getType (const char * relative_path) const;
883
884
885   /**
886    * Test whether another node has a leaf value.
887    */
888   bool hasValue (const char * relative_path) const;
889
890
891   /**
892    * Get another node's value as a bool.
893    */
894   bool getBoolValue (const char * relative_path,
895                      bool defaultValue = false) const;
896
897
898   /**
899    * Get another node's value as an int.
900    */
901   int getIntValue (const char * relative_path,
902                    int defaultValue = 0) const;
903
904
905   /**
906    * Get another node's value as a long int.
907    */
908   long getLongValue (const char * relative_path,
909                      long defaultValue = 0L) const;
910
911
912   /**
913    * Get another node's value as a float.
914    */
915   float getFloatValue (const char * relative_path,
916                        float defaultValue = 0.0) const;
917
918
919   /**
920    * Get another node's value as a double.
921    */
922   double getDoubleValue (const char * relative_path,
923                          double defaultValue = 0.0L) const;
924
925
926   /**
927    * Get another node's value as a string.
928    */
929   const char * getStringValue (const char * relative_path,
930                                const char * defaultValue = "") const;
931
932
933   /**
934    * Set another node's value as a bool.
935    */
936   bool setBoolValue (const char * relative_path, bool value);
937
938
939   /**
940    * Set another node's value as an int.
941    */
942   bool setIntValue (const char * relative_path, int value);
943
944
945   /**
946    * Set another node's value as a long int.
947    */
948   bool setLongValue (const char * relative_path, long value);
949
950
951   /**
952    * Set another node's value as a float.
953    */
954   bool setFloatValue (const char * relative_path, float value);
955
956
957   /**
958    * Set another node's value as a double.
959    */
960   bool setDoubleValue (const char * relative_path, double value);
961
962
963   /**
964    * Set another node's value as a string.
965    */
966   bool setStringValue (const char * relative_path, const char * value);
967
968
969   /**
970    * Set another node's value with no specified type.
971    */
972   bool setUnspecifiedValue (const char * relative_path, const char * value);
973
974
975   /**
976    * Test whether another node is bound to an external data source.
977    */
978   bool isTied (const char * relative_path) const;
979
980
981   /**
982    * Bind another node to an external bool source.
983    */
984   bool tie (const char * relative_path, const SGRawValue<bool> &rawValue,
985             bool useDefault = true);
986
987
988   /**
989    * Bind another node to an external int source.
990    */
991   bool tie (const char * relative_path, const SGRawValue<int> &rawValue,
992             bool useDefault = true);
993
994
995   /**
996    * Bind another node to an external long int source.
997    */
998   bool tie (const char * relative_path, const SGRawValue<long> &rawValue,
999             bool useDefault = true);
1000
1001
1002   /**
1003    * Bind another node to an external float source.
1004    */
1005   bool tie (const char * relative_path, const SGRawValue<float> &rawValue,
1006             bool useDefault = true);
1007
1008
1009   /**
1010    * Bind another node to an external double source.
1011    */
1012   bool tie (const char * relative_path, const SGRawValue<double> &rawValue,
1013             bool useDefault = true);
1014
1015
1016   /**
1017    * Bind another node to an external string source.
1018    */
1019   bool tie (const char * relative_path, const SGRawValue<const char *> &rawValue,
1020             bool useDefault = true);
1021
1022
1023   /**
1024    * Unbind another node from any external data source.
1025    */
1026   bool untie (const char * relative_path);
1027
1028
1029 protected:
1030
1031
1032   /**
1033    * Protected constructor for making new nodes on demand.
1034    */
1035   SGPropertyNode (const char * name, int index, SGPropertyNode * parent);
1036
1037
1038 private:
1039
1040                                 // Get the raw value
1041   bool get_bool () const;
1042   int get_int () const;
1043   long get_long () const;
1044   float get_float () const;
1045   double get_double () const;
1046   const char * get_string () const;
1047
1048                                 // Set the raw value
1049   bool set_bool (bool value);
1050   bool set_int (int value);
1051   bool set_long (long value);
1052   bool set_float (float value);
1053   bool set_double (double value);
1054   bool set_string (const char * value);
1055
1056
1057   /**
1058    * Clear any existing value and set the type to NONE.
1059    */
1060   void clear_value ();
1061
1062
1063   /**
1064    * Get the value as a string.
1065    */
1066   const char * make_string () const;
1067
1068
1069   /**
1070    * Trace a read access.
1071    */
1072   void trace_read () const;
1073
1074
1075   /**
1076    * Trace a write access.
1077    */
1078   void trace_write () const;
1079
1080   mutable char _buffer[MAX_STRING_LEN+1];
1081
1082   class hash_table;
1083
1084   char * _name;
1085   int _index;
1086   SGPropertyNode * _parent;
1087   vector<SGPropertyNode *> _children;
1088   hash_table * _path_cache;
1089   Type _type;
1090   bool _tied;
1091   int _attr;
1092
1093                                 // The right kind of pointer...
1094   union {
1095     SGPropertyNode * alias;
1096     SGRawValue<bool> * bool_val;
1097     SGRawValue<int> * int_val;
1098     SGRawValue<long> * long_val;
1099     SGRawValue<float> * float_val;
1100     SGRawValue<double> * double_val;
1101     SGRawValue<const char *> * string_val;
1102   } _value;
1103
1104   union {
1105     bool bool_val;
1106     int int_val;
1107     long long_val;
1108     float float_val;
1109     double double_val;
1110     char * string_val;
1111   } _local_val;
1112
1113
1114 \f
1115   /**
1116    * A very simple hash table with no remove functionality.
1117    */
1118   class hash_table {
1119   public:
1120
1121     /**
1122      * An entry in a bucket in a hash table.
1123      */
1124     class entry {
1125     public:
1126       entry ();
1127       virtual ~entry ();
1128       virtual const char * get_key () { return _key; }
1129       virtual void set_key (const char * key);
1130       virtual SGPropertyNode * get_value () { return _value; }
1131       virtual void set_value (SGPropertyNode * value);
1132     private:
1133       char * _key;
1134       SGPropertyNode * _value;
1135     };
1136
1137
1138     /**
1139      * A bucket in a hash table.
1140      */
1141     class bucket {
1142     public:
1143       bucket ();
1144       virtual ~bucket ();
1145       virtual entry * get_entry (const char * key, bool create = false);
1146     private:
1147       int _length;
1148       entry ** _entries;
1149     };
1150
1151     friend class bucket;
1152
1153     hash_table ();
1154     virtual ~hash_table ();
1155     virtual SGPropertyNode * get (const char * key);
1156     virtual void put (const char * key, SGPropertyNode * value);
1157
1158   private:
1159     unsigned int hashcode (const char * key);
1160     unsigned int _data_length;
1161     bucket ** _data;
1162   };
1163
1164 };
1165
1166 #endif // __PROPS_HXX
1167
1168 // end of props.hxx