VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/ds/nsCOMArray.h@ 4837

Last change on this file since 4837 was 1, checked in by vboxsync, 54 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.5 KB
Line 
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
4 *
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/
9 *
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
13 * License.
14 *
15 * The Original Code is a COM aware array class.
16 *
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.
21 *
22 * Contributor(s):
23 * Alec Flett <alecf@netscape.com>
24 *
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.
36 *
37 * ***** END LICENSE BLOCK ***** */
38
39#ifndef nsCOMArray_h__
40#define nsCOMArray_h__
41
42#include "nsVoidArray.h"
43#include "nsISupports.h"
44
45// See below for the definition of nsCOMArray<T>
46
47// a class that's nsISupports-specific, so that we can contain the
48// work of this class in the XPCOM dll
49class NS_COM nsCOMArray_base
50{
51 friend class nsArray;
52protected:
53 nsCOMArray_base() {}
54 nsCOMArray_base(PRInt32 aCount) : mArray(aCount) {}
55 nsCOMArray_base(const nsCOMArray_base& other);
56 ~nsCOMArray_base();
57
58 PRInt32 IndexOf(nsISupports* aObject) const {
59 return mArray.IndexOf(aObject);
60 }
61
62 PRInt32 IndexOfObject(nsISupports* aObject) const;
63
64 PRBool EnumerateForwards(nsVoidArrayEnumFunc aFunc, void* aData) {
65 return mArray.EnumerateForwards(aFunc, aData);
66 }
67
68 PRBool EnumerateBackwards(nsVoidArrayEnumFunc aFunc, void* aData) {
69 return mArray.EnumerateBackwards(aFunc, aData);
70 }
71
72 void Sort(nsVoidArrayComparatorFunc aFunc, void* aData) {
73 mArray.Sort(aFunc, aData);
74 }
75
76 // any method which is not a direct forward to mArray should
77 // avoid inline bodies, so that the compiler doesn't inline them
78 // all over the place
79 void Clear();
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());
85 }
86 PRBool AppendObjects(const nsCOMArray_base& aObjects) {
87 return InsertObjectsAt(aObjects, Count());
88 }
89 PRBool RemoveObject(nsISupports *aObject);
90 PRBool RemoveObjectAt(PRInt32 aIndex);
91
92public:
93 // override nsVoidArray stuff so that they can be accessed by
94 // consumers of nsCOMArray
95 PRInt32 Count() const {
96 return mArray.Count();
97 }
98
99 nsISupports* ObjectAt(PRInt32 aIndex) const {
100 return NS_STATIC_CAST(nsISupports*, mArray.FastElementAt(aIndex));
101 }
102
103 nsISupports* SafeObjectAt(PRInt32 aIndex) const {
104 return NS_STATIC_CAST(nsISupports*, mArray.SafeElementAt(aIndex));
105 }
106
107 nsISupports* operator[](PRInt32 aIndex) const {
108 return ObjectAt(aIndex);
109 }
110
111private:
112
113 // the actual storage
114 nsVoidArray mArray;
115
116 // don't implement these, defaults will muck with refcounts!
117 nsCOMArray_base& operator=(const nsCOMArray_base& other);
118};
119
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
123//
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:
126//
127// * does an addref, but holds onto a reference
128// nsCOMPtr<T> foo = array[i];
129//
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];
133//
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.
138template <class T>
139class nsCOMArray : public nsCOMArray_base
140{
141 public:
142 nsCOMArray() {}
143 nsCOMArray(PRInt32 aCount) : nsCOMArray_base(aCount) {}
144
145 // only to be used by trusted classes who are going to pass us the
146 // right type!
147 nsCOMArray(const nsCOMArray<T>& aOther) : nsCOMArray_base(aOther) { }
148
149 ~nsCOMArray() {}
150
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));
154 }
155
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));
159 }
160
161 // indexing operator for syntactic sugar
162 T* operator[](PRInt32 aIndex) const {
163 return ObjectAt(aIndex);
164 }
165
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));
171 }
172
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));
180 }
181
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);
186 }
187
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);
192 }
193
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);
198 }
199
200 // override nsVoidArray stuff so that they can be accessed by
201 // other methods
202
203 // elements in the array (including null elements!)
204 PRInt32 Count() const {
205 return nsCOMArray_base::Count();
206 }
207
208 // remove all elements in the array, and call NS_RELEASE on each one
209 void Clear() {
210 nsCOMArray_base::Clear();
211 }
212
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);
218
219 // enumerate through the array with a callback.
220 PRBool EnumerateForwards(nsCOMArrayEnumFunc aFunc, void* aData) {
221 return nsCOMArray_base::EnumerateForwards(nsVoidArrayEnumFunc(aFunc),
222 aData);
223 }
224
225 PRBool EnumerateBackwards(nsCOMArrayEnumFunc aFunc, void* aData) {
226 return nsCOMArray_base::EnumerateBackwards(nsVoidArrayEnumFunc(aFunc),
227 aData);
228 }
229
230 typedef int (* PR_CALLBACK nsCOMArrayComparatorFunc)
231 (T* aElement1, T* aElement2, void* aData);
232
233 void Sort(nsCOMArrayComparatorFunc aFunc, void* aData) {
234 nsCOMArray_base::Sort(nsVoidArrayComparatorFunc(aFunc), aData);
235 }
236
237 // append an object, growing the array as necessary
238 PRBool AppendObject(T *aObject) {
239 return nsCOMArray_base::AppendObject(NS_STATIC_CAST(nsISupports*, aObject));
240 }
241
242 // append objects, growing the array as necessary
243 PRBool AppendObjects(const nsCOMArray<T>& aObjects) {
244 return nsCOMArray_base::AppendObjects(aObjects);
245 }
246
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));
252 }
253
254 // remove an element at a specific position, shrinking the array
255 // as necessary
256 PRBool RemoveObjectAt(PRInt32 aIndex) {
257 return nsCOMArray_base::RemoveObjectAt(aIndex);
258 }
259
260private:
261
262 // don't implement these!
263 nsCOMArray<T>& operator=(const nsCOMArray<T>& other);
264};
265
266
267#endif
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use