Jamoma API  0.6.0.a19
TTHash.h
1 /*
2  * Jamoma Hash Table 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 #ifndef __TT_HASH_H__
10 #define __TT_HASH_H__
11 
12 #include "TTBase.h"
13 #include "TTValue.h"
14 #include "TTMutex.h"
15 
16 
17 /** A type that contains a key and a value. */
18 typedef std::pair<TTPtrSizedInt,TTValue> TTKeyVal;
19 typedef TTKeyVal* TTKeyValPtr;
20 typedef void (*TTHashIteratorType)(TTPtr, const TTKeyVal&);
21 
22 
23 /****************************************************************************************************/
24 // Class Specification
25 
26 /**
27  Maintain a collection of TTValue objects indexed by TTSymbol pointers.
28  The TTValue objects can themselves include TTSymbol pointers, TTObjects, simple values, polymorphic arrays, etc.
29 
30  TTHash is implemented as a light wrapper class around the STL hash_map templates.
31  While we could inherit from hash_map and then override the appropriate things, there are a few reasons to just contain the hash_map and wrap it.
32  - For example, we may later make TTHash inherit from TTObject and want to address it with messages.
33  - This approach isolates coders from having to worry about an template craziness
34  - This approach offers the possibility, should the need ever arise, to change the underlying implementation away from STL.
35 */
36 class TTFOUNDATION_EXPORT TTHash {
37 private:
38  TTPtr mHashMap;
39  TTBoolean mThreadProtection; ///< Use thread safety mechanisms. Only disable this if you are certain that you will be calling from a single thread.
40  TTMutexPtr mMutex;
41 
42  void lock();
43  void unlock();
44 
45 public:
46  TTHash();
47  virtual ~TTHash();
48 
49  TTHash(TTHash& that);
50 
51  /** Insert an item into the hash table. */
52  TTErr append(const TTSymbol key, const TTValue& value);
53 
54  // Used by the Ruby extension
55  TTErr append(const TTPtr key, const TTValue& value);
56 
57  /** Find the value for the given key. */
58  TTErr lookup(const TTSymbol key, TTValue& value);
59 
60  // This is used by the Ruby extension
61  TTErr lookup(const TTPtr key, TTValue& value);
62 
63  /** Remove an item from the hash table. */
64  TTErr remove(const TTSymbol key);
65 
66  /** Remove all items from the hash table. */
67  TTErr clear();
68 
69  /** For each item in the hash, run specified function. */
70  TTErr iterate(const TTPtr target, const TTHashIteratorType callback);
71 
72  /** Get an array of all of the keys for the hash table. */
73  TTErr getKeys(TTValue& hashKeys);
74 
75  /** Get an array of all of the keys sorted for the hash table. */
76  TTErr getKeysSorted(TTValue& hashKeysSorted, TTBoolean(*comparisonFunction)(TTValue&, TTValue&) = NULL);
77 
78  /** Return the number of keys in the hash table. */
79  TTUInt32 getSize();
80 
81  /** Return true if the hash has nothing stored in it. */
82  TTBoolean isEmpty();
83 
84  void setThreadProtection(TTBoolean threadProtection)
85  {
86  mThreadProtection = threadProtection;
87  }
88 };
89 
90 
91 typedef TTHash* TTHashPtr;
92 
93 
94 #endif // __TT_HASH_H__
95 
bool TTBoolean
Boolean flag, same as Boolean on the Mac.
Definition: TTBase.h:167
Maintain a collection of TTValue objects indexed by TTSymbol pointers.
Definition: TTHash.h:36
void * TTPtr
A generic pointer.
Definition: TTBase.h:248
Jamoma's lowest-level base class and related infrastructure.
The TTSymbol class is used to represent a string and efficiently pass and compare that string...
Definition: TTSymbol.h:26
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
std::uint32_t TTUInt32
32 bit unsigned integer
Definition: TTBase.h:178
Provides a common way of representing composite values.
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34