Skip to content

Commit 25738e0

Browse files
author
neil%parkwaycc.co.uk
committed
Bug 198685 need nsCOMArray function like IndexOf that checks COM object identity p=bsmedberg@covad.net r=alecf (no sr needed)
1 parent 96531e8 commit 25738e0

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

xpcom/ds/nsCOMArray.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,26 @@ nsCOMArray_base::~nsCOMArray_base()
6161
}
6262
}
6363

64+
PRInt32
65+
nsCOMArray_base::IndexOfObject(nsISupports* aObject) const {
66+
NS_ENSURE_TRUE(aObject, -1);
67+
nsCOMPtr<nsISupports> supports = do_QueryInterface(aObject);
68+
NS_ENSURE_TRUE(supports, -1);
69+
70+
PRInt32 i, count;
71+
PRInt32 retval = -1;
72+
count = mArray.Count();
73+
for (i = 0; i < count; ++i) {
74+
nsCOMPtr<nsISupports> arrayItem =
75+
do_QueryInterface(NS_REINTERPRET_CAST(nsISupports*,mArray.ElementAt(i)));
76+
if (arrayItem == supports) {
77+
retval = i;
78+
break;
79+
}
80+
}
81+
return retval;
82+
}
83+
6484
PRBool
6585
nsCOMArray_base::InsertObjectAt(nsISupports* aObject, PRInt32 aIndex) {
6686
PRBool result = mArray.InsertElementAt(aObject, aIndex);

xpcom/ds/nsCOMArray.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ class NS_COM nsCOMArray_base
6060
return mArray.IndexOf(aObject);
6161
}
6262

63+
PRInt32 IndexOfObject(nsISupports* aObject) const;
64+
6365
PRBool EnumerateForwards(nsVoidArrayEnumFunc aFunc, void* aData) {
6466
return mArray.EnumerateForwards(aFunc, aData);
6567
}
@@ -150,10 +152,21 @@ class nsCOMArray : public nsCOMArray_base
150152
}
151153

152154
// index of the element in question.. does NOT refcount
155+
// note: this does not check COM object identity. Use
156+
// IndexOfObject() for that purpose
153157
PRInt32 IndexOf(T* aObject) const {
154158
return nsCOMArray_base::IndexOf(NS_STATIC_CAST(nsISupports*, aObject));
155159
}
156160

161+
// index of the element in question.. be careful!
162+
// this is much slower than IndexOf() because it uses
163+
// QueryInterface to determine actual COM identity of the object
164+
// if you need to do this frequently then consider enforcing
165+
// COM object identity before adding/comparing elements
166+
PRInt32 IndexOfObject(T* aObject) const {
167+
return nsCOMArray_base::IndexOfObject(NS_STATIC_CAST(nsISupports*, aObject));
168+
}
169+
157170
// inserts aObject at aIndex, shifting the objects at aIndex and
158171
// later to make space
159172
PRBool InsertObjectAt(T* aObject, PRInt32 aIndex) {

0 commit comments

Comments
 (0)