Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / xpcom / glue / nsClassHashtable.h
blob53ec165f83d2be98ce95e6cd2187bbdba698e16e
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is C++ hashtable templates.
17 * The Initial Developer of the Original Code is
18 * Benjamin Smedberg.
19 * Portions created by the Initial Developer are Copyright (C) 2002
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #ifndef nsClassHashtable_h__
39 #define nsClassHashtable_h__
41 #include "nsBaseHashtable.h"
42 #include "nsHashKeys.h"
43 #include "nsAutoPtr.h"
45 /**
46 * templated hashtable class maps keys to C++ object pointers.
47 * See nsBaseHashtable for complete declaration.
48 * @param KeyClass a wrapper-class for the hashtable key, see nsHashKeys.h
49 * for a complete specification.
50 * @param Class the class-type being wrapped
51 * @see nsInterfaceHashtable, nsClassHashtable
53 template<class KeyClass,class T>
54 class nsClassHashtable :
55 public nsBaseHashtable< KeyClass, nsAutoPtr<T>, T* >
57 public:
58 typedef typename KeyClass::KeyType KeyType;
59 typedef T* UserDataType;
61 /**
62 * @copydoc nsBaseHashtable::Get
63 * @param pData if the key doesn't exist, pData will be set to nsnull.
65 PRBool Get(KeyType aKey, UserDataType* pData) const;
69 /**
70 * Thread-safe version of nsClassHashtable
71 * @param KeyClass a wrapper-class for the hashtable key, see nsHashKeys.h
72 * for a complete specification.
73 * @param Class the class-type being wrapped
74 * @see nsInterfaceHashtable, nsClassHashtable
76 template<class KeyClass,class T>
77 class nsClassHashtableMT :
78 public nsBaseHashtableMT< KeyClass, nsAutoPtr<T>, T* >
80 public:
81 typedef typename KeyClass::KeyType KeyType;
82 typedef T* UserDataType;
84 /**
85 * @copydoc nsBaseHashtable::Get
86 * @param pData if the key doesn't exist, pData will be set to nsnull.
88 PRBool Get(KeyType aKey, UserDataType* pData) const;
93 // nsClassHashtable definitions
96 template<class KeyClass,class T>
97 PRBool
98 nsClassHashtable<KeyClass,T>::Get(KeyType aKey, T** retVal) const
100 typename nsBaseHashtable<KeyClass,nsAutoPtr<T>,T*>::EntryType* ent =
101 GetEntry(aKey);
103 if (ent)
105 if (retVal)
106 *retVal = ent->mData;
108 return PR_TRUE;
111 if (retVal)
112 *retVal = nsnull;
114 return PR_FALSE;
119 // nsClassHashtableMT definitions
122 template<class KeyClass,class T>
123 PRBool
124 nsClassHashtableMT<KeyClass,T>::Get(KeyType aKey, T** retVal) const
126 PR_Lock(this->mLock);
128 typename nsBaseHashtableMT<KeyClass,nsAutoPtr<T>,T*>::EntryType* ent =
129 GetEntry(aKey);
131 if (ent)
133 if (retVal)
134 *retVal = ent->mData;
136 PR_Unlock(this->mLock);
138 return PR_TRUE;
141 if (retVal)
142 *retVal = nsnull;
144 PR_Unlock(this->mLock);
146 return PR_FALSE;
149 #endif // nsClassHashtable_h__