Bug 984123, part 2 - Convert nsTArray<char*> to nsTArray<nsCString>. r=ehsan

This commit is contained in:
Andrew McCreight 2014-05-13 09:42:41 -07:00
Родитель 5fabe16c55
Коммит 0ecdc47c59
2 изменённых файлов: 24 добавлений и 54 удалений

Просмотреть файл

@ -25,7 +25,7 @@ public:
NS_DECL_NSISIMPLEENUMERATOR
protected:
static PLDHashOperator HashEnum(const nsACString &aKey, nsTArray<char*> *aData, void *aClosure);
static PLDHashOperator HashEnum(const nsACString &aKey, nsTArray<nsCString> *aData, void *aClosure);
nsresult Initialize();
protected:
@ -100,7 +100,7 @@ nsGroupsEnumerator::GetNext(nsISupports **_retval)
/* static */
/* return false to stop */
PLDHashOperator
nsGroupsEnumerator::HashEnum(const nsACString &aKey, nsTArray<char*> *aData, void *aClosure)
nsGroupsEnumerator::HashEnum(const nsACString &aKey, nsTArray<nsCString> *aData, void *aClosure)
{
nsGroupsEnumerator *groupsEnum = static_cast<nsGroupsEnumerator*>(aClosure);
groupsEnum->mGroupNames[groupsEnum->mIndex] = (char*)aKey.Data();
@ -131,18 +131,18 @@ nsGroupsEnumerator::Initialize()
class nsNamedGroupEnumerator : public nsISimpleEnumerator
{
public:
nsNamedGroupEnumerator(nsTArray<char*> *inArray);
nsNamedGroupEnumerator(nsTArray<nsCString> *inArray);
virtual ~nsNamedGroupEnumerator();
NS_DECL_ISUPPORTS
NS_DECL_NSISIMPLEENUMERATOR
protected:
nsTArray<char*> *mGroupArray;
nsTArray<nsCString> *mGroupArray;
int32_t mIndex;
};
nsNamedGroupEnumerator::nsNamedGroupEnumerator(nsTArray<char*> *inArray)
nsNamedGroupEnumerator::nsNamedGroupEnumerator(nsTArray<nsCString> *inArray)
: mGroupArray(inArray)
, mIndex(-1)
{
@ -178,14 +178,13 @@ nsNamedGroupEnumerator::GetNext(nsISupports **_retval)
if (mIndex >= int32_t(mGroupArray->Length()))
return NS_ERROR_FAILURE;
char16_t *thisGroupName = (char16_t*)mGroupArray->ElementAt(mIndex);
NS_ASSERTION(thisGroupName, "Bad Element in mGroupArray");
const nsCString& thisGroupName = mGroupArray->ElementAt(mIndex);
nsresult rv;
nsCOMPtr<nsISupportsString> supportsString = do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv);
nsCOMPtr<nsISupportsCString> supportsString = do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID, &rv);
if (NS_FAILED(rv)) return rv;
supportsString->SetData(nsDependentString(thisGroupName));
supportsString->SetData(thisGroupName);
return CallQueryInterface(supportsString, _retval);
}
@ -209,7 +208,6 @@ nsControllerCommandGroup::~nsControllerCommandGroup()
void
nsControllerCommandGroup::ClearGroupsHash()
{
mGroupsHash.EnumerateRead(ClearEnumerator, nullptr);
mGroupsHash.Clear();
}
@ -222,21 +220,18 @@ NS_IMETHODIMP
nsControllerCommandGroup::AddCommandToGroup(const char *aCommand, const char *aGroup)
{
nsDependentCString groupKey(aGroup);
nsTArray<char*> *commandList;
nsTArray<nsCString> *commandList;
if ((commandList = mGroupsHash.Get(groupKey)) == nullptr)
{
// make this list
commandList = new nsAutoTArray<char*, 8>;
commandList = new nsAutoTArray<nsCString, 8>;
mGroupsHash.Put(groupKey, commandList);
}
// add the command to the list. Note that we're not checking for duplicates here
char *commandString = NS_strdup(aCommand); // we store allocated char16_t* in the array
if (!commandString) return NS_ERROR_OUT_OF_MEMORY;
#ifdef DEBUG
char **appended =
nsCString *appended =
#endif
commandList->AppendElement(commandString);
commandList->AppendElement(aCommand);
NS_ASSERTION(appended, "Append failed");
return NS_OK;
@ -247,21 +242,19 @@ NS_IMETHODIMP
nsControllerCommandGroup::RemoveCommandFromGroup(const char *aCommand, const char *aGroup)
{
nsDependentCString groupKey(aGroup);
nsTArray<char*> *commandList = mGroupsHash.Get(groupKey);
nsTArray<nsCString> *commandList = mGroupsHash.Get(groupKey);
if (!commandList) return NS_OK; // no group
uint32_t numEntries = commandList->Length();
for (uint32_t i = 0; i < numEntries; i++)
{
char *commandString = commandList->ElementAt(i);
if (!nsCRT::strcmp(aCommand,commandString))
nsCString commandString = commandList->ElementAt(i);
if (nsDependentCString(aCommand) != commandString)
{
commandList->RemoveElementAt(i);
nsMemory::Free(commandString);
break;
}
}
return NS_OK;
}
@ -273,14 +266,14 @@ nsControllerCommandGroup::IsCommandInGroup(const char *aCommand, const char *aGr
*_retval = false;
nsDependentCString groupKey(aGroup);
nsTArray<char*> *commandList = mGroupsHash.Get(groupKey);
nsTArray<nsCString> *commandList = mGroupsHash.Get(groupKey);
if (!commandList) return NS_OK; // no group
uint32_t numEntries = commandList->Length();
for (uint32_t i = 0; i < numEntries; i++)
{
char *commandString = commandList->ElementAt(i);
if (!nsCRT::strcmp(aCommand,commandString))
nsCString commandString = commandList->ElementAt(i);
if (nsDependentCString(aCommand) != commandString)
{
*_retval = true;
break;
@ -304,9 +297,9 @@ NS_IMETHODIMP
nsControllerCommandGroup::GetEnumeratorForGroup(const char *aGroup, nsISimpleEnumerator **_retval)
{
nsDependentCString groupKey(aGroup);
nsTArray<char*> *commandList = mGroupsHash.Get(groupKey); // may be null
nsTArray<nsCString> *commandList = mGroupsHash.Get(groupKey); // may be null
nsNamedGroupEnumerator* theGroupEnum = new nsNamedGroupEnumerator(commandList);
nsNamedGroupEnumerator *theGroupEnum = new nsNamedGroupEnumerator(commandList);
if (!theGroupEnum) return NS_ERROR_OUT_OF_MEMORY;
return theGroupEnum->QueryInterface(NS_GET_IID(nsISimpleEnumerator), (void **)_retval);
@ -315,19 +308,3 @@ nsControllerCommandGroup::GetEnumeratorForGroup(const char *aGroup, nsISimpleEnu
#if 0
#pragma mark -
#endif
PLDHashOperator
nsControllerCommandGroup::ClearEnumerator(const nsACString &aKey, nsTArray<char*> *aData, void *closure)
{
nsTArray<char*> *commandList = aData;
if (commandList)
{
uint32_t numEntries = commandList->Length();
for (uint32_t i = 0; i < numEntries; i++)
{
char *commandString = commandList->ElementAt(i);
nsMemory::Free(commandString);
}
}
return PL_DHASH_NEXT;
}

Просмотреть файл

@ -3,17 +3,13 @@
* 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/. */
#ifndef nsCommandGroup_h__
#define nsCommandGroup_h__
#include "nsIController.h"
#include "nsHashtable.h"
#include "nsClassHashtable.h"
#include "nsHashKeys.h"
// {ecd55a01-2780-11d5-a73c-ca641a6813bc}
#define NS_CONTROLLER_COMMAND_GROUP_CID \
{ 0xecd55a01, 0x2780, 0x11d5, { 0xa7, 0x3c, 0xca, 0x64, 0x1a, 0x68, 0x13, 0xbc } }
@ -21,7 +17,6 @@
#define NS_CONTROLLER_COMMAND_GROUP_CONTRACTID \
"@mozilla.org/embedcomp/controller-command-group;1"
class nsControllerCommandGroup : public nsIControllerCommandGroup
{
public:
@ -32,16 +27,14 @@ public:
NS_DECL_NSICONTROLLERCOMMANDGROUP
public:
typedef nsClassHashtable<nsCStringHashKey, nsTArray<char*>> GroupsHashtable;
typedef nsClassHashtable<nsCStringHashKey, nsTArray<nsCString>> GroupsHashtable;
protected:
void ClearGroupsHash();
static PLDHashOperator ClearEnumerator(const nsACString &aKey, nsTArray<char*> *aData, void *closure);
protected:
nsClassHashtable<nsCStringHashKey, nsTArray<char*>> mGroupsHash; // hash keyed on command group.
// This could be made more space-efficient, maybe with atoms
GroupsHashtable mGroupsHash; // hash keyed on command group.
// This could be made more space-efficient, maybe with atoms
};
#endif // nsCommandGroup_h__