2014-06-30 19:39:45 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04: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/. */
|
2005-11-02 19:04:34 +03:00
|
|
|
|
|
|
|
#include "nsCOMArray.h"
|
2013-06-30 20:26:39 +04:00
|
|
|
|
|
|
|
#include "mozilla/MemoryReporting.h"
|
2016-08-23 01:40:10 +03:00
|
|
|
#include "mozilla/OperatorNewExtensions.h"
|
2013-06-30 20:26:39 +04:00
|
|
|
|
2005-11-02 19:05:13 +03:00
|
|
|
#include "nsCOMPtr.h"
|
2005-11-02 19:04:34 +03:00
|
|
|
|
2012-12-20 01:33:54 +04:00
|
|
|
// This specialization is private to nsCOMArray.
|
|
|
|
// It exists solely to automatically zero-out newly created array elements.
|
|
|
|
template<>
|
|
|
|
class nsTArrayElementTraits<nsISupports*>
|
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
typedef nsISupports* E;
|
2012-12-20 01:33:54 +04:00
|
|
|
public:
|
2014-06-27 05:35:39 +04:00
|
|
|
// Zero out the value
|
|
|
|
static inline void Construct(E* aE)
|
|
|
|
{
|
2016-08-23 01:40:10 +03:00
|
|
|
new (mozilla::KnownNotNull, static_cast<void*>(aE)) E();
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
|
|
|
// Invoke the copy-constructor in place.
|
|
|
|
template<class A>
|
|
|
|
static inline void Construct(E* aE, const A& aArg)
|
|
|
|
{
|
2016-08-23 01:40:10 +03:00
|
|
|
new (mozilla::KnownNotNull, static_cast<void*>(aE)) E(aArg);
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
|
|
|
// Invoke the destructor in place.
|
|
|
|
static inline void Destruct(E* aE)
|
|
|
|
{
|
|
|
|
aE->~E();
|
|
|
|
}
|
2012-12-20 01:33:54 +04:00
|
|
|
};
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
static void ReleaseObjects(nsTArray<nsISupports*>& aArray);
|
2005-11-02 19:04:36 +03:00
|
|
|
|
2005-11-02 19:04:34 +03:00
|
|
|
// implementations of non-trivial methods in nsCOMArray_base
|
|
|
|
|
2005-11-02 19:04:36 +03:00
|
|
|
nsCOMArray_base::nsCOMArray_base(const nsCOMArray_base& aOther)
|
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
// make sure we do only one allocation
|
|
|
|
mArray.SetCapacity(aOther.Count());
|
|
|
|
AppendObjects(aOther);
|
2005-11-02 19:04:36 +03:00
|
|
|
}
|
|
|
|
|
2005-11-02 19:04:47 +03:00
|
|
|
nsCOMArray_base::~nsCOMArray_base()
|
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
Clear();
|
2012-12-20 01:33:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
2013-02-05 02:05:40 +04:00
|
|
|
nsCOMArray_base::IndexOf(nsISupports* aObject, uint32_t aStartIndex) const
|
|
|
|
{
|
|
|
|
return mArray.IndexOf(aObject, aStartIndex);
|
2005-11-02 19:04:47 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
2013-02-05 02:05:40 +04:00
|
|
|
nsCOMArray_base::IndexOfObject(nsISupports* aObject) const
|
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
nsCOMPtr<nsISupports> supports = do_QueryInterface(aObject);
|
|
|
|
if (NS_WARN_IF(!supports)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t i, count;
|
|
|
|
int32_t retval = -1;
|
|
|
|
count = mArray.Length();
|
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
nsCOMPtr<nsISupports> arrayItem = do_QueryInterface(mArray[i]);
|
|
|
|
if (arrayItem == supports) {
|
|
|
|
retval = i;
|
|
|
|
break;
|
2005-11-02 19:05:09 +03:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
|
|
|
return retval;
|
2005-11-02 19:05:09 +03:00
|
|
|
}
|
|
|
|
|
2012-12-20 01:33:54 +04:00
|
|
|
bool
|
|
|
|
nsCOMArray_base::EnumerateForwards(nsBaseArrayEnumFunc aFunc, void* aData) const
|
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
for (uint32_t index = 0; index < mArray.Length(); ++index) {
|
|
|
|
if (!(*aFunc)(mArray[index], aData)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-12-20 01:33:54 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
return true;
|
2012-12-20 01:33:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsCOMArray_base::EnumerateBackwards(nsBaseArrayEnumFunc aFunc, void* aData) const
|
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
for (uint32_t index = mArray.Length(); index--; ) {
|
|
|
|
if (!(*aFunc)(mArray[index], aData)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-12-20 01:33:54 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
return true;
|
2012-12-20 01:33:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-06-27 05:35:39 +04:00
|
|
|
nsCOMArray_base::nsCOMArrayComparator(const void* aElement1,
|
|
|
|
const void* aElement2,
|
|
|
|
void* aData)
|
2012-12-20 01:33:54 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
nsCOMArrayComparatorContext* ctx =
|
|
|
|
static_cast<nsCOMArrayComparatorContext*>(aData);
|
|
|
|
return (*ctx->mComparatorFunc)(*static_cast<nsISupports* const*>(aElement1),
|
|
|
|
*static_cast<nsISupports* const*>(aElement2),
|
|
|
|
ctx->mData);
|
2012-12-20 01:33:54 +04:00
|
|
|
}
|
|
|
|
|
2013-02-05 02:05:40 +04:00
|
|
|
void
|
|
|
|
nsCOMArray_base::Sort(nsBaseArrayComparatorFunc aFunc, void* aData)
|
2012-12-20 01:33:54 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
if (mArray.Length() > 1) {
|
|
|
|
nsCOMArrayComparatorContext ctx = {aFunc, aData};
|
|
|
|
NS_QuickSort(mArray.Elements(), mArray.Length(), sizeof(nsISupports*),
|
|
|
|
nsCOMArrayComparator, &ctx);
|
|
|
|
}
|
2012-12-20 01:33:54 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2013-02-05 02:05:40 +04:00
|
|
|
nsCOMArray_base::InsertObjectAt(nsISupports* aObject, int32_t aIndex)
|
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
if ((uint32_t)aIndex > mArray.Length()) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-12-20 01:33:54 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
if (!mArray.InsertElementAt(aIndex, aObject)) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-12-20 01:33:54 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
NS_IF_ADDREF(aObject);
|
|
|
|
return true;
|
2005-11-02 19:04:34 +03:00
|
|
|
}
|
|
|
|
|
2013-02-05 02:05:40 +04:00
|
|
|
void
|
|
|
|
nsCOMArray_base::InsertElementAt(uint32_t aIndex, nsISupports* aElement)
|
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
mArray.InsertElementAt(aIndex, aElement);
|
|
|
|
NS_IF_ADDREF(aElement);
|
2013-02-05 02:05:40 +04:00
|
|
|
}
|
|
|
|
|
2016-06-07 11:10:25 +03:00
|
|
|
void
|
|
|
|
nsCOMArray_base::InsertElementAt(uint32_t aIndex, already_AddRefed<nsISupports> aElement)
|
|
|
|
{
|
|
|
|
mArray.InsertElementAt(aIndex, aElement.take());
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2013-02-05 02:05:40 +04:00
|
|
|
nsCOMArray_base::InsertObjectsAt(const nsCOMArray_base& aObjects, int32_t aIndex)
|
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
if ((uint32_t)aIndex > mArray.Length()) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-12-20 01:33:54 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
if (!mArray.InsertElementsAt(aIndex, aObjects.mArray)) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-12-20 01:33:54 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
// need to addref all these
|
|
|
|
uint32_t count = aObjects.Length();
|
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
|
|
|
NS_IF_ADDREF(aObjects[i]);
|
|
|
|
}
|
2012-12-20 01:33:54 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
return true;
|
2005-11-02 19:04:44 +03:00
|
|
|
}
|
|
|
|
|
2013-02-05 02:05:40 +04:00
|
|
|
void
|
2014-06-27 05:35:39 +04:00
|
|
|
nsCOMArray_base::InsertElementsAt(uint32_t aIndex,
|
|
|
|
const nsCOMArray_base& aElements)
|
2013-02-05 02:05:40 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
mArray.InsertElementsAt(aIndex, aElements.mArray);
|
2013-02-05 02:05:40 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
// need to addref all these
|
|
|
|
uint32_t count = aElements.Length();
|
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
|
|
|
NS_IF_ADDREF(aElements[i]);
|
|
|
|
}
|
2013-02-05 02:05:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-06-27 05:35:39 +04:00
|
|
|
nsCOMArray_base::InsertElementsAt(uint32_t aIndex,
|
|
|
|
nsISupports* const* aElements,
|
|
|
|
uint32_t aCount)
|
2013-02-05 02:05:40 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
mArray.InsertElementsAt(aIndex, aElements, aCount);
|
2013-02-05 02:05:40 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
// need to addref all these
|
|
|
|
for (uint32_t i = 0; i < aCount; ++i) {
|
|
|
|
NS_IF_ADDREF(aElements[i]);
|
|
|
|
}
|
2013-02-05 02:05:40 +04:00
|
|
|
}
|
|
|
|
|
2016-08-02 17:20:00 +03:00
|
|
|
void
|
2012-08-22 19:56:38 +04:00
|
|
|
nsCOMArray_base::ReplaceObjectAt(nsISupports* aObject, int32_t aIndex)
|
2005-11-02 19:04:35 +03:00
|
|
|
{
|
2013-04-02 00:42:54 +04:00
|
|
|
mArray.EnsureLengthAtLeast(aIndex + 1);
|
2014-06-27 05:35:39 +04:00
|
|
|
nsISupports* oldObject = mArray[aIndex];
|
2013-04-02 00:42:54 +04:00
|
|
|
// Make sure to addref first, in case aObject == oldObject
|
|
|
|
NS_IF_ADDREF(mArray[aIndex] = aObject);
|
|
|
|
NS_IF_RELEASE(oldObject);
|
2005-11-02 19:04:34 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2014-06-27 05:35:39 +04:00
|
|
|
nsCOMArray_base::RemoveObject(nsISupports* aObject)
|
2005-11-02 19:04:35 +03:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
bool result = mArray.RemoveElement(aObject);
|
|
|
|
if (result) {
|
|
|
|
NS_IF_RELEASE(aObject);
|
|
|
|
}
|
|
|
|
return result;
|
2005-11-02 19:04:34 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2012-08-22 19:56:38 +04:00
|
|
|
nsCOMArray_base::RemoveObjectAt(int32_t aIndex)
|
2005-11-02 19:04:35 +03:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
if (uint32_t(aIndex) < mArray.Length()) {
|
|
|
|
nsISupports* element = mArray[aIndex];
|
2006-03-01 04:10:20 +03:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
mArray.RemoveElementAt(aIndex);
|
|
|
|
NS_IF_RELEASE(element);
|
|
|
|
return true;
|
|
|
|
}
|
2006-03-01 04:10:20 +03:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
return false;
|
2005-11-02 19:04:34 +03:00
|
|
|
}
|
|
|
|
|
2013-02-05 02:05:40 +04:00
|
|
|
void
|
|
|
|
nsCOMArray_base::RemoveElementAt(uint32_t aIndex)
|
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
nsISupports* element = mArray[aIndex];
|
|
|
|
mArray.RemoveElementAt(aIndex);
|
|
|
|
NS_IF_RELEASE(element);
|
2013-02-05 02:05:40 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2012-08-22 19:56:38 +04:00
|
|
|
nsCOMArray_base::RemoveObjectsAt(int32_t aIndex, int32_t aCount)
|
2011-04-14 18:18:02 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04: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 18:18:02 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
return false;
|
2011-04-14 18:18:02 +04:00
|
|
|
}
|
|
|
|
|
2013-02-05 02:05:40 +04:00
|
|
|
void
|
|
|
|
nsCOMArray_base::RemoveElementsAt(uint32_t aIndex, uint32_t aCount)
|
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
nsTArray<nsISupports*> elementsToDestroy(aCount);
|
|
|
|
elementsToDestroy.AppendElements(mArray.Elements() + aIndex, aCount);
|
|
|
|
mArray.RemoveElementsAt(aIndex, aCount);
|
|
|
|
ReleaseObjects(elementsToDestroy);
|
2013-02-05 02:05:40 +04:00
|
|
|
}
|
|
|
|
|
2005-11-02 19:04:36 +03:00
|
|
|
// useful for destructors
|
2012-12-20 01:33:54 +04:00
|
|
|
void
|
2014-06-27 05:35:39 +04:00
|
|
|
ReleaseObjects(nsTArray<nsISupports*>& aArray)
|
2005-11-02 19:04:34 +03:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
for (uint32_t i = 0; i < aArray.Length(); ++i) {
|
|
|
|
NS_IF_RELEASE(aArray[i]);
|
|
|
|
}
|
2005-11-02 19:04:34 +03:00
|
|
|
}
|
2005-11-02 19:04:35 +03:00
|
|
|
|
2005-11-08 23:55:30 +03:00
|
|
|
void
|
2005-11-02 19:04:35 +03:00
|
|
|
nsCOMArray_base::Clear()
|
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
nsTArray<nsISupports*> objects;
|
|
|
|
objects.SwapElements(mArray);
|
|
|
|
ReleaseObjects(objects);
|
2005-11-02 19:04:35 +03:00
|
|
|
}
|
2005-11-02 19:04:36 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2012-08-22 19:56:38 +04:00
|
|
|
nsCOMArray_base::SetCount(int32_t aNewCount)
|
2010-03-15 14:38:50 +03:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
NS_ASSERTION(aNewCount >= 0, "SetCount(negative index)");
|
|
|
|
if (aNewCount < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t count = mArray.Length();
|
|
|
|
if (count > aNewCount) {
|
|
|
|
RemoveObjectsAt(aNewCount, mArray.Length() - aNewCount);
|
|
|
|
}
|
|
|
|
mArray.SetLength(aNewCount);
|
|
|
|
return true;
|
2010-03-15 14:38:50 +03:00
|
|
|
}
|
|
|
|
|
2014-01-23 02:19:00 +04:00
|
|
|
void
|
|
|
|
nsCOMArray_base::Adopt(nsISupports** aElements, uint32_t aSize)
|
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
Clear();
|
|
|
|
mArray.AppendElements(aElements, aSize);
|
2014-01-23 02:19:00 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
// Free the allocated array as well.
|
|
|
|
NS_Free(aElements);
|
2014-01-23 02:19:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
2014-06-27 05:35:39 +04:00
|
|
|
nsCOMArray_base::Forget(nsISupports*** aElements)
|
2014-01-23 02:19:00 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
uint32_t length = Length();
|
|
|
|
size_t array_size = sizeof(nsISupports*) * length;
|
|
|
|
nsISupports** array = static_cast<nsISupports**>(NS_Alloc(array_size));
|
|
|
|
memmove(array, Elements(), array_size);
|
|
|
|
*aElements = array;
|
|
|
|
// Don't Release the contained pointers; the caller of the method will
|
|
|
|
// do this eventually.
|
|
|
|
mArray.Clear();
|
|
|
|
|
|
|
|
return length;
|
2014-01-23 02:19:00 +04:00
|
|
|
}
|