00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef __UT_SymbolTable_h__
00019 #define __UT_SymbolTable_h__
00020
00021 #include "UT_API.h"
00022 #include <stdio.h>
00023 #include <SYS/SYS_Types.h>
00024 #include "UT_Thing.h"
00025 #include "UT_SymList.h"
00026
00027 class UT_API UT_SymbolTable
00028 {
00029 public:
00030 explicit UT_SymbolTable();
00031 virtual ~UT_SymbolTable();
00032
00033 void addSymbol(const char *symbol, UT_Thing data);
00034 int findSymbol(const char *symbol, UT_Thing *datap) const;
00035
00036 int contains(const char *symbol) const
00037 { UT_Thing value; return findSymbol(symbol, &value); }
00038
00039 template <typename T>
00040 T *findPointer(const char *symbol) const
00041 {
00042 UT_Thing thing;
00043 if (!findSymbol(symbol, &thing))
00044 return NULL;
00045 return thing.template asPointer<T>();
00046 }
00047
00048 int findOrCreateSymbol(const char *symbol, UT_Thing **datap);
00049 int deleteSymbol(const char *symbol);
00050 void clear();
00051
00052 const char *addSymbolAndGetReference(const char *symbol, UT_Thing data);
00053
00054
00055 unsigned entries() const { return myNbrEntries; }
00056 bool empty() const { return !myNbrEntries; }
00057
00058
00059
00060 UT_Thing &operator[](const char *symbol);
00061
00062
00063
00064
00065 float getMaxLoadFactor() const { return myMaxLoadFactor; }
00066 void setMaxLoadFactor(float max_load_factor);
00067 float getMinLoadFactor() const { return myMinLoadFactor; }
00068 void setMinLoadFactor(float min_load_factor);
00069
00070
00071 void mergeTable(const UT_SymbolTable &table);
00072
00073
00074
00075
00076 const char *getStringReference(const char *symbol);
00077
00078
00079
00080
00081
00082
00083 char *newName(const char *symbol) const;
00084
00085 void outputStats(FILE *output) const;
00086 void dumpTable(FILE *output) const;
00087
00088 int64 getMemUsage(bool only_this=false) const;
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100 int traverseConst(
00101 int (*function)(UT_Thing &, const char *, void *),
00102 void *data) const;
00103 int traverse(int (*function)(UT_Thing &, const char *, void *),
00104 void *data);
00105
00106 class UT_API traverser
00107 {
00108 public:
00109 traverser()
00110 : myCurrList(0), myEndList(0) { }
00111 traverser(const traverser &src)
00112 { *this = src; }
00113 ~traverser() { }
00114
00115 UT_Thing &thing() { return myCurrSym.thing(); }
00116 const char *name() const { return myCurrSym.name(); }
00117
00118 int operator==(const traverser &cmp) const
00119 {
00120 return ((!myCurrList && !cmp.myCurrList) ||
00121 ((myCurrList == cmp.myCurrList) &&
00122 (myCurrSym == cmp.myCurrSym)));
00123 }
00124 int operator!=(const traverser &cmp) const
00125 { return !(*this == cmp); }
00126
00127 traverser &operator++()
00128 {
00129 if( ++myCurrSym == myCurrList->end() )
00130 {
00131 ++myCurrList;
00132 advanceToNextList();
00133 }
00134 return *this;
00135 }
00136
00137 const traverser &operator=(const traverser &src)
00138 {
00139 myCurrList = src.myCurrList;
00140 myEndList = src.myEndList;
00141 myCurrSym = src.myCurrSym;
00142 return *this;
00143 }
00144
00145 private:
00146 traverser(const UT_SymList *list, const UT_SymList *endlist)
00147 : myCurrList(list), myEndList(endlist)
00148 { advanceToNextList(); }
00149
00150 void advanceToNextList()
00151 {
00152 while( myCurrList != myEndList &&
00153 myCurrList->begin() == myCurrList->end() )
00154 ++myCurrList;
00155 if( myCurrList == myEndList )
00156 myCurrList = 0;
00157 else
00158 myCurrSym = myCurrList->begin();
00159 }
00160
00161 const UT_SymList *myCurrList;
00162 const UT_SymList *myEndList;
00163 UT_SymList::traverser myCurrSym;
00164
00165 friend class UT_SymbolTable;
00166 };
00167
00168 traverser begin() const
00169 {
00170 if (myTable)
00171 return traverser(myTable, myTable + mySize);
00172 return traverser();
00173 }
00174 traverser end() const { return traverser(); }
00175
00176 private:
00177 #ifndef INTEL_COMPILER
00178
00179
00180
00181 UT_SymbolTable(const UT_SymbolTable&) { }
00182 #endif
00183 UT_SymbolTable& operator=(const UT_SymbolTable&);
00184
00185 private:
00186 enum UT_AdjustType { UT_ADJUST_INCREASE, UT_ADJUST_DECREASE };
00187 void adjustTableSize(UT_AdjustType adjust_type);
00188
00189
00190 UT_SymList *myTable;
00191 int mySize;
00192 unsigned int myNbrEntries;
00193 fpreal myMaxLoadFactor;
00194 fpreal myMinLoadFactor;
00195 bool myIsTraversing;
00196 };
00197
00198
00199 class UT_API UT_SnapshotTable : public UT_SymbolTable
00200 {
00201 public:
00202 UT_SnapshotTable() : UT_SymbolTable() {}
00203 virtual ~UT_SnapshotTable();
00204
00205 virtual void setSnapshotWatch(UT_Thing watch) = 0;
00206 virtual void snapshot(const char *symbol) = 0;
00207 virtual void updateWatch(const char *symbol) = 0;
00208 void getSnapshot(const char *symbol, UT_Thing *data) const
00209 {
00210 if (!findSymbol(symbol, data))
00211 data->value.longv = 0L;
00212 }
00213
00214 protected:
00215 virtual void getWatch(UT_Thing *watch) const = 0;
00216 private:
00217 };
00218
00219 #endif