FAUST compiler  0.9.9.6b8
symbol.hh
Go to the documentation of this file.
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 #include <map>
00045 
00046 using namespace std;
00047 
00048 //--------------------------------SYMBOL-------------------------------------
00049 
00053 class Symbol
00054 {
00055     
00056  private:
00057          
00058     static const int    kHashTableSize = 511;                   
00059     static Symbol*      gSymbolTable[kHashTableSize];           
00060     
00061     
00062  // Fields
00063     char*           fName;                                      
00064     unsigned int            fHash;                                      
00065     Symbol*         fNext;                                      
00066     void*           fData;                                      
00067     
00068  // Constructors & destructors
00069     Symbol (const char* str, unsigned int hsh, Symbol* nxt);    
00070    ~Symbol ();                                                  
00071     
00072  // Others
00073     bool            equiv (unsigned int hash, const char* str) const ;  
00074     static unsigned int     calcHashKey (const char* str);              
00075 
00076  // Static methods
00077     static Symbol*      get (const string& str);                
00078     static Symbol*      get (const char* str);                  
00079     static Symbol*      prefix (const char* str);               
00080     static bool         isnew (const char* str);                
00081     
00082  public:
00083     ostream&                print (ostream& fout) const;                                
00084 
00085     friend Symbol*          symbol (const char* str);
00086     friend Symbol*          symbol (const string& str);
00087     friend Symbol*          unique (const char* str);
00088     friend const char*  name (Symbol* sym);
00089     
00090     friend void*            getUserData (Symbol* sym);
00091     friend void             setUserData (Symbol* sym, void* d);
00092         
00093 };
00094 
00095 inline Symbol*          symbol (const char* str)    { return Symbol::get(str); }    
00096 inline Symbol*          symbol (const string& str)  { return Symbol::get(str); }    
00097 inline Symbol*          unique (const char* str)    { return Symbol::prefix(str);}  
00098 inline const char*  name (Symbol* sym)          { return sym->fName; }          
00099     
00100 inline void*            getUserData (Symbol* sym)           { return sym->fData; }      
00101 inline void             setUserData (Symbol* sym, void* d)  { sym->fData=d; }           
00102 
00103 inline ostream& operator << (ostream& s, const Symbol& n) { return n.print(s); }
00104 
00105 
00106 typedef Symbol* Sym;
00107 
00108 #endif