diff --git a/xpcom/glue/nsCOMArray.cpp b/xpcom/glue/nsCOMArray.cpp index 4054426e62a7..dc2d536608bd 100644 --- a/xpcom/glue/nsCOMArray.cpp +++ b/xpcom/glue/nsCOMArray.cpp @@ -61,6 +61,26 @@ nsCOMArray_base::~nsCOMArray_base() } } +PRInt32 +nsCOMArray_base::IndexOfObject(nsISupports* aObject) const { + NS_ENSURE_TRUE(aObject, -1); + nsCOMPtr supports = do_QueryInterface(aObject); + NS_ENSURE_TRUE(supports, -1); + + PRInt32 i, count; + PRInt32 retval = -1; + count = mArray.Count(); + for (i = 0; i < count; ++i) { + nsCOMPtr arrayItem = + do_QueryInterface(NS_REINTERPRET_CAST(nsISupports*,mArray.ElementAt(i))); + if (arrayItem == supports) { + retval = i; + break; + } + } + return retval; +} + PRBool nsCOMArray_base::InsertObjectAt(nsISupports* aObject, PRInt32 aIndex) { PRBool result = mArray.InsertElementAt(aObject, aIndex); diff --git a/xpcom/glue/nsCOMArray.h b/xpcom/glue/nsCOMArray.h index c69eb72b471c..996a7504878f 100644 --- a/xpcom/glue/nsCOMArray.h +++ b/xpcom/glue/nsCOMArray.h @@ -60,6 +60,8 @@ protected: return mArray.IndexOf(aObject); } + PRInt32 IndexOfObject(nsISupports* aObject) const; + PRBool EnumerateForwards(nsVoidArrayEnumFunc aFunc, void* aData) { return mArray.EnumerateForwards(aFunc, aData); } @@ -150,10 +152,21 @@ class nsCOMArray : public nsCOMArray_base } // index of the element in question.. does NOT refcount + // note: this does not check COM object identity. Use + // IndexOfObject() for that purpose PRInt32 IndexOf(T* aObject) const { return nsCOMArray_base::IndexOf(NS_STATIC_CAST(nsISupports*, aObject)); } + // index of the element in question.. be careful! + // this is much slower than IndexOf() because it uses + // QueryInterface to determine actual COM identity of the object + // if you need to do this frequently then consider enforcing + // COM object identity before adding/comparing elements + PRInt32 IndexOfObject(T* aObject) const { + return nsCOMArray_base::IndexOfObject(NS_STATIC_CAST(nsISupports*, aObject)); + } + // inserts aObject at aIndex, shifting the objects at aIndex and // later to make space PRBool InsertObjectAt(T* aObject, PRInt32 aIndex) {