- fix ReplaceObjectAt to properly account for existing null entries

- make nsCOMArray_base accessible from nsCOMArray<T> so that a nsCOMArray<T> can passed to NS_NewArray
for bug 162115, not part of build


git-svn-id: svn://10.0.0.236/trunk@130857 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
alecf%netscape.com 2002-10-01 00:37:41 +00:00
parent 61fdf84254
commit 82affdbd0d
2 changed files with 11 additions and 23 deletions

View File

@ -38,10 +38,8 @@
#include "nsCOMArray.h"
static PRBool AddRefObjects(void* aElement, void*);
static PRBool ReleaseObjects(void* aElement, void*);
// implementations of non-trivial methods in nsCOMArray_base
// copy constructor - we can't just memcpy here, because
@ -55,6 +53,7 @@ nsCOMArray_base::nsCOMArray_base(const nsCOMArray_base& aOther)
PRInt32 i;
for (i=0; i<count; i++) {
// ReplaceObjectAt will handle existing null entries for us
ReplaceObjectAt(aOther[i], i);
}
}
@ -70,19 +69,18 @@ nsCOMArray_base::InsertObjectAt(nsISupports* aObject, PRInt32 aIndex) {
PRBool
nsCOMArray_base::ReplaceObjectAt(nsISupports* aObject, PRInt32 aIndex)
{
// its ok if oldObject is null here
nsISupports *oldObject = ObjectAt(aIndex);
if (oldObject) {
PRBool result = mArray.ReplaceElementAt(aObject, aIndex);
// ReplaceElementAt could fail, such as if the array grows
// so only release the existing object if the replacement succeeded
if (result) {
NS_IF_RELEASE(oldObject);
NS_IF_ADDREF(aObject);
}
return result;
PRBool result = mArray.ReplaceElementAt(aObject, aIndex);
// ReplaceElementAt could fail, such as if the array grows
// so only release the existing object if the replacement succeeded
if (result) {
NS_IF_RELEASE(oldObject);
NS_IF_ADDREF(aObject);
}
return PR_FALSE;
return result;
}
@ -117,16 +115,6 @@ nsCOMArray_base::RemoveObjectAt(PRInt32 aIndex)
return PR_FALSE;
}
// useful for copy constructors
PRBool
AddRefObjects(void* aElement, void*)
{
nsISupports* element = NS_STATIC_CAST(nsISupports*,aElement);
NS_IF_ADDREF(element);
return PR_TRUE;
}
// useful for destructors
PRBool
ReleaseObjects(void* aElement, void*)

View File

@ -113,7 +113,7 @@ protected:
// that methods like ObjectAt() may return null when refering to an
// existing, but null entry in the array.
template <class T>
class nsCOMArray : protected nsCOMArray_base
class nsCOMArray : public nsCOMArray_base
{
public:
nsCOMArray() {}