sbFixedInterfaceCache.h
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2008 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25 */
26 #ifndef SBFIXEDINTERFACECACHE_H_
27 #define SBFIXEDINTERFACECACHE_H_
28 
29 #include <utility>
30 
31 #include <nsAutoLock.h>
32 #include <nsTArray.h>
33 
45 template <class KeyClass, class Interface>
47 {
48 public:
49  typedef typename KeyClass::KeyType KeyType;
50 
55  sbFixedInterfaceCache(PRUint32 aSize) : mKeys(aSize),
56  mCurrent(aSize),
57  mSize(aSize)
58  {
59  NS_ASSERTION(aSize, "sbFixedInterfaceCache must have a size > 0");
60  // Fill out array so that we don't have to worry about using append later
61  mKeys.AppendElements(mSize);
62  mCacheMap.Init(aSize);
63  }
68  {
69  for (PRUint32 index = 0; index < mSize; ++index)
70  {
71  Interface * obj = nsnull;
72  mCacheMap.Get(mKeys[index], &obj);
73  NS_IF_RELEASE(obj);
74  }
75  }
80  void Put(KeyType aKey, Interface * aValue)
81  {
82  NS_IF_ADDREF(aValue);
83  if (mCurrent == 0)
84  mCurrent = mSize - 1;
85  else
86  --mCurrent;
87  // Clean up the old entry
88  Interface * oldObject = nsnull;
89  nsString const & oldKey = mKeys[mCurrent];
90  if (!oldKey.IsEmpty() && mCacheMap.Get(oldKey, &oldObject) && oldObject) {
91  NS_RELEASE(oldObject);
92  mCacheMap.Remove(oldKey);
93  }
94  // Save off the new one
95  mKeys[mCurrent] = aKey;
96  mCacheMap.Put(aKey, aValue);
97  }
104  Interface * Get(KeyType aKey) const
105  {
106  Interface * obj = nsnull;
107  // If this fails, obj is null which is what we want to return in that case
108  mCacheMap.Get(aKey, &obj);
109  return obj;
110  }
111 private:
112  // Holds the list of keys and is used for aging entries out
113  // nsStringHashKey has sucky traits so we have to hard code string here
114  nsTArray<nsString> mKeys;
115  // Provides a fast lookup non-owning map
116  nsDataHashtable<KeyClass, Interface*> mCacheMap;
117  PRUint32 mCurrent;
118  PRUint32 const mSize;
119 };
120 
121 #endif /* SBFIXEDINTERFACECACHE_H_ */
onPageChanged aValue
Definition: FeedWriter.js:1395
Interface * Get(KeyType aKey) const
void Put(KeyType aKey, Interface *aValue)
sbFixedInterfaceCache(PRUint32 aSize)