VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/ds/nsHashtable.h

Last change on this file was 101975, checked in by vboxsync, 12 months ago

libs/xpcom/xpcom: Convert some code from using PRLock to IPRT's RTSEMFASTMUTEX locks, bugref:10545

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.6 KB
Line 
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 mozilla.org code.
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
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 of the GNU General Public License Version 2 or later (the "GPL"),
26 * or 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 * This Original Code has been modified by IBM Corporation.
38 * Modifications made by IBM described herein are
39 * Copyright (c) International Business Machines
40 * Corporation, 2000
41 *
42 * Modifications to Mozilla code or documentation
43 * identified per MPL Section 3.3
44 *
45 * Date Modified by Description of modification
46 * 04/20/2000 IBM Corp. Added PR_CALLBACK for Optlink use in OS2
47 */
48
49/**
50 * nsHashtable is OBSOLETE. Use nsTHashtable or a derivative instead.
51 */
52
53#ifndef nsHashtable_h__
54#define nsHashtable_h__
55
56#include "pldhash.h"
57#include "nscore.h"
58#include "nsString.h"
59#include "nsISupportsBase.h"
60#include "nsTraceRefcnt.h"
61
62#include <iprt/semaphore.h>
63
64class nsIObjectInputStream;
65class nsIObjectOutputStream;
66
67class nsHashtable;
68class nsStringKey;
69
70class NS_COM nsHashKey {
71 protected:
72 nsHashKey(void) {
73#ifdef DEBUG
74 mKeyType = UnknownKey;
75#endif
76 MOZ_COUNT_CTOR(nsHashKey);
77 }
78
79
80 public:
81 // Virtual destructor because all hash keys are |delete|d via a
82 // nsHashKey pointer.
83
84 virtual ~nsHashKey(void);
85 virtual PRUint32 HashCode(void) const = 0;
86 virtual PRBool Equals(const nsHashKey *aKey) const = 0;
87 virtual nsHashKey *Clone() const = 0;
88 virtual nsresult Write(nsIObjectOutputStream* aStream) const;
89
90#ifdef DEBUG
91 public:
92 // used for verification that we're casting to the correct key type
93 enum nsHashKeyType {
94 UnknownKey,
95 SupportsKey,
96 PRUint32Key,
97 VoidKey,
98 IDKey,
99 CStringKey,
100 StringKey
101 };
102 nsHashKeyType GetKeyType() const { return mKeyType; }
103 protected:
104 nsHashKeyType mKeyType;
105#endif
106};
107
108// Enumerator and Read/Write callback functions.
109
110// Return values for nsHashtableEnumFunc
111enum {
112 kHashEnumerateStop = PR_FALSE,
113 kHashEnumerateNext = PR_TRUE,
114 kHashEnumerateRemove = 2
115};
116
117typedef PRIntn
118(*PR_CALLBACK nsHashtableEnumFunc)(nsHashKey *aKey, void *aData, void* aClosure);
119
120typedef nsresult
121(*PR_CALLBACK nsHashtableReadEntryFunc)(nsIObjectInputStream *aStream,
122 nsHashKey **aKey,
123 void **aData);
124
125// NB: may be called with null aKey or aData, to free just one of the two.
126typedef void
127(*PR_CALLBACK nsHashtableFreeEntryFunc)(nsIObjectInputStream *aStream,
128 nsHashKey *aKey,
129 void *aData);
130
131typedef nsresult
132(*PR_CALLBACK nsHashtableWriteDataFunc)(nsIObjectOutputStream *aStream,
133 void *aData);
134
135class NS_COM nsHashtable {
136 protected:
137 // members
138 RTSEMFASTMUTEX mLock;
139 PLDHashTable mHashtable;
140 PRBool mEnumerating;
141
142 public:
143 nsHashtable(PRUint32 aSize = 16, PRBool threadSafe = PR_FALSE);
144 virtual ~nsHashtable();
145
146 PRInt32 Count(void) { return mHashtable.entryCount; }
147 PRBool Exists(nsHashKey *aKey);
148 void *Put(nsHashKey *aKey, void *aData);
149 void *Get(nsHashKey *aKey);
150 void *Remove(nsHashKey *aKey);
151 nsHashtable *Clone();
152 void Enumerate(nsHashtableEnumFunc aEnumFunc, void* aClosure = NULL);
153 void Reset();
154 void Reset(nsHashtableEnumFunc destroyFunc, void* aClosure = NULL);
155
156 nsHashtable(nsIObjectInputStream* aStream,
157 nsHashtableReadEntryFunc aReadEntryFunc,
158 nsHashtableFreeEntryFunc aFreeEntryFunc,
159 nsresult *aRetVal);
160 nsresult Write(nsIObjectOutputStream* aStream,
161 nsHashtableWriteDataFunc aWriteDataFunc) const;
162};
163
164////////////////////////////////////////////////////////////////////////////////
165// nsObjectHashtable: an nsHashtable where the elements are C++ objects to be
166// deleted
167
168typedef void* (*PR_CALLBACK nsHashtableCloneElementFunc)(nsHashKey *aKey, void *aData, void* aClosure);
169
170class NS_COM nsObjectHashtable : public nsHashtable {
171 public:
172 nsObjectHashtable(nsHashtableCloneElementFunc cloneElementFun,
173 void* cloneElementClosure,
174 nsHashtableEnumFunc destroyElementFun,
175 void* destroyElementClosure,
176 PRUint32 aSize = 16, PRBool threadSafe = PR_FALSE);
177 ~nsObjectHashtable();
178
179 nsHashtable *Clone();
180 void Reset();
181 PRBool RemoveAndDelete(nsHashKey *aKey);
182
183 protected:
184 static PLDHashOperator PR_CALLBACK CopyElement(PLDHashTable* table,
185 PLDHashEntryHdr* hdr,
186 PRUint32 i, void *arg);
187
188 nsHashtableCloneElementFunc mCloneElementFun;
189 void* mCloneElementClosure;
190 nsHashtableEnumFunc mDestroyElementFun;
191 void* mDestroyElementClosure;
192};
193
194////////////////////////////////////////////////////////////////////////////////
195// nsSupportsHashtable: an nsHashtable where the elements are nsISupports*
196
197class nsISupports;
198
199class NS_COM nsSupportsHashtable
200 : private nsHashtable
201{
202 public:
203 typedef PRBool (* PR_CALLBACK EnumFunc) (nsHashKey *aKey, void *aData, void* aClosure);
204
205 nsSupportsHashtable(PRUint32 aSize = 16, PRBool threadSafe = PR_FALSE)
206 : nsHashtable(aSize, threadSafe) {}
207 ~nsSupportsHashtable();
208
209 PRInt32 Count(void) {
210 return nsHashtable::Count();
211 }
212 PRBool Exists(nsHashKey *aKey) {
213 return nsHashtable::Exists (aKey);
214 }
215 PRBool Put(nsHashKey *aKey,
216 nsISupports *aData,
217 nsISupports **value = nsnull);
218 nsISupports* Get(nsHashKey *aKey);
219 PRBool Remove(nsHashKey *aKey, nsISupports **value = nsnull);
220 nsHashtable *Clone();
221 void Enumerate(EnumFunc aEnumFunc, void* aClosure = NULL) {
222 nsHashtable::Enumerate(aEnumFunc, aClosure);
223 }
224 void Reset();
225
226 private:
227 static PRBool PR_CALLBACK ReleaseElement(nsHashKey *, void *, void *);
228 static PLDHashOperator PR_CALLBACK EnumerateCopy(PLDHashTable*,
229 PLDHashEntryHdr* hdr,
230 PRUint32 i, void *arg);
231};
232
233////////////////////////////////////////////////////////////////////////////////
234// nsISupportsKey: Where keys are nsISupports objects that get refcounted.
235
236#include "nsISupports.h"
237
238class NS_COM nsISupportsKey : public nsHashKey {
239 protected:
240 nsISupports* mKey;
241
242 public:
243 nsISupportsKey(const nsISupportsKey& aKey) : mKey(aKey.mKey) {
244#ifdef DEBUG
245 mKeyType = SupportsKey;
246#endif
247 NS_IF_ADDREF(mKey);
248 }
249
250 nsISupportsKey(nsISupports* key) {
251#ifdef DEBUG
252 mKeyType = SupportsKey;
253#endif
254 mKey = key;
255 NS_IF_ADDREF(mKey);
256 }
257
258 ~nsISupportsKey(void) {
259 NS_IF_RELEASE(mKey);
260 }
261
262 PRUint32 HashCode(void) const {
263 return NS_PTR_TO_INT32(mKey);
264 }
265
266 PRBool Equals(const nsHashKey *aKey) const {
267 NS_ASSERTION(aKey->GetKeyType() == SupportsKey, "mismatched key types");
268 return (mKey == ((nsISupportsKey *) aKey)->mKey);
269 }
270
271 nsHashKey *Clone() const {
272 return new nsISupportsKey(mKey);
273 }
274
275 nsISupportsKey(nsIObjectInputStream* aStream, nsresult *aResult);
276 nsresult Write(nsIObjectOutputStream* aStream) const;
277};
278
279
280class nsPRUint32Key : public nsHashKey {
281protected:
282 PRUint32 mKey;
283public:
284 nsPRUint32Key(PRUint32 key) {
285#ifdef DEBUG
286 mKeyType = PRUint32Key;
287#endif
288 mKey = key;
289 }
290
291 PRUint32 HashCode(void) const {
292 return mKey;
293 }
294
295 PRBool Equals(const nsHashKey *aKey) const {
296 return mKey == ((const nsPRUint32Key *) aKey)->mKey;
297 }
298 nsHashKey *Clone() const {
299 return new nsPRUint32Key(mKey);
300 }
301 PRUint32 GetValue() { return mKey; }
302};
303
304////////////////////////////////////////////////////////////////////////////////
305// nsVoidKey: Where keys are void* objects that don't get refcounted.
306
307class nsVoidKey : public nsHashKey {
308 protected:
309 void* mKey;
310
311 public:
312 nsVoidKey(const nsVoidKey& aKey) : mKey(aKey.mKey) {
313#ifdef DEBUG
314 mKeyType = aKey.mKeyType;
315#endif
316 }
317
318 nsVoidKey(void* key) {
319#ifdef DEBUG
320 mKeyType = VoidKey;
321#endif
322 mKey = key;
323 }
324
325 PRUint32 HashCode(void) const {
326 return NS_PTR_TO_INT32(mKey);
327 }
328
329 PRBool Equals(const nsHashKey *aKey) const {
330 NS_ASSERTION(aKey->GetKeyType() == VoidKey, "mismatched key types");
331 return (mKey == ((const nsVoidKey *) aKey)->mKey);
332 }
333
334 nsHashKey *Clone() const {
335 return new nsVoidKey(mKey);
336 }
337
338 void* GetValue() { return mKey; }
339};
340
341////////////////////////////////////////////////////////////////////////////////
342// nsIDKey: Where keys are nsIDs (e.g. nsIID, nsCID).
343
344#include "nsID.h"
345
346class NS_COM nsIDKey : public nsHashKey {
347 protected:
348 nsID mID;
349
350 public:
351 nsIDKey(const nsIDKey& aKey) : mID(aKey.mID) {
352#ifdef DEBUG
353 mKeyType = IDKey;
354#endif
355 }
356
357 nsIDKey(const nsID &aID) {
358#ifdef DEBUG
359 mKeyType = IDKey;
360#endif
361 mID = aID;
362 }
363
364 PRUint32 HashCode(void) const {
365 return mID.m0;
366 }
367
368 PRBool Equals(const nsHashKey *aKey) const {
369 NS_ASSERTION(aKey->GetKeyType() == IDKey, "mismatched key types");
370 return (mID.Equals(((const nsIDKey *) aKey)->mID));
371 }
372
373 nsHashKey *Clone() const {
374 return new nsIDKey(mID);
375 }
376
377 nsIDKey(nsIObjectInputStream* aStream, nsresult *aResult);
378 nsresult Write(nsIObjectOutputStream* aStream) const;
379};
380
381////////////////////////////////////////////////////////////////////////////////
382
383#include "nsString.h"
384
385// for null-terminated c-strings
386class NS_COM nsCStringKey : public nsHashKey {
387 public:
388
389 // NB: when serializing, NEVER_OWN keys are deserialized as OWN.
390 enum Ownership {
391 NEVER_OWN, // very long lived, even clones don't need to copy it.
392 OWN_CLONE, // as long lived as this key. But clones make a copy.
393 OWN // to be free'd in key dtor. Clones make their own copy.
394 };
395
396 nsCStringKey(const nsCStringKey& aStrKey);
397 nsCStringKey(const char* str, PRInt32 strLen = -1, Ownership own = OWN_CLONE);
398 nsCStringKey(const nsAFlatCString& str);
399 nsCStringKey(const nsACString& str);
400 ~nsCStringKey(void);
401
402 PRUint32 HashCode(void) const;
403 PRBool Equals(const nsHashKey* aKey) const;
404 nsHashKey* Clone() const;
405 nsCStringKey(nsIObjectInputStream* aStream, nsresult *aResult);
406 nsresult Write(nsIObjectOutputStream* aStream) const;
407
408 // For when the owner of the hashtable wants to peek at the actual
409 // string in the key. No copy is made, so be careful.
410 const char* GetString() const { return mStr; }
411 PRUint32 GetStringLength() const { return mStrLen; }
412
413 protected:
414 char* mStr;
415 PRUint32 mStrLen;
416 Ownership mOwnership;
417};
418
419// for null-terminated unicode strings
420class NS_COM nsStringKey : public nsHashKey {
421 public:
422
423 // NB: when serializing, NEVER_OWN keys are deserialized as OWN.
424 enum Ownership {
425 NEVER_OWN, // very long lived, even clones don't need to copy it.
426 OWN_CLONE, // as long lived as this key. But clones make a copy.
427 OWN // to be free'd in key dtor. Clones make their own copy.
428 };
429
430 nsStringKey(const nsStringKey& aKey);
431 nsStringKey(const PRUnichar* str, PRInt32 strLen = -1, Ownership own = OWN_CLONE);
432 nsStringKey(const nsAFlatString& str);
433 nsStringKey(const nsAString& str);
434 ~nsStringKey(void);
435
436 PRUint32 HashCode(void) const;
437 PRBool Equals(const nsHashKey* aKey) const;
438 nsHashKey* Clone() const;
439 nsStringKey(nsIObjectInputStream* aStream, nsresult *aResult);
440 nsresult Write(nsIObjectOutputStream* aStream) const;
441
442 // For when the owner of the hashtable wants to peek at the actual
443 // string in the key. No copy is made, so be careful.
444 const PRUnichar* GetString() const { return mStr; }
445 PRUint32 GetStringLength() const { return mStrLen; }
446
447 protected:
448 PRUnichar* mStr;
449 PRUint32 mStrLen;
450 Ownership mOwnership;
451};
452
453////////////////////////////////////////////////////////////////////////////////
454
455#endif // nsHashtable_h__
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette