Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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
8 * Corporation, 2000
9 *
10 * Modifications to Mozilla code or documentation
11 * identified per MPL Section 3.3
12 *
13 * Date Modified by Description of modification
14 * 04/20/2000 IBM Corp. Added PR_CALLBACK for Optlink use in OS2
15 */
17 /**
18 * nsHashtable is OBSOLETE. Use nsTHashtable or a derivative instead.
19 */
21 #ifndef nsHashtable_h__
22 #define nsHashtable_h__
24 #include "pldhash.h"
25 #include "nscore.h"
26 #include "nsISupports.h"
27 #include "nsISupportsImpl.h"
28 #include "nsStringFwd.h"
30 class nsIObjectInputStream;
31 class nsIObjectOutputStream;
33 struct PRLock;
35 class nsHashKey {
36 protected:
37 nsHashKey(void) {
38 #ifdef DEBUG
39 mKeyType = UnknownKey;
40 #endif
41 MOZ_COUNT_CTOR(nsHashKey);
42 }
45 public:
46 // Virtual destructor because all hash keys are |delete|d via a
47 // nsHashKey pointer.
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;
55 #ifdef DEBUG
56 public:
57 // used for verification that we're casting to the correct key type
58 enum nsHashKeyType {
59 UnknownKey,
60 SupportsKey,
61 PRUint32Key,
62 VoidKey,
63 IDKey,
64 CStringKey,
65 StringKey
66 };
67 nsHashKeyType GetKeyType() const { return mKeyType; }
68 protected:
69 nsHashKeyType mKeyType;
70 #endif
71 };
73 // Enumerator and Read/Write callback functions.
75 // Return values for nsHashtableEnumFunc
76 enum {
77 kHashEnumerateStop = false,
78 kHashEnumerateNext = true
79 };
81 typedef bool
82 (* nsHashtableEnumFunc)(nsHashKey *aKey, void *aData, void* aClosure);
84 typedef nsresult
85 (* nsHashtableReadEntryFunc)(nsIObjectInputStream *aStream, nsHashKey **aKey,
86 void **aData);
88 // NB: may be called with null aKey or aData, to free just one of the two.
89 typedef void
90 (* nsHashtableFreeEntryFunc)(nsIObjectInputStream *aStream, nsHashKey *aKey,
91 void *aData);
93 typedef nsresult
94 (* nsHashtableWriteDataFunc)(nsIObjectOutputStream *aStream, void *aData);
96 class nsHashtable {
97 protected:
98 // members
99 PRLock* mLock;
100 PLDHashTable mHashtable;
101 bool mEnumerating;
103 public:
104 nsHashtable(uint32_t aSize = 16, bool aThreadSafe = 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);
114 void Reset();
115 void Reset(nsHashtableEnumFunc destroyFunc, void* aClosure = nullptr);
117 nsHashtable(nsIObjectInputStream* aStream,
118 nsHashtableReadEntryFunc aReadEntryFunc,
119 nsHashtableFreeEntryFunc aFreeEntryFunc,
120 nsresult *aRetVal);
121 nsresult Write(nsIObjectOutputStream* aStream,
122 nsHashtableWriteDataFunc aWriteDataFunc) const;
123 };
125 ////////////////////////////////////////////////////////////////////////////////
126 // nsObjectHashtable: an nsHashtable where the elements are C++ objects to be
127 // deleted
129 typedef void* (* nsHashtableCloneElementFunc)(nsHashKey *aKey, void *aData, void* aClosure);
131 class nsObjectHashtable : public nsHashtable {
132 public:
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();
141 void Reset();
142 bool RemoveAndDelete(nsHashKey *aKey);
144 protected:
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;
153 };
155 ////////////////////////////////////////////////////////////////////////////////
157 class nsPRUint32Key : public nsHashKey {
158 protected:
159 uint32_t mKey;
160 public:
161 nsPRUint32Key(uint32_t key) {
162 #ifdef DEBUG
163 mKeyType = PRUint32Key;
164 #endif
165 mKey = key;
166 }
168 uint32_t HashCode(void) const {
169 return mKey;
170 }
172 bool Equals(const nsHashKey *aKey) const {
173 return mKey == ((const nsPRUint32Key *) aKey)->mKey;
174 }
175 nsHashKey *Clone() const {
176 return new nsPRUint32Key(mKey);
177 }
178 uint32_t GetValue() { return mKey; }
179 };
181 // for null-terminated c-strings
182 class nsCStringKey : public nsHashKey {
183 public:
185 // NB: when serializing, NEVER_OWN keys are deserialized as OWN.
186 enum Ownership {
187 NEVER_OWN, // very long lived, even clones don't need to copy it.
188 OWN_CLONE, // as long lived as this key. But clones make a copy.
189 OWN // to be free'd in key dtor. Clones make their own copy.
190 };
192 nsCStringKey(const nsCStringKey& aStrKey);
193 nsCStringKey(const char* str, int32_t strLen = -1, Ownership own = OWN_CLONE);
194 nsCStringKey(const nsAFlatCString& str);
195 nsCStringKey(const nsACString& str);
196 ~nsCStringKey(void);
198 uint32_t HashCode(void) const;
199 bool Equals(const nsHashKey* aKey) const;
200 nsHashKey* Clone() const;
201 nsCStringKey(nsIObjectInputStream* aStream, nsresult *aResult);
202 nsresult Write(nsIObjectOutputStream* aStream) const;
204 // For when the owner of the hashtable wants to peek at the actual
205 // string in the key. No copy is made, so be careful.
206 const char* GetString() const { return mStr; }
207 uint32_t GetStringLength() const { return mStrLen; }
209 protected:
210 char* mStr;
211 uint32_t mStrLen;
212 Ownership mOwnership;
213 };
215 ////////////////////////////////////////////////////////////////////////////////
217 #endif // nsHashtable_h__