From 100f075a9136306a905664521e6da674c51ccd92 Mon Sep 17 00:00:00 2001 From: "neil%parkwaycc.co.uk" Date: Wed, 2 Nov 2005 16:05:09 +0000 Subject: [PATCH] Bug 198685 need nsCOMArray function like IndexOf that checks COM object identity p=bsmedberg@covad.net r=alecf (no sr needed) --- xpcom/glue/nsCOMArray.cpp | 20 ++++++++++++++++++++ xpcom/glue/nsCOMArray.h | 13 +++++++++++++ 2 files changed, 33 insertions(+) diff --git a/xpcom/glue/nsCOMArray.cpp b/xpcom/glue/nsCOMArray.cpp index 4054426e62a..dc2d536608b 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 c69eb72b471..996a7504878 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) {