Bug 630001, part2 - fix nsAccUtils::TextLength to not use nsIFrame::GetRenderedText...
[mozilla-central.git] / xpcom / ds / nsHashtable.h
blob067485c69615e4e40b831f3e1cb67618f388dc41
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
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 mozilla.org code.
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.
22 * Contributor(s):
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.
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
42 * Modifications to Mozilla code or documentation
43 * identified per MPL Section 3.3
45 * Date Modified by Description of modification
46 * 04/20/2000 IBM Corp. Added PR_CALLBACK for Optlink use in OS2
49 /**
50 * nsHashtable is OBSOLETE. Use nsTHashtable or a derivative instead.
53 #ifndef nsHashtable_h__
54 #define nsHashtable_h__
56 #include "pldhash.h"
57 #include "prlock.h"
58 #include "nscore.h"
59 #include "nsString.h"
60 #include "nsISupportsBase.h"
61 #include "nsTraceRefcnt.h"
63 class nsIObjectInputStream;
64 class nsIObjectOutputStream;
66 class nsHashtable;
67 class nsStringKey;
69 class NS_COM nsHashKey {
70 protected:
71 nsHashKey(void) {
72 #ifdef DEBUG
73 mKeyType = UnknownKey;
74 #endif
75 MOZ_COUNT_CTOR(nsHashKey);
79 public:
80 // Virtual destructor because all hash keys are |delete|d via a
81 // nsHashKey pointer.
83 virtual ~nsHashKey(void);
84 virtual PRUint32 HashCode(void) const = 0;
85 virtual PRBool Equals(const nsHashKey *aKey) const = 0;
86 virtual nsHashKey *Clone() const = 0;
87 virtual nsresult Write(nsIObjectOutputStream* aStream) const;
89 #ifdef DEBUG
90 public:
91 // used for verification that we're casting to the correct key type
92 enum nsHashKeyType {
93 UnknownKey,
94 SupportsKey,
95 PRUint32Key,
96 VoidKey,
97 IDKey,
98 CStringKey,
99 StringKey
101 nsHashKeyType GetKeyType() const { return mKeyType; }
102 protected:
103 nsHashKeyType mKeyType;
104 #endif
107 // Enumerator and Read/Write callback functions.
109 // Return values for nsHashtableEnumFunc
110 enum {
111 kHashEnumerateStop = PR_FALSE,
112 kHashEnumerateNext = PR_TRUE,
113 kHashEnumerateRemove = 2
116 typedef PRIntn
117 (* nsHashtableEnumFunc)(nsHashKey *aKey, void *aData, void* aClosure);
119 typedef nsresult
120 (* nsHashtableReadEntryFunc)(nsIObjectInputStream *aStream, nsHashKey **aKey,
121 void **aData);
123 // NB: may be called with null aKey or aData, to free just one of the two.
124 typedef void
125 (* nsHashtableFreeEntryFunc)(nsIObjectInputStream *aStream, nsHashKey *aKey,
126 void *aData);
128 typedef nsresult
129 (* nsHashtableWriteDataFunc)(nsIObjectOutputStream *aStream, void *aData);
131 class NS_COM nsHashtable {
132 protected:
133 // members
134 PRLock* mLock;
135 PLDHashTable mHashtable;
136 PRBool mEnumerating;
138 public:
139 nsHashtable(PRUint32 aSize = 16, PRBool threadSafe = PR_FALSE);
140 virtual ~nsHashtable();
142 PRInt32 Count(void) { return mHashtable.entryCount; }
143 PRBool Exists(nsHashKey *aKey);
144 void *Put(nsHashKey *aKey, void *aData);
145 void *Get(nsHashKey *aKey);
146 void *Remove(nsHashKey *aKey);
147 nsHashtable *Clone();
148 void Enumerate(nsHashtableEnumFunc aEnumFunc, void* aClosure = NULL);
149 void Reset();
150 void Reset(nsHashtableEnumFunc destroyFunc, void* aClosure = NULL);
152 nsHashtable(nsIObjectInputStream* aStream,
153 nsHashtableReadEntryFunc aReadEntryFunc,
154 nsHashtableFreeEntryFunc aFreeEntryFunc,
155 nsresult *aRetVal);
156 nsresult Write(nsIObjectOutputStream* aStream,
157 nsHashtableWriteDataFunc aWriteDataFunc) const;
160 ////////////////////////////////////////////////////////////////////////////////
161 // nsObjectHashtable: an nsHashtable where the elements are C++ objects to be
162 // deleted
164 typedef void* (* nsHashtableCloneElementFunc)(nsHashKey *aKey, void *aData, void* aClosure);
166 class NS_COM nsObjectHashtable : public nsHashtable {
167 public:
168 nsObjectHashtable(nsHashtableCloneElementFunc cloneElementFun,
169 void* cloneElementClosure,
170 nsHashtableEnumFunc destroyElementFun,
171 void* destroyElementClosure,
172 PRUint32 aSize = 16, PRBool threadSafe = PR_FALSE);
173 ~nsObjectHashtable();
175 nsHashtable *Clone();
176 void Reset();
177 PRBool RemoveAndDelete(nsHashKey *aKey);
179 protected:
180 static PLDHashOperator CopyElement(PLDHashTable* table,
181 PLDHashEntryHdr* hdr,
182 PRUint32 i, void *arg);
184 nsHashtableCloneElementFunc mCloneElementFun;
185 void* mCloneElementClosure;
186 nsHashtableEnumFunc mDestroyElementFun;
187 void* mDestroyElementClosure;
190 ////////////////////////////////////////////////////////////////////////////////
191 // nsSupportsHashtable: an nsHashtable where the elements are nsISupports*
193 class nsISupports;
195 class NS_COM nsSupportsHashtable
196 : private nsHashtable
198 public:
199 typedef PRBool (* EnumFunc) (nsHashKey *aKey, void *aData, void* aClosure);
201 nsSupportsHashtable(PRUint32 aSize = 16, PRBool threadSafe = PR_FALSE)
202 : nsHashtable(aSize, threadSafe) {}
203 ~nsSupportsHashtable();
205 PRInt32 Count(void) {
206 return nsHashtable::Count();
208 PRBool Exists(nsHashKey *aKey) {
209 return nsHashtable::Exists (aKey);
211 PRBool Put(nsHashKey *aKey,
212 nsISupports *aData,
213 nsISupports **value = nsnull);
214 nsISupports* Get(nsHashKey *aKey);
215 PRBool Remove(nsHashKey *aKey, nsISupports **value = nsnull);
216 nsHashtable *Clone();
217 void Enumerate(EnumFunc aEnumFunc, void* aClosure = NULL) {
218 nsHashtable::Enumerate(aEnumFunc, aClosure);
220 void Reset();
222 private:
223 static PRBool ReleaseElement(nsHashKey *, void *, void *);
224 static PLDHashOperator EnumerateCopy(PLDHashTable*,
225 PLDHashEntryHdr* hdr,
226 PRUint32 i, void *arg);
229 ////////////////////////////////////////////////////////////////////////////////
230 // nsISupportsKey: Where keys are nsISupports objects that get refcounted.
232 #include "nsISupports.h"
234 class NS_COM nsISupportsKey : public nsHashKey {
235 protected:
236 nsISupports* mKey;
238 public:
239 nsISupportsKey(const nsISupportsKey& aKey) : mKey(aKey.mKey) {
240 #ifdef DEBUG
241 mKeyType = SupportsKey;
242 #endif
243 NS_IF_ADDREF(mKey);
246 nsISupportsKey(nsISupports* key) {
247 #ifdef DEBUG
248 mKeyType = SupportsKey;
249 #endif
250 mKey = key;
251 NS_IF_ADDREF(mKey);
254 ~nsISupportsKey(void) {
255 NS_IF_RELEASE(mKey);
258 PRUint32 HashCode(void) const {
259 return NS_PTR_TO_INT32(mKey);
262 PRBool Equals(const nsHashKey *aKey) const {
263 NS_ASSERTION(aKey->GetKeyType() == SupportsKey, "mismatched key types");
264 return (mKey == ((nsISupportsKey *) aKey)->mKey);
267 nsHashKey *Clone() const {
268 return new nsISupportsKey(mKey);
271 nsISupportsKey(nsIObjectInputStream* aStream, nsresult *aResult);
272 nsresult Write(nsIObjectOutputStream* aStream) const;
276 class nsPRUint32Key : public nsHashKey {
277 protected:
278 PRUint32 mKey;
279 public:
280 nsPRUint32Key(PRUint32 key) {
281 #ifdef DEBUG
282 mKeyType = PRUint32Key;
283 #endif
284 mKey = key;
287 PRUint32 HashCode(void) const {
288 return mKey;
291 PRBool Equals(const nsHashKey *aKey) const {
292 return mKey == ((const nsPRUint32Key *) aKey)->mKey;
294 nsHashKey *Clone() const {
295 return new nsPRUint32Key(mKey);
297 PRUint32 GetValue() { return mKey; }
300 ////////////////////////////////////////////////////////////////////////////////
301 // nsVoidKey: Where keys are void* objects that don't get refcounted.
303 class nsVoidKey : public nsHashKey {
304 protected:
305 void* mKey;
307 public:
308 nsVoidKey(const nsVoidKey& aKey) : mKey(aKey.mKey) {
309 #ifdef DEBUG
310 mKeyType = aKey.mKeyType;
311 #endif
314 nsVoidKey(void* key) {
315 #ifdef DEBUG
316 mKeyType = VoidKey;
317 #endif
318 mKey = key;
321 PRUint32 HashCode(void) const {
322 return NS_PTR_TO_INT32(mKey);
325 PRBool Equals(const nsHashKey *aKey) const {
326 NS_ASSERTION(aKey->GetKeyType() == VoidKey, "mismatched key types");
327 return (mKey == ((const nsVoidKey *) aKey)->mKey);
330 nsHashKey *Clone() const {
331 return new nsVoidKey(mKey);
334 void* GetValue() { return mKey; }
337 #include "nsString.h"
339 // for null-terminated c-strings
340 class NS_COM nsCStringKey : public nsHashKey {
341 public:
343 // NB: when serializing, NEVER_OWN keys are deserialized as OWN.
344 enum Ownership {
345 NEVER_OWN, // very long lived, even clones don't need to copy it.
346 OWN_CLONE, // as long lived as this key. But clones make a copy.
347 OWN // to be free'd in key dtor. Clones make their own copy.
350 nsCStringKey(const nsCStringKey& aStrKey);
351 nsCStringKey(const char* str, PRInt32 strLen = -1, Ownership own = OWN_CLONE);
352 nsCStringKey(const nsAFlatCString& str);
353 nsCStringKey(const nsACString& str);
354 ~nsCStringKey(void);
356 PRUint32 HashCode(void) const;
357 PRBool Equals(const nsHashKey* aKey) const;
358 nsHashKey* Clone() const;
359 nsCStringKey(nsIObjectInputStream* aStream, nsresult *aResult);
360 nsresult Write(nsIObjectOutputStream* aStream) const;
362 // For when the owner of the hashtable wants to peek at the actual
363 // string in the key. No copy is made, so be careful.
364 const char* GetString() const { return mStr; }
365 PRUint32 GetStringLength() const { return mStrLen; }
367 protected:
368 char* mStr;
369 PRUint32 mStrLen;
370 Ownership mOwnership;
373 // for null-terminated unicode strings
374 class NS_COM nsStringKey : public nsHashKey {
375 public:
377 // NB: when serializing, NEVER_OWN keys are deserialized as OWN.
378 enum Ownership {
379 NEVER_OWN, // very long lived, even clones don't need to copy it.
380 OWN_CLONE, // as long lived as this key. But clones make a copy.
381 OWN // to be free'd in key dtor. Clones make their own copy.
384 nsStringKey(const nsStringKey& aKey);
385 nsStringKey(const PRUnichar* str, PRInt32 strLen = -1, Ownership own = OWN_CLONE);
386 nsStringKey(const nsAFlatString& str);
387 nsStringKey(const nsAString& str);
388 ~nsStringKey(void);
390 PRUint32 HashCode(void) const;
391 PRBool Equals(const nsHashKey* aKey) const;
392 nsHashKey* Clone() const;
393 nsStringKey(nsIObjectInputStream* aStream, nsresult *aResult);
394 nsresult Write(nsIObjectOutputStream* aStream) const;
396 // For when the owner of the hashtable wants to peek at the actual
397 // string in the key. No copy is made, so be careful.
398 const PRUnichar* GetString() const { return mStr; }
399 PRUint32 GetStringLength() const { return mStrLen; }
401 protected:
402 PRUnichar* mStr;
403 PRUint32 mStrLen;
404 Ownership mOwnership;
407 ////////////////////////////////////////////////////////////////////////////////
409 #endif // nsHashtable_h__