xpcom/ds/nsHashtable.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
michael@0 5 * This Original Code has been modified by IBM Corporation.
michael@0 6 * Modifications made by IBM described herein are
michael@0 7 * Copyright (c) International Business Machines
michael@0 8 * Corporation, 2000
michael@0 9 *
michael@0 10 * Modifications to Mozilla code or documentation
michael@0 11 * identified per MPL Section 3.3
michael@0 12 *
michael@0 13 * Date Modified by Description of modification
michael@0 14 * 04/20/2000 IBM Corp. Added PR_CALLBACK for Optlink use in OS2
michael@0 15 */
michael@0 16
michael@0 17 /**
michael@0 18 * nsHashtable is OBSOLETE. Use nsTHashtable or a derivative instead.
michael@0 19 */
michael@0 20
michael@0 21 #ifndef nsHashtable_h__
michael@0 22 #define nsHashtable_h__
michael@0 23
michael@0 24 #include "pldhash.h"
michael@0 25 #include "nscore.h"
michael@0 26 #include "nsISupports.h"
michael@0 27 #include "nsISupportsImpl.h"
michael@0 28 #include "nsStringFwd.h"
michael@0 29
michael@0 30 class nsIObjectInputStream;
michael@0 31 class nsIObjectOutputStream;
michael@0 32
michael@0 33 struct PRLock;
michael@0 34
michael@0 35 class nsHashKey {
michael@0 36 protected:
michael@0 37 nsHashKey(void) {
michael@0 38 #ifdef DEBUG
michael@0 39 mKeyType = UnknownKey;
michael@0 40 #endif
michael@0 41 MOZ_COUNT_CTOR(nsHashKey);
michael@0 42 }
michael@0 43
michael@0 44
michael@0 45 public:
michael@0 46 // Virtual destructor because all hash keys are |delete|d via a
michael@0 47 // nsHashKey pointer.
michael@0 48
michael@0 49 virtual ~nsHashKey(void);
michael@0 50 virtual uint32_t HashCode(void) const = 0;
michael@0 51 virtual bool Equals(const nsHashKey *aKey) const = 0;
michael@0 52 virtual nsHashKey *Clone() const = 0;
michael@0 53 virtual nsresult Write(nsIObjectOutputStream* aStream) const;
michael@0 54
michael@0 55 #ifdef DEBUG
michael@0 56 public:
michael@0 57 // used for verification that we're casting to the correct key type
michael@0 58 enum nsHashKeyType {
michael@0 59 UnknownKey,
michael@0 60 SupportsKey,
michael@0 61 PRUint32Key,
michael@0 62 VoidKey,
michael@0 63 IDKey,
michael@0 64 CStringKey,
michael@0 65 StringKey
michael@0 66 };
michael@0 67 nsHashKeyType GetKeyType() const { return mKeyType; }
michael@0 68 protected:
michael@0 69 nsHashKeyType mKeyType;
michael@0 70 #endif
michael@0 71 };
michael@0 72
michael@0 73 // Enumerator and Read/Write callback functions.
michael@0 74
michael@0 75 // Return values for nsHashtableEnumFunc
michael@0 76 enum {
michael@0 77 kHashEnumerateStop = false,
michael@0 78 kHashEnumerateNext = true
michael@0 79 };
michael@0 80
michael@0 81 typedef bool
michael@0 82 (* nsHashtableEnumFunc)(nsHashKey *aKey, void *aData, void* aClosure);
michael@0 83
michael@0 84 typedef nsresult
michael@0 85 (* nsHashtableReadEntryFunc)(nsIObjectInputStream *aStream, nsHashKey **aKey,
michael@0 86 void **aData);
michael@0 87
michael@0 88 // NB: may be called with null aKey or aData, to free just one of the two.
michael@0 89 typedef void
michael@0 90 (* nsHashtableFreeEntryFunc)(nsIObjectInputStream *aStream, nsHashKey *aKey,
michael@0 91 void *aData);
michael@0 92
michael@0 93 typedef nsresult
michael@0 94 (* nsHashtableWriteDataFunc)(nsIObjectOutputStream *aStream, void *aData);
michael@0 95
michael@0 96 class nsHashtable {
michael@0 97 protected:
michael@0 98 // members
michael@0 99 PRLock* mLock;
michael@0 100 PLDHashTable mHashtable;
michael@0 101 bool mEnumerating;
michael@0 102
michael@0 103 public:
michael@0 104 nsHashtable(uint32_t aSize = 16, bool aThreadSafe = false);
michael@0 105 virtual ~nsHashtable();
michael@0 106
michael@0 107 int32_t Count(void) { return mHashtable.entryCount; }
michael@0 108 bool Exists(nsHashKey *aKey);
michael@0 109 void *Put(nsHashKey *aKey, void *aData);
michael@0 110 void *Get(nsHashKey *aKey);
michael@0 111 void *Remove(nsHashKey *aKey);
michael@0 112 nsHashtable *Clone();
michael@0 113 void Enumerate(nsHashtableEnumFunc aEnumFunc, void* aClosure = nullptr);
michael@0 114 void Reset();
michael@0 115 void Reset(nsHashtableEnumFunc destroyFunc, void* aClosure = nullptr);
michael@0 116
michael@0 117 nsHashtable(nsIObjectInputStream* aStream,
michael@0 118 nsHashtableReadEntryFunc aReadEntryFunc,
michael@0 119 nsHashtableFreeEntryFunc aFreeEntryFunc,
michael@0 120 nsresult *aRetVal);
michael@0 121 nsresult Write(nsIObjectOutputStream* aStream,
michael@0 122 nsHashtableWriteDataFunc aWriteDataFunc) const;
michael@0 123 };
michael@0 124
michael@0 125 ////////////////////////////////////////////////////////////////////////////////
michael@0 126 // nsObjectHashtable: an nsHashtable where the elements are C++ objects to be
michael@0 127 // deleted
michael@0 128
michael@0 129 typedef void* (* nsHashtableCloneElementFunc)(nsHashKey *aKey, void *aData, void* aClosure);
michael@0 130
michael@0 131 class nsObjectHashtable : public nsHashtable {
michael@0 132 public:
michael@0 133 nsObjectHashtable(nsHashtableCloneElementFunc cloneElementFun,
michael@0 134 void* cloneElementClosure,
michael@0 135 nsHashtableEnumFunc destroyElementFun,
michael@0 136 void* destroyElementClosure,
michael@0 137 uint32_t aSize = 16, bool threadSafe = false);
michael@0 138 ~nsObjectHashtable();
michael@0 139
michael@0 140 nsHashtable *Clone();
michael@0 141 void Reset();
michael@0 142 bool RemoveAndDelete(nsHashKey *aKey);
michael@0 143
michael@0 144 protected:
michael@0 145 static PLDHashOperator CopyElement(PLDHashTable* table,
michael@0 146 PLDHashEntryHdr* hdr,
michael@0 147 uint32_t i, void *arg);
michael@0 148
michael@0 149 nsHashtableCloneElementFunc mCloneElementFun;
michael@0 150 void* mCloneElementClosure;
michael@0 151 nsHashtableEnumFunc mDestroyElementFun;
michael@0 152 void* mDestroyElementClosure;
michael@0 153 };
michael@0 154
michael@0 155 ////////////////////////////////////////////////////////////////////////////////
michael@0 156
michael@0 157 class nsPRUint32Key : public nsHashKey {
michael@0 158 protected:
michael@0 159 uint32_t mKey;
michael@0 160 public:
michael@0 161 nsPRUint32Key(uint32_t key) {
michael@0 162 #ifdef DEBUG
michael@0 163 mKeyType = PRUint32Key;
michael@0 164 #endif
michael@0 165 mKey = key;
michael@0 166 }
michael@0 167
michael@0 168 uint32_t HashCode(void) const {
michael@0 169 return mKey;
michael@0 170 }
michael@0 171
michael@0 172 bool Equals(const nsHashKey *aKey) const {
michael@0 173 return mKey == ((const nsPRUint32Key *) aKey)->mKey;
michael@0 174 }
michael@0 175 nsHashKey *Clone() const {
michael@0 176 return new nsPRUint32Key(mKey);
michael@0 177 }
michael@0 178 uint32_t GetValue() { return mKey; }
michael@0 179 };
michael@0 180
michael@0 181 // for null-terminated c-strings
michael@0 182 class nsCStringKey : public nsHashKey {
michael@0 183 public:
michael@0 184
michael@0 185 // NB: when serializing, NEVER_OWN keys are deserialized as OWN.
michael@0 186 enum Ownership {
michael@0 187 NEVER_OWN, // very long lived, even clones don't need to copy it.
michael@0 188 OWN_CLONE, // as long lived as this key. But clones make a copy.
michael@0 189 OWN // to be free'd in key dtor. Clones make their own copy.
michael@0 190 };
michael@0 191
michael@0 192 nsCStringKey(const nsCStringKey& aStrKey);
michael@0 193 nsCStringKey(const char* str, int32_t strLen = -1, Ownership own = OWN_CLONE);
michael@0 194 nsCStringKey(const nsAFlatCString& str);
michael@0 195 nsCStringKey(const nsACString& str);
michael@0 196 ~nsCStringKey(void);
michael@0 197
michael@0 198 uint32_t HashCode(void) const;
michael@0 199 bool Equals(const nsHashKey* aKey) const;
michael@0 200 nsHashKey* Clone() const;
michael@0 201 nsCStringKey(nsIObjectInputStream* aStream, nsresult *aResult);
michael@0 202 nsresult Write(nsIObjectOutputStream* aStream) const;
michael@0 203
michael@0 204 // For when the owner of the hashtable wants to peek at the actual
michael@0 205 // string in the key. No copy is made, so be careful.
michael@0 206 const char* GetString() const { return mStr; }
michael@0 207 uint32_t GetStringLength() const { return mStrLen; }
michael@0 208
michael@0 209 protected:
michael@0 210 char* mStr;
michael@0 211 uint32_t mStrLen;
michael@0 212 Ownership mOwnership;
michael@0 213 };
michael@0 214
michael@0 215 ////////////////////////////////////////////////////////////////////////////////
michael@0 216
michael@0 217 #endif // nsHashtable_h__

mercurial