Bug 316732 - Allow for hashing on nsIFile by introducing a new nsIHashable interface and implementing it for the file types r=darin

This commit is contained in:
bsmedberg%covad.net 2005-11-18 14:18:49 +00:00
parent aa31dc4ef1
commit c0af4ff1aa
9 changed files with 191 additions and 3 deletions

View File

@ -122,6 +122,7 @@ XPIDLSRCS = \
nsIAtomService.idl \
nsICollection.idl \
nsIEnumerator.idl \
nsIHashable.idl \
nsIINIParser.idl \
nsIPersistentProperties2.idl \
nsIProperty.idl \

58
xpcom/ds/nsIHashable.idl Normal file
View File

@ -0,0 +1,58 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla XPCOM.
*
* The Initial Developer of the Original Code is
* Benjamin Smedberg <benjamin@smedbergs.us>.
*
* Portions created by the Initial Developer are Copyright (C) 2005
* the Mozilla Foundation <http://www.mozilla.org/>. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsISupports.idl"
/**
* Represents an object that can be stored in a hashtable.
*/
[scriptable, uuid(17e595fa-b57a-4933-bd0f-b1812e8ab188)]
interface nsIHashable : nsISupports
{
/**
* Is this object the equivalent of the other object?
*/
boolean equals(in nsIHashable aOther);
/**
* A generated hashcode for this object. Objects that are equivalent
* must have the same hash code. Getting this property should never
* throw an exception!
*/
readonly attribute unsigned long hashCode;
};

View File

@ -40,6 +40,7 @@
#include "nsID.h"
#include "nsISupports.h"
#include "nsIHashable.h"
#include "nsCOMPtr.h"
#include "pldhash.h"
#include NEW_H
@ -317,4 +318,44 @@ private:
const char* mKey;
};
/**
* Hashtable key class to use with objects that support nsIHashable
*/
class nsHashableHashKey : public PLDHashEntryHdr
{
public:
typedef nsIHashable* KeyType;
typedef const nsIHashable* KeyTypePointer;
nsHashableHashKey(const nsIHashable* aKey) :
mKey(NS_CONST_CAST(nsIHashable*, aKey)) { }
nsHashableHashKey(const nsHashableHashKey& toCopy) :
mKey(toCopy.mKey) { }
~nsHashableHashKey() { }
nsIHashable* GetKey() const { return mKey; }
const nsIHashable* GetKeyPointer() const { return mKey; }
PRBool KeyEquals(const nsIHashable* aKey) const {
PRBool eq;
if (NS_SUCCEEDED(mKey->Equals(NS_CONST_CAST(nsIHashable*,aKey), &eq))) {
return eq;
}
return PR_FALSE;
}
static const nsIHashable* KeyToPointer(nsIHashable* aKey) { return aKey; }
static PLDHashNumber HashKey(const nsIHashable* aKey) {
PRUint32 code = 8888; // magic number if GetHashCode fails :-(
nsresult rv = NS_CONST_CAST(nsIHashable*,aKey)->GetHashCode(&code);
NS_ASSERTION(NS_SUCCEEDED(rv), "GetHashCode should not throw!");
return code;
}
enum { ALLOW_MEMMOVE = PR_TRUE };
private:
nsCOMPtr<nsIHashable> mKey;
};
#endif // nsTHashKeys_h__

View File

@ -2038,6 +2038,28 @@ nsresult nsLocalFile::CFStringReftoUTF8(CFStringRef aInStrRef, nsACString& aOutS
return rv;
}
// nsIHashable
NS_IMETHODIMP
nsLocalFile::Equals(nsIHashable* aOther, PRBool *aResult)
{
nsCOMPtr<nsIFile> otherfile(do_QueryInterface(aOther));
if (!otherfile) {
*aResult = PR_FALSE;
return NS_OK;
}
return Equals(otherfile, aResult);
}
NS_IMETHODIMP
nsLocalFile::GetHashCode(PRUint32 *aResult)
{
CFURLRef whichURLRef = mFollowLinks ? mTargetRef : mBaseRef;
*aResult = CFHash(whichURLRef);
return NS_OK;
}
//*****************************************************************************
// Global Functions
//*****************************************************************************

View File

