add support for a nsISimpleEnumerator implementation for nsCOMArray<T> and nsIArray, and update existing interfaces to support that:

- move nsCOMArray_base's ObjectAt/[]/Count() methods into the public so that nsCOMArrayEnumerator can get to it
- tweak NS_NewArray() to match the existing enumerator NS_New* API
- hook up NS_NewArrayEnumerator to nsArray::Enumerate
Not part of the build, for bug 162115
This commit is contained in:
alecf%netscape.com 2002-10-01 17:34:25 +00:00
Родитель d901a98463
Коммит 16cf343fb7
5 изменённых файлов: 336 добавлений и 27 удалений

Просмотреть файл

@ -37,6 +37,7 @@
* ***** END LICENSE BLOCK ***** */
#include "nsArray.h"
#include "nsArrayEnumerator.h"
// used by IndexOf()
struct findIndexOfClosure
@ -98,7 +99,7 @@ nsArray::IndexOf(PRUint32 aStartIndex, nsISupports* aElement,
NS_IMETHODIMP
nsArray::Enumerate(nsISimpleEnumerator **aResult)
{
return NS_ERROR_NOT_IMPLEMENTED;
return NS_NewArrayEnumerator(aResult, NS_STATIC_CAST(nsIArray*, this));
}
// nsIMutableArray implementation
@ -160,7 +161,7 @@ NS_NewArray(nsIArray** aResult)
}
nsresult
NS_NewArray(const nsCOMArray_base& aBaseArray, nsIArray** aResult)
NS_NewArray(nsIArray** aResult, const nsCOMArray_base& aBaseArray)
{
nsArray* arr = new nsArray(aBaseArray);
if (!arr) return NS_ERROR_OUT_OF_MEMORY;

Просмотреть файл

@ -50,9 +50,23 @@
{ 0x35c66fd1, 0x95e9, 0x4e0a, \
{ 0x80, 0xc5, 0xc3, 0xbd, 0x2b, 0x37, 0x54, 0x81 } }
// create a new, empty array
nsresult NS_COM
NS_NewArray(nsIArray** aResult);
// The resulting array will hold an owning reference to every element
// in the original nsCOMArray<T>. This also means that any further
// changes to the original nsCOMArray<T> will not affect the new
// array, and that the original array can go away and the new array
// will still hold valid elements.
nsresult NS_COM
NS_NewArray(nsIArray** aResult, const nsCOMArray_base& base);
// adapter class to map nsIArray->nsCOMArray
// do NOT declare this as a stack or member variable, use
// nsCOMArray instead
// nsCOMArray instead!
// if you need to convert a nsCOMArray->nsIArray, see NS_NewArray above
class nsArray : public nsIMutableArray
{
public:
@ -71,13 +85,4 @@ private:
};
// create a new, empty array
nsresult NS_COM
NS_NewArray(nsIArray** aResult);
// makes a copy of an nsCOMArray<T> - any further changes to the base
// array will not affect the new array
nsresult NS_COM
NS_NewArray(const nsCOMArray_base& base, nsIArray** aResult);
#endif

Просмотреть файл

@ -0,0 +1,218 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is XPCOM Array implementation.
*
* The Initial Developer of the Original Code
* Netscape Communications Corp.
* Portions created by the Initial Developer are Copyright (C) 2002
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Alec Flett <alecf@netscape.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsArrayEnumerator.h"
NS_IMPL_ISUPPORTS1(nsSimpleArrayEnumerator, nsISimpleEnumerator)
NS_IMETHODIMP
nsSimpleArrayEnumerator::HasMoreElements(PRBool* aResult)
{
NS_PRECONDITION(aResult != 0, "null ptr");
if (! aResult)
return NS_ERROR_NULL_POINTER;
if (!mValueArray) {
*aResult = PR_FALSE;
return NS_OK;
}
PRUint32 cnt;
nsresult rv = mValueArray->GetLength(&cnt);
if (NS_FAILED(rv)) return rv;
*aResult = (mIndex < (PRInt32) cnt);
return NS_OK;
}
NS_IMETHODIMP
nsSimpleArrayEnumerator::GetNext(nsISupports** aResult)
{
NS_PRECONDITION(aResult != 0, "null ptr");
if (! aResult)
return NS_ERROR_NULL_POINTER;
if (!mValueArray) {
*aResult = nsnull;
return NS_OK;
}
PRUint32 cnt;
nsresult rv = mValueArray->GetLength(&cnt);
if (NS_FAILED(rv)) return rv;
if (mIndex >= (PRInt32) cnt)
return NS_ERROR_UNEXPECTED;
return mValueArray->QueryElementAt(mIndex++, NS_GET_IID(nsISupports), (void**)aResult);
}
extern NS_COM nsresult
NS_NewArrayEnumerator(nsISimpleEnumerator* *result,
nsIArray* array)
{
nsSimpleArrayEnumerator* enumer = new nsSimpleArrayEnumerator(array);
if (enumer == nsnull)
return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(enumer);
*result = enumer;
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////
// enumerator implementation for nsCOMArray
// creates a snapshot of the array in question
// you MUST use NS_NewArrayEnumerator to create this, so that
// allocation is done correctly
class nsCOMArrayEnumerator : public nsISimpleEnumerator
{
public:
// nsISupports interface
NS_DECL_ISUPPORTS
// nsISimpleEnumerator interface
NS_DECL_NSISIMPLEENUMERATOR
// nsSimpleArrayEnumerator methods
nsCOMArrayEnumerator() : mIndex(0) {}
virtual ~nsCOMArrayEnumerator(void);
// specialized operator to make sure we make room for mValues
void* operator new (size_t size, const nsCOMArray_base& aArray) CPP_THROW_NEW;
void operator delete(void* ptr) {
::operator delete(ptr);
}
protected:
PRUint32 mIndex; // current position
PRUint32 mArraySize; // size of the array
// this is actually bigger
nsISupports* mValueArray[1];
};
NS_IMPL_ISUPPORTS1(nsCOMArrayEnumerator, nsISimpleEnumerator)
nsCOMArrayEnumerator::~nsCOMArrayEnumerator()
{
// only release the entries that we haven't visited yet
while (mIndex < mArraySize) {
NS_RELEASE(mValueArray[mIndex++]);
}
}
NS_IMETHODIMP
nsCOMArrayEnumerator::HasMoreElements(PRBool* aResult)
{
NS_PRECONDITION(aResult != 0, "null ptr");
if (! aResult)
return NS_ERROR_NULL_POINTER;
if (!mValueArray) {
*aResult = PR_FALSE;
return NS_OK;
}
*aResult = (mIndex < mArraySize);
return NS_OK;
}
NS_IMETHODIMP
nsCOMArrayEnumerator::GetNext(nsISupports** aResult)
{
NS_PRECONDITION(aResult != 0, "null ptr");
if (! aResult)
return NS_ERROR_NULL_POINTER;
if (!mValueArray) {
*aResult = nsnull;
return NS_OK;
}
if (mIndex >= mArraySize)
return NS_ERROR_UNEXPECTED;
// pass the ownership of the reference to the caller. Since
// we AddRef'ed during creation of |this|, there is no need
// to AddRef here
*aResult = mValueArray[mIndex++];
// this really isn't necessary. just pretend this happens, since
// we'll never visit this value again!
// mValueArray[(mIndex-1)] = nsnull;
return NS_OK;
}
void*
nsCOMArrayEnumerator::operator new (size_t size, const nsCOMArray_base& aArray)
CPP_THROW_NEW
{
// create enough space such that mValueArray points to a large
// enough value. Note that the initial value of size gives us
// space for mValueArray[0], so we must subtract
size += (aArray.Count() - 1) * sizeof(aArray[0]);
// do the actual allocation
nsCOMArrayEnumerator * result =
NS_STATIC_CAST(nsCOMArrayEnumerator*, ::operator new(size));
// now need to copy over the values, and addref each one
// now this might seem like alot of work, but we're actually just
// doing all our AddRef's ahead of time since GetNext() doesn't
// need to AddRef() on the way out
PRUint32 i;
PRUint32 max = result->mArraySize = aArray.Count();
for (i = 0; i<max; i++) {
result->mValueArray[i] = aArray[i];
NS_IF_ADDREF(result->mValueArray[i]);
}
return result;
}
extern NS_COM nsresult
NS_NewArrayEnumerator(nsISimpleEnumerator* *aResult,
const nsCOMArray_base& aArray)
{
nsCOMArrayEnumerator *enumerator = new (aArray) nsCOMArrayEnumerator();
if (!enumerator) return NS_ERROR_OUT_OF_MEMORY;
*aResult = enumerator;
NS_ADDREF(*aResult);
return NS_OK;
}

Просмотреть файл

@ -0,0 +1,84 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is XPCOM Array implementation.
*
* The Initial Developer of the Original Code
* Netscape Communications Corp.
* Portions created by the Initial Developer are Copyright (C) 2002
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Alec Flett <alecf@netscape.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifndef nsArrayEnumerator_h__
#define nsArrayEnumerator_h__
// enumerator implementation for nsIArray
#include "nsIArray.h"
#include "nsCOMArray.h"
#include "nsISimpleEnumerator.h"
#include "nsCOMPtr.h"
class NS_COM nsSimpleArrayEnumerator : public nsISimpleEnumerator
{
public:
// nsISupports interface
NS_DECL_ISUPPORTS
// nsISimpleEnumerator interface
NS_DECL_NSISIMPLEENUMERATOR
// nsSimpleArrayEnumerator methods
nsSimpleArrayEnumerator(nsIArray* aValueArray) :
mValueArray(aValueArray), mIndex(0) {}
virtual ~nsSimpleArrayEnumerator(void) {}
protected:
nsCOMPtr<nsIArray> mValueArray;
PRUint32 mIndex;
};
// Create an enumerator for an existing nsIArray implementation
// The enumerator holds an owning reference to the array.
extern NS_COM nsresult
NS_NewArrayEnumerator(nsISimpleEnumerator* *result,
nsIArray* array);
// create an enumerator for an existing nsCOMArray<T> implementation
// The enumerator will hold an owning reference to each ELEMENT in
// the array. This means that the nsCOMArray<T> can safely go away
// without its objects going away.
extern NS_COM nsresult
NS_NewArrayEnumerator(nsISimpleEnumerator* *aResult,
const nsCOMArray_base& aArray);
#endif

Просмотреть файл

@ -53,24 +53,10 @@ protected:
nsCOMArray_base(PRInt32 aCount) : mArray(aCount) {}
nsCOMArray_base(const nsCOMArray_base& other);
nsISupports* ObjectAt(PRInt32 aIndex) const {
return NS_STATIC_CAST(nsISupports*, mArray.ElementAt(aIndex));
}
nsISupports* operator[](PRInt32 aIndex) const {
return ObjectAt(aIndex);
}
PRInt32 IndexOf(nsISupports* aObject) {
return mArray.IndexOf(aObject);
}
// override nsVoidArray stuff so that they can be accessed by
// consumers of nsCOMArray
PRInt32 Count() const {
return mArray.Count();
}
PRBool EnumerateForwards(nsVoidArrayEnumFunc aFunc, void* aData) {
return mArray.EnumerateForwards(aFunc, aData);
}
@ -85,7 +71,22 @@ protected:
PRBool RemoveObject(nsISupports *aObject);
PRBool RemoveObjectAt(PRInt32 aIndex);
private:
public:
// override nsVoidArray stuff so that they can be accessed by
// consumers of nsCOMArray
PRInt32 Count() const {
return mArray.Count();
}
nsISupports* ObjectAt(PRInt32 aIndex) const {
return NS_STATIC_CAST(nsISupports*, mArray.ElementAt(aIndex));
}
nsISupports* operator[](PRInt32 aIndex) const {
return ObjectAt(aIndex);
}
private:
// the actual storage
nsVoidArray mArray;