1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Mozilla Public License Version
6
* 1.1 (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/MPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is a COM aware array class.
17
* The Initial Developer of the Original Code
18
* Netscape Communications Corp.
19
* Portions created by the Initial Developer are Copyright (C) 2002
20
* the Initial Developer. All Rights Reserved.
23
* Alec Flett <alecf@netscape.com>
25
* Alternatively, the contents of this file may be used under the terms of
26
* either the GNU General Public License Version 2 or later (the "GPL"), or
27
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28
* in which case the provisions of the GPL or the LGPL are applicable instead
29
* of those above. If you wish to allow use of your version of this file only
30
* under the terms of either the GPL or the LGPL, and not to allow others to
31
* use your version of this file under the terms of the MPL, indicate your
32
* decision by deleting the provisions above and replace them with the notice
33
* and other provisions required by the GPL or the LGPL. If you do not delete
34
* the provisions above, a recipient may use your version of this file under
35
* the terms of any one of the MPL, the GPL or the LGPL.
37
* ***** END LICENSE BLOCK ***** */
39
#ifndef nsCOMArray_h__
40
#define nsCOMArray_h__
42
#include "nsVoidArray.h"
43
#include "nsISupports.h"
45
// See below for the definition of nsCOMArray<T>
47
// a class that's nsISupports-specific, so that we can contain the
48
// work of this class in the XPCOM dll
49
class NS_COM nsCOMArray_base
54
nsCOMArray_base(PRInt32 aCount) : mArray(aCount) {}
55
nsCOMArray_base(const nsCOMArray_base& other);
58
PRInt32 IndexOf(nsISupports* aObject) const {
59
return mArray.IndexOf(aObject);
62
PRInt32 IndexOfObject(nsISupports* aObject) const;
64
PRBool EnumerateForwards(nsVoidArrayEnumFunc aFunc, void* aData) {
65
return mArray.EnumerateForwards(aFunc, aData);
68
PRBool EnumerateBackwards(nsVoidArrayEnumFunc aFunc, void* aData) {
69
return mArray.EnumerateBackwards(aFunc, aData);
72
void Sort(nsVoidArrayComparatorFunc aFunc, void* aData) {
73
mArray.Sort(aFunc, aData);
76
// any method which is not a direct forward to mArray should
77
// avoid inline bodies, so that the compiler doesn't inline them
80
PRBool InsertObjectAt(nsISupports* aObject, PRInt32 aIndex);
81
PRBool InsertObjectsAt(const nsCOMArray_base& aObjects, PRInt32 aIndex);
82
PRBool ReplaceObjectAt(nsISupports* aObject, PRInt32 aIndex);
83
PRBool AppendObject(nsISupports *aObject) {
84
return InsertObjectAt(aObject, Count());
86
PRBool AppendObjects(const nsCOMArray_base& aObjects) {
87
return InsertObjectsAt(aObjects, Count());
89
PRBool RemoveObject(nsISupports *aObject);
90
PRBool RemoveObjectAt(PRInt32 aIndex);
93
// override nsVoidArray stuff so that they can be accessed by
94
// consumers of nsCOMArray
95
PRInt32 Count() const {
96
return mArray.Count();
99
nsISupports* ObjectAt(PRInt32 aIndex) const {
100
return NS_STATIC_CAST(nsISupports*, mArray.ElementAt(aIndex));
103
nsISupports* SafeObjectAt(PRInt32 aIndex) const {
104
return NS_STATIC_CAST(nsISupports*, mArray.SafeElementAt(aIndex));
107
nsISupports* operator[](PRInt32 aIndex) const {
108
return ObjectAt(aIndex);
113
// the actual storage
116
// don't implement these, defaults will muck with refcounts!
117
nsCOMArray_base& operator=(const nsCOMArray_base& other);
120
// a non-XPCOM, refcounting array of XPCOM objects
121
// used as a member variable or stack variable - this object is NOT
122
// refcounted, but the objects that it holds are
124
// most of the read-only accessors like ObjectAt()/etc do NOT refcount
125
// on the way out. This means that you can do one of two things:
127
// * does an addref, but holds onto a reference
128
// nsCOMPtr<T> foo = array[i];
130
// * avoids the refcount, but foo might go stale if array[i] is ever
131
// * modified/removed. Be careful not to NS_RELEASE(foo)!
132
// T* foo = array[i];
134
// This array will accept null as an argument for any object, and will
135
// store null in the array, just like nsVoidArray. But that also means
136
// that methods like ObjectAt() may return null when referring to an
137
// existing, but null entry in the array.
139
class nsCOMArray : public nsCOMArray_base
143
nsCOMArray(PRInt32 aCount) : nsCOMArray_base(aCount) {}
145
// only to be used by trusted classes who are going to pass us the
147
nsCOMArray(const nsCOMArray<T>& aOther) : nsCOMArray_base(aOther) { }
151
// these do NOT refcount on the way out, for speed
152
T* ObjectAt(PRInt32 aIndex) const {
153
return NS_STATIC_CAST(T*,nsCOMArray_base::ObjectAt(aIndex));
156
// these do NOT refcount on the way out, for speed
157
T* SafeObjectAt(PRInt32 aIndex) const {
158
return NS_STATIC_CAST(T*,nsCOMArray_base::SafeObjectAt(aIndex));
161
// indexing operator for syntactic sugar
162
T* operator[](PRInt32 aIndex) const {
163
return ObjectAt(aIndex);
166
// index of the element in question.. does NOT refcount
167
// note: this does not check COM object identity. Use
168
// IndexOfObject() for that purpose
169
PRInt32 IndexOf(T* aObject) const {
170
return nsCOMArray_base::IndexOf(NS_STATIC_CAST(nsISupports*, aObject));
173
// index of the element in question.. be careful!
174
// this is much slower than IndexOf() because it uses
175
// QueryInterface to determine actual COM identity of the object
176
// if you need to do this frequently then consider enforcing
177
// COM object identity before adding/comparing elements
178
PRInt32 IndexOfObject(T* aObject) const {
179
return nsCOMArray_base::IndexOfObject(NS_STATIC_CAST(nsISupports*, aObject));
182
// inserts aObject at aIndex, shifting the objects at aIndex and
183
// later to make space
184
PRBool InsertObjectAt(T* aObject, PRInt32 aIndex) {
185
return nsCOMArray_base::InsertObjectAt(NS_STATIC_CAST(nsISupports*, aObject), aIndex);
188
// inserts the objects from aObject at aIndex, shifting the
189
// objects at aIndex and later to make space
190
PRBool InsertObjectsAt(const nsCOMArray<T>& aObjects, PRInt32 aIndex) {
191
return nsCOMArray_base::InsertObjectsAt(aObjects, aIndex);
194
// replaces an existing element. Warning: if the array grows,
195
// the newly created entries will all be null
196
PRBool ReplaceObjectAt(T* aObject, PRInt32 aIndex) {
197
return nsCOMArray_base::ReplaceObjectAt(NS_STATIC_CAST(nsISupports*, aObject), aIndex);
200
// override nsVoidArray stuff so that they can be accessed by
203
// elements in the array (including null elements!)
204
PRInt32 Count() const {
205
return nsCOMArray_base::Count();
208
// remove all elements in the array, and call NS_RELEASE on each one
210
nsCOMArray_base::Clear();
213
// Enumerator callback function. Return PR_FALSE to stop
214
// Here's a more readable form:
215
// PRBool PR_CALLBACK enumerate(T* aElement, void* aData)
216
typedef PRBool (* PR_CALLBACK nsCOMArrayEnumFunc)
217
(T* aElement, void *aData);
219
// enumerate through the array with a callback.
220
PRBool EnumerateForwards(nsCOMArrayEnumFunc aFunc, void* aData) {
221
return nsCOMArray_base::EnumerateForwards(nsVoidArrayEnumFunc(aFunc),
225
PRBool EnumerateBackwards(nsCOMArrayEnumFunc aFunc, void* aData) {
226
return nsCOMArray_base::EnumerateBackwards(nsVoidArrayEnumFunc(aFunc),
230
typedef int (* PR_CALLBACK nsCOMArrayComparatorFunc)
231
(T* aElement1, T* aElement2, void* aData);
233
void Sort(nsCOMArrayComparatorFunc aFunc, void* aData) {
234
nsCOMArray_base::Sort(nsVoidArrayComparatorFunc(aFunc), aData);
237
// append an object, growing the array as necessary
238
PRBool AppendObject(T *aObject) {
239
return nsCOMArray_base::AppendObject(NS_STATIC_CAST(nsISupports*, aObject));
242
// append objects, growing the array as necessary
243
PRBool AppendObjects(const nsCOMArray<T>& aObjects) {
244
return nsCOMArray_base::AppendObjects(aObjects);
247
// remove the first instance of the given object and shrink the
248
// array as necessary
249
// Warning: if you pass null here, it will remove the first null element
250
PRBool RemoveObject(T *aObject) {
251
return nsCOMArray_base::RemoveObject(NS_STATIC_CAST(nsISupports*, aObject));
254
// remove an element at a specific position, shrinking the array
256
PRBool RemoveObjectAt(PRInt32 aIndex) {
257
return nsCOMArray_base::RemoveObjectAt(aIndex);
262
// don't implement these!
263
nsCOMArray<T>& operator=(const nsCOMArray<T>& other);