Jamoma API  0.6.0.a19
TTList.cpp
1 /*
2  * TTBlue (Linked) List Class
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 #include "TTList.h"
10 #include "TTObject.h"
11 #include "TTEnvironment.h"
12 
13 
14 /****************************************************************************************************/
15 
16 TTList::TTList()
17  :mThreadProtection(YES)
18 {
19  mMutex = new TTMutex(false);
20 }
21 
22 
23 TTList::~TTList()
24 {
25  delete mMutex;
26 }
27 
28 
29 TTList::TTList(TTList& that) :
30  mThreadProtection(YES)
31 {
32  mMutex = new TTMutex(false);
33 
34  theList = that.theList;
35 }
36 
37 
38 
39 TTUInt32 TTList::getSize() const
40 {
41  return theList.size();
42 }
43 
44 
45 TTValue& TTList::getHead()
46 {
47  return theList.front();
48 }
49 
50 
51 TTValue& TTList::getTail()
52 {
53  return theList.back();
54 }
55 
56 
57 void TTList::begin()
58 {
59  theIter = theList.begin();
60 }
61 
62 
63 bool TTList::end()
64 {
65  return theIter != theList.end();
66 }
67 
68 
69 void TTList::next()
70 {
71  theIter++;
72 }
73 
74 
75 void TTList::prev()
76 {
77  theIter--;
78 }
79 
80 
81 TTValue& TTList::current()
82 {
83  return *theIter;
84 }
85 
86 
87 TTBoolean TTList::isEmpty()
88 {
89  return theList.empty();
90 }
91 
92 
93 TTErr TTList::getIndex(TTUInt32 index, TTValue& returnedValue)
94 {
96  TTUInt32 i=0;
97 
98  lock();
99  for (TTListIter iter = theList.begin(); iter != theList.end(); ++iter) {
100  if (i==index) {
101  err = kTTErrNone;
102  returnedValue = *iter;
103  break;
104  }
105  i++;
106  }
107  unlock();
108 
109  return err;
110 }
111 
112 
113 void TTList::append(const TTValue& newValue)
114 {
115  lock();
116 // theList.insert(theList.end(), (TTValue*)&newValue);
117  theList.insert(theList.end(), newValue);
118  unlock();
119 }
120 
121 
122 void TTList::appendUnique(const TTValue& newValue)
123 {
124  TTErr err;
125  TTValue foundValue;
126 
127  err = findEquals(newValue, foundValue);
128  if (err == kTTErrValueNotFound)
129  append(newValue);
130 }
131 
132 void TTList::insert(TTUInt32 index, const TTValue& newValue)
133 {
134  TTListIter iter;
135  TTUInt32 i=0;
136 
137  lock();
138  for (iter = theList.begin(); iter != theList.end(); ++iter) {
139  if (i==index) {
140  break;
141  }
142  i++;
143  }
144 
145  theList.insert(iter, newValue);
146  unlock();
147 }
148 
149 void TTList::merge(TTList& newList)
150 {
151  lock();
152  theList.sort();
153  newList.theList.sort();
154  theList.merge(newList.theList);
155  unlock();
156 }
157 
158 
159 void TTList::sort(TTBoolean(*comparisonFunction)(TTValue&, TTValue&))
160 {
161  lock();
162 
163  // If a comparison fonction is given : use it
164  if (comparisonFunction)
165  theList.sort(comparisonFunction);
166 
167  // else use the < operator of TTValue to sort the list
168  else
169  theList.sort();
170  unlock();
171 }
172 
173 
174 TTErr TTList::find(TTFunctionMatch aMatchFunction, TTPtr aBaton, TTValue& returnedValue)
175 {
176  TTErr err = kTTErrGeneric;
177  TTBoolean found = NO;
178 
179  lock();
180  for (TTListIter iter = theList.begin(); iter != theList.end(); ++iter) {
181  TTValue& v = *iter;
182 
183  aMatchFunction(v, aBaton, found);
184  if (found) {
185  returnedValue = v;
186  err = kTTErrNone;
187  break;
188  }
189  }
190  unlock();
191  return err;
192 }
193 
194 
195 TTErr TTList::findEquals(const TTValue& valueToCompareAgainst, TTValue& foundValue)
196 {
198 
199  lock();
200  for (TTListIter iter = theList.begin(); iter != theList.end(); ++iter) {
201  if ((*iter) == valueToCompareAgainst) {
202  foundValue = *iter;
203  err = kTTErrNone;
204  break;
205  }
206  }
207  unlock();
208 
209  return err;
210 }
211 
212 
213 void TTList::remove(const TTValue& aValue)
214 {
215  lock();
216  for (TTListIter iter = theList.begin(); iter != theList.end(); ++iter) {
217  TTValue v = *iter;
218 
219  if (v == aValue) {
220  theList.remove(v);
221  break;
222  }
223  }
224  unlock();
225 }
226 
227 
228 void TTList::clear()
229 {
230  lock();
231  theList.clear();
232  unlock();
233 }
234 
235 
236 void TTList::free()
237 {
238 // lock();
239 
240 // TTValue takes care of memory management of member objects now, so we don't have to worry about it.
241 // for (TTListIter iter = theList.begin(); iter != theList.end(); iter++) {
242 // TTValue& v = *iter;
243 // if (v[0].type() == kTypeObject) {
244 // TTValue
245 // TTObjectBasePtr o = NULL;
246 // v.get(0, &o);
247 // o = v;
248 // TTObjectBaseRelease(&o);
249 // }
250 // delete *iter;
251 // }
252 // theList.clear();
253 // unlock();
254  clear();
255 }
256 
257 
258 void TTList::assignToValue(TTValue& value)
259 {
260  TTListIter iter;
261 
262  value.clear();
263 
264  lock();
265  for (iter = theList.begin(); iter != theList.end(); ++iter) {
266  value.append(*iter);
267  }
268  unlock();
269 }
270 
271 
272 TTErr TTList::iterate(const TTObjectBasePtr target, const TTFunctionWithBatonAndValue callback)
273 {
274  if (theList.empty())
275  return kTTErrNone;
276 
277  lock();
278  for (TTListIter iter = theList.begin(); iter != theList.end(); ++iter) {
279  callback(target, *iter);
280  }
281  unlock();
282  return kTTErrNone;
283 }
284 
285 
286 TTErr TTList::iterate(const TTObjectBasePtr target, const TTSymbol messageName)
287 {
288  if (theList.empty())
289  return kTTErrNone;
290 
291  lock();
292  for (TTListIter iter = theList.begin(); iter != theList.end(); ++iter) {
293  TTValue v;
294  target->sendMessage(messageName, *iter, v);
295  }
296  unlock();
297  return kTTErrNone;
298 }
299 
300 
301 TTErr TTList::iterateObjectsSendingMessage(const TTSymbol messageName)
302 {
303  if (theList.empty())
304  return kTTErrNone;
305 
306  lock();
307  for (TTListIter iter = theList.begin(); iter != theList.end(); ++iter) {
308 #ifdef OLD
309  TTObjectBasePtr obj = NULL;
310 
311  //(iter)->get(0, &obj);
312  obj = iter->at(0);
313  if (obj && obj->valid)
314  obj->sendMessage(messageName);
315 #else // NEW
316  TTObject o = iter->at(0);
317 
318  if (o.valid())
319  o.send(messageName);
320 #endif
321  }
322  unlock();
323  return kTTErrNone;
324 }
325 
326 
327 TTErr TTList::iterateObjectsSendingMessage(const TTSymbol messageName, TTValue& aValue)
328 {
329  if (theList.empty())
330  return kTTErrNone;
331 
332  lock();
333  for (TTListIter iter = theList.begin(); iter != theList.end(); ++iter) {
334 #ifdef OLD
335  TTObjectBasePtr obj = NULL;
336 
337  //(iter)->get(0, &obj);
338  obj = iter->at(0);
339  if (obj && obj->valid) {
340  TTValue v;
341  obj->sendMessage(messageName, aValue, v);
342  }
343 #else // NEW
344  TTObject o = iter->at(0);
345  TTValue unusedReturnValue;
346 
347  if (o.valid())
348  o.send(messageName, aValue);
349 #endif
350  }
351  unlock();
352  return kTTErrNone;
353 }
354 
355 
356 void TTList::lock()
357 {
358  if (mThreadProtection)
359  mMutex->lock();
360 }
361 
362 
363 void TTList::unlock()
364 {
365  if (mThreadProtection)
366  mMutex->unlock();
367 }
368 
TTErr sendMessage(const TTSymbol name)
TODO: Document this function.
bool TTBoolean
Boolean flag, same as Boolean on the Mac.
Definition: TTBase.h:167
TTErr send(const TTSymbol aName)
Send a message to this object with no arguments.
Definition: TTObject.cpp:135
Create Jamoma object instances.
Create and use Jamoma object instances.
Definition: TTObject.h:29
Base class for all first-class Jamoma objects.
Definition: TTObjectBase.h:109
TTBoolean valid
If the object isn't completely built, or is in the process of freeing, this will be false...
Definition: TTObjectBase.h:124
void append(const T &anElementValueToAppend)
Insert a single TTElement at the end.
Definition: TTValue.h:243
void * TTPtr
A generic pointer.
Definition: TTBase.h:248
The TTSymbol class is used to represent a string and efficiently pass and compare that string...
Definition: TTSymbol.h:26
A value was not found when doing a look up for it (in a TTHash, TTList, or other class).
Definition: TTBase.h:352
void(* TTFunctionMatch)(const TTValue &valueToCheck, TTPtr baton, TTBoolean &found)
Use for finding stuff in a list or a hash or a dictionary.
Definition: TTBase.h:262
void clear()
Clear all values from the vector, leaving with size of 0.
Definition: TTValue.h:131
TTEnvironment is a global object providing information on the environemt.
Something went wrong, but what exactly is not known. Typically used for context-specific problems...
Definition: TTBase.h:344
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
TTErr(* TTFunctionWithBatonAndValue)(const TTValue &, const TTValue &)
A simple/generic function pointer with a baton as TTValueRef and the value to send back as a TTValueR...
Definition: TTBase.h:361
std::uint32_t TTUInt32
32 bit unsigned integer
Definition: TTBase.h:178
No Error.
Definition: TTBase.h:343
TTBoolean valid() const
Determine if the object contained by this TTObject is truly ready for use.
Definition: TTObject.cpp:179
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34