michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsSupportsArrayEnumerator.h" michael@0: #include "nsISupportsArray.h" michael@0: michael@0: nsSupportsArrayEnumerator::nsSupportsArrayEnumerator(nsISupportsArray* array) michael@0: : mArray(array), mCursor(0) michael@0: { michael@0: NS_ASSERTION(array, "null array"); michael@0: NS_ADDREF(mArray); michael@0: } michael@0: michael@0: nsSupportsArrayEnumerator::~nsSupportsArrayEnumerator() michael@0: { michael@0: NS_RELEASE(mArray); michael@0: } michael@0: michael@0: NS_IMPL_ISUPPORTS(nsSupportsArrayEnumerator, nsIBidirectionalEnumerator, nsIEnumerator) michael@0: michael@0: NS_IMETHODIMP michael@0: nsSupportsArrayEnumerator::First() michael@0: { michael@0: mCursor = 0; michael@0: uint32_t cnt; michael@0: nsresult rv = mArray->Count(&cnt); michael@0: if (NS_FAILED(rv)) return rv; michael@0: int32_t end = (int32_t)cnt; michael@0: if (mCursor < end) michael@0: return NS_OK; michael@0: else michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsSupportsArrayEnumerator::Next() michael@0: { michael@0: uint32_t cnt; michael@0: nsresult rv = mArray->Count(&cnt); michael@0: if (NS_FAILED(rv)) return rv; michael@0: int32_t end = (int32_t)cnt; michael@0: if (mCursor < end) // don't count upward forever michael@0: mCursor++; michael@0: if (mCursor < end) michael@0: return NS_OK; michael@0: else michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsSupportsArrayEnumerator::CurrentItem(nsISupports **aItem) michael@0: { michael@0: NS_ASSERTION(aItem, "null out parameter"); michael@0: uint32_t cnt; michael@0: nsresult rv = mArray->Count(&cnt); michael@0: if (NS_FAILED(rv)) return rv; michael@0: if (mCursor >= 0 && mCursor < (int32_t)cnt) { michael@0: return mArray->GetElementAt(mCursor, aItem); michael@0: } michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsSupportsArrayEnumerator::IsDone() michael@0: { michael@0: uint32_t cnt; michael@0: nsresult rv = mArray->Count(&cnt); michael@0: if (NS_FAILED(rv)) return rv; michael@0: // XXX This is completely incompatible with the meaning of nsresult. michael@0: // NS_ENUMERATOR_FALSE is defined to be 1. (bug 778111) michael@0: return (mCursor >= 0 && mCursor < (int32_t)cnt) michael@0: ? (nsresult)NS_ENUMERATOR_FALSE : NS_OK; michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: NS_IMETHODIMP michael@0: nsSupportsArrayEnumerator::Last() michael@0: { michael@0: uint32_t cnt; michael@0: nsresult rv = mArray->Count(&cnt); michael@0: if (NS_FAILED(rv)) return rv; michael@0: mCursor = cnt - 1; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsSupportsArrayEnumerator::Prev() michael@0: { michael@0: if (mCursor >= 0) michael@0: --mCursor; michael@0: if (mCursor >= 0) michael@0: return NS_OK; michael@0: else michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: