Jamoma API  0.6.0.a19
CATokenMap.h
1 /* Copyright 2007 Apple Inc. All Rights Reserved.
2 
3  Disclaimer: IMPORTANT: This Apple software is supplied to you by
4  Apple Inc. ("Apple") in consideration of your agreement to the
5  following terms, and your use, installation, modification or
6  redistribution of this Apple software constitutes acceptance of these
7  terms. If you do not agree with these terms, please do not use,
8  install, modify or redistribute this Apple software.
9 
10  In consideration of your agreement to abide by the following terms, and
11  subject to these terms, Apple grants you a personal, non-exclusive
12  license, under Apple's copyrights in this original Apple software (the
13  "Apple Software"), to use, reproduce, modify and redistribute the Apple
14  Software, with or without modifications, in source and/or binary forms;
15  provided that if you redistribute the Apple Software in its entirety and
16  without modifications, you must retain this notice and the following
17  text and disclaimers in all such redistributions of the Apple Software.
18  Neither the name, trademarks, service marks or logos of Apple Inc.
19  may be used to endorse or promote products derived from the Apple
20  Software without specific prior written permission from Apple. Except
21  as expressly stated in this notice, no other rights or licenses, express
22  or implied, are granted by Apple herein, including but not limited to
23  any patent rights that may be infringed by your derivative works or by
24  other works in which the Apple Software may be incorporated.
25 
26  The Apple Software is provided by Apple on an "AS IS" basis. APPLE
27  MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
28  THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
29  FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
30  OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
31 
32  IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
33  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
36  MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
37  AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
38  STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
39  POSSIBILITY OF SUCH DAMAGE.
40 */
41 #if !defined(__CATokenMap_h__)
42 #define __CATokenMap_h__
43 
44 //=============================================================================
45 // Includes
46 //=============================================================================
47 
48 // System Includes
49 #if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
50  #include <CoreAudio/CoreAudioTypes.h>
51 #else
52  #include <CoreAudioTypes.h>
53 #endif
54 
55 // PublicUtility Includes
56 #include "CAMutex.h"
57 
58 // Standard Library Includes
59 #include <map>
60 #include <iterator>
61 
62 //=============================================================================
63 // CATokenMap
64 //=============================================================================
65 
66 template <class T>
67 class CATokenMap
68 {
69 
70 // Types
71 private:
72  typedef std::map<UInt32, T*> TokenMap;
73 
74 // Construction/Destruction
75 public:
76  CATokenMap() : mTokenMap(), mNextToken(256), mTokenMapMutex("CATokenMap Mutex") {}
77  ~CATokenMap() {}
78 
79 // Operations
80 public:
81  bool Lock()
82  {
83  return mTokenMapMutex.Lock();
84  }
85 
86  void Unlock()
87  {
88  mTokenMapMutex.Unlock();
89  }
90 
91  UInt32 GetToken(T* inObject) const
92  {
93  CAMutex::Locker theLocker(const_cast<CAMutex&>(mTokenMapMutex));
94  UInt32 theAnswer = 0;
95  typename TokenMap::const_iterator i = mTokenMap.begin();
96  while((theAnswer == 0) && (i != mTokenMap.end()))
97  {
98  if(i->second == inObject)
99  {
100  theAnswer = i->first;
101  }
102  std::advance(i, 1);
103  }
104  return theAnswer;
105  }
106 
107  T* GetObject(UInt32 inToken) const
108  {
109  CAMutex::Locker theLocker(const_cast<CAMutex&>(mTokenMapMutex));
110  typename TokenMap::const_iterator i = mTokenMap.find(inToken);
111  return i != mTokenMap.end() ? i->second : NULL;
112  }
113 
114  T* GetObject(const void* inToken) const
115  {
116  #if __LP64__
117  return GetObject((UInt32)((UInt64)inToken));
118  #else
119  return GetObject((UInt32)inToken);
120  #endif
121  }
122 
123  UInt32 GetNumberObjects() const
124  {
125  CAMutex::Locker theLocker(const_cast<CAMutex&>(mTokenMapMutex));
126  return mTokenMap.size();
127  }
128 
129  T* GetObjectByIndex(UInt32 inIndex) const
130  {
131  T* theAnswer = NULL;
132  CAMutex::Locker theLocker(const_cast<CAMutex&>(mTokenMapMutex));
133  if(inIndex < mTokenMap.size())
134  {
135  typename TokenMap::const_iterator i = mTokenMap.begin();
136  std::advance(i, inIndex);
137  theAnswer = (i != mTokenMap.end()) ? i->second : NULL;
138  }
139  return theAnswer;
140  }
141 
142  void AddMapping(UInt32 inToken, T* inObject)
143  {
144  CAMutex::Locker theLocker(mTokenMapMutex);
145  typename TokenMap::iterator i = mTokenMap.find(inToken);
146  if(i != mTokenMap.end())
147  {
148  i->second = inObject;
149  }
150  else
151  {
152  mTokenMap.insert(typename TokenMap::value_type(inToken, inObject));
153  }
154  }
155 
156  void RemoveMapping(UInt32 inToken, T* /*inObject*/)
157  {
158  CAMutex::Locker theLocker(mTokenMapMutex);
159  typename TokenMap::iterator i = mTokenMap.find(inToken);
160  if(i != mTokenMap.end())
161  {
162  mTokenMap.erase(i);
163  }
164  }
165 
166  UInt32 GetNextToken()
167  {
168  return mNextToken++;
169  }
170 
171  UInt32 MapObject(T* inObject)
172  {
173  CAMutex::Locker theLocker(mTokenMapMutex);
174  UInt32 theToken = GetNextToken();
175  mTokenMap.insert(typename TokenMap::value_type(theToken, inObject));
176  return theToken;
177  }
178 
179  void UnmapObject(T* inObject)
180  {
181  CAMutex::Locker theLocker(mTokenMapMutex);
182  bool isDone = false;
183  typename TokenMap::iterator i = mTokenMap.begin();
184  while(!isDone && (i != mTokenMap.end()))
185  {
186  if(i->second == inObject)
187  {
188  mTokenMap.erase(i);
189  isDone = true;
190  }
191  else
192  {
193  std::advance(i, 1);
194  }
195  }
196  }
197 
198 // Implementation
199 private:
200  TokenMap mTokenMap;
201  UInt32 mNextToken;
202  CAMutex mTokenMapMutex;
203 
204 };
205 
206 #endif