Back to index

lightning-sunbird  0.9+nobinonly
nsHashKeys.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is C++ hashtable templates.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Benjamin Smedberg.
00019  * Portions created by the Initial Developer are Copyright (C) 2002
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef nsTHashKeys_h__
00039 #define nsTHashKeys_h__
00040 
00041 #include "nsID.h"
00042 #include "nsISupports.h"
00043 #include "nsCOMPtr.h"
00044 #include "pldhash.h"
00045 #include NEW_H
00046 
00047 #ifdef MOZILLA_INTERNAL_API
00048 #include "nsAString.h"
00049 #include "nsString.h"
00050 #else
00051 #include "nsStringAPI.h"
00052 #endif
00053 
00054 #include <stdlib.h>
00055 #include <string.h>
00056 
00070 NS_COM_GLUE PRUint32 HashString(const nsAString& aStr);
00071 NS_COM_GLUE PRUint32 HashString(const nsACString& aStr);
00072 NS_COM_GLUE PRUint32 HashCString(const char* aKey);
00073 
00079 class nsStringHashKey : public PLDHashEntryHdr
00080 {
00081 public:
00082   typedef const nsAString& KeyType;
00083   typedef const nsAString* KeyTypePointer;
00084 
00085   nsStringHashKey(KeyTypePointer aStr) : mStr(*aStr) { }
00086   nsStringHashKey(const nsStringHashKey& toCopy) : mStr(toCopy.mStr) { }
00087   ~nsStringHashKey() { }
00088 
00089   KeyType GetKey() const { return mStr; }
00090   KeyTypePointer GetKeyPointer() const { return &mStr; }
00091   PRBool KeyEquals(const KeyTypePointer aKey) const
00092   {
00093     return mStr.Equals(*aKey);
00094   }
00095 
00096   static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
00097   static PLDHashNumber HashKey(const KeyTypePointer aKey)
00098   {
00099     return HashString(*aKey);
00100   }
00101   enum { ALLOW_MEMMOVE = PR_TRUE };
00102 
00103 private:
00104   const nsString mStr;
00105 };
00106 
00112 class nsCStringHashKey : public PLDHashEntryHdr
00113 {
00114 public:
00115   typedef const nsACString& KeyType;
00116   typedef const nsACString* KeyTypePointer;
00117   
00118   nsCStringHashKey(const nsACString* aStr) : mStr(*aStr) { }
00119   nsCStringHashKey(const nsCStringHashKey& toCopy) : mStr(toCopy.mStr) { }
00120   ~nsCStringHashKey() { }
00121 
00122   KeyType GetKey() const { return mStr; }
00123   KeyTypePointer GetKeyPointer() const { return &mStr; }
00124 
00125   PRBool KeyEquals(KeyTypePointer aKey) const { return mStr.Equals(*aKey); }
00126 
00127   static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
00128   static PLDHashNumber HashKey(KeyTypePointer aKey)
00129   {
00130     return HashString(*aKey);
00131   }
00132   enum { ALLOW_MEMMOVE = PR_TRUE };
00133 
00134 private:
00135   const nsCString mStr;
00136 };
00137 
00143 class nsUint32HashKey : public PLDHashEntryHdr
00144 {
00145 public:
00146   typedef const PRUint32& KeyType;
00147   typedef const PRUint32* KeyTypePointer;
00148   
00149   nsUint32HashKey(KeyTypePointer aKey) : mValue(*aKey) { }
00150   nsUint32HashKey(const nsUint32HashKey& toCopy) : mValue(toCopy.mValue) { }
00151   ~nsUint32HashKey() { }
00152 
00153   KeyType GetKey() const { return mValue; }
00154   KeyTypePointer GetKeyPointer() const { return &mValue; }
00155   PRBool KeyEquals(KeyTypePointer aKey) const { return *aKey == mValue; }
00156 
00157   static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
00158   static PLDHashNumber HashKey(KeyTypePointer aKey) { return *aKey; }
00159   enum { ALLOW_MEMMOVE = PR_TRUE };
00160 
00161 private:
00162   const PRUint32 mValue;
00163 };
00164 
00170 class nsISupportsHashKey : public PLDHashEntryHdr
00171 {
00172 public:
00173   typedef nsISupports* KeyType;
00174   typedef const nsISupports* KeyTypePointer;
00175 
00176   nsISupportsHashKey(const nsISupports* key) :
00177     mSupports(NS_CONST_CAST(nsISupports*,key)) { }
00178   nsISupportsHashKey(const nsISupportsHashKey& toCopy) :
00179     mSupports(toCopy.mSupports) { }
00180   ~nsISupportsHashKey() { }
00181 
00182   KeyType GetKey() const { return mSupports; }
00183   KeyTypePointer GetKeyPointer() const { return mSupports; }
00184   
00185   PRBool KeyEquals(KeyTypePointer aKey) const { return aKey == mSupports; }
00186 
00187   static KeyTypePointer KeyToPointer(KeyType aKey) { return aKey; }
00188   static PLDHashNumber HashKey(KeyTypePointer aKey)
00189   {
00190     return NS_PTR_TO_INT32(aKey) >>2;
00191   }
00192   enum { ALLOW_MEMMOVE = PR_TRUE };
00193 
00194 private:
00195   nsCOMPtr<nsISupports> mSupports;
00196 };
00197 
00203 class nsVoidPtrHashKey : public PLDHashEntryHdr
00204 {
00205 public:
00206   typedef const void* KeyType;
00207   typedef const void* KeyTypePointer;
00208 
00209   nsVoidPtrHashKey(const void* key) :
00210     mKey(key) { }
00211   nsVoidPtrHashKey(const nsVoidPtrHashKey& toCopy) :
00212     mKey(toCopy.mKey) { }
00213   ~nsVoidPtrHashKey() { }
00214 
00215   KeyType GetKey() const { return mKey; }
00216   KeyTypePointer GetKeyPointer() const { return mKey; }
00217   
00218   PRBool KeyEquals(KeyTypePointer aKey) const { return aKey == mKey; }
00219 
00220   static KeyTypePointer KeyToPointer(KeyType aKey) { return aKey; }
00221   static PLDHashNumber HashKey(KeyTypePointer aKey)
00222   {
00223     return NS_PTR_TO_INT32(aKey) >>2;
00224   }
00225   enum { ALLOW_MEMMOVE = PR_TRUE };
00226 
00227 private:
00228   const void* mKey;
00229 };
00230 
00236 class nsIDHashKey : public PLDHashEntryHdr
00237 {
00238 public:
00239   typedef const nsID& KeyType;
00240   typedef const nsID* KeyTypePointer;
00241   
00242   nsIDHashKey(const nsID* inID) : mID(*inID) { }
00243   nsIDHashKey(const nsIDHashKey& toCopy) : mID(toCopy.mID) { }
00244   ~nsIDHashKey() { }
00245 
00246   KeyType GetKey() const { return mID; }
00247   KeyTypePointer GetKeyPointer() const { return &mID; }
00248 
00249   PRBool KeyEquals(KeyTypePointer aKey) const { return aKey->Equals(mID); }
00250 
00251   static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
00252   static PLDHashNumber HashKey(KeyTypePointer aKey);
00253   enum { ALLOW_MEMMOVE = PR_TRUE };
00254 
00255 private:
00256   const nsID mID;
00257 };
00258 
00269 class nsDepCharHashKey : public PLDHashEntryHdr
00270 {
00271 public:
00272   typedef const char* KeyType;
00273   typedef const char* KeyTypePointer;
00274 
00275   nsDepCharHashKey(const char* aKey) { mKey = aKey; }
00276   nsDepCharHashKey(const nsDepCharHashKey& toCopy) { mKey = toCopy.mKey; }
00277   ~nsDepCharHashKey() { }
00278 
00279   const char* GetKey() const { return mKey; }
00280   const char* GetKeyPointer() const { return mKey; }
00281   PRBool KeyEquals(const char* aKey) const
00282   {
00283     return !strcmp(mKey, aKey);
00284   }
00285 
00286   static const char* KeyToPointer(const char* aKey) { return aKey; }
00287   static PLDHashNumber HashKey(const char* aKey) { return HashCString(aKey); }
00288   enum { ALLOW_MEMMOVE = PR_TRUE };
00289 
00290 private:
00291   const char* mKey;
00292 };
00293 
00299 class nsCharPtrHashKey : public PLDHashEntryHdr
00300 {
00301 public:
00302   typedef const char* KeyType;
00303   typedef const char* KeyTypePointer;
00304 
00305   nsCharPtrHashKey(const char* aKey) : mKey(strdup(aKey)) { }
00306   nsCharPtrHashKey(const nsCharPtrHashKey& toCopy) : mKey(strdup(toCopy.mKey)) { }
00307   ~nsCharPtrHashKey() { if (mKey) free(NS_CONST_CAST(char *, mKey)); }
00308 
00309   const char* GetKey() const { return mKey; }
00310   const char* GetKeyPointer() const { return mKey; }
00311   PRBool KeyEquals(KeyTypePointer aKey) const
00312   {
00313     return !strcmp(mKey, aKey);
00314   }
00315 
00316   static KeyTypePointer KeyToPointer(KeyType aKey) { return aKey; }
00317   static PLDHashNumber HashKey(KeyTypePointer aKey) { return HashCString(aKey); }
00318 
00319   enum { ALLOW_MEMMOVE = PR_TRUE };
00320 
00321 private:
00322   const char* mKey;
00323 };
00324 
00325 #endif // nsTHashKeys_h__