Bug 1181445 (part 13) - Use nsBaseHashTable::Iterator in xpcom/ds/. r=froydnj.

This commit is contained in:
Nicholas Nethercote 2015-07-09 16:54:59 -07:00
parent 4d241772da
commit e7e7541033
2 changed files with 22 additions and 52 deletions

View File

@ -114,18 +114,6 @@ nsSimpleProperty::GetValue(nsIVariant** aValue)
// end nsSimpleProperty // end nsSimpleProperty
static PLDHashOperator
PropertyHashToArrayFunc(const nsAString& aKey,
nsIVariant* aData,
void* aUserArg)
{
nsIMutableArray* propertyArray = static_cast<nsIMutableArray*>(aUserArg);
nsSimpleProperty* sprop = new nsSimpleProperty(aKey, aData);
propertyArray->AppendElement(sprop, false);
return PL_DHASH_NEXT;
}
NS_IMETHODIMP NS_IMETHODIMP
nsHashPropertyBagBase::GetEnumerator(nsISimpleEnumerator** aResult) nsHashPropertyBagBase::GetEnumerator(nsISimpleEnumerator** aResult)
{ {
@ -134,7 +122,12 @@ nsHashPropertyBagBase::GetEnumerator(nsISimpleEnumerator** aResult)
return NS_ERROR_OUT_OF_MEMORY; return NS_ERROR_OUT_OF_MEMORY;
} }
mPropertyHash.EnumerateRead(PropertyHashToArrayFunc, propertyArray.get()); for (auto iter = mPropertyHash.Iter(); !iter.Done(); iter.Next()) {
const nsAString& key = iter.GetKey();
nsIVariant* data = iter.GetUserData();
nsSimpleProperty* sprop = new nsSimpleProperty(key, data);
propertyArray->AppendElement(sprop, false);
}
return NS_NewArrayEnumerator(aResult, propertyArray); return NS_NewArrayEnumerator(aResult, propertyArray);
} }

View File

@ -65,28 +65,6 @@ nsProperties::Has(const char* prop, bool* result)
return NS_OK; return NS_OK;
} }
struct GetKeysEnumData
{
char** keys;
uint32_t next;
nsresult res;
};
PLDHashOperator
GetKeysEnumerate(const char* aKey, nsISupports* aData, void* aArg)
{
GetKeysEnumData* gkedp = (GetKeysEnumData*)aArg;
gkedp->keys[gkedp->next] = strdup(aKey);
if (!gkedp->keys[gkedp->next]) {
gkedp->res = NS_ERROR_OUT_OF_MEMORY;
return PL_DHASH_STOP;
}
gkedp->next++;
return PL_DHASH_NEXT;
}
NS_IMETHODIMP NS_IMETHODIMP
nsProperties::GetKeys(uint32_t* aCount, char*** aKeys) nsProperties::GetKeys(uint32_t* aCount, char*** aKeys)
{ {
@ -94,28 +72,27 @@ nsProperties::GetKeys(uint32_t* aCount, char*** aKeys)
return NS_ERROR_INVALID_ARG; return NS_ERROR_INVALID_ARG;
} }
uint32_t n = Count(); uint32_t count = Count();
char** k = (char**)moz_xmalloc(n * sizeof(char*)); char** keys = (char**)moz_xmalloc(count * sizeof(char*));
uint32_t j = 0;
GetKeysEnumData gked; for (auto iter = this->Iter(); !iter.Done(); iter.Next()) {
gked.keys = k; const char* key = iter.GetKey();
gked.next = 0; keys[j] = strdup(key);
gked.res = NS_OK;
EnumerateRead(GetKeysEnumerate, &gked); if (!keys[j]) {
// Free 'em all
nsresult rv = gked.res; for (uint32_t i = 0; i < j; i++) {
if (NS_FAILED(rv)) { free(keys[i]);
// Free 'em all }
for (uint32_t i = 0; i < gked.next; i++) { free(keys);
free(k[i]); return NS_ERROR_OUT_OF_MEMORY;
} }
free(k); j++;
return rv;
} }
*aCount = n; *aCount = count;
*aKeys = k; *aKeys = keys;
return NS_OK; return NS_OK;
} }