1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 * This Original Code has been modified by IBM Corporation.
6 * Modifications made by IBM described herein are
7 * Copyright (c) International Business Machines
10 * Modifications to Mozilla code or documentation
11 * identified per MPL Section 3.3
13 * Date Modified by Description of modification
14 * 04/20/2000 IBM Corp. Added PR_CALLBACK for Optlink use in OS2
18 * nsHashtable is OBSOLETE. Use nsTHashtable or a derivative instead.
21 #ifndef nsHashtable_h__
22 #define nsHashtable_h__
26 #include "nsISupports.h"
27 #include "nsTraceRefcnt.h"
28 #include "nsStringFwd.h"
30 class nsIObjectInputStream
;
31 class nsIObjectOutputStream
;
39 mKeyType
= UnknownKey
;
41 MOZ_COUNT_CTOR(nsHashKey
);
46 // Virtual destructor because all hash keys are |delete|d via a
49 virtual ~nsHashKey(void);
50 virtual uint32_t HashCode(void) const = 0;
51 virtual bool Equals(const nsHashKey
*aKey
) const = 0;
52 virtual nsHashKey
*Clone() const = 0;
53 virtual nsresult
Write(nsIObjectOutputStream
* aStream
) const;
57 // used for verification that we're casting to the correct key type
67 nsHashKeyType
GetKeyType() const { return mKeyType
; }
69 nsHashKeyType mKeyType
;
73 // Enumerator and Read/Write callback functions.
75 // Return values for nsHashtableEnumFunc
77 kHashEnumerateStop
= false,
78 kHashEnumerateNext
= true
82 (* nsHashtableEnumFunc
)(nsHashKey
*aKey
, void *aData
, void* aClosure
);
85 (* nsHashtableReadEntryFunc
)(nsIObjectInputStream
*aStream
, nsHashKey
**aKey
,
88 // NB: may be called with null aKey or aData, to free just one of the two.
90 (* nsHashtableFreeEntryFunc
)(nsIObjectInputStream
*aStream
, nsHashKey
*aKey
,
94 (* nsHashtableWriteDataFunc
)(nsIObjectOutputStream
*aStream
, void *aData
);
100 PLDHashTable mHashtable
;
104 nsHashtable(uint32_t aSize
= 16, bool threadSafe
= false);
105 virtual ~nsHashtable();
107 int32_t Count(void) { return mHashtable
.entryCount
; }
108 bool Exists(nsHashKey
*aKey
);
109 void *Put(nsHashKey
*aKey
, void *aData
);
110 void *Get(nsHashKey
*aKey
);
111 void *Remove(nsHashKey
*aKey
);
112 nsHashtable
*Clone();
113 void Enumerate(nsHashtableEnumFunc aEnumFunc
, void* aClosure
= nullptr);
115 void Reset(nsHashtableEnumFunc destroyFunc
, void* aClosure
= nullptr);
117 nsHashtable(nsIObjectInputStream
* aStream
,
118 nsHashtableReadEntryFunc aReadEntryFunc
,
119 nsHashtableFreeEntryFunc aFreeEntryFunc
,
121 nsresult
Write(nsIObjectOutputStream
* aStream
,
122 nsHashtableWriteDataFunc aWriteDataFunc
) const;
125 ////////////////////////////////////////////////////////////////////////////////
126 // nsObjectHashtable: an nsHashtable where the elements are C++ objects to be
129 typedef void* (* nsHashtableCloneElementFunc
)(nsHashKey
*aKey
, void *aData
, void* aClosure
);
131 class nsObjectHashtable
: public nsHashtable
{
133 nsObjectHashtable(nsHashtableCloneElementFunc cloneElementFun
,
134 void* cloneElementClosure
,
135 nsHashtableEnumFunc destroyElementFun
,
136 void* destroyElementClosure
,
137 uint32_t aSize
= 16, bool threadSafe
= false);
138 ~nsObjectHashtable();
140 nsHashtable
*Clone();
142 bool RemoveAndDelete(nsHashKey
*aKey
);
145 static PLDHashOperator
CopyElement(PLDHashTable
* table
,
146 PLDHashEntryHdr
* hdr
,
147 uint32_t i
, void *arg
);
149 nsHashtableCloneElementFunc mCloneElementFun
;
150 void* mCloneElementClosure
;
151 nsHashtableEnumFunc mDestroyElementFun
;
152 void* mDestroyElementClosure
;
155 ////////////////////////////////////////////////////////////////////////////////
156 // nsSupportsHashtable: an nsHashtable where the elements are nsISupports*
158 class nsSupportsHashtable
159 : private nsHashtable
162 nsSupportsHashtable(uint32_t aSize
= 16, bool threadSafe
= false)
163 : nsHashtable(aSize
, threadSafe
) {}
164 ~nsSupportsHashtable();
166 int32_t Count(void) {
167 return nsHashtable::Count();
169 bool Exists(nsHashKey
*aKey
) {
170 return nsHashtable::Exists (aKey
);
172 bool Put(nsHashKey
*aKey
,
174 nsISupports
**value
= nullptr);
175 nsISupports
* Get(nsHashKey
*aKey
);
176 bool Remove(nsHashKey
*aKey
, nsISupports
**value
= nullptr);
177 nsHashtable
*Clone();
178 void Enumerate(nsHashtableEnumFunc aEnumFunc
, void* aClosure
= nullptr) {
179 nsHashtable::Enumerate(aEnumFunc
, aClosure
);
184 static bool ReleaseElement(nsHashKey
*, void *, void *);
185 static PLDHashOperator
EnumerateCopy(PLDHashTable
*,
186 PLDHashEntryHdr
* hdr
,
187 uint32_t i
, void *arg
);
190 ////////////////////////////////////////////////////////////////////////////////
191 // nsISupportsKey: Where keys are nsISupports objects that get refcounted.
193 class nsISupportsKey
: public nsHashKey
{
198 nsISupportsKey(const nsISupportsKey
& aKey
) : mKey(aKey
.mKey
) {
200 mKeyType
= SupportsKey
;
205 nsISupportsKey(nsISupports
* key
) {
207 mKeyType
= SupportsKey
;
213 ~nsISupportsKey(void) {
217 uint32_t HashCode(void) const {
218 return NS_PTR_TO_INT32(mKey
);
221 bool Equals(const nsHashKey
*aKey
) const {
222 NS_ASSERTION(aKey
->GetKeyType() == SupportsKey
, "mismatched key types");
223 return (mKey
== ((nsISupportsKey
*) aKey
)->mKey
);
226 nsHashKey
*Clone() const {
227 return new nsISupportsKey(mKey
);
230 nsISupportsKey(nsIObjectInputStream
* aStream
, nsresult
*aResult
);
231 nsresult
Write(nsIObjectOutputStream
* aStream
) const;
233 nsISupports
* GetValue() { return mKey
; }
237 class nsPRUint32Key
: public nsHashKey
{
241 nsPRUint32Key(uint32_t key
) {
243 mKeyType
= PRUint32Key
;
248 uint32_t HashCode(void) const {
252 bool Equals(const nsHashKey
*aKey
) const {
253 return mKey
== ((const nsPRUint32Key
*) aKey
)->mKey
;
255 nsHashKey
*Clone() const {
256 return new nsPRUint32Key(mKey
);
258 uint32_t GetValue() { return mKey
; }
261 ////////////////////////////////////////////////////////////////////////////////
262 // nsVoidKey: Where keys are void* objects that don't get refcounted.
264 class nsVoidKey
: public nsHashKey
{
269 nsVoidKey(const nsVoidKey
& aKey
) : mKey(aKey
.mKey
) {
271 mKeyType
= aKey
.mKeyType
;
275 nsVoidKey(void* key
) {
282 uint32_t HashCode(void) const {
283 return NS_PTR_TO_INT32(mKey
);
286 bool Equals(const nsHashKey
*aKey
) const {
287 NS_ASSERTION(aKey
->GetKeyType() == VoidKey
, "mismatched key types");
288 return (mKey
== ((const nsVoidKey
*) aKey
)->mKey
);
291 nsHashKey
*Clone() const {
292 return new nsVoidKey(mKey
);
295 void* GetValue() { return mKey
; }
298 // for null-terminated c-strings
299 class nsCStringKey
: public nsHashKey
{
302 // NB: when serializing, NEVER_OWN keys are deserialized as OWN.
304 NEVER_OWN
, // very long lived, even clones don't need to copy it.
305 OWN_CLONE
, // as long lived as this key. But clones make a copy.
306 OWN
// to be free'd in key dtor. Clones make their own copy.
309 nsCStringKey(const nsCStringKey
& aStrKey
);
310 nsCStringKey(const char* str
, int32_t strLen
= -1, Ownership own
= OWN_CLONE
);
311 nsCStringKey(const nsAFlatCString
& str
);
312 nsCStringKey(const nsACString
& str
);
315 uint32_t HashCode(void) const;
316 bool Equals(const nsHashKey
* aKey
) const;
317 nsHashKey
* Clone() const;
318 nsCStringKey(nsIObjectInputStream
* aStream
, nsresult
*aResult
);
319 nsresult
Write(nsIObjectOutputStream
* aStream
) const;
321 // For when the owner of the hashtable wants to peek at the actual
322 // string in the key. No copy is made, so be careful.
323 const char* GetString() const { return mStr
; }
324 uint32_t GetStringLength() const { return mStrLen
; }
329 Ownership mOwnership
;
332 // for null-terminated unicode strings
333 class nsStringKey
: public nsHashKey
{
336 // NB: when serializing, NEVER_OWN keys are deserialized as OWN.
338 NEVER_OWN
, // very long lived, even clones don't need to copy it.
339 OWN_CLONE
, // as long lived as this key. But clones make a copy.
340 OWN
// to be free'd in key dtor. Clones make their own copy.
343 nsStringKey(const nsStringKey
& aKey
);
344 nsStringKey(const PRUnichar
* str
, int32_t strLen
= -1, Ownership own
= OWN_CLONE
);
345 nsStringKey(const nsAFlatString
& str
);
346 nsStringKey(const nsAString
& str
);
349 uint32_t HashCode(void) const;
350 bool Equals(const nsHashKey
* aKey
) const;
351 nsHashKey
* Clone() const;
352 nsStringKey(nsIObjectInputStream
* aStream
, nsresult
*aResult
);
353 nsresult
Write(nsIObjectOutputStream
* aStream
) const;
355 // For when the owner of the hashtable wants to peek at the actual
356 // string in the key. No copy is made, so be careful.
357 const PRUnichar
* GetString() const { return mStr
; }
358 uint32_t GetStringLength() const { return mStrLen
; }
363 Ownership mOwnership
;
366 ////////////////////////////////////////////////////////////////////////////////
368 #endif // nsHashtable_h__