1999-08-04 06:24:34 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The contents of this file are subject to the Netscape 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/NPL/
|
1999-08-04 06:24:34 +04:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* 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.
|
1999-08-04 06:24:34 +04:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1999-08-04 06:24:34 +04:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 06:43:54 +03:00
|
|
|
* Copyright (C) 1999 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
1999-12-01 02:36:32 +03:00
|
|
|
* Pierre Phaneuf <pp@ludusdesign.com>
|
2000-05-03 02:38:04 +04:00
|
|
|
* IBM Corp.
|
1999-08-04 06:24:34 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Implementation of nsHashEnumerator.
|
|
|
|
* Use it to expose nsIEnumerator interfaces around nsHashtable objects.
|
1999-11-16 06:23:10 +03:00
|
|
|
* Contributed by Rob Ginda, rginda@ix.netcom.com
|
1999-08-04 06:24:34 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nscore.h"
|
|
|
|
#include "nsHashtableEnumerator.h"
|
|
|
|
|
|
|
|
class nsHashtableEnumerator : public nsIBidirectionalEnumerator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
1999-08-22 15:25:50 +04:00
|
|
|
NS_DECL_NSIENUMERATOR
|
|
|
|
NS_DECL_NSIBIDIRECTIONALENUMERATOR
|
1999-08-04 06:24:34 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
virtual ~nsHashtableEnumerator ();
|
|
|
|
nsHashtableEnumerator (nsHashtable *aHash,
|
|
|
|
NS_HASH_ENUMERATOR_CONVERTER aConverter,
|
|
|
|
void *aData);
|
|
|
|
nsHashtableEnumerator (); /* no implementation */
|
|
|
|
|
|
|
|
private:
|
|
|
|
NS_IMETHOD Reset(nsHashtable *aHash,
|
|
|
|
NS_HASH_ENUMERATOR_CONVERTER aConverter,
|
|
|
|
void *aData);
|
|
|
|
NS_IMETHOD ReleaseElements();
|
|
|
|
|
|
|
|
nsISupports **mElements;
|
|
|
|
PRInt16 mCount, mCurrent;
|
|
|
|
PRBool mDoneFlag;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
struct nsHashEnumClosure
|
|
|
|
{
|
|
|
|
NS_HASH_ENUMERATOR_CONVERTER Converter;
|
|
|
|
nsISupports **Elements;
|
|
|
|
PRInt16 Current;
|
|
|
|
void *Data;
|
|
|
|
};
|
|
|
|
|
1999-11-16 06:10:54 +03:00
|
|
|
extern "C" NS_COM nsresult
|
1999-08-04 06:24:34 +04:00
|
|
|
NS_NewHashtableEnumerator (nsHashtable *aHash,
|
|
|
|
NS_HASH_ENUMERATOR_CONVERTER aConverter,
|
|
|
|
void *aData, nsIEnumerator **retval)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION (retval, "null ptr");
|
|
|
|
|
|
|
|
*retval = nsnull;
|
|
|
|
|
|
|
|
nsHashtableEnumerator *hte = new nsHashtableEnumerator (aHash, aConverter,
|
|
|
|
aData);
|
|
|
|
if (!hte)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
1999-12-01 02:36:32 +03:00
|
|
|
return hte->QueryInterface (NS_GET_IID(nsIEnumerator),
|
1999-08-04 06:24:34 +04:00
|
|
|
(void **)retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsHashtableEnumerator::nsHashtableEnumerator (nsHashtable *aHash,
|
|
|
|
NS_HASH_ENUMERATOR_CONVERTER aConverter,
|
|
|
|
void *aData)
|
|
|
|
: mElements(nsnull), mCount(0), mDoneFlag(PR_TRUE)
|
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
Reset (aHash, aConverter, aData);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2000-05-03 02:38:04 +04:00
|
|
|
PRBool PR_CALLBACK
|
1999-08-04 06:24:34 +04:00
|
|
|
hash_enumerator (nsHashKey *aKey, void *aObject, void *closure)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsHashEnumClosure *c = (nsHashEnumClosure *)closure;
|
|
|
|
|
|
|
|
rv = c->Converter (aKey, (void *)aObject, (void *)c->Data,
|
|
|
|
&c->Elements[c->Current]);
|
|
|
|
|
|
|
|
if (!NS_FAILED(rv))
|
|
|
|
c->Current++;
|
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHashtableEnumerator::Reset (nsHashtable *aHash,
|
|
|
|
NS_HASH_ENUMERATOR_CONVERTER aConverter,
|
|
|
|
void *aData)
|
|
|
|
{
|
|
|
|
nsHashEnumClosure c;
|
|
|
|
|
|
|
|
ReleaseElements();
|
|
|
|
|
|
|
|
mCurrent = c.Current = 0;
|
|
|
|
mCount = aHash->Count();
|
|
|
|
if (mCount == 0)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
mElements = c.Elements = new nsISupports*[mCount];
|
|
|
|
c.Data = aData;
|
|
|
|
c.Converter = aConverter;
|
|
|
|
aHash->Enumerate (&hash_enumerator, &c);
|
|
|
|
|
|
|
|
mCount = c.Current; /* some items may not have converted correctly */
|
|
|
|
mDoneFlag = PR_FALSE;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHashtableEnumerator::ReleaseElements()
|
|
|
|
{
|
|
|
|
|
|
|
|
for (; mCount > 0; mCount--)
|
|
|
|
if (mElements[mCount - 1])
|
|
|
|
NS_RELEASE(mElements[mCount - 1]);
|
|
|
|
|
|
|
|
delete[] mElements;
|
|
|
|
mElements = nsnull;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
1999-08-23 14:14:16 +04:00
|
|
|
NS_IMPL_ISUPPORTS2(nsHashtableEnumerator, nsIBidirectionalEnumerator, nsIEnumerator)
|
1999-08-04 06:24:34 +04:00
|
|
|
|
|
|
|
nsHashtableEnumerator::~nsHashtableEnumerator()
|
|
|
|
{
|
|
|
|
ReleaseElements();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHashtableEnumerator::First ()
|
|
|
|
{
|
|
|
|
if (!mElements || (mCount == 0))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
mCurrent = 0;
|
|
|
|
mDoneFlag = PR_FALSE;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHashtableEnumerator::Last ()
|
|
|
|
{
|
|
|
|
if (!mElements || (mCount == 0))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
mCurrent = mCount - 1;
|
|
|
|
mDoneFlag = PR_FALSE;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHashtableEnumerator::Prev ()
|
|
|
|
{
|
1999-11-16 06:10:54 +03:00
|
|
|
if (!mElements || (mCount == 0) || (mCurrent == 0)) {
|
|
|
|
mDoneFlag = PR_TRUE;
|
1999-08-04 06:24:34 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
1999-11-16 06:10:54 +03:00
|
|
|
}
|
1999-08-04 06:24:34 +04:00
|
|
|
|
|
|
|
mCurrent--;
|
1999-11-16 06:10:54 +03:00
|
|
|
mDoneFlag = PR_FALSE;
|
1999-08-04 06:24:34 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHashtableEnumerator::Next ()
|
|
|
|
{
|
1999-11-16 06:10:54 +03:00
|
|
|
if (!mElements || (mCount == 0) || (mCurrent == mCount - 1)) {
|
|
|
|
mDoneFlag = PR_TRUE;
|
1999-08-04 06:24:34 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
1999-11-16 06:10:54 +03:00
|
|
|
}
|
1999-08-04 06:24:34 +04:00
|
|
|
|
|
|
|
mCurrent++;
|
1999-11-16 06:10:54 +03:00
|
|
|
mDoneFlag = PR_FALSE;
|
1999-08-04 06:24:34 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHashtableEnumerator::CurrentItem (nsISupports **retval)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION (retval, "null ptr");
|
|
|
|
NS_PRECONDITION (mElements, "invalid state");
|
|
|
|
NS_ASSERTION (mCurrent >= 0, "mCurrent less than zero");
|
|
|
|
|
|
|
|
if (mCount == 0)
|
|
|
|
{
|
|
|
|
*retval = nsnull;
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION (mCurrent <= mCount - 1, "mCurrent too high");
|
|
|
|
|
|
|
|
*retval = mElements[mCurrent];
|
|
|
|
|
|
|
|
/* who says the item can't be null? */
|
|
|
|
if (*retval)
|
|
|
|
NS_ADDREF(*retval);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHashtableEnumerator::IsDone ()
|
|
|
|
{
|
|
|
|
|
|
|
|
if ((!mElements) || (mCount == 0) || (mDoneFlag))
|
1999-11-16 06:10:54 +03:00
|
|
|
return NS_OK;
|
1999-08-04 06:24:34 +04:00
|
|
|
|
1999-11-16 06:10:54 +03:00
|
|
|
return NS_COMFALSE;
|
1999-08-04 06:24:34 +04:00
|
|
|
}
|