head 1.4; access; symbols; locks; strict; comment @# @; 1.4 date 2002.09.07.01.50.39; author curt; state Exp; branches; next 1.3; 1.3 date 2002.08.31.19.01.56; author curt; state Exp; branches; next 1.2; 1.2 date 2002.08.31.18.54.02; author curt; state Exp; branches; next 1.1; 1.1 date 2002.02.15.17.21.59; author curt; state Exp; branches; next ; desc @@ 1.4 log @Updated doxygen docs. @ text @
Main Page Class Hierarchy Compound List File List Compound Members File Members

# props.hxx

Go to the documentation of this file.

    00001
    00012 #ifndef __PROPS_HXX
    00013 #define __PROPS_HXX
    00014
    00015 #ifndef PROPS_STANDALONE
    00016 #define PROPS_STANDALONE 0
    00017 #endif
    00018
    00019 #include <vector>
    00020
    00021 #if PROPS_STANDALONE
    00022
    00023 #include <string>
    00024 #include <iostream>
    00025
    00026 using std::string;
    00027 using std::vector;
    00028 using std::istream;
    00029 using std::ostream;
    00030
    00031 #else
    00032
    00033 #include <simgear/compiler.h>
    00034 #include <simgear/debug/logstream.hxx>
    00035 #include STL_STRING
    00036 #include STL_IOSTREAM
    00037 SG_USING_STD(string);
    00038 SG_USING_STD(vector);
    00039 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
    00040 SG_USING_STD(istream);
    00041 SG_USING_STD(ostream);
    00042 #endif
    00043
    00044 #endif
    00045
    00046
    00047 #ifdef NONE
    00048 #pragma warn A sloppy coder has defined NONE as a macro!
    00049 #undef NONE
    00050 #endif
    00051
    00052 #ifdef ALIAS
    00053 #pragma warn A sloppy coder has defined ALIAS as a macro!
    00054 #undef ALIAS
    00055 #endif
    00056
    00057 #ifdef UNSPECIFIED
    00058 #pragma warn A sloppy coder has defined UNSPECIFIED as a macro!
    00059 #undef UNSPECIFIED
    00060 #endif
    00061
    00062 #ifdef BOOL
    00063 #pragma warn A sloppy coder has defined BOOL as a macro!
    00064 #undef BOOL
    00065 #endif
    00066
    00067 #ifdef INT
    00068 #pragma warn A sloppy coder has defined INT as a macro!
    00069 #undef INT
    00070 #endif
    00071
    00072 #ifdef LONG
    00073 #pragma warn A sloppy coder has defined LONG as a macro!
    00074 #undef LONG
    00075 #endif
    00076
    00077 #ifdef FLOAT
    00078 #pragma warn A sloppy coder has defined FLOAT as a macro!
    00079 #undef FLOAT
    00080 #endif
    00081
    00082 #ifdef DOUBLE
    00083 #pragma warn A sloppy coder has defined DOUBLE as a macro!
    00084 #undef DOUBLE
    00085 #endif
    00086
    00087 #ifdef STRING
    00088 #pragma warn A sloppy coder has defined STRING as a macro!
    00089 #undef STRING
    00090 #endif
    00091
    00092
    00093
    00095 // A raw value.
    00096 //
    00097 // This is the mechanism that information-providing routines can
    00098 // use to link their own values to the property manager.  Any
    00099 // SGValue can be tied to a raw value and then untied again.
    00100 //
    00101 // Note: we are forced to use inlined methods here to ensure
    00102 // that the templates will be instantiated.  We're probably taking
    00103 // a small performance hit for that.
    00105
    00106
    00144 template <class T>
    00145 class SGRawValue
    00146 {
    00147 public:
    00148
    00158   static const T DefaultValue;  // Default for this kind of raw value.
    00159
    00160
    00166   SGRawValue () {}
    00167
    00168
    00172   virtual ~SGRawValue () {}
    00173
    00174
    00181   virtual T getValue () const = 0;
    00182
    00183
    00196   virtual bool setValue (T value) = 0;
    00197
    00198
    00207   virtual SGRawValue * clone () const = 0;
    00208 };
    00209
    00210
    00221 template <class T>
    00222 class SGRawValuePointer : public SGRawValue<T>
    00223 {
    00224 public:
    00225
    00235   SGRawValuePointer (T * ptr) : _ptr(ptr) {}
    00236
    00240   virtual ~SGRawValuePointer () {}
    00241
    00248   virtual T getValue () const { return *_ptr; }
    00249
    00256   virtual bool setValue (T value) { *_ptr = value; return true; }
    00257
    00263   virtual SGRawValue<T> * clone () const {
    00264     return new SGRawValuePointer<T>(_ptr);
    00265   }
    00266
    00267 private:
    00268   T * _ptr;
    00269 };
    00270
    00271
    00278 template <class T>
    00279 class SGRawValueFunctions : public SGRawValue<T>
    00280 {
    00281 public:
    00282
    00286   typedef T (*getter_t)();
    00287
    00291   typedef void (*setter_t)(T);
    00292
    00303   SGRawValueFunctions (getter_t getter = 0, setter_t setter = 0)
    00304     : _getter(getter), _setter(setter) {}
    00305
    00309   virtual ~SGRawValueFunctions () {}
    00310
    00318   virtual T getValue () const {
    00319     if (_getter) return (*_getter)();
    00320     else return SGRawValue<T>::DefaultValue;
    00321   }
    00322
    00330   virtual bool setValue (T value) {
    00331     if (_setter) { (*_setter)(value); return true; }
    00332     else return false;
    00333   }
    00334
    00338   virtual SGRawValue<T> * clone () const {
    00339     return new SGRawValueFunctions<T>(_getter,_setter);
    00340   }
    00341
    00342 private:
    00343   getter_t _getter;
    00344   setter_t _setter;
    00345 };
    00346
    00347
    00358 template <class T>
    00359 class SGRawValueFunctionsIndexed : public SGRawValue<T>
    00360 {
    00361 public:
    00362   typedef T (*getter_t)(int);
    00363   typedef void (*setter_t)(int,T);
    00364   SGRawValueFunctionsIndexed (int index, getter_t getter = 0, setter_t setter = 0)
    00365     : _index(index), _getter(getter), _setter(setter) {}
    00366   virtual ~SGRawValueFunctionsIndexed () {}
    00367   virtual T getValue () const {
    00368     if (_getter) return (*_getter)(_index);
    00369     else return SGRawValue<T>::DefaultValue;
    00370   }
    00371   virtual bool setValue (T value) {
    00372     if (_setter) { (*_setter)(_index, value); return true; }
    00373     else return false;
    00374   }
    00375   virtual SGRawValue<T> * clone () const {
    00376     return new SGRawValueFunctionsIndexed<T>(_index, _getter, _setter);
    00377   }
    00378 private:
    00379   int _index;
    00380   getter_t _getter;
    00381   setter_t _setter;
    00382 };
    00383
    00384
    00391 template <class C, class T>
    00392 class SGRawValueMethods : public SGRawValue<T>
    00393 {
    00394 public:
    00395   typedef T (C::*getter_t)() const;
    00396   typedef void (C::*setter_t)(T);
    00397   SGRawValueMethods (C &obj, getter_t getter = 0, setter_t setter = 0)
    00398     : _obj(obj), _getter(getter), _setter(setter) {}
    00399   virtual ~SGRawValueMethods () {}
    00400   virtual T getValue () const {
    00401     if (_getter) { return (_obj.*_getter)(); }
    00402     else { return SGRawValue<T>::DefaultValue; }
    00403   }
    00404   virtual bool setValue (T value) {
    00405     if (_setter) { (_obj.*_setter)(value); return true; }
    00406     else return false;
    00407   }
    00408   virtual SGRawValue<T> * clone () const {
    00409     return new SGRawValueMethods<C,T>(_obj, _getter, _setter);
    00410   }
    00411 private:
    00412   C &_obj;
    00413   getter_t _getter;
    00414   setter_t _setter;
    00415 };
    00416
    00417
    00424 template <class C, class T>
    00425 class SGRawValueMethodsIndexed : public SGRawValue<T>
    00426 {
    00427 public:
    00428   typedef T (C::*getter_t)(int) const;
    00429   typedef void (C::*setter_t)(int, T);
    00430   SGRawValueMethodsIndexed (C &obj, int index,
    00431                      getter_t getter = 0, setter_t setter = 0)
    00432     : _obj(obj), _index(index), _getter(getter), _setter(setter) {}
    00433   virtual ~SGRawValueMethodsIndexed () {}
    00434   virtual T getValue () const {
    00435     if (_getter) { return (_obj.*_getter)(_index); }
    00436     else { return SGRawValue<T>::DefaultValue; }
    00437   }
    00438   virtual bool setValue (T value) {
    00439     if (_setter) { (_obj.*_setter)(_index, value); return true; }
    00440     else return false;
    00441   }
    00442   virtual SGRawValue<T> * clone () const {
    00443     return new SGRawValueMethodsIndexed<C,T>(_obj, _index, _getter, _setter);
    00444   }
    00445 private:
    00446   C &_obj;
    00447   int _index;
    00448   getter_t _getter;
    00449   setter_t _setter;
    00450 };
    00451
    00452
    00456 class SGPropertyNode;
    00457 class SGPropertyNode_ptr
    00458 {
    00459 public:
    00460
    00464   SGPropertyNode_ptr();
    00465
    00469   SGPropertyNode_ptr( const SGPropertyNode_ptr &r );
    00470
    00474   SGPropertyNode_ptr( SGPropertyNode *p );
    00475
    00479   ~SGPropertyNode_ptr();
    00480
    00484   SGPropertyNode_ptr &operator=( const SGPropertyNode_ptr &r );
    00485
    00489   SGPropertyNode *operator->();
    00490
    00494   const SGPropertyNode *operator->() const;
    00495
    00499   operator SGPropertyNode *();
    00500
    00504   operator const SGPropertyNode *() const;
    00505
    00509   SGPropertyNode * ptr () { return _ptr; }
    00510
    00514   bool valid() const;
    00515
    00516 private:
    00517
    00518   SGPropertyNode *_ptr;
    00519 };
    00520
    00521
    00522
    00529 class SGPropertyChangeListener
    00530 {
    00531 public:
    00532   virtual ~SGPropertyChangeListener ();
    00533   virtual void valueChanged (SGPropertyNode * node);
    00534   virtual void childAdded (SGPropertyNode * parent, SGPropertyNode * child);
    00535   virtual void childRemoved (SGPropertyNode * parent, SGPropertyNode * child);
    00536
    00537 protected:
    00538   friend class SGPropertyNode;
    00539   virtual void register_property (SGPropertyNode * node);
    00540   virtual void unregister_property (SGPropertyNode * node);
    00541
    00542 private:
    00543   vector<SGPropertyNode *> _properties;
    00544 };
    00545
    00546
    00547
    00551 class SGPropertyNode
    00552 {
    00553 public:
    00554
    00558   enum {
    00559     MAX_STRING_LEN = 1024
    00560   };
    00561
    00565   enum Type {
    00566     NONE,
    00567     ALIAS,
    00568     BOOL,
    00569     INT,
    00570     LONG,
    00571     FLOAT,
    00572     DOUBLE,
    00573     STRING,
    00574     UNSPECIFIED
    00575   };
    00576
    00577
    00584   enum Attribute {
    00585     READ = 1,
    00586     WRITE = 2,
    00587     ARCHIVE = 4,
    00588     REMOVED = 8,
    00589     TRACE_READ = 16,
    00590     TRACE_WRITE = 32
    00591   };
    00592
    00593
    00598   static const int LAST_USED_ATTRIBUTE;
    00599
    00600
    00604   SGPropertyNode ();
    00605
    00606
    00610   SGPropertyNode (const SGPropertyNode &node);
    00611
    00612
    00616   virtual ~SGPropertyNode ();
    00617
    00618
    00619
    00620   //
    00621   // Basic properties.
    00622   //
    00623
    00627   bool hasValue () const { return (_type != NONE); }
    00628
    00629
    00633   const char * getName () const { return _name; }
    00634
    00635
    00639   const char * getDisplayName (bool simplify = false) const;
    00640
    00641
    00645   int getIndex () const { return _index; }
    00646
    00647
    00651   SGPropertyNode * getParent () { return _parent; }
    00652
    00653
    00657   const SGPropertyNode * getParent () const { return _parent; }
    00658
    00659
    00660   //
    00661   // Children.
    00662   //
    00663
    00664
    00668   int nChildren () const { return _children.size(); }
    00669
    00670
    00674   SGPropertyNode * getChild (int position);
    00675
    00676
    00680   const SGPropertyNode * getChild (int position) const;
    00681
    00682
    00686   bool hasChild (const char * name, int index = 0) const
    00687   {
    00688     return (getChild(name, index) != 0);
    00689   }
    00690
    00691
    00695   SGPropertyNode * getChild (const char * name, int index = 0,
    00696                              bool create = false);
    00697
    00698
    00702   const SGPropertyNode * getChild (const char * name, int index = 0) const;
    00703
    00704
    00708   vector<SGPropertyNode_ptr> getChildren (const char * name) const;
    00709
    00710
    00714   SGPropertyNode_ptr removeChild (const char * name, int index = 0,
    00715                                   bool keep = true);
    00716
    00717
    00718   //
    00719   // Alias support.
    00720   //
    00721
    00722
    00726   bool alias (SGPropertyNode * target);
    00727
    00728
    00732   bool alias (const char * path);
    00733
    00734
    00738   bool unalias ();
    00739
    00740
    00744   bool isAlias () const { return (_type == ALIAS); }
    00745
    00746
    00750   SGPropertyNode * getAliasTarget ();
    00751
    00752
    00756   const SGPropertyNode * getAliasTarget () const;
    00757
    00758
    00759   //
    00760   // Path information.
    00761   //
    00762
    00763
    00767   const char * getPath (bool simplify = false) const;
    00768
    00769
    00773   SGPropertyNode * getRootNode ();
    00774
    00775
    00779   const SGPropertyNode * getRootNode () const;
    00780
    00781
    00785   SGPropertyNode * getNode (const char * relative_path, bool create = false);
    00786
    00787
    00798   SGPropertyNode * getNode (const char * relative_path, int index,
    00799                             bool create = false);
    00800
    00801
    00805   const SGPropertyNode * getNode (const char * relative_path) const;
    00806
    00807
    00814   const SGPropertyNode * getNode (const char * relative_path,
    00815                                   int index) const;
    00816
    00817
    00818   //
    00819   // Access Mode.
    00820   //
    00821
    00825   bool getAttribute (Attribute attr) const { return ((_attr & attr) != 0); }
    00826
    00827
    00831   void setAttribute (Attribute attr, bool state) {
    00832     (state ? _attr |= attr : _attr &= ~attr);
    00833   }
    00834
    00835
    00839   int getAttributes () const { return _attr; }
    00840
    00841
    00845   void setAttributes (int attr) { _attr = attr; }
    00846
    00847
    00848   //
    00849   // Leaf Value (primitive).
    00850   //
    00851
    00852
    00856   Type getType () const;
    00857
    00858
    00862   bool getBoolValue () const;
    00863
    00864
    00868   int getIntValue () const;
    00869
    00870
    00874   long getLongValue () const;
    00875
    00876
    00880   float getFloatValue () const;
    00881
    00882
    00886   double getDoubleValue () const;
    00887
    00888
    00892   const char * getStringValue () const;
    00893
    00894
    00895
    00899   bool setBoolValue (bool value);
    00900
    00901
    00905   bool setIntValue (int value);
    00906
    00907
    00911   bool setLongValue (long value);
    00912
    00913
    00917   bool setFloatValue (float value);
    00918
    00919
    00923   bool setDoubleValue (double value);
    00924
    00925
    00929   bool setStringValue (const char * value);
    00930
    00931
    00935   bool setUnspecifiedValue (const char * value);
    00936
    00937
    00938   //
    00939   // Data binding.
    00940   //
    00941
    00942
    00946   bool isTied () const { return _tied; }
    00947
    00948
    00952   bool tie (const SGRawValue<bool> &rawValue, bool useDefault = true);
    00953
    00954
    00958   bool tie (const SGRawValue<int> &rawValue, bool useDefault = true);
    00959
    00960
    00964   bool tie (const SGRawValue<long> &rawValue, bool useDefault = true);
    00965
    00966
    00970   bool tie (const SGRawValue<float> &rawValue, bool useDefault = true);
    00971
    00972
    00976   bool tie (const SGRawValue<double> &rawValue, bool useDefault = true);
    00977
    00978
    00982   bool tie (const SGRawValue<const char *> &rawValue, bool useDefault = true);
    00983
    00984
    00988   bool untie ();
    00989
    00990
    00991   //
    00992   // Convenience methods using paths.
    00993   // TODO: add attribute methods
    00994   //
    00995
    00996
    01000   Type getType (const char * relative_path) const;
    01001
    01002
    01006   bool hasValue (const char * relative_path) const;
    01007
    01008
    01012   bool getBoolValue (const char * relative_path,
    01013                      bool defaultValue = false) const;
    01014
    01015
    01019   int getIntValue (const char * relative_path,
    01020                    int defaultValue = 0) const;
    01021
    01022
    01026   long getLongValue (const char * relative_path,
    01027                      long defaultValue = 0L) const;
    01028
    01029
    01033   float getFloatValue (const char * relative_path,
    01034                        float defaultValue = 0.0) const;
    01035
    01036
    01040   double getDoubleValue (const char * relative_path,
    01041                          double defaultValue = 0.0L) const;
    01042
    01043
    01047   const char * getStringValue (const char * relative_path,
    01048                                const char * defaultValue = "") const;
    01049
    01050
    01054   bool setBoolValue (const char * relative_path, bool value);
    01055
    01056
    01060   bool setIntValue (const char * relative_path, int value);
    01061
    01062
    01066   bool setLongValue (const char * relative_path, long value);
    01067
    01068
    01072   bool setFloatValue (const char * relative_path, float value);
    01073
    01074
    01078   bool setDoubleValue (const char * relative_path, double value);
    01079
    01080
    01084   bool setStringValue (const char * relative_path, const char * value);
    01085
    01086
    01090   bool setUnspecifiedValue (const char * relative_path, const char * value);
    01091
    01092
    01096   bool isTied (const char * relative_path) const;
    01097
    01098
    01102   bool tie (const char * relative_path, const SGRawValue<bool> &rawValue,
    01103             bool useDefault = true);
    01104
    01105
    01109   bool tie (const char * relative_path, const SGRawValue<int> &rawValue,
    01110             bool useDefault = true);
    01111
    01112
    01116   bool tie (const char * relative_path, const SGRawValue<long> &rawValue,
    01117             bool useDefault = true);
    01118
    01119
    01123   bool tie (const char * relative_path, const SGRawValue<float> &rawValue,
    01124             bool useDefault = true);
    01125
    01126
    01130   bool tie (const char * relative_path, const SGRawValue<double> &rawValue,
    01131             bool useDefault = true);
    01132
    01133
    01137   bool tie (const char * relative_path, const SGRawValue<const char *> &rawValue,
    01138             bool useDefault = true);
    01139
    01140
    01144   bool untie (const char * relative_path);
    01145
    01146
    01150   void addChangeListener (SGPropertyChangeListener * listener);
    01151
    01152
    01156   void removeChangeListener (SGPropertyChangeListener * listener);
    01157
    01158
    01162   void fireValueChanged ();
    01163
    01164
    01168   void fireChildAdded (SGPropertyNode * child);
    01169
    01170
    01174   void fireChildRemoved (SGPropertyNode * child);
    01175
    01176
    01177 protected:
    01178
    01179   void fireValueChanged (SGPropertyNode * node);
    01180   void fireChildAdded (SGPropertyNode * parent, SGPropertyNode * child);
    01181   void fireChildRemoved (SGPropertyNode * parent, SGPropertyNode * child);
    01182
    01186   SGPropertyNode (const char * name, int index, SGPropertyNode * parent);
    01187
    01188
    01189 private:
    01190
    01191                                 // Get the raw value
    01192   bool get_bool () const;
    01193   int get_int () const;
    01194   long get_long () const;
    01195   float get_float () const;
    01196   double get_double () const;
    01197   const char * get_string () const;
    01198
    01199                                 // Set the raw value
    01200   bool set_bool (bool value);
    01201   bool set_int (int value);
    01202   bool set_long (long value);
    01203   bool set_float (float value);
    01204   bool set_double (double value);
    01205   bool set_string (const char * value);
    01206
    01207
    01211   void clear_value ();
    01212
    01213
    01217   const char * make_string () const;
    01218
    01219
    01223   void trace_read () const;
    01224
    01225
    01229   void trace_write () const;
    01230
    01231
    01235   void incrementRef();
    01236
    01240   int decrementRef();
    01241
    01242   friend class SGPropertyNode_ptr;
    01243
    01244
    01245   mutable char _buffer[MAX_STRING_LEN+1];
    01246
    01247   class hash_table;
    01248
    01249   char * _name;
    01250   mutable char * _display_name;
    01251   int _index;
    01252   SGPropertyNode * _parent;
    01253   vector<SGPropertyNode_ptr> _children;
    01254   vector<SGPropertyNode_ptr> _removedChildren;
    01255   mutable char * _path;
    01256   hash_table * _path_cache;
    01257   Type _type;
    01258   bool _tied;
    01259   int _attr;
    01260   int _count;
    01261
    01262                                 // The right kind of pointer...
    01263   union {
    01264     SGPropertyNode * alias;
    01265     SGRawValue<bool> * bool_val;
    01266     SGRawValue<int> * int_val;
    01267     SGRawValue<long> * long_val;
    01268     SGRawValue<float> * float_val;
    01269     SGRawValue<double> * double_val;
    01270     SGRawValue<const char *> * string_val;
    01271   } _value;
    01272
    01273   union {
    01274     bool bool_val;
    01275     int int_val;
    01276     long long_val;
    01277     float float_val;
    01278     double double_val;
    01279     char * string_val;
    01280   } _local_val;
    01281
    01282   vector <SGPropertyChangeListener *> * _listeners;
    01283
    01284
    01285
    01289   class hash_table {
    01290   public:
    01291
    01295     class entry {
    01296     public:
    01297       entry ();
    01298       virtual ~entry ();
    01299       virtual const char * get_key () { return _key; }
    01300       virtual void set_key (const char * key);
    01301       virtual SGPropertyNode * get_value () { return _value; }
    01302       virtual void set_value (SGPropertyNode * value);
    01303     private:
    01304       char * _key;
    01305       SGPropertyNode * _value;
    01306     };
    01307
    01308
    01312     class bucket {
    01313     public:
    01314       bucket ();
    01315       virtual ~bucket ();
    01316       virtual entry * get_entry (const char * key, bool create = false);
    01317     private:
    01318       int _length;
    01319       entry ** _entries;
    01320     };
    01321
    01322     friend class bucket;
    01323
    01324     hash_table ();
    01325     virtual ~hash_table ();
    01326     virtual SGPropertyNode * get (const char * key);
    01327     virtual void put (const char * key, SGPropertyNode * value);
    01328
    01329   private:
    01330     unsigned int hashcode (const char * key);
    01331     unsigned int _data_length;
    01332     bucket ** _data;
    01333   };
    01334
    01335 };
    01336
    01337 #endif // __PROPS_HXX
    01338
    01339 // end of props.hxx

@ 1.3 log @Oops, try again. @ text @d800 1 a800 1