@ -41,6 +41,7 @@
#include "nsILocalFileMac.h"
#include "nsString.h"
#include "nsIHashable.h"
class nsDirEnumerator;
@ -55,7 +56,8 @@ class nsDirEnumerator;
// for the conversion to NFC (composed Unicode) done in "non-Native" getters.
//*****************************************************************************
class NS_COM nsLocalFile : public nsILocalFileMac
class NS_COM nsLocalFile : public nsILocalFileMac,
public nsIHashable
{
friend class nsDirEnumerator;
@ -70,6 +72,7 @@ public:
NS_DECL_NSIFILE
NS_DECL_NSILOCALFILE
NS_DECL_NSILOCALFILEMAC
NS_DECL_NSIHASHABLE
public:

View File

@ -1750,6 +1750,28 @@ nsLocalFile::GetTarget(nsAString &_retval)
{
GET_UCS(GetNativeTarget, _retval);
}
// nsIHashable
NS_IMETHODIMP
nsLocalFile::Equals(nsIHashable* aOther, PRBool *aResult)
{
nsCOMPtr<nsIFile> otherFile(do_QueryInterface(aOther));
if (!otherFile) {
*aResult = PR_FALSE;
return NS_OK;
}
return Equals(otherFile, aResult);
}
NS_IMETHODIMP
nsLocalFile::GetHashCode(PRUint32 *aResult)
{
*aResult = nsCRT::HashCode(mPath.get());
return NS_OK;
}
nsresult
NS_NewLocalFile(const nsAString &path, PRBool followLinks, nsILocalFile* *result)
{

View File

@ -51,6 +51,7 @@
#include "nscore.h"
#include "nsString.h"
#include "nsReadableUtils.h"
#include "nsIHashable.h"
/**
* we need these for statfs()
@ -80,7 +81,8 @@
#include <sys/mount.h>
#endif
class NS_COM nsLocalFile : public nsILocalFile
class NS_COM nsLocalFile : public nsILocalFile,
public nsIHashable
{
public:
NS_DEFINE_STATIC_CID_ACCESSOR(NS_LOCAL_FILE_CID)
@ -98,6 +100,9 @@ public:
// nsILocalFile
NS_DECL_NSILOCALFILE
// nsIHashable
NS_DECL_NSIHASHABLE
public:
static void GlobalInit();
static void GlobalShutdown();

View File

@ -2524,6 +2524,37 @@ nsLocalFile::GetTarget(nsAString &_retval)
return rv;
}
// nsIHashable
NS_IMETHODIMP
nsLocalFile::Equals(nsIHashable* aOther, PRBool *aResult)
{
nsCOMPtr<nsIFile> otherfile(do_QueryInterface(aOther));
if (!otherfile) {
*aResult = PR_FALSE;
return NS_OK;
}
return Equals(otherfile, aResult);
}
NS_IMETHODIMP
nsLocalFile::GetHashCode(PRUint32 *aResult)
{
// In order for short and long path names to hash to the same value we
// always hash on the short pathname.
char thisshort[MAX_PATH];
DWORD thisr = GetShortPathName(mWorkingPath.get(),
thisshort, sizeof(thisshort));
if (thisr < sizeof(thisshort))
*aResult = nsCRT::HashCode(thisshort);
else
*aResult = nsCRT::HashCode(mWorkingPath.get());
return NS_OK;
}
nsresult
NS_NewLocalFile(const nsAString &path, PRBool followLinks, nsILocalFile* *result)
{

View File

@ -48,6 +48,7 @@
#include "nsIFile.h"
#include "nsIFactory.h"
#include "nsILocalFileWin.h"
#include "nsIHashable.h"
#include "windows.h"
@ -61,7 +62,8 @@ DEFINE_OLEGUID(IID_IPersistFile, 0x0000010BL, 0, 0);
#include <sys/stat.h>
class nsLocalFile : public nsILocalFileWin
class nsLocalFile : public nsILocalFileWin,
public nsIHashable
{
public:
NS_DEFINE_STATIC_CID_ACCESSOR(NS_LOCAL_FILE_CID)
@ -82,6 +84,9 @@ public:
// nsILocalFileWin interface
NS_DECL_NSILOCALFILEWIN
// nsIHashable interface
NS_DECL_NSIHASHABLE
public:
static void GlobalInit();
static void GlobalShutdown();