FAUST compiler  0.9.9.6b8
symbol.hh
Go to the documentation of this file.
1 /************************************************************************
2  ************************************************************************
3  FAUST compiler
4  Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale
5  ---------------------------------------------------------------------
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  ************************************************************************
20  ************************************************************************/
21 
40 #ifndef __SYMBOL__
41 #define __SYMBOL__
42 
43 #include <string>
44 #include <map>
45 
46 using namespace std;
47 
48 //--------------------------------SYMBOL-------------------------------------
49 
53 class Symbol
54 {
55 
56  private:
57 
58  static const int kHashTableSize = 511;
59  static Symbol* gSymbolTable[kHashTableSize];
60 
61 
62  // Fields
63  char* fName;
64  unsigned int fHash;
66  void* fData;
67 
68  // Constructors & destructors
69  Symbol (const char* str, unsigned int hsh, Symbol* nxt);
70  ~Symbol ();
71 
72  // Others
73  bool equiv (unsigned int hash, const char* str) const ;
74  static unsigned int calcHashKey (const char* str);
75 
76  // Static methods
77  static Symbol* get (const string& str);
78  static Symbol* get (const char* str);
79  static Symbol* prefix (const char* str);
80  static bool isnew (const char* str);
81 
82  public:
83  ostream& print (ostream& fout) const;
84 
85  friend Symbol* symbol (const char* str);
86  friend Symbol* symbol (const string& str);
87  friend Symbol* unique (const char* str);
88  friend const char* name (Symbol* sym);
89 
90  friend void* getUserData (Symbol* sym);
91  friend void setUserData (Symbol* sym, void* d);
92 
93 };
94 
95 inline Symbol* symbol (const char* str) { return Symbol::get(str); }
96 inline Symbol* symbol (const string& str) { return Symbol::get(str); }
97 inline Symbol* unique (const char* str) { return Symbol::prefix(str);}
98 inline const char* name (Symbol* sym) { return sym->fName; }
99 
100 inline void* getUserData (Symbol* sym) { return sym->fData; }
101 inline void setUserData (Symbol* sym, void* d) { sym->fData=d; }
102 
103 inline ostream& operator << (ostream& s, const Symbol& n) { return n.print(s); }
104 
105 
106 typedef Symbol* Sym;
107 
108 #endif
void setUserData(Symbol *sym, void *d)
Set user data.
Definition: symbol.hh:101
Symbol * unique(const char *str)
Returns a new unique symbol of name strxxx.
Definition: symbol.hh:97
ostream & print(ostream &fout) const
print a symbol on a stream
Definition: symbol.cpp:174
static Symbol * get(const string &str)
Get the symbol of name str.
Definition: symbol.cpp:46
ostream & operator<<(ostream &s, const Symbol &n)
Definition: symbol.hh:103
void * fData
Field to user disposal to store additional data.
Definition: symbol.hh:66
Symbol * fNext
Next symbol in the hash table entry.
Definition: symbol.hh:65
static Symbol * prefix(const char *str)
Creates a new symbol of name prefixed by str.
Definition: symbol.cpp:102
char * fName
Name of the symbol.
Definition: symbol.hh:63
unsigned int fHash
Hash key computed from the name and used to determine the hash table entry.
Definition: symbol.hh:64
const char * name(Symbol *sym)
Returns the name of a symbol.
Definition: symbol.hh:98
Symbols are unique objects with a name stored in a hash table.
Definition: symbol.hh:53
Symbol * Sym
Definition: symbol.hh:106
void * getUserData(Symbol *sym)
Returns user data.
Definition: symbol.hh:100
Symbol * symbol(const char *str)
Returns (and creates if new) the symbol of name str.
Definition: symbol.hh:95
void print(Tree t, FILE *out)
Definition: list.cpp:154