Bug 940142 - Add a lightweight generic hash key class to nsHashKeys.h. r=bsmedberg

This commit is contained in:
Seth Fowler 2013-12-03 16:23:32 -08:00
parent 86f4f5d832
commit 6d3953bff9
2 changed files with 26 additions and 26 deletions

View File

@ -33,32 +33,6 @@ using std::max;
using std::min;
using mozilla::gfx::DrawTarget;
/**
* Hashtable key class to use with objects for which Hash() and operator==()
* are defined.
* XXX(seth): This will get moved to xpcom/glue/nsHashKeys.h in a followup bug.
*/
template <typename T>
class nsGenericHashKey : public PLDHashEntryHdr
{
public:
typedef const T& KeyType;
typedef const T* KeyTypePointer;
nsGenericHashKey(KeyTypePointer aKey) : mKey(*aKey) { }
nsGenericHashKey(const nsGenericHashKey<T>& aOther) : mKey(aOther.mKey) { }
KeyType GetKey() const { return mKey; }
bool KeyEquals(KeyTypePointer aKey) const { return *aKey == mKey; }
static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
static PLDHashNumber HashKey(KeyTypePointer aKey) { return aKey->Hash(); }
enum { ALLOW_MEMMOVE = true };
private:
T mKey;
};
namespace mozilla {
namespace image {

View File

@ -62,6 +62,7 @@ HashString(const nsACString& aStr)
* nsCharPtrHashKey
* nsUnicharPtrHashKey
* nsHashableHashKey
* nsGenericHashKey
*/
/**
@ -585,4 +586,29 @@ private:
nsCOMPtr<nsIHashable> mKey;
};
/**
* Hashtable key class to use with objects for which Hash() and operator==()
* are defined.
*/
template <typename T>
class nsGenericHashKey : public PLDHashEntryHdr
{
public:
typedef const T& KeyType;
typedef const T* KeyTypePointer;
nsGenericHashKey(KeyTypePointer aKey) : mKey(*aKey) { }
nsGenericHashKey(const nsGenericHashKey<T>& aOther) : mKey(aOther.mKey) { }
KeyType GetKey() const { return mKey; }
bool KeyEquals(KeyTypePointer aKey) const { return *aKey == mKey; }
static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
static PLDHashNumber HashKey(KeyTypePointer aKey) { return aKey->Hash(); }
enum { ALLOW_MEMMOVE = true };
private:
T mKey;
};
#endif // nsTHashKeys_h__