Jamoma API  0.6.0.a19
TTSymbolTable.cpp
1 /*
2  * Jamoma Class that maintains a table of symbols
3  * Copyright © 2008, Timothy Place
4  *
5  * License: This code is licensed under the terms of the "New BSD License"
6  * http://creativecommons.org/licenses/BSD/
7  */
8 
9 
10 
11 #include "TTSymbolTable.h"
12 #include "TTMutex.h"
13 #include "TTValue.h"
14 
15 #include<iostream>
16 #include<unordered_map>
17 
18 
19 typedef std::unordered_map<TTString, TTSymbolBase*> TTSymbolTableHash;
20 
21 
22 /** A type that represents the key as a C-String and the value as a pointer to the matching TTSymbol object. */
23 typedef std::pair<TTString, TTSymbolBase*> TTSymbolTablePair;
24 
25 
26 /** An iterator for the STL hash_map used by TTSymbolTable. */
27 typedef TTSymbolTableHash::const_iterator TTSymbolTableIter;
28 
29 
30 
31 static TTMutex* sMutex = NULL;
32 
33 
34 #define mSYMBOLTABLE ((TTSymbolTableHash*)(mSymbolTable))
35 
36 
37 /****************************************************************************************************/
38 
39 TTSymbolTable::TTSymbolTable(TTBoolean createEmptyTable)
40 {
41  if (!sMutex)
42  sMutex = new TTMutex(true);
43  mSymbolTable = (TTPtr) new TTSymbolTableHash;
44 
45  // subclasses of the symbol table will want to initialize this themselves
46  if (!createEmptyTable)
47  mSYMBOLTABLE->insert(TTSymbolTablePair(TTString(""), createEntry(TTString(""), 0)) );
48 }
49 
50 
51 TTSymbolTable::~TTSymbolTable()
52 {
53  TTSymbolTableIter iter;
54 
55  for (iter = mSYMBOLTABLE->begin(); iter != mSYMBOLTABLE->end(); iter++)
56  delete (TTSymbolBase*)(iter->second);
57  mSYMBOLTABLE->clear();
58  delete mSYMBOLTABLE;
59  // TODO: we should reference count symbol tables and then free the mutex here, yes?
60 }
61 
62 
64 {
65  return new TTSymbolBase(aString, TTPtrSizedInt(this), newSymbolId);
66 }
67 
68 
69 TTSymbolBase* TTSymbolTable::lookup(const char* aString)
70 {
71 #if defined( TT_PLATFORM_WIN ) && OLD
72  TTSymbolTableIter iter;
73 
74  sMutex->lock();
75 
76  iter = mSYMBOLTABLE->find(aString);
77  if (iter == mSYMBOLTABLE->end()) {
78  // The symbol wasn't found in the table, so we need to create and add it.
79  // TTLogMessage("Adding symbol: %s With Address: %x", aString, aString);
80  TTSymbolBase* newSymbol = createEntry(aString, mSYMBOLTABLE->size());
81  mSYMBOLTABLE->insert(TTSymbolTablePair(newSymbol->string(), newSymbol));
82  sMutex->unlock();
83  return *newSymbol;
84  }
85  else {
86  // The symbol was found, so we return it.
87  sMutex->unlock();
88  return iter->second;
89  }
90 #else
91  const TTString s(aString);
92  return lookup(s);
93 #endif
94 }
95 
96 
98 {
99 // if (!gTTSymbolTable) // symbol table hasn't been created yet!
100 // gTTSymbolTable = new TTSymbolTable;
101 //
102 // TTSymbolTable *self = gTTSymbolTable; // can't rely on 'this' in the case where we just created the table!
103 //TTSymbolTable *self = &gTTSymbolTable;
104 #if defined( TT_PLATFORM_WIN ) && OLD
105  return self->lookup(aString.c_str());
106 #else
107  TTSymbolTableIter iter;
108 
109  sMutex->lock();
110 
111  iter = ((TTSymbolTableHash*)(mSymbolTable))->find(aString);
112  if (iter == ((TTSymbolTableHash*)(mSymbolTable))->end()) {
113  // The symbol wasn't found in the table, so we need to create and add it.
114  // TTLogMessage("Adding symbol: %s With Address: %x", aString, aString);
115  TTSymbolBase* newSymbol = createEntry(aString, ((TTSymbolTableHash*)(mSymbolTable))->size());
116  ((TTSymbolTableHash*)(mSymbolTable))->insert(TTSymbolTablePair(newSymbol->string(), newSymbol));
117  sMutex->unlock();
118  return newSymbol;
119  }
120  else {
121  // The symbol was found, so we return it.
122  sMutex->unlock();
123  return iter->second;
124  }
125 #endif
126 }
127 
128 
129 TTSymbolBase* TTSymbolTable::lookup(const int& aNumberToBeConvertedToAString)
130 {
131 #if defined( TT_PLATFORM_WIN ) && OLD
132  char cString[16];
133 
134  snprintf(cString, 16, "%d", aNumberToBeConvertedToAString);
135  return lookup(cString);
136 #else
137  char cString[16];
138 
139  snprintf(cString, 16, "%d", aNumberToBeConvertedToAString);
140 
141  TTString s(cString);
142  return lookup(s);
143 #endif
144 }
145 
146 
147 void TTSymbolTable::dump(/*TTValue& allSymbols*/)
148 {
149  TTSymbolTableIter iter;
150 
151  TTLogMessage("---- DUMPING SYMBOL TABLE -- BEGIN ----\n");
152  //allSymbols.clear();
153  for (iter = mSYMBOLTABLE->begin(); iter != mSYMBOLTABLE->end(); iter++) {
154  //allSymbols.append(TTSymbolRef(iter->second));
155  TTLogMessage("KEY:%s VALUE:%s\n", (iter->first).c_str(), ((TTSymbolBase*)(iter->second))->getCString());
156  }
157  TTLogMessage("---- DUMPING SYMBOL TABLE -- END ----\n");
158 }
bool TTBoolean
Boolean flag, same as Boolean on the Mac.
Definition: TTBase.h:167
TTSymbolBase * lookup(const char *aString)
Look in the symbol table for this string.
const char * c_str() const
Return a pointer to the internal C-string.
Definition: TTString.h:83
The TTSymbolBase class is used to represent a string and efficiently pass and compare that string...
Definition: TTSymbolBase.h:28
void * TTPtr
A generic pointer.
Definition: TTBase.h:248
virtual TTSymbolBase * createEntry(const TTString &aString, TTInt32 newSymbolId)
Overridable method used internally when constructing a new TTSymbolBase.
std::int32_t TTInt32
32 bit signed integer
Definition: TTBase.h:177
void TTFOUNDATION_EXPORT TTLogMessage(TTImmutableCString message,...)
Platform and host independent method for posting log messages.
Definition: TTBase.cpp:534
long TTPtrSizedInt
An integer that is the same size as a pointer.
Definition: TTBase.h:240
The TTString class is used to represent a string.
Definition: TTString.h:34
Provides a common way of representing composite values.
void dump()
Debugging tool to make it easy to examine everything that is in the symbol table. ...