1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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
15
* The Original Code is C++ hashtable templates.
17
* The Initial Developer of the Original Code is
19
* Portions created by the Initial Developer are Copyright (C) 2002
20
* the Initial Developer. All Rights Reserved.
24
* Alternatively, the contents of this file may be used under the terms of
25
* either the GNU General Public License Version 2 or later (the "GPL"), or
26
* 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 ***** */
38
#ifndef nsTHashKeys_h__
39
#define nsTHashKeys_h__
41
#include "nsAString.h"
45
#include "nsReadableUtils.h"
46
#include "nsISupports.h"
51
/** @file nsHashKeys.h
52
* standard HashKey classes for nsBaseHashtable and relatives. Each of these
53
* classes follows the nsTHashtable::EntryType specification
55
* Lightweight keytypes provided here:
65
* hashkey wrapper using nsAString KeyType
67
* @see nsTHashtable::EntryType for specification
69
class NS_COM nsStringHashKey : public PLDHashEntryHdr
72
typedef const nsAString& KeyType;
73
typedef const nsAString* KeyTypePointer;
75
nsStringHashKey(KeyTypePointer aStr) : mStr(*aStr) { }
76
nsStringHashKey(const nsStringHashKey& toCopy) : mStr(toCopy.mStr) { }
77
~nsStringHashKey() { }
79
KeyType GetKey() const { return mStr; }
80
KeyTypePointer GetKeyPointer() const { return &mStr; }
81
PRBool KeyEquals(const KeyTypePointer aKey) const
83
return mStr.Equals(*aKey);
86
static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
87
static PLDHashNumber HashKey(const KeyTypePointer aKey)
89
return HashString(*aKey);
91
enum { ALLOW_MEMMOVE = PR_TRUE };
98
* hashkey wrapper using nsACString KeyType
100
* @see nsTHashtable::EntryType for specification
102
class NS_COM nsCStringHashKey : public PLDHashEntryHdr
105
typedef const nsACString& KeyType;
106
typedef const nsACString* KeyTypePointer;
108
nsCStringHashKey(const nsACString* aStr) : mStr(*aStr) { }
109
nsCStringHashKey(const nsCStringHashKey& toCopy) : mStr(toCopy.mStr) { }
110
~nsCStringHashKey() { }
112
KeyType GetKey() const { return mStr; }
113
KeyTypePointer GetKeyPointer() const { return &mStr; }
115
PRBool KeyEquals(KeyTypePointer aKey) const { return mStr.Equals(*aKey); }
117
static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
118
static PLDHashNumber HashKey(KeyTypePointer aKey)
120
return HashString(*aKey);
122
enum { ALLOW_MEMMOVE = PR_TRUE };
125
const nsCString mStr;
129
* hashkey wrapper using PRUint32 KeyType
131
* @see nsTHashtable::EntryType for specification
133
class NS_COM nsUint32HashKey : public PLDHashEntryHdr
136
typedef const PRUint32& KeyType;
137
typedef const PRUint32* KeyTypePointer;
139
nsUint32HashKey(KeyTypePointer aKey) : mValue(*aKey) { }
140
nsUint32HashKey(const nsUint32HashKey& toCopy) : mValue(toCopy.mValue) { }
141
~nsUint32HashKey() { }
143
KeyType GetKey() const { return mValue; }
144
KeyTypePointer GetKeyPointer() const { return &mValue; }
145
PRBool KeyEquals(KeyTypePointer aKey) const { return *aKey == mValue; }
147
static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
148
static PLDHashNumber HashKey(KeyTypePointer aKey) { return *aKey; }
149
enum { ALLOW_MEMMOVE = PR_TRUE };
152
const PRUint32 mValue;
156
* hashkey wrapper using nsISupports* KeyType
158
* @see nsTHashtable::EntryType for specification
160
class NS_COM nsISupportsHashKey : public PLDHashEntryHdr
163
typedef nsISupports* KeyType;
164
typedef const nsISupports* KeyTypePointer;
166
nsISupportsHashKey(const nsISupports* key) :
167
mSupports(NS_CONST_CAST(nsISupports*,key)) { }
168
nsISupportsHashKey(const nsISupportsHashKey& toCopy) :
169
mSupports(toCopy.mSupports) { }
170
~nsISupportsHashKey() { }
172
KeyType GetKey() const { return mSupports; }
173
KeyTypePointer GetKeyPointer() const { return mSupports; }
175
PRBool KeyEquals(KeyTypePointer aKey) const { return aKey == mSupports; }
177
static KeyTypePointer KeyToPointer(KeyType aKey) { return aKey; }
178
static PLDHashNumber HashKey(KeyTypePointer aKey)
180
return NS_PTR_TO_INT32(aKey) >>2;
182
enum { ALLOW_MEMMOVE = PR_TRUE };
185
nsCOMPtr<nsISupports> mSupports;
189
* hashkey wrapper using void* KeyType
191
* @see nsTHashtable::EntryType for specification
193
class NS_COM nsVoidPtrHashKey : public PLDHashEntryHdr
196
typedef const void* KeyType;
197
typedef const void* KeyTypePointer;
199
nsVoidPtrHashKey(const void* key) :
201
nsVoidPtrHashKey(const nsVoidPtrHashKey& toCopy) :
202
mKey(toCopy.mKey) { }
203
~nsVoidPtrHashKey() { }
205
KeyType GetKey() const { return mKey; }
206
KeyTypePointer GetKeyPointer() const { return mKey; }
208
PRBool KeyEquals(KeyTypePointer aKey) const { return aKey == mKey; }
210
static KeyTypePointer KeyToPointer(KeyType aKey) { return aKey; }
211
static PLDHashNumber HashKey(KeyTypePointer aKey)
213
return NS_PTR_TO_INT32(aKey) >>2;
215
enum { ALLOW_MEMMOVE = PR_TRUE };
222
* hashkey wrapper using nsID KeyType
224
* @see nsTHashtable::EntryType for specification
226
class NS_COM nsIDHashKey : public PLDHashEntryHdr
229
typedef const nsID& KeyType;
230
typedef const nsID* KeyTypePointer;
232
nsIDHashKey(const nsID* id) : mID(*id) { }
233
nsIDHashKey(const nsIDHashKey& toCopy) : mID(toCopy.mID) { }
236
KeyType GetKey() const { return mID; }
237
KeyTypePointer GetKeyPointer() const { return &mID; }
239
PRBool KeyEquals(KeyTypePointer aKey) const { return aKey->Equals(mID); }
241
static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
242
static PLDHashNumber HashKey(KeyTypePointer aKey);
243
enum { ALLOW_MEMMOVE = PR_TRUE };
250
* hashkey wrapper for "dependent" const char*; this class does not "own"
251
* its string pointer.
253
* This class must only be used if the strings have a lifetime longer than
254
* the hashtable they occupy. This normally occurs only for static
255
* strings or strings that have been arena-allocated.
257
* @see nsTHashtable::EntryType for specification
259
class NS_COM nsDepCharHashKey : public PLDHashEntryHdr
262
typedef const char* KeyType;
263
typedef const char* KeyTypePointer;
265
nsDepCharHashKey(const char* aKey) { mKey = aKey; }
266
nsDepCharHashKey(const nsDepCharHashKey& toCopy) { mKey = toCopy.mKey; }
267
~nsDepCharHashKey() { }
269
const char* GetKey() const { return mKey; }
270
const char* GetKeyPointer() const { return mKey; }
271
PRBool KeyEquals(const char* aKey) const
273
return !strcmp(mKey, aKey);
276
static const char* KeyToPointer(const char* aKey) { return aKey; }
277
static PLDHashNumber HashKey(const char* aKey) { return nsCRT::HashCode(aKey); }
278
enum { ALLOW_MEMMOVE = PR_TRUE };
285
* hashkey wrapper for const char*; at construction, this class duplicates
286
* a string pointed to by the pointer so that it doesn't matter whether or not
287
* the string lives longer than the hash table.
289
class NS_COM nsCharPtrHashKey : public PLDHashEntryHdr
292
typedef const char* KeyType;
293
typedef const char* KeyTypePointer;
295
nsCharPtrHashKey(const char* aKey) : mKey(strdup(aKey)) { }
296
nsCharPtrHashKey(const nsCharPtrHashKey& toCopy) : mKey(strdup(toCopy.mKey)) { }
297
~nsCharPtrHashKey() { if (mKey) free(NS_CONST_CAST(char *, mKey)); }
299
const char* GetKey() const { return mKey; }
300
const char* GetKeyPointer() const { return mKey; }
301
PRBool KeyEquals(KeyTypePointer aKey) const
303
return !strcmp(mKey, aKey);
306
static KeyTypePointer KeyToPointer(KeyType aKey) { return aKey; }
307
static PLDHashNumber HashKey(KeyTypePointer aKey) { return nsCRT::HashCode(aKey); }
309
enum { ALLOW_MEMMOVE = PR_TRUE };
315
#endif // nsTHashKeys_h__