Back to index

lightning-sunbird  0.9+nobinonly
nsSupportsArrayEnumerator.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsSupportsArrayEnumerator.h"
00039 #include "nsISupportsArray.h"
00040 
00041 nsSupportsArrayEnumerator::nsSupportsArrayEnumerator(nsISupportsArray* array)
00042   : mArray(array), mCursor(0)
00043 {
00044   NS_ASSERTION(array, "null array");
00045   NS_ADDREF(mArray);
00046 }
00047 
00048 nsSupportsArrayEnumerator::~nsSupportsArrayEnumerator()
00049 {
00050   NS_RELEASE(mArray);
00051 }
00052 
00053 NS_IMPL_ISUPPORTS2(nsSupportsArrayEnumerator, nsIBidirectionalEnumerator, nsIEnumerator)
00054 
00055 NS_IMETHODIMP
00056 nsSupportsArrayEnumerator::First()
00057 {
00058   mCursor = 0;
00059   PRUint32 cnt;
00060   nsresult rv = mArray->Count(&cnt);
00061   if (NS_FAILED(rv)) return rv;
00062   PRInt32 end = (PRInt32)cnt;
00063   if (mCursor < end)
00064     return NS_OK;
00065   else
00066     return NS_ERROR_FAILURE;
00067 }
00068 
00069 NS_IMETHODIMP
00070 nsSupportsArrayEnumerator::Next()
00071 {
00072   PRUint32 cnt;
00073   nsresult rv = mArray->Count(&cnt);
00074   if (NS_FAILED(rv)) return rv;
00075   PRInt32 end = (PRInt32)cnt;
00076   if (mCursor < end)   // don't count upward forever
00077     mCursor++;
00078   if (mCursor < end)
00079     return NS_OK;
00080   else
00081     return NS_ERROR_FAILURE;
00082 }
00083 
00084 NS_IMETHODIMP
00085 nsSupportsArrayEnumerator::CurrentItem(nsISupports **aItem)
00086 {
00087   NS_ASSERTION(aItem, "null out parameter");
00088   PRUint32 cnt;
00089   nsresult rv = mArray->Count(&cnt);
00090   if (NS_FAILED(rv)) return rv;
00091   if (mCursor >= 0 && mCursor < (PRInt32)cnt) {
00092     *aItem = mArray->ElementAt(mCursor);
00093     return NS_OK;
00094   }
00095   return NS_ERROR_FAILURE;
00096 }
00097 
00098 NS_IMETHODIMP
00099 nsSupportsArrayEnumerator::IsDone()
00100 {
00101   PRUint32 cnt;
00102   nsresult rv = mArray->Count(&cnt);
00103   if (NS_FAILED(rv)) return rv;
00104   return (mCursor >= 0 && mCursor < (PRInt32)cnt)
00105     ? NS_ENUMERATOR_FALSE : NS_OK;
00106 }
00107 
00109 
00110 NS_IMETHODIMP
00111 nsSupportsArrayEnumerator::Last()
00112 {
00113   PRUint32 cnt;
00114   nsresult rv = mArray->Count(&cnt);
00115   if (NS_FAILED(rv)) return rv;
00116   mCursor = cnt - 1;
00117   return NS_OK;
00118 }
00119 
00120 NS_IMETHODIMP
00121 nsSupportsArrayEnumerator::Prev()
00122 {
00123   if (mCursor >= 0)
00124     --mCursor;
00125   if (mCursor >= 0)
00126     return NS_OK;
00127   else
00128     return NS_ERROR_FAILURE;
00129 }
00130 
00132 
00133 NS_COM nsresult
00134 NS_NewISupportsArrayEnumerator(nsISupportsArray* array,
00135                                nsIBidirectionalEnumerator* *aInstancePtrResult)
00136 {
00137   if (aInstancePtrResult == 0)
00138     return NS_ERROR_NULL_POINTER;
00139   nsSupportsArrayEnumerator* e = new nsSupportsArrayEnumerator(array);
00140   if (e == 0)
00141     return NS_ERROR_OUT_OF_MEMORY;
00142   NS_ADDREF(e);
00143   *aInstancePtrResult = e;
00144   return NS_OK;
00145 }
00146 
00148