File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed
Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff 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+
6484PRBool
6585nsCOMArray_base::InsertObjectAt (nsISupports* aObject, PRInt32 aIndex) {
6686 PRBool result = mArray .InsertElementAt (aObject, aIndex);
Original file line number Diff line number Diff 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) {
You can’t perform that action at this time.
0 commit comments