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