From 3eb2f75165f5210d097549c97f2b343e768d6ff6 Mon Sep 17 00:00:00 2001 From: "darin%meer.net" Date: Mon, 25 Oct 2004 19:52:48 +0000 Subject: [PATCH] fixes bug 263957 "Convert nsProperties to nsTHashtable, implement GetKeys" r=bsmedberg sr=shaver --- xpcom/ds/nsArray.cpp | 2 +- xpcom/ds/nsProperties.cpp | 134 +++++++++++++++++++++----------------- xpcom/ds/nsProperties.h | 16 +++-- 3 files changed, 87 insertions(+), 65 deletions(-) diff --git a/xpcom/ds/nsArray.cpp b/xpcom/ds/nsArray.cpp index 9eaf78685e83..b2ef407e86b8 100644 --- a/xpcom/ds/nsArray.cpp +++ b/xpcom/ds/nsArray.cpp @@ -85,7 +85,7 @@ nsArray::IndexOf(PRUint32 aStartIndex, nsISupports* aElement, // optimize for the common case by forwarding to mArray if (aStartIndex == 0) { *aResult = mArray.IndexOf(aElement); - if (*aResult == -1) + if (*aResult == PR_UINT32_MAX) return NS_ERROR_FAILURE; return NS_OK; } diff --git a/xpcom/ds/nsProperties.cpp b/xpcom/ds/nsProperties.cpp index 8276e4a5db7d..a2c1dcab5193 100644 --- a/xpcom/ds/nsProperties.cpp +++ b/xpcom/ds/nsProperties.cpp @@ -36,45 +36,10 @@ * ***** END LICENSE BLOCK ***** */ #include "nsProperties.h" - -//#include +#include "nsString.h" //////////////////////////////////////////////////////////////////////////////// -nsProperties::nsProperties(nsISupports* outer) -{ - NS_INIT_AGGREGATED(outer); -} - -NS_METHOD -nsProperties::Create(nsISupports *outer, REFNSIID aIID, void **aResult) -{ - NS_ENSURE_ARG_POINTER(aResult); - NS_ENSURE_PROPER_AGGREGATION(outer, aIID); - - nsProperties* props = new nsProperties(outer); - if (props == NULL) - return NS_ERROR_OUT_OF_MEMORY; - - nsresult rv = props->AggregatedQueryInterface(aIID, aResult); - if (NS_FAILED(rv)) - delete props; - return rv; -} - -PRBool PR_CALLBACK -nsProperties::ReleaseValues(nsHashKey* key, void* data, void* closure) -{ - nsISupports* value = (nsISupports*)data; - NS_IF_RELEASE(value); - return PR_TRUE; -} - -nsProperties::~nsProperties() -{ - Enumerate(ReleaseValues); -} - NS_IMPL_AGGREGATED(nsProperties) NS_METHOD @@ -98,53 +63,106 @@ nsProperties::AggregatedQueryInterface(const nsIID& aIID, void** aInstancePtr) NS_IMETHODIMP nsProperties::Get(const char* prop, const nsIID & uuid, void* *result) { - nsresult rv; - nsCStringKey key(prop); - nsISupports* value = (nsISupports*)nsHashtable::Get(&key); - if (value) { - rv = value->QueryInterface(uuid, result); + nsCOMPtr value; + if (!nsProperties_HashBase::Get(prop, getter_AddRefs(value))) { + return NS_ERROR_FAILURE; } - else { - rv = NS_ERROR_FAILURE; - } - return rv; + return value->QueryInterface(uuid, result); } NS_IMETHODIMP nsProperties::Set(const char* prop, nsISupports* value) { - nsCStringKey key(prop); - - nsISupports* prevValue = (nsISupports*)Put(&key, value); - NS_IF_RELEASE(prevValue); - NS_IF_ADDREF(value); - return NS_OK; + return Put(prop, value) ? NS_OK : NS_ERROR_FAILURE; } NS_IMETHODIMP nsProperties::Undefine(const char* prop) { - nsCStringKey key(prop); - if (!Exists(&key)) + nsCOMPtr value; + if (!nsProperties_HashBase::Get(prop, getter_AddRefs(value))) return NS_ERROR_FAILURE; - nsISupports* prevValue = (nsISupports*)Remove(&key); - NS_IF_RELEASE(prevValue); + Remove(prop); return NS_OK; } NS_IMETHODIMP nsProperties::Has(const char* prop, PRBool *result) { - nsCStringKey key(prop); - *result = nsHashtable::Exists(&key); + nsCOMPtr value; + *result = nsProperties_HashBase::Get(prop, + getter_AddRefs(value)); return NS_OK; } +struct GetKeysEnumData +{ + char **keys; + PRUint32 next; + nsresult res; +}; + +PR_CALLBACK PLDHashOperator +GetKeysEnumerate(const char *key, nsISupports* data, + void *arg) +{ + GetKeysEnumData *gkedp = (GetKeysEnumData *)arg; + gkedp->keys[gkedp->next] = nsCRT::strdup(key); + + if (!gkedp->keys[gkedp->next]) { + gkedp->res = NS_ERROR_OUT_OF_MEMORY; + return PL_DHASH_STOP; + } + + gkedp->next++; + return PL_DHASH_NEXT; +} + NS_IMETHODIMP nsProperties::GetKeys(PRUint32 *count, char ***keys) { - return NS_ERROR_NOT_IMPLEMENTED; + PRUint32 n = Count(); + char ** k = (char **) nsMemory::Alloc(n * sizeof(char *)); + if (!k) + return NS_ERROR_OUT_OF_MEMORY; + + GetKeysEnumData gked; + gked.keys = k; + gked.next = 0; + gked.res = NS_OK; + + EnumerateRead(GetKeysEnumerate, &gked); + + if (NS_FAILED(gked.res)) { + // Free 'em all + for (PRUint32 i = 0; i < gked.next; i++) + nsMemory::Free(k[i]); + nsMemory::Free(k); + return gked.res; + } + + *count = n; + *keys = k; + return NS_OK; +} + +NS_METHOD +nsProperties::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult) +{ + NS_ENSURE_PROPER_AGGREGATION(aOuter, aIID); + + nsProperties* props = new nsProperties(aOuter); + if (props == nsnull) + return NS_ERROR_OUT_OF_MEMORY; + + NS_ADDREF(props); + nsresult rv = props->Init(); + if (NS_SUCCEEDED(rv)) + rv = props->AggregatedQueryInterface(aIID, aResult); + + NS_RELEASE(props); + return rv; } //////////////////////////////////////////////////////////////////////////////// diff --git a/xpcom/ds/nsProperties.h b/xpcom/ds/nsProperties.h index ee2255474d64..0f4fc4f72fda 100644 --- a/xpcom/ds/nsProperties.h +++ b/xpcom/ds/nsProperties.h @@ -20,6 +20,7 @@ * the Initial Developer. All Rights Reserved. * * Contributor(s): + * Mike Shaver * * Alternatively, the contents of this file may be used under the terms of * either of the GNU General Public License Version 2 or later (the "GPL"), @@ -39,7 +40,8 @@ #define nsProperties_h___ #include "nsIProperties.h" -#include "nsHashtable.h" +#include "nsInterfaceHashtable.h" +#include "nsHashKeys.h" #include "nsAgg.h" #define NS_PROPERTIES_CID \ @@ -52,21 +54,23 @@ class nsIUnicharInputStream; -class nsProperties : public nsIProperties, public nsHashtable { +typedef nsInterfaceHashtable + nsProperties_HashBase; + +class nsProperties : public nsIProperties, + public nsProperties_HashBase { public: NS_DECL_AGGREGATED NS_DECL_NSIPROPERTIES - nsProperties(nsISupports* outer); + nsProperties(nsISupports *aOuter) { NS_INIT_AGGREGATED(aOuter); } static NS_METHOD Create(nsISupports *aOuter, REFNSIID aIID, void **aResult); - static PRBool PR_CALLBACK ReleaseValues(nsHashKey* key, void* data, void* closure); - private: - ~nsProperties(); + ~nsProperties() { } }; #endif /* nsProperties_h___ */