00001 /************************************************************************ 00002 ************************************************************************ 00003 FAUST compiler 00004 Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale 00005 --------------------------------------------------------------------- 00006 This program is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 2 of the License, or 00009 (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program; if not, write to the Free Software 00018 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00019 ************************************************************************ 00020 ************************************************************************/ 00021 00040 #ifndef __SYMBOL__ 00041 #define __SYMBOL__ 00042 00043 #include <string> 00044 00045 using namespace std; 00046 00047 //--------------------------------SYMBOL------------------------------------- 00048 00052 class Symbol 00053 { 00054 00055 private: 00056 00057 static const int kHashTableSize = 511; 00058 static Symbol* gSymbolTable[kHashTableSize]; 00059 00060 00061 // Fields 00062 char* fName; 00063 unsigned int fHash; 00064 Symbol* fNext; 00065 void* fData; 00066 00067 // Constructors & destructors 00068 Symbol (const char* str, unsigned int hsh, Symbol* nxt); 00069 ~Symbol () {} 00070 00071 // Others 00072 bool equiv (unsigned int hash, const char* str) const ; 00073 static unsigned int calcHashKey (const char* str); 00074 00075 // Static methods 00076 static Symbol* get (const string& str); 00077 static Symbol* get (const char* str); 00078 static Symbol* prefix (const char* str); 00079 static bool isnew (const char* str); 00080 00081 public: 00082 ostream& print (ostream& fout) const; 00083 00084 friend Symbol* symbol (const char* str); 00085 friend Symbol* symbol (const string& str); 00086 friend Symbol* unique (const char* str); 00087 friend const char* name (Symbol* sym); 00088 00089 friend void* getUserData (Symbol* sym); 00090 friend void setUserData (Symbol* sym, void* d); 00091 00092 }; 00093 00094 inline Symbol* symbol (const char* str) { return Symbol::get(str); } 00095 inline Symbol* symbol (const string& str) { return Symbol::get(str); } 00096 inline Symbol* unique (const char* str) { return Symbol::prefix(str);} 00097 inline const char* name (Symbol* sym) { return sym->fName; } 00098 00099 inline void* getUserData (Symbol* sym) { return sym->fData; } 00100 inline void setUserData (Symbol* sym, void* d) { sym->fData=d; } 00101 00102 inline ostream& operator << (ostream& s, const Symbol& n) { return n.print(s); } 00103 00104 00105 typedef Symbol* Sym; 00106 00107 #endif