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