Back to index

lightning-sunbird  0.9+nobinonly
nsArrayEnumerator.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 XPCOM Array implementation.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corp.
00019  * Portions created by the Initial Developer are Copyright (C) 2002
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Alec Flett <alecf@netscape.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsArrayEnumerator.h"
00040 
00041 NS_IMPL_ISUPPORTS1(nsSimpleArrayEnumerator, nsISimpleEnumerator)
00042 
00043 NS_IMETHODIMP
00044 nsSimpleArrayEnumerator::HasMoreElements(PRBool* aResult)
00045 {
00046     NS_PRECONDITION(aResult != 0, "null ptr");
00047     if (! aResult)
00048         return NS_ERROR_NULL_POINTER;
00049 
00050     if (!mValueArray) {
00051         *aResult = PR_FALSE;
00052         return NS_OK;
00053     }
00054 
00055     PRUint32 cnt;
00056     nsresult rv = mValueArray->GetLength(&cnt);
00057     if (NS_FAILED(rv)) return rv;
00058     *aResult = (mIndex < cnt);
00059     return NS_OK;
00060 }
00061 
00062 NS_IMETHODIMP
00063 nsSimpleArrayEnumerator::GetNext(nsISupports** aResult)
00064 {
00065     NS_PRECONDITION(aResult != 0, "null ptr");
00066     if (! aResult)
00067         return NS_ERROR_NULL_POINTER;
00068 
00069     if (!mValueArray) {
00070         *aResult = nsnull;
00071         return NS_OK;
00072     }
00073 
00074     PRUint32 cnt;
00075     nsresult rv = mValueArray->GetLength(&cnt);
00076     if (NS_FAILED(rv)) return rv;
00077     if (mIndex >= cnt)
00078         return NS_ERROR_UNEXPECTED;
00079 
00080     return mValueArray->QueryElementAt(mIndex++, NS_GET_IID(nsISupports), (void**)aResult);
00081 }
00082 
00083 extern NS_COM nsresult
00084 NS_NewArrayEnumerator(nsISimpleEnumerator* *result,
00085                       nsIArray* array)
00086 {
00087     nsSimpleArrayEnumerator* enumer = new nsSimpleArrayEnumerator(array);
00088     if (enumer == nsnull)
00089         return NS_ERROR_OUT_OF_MEMORY;
00090 
00091     NS_ADDREF(*result = enumer);
00092     return NS_OK;
00093 }
00094 
00096 
00097 // enumerator implementation for nsCOMArray
00098 // creates a snapshot of the array in question
00099 // you MUST use NS_NewArrayEnumerator to create this, so that
00100 // allocation is done correctly
00101 class nsCOMArrayEnumerator : public nsISimpleEnumerator
00102 {
00103 public:
00104     // nsISupports interface
00105     NS_DECL_ISUPPORTS
00106 
00107     // nsISimpleEnumerator interface
00108     NS_DECL_NSISIMPLEENUMERATOR
00109 
00110     // nsSimpleArrayEnumerator methods
00111     nsCOMArrayEnumerator() : mIndex(0) {
00112     }
00113 
00114     // specialized operator to make sure we make room for mValues
00115     void* operator new (size_t size, const nsCOMArray_base& aArray) CPP_THROW_NEW;
00116     void operator delete(void* ptr) {
00117         ::operator delete(ptr);
00118     }
00119 
00120 private:
00121     ~nsCOMArrayEnumerator(void);
00122 
00123 protected:
00124     PRUint32 mIndex;            // current position
00125     PRUint32 mArraySize;        // size of the array
00126     
00127     // this is actually bigger
00128     nsISupports* mValueArray[1];
00129 };
00130 
00131 NS_IMPL_ISUPPORTS1(nsCOMArrayEnumerator, nsISimpleEnumerator)
00132 
00133 nsCOMArrayEnumerator::~nsCOMArrayEnumerator()
00134 {
00135     // only release the entries that we haven't visited yet
00136     for (; mIndex < mArraySize; ++mIndex) {
00137         NS_IF_RELEASE(mValueArray[mIndex]);
00138     }
00139 }
00140 
00141 NS_IMETHODIMP
00142 nsCOMArrayEnumerator::HasMoreElements(PRBool* aResult)
00143 {
00144     NS_PRECONDITION(aResult != 0, "null ptr");
00145     if (! aResult)
00146         return NS_ERROR_NULL_POINTER;
00147 
00148     *aResult = (mIndex < mArraySize);
00149     return NS_OK;
00150 }
00151 
00152 NS_IMETHODIMP
00153 nsCOMArrayEnumerator::GetNext(nsISupports** aResult)
00154 {
00155     NS_PRECONDITION(aResult != 0, "null ptr");
00156     if (! aResult)
00157         return NS_ERROR_NULL_POINTER;
00158 
00159     if (mIndex >= mArraySize)
00160         return NS_ERROR_UNEXPECTED;
00161 
00162     // pass the ownership of the reference to the caller. Since
00163     // we AddRef'ed during creation of |this|, there is no need
00164     // to AddRef here
00165     *aResult = mValueArray[mIndex++];
00166 
00167     // this really isn't necessary. just pretend this happens, since
00168     // we'll never visit this value again!
00169     // mValueArray[(mIndex-1)] = nsnull;
00170     
00171     return NS_OK;
00172 }
00173 
00174 void*
00175 nsCOMArrayEnumerator::operator new (size_t size, const nsCOMArray_base& aArray)
00176     CPP_THROW_NEW
00177 {
00178     // create enough space such that mValueArray points to a large
00179     // enough value. Note that the initial value of size gives us
00180     // space for mValueArray[0], so we must subtract
00181     size += (aArray.Count() - 1) * sizeof(aArray[0]);
00182 
00183     // do the actual allocation
00184     nsCOMArrayEnumerator * result =
00185         NS_STATIC_CAST(nsCOMArrayEnumerator*, ::operator new(size));
00186 
00187     // now need to copy over the values, and addref each one
00188     // now this might seem like alot of work, but we're actually just
00189     // doing all our AddRef's ahead of time since GetNext() doesn't
00190     // need to AddRef() on the way out
00191     PRUint32 i;
00192     PRUint32 max = result->mArraySize = aArray.Count();
00193     for (i = 0; i<max; i++) {
00194         result->mValueArray[i] = aArray[i];
00195         NS_IF_ADDREF(result->mValueArray[i]);
00196     }
00197 
00198     return result;
00199 }
00200 
00201 extern NS_COM nsresult
00202 NS_NewArrayEnumerator(nsISimpleEnumerator* *aResult,
00203                       const nsCOMArray_base& aArray)
00204 {
00205     nsCOMArrayEnumerator *enumerator = new (aArray) nsCOMArrayEnumerator();
00206     if (!enumerator) return NS_ERROR_OUT_OF_MEMORY;
00207 
00208     NS_ADDREF(*aResult = enumerator);
00209     return NS_OK;
00210 }