2015-04-09 20:25:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* 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/. */
|
1999-02-23 11:04:28 +03:00
|
|
|
|
|
|
|
#include "nsSupportsArrayEnumerator.h"
|
|
|
|
#include "nsISupportsArray.h"
|
|
|
|
|
|
|
|
nsSupportsArrayEnumerator::nsSupportsArrayEnumerator(nsISupportsArray* array)
|
2014-07-09 19:15:21 +04:00
|
|
|
: mArray(array)
|
|
|
|
, mCursor(0)
|
1999-02-23 11:04:28 +03:00
|
|
|
{
|
|
|
|
NS_ASSERTION(array, "null array");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSupportsArrayEnumerator::~nsSupportsArrayEnumerator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsSupportsArrayEnumerator, nsIBidirectionalEnumerator,
|
|
|
|
nsIEnumerator)
|
2008-02-21 23:39:20 +03:00
|
|
|
|
1999-02-23 11:04:28 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSupportsArrayEnumerator::First()
|
|
|
|
{
|
|
|
|
mCursor = 0;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t cnt;
|
1999-05-13 08:56:04 +04:00
|
|
|
nsresult rv = mArray->Count(&cnt);
|
2014-07-09 19:15:21 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t end = (int32_t)cnt;
|
2014-07-09 19:15:21 +04:00
|
|
|
if (mCursor < end) {
|
1999-03-05 08:48:21 +03:00
|
|
|
return NS_OK;
|
2014-07-09 19:15:21 +04:00
|
|
|
} else {
|
1999-03-05 08:48:21 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-07-09 19:15:21 +04:00
|
|
|
}
|
1999-02-23 11:04:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSupportsArrayEnumerator::Next()
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t cnt;
|
1999-05-13 08:56:04 +04:00
|
|
|
nsresult rv = mArray->Count(&cnt);
|
2014-07-09 19:15:21 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t end = (int32_t)cnt;
|
2014-07-09 19:15:21 +04:00
|
|
|
if (mCursor < end) { // don't count upward forever
|
1999-02-23 11:04:28 +03:00
|
|
|
mCursor++;
|
2014-07-09 19:15:21 +04:00
|
|
|
}
|
|
|
|
if (mCursor < end) {
|
1999-02-23 11:04:28 +03:00
|
|
|
return NS_OK;
|
2014-07-09 19:15:21 +04:00
|
|
|
} else {
|
1999-02-23 11:04:28 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-07-09 19:15:21 +04:00
|
|
|
}
|
1999-02-23 11:04:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-07-09 19:15:21 +04:00
|
|
|
nsSupportsArrayEnumerator::CurrentItem(nsISupports** aItem)
|
1999-02-23 11:04:28 +03:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aItem, "null out parameter");
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t cnt;
|
1999-05-13 08:56:04 +04:00
|
|
|
nsresult rv = mArray->Count(&cnt);
|
2014-07-09 19:15:21 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
if (mCursor >= 0 && mCursor < (int32_t)cnt) {
|
2012-12-11 20:16:42 +04:00
|
|
|
return mArray->GetElementAt(mCursor, aItem);
|
1999-02-23 11:04:28 +03:00
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSupportsArrayEnumerator::IsDone()
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t cnt;
|
1999-05-13 08:56:04 +04:00
|
|
|
nsresult rv = mArray->Count(&cnt);
|
2014-07-09 19:15:21 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2012-07-27 18:03:25 +04:00
|
|
|
// XXX This is completely incompatible with the meaning of nsresult.
|
|
|
|
// NS_ENUMERATOR_FALSE is defined to be 1. (bug 778111)
|
2012-08-22 19:56:38 +04:00
|
|
|
return (mCursor >= 0 && mCursor < (int32_t)cnt)
|
2012-07-27 18:03:25 +04:00
|
|
|
? (nsresult)NS_ENUMERATOR_FALSE : NS_OK;
|
1999-02-23 11:04:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSupportsArrayEnumerator::Last()
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t cnt;
|
1999-05-13 08:56:04 +04:00
|
|
|
nsresult rv = mArray->Count(&cnt);
|
2014-07-09 19:15:21 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
1999-05-13 08:56:04 +04:00
|
|
|
mCursor = cnt - 1;
|
1999-02-23 11:04:28 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSupportsArrayEnumerator::Prev()
|
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
if (mCursor >= 0) {
|
1999-02-23 11:04:28 +03:00
|
|
|
--mCursor;
|
2014-07-09 19:15:21 +04:00
|
|
|
}
|
|
|
|
if (mCursor >= 0) {
|
1999-02-23 11:04:28 +03:00
|
|
|
return NS_OK;
|
2014-07-09 19:15:21 +04:00
|
|
|
} else {
|
1999-02-23 11:04:28 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-07-09 19:15:21 +04:00
|
|
|
}
|
1999-02-23 11:04:28 +03:00
|
|
|
}
|
|
|
|
|