Jamoma API  0.6.0.a19
TTDictionaryBase.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup foundationLibrary
4  *
5  * @brief Foundation Dictionary Class.
6  *
7  * @details The dictionary is a data structure that combines the fast lookup of a hashtable,
8  * but may be sorted like a linked-list.
9  * The linked list contains the key-value pairs of the hash a linked-list of TTKeyValues.
10  *
11  * @author Timothy Place, Trond Lossius
12  *
13  * @copyright Copyright © 2010, Timothy Place @n
14  * This code is licensed under the terms of the "New BSD License" @n
15  * http://creativecommons.org/licenses/BSD/
16  */
17 
18 #include "TTDictionaryBase.h"
19 #include "TTSymbolTable.h"
20 #include "TTSymbolCache.h"
21 
22 //void TTDictionaryBaseFindKeyInList(const TTValue& valueToCheck, TTPtr baton, TTBoolean& found);
23 //void TTDictionaryBaseFindKeyInList(const TTValue& valueToCheck, TTPtr baton, TTBoolean& found)
24 //{
25 // TTKeyValPtr keyval = TTKeyValPtr(TTPtr(valueToCheck));
26 // TTSymbol key = TTSymbol((TTSymbolBase*)baton);
27  //
28 // if (keyval && TTSymbol((TTSymbolBase*)keyval->first) == key)
29 // found = YES;
30 //}
31 
32 
33 /****************************************************************************************************/
34 
35 TTDictionaryBase::TTDictionaryBase() :
36  mReferenceCount(0)
37 {
38 // mHashTable = new TTHash;
39 // mList = new TTList;
40 }
41 
42 
43 TTDictionaryBase::~TTDictionaryBase()
44 {
45 // delete mHashTable;
46 // delete mList;
47 }
48 
49 
51 {
52  return append(kTTSym_schema, schemaName);
53 }
54 
55 
57 {
58  TTValue v;
59 
60  lookup(kTTSym_schema, v);
61  return v;
62 }
63 
64 
66 {
67  return append(kTTSym_value, newValue);
68 }
69 
70 
72 {
73  return lookup(kTTSym_value, returnedValue);
74 }
75 
76 
78 {
79 // TTValue v = new TTKeyVal(TTPtrSizedInt(&key), value);
80 // lock();
81  remove(key);
82 // mMap.insert(TTDictionaryBasePair(key.getSymbolId(), value));
83  mMap.insert(TTDictionaryBasePair((TTPtrSizedInt)key.rawpointer(), value));
84 // unlock();
85  sendNotification("change", TTValue());
86  return kTTErrNone;
87 
88 // mList->append(v);
89 // return mMap->append(key, value);
90 // return kTTErrNone;
91 }
92 
93 
94 
96 {
97 // return mHashTable->lookup(key, value);
98 // lock();
99 // TTHashMap* theMap = (TTHashMap*)mHashMap;
100 // TTDictionaryBaseMapIterK iter = mMap.find(key.getSymbolId());
101  TTDictionaryBaseMapIterK iter = mMap.find((TTPtrSizedInt)key.rawpointer());
102 
103  // TTPtrSizedInt a = iter->first;
104  // TTSymbol* b = (TTSymbol*)a;
105  // TTValue v = iter->second;
106  // TTValue v = (*theMap)[TTPtrSizedInt(&key)];
107 
108  if (iter != mMap.end()) {
109  value = iter->second;
110 // unlock();
111  return kTTErrNone;
112 
113  }
114  else {
115  return kTTErrValueNotFound;
116  }
117 
118 }
119 
120 
122 {
123 // TTValue v;
124 // TTErr err;
125 
126 // err = mList->find(TTDictionaryBaseFindKeyInList, key.rawpointer(), v);
127 // if (!err)
128 // mList->remove(v);
129 // return mHashTable->remove(key);
130 // mMap.erase(key.getSymbolId());
131  mMap.erase((TTPtrSizedInt)key.rawpointer());
132  sendNotification("change", TTValue());
133  return kTTErrNone;
134 }
135 
136 
138 {
139 // mList->clear();
140  mMap.clear();
141  sendNotification("change", TTValue());
142  return kTTErrNone;
143 }
144 
145 
147 {
148 // lock();
149  hashKeys.clear();
150 
151  for (TTDictionaryBaseMapIter iter = mMap.begin(); iter != mMap.end(); iter++) {
152  TTPtrSizedInt a = iter->first;
153  TTSymbol b((TTSymbolBase*)a);
154  //TTValue v = iter->second;
155  //hashKeys.append(TTSymbolRef(*(TTSymbol*)iter->first));
156  hashKeys.append(b);
157  }
158 // unlock();
159  return kTTErrNone;
160 }
161 
162 
164 {
165  return mMap.size();
166 }
167 
168 
170 {
171  return mMap.empty();
172 }
173 
174 
175 TTErr TTDictionaryBase::registerObserverForNotifications(const TTObjectBase& observingObject)
176 {
177  TTValue v = observingObject;
178  mObservers.appendUnique(v);
179  return kTTErrNone;
180 }
181 
182 
183 TTErr TTDictionaryBase::unregisterObserverForNotifications(const TTObjectBase& observingObject)
184 {
185  TTValue c(observingObject);
186  TTValue v;
187  TTErr err;
188 
189  err = mObservers.findEquals(c, v);
190  if (!err)
191  mObservers.remove(v);
192  return err;
193 }
194 
195 
196 TTErr TTDictionaryBase::sendNotification(const TTSymbol name, const TTValue& arguments)
197 {
198  return mObservers.iterateObjectsSendingMessage(name, TTValueRef(arguments));
199 }
200 
TTErr setSchema(const TTSymbol schemaName)
TODO: Add documentation schemaName TODO: Add documentation.
bool TTBoolean
Boolean flag, same as Boolean on the Mac.
Definition: TTBase.h:167
TTErr setValue(const TTValue &newValue)
TODO: Add documentation.
Base class for all first-class Jamoma objects.
Definition: TTObjectBase.h:109
The TTSymbolBase class is used to represent a string and efficiently pass and compare that string...
Definition: TTSymbolBase.h:28
const TTSymbol getSchema() const
TODO: Add documentation.
void append(const T &anElementValueToAppend)
Insert a single TTElement at the end.
Definition: TTValue.h:243
TTErr clear()
Remove all items from the hash table.
TTPtr rawpointer() const
Get the value of the raw pointer into the symbol table.
Definition: TTSymbol.h:134
TTBoolean isEmpty()
Return true if the hash has nothing stored in it.
The TTSymbol class is used to represent a string and efficiently pass and compare that string...
Definition: TTSymbol.h:26
TTUInt32 getSize()
Return the number of keys in the hash table.
A value was not found when doing a look up for it (in a TTHash, TTList, or other class).
Definition: TTBase.h:352
TTErr lookup(const TTSymbol key, TTValue &value) const
Find the value for the given key.
void clear()
Clear all values from the vector, leaving with size of 0.
Definition: TTValue.h:131
long TTPtrSizedInt
An integer that is the same size as a pointer.
Definition: TTBase.h:240
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
TTErr append(const TTSymbol key, const TTValue &value)
Insert an item into the hash table.
std::uint32_t TTUInt32
32 bit unsigned integer
Definition: TTBase.h:178
TTErr remove(const TTSymbol key)
Remove an item from the hash table.
No Error.
Definition: TTBase.h:343
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34
Foundation Dictionary Class.
TTErr getValue(TTValue &returnedValue) const
TODO: Add documentation.
TTErr getKeys(TTValue &hashKeys)
Get an array of all of the keys for the hash table.