2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsCOMArray.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
|
2012-12-19 13:33:54 -08:00
|
|
|
// This specialization is private to nsCOMArray.
|
|
|
|
// It exists solely to automatically zero-out newly created array elements.
|
|
|
|
template<>
|
|
|
|
class nsTArrayElementTraits<nsISupports*>
|
|
|
|
{
|
|
|
|
typedef nsISupports* E;
|
|
|
|
public:
|
|
|
|
// Zero out the value
|
|
|
|
static inline void Construct(E *e) {
|
|
|
|
new (static_cast<void *>(e)) E();
|
|
|
|
}
|
|
|
|
// Invoke the copy-constructor in place.
|
|
|
|
template<class A>
|
|
|
|
static inline void Construct(E *e, const A &arg) {
|
|
|
|
new (static_cast<void *>(e)) E(arg);
|
|
|
|
}
|
|
|
|
// Invoke the destructor in place.
|
|
|
|
static inline void Destruct(E *e) {
|
|
|
|
e->~E();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static void ReleaseObjects(nsTArray<nsISupports*> &aArray);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// implementations of non-trivial methods in nsCOMArray_base
|
|
|
|
|
|
|
|
nsCOMArray_base::nsCOMArray_base(const nsCOMArray_base& aOther)
|
|
|
|
{
|
|
|
|
// make sure we do only one allocation
|
2012-12-19 13:33:54 -08:00
|
|
|
mArray.SetCapacity(aOther.Count());
|
2007-03-22 10:30:00 -07:00
|
|
|
AppendObjects(aOther);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMArray_base::~nsCOMArray_base()
|
|
|
|
{
|
2012-12-19 13:33:54 -08:00
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
2013-02-04 14:05:40 -08:00
|
|
|
nsCOMArray_base::IndexOf(nsISupports* aObject, uint32_t aStartIndex) const
|
|
|
|
{
|
|
|
|
return mArray.IndexOf(aObject, aStartIndex);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t
|
2013-02-04 14:05:40 -08:00
|
|
|
nsCOMArray_base::IndexOfObject(nsISupports* aObject) const
|
|
|
|
{
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsISupports> supports = do_QueryInterface(aObject);
|
|
|
|
NS_ENSURE_TRUE(supports, -1);
|
|
|
|
|
2012-12-19 13:33:54 -08:00
|
|
|
uint32_t i, count;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t retval = -1;
|
2012-12-19 13:33:54 -08:00
|
|
|
count = mArray.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
for (i = 0; i < count; ++i) {
|
2012-12-19 13:33:54 -08:00
|
|
|
nsCOMPtr<nsISupports> arrayItem = do_QueryInterface(mArray[i]);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (arrayItem == supports) {
|
|
|
|
retval = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2012-12-19 13:33:54 -08:00
|
|
|
bool
|
|
|
|
nsCOMArray_base::EnumerateForwards(nsBaseArrayEnumFunc aFunc, void* aData) const
|
|
|
|
{
|
|
|
|
for (uint32_t index = 0; index < mArray.Length(); index++)
|
|
|
|
if (!(*aFunc)(mArray[index], aData))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsCOMArray_base::EnumerateBackwards(nsBaseArrayEnumFunc aFunc, void* aData) const
|
|
|
|
{
|
|
|
|
for (uint32_t index = mArray.Length(); index--; )
|
|
|
|
if (!(*aFunc)(mArray[index], aData))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
nsCOMArray_base::nsCOMArrayComparator(const void* aElement1, const void* aElement2, void* aData)
|
|
|
|
{
|
|
|
|
nsCOMArrayComparatorContext* ctx = static_cast<nsCOMArrayComparatorContext*>(aData);
|
|
|
|
return (*ctx->mComparatorFunc)(*static_cast<nsISupports* const*>(aElement1),
|
|
|
|
*static_cast<nsISupports* const*>(aElement2),
|
|
|
|
ctx->mData);
|
|
|
|
}
|
|
|
|
|
2013-02-04 14:05:40 -08:00
|
|
|
void
|
|
|
|
nsCOMArray_base::Sort(nsBaseArrayComparatorFunc aFunc, void* aData)
|
2012-12-19 13:33:54 -08:00
|
|
|
{
|
|
|
|
if (mArray.Length() > 1) {
|
|
|
|
nsCOMArrayComparatorContext ctx = {aFunc, aData};
|
|
|
|
NS_QuickSort(mArray.Elements(), mArray.Length(), sizeof(nsISupports*),
|
|
|
|
nsCOMArrayComparator, &ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2013-02-04 14:05:40 -08:00
|
|
|
nsCOMArray_base::InsertObjectAt(nsISupports* aObject, int32_t aIndex)
|
|
|
|
{
|
2012-12-19 13:33:54 -08:00
|
|
|
if ((uint32_t)aIndex > mArray.Length())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!mArray.InsertElementAt(aIndex, aObject))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
NS_IF_ADDREF(aObject);
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-02-04 14:05:40 -08:00
|
|
|
void
|
|
|
|
nsCOMArray_base::InsertElementAt(uint32_t aIndex, nsISupports* aElement)
|
|
|
|
{
|
|
|
|
mArray.InsertElementAt(aIndex, aElement);
|
|
|
|
NS_IF_ADDREF(aElement);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2013-02-04 14:05:40 -08:00
|
|
|
nsCOMArray_base::InsertObjectsAt(const nsCOMArray_base& aObjects, int32_t aIndex)
|
|
|
|
{
|
2012-12-19 13:33:54 -08:00
|
|
|
if ((uint32_t)aIndex > mArray.Length())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!mArray.InsertElementsAt(aIndex, aObjects.mArray))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// need to addref all these
|
2013-02-04 14:05:40 -08:00
|
|
|
uint32_t count = aObjects.Length();
|
|
|
|
for (uint32_t i = 0; i < count; ++i)
|
|
|
|
NS_IF_ADDREF(aObjects[i]);
|
2012-12-19 13:33:54 -08:00
|
|
|
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-02-04 14:05:40 -08:00
|
|
|
void
|
|
|
|
nsCOMArray_base::InsertElementsAt(uint32_t aIndex, const nsCOMArray_base& aElements)
|
|
|
|
{
|
|
|
|
mArray.InsertElementsAt(aIndex, aElements.mArray);
|
|
|
|
|
|
|
|
// need to addref all these
|
|
|
|
uint32_t count = aElements.Length();
|
|
|
|
for (uint32_t i = 0; i < count; ++i)
|
|
|
|
NS_IF_ADDREF(aElements[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCOMArray_base::InsertElementsAt(uint32_t aIndex, nsISupports* const* aElements, uint32_t aCount)
|
|
|
|
{
|
|
|
|
mArray.InsertElementsAt(aIndex, aElements, aCount);
|
|
|
|
|
|
|
|
// need to addref all these
|
|
|
|
for (uint32_t i = 0; i < aCount; ++i)
|
|
|
|
NS_IF_ADDREF(aElements[i]);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2012-08-22 08:56:38 -07:00
|
|
|
nsCOMArray_base::ReplaceObjectAt(nsISupports* aObject, int32_t aIndex)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-04-01 13:42:54 -07:00
|
|
|
mArray.EnsureLengthAtLeast(aIndex + 1);
|
|
|
|
nsISupports *oldObject = mArray[aIndex];
|
|
|
|
// Make sure to addref first, in case aObject == oldObject
|
|
|
|
NS_IF_ADDREF(mArray[aIndex] = aObject);
|
|
|
|
NS_IF_RELEASE(oldObject);
|
|
|
|
// XXX make this return void
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMArray_base::RemoveObject(nsISupports *aObject)
|
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool result = mArray.RemoveElement(aObject);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (result)
|
|
|
|
NS_IF_RELEASE(aObject);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2012-08-22 08:56:38 -07:00
|
|
|
nsCOMArray_base::RemoveObjectAt(int32_t aIndex)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-12-19 13:33:54 -08:00
|
|
|
if (uint32_t(aIndex) < mArray.Length()) {
|
|
|
|
nsISupports* element = mArray[aIndex];
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-12-19 13:33:54 -08:00
|
|
|
mArray.RemoveElementAt(aIndex);
|
2010-06-09 11:13:16 -07:00
|
|
|
NS_IF_RELEASE(element);
|
2012-12-19 13:33:54 -08:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-02-04 14:05:40 -08:00
|
|
|
void
|
|
|
|
nsCOMArray_base::RemoveElementAt(uint32_t aIndex)
|
|
|
|
{
|
|
|
|
nsISupports* element = mArray[aIndex];
|
|
|
|
mArray.RemoveElementAt(aIndex);
|
|
|
|
NS_IF_RELEASE(element);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2012-08-22 08:56:38 -07:00
|
|
|
nsCOMArray_base::RemoveObjectsAt(int32_t aIndex, int32_t aCount)
|
2011-04-14 07:18:02 -07:00
|
|
|
{
|
2012-12-19 13:33:54 -08:00
|
|
|
if (uint32_t(aIndex) + uint32_t(aCount) <= mArray.Length()) {
|
|
|
|
nsTArray<nsISupports*> elementsToDestroy(aCount);
|
|
|
|
elementsToDestroy.AppendElements(mArray.Elements() + aIndex, aCount);
|
|
|
|
mArray.RemoveElementsAt(aIndex, aCount);
|
|
|
|
ReleaseObjects(elementsToDestroy);
|
|
|
|
return true;
|
2011-04-14 07:18:02 -07:00
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2011-04-14 07:18:02 -07:00
|
|
|
}
|
|
|
|
|
2013-02-04 14:05:40 -08:00
|
|
|
void
|
|
|
|
nsCOMArray_base::RemoveElementsAt(uint32_t aIndex, uint32_t aCount)
|
|
|
|
{
|
|
|
|
nsTArray<nsISupports*> elementsToDestroy(aCount);
|
|
|
|
elementsToDestroy.AppendElements(mArray.Elements() + aIndex, aCount);
|
|
|
|
mArray.RemoveElementsAt(aIndex, aCount);
|
|
|
|
ReleaseObjects(elementsToDestroy);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// useful for destructors
|
2012-12-19 13:33:54 -08:00
|
|
|
void
|
|
|
|
ReleaseObjects(nsTArray<nsISupports*> &aArray)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-12-19 13:33:54 -08:00
|
|
|
for (uint32_t i = 0; i < aArray.Length(); i++)
|
|
|
|
NS_IF_RELEASE(aArray[i]);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCOMArray_base::Clear()
|
|
|
|
{
|
2012-12-19 13:33:54 -08:00
|
|
|
nsTArray<nsISupports*> objects;
|
|
|
|
objects.SwapElements(mArray);
|
|
|
|
ReleaseObjects(objects);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2012-08-22 08:56:38 -07:00
|
|
|
nsCOMArray_base::SetCount(int32_t aNewCount)
|
2010-03-15 04:38:50 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aNewCount >= 0,"SetCount(negative index)");
|
|
|
|
if (aNewCount < 0)
|
2012-12-19 13:33:54 -08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
int32_t count = mArray.Length();
|
|
|
|
if (count > aNewCount)
|
|
|
|
RemoveObjectsAt(aNewCount, mArray.Length() - aNewCount);
|
|
|
|
return mArray.SetLength(aNewCount);
|
2010-03-15 04:38:50 -07:00
|
|
|
}
|
|
|
|
|
2012-12-19 13:33:54 -08:00
|
|
|
size_t
|
|
|
|
nsCOMArray_base::SizeOfExcludingThis(
|
|
|
|
nsBaseArraySizeOfElementIncludingThisFunc aSizeOfElementIncludingThis,
|
|
|
|
nsMallocSizeOfFun aMallocSizeOf, void* aData) const
|
|
|
|
{
|
|
|
|
size_t n = mArray.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
|
|
|
|
if (aSizeOfElementIncludingThis)
|
|
|
|
for (uint32_t index = 0; index < mArray.Length(); index++)
|
|
|
|
n += aSizeOfElementIncludingThis(mArray[index], aMallocSizeOf, aData);
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|