2001-07-31 19:05:34 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2000-05-02 22:38:04 +00:00
|
|
|
* This Original Code has been modified by IBM Corporation.
|
|
|
|
* Modifications made by IBM described herein are
|
|
|
|
* Copyright (c) International Business Machines
|
|
|
|
* Corporation, 2000
|
|
|
|
*
|
|
|
|
* Modifications to Mozilla code or documentation
|
|
|
|
* identified per MPL Section 3.3
|
|
|
|
*
|
|
|
|
* Date Modified by Description of modification
|
|
|
|
* 04/20/2000 IBM Corp. Added PR_CALLBACK for Optlink use in OS2
|
1998-10-19 17:48:55 +00:00
|
|
|
*/
|
|
|
|
|
2003-04-02 06:53:57 +00:00
|
|
|
/**
|
|
|
|
* nsHashtable is OBSOLETE. Use nsTHashtable or a derivative instead.
|
|
|
|
*/
|
|
|
|
|
1998-10-19 17:48:55 +00:00
|
|
|
#ifndef nsHashtable_h__
|
|
|
|
#define nsHashtable_h__
|
|
|
|
|
2002-11-16 02:33:49 +00:00
|
|
|
#include "pldhash.h"
|
2000-06-14 06:28:13 +00:00
|
|
|
#include "nscore.h"
|
2013-09-19 19:31:21 +00:00
|
|
|
#include "nsISupports.h"
|
2014-02-26 21:36:35 +00:00
|
|
|
#include "nsISupportsImpl.h"
|
2013-09-19 19:31:21 +00:00
|
|
|
#include "nsStringFwd.h"
|
1998-10-19 17:48:55 +00:00
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
class nsIObjectInputStream;
|
|
|
|
class nsIObjectOutputStream;
|
|
|
|
|
2013-03-17 02:46:40 +00:00
|
|
|
struct PRLock;
|
2000-08-10 06:19:37 +00:00
|
|
|
|
2011-08-18 13:46:39 +00:00
|
|
|
class nsHashKey {
|
2001-07-31 19:05:34 +00:00
|
|
|
protected:
|
2001-11-27 23:55:03 +00:00
|
|
|
nsHashKey(void) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
mKeyType = UnknownKey;
|
|
|
|
#endif
|
|
|
|
MOZ_COUNT_CTOR(nsHashKey);
|
|
|
|
}
|
|
|
|
|
2000-08-10 06:19:37 +00:00
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
public:
|
2004-01-22 01:01:19 +00:00
|
|
|
// Virtual destructor because all hash keys are |delete|d via a
|
|
|
|
// nsHashKey pointer.
|
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
virtual ~nsHashKey(void);
|
2012-08-22 15:56:38 +00:00
|
|
|
virtual uint32_t HashCode(void) const = 0;
|
2011-09-29 06:19:26 +00:00
|
|
|
virtual bool Equals(const nsHashKey *aKey) const = 0;
|
2001-07-31 19:05:34 +00:00
|
|
|
virtual nsHashKey *Clone() const = 0;
|
|
|
|
virtual nsresult Write(nsIObjectOutputStream* aStream) const;
|
2000-08-10 06:19:37 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2001-07-31 19:05:34 +00:00
|
|
|
public:
|
|
|
|
// used for verification that we're casting to the correct key type
|
|
|
|
enum nsHashKeyType {
|
|
|
|
UnknownKey,
|
|
|
|
SupportsKey,
|
2001-09-28 21:37:23 +00:00
|
|
|
PRUint32Key,
|
2001-07-31 19:05:34 +00:00
|
|
|
VoidKey,
|
|
|
|
IDKey,
|
|
|
|
CStringKey,
|
2003-03-15 01:04:32 +00:00
|
|
|
StringKey
|
2001-07-31 19:05:34 +00:00
|
|
|
};
|
|
|
|
nsHashKeyType GetKeyType() const { return mKeyType; }
|
|
|
|
protected:
|
|
|
|
nsHashKeyType mKeyType;
|
2000-08-10 06:19:37 +00:00
|
|
|
#endif
|
1998-10-19 17:48:55 +00:00
|
|
|
};
|
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
// Enumerator and Read/Write callback functions.
|
|
|
|
|
2002-07-29 22:48:15 +00:00
|
|
|
// Return values for nsHashtableEnumFunc
|
|
|
|
enum {
|
2011-10-17 14:59:28 +00:00
|
|
|
kHashEnumerateStop = false,
|
|
|
|
kHashEnumerateNext = true
|
2002-07-29 22:48:15 +00:00
|
|
|
};
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
typedef bool
|
2008-10-10 15:04:34 +00:00
|
|
|
(* nsHashtableEnumFunc)(nsHashKey *aKey, void *aData, void* aClosure);
|
1998-10-19 17:48:55 +00:00
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
typedef nsresult
|
2008-10-10 15:04:34 +00:00
|
|
|
(* nsHashtableReadEntryFunc)(nsIObjectInputStream *aStream, nsHashKey **aKey,
|
|
|
|
void **aData);
|
2001-07-31 19:05:34 +00:00
|
|
|
|
|
|
|
// NB: may be called with null aKey or aData, to free just one of the two.
|
|
|
|
typedef void
|
2008-10-10 15:04:34 +00:00
|
|
|
(* nsHashtableFreeEntryFunc)(nsIObjectInputStream *aStream, nsHashKey *aKey,
|
|
|
|
void *aData);
|
2001-07-31 19:05:34 +00:00
|
|
|
|
|
|
|
typedef nsresult
|
2008-10-10 15:04:34 +00:00
|
|
|
(* nsHashtableWriteDataFunc)(nsIObjectOutputStream *aStream, void *aData);
|
1998-10-19 17:48:55 +00:00
|
|
|
|
2011-08-18 13:46:39 +00:00
|
|
|
class nsHashtable {
|
2001-07-31 19:05:34 +00:00
|
|
|
protected:
|
2014-02-06 15:54:53 +00:00
|
|
|
// members
|
2002-11-16 02:33:49 +00:00
|
|
|
PRLock* mLock;
|
|
|
|
PLDHashTable mHashtable;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mEnumerating;
|
2001-07-31 19:05:34 +00:00
|
|
|
|
|
|
|
public:
|
2014-02-27 18:04:09 +00:00
|
|
|
nsHashtable(uint32_t aSize = 16, bool aThreadSafe = false);
|
2004-02-13 19:04:38 +00:00
|
|
|
virtual ~nsHashtable();
|
2001-07-31 19:05:34 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t Count(void) { return mHashtable.entryCount; }
|
2011-09-29 06:19:26 +00:00
|
|
|
bool Exists(nsHashKey *aKey);
|
2001-07-31 19:05:34 +00:00
|
|
|
void *Put(nsHashKey *aKey, void *aData);
|
|
|
|
void *Get(nsHashKey *aKey);
|
|
|
|
void *Remove(nsHashKey *aKey);
|
|
|
|
nsHashtable *Clone();
|
2013-10-10 20:41:00 +00:00
|
|
|
void Enumerate(nsHashtableEnumFunc aEnumFunc, void* aClosure = nullptr);
|
2001-07-31 19:05:34 +00:00
|
|
|
void Reset();
|
2013-10-10 20:41:00 +00:00
|
|
|
void Reset(nsHashtableEnumFunc destroyFunc, void* aClosure = nullptr);
|
2001-07-31 19:05:34 +00:00
|
|
|
|
|
|
|
nsHashtable(nsIObjectInputStream* aStream,
|
|
|
|
nsHashtableReadEntryFunc aReadEntryFunc,
|
|
|
|
nsHashtableFreeEntryFunc aFreeEntryFunc,
|
|
|
|
nsresult *aRetVal);
|
|
|
|
nsresult Write(nsIObjectOutputStream* aStream,
|
|
|
|
nsHashtableWriteDataFunc aWriteDataFunc) const;
|
1998-10-19 17:48:55 +00:00
|
|
|
};
|
|
|
|
|
1999-07-28 07:56:28 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsObjectHashtable: an nsHashtable where the elements are C++ objects to be
|
|
|
|
// deleted
|
|
|
|
|
2008-10-10 15:04:34 +00:00
|
|
|
typedef void* (* nsHashtableCloneElementFunc)(nsHashKey *aKey, void *aData, void* aClosure);
|
1999-07-28 07:56:28 +00:00
|
|
|
|
2011-08-18 13:46:39 +00:00
|
|
|
class nsObjectHashtable : public nsHashtable {
|
2001-07-31 19:05:34 +00:00
|
|
|
public:
|
|
|
|
nsObjectHashtable(nsHashtableCloneElementFunc cloneElementFun,
|
|
|
|
void* cloneElementClosure,
|
|
|
|
nsHashtableEnumFunc destroyElementFun,
|
|
|
|
void* destroyElementClosure,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t aSize = 16, bool threadSafe = false);
|
2001-07-31 19:05:34 +00:00
|
|
|
~nsObjectHashtable();
|
|
|
|
|
|
|
|
nsHashtable *Clone();
|
|
|
|
void Reset();
|
2011-09-29 06:19:26 +00:00
|
|
|
bool RemoveAndDelete(nsHashKey *aKey);
|
2001-07-31 19:05:34 +00:00
|
|
|
|
|
|
|
protected:
|
2008-10-10 15:04:34 +00:00
|
|
|
static PLDHashOperator CopyElement(PLDHashTable* table,
|
|
|
|
PLDHashEntryHdr* hdr,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t i, void *arg);
|
2014-02-06 15:54:53 +00:00
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
nsHashtableCloneElementFunc mCloneElementFun;
|
|
|
|
void* mCloneElementClosure;
|
|
|
|
nsHashtableEnumFunc mDestroyElementFun;
|
|
|
|
void* mDestroyElementClosure;
|
1999-07-28 07:56:28 +00:00
|
|
|
};
|
|
|
|
|
1999-01-27 02:02:22 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
1999-02-06 04:01:23 +00:00
|
|
|
// nsISupportsKey: Where keys are nsISupports objects that get refcounted.
|
|
|
|
|
2011-08-18 13:46:39 +00:00
|
|
|
class nsISupportsKey : public nsHashKey {
|
2001-07-31 19:05:34 +00:00
|
|
|
protected:
|
|
|
|
nsISupports* mKey;
|
2014-02-06 15:54:53 +00:00
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
public:
|
2002-03-01 09:28:55 +00:00
|
|
|
nsISupportsKey(const nsISupportsKey& aKey) : mKey(aKey.mKey) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
mKeyType = SupportsKey;
|
|
|
|
#endif
|
|
|
|
NS_IF_ADDREF(mKey);
|
|
|
|
}
|
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
nsISupportsKey(nsISupports* key) {
|
2000-08-10 06:19:37 +00:00
|
|
|
#ifdef DEBUG
|
2001-07-31 19:05:34 +00:00
|
|
|
mKeyType = SupportsKey;
|
2000-08-10 06:19:37 +00:00
|
|
|
#endif
|
2001-07-31 19:05:34 +00:00
|
|
|
mKey = key;
|
|
|
|
NS_IF_ADDREF(mKey);
|
|
|
|
}
|
2014-02-06 15:54:53 +00:00
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
~nsISupportsKey(void) {
|
|
|
|
NS_IF_RELEASE(mKey);
|
|
|
|
}
|
2014-02-06 15:54:53 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t HashCode(void) const {
|
2001-08-14 04:18:27 +00:00
|
|
|
return NS_PTR_TO_INT32(mKey);
|
2001-07-31 19:05:34 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool Equals(const nsHashKey *aKey) const {
|
2001-07-31 19:05:34 +00:00
|
|
|
NS_ASSERTION(aKey->GetKeyType() == SupportsKey, "mismatched key types");
|
|
|
|
return (mKey == ((nsISupportsKey *) aKey)->mKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsHashKey *Clone() const {
|
|
|
|
return new nsISupportsKey(mKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsISupportsKey(nsIObjectInputStream* aStream, nsresult *aResult);
|
|
|
|
nsresult Write(nsIObjectOutputStream* aStream) const;
|
2011-11-03 20:39:08 +00:00
|
|
|
|
|
|
|
nsISupports* GetValue() { return mKey; }
|
1999-02-06 04:01:23 +00:00
|
|
|
};
|
|
|
|
|
2001-09-28 21:37:23 +00:00
|
|
|
|
|
|
|
class nsPRUint32Key : public nsHashKey {
|
|
|
|
protected:
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mKey;
|
2001-09-28 21:37:23 +00:00
|
|
|
public:
|
2012-08-22 15:56:38 +00:00
|
|
|
nsPRUint32Key(uint32_t key) {
|
2001-09-28 21:37:23 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
mKeyType = PRUint32Key;
|
|
|
|
#endif
|
|
|
|
mKey = key;
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t HashCode(void) const {
|
2001-09-28 21:37:23 +00:00
|
|
|
return mKey;
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool Equals(const nsHashKey *aKey) const {
|
2001-09-28 21:37:23 +00:00
|
|
|
return mKey == ((const nsPRUint32Key *) aKey)->mKey;
|
|
|
|
}
|
|
|
|
nsHashKey *Clone() const {
|
|
|
|
return new nsPRUint32Key(mKey);
|
|
|
|
}
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t GetValue() { return mKey; }
|
2001-09-28 21:37:23 +00:00
|
|
|
};
|
|
|
|
|
2000-08-21 02:37:20 +00:00
|
|
|
// for null-terminated c-strings
|
2011-08-18 13:46:39 +00:00
|
|
|
class nsCStringKey : public nsHashKey {
|
2001-07-31 19:05:34 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
// NB: when serializing, NEVER_OWN keys are deserialized as OWN.
|
|
|
|
enum Ownership {
|
|
|
|
NEVER_OWN, // very long lived, even clones don't need to copy it.
|
|
|
|
OWN_CLONE, // as long lived as this key. But clones make a copy.
|
|
|
|
OWN // to be free'd in key dtor. Clones make their own copy.
|
|
|
|
};
|
|
|
|
|
2002-03-01 09:28:55 +00:00
|
|
|
nsCStringKey(const nsCStringKey& aStrKey);
|
2012-08-22 15:56:38 +00:00
|
|
|
nsCStringKey(const char* str, int32_t strLen = -1, Ownership own = OWN_CLONE);
|
2001-07-31 19:05:34 +00:00
|
|
|
nsCStringKey(const nsAFlatCString& str);
|
|
|
|
nsCStringKey(const nsACString& str);
|
|
|
|
~nsCStringKey(void);
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t HashCode(void) const;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool Equals(const nsHashKey* aKey) const;
|
2001-07-31 19:05:34 +00:00
|
|
|
nsHashKey* Clone() const;
|
|
|
|
nsCStringKey(nsIObjectInputStream* aStream, nsresult *aResult);
|
|
|
|
nsresult Write(nsIObjectOutputStream* aStream) const;
|
|
|
|
|
|
|
|
// For when the owner of the hashtable wants to peek at the actual
|
|
|
|
// string in the key. No copy is made, so be careful.
|
|
|
|
const char* GetString() const { return mStr; }
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t GetStringLength() const { return mStrLen; }
|
2001-07-31 19:05:34 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
char* mStr;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mStrLen;
|
2001-07-31 19:05:34 +00:00
|
|
|
Ownership mOwnership;
|
2000-08-10 06:19:37 +00:00
|
|
|
};
|
1999-11-16 03:10:54 +00:00
|
|
|
|
2000-08-21 02:37:20 +00:00
|
|
|
// for null-terminated unicode strings
|
2011-08-18 13:46:39 +00:00
|
|
|
class nsStringKey : public nsHashKey {
|
2001-07-31 19:05:34 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
// NB: when serializing, NEVER_OWN keys are deserialized as OWN.
|
|
|
|
enum Ownership {
|
|
|
|
NEVER_OWN, // very long lived, even clones don't need to copy it.
|
|
|
|
OWN_CLONE, // as long lived as this key. But clones make a copy.
|
|
|
|
OWN // to be free'd in key dtor. Clones make their own copy.
|
|
|
|
};
|
|
|
|
|
2002-03-01 09:28:55 +00:00
|
|
|
nsStringKey(const nsStringKey& aKey);
|
2014-01-04 15:02:17 +00:00
|
|
|
nsStringKey(const char16_t* str, int32_t strLen = -1, Ownership own = OWN_CLONE);
|
2001-07-31 19:05:34 +00:00
|
|
|
nsStringKey(const nsAFlatString& str);
|
|
|
|
nsStringKey(const nsAString& str);
|
|
|
|
~nsStringKey(void);
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t HashCode(void) const;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool Equals(const nsHashKey* aKey) const;
|
2001-07-31 19:05:34 +00:00
|
|
|
nsHashKey* Clone() const;
|
|
|
|
nsStringKey(nsIObjectInputStream* aStream, nsresult *aResult);
|
|
|
|
nsresult Write(nsIObjectOutputStream* aStream) const;
|
|
|
|
|
|
|
|
// For when the owner of the hashtable wants to peek at the actual
|
|
|
|
// string in the key. No copy is made, so be careful.
|
2014-01-04 15:02:17 +00:00
|
|
|
const char16_t* GetString() const { return mStr; }
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t GetStringLength() const { return mStrLen; }
|
2001-07-31 19:05:34 +00:00
|
|
|
|
|
|
|
protected:
|
2014-01-04 15:02:17 +00:00
|
|
|
char16_t* mStr;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mStrLen;
|
2001-07-31 19:05:34 +00:00
|
|
|
Ownership mOwnership;
|
1999-11-16 03:10:54 +00:00
|
|
|
};
|
1999-07-22 02:11:04 +00:00
|
|
|
|
2000-08-10 06:19:37 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
#endif // nsHashtable_h__
|