VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/ds/nsHashKeys.h@ 4837

Last change on this file since 4837 was 1, checked in by vboxsync, 54 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.1 KB
Line 
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
4 *
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/
9 *
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.
14 *
15 * The Original Code is C++ hashtable templates.
16 *
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.
21 *
22 * Contributor(s):
23 *
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.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38#ifndef nsTHashKeys_h__
39#define nsTHashKeys_h__
40
41#include "nsAString.h"
42#include "nsString.h"
43#include "nsID.h"
44#include "nsCRT.h"
45#include "nsReadableUtils.h"
46#include "nsISupports.h"
47#include "nsCOMPtr.h"
48#include "pldhash.h"
49#include NEW_H
50
51/** @file nsHashKeys.h
52 * standard HashKey classes for nsBaseHashtable and relatives. Each of these
53 * classes follows the nsTHashtable::EntryType specification
54 *
55 * Lightweight keytypes provided here:
56 * nsStringHashKey
57 * nsCStringHashKey
58 * nsUint32HashKey
59 * nsISupportsHashKey
60 * nsIDHashKey
61 * nsDepCharHashKey
62 */
63
64/**
65 * hashkey wrapper using nsAString KeyType
66 *
67 * @see nsTHashtable::EntryType for specification
68 */
69class NS_COM nsStringHashKey : public PLDHashEntryHdr
70{
71public:
72 typedef const nsAString& KeyType;
73 typedef const nsAString* KeyTypePointer;
74
75 nsStringHashKey(KeyTypePointer aStr) : mStr(*aStr) { }
76 nsStringHashKey(const nsStringHashKey& toCopy) : mStr(toCopy.mStr) { }
77 ~nsStringHashKey() { }
78
79 KeyType GetKey() const { return mStr; }
80 KeyTypePointer GetKeyPointer() const { return &mStr; }
81 PRBool KeyEquals(const KeyTypePointer aKey) const
82 {
83 return mStr.Equals(*aKey);
84 }
85
86 static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
87 static PLDHashNumber HashKey(const KeyTypePointer aKey)
88 {
89 return HashString(*aKey);
90 }
91 enum { ALLOW_MEMMOVE = PR_TRUE };
92
93private:
94 const nsString mStr;
95};
96
97/**
98 * hashkey wrapper using nsACString KeyType
99 *
100 * @see nsTHashtable::EntryType for specification
101 */
102class NS_COM nsCStringHashKey : public PLDHashEntryHdr
103{
104public:
105 typedef const nsACString& KeyType;
106 typedef const nsACString* KeyTypePointer;
107
108 nsCStringHashKey(const nsACString* aStr) : mStr(*aStr) { }
109 nsCStringHashKey(const nsCStringHashKey& toCopy) : mStr(toCopy.mStr) { }
110 ~nsCStringHashKey() { }
111
112 KeyType GetKey() const { return mStr; }
113 KeyTypePointer GetKeyPointer() const { return &mStr; }
114
115 PRBool KeyEquals(KeyTypePointer aKey) const { return mStr.Equals(*aKey); }
116
117 static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
118 static PLDHashNumber HashKey(KeyTypePointer aKey)
119 {
120 return HashString(*aKey);
121 }
122 enum { ALLOW_MEMMOVE = PR_TRUE };
123
124private:
125 const nsCString mStr;
126};
127
128/**
129 * hashkey wrapper using PRUint32 KeyType
130 *
131 * @see nsTHashtable::EntryType for specification
132 */
133class NS_COM nsUint32HashKey : public PLDHashEntryHdr
134{
135public:
136 typedef const PRUint32& KeyType;
137 typedef const PRUint32* KeyTypePointer;
138
139 nsUint32HashKey(KeyTypePointer aKey) : mValue(*aKey) { }
140 nsUint32HashKey(const nsUint32HashKey& toCopy) : mValue(toCopy.mValue) { }
141 ~nsUint32HashKey() { }
142
143 KeyType GetKey() const { return mValue; }
144 KeyTypePointer GetKeyPointer() const { return &mValue; }
145 PRBool KeyEquals(KeyTypePointer aKey) const { return *aKey == mValue; }
146
147 static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
148 static PLDHashNumber HashKey(KeyTypePointer aKey) { return *aKey; }
149 enum { ALLOW_MEMMOVE = PR_TRUE };
150
151private:
152 const PRUint32 mValue;
153};
154
155/**
156 * hashkey wrapper using nsISupports* KeyType
157 *
158 * @see nsTHashtable::EntryType for specification
159 */
160class NS_COM nsISupportsHashKey : public PLDHashEntryHdr
161{
162public:
163 typedef nsISupports* KeyType;
164 typedef const nsISupports* KeyTypePointer;
165
166 nsISupportsHashKey(const nsISupports* key) :
167 mSupports(NS_CONST_CAST(nsISupports*,key)) { }
168 nsISupportsHashKey(const nsISupportsHashKey& toCopy) :
169 mSupports(toCopy.mSupports) { }
170 ~nsISupportsHashKey() { }
171
172 KeyType GetKey() const { return mSupports; }
173 KeyTypePointer GetKeyPointer() const { return mSupports; }
174
175 PRBool KeyEquals(KeyTypePointer aKey) const { return aKey == mSupports; }
176
177 static KeyTypePointer KeyToPointer(KeyType aKey) { return aKey; }
178 static PLDHashNumber HashKey(KeyTypePointer aKey)
179 {
180 return NS_PTR_TO_INT32(aKey) >>2;
181 }
182 enum { ALLOW_MEMMOVE = PR_TRUE };
183
184private:
185 nsCOMPtr<nsISupports> mSupports;
186};
187
188/**
189 * hashkey wrapper using void* KeyType
190 *
191 * @see nsTHashtable::EntryType for specification
192 */
193class NS_COM nsVoidPtrHashKey : public PLDHashEntryHdr
194{
195public:
196 typedef const void* KeyType;
197 typedef const void* KeyTypePointer;
198
199 nsVoidPtrHashKey(const void* key) :
200 mKey(key) { }
201 nsVoidPtrHashKey(const nsVoidPtrHashKey& toCopy) :
202 mKey(toCopy.mKey) { }
203 ~nsVoidPtrHashKey() { }
204
205 KeyType GetKey() const { return mKey; }
206 KeyTypePointer GetKeyPointer() const { return mKey; }
207
208 PRBool KeyEquals(KeyTypePointer aKey) const { return aKey == mKey; }
209
210 static KeyTypePointer KeyToPointer(KeyType aKey) { return aKey; }
211 static PLDHashNumber HashKey(KeyTypePointer aKey)
212 {
213 return NS_PTR_TO_INT32(aKey) >>2;
214 }
215 enum { ALLOW_MEMMOVE = PR_TRUE };
216
217private:
218 const void* mKey;
219};
220
221/**
222 * hashkey wrapper using nsID KeyType
223 *
224 * @see nsTHashtable::EntryType for specification
225 */
226class NS_COM nsIDHashKey : public PLDHashEntryHdr
227{
228public:
229 typedef const nsID& KeyType;
230 typedef const nsID* KeyTypePointer;
231
232 nsIDHashKey(const nsID* id) : mID(*id) { }
233 nsIDHashKey(const nsIDHashKey& toCopy) : mID(toCopy.mID) { }
234 ~nsIDHashKey() { }
235
236 KeyType GetKey() const { return mID; }
237 KeyTypePointer GetKeyPointer() const { return &mID; }
238
239 PRBool KeyEquals(KeyTypePointer aKey) const { return aKey->Equals(mID); }
240
241 static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
242 static PLDHashNumber HashKey(KeyTypePointer aKey);
243 enum { ALLOW_MEMMOVE = PR_TRUE };
244
245private:
246 const nsID mID;
247};
248
249/**
250 * hashkey wrapper for "dependent" const char*; this class does not "own"
251 * its string pointer.
252 *
253 * This class must only be used if the strings have a lifetime longer than
254 * the hashtable they occupy. This normally occurs only for static
255 * strings or strings that have been arena-allocated.
256 *
257 * @see nsTHashtable::EntryType for specification
258 */
259class NS_COM nsDepCharHashKey : public PLDHashEntryHdr
260{
261public:
262 typedef const char* KeyType;
263 typedef const char* KeyTypePointer;
264
265 nsDepCharHashKey(const char* aKey) { mKey = aKey; }
266 nsDepCharHashKey(const nsDepCharHashKey& toCopy) { mKey = toCopy.mKey; }
267 ~nsDepCharHashKey() { }
268
269 const char* GetKey() const { return mKey; }
270 const char* GetKeyPointer() const { return mKey; }
271 PRBool KeyEquals(const char* aKey) const
272 {
273 return !strcmp(mKey, aKey);
274 }
275
276 static const char* KeyToPointer(const char* aKey) { return aKey; }
277 static PLDHashNumber HashKey(const char* aKey) { return nsCRT::HashCode(aKey); }
278 enum { ALLOW_MEMMOVE = PR_TRUE };
279
280private:
281 const char* mKey;
282};
283
284/**
285 * hashkey wrapper for const char*; at construction, this class duplicates
286 * a string pointed to by the pointer so that it doesn't matter whether or not
287 * the string lives longer than the hash table.
288 */
289class NS_COM nsCharPtrHashKey : public PLDHashEntryHdr
290{
291public:
292 typedef const char* KeyType;
293 typedef const char* KeyTypePointer;
294
295 nsCharPtrHashKey(const char* aKey) : mKey(strdup(aKey)) { }
296 nsCharPtrHashKey(const nsCharPtrHashKey& toCopy) : mKey(strdup(toCopy.mKey)) { }
297 ~nsCharPtrHashKey() { if (mKey) free(NS_CONST_CAST(char *, mKey)); }
298
299 const char* GetKey() const { return mKey; }
300 const char* GetKeyPointer() const { return mKey; }
301 PRBool KeyEquals(KeyTypePointer aKey) const
302 {
303 return !strcmp(mKey, aKey);
304 }
305
306 static KeyTypePointer KeyToPointer(KeyType aKey) { return aKey; }
307 static PLDHashNumber HashKey(KeyTypePointer aKey) { return nsCRT::HashCode(aKey); }
308
309 enum { ALLOW_MEMMOVE = PR_TRUE };
310
311private:
312 const char* mKey;
313};
314
315#endif // nsTHashKeys_h__
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use