Back to index

lightning-sunbird  0.9+nobinonly
nsEnumeratorUtils.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 "nsEnumeratorUtils.h"
00039 
00040 
00041 nsArrayEnumerator::nsArrayEnumerator(nsISupportsArray* aValueArray)
00042     : mValueArray(aValueArray),
00043       mIndex(0)
00044 {
00045     NS_IF_ADDREF(mValueArray);
00046 }
00047 
00048 nsArrayEnumerator::~nsArrayEnumerator(void)
00049 {
00050     NS_IF_RELEASE(mValueArray);
00051 }
00052 
00053 NS_IMPL_ISUPPORTS1(nsArrayEnumerator, nsISimpleEnumerator)
00054 
00055 NS_IMETHODIMP
00056 nsArrayEnumerator::HasMoreElements(PRBool* aResult)
00057 {
00058     NS_PRECONDITION(aResult != 0, "null ptr");
00059     if (! aResult)
00060         return NS_ERROR_NULL_POINTER;
00061 
00062     if (!mValueArray) {
00063         *aResult = PR_FALSE;
00064         return NS_OK;
00065     }
00066 
00067     PRUint32 cnt;
00068     nsresult rv = mValueArray->Count(&cnt);
00069     if (NS_FAILED(rv)) return rv;
00070     *aResult = (mIndex < (PRInt32) cnt);
00071     return NS_OK;
00072 }
00073 
00074 NS_IMETHODIMP
00075 nsArrayEnumerator::GetNext(nsISupports** aResult)
00076 {
00077     NS_PRECONDITION(aResult != 0, "null ptr");
00078     if (! aResult)
00079         return NS_ERROR_NULL_POINTER;
00080 
00081     if (!mValueArray) {
00082         *aResult = nsnull;
00083         return NS_OK;
00084     }
00085 
00086     PRUint32 cnt;
00087     nsresult rv = mValueArray->Count(&cnt);
00088     if (NS_FAILED(rv)) return rv;
00089     if (mIndex >= (PRInt32) cnt)
00090         return NS_ERROR_UNEXPECTED;
00091 
00092     *aResult = mValueArray->ElementAt(mIndex++);
00093     return NS_OK;
00094 }
00095 
00096 extern NS_COM nsresult
00097 NS_NewArrayEnumerator(nsISimpleEnumerator* *result,
00098                       nsISupportsArray* array)
00099 {
00100     nsArrayEnumerator* enumer = new nsArrayEnumerator(array);
00101     if (enumer == nsnull)
00102         return NS_ERROR_OUT_OF_MEMORY;
00103     *result = enumer; 
00104     NS_ADDREF(*result);
00105     return NS_OK;
00106 }
00107 
00109 
00110 nsSingletonEnumerator::nsSingletonEnumerator(nsISupports* aValue)
00111     : mValue(aValue)
00112 {
00113     NS_IF_ADDREF(mValue);
00114     mConsumed = (mValue ? PR_FALSE : PR_TRUE);
00115 }
00116 
00117 nsSingletonEnumerator::~nsSingletonEnumerator()
00118 {
00119     NS_IF_RELEASE(mValue);
00120 }
00121 
00122 NS_IMPL_ISUPPORTS1(nsSingletonEnumerator, nsISimpleEnumerator)
00123 
00124 NS_IMETHODIMP
00125 nsSingletonEnumerator::HasMoreElements(PRBool* aResult)
00126 {
00127     NS_PRECONDITION(aResult != 0, "null ptr");
00128     if (! aResult)
00129         return NS_ERROR_NULL_POINTER;
00130 
00131     *aResult = !mConsumed;
00132     return NS_OK;
00133 }
00134 
00135 
00136 NS_IMETHODIMP
00137 nsSingletonEnumerator::GetNext(nsISupports** aResult)
00138 {
00139     NS_PRECONDITION(aResult != 0, "null ptr");
00140     if (! aResult)
00141         return NS_ERROR_NULL_POINTER;
00142 
00143     if (mConsumed)
00144         return NS_ERROR_UNEXPECTED;
00145 
00146     mConsumed = PR_TRUE;
00147 
00148     *aResult = mValue;
00149     NS_ADDREF(*aResult);
00150     return NS_OK;
00151 }
00152 
00153 extern "C" NS_COM nsresult
00154 NS_NewSingletonEnumerator(nsISimpleEnumerator* *result,
00155                           nsISupports* singleton)
00156 {
00157     nsSingletonEnumerator* enumer = new nsSingletonEnumerator(singleton);
00158     if (enumer == nsnull)
00159         return NS_ERROR_OUT_OF_MEMORY;
00160     *result = enumer; 
00161     NS_ADDREF(*result);
00162     return NS_OK;
00163 }
00164 
00166 
00167 nsUnionEnumerator::nsUnionEnumerator(nsISimpleEnumerator* firstEnumerator,
00168                                      nsISimpleEnumerator* secondEnumerator)
00169     : mFirstEnumerator(firstEnumerator),
00170       mSecondEnumerator(secondEnumerator),
00171       mConsumed(PR_FALSE), mAtSecond(PR_FALSE)
00172 {
00173 }
00174 
00175 nsUnionEnumerator::~nsUnionEnumerator()
00176 {
00177 }
00178 
00179 NS_IMPL_ISUPPORTS1(nsUnionEnumerator, nsISimpleEnumerator)
00180 
00181 NS_IMETHODIMP
00182 nsUnionEnumerator::HasMoreElements(PRBool* aResult)
00183 {
00184     NS_PRECONDITION(aResult != 0, "null ptr");
00185     if (! aResult)
00186         return NS_ERROR_NULL_POINTER;
00187 
00188     nsresult rv;
00189 
00190     if (mConsumed) {
00191         *aResult = PR_FALSE;
00192         return NS_OK;
00193     }
00194 
00195     if (! mAtSecond) {
00196         rv = mFirstEnumerator->HasMoreElements(aResult);
00197         if (NS_FAILED(rv)) return rv;
00198 
00199         if (*aResult)
00200             return NS_OK;
00201 
00202         mAtSecond = PR_TRUE;
00203     }
00204 
00205     rv = mSecondEnumerator->HasMoreElements(aResult);
00206     if (NS_FAILED(rv)) return rv;
00207 
00208     if (*aResult)
00209         return NS_OK;
00210 
00211     *aResult = PR_FALSE;
00212     mConsumed = PR_TRUE;
00213     return NS_OK;
00214 }
00215 
00216 NS_IMETHODIMP
00217 nsUnionEnumerator::GetNext(nsISupports** aResult)
00218 {
00219     NS_PRECONDITION(aResult != 0, "null ptr");
00220     if (! aResult)
00221         return NS_ERROR_NULL_POINTER;
00222 
00223     if (mConsumed)
00224         return NS_ERROR_UNEXPECTED;
00225 
00226     if (! mAtSecond)
00227         return mFirstEnumerator->GetNext(aResult);
00228 
00229     return mSecondEnumerator->GetNext(aResult);
00230 }
00231 
00232 extern "C" NS_COM nsresult
00233 NS_NewUnionEnumerator(nsISimpleEnumerator* *result,
00234                       nsISimpleEnumerator* firstEnumerator,
00235                       nsISimpleEnumerator* secondEnumerator)
00236 {
00237     *result = nsnull;
00238     if (! firstEnumerator) {
00239         *result = secondEnumerator;
00240     } else if (! secondEnumerator) {
00241         *result = firstEnumerator;
00242     } else {
00243         nsUnionEnumerator* enumer = new nsUnionEnumerator(firstEnumerator, secondEnumerator);
00244         if (enumer == nsnull)
00245             return NS_ERROR_OUT_OF_MEMORY;
00246         *result = enumer; 
00247     }
00248     NS_ADDREF(*result);
00249     return NS_OK;
00250 }
00251 
00252