2002-09-27 10:08:49 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
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/. */
|
2002-09-27 10:08:49 +04:00
|
|
|
|
|
|
|
#include "nsArray.h"
|
2002-10-01 21:34:25 +04:00
|
|
|
#include "nsArrayEnumerator.h"
|
2013-09-19 23:31:21 +04:00
|
|
|
#include "nsIWeakReference.h"
|
|
|
|
#include "nsIWeakReferenceUtils.h"
|
2009-01-16 07:02:22 +03:00
|
|
|
#include "nsThreadUtils.h"
|
2002-09-27 10:08:49 +04:00
|
|
|
|
|
|
|
// used by IndexOf()
|
|
|
|
struct findIndexOfClosure
|
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
nsISupports* targetElement;
|
|
|
|
uint32_t startIndex;
|
|
|
|
uint32_t resultIndex;
|
2002-09-27 10:08:49 +04:00
|
|
|
};
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool FindElementCallback(void* aElement, void* aClosure);
|
2002-10-01 03:02:31 +04:00
|
|
|
|
2009-01-16 07:02:22 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN(nsArray)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIArray)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIMutableArray)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIMutableArray)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsArrayCC)
|
2008-02-21 23:39:20 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIArray)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIMutableArray)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIMutableArray)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2014-05-15 20:52:58 +04:00
|
|
|
nsArrayBase::~nsArrayBase()
|
2002-09-27 10:08:49 +04:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
Clear();
|
2002-09-27 10:08:49 +04:00
|
|
|
}
|
2008-02-07 14:37:18 +03:00
|
|
|
|
|
|
|
|
2009-01-16 07:02:22 +03:00
|
|
|
NS_IMPL_ADDREF(nsArray)
|
|
|
|
NS_IMPL_RELEASE(nsArray)
|
|
|
|
|
2013-08-02 05:29:05 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsArrayCC)
|
|
|
|
|
2009-01-16 07:02:22 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsArrayCC)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsArrayCC)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsArrayCC)
|
2014-07-09 19:15:21 +04:00
|
|
|
tmp->Clear();
|
2008-02-07 14:37:18 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
2009-01-16 07:02:22 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsArrayCC)
|
2014-07-09 19:15:21 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mArray)
|
2008-02-07 14:37:18 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2002-09-27 10:08:49 +04:00
|
|
|
NS_IMETHODIMP
|
2014-05-15 20:52:58 +04:00
|
|
|
nsArrayBase::GetLength(uint32_t* aLength)
|
2002-09-27 10:08:49 +04:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
*aLength = mArray.Count();
|
|
|
|
return NS_OK;
|
2002-09-27 10:08:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 20:52:58 +04:00
|
|
|
nsArrayBase::QueryElementAt(uint32_t aIndex,
|
|
|
|
const nsIID& aIID,
|
|
|
|
void** aResult)
|
2002-09-27 10:08:49 +04:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
nsISupports* obj = mArray.SafeObjectAt(aIndex);
|
|
|
|
if (!obj) {
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// no need to worry about a leak here, because SafeObjectAt()
|
|
|
|
// doesn't addref its result
|
|
|
|
return obj->QueryInterface(aIID, aResult);
|
2002-09-27 10:08:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 20:52:58 +04:00
|
|
|
nsArrayBase::IndexOf(uint32_t aStartIndex, nsISupports* aElement,
|
|
|
|
uint32_t* aResult)
|
2002-09-27 10:08:49 +04:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
// optimize for the common case by forwarding to mArray
|
|
|
|
if (aStartIndex == 0) {
|
|
|
|
uint32_t idx = mArray.IndexOf(aElement);
|
|
|
|
if (idx == UINT32_MAX) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2002-09-27 10:08:49 +04:00
|
|
|
}
|
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
*aResult = idx;
|
2002-09-27 10:08:49 +04:00
|
|
|
return NS_OK;
|
2014-07-09 19:15:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
findIndexOfClosure closure = { aElement, aStartIndex, 0 };
|
|
|
|
bool notFound = mArray.EnumerateForwards(FindElementCallback, &closure);
|
|
|
|
if (notFound) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = closure.resultIndex;
|
|
|
|
return NS_OK;
|
2002-09-27 10:08:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 20:52:58 +04:00
|
|
|
nsArrayBase::Enumerate(nsISimpleEnumerator** aResult)
|
2002-09-27 10:08:49 +04:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
return NS_NewArrayEnumerator(aResult, static_cast<nsIArray*>(this));
|
2002-09-27 10:08:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// nsIMutableArray implementation
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 20:52:58 +04:00
|
|
|
nsArrayBase::AppendElement(nsISupports* aElement, bool aWeak)
|
2002-09-27 10:08:49 +04:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
bool result;
|
|
|
|
if (aWeak) {
|
|
|
|
nsCOMPtr<nsIWeakReference> elementRef = do_GetWeakReference(aElement);
|
|
|
|
NS_ASSERTION(elementRef,
|
|
|
|
"AppendElement: Trying to use weak references on an object that doesn't support it");
|
|
|
|
if (!elementRef) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2002-10-07 23:03:41 +04:00
|
|
|
}
|
2014-07-09 19:15:21 +04:00
|
|
|
result = mArray.AppendObject(elementRef);
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
// add the object directly
|
|
|
|
result = mArray.AppendObject(aElement);
|
|
|
|
}
|
|
|
|
return result ? NS_OK : NS_ERROR_FAILURE;
|
2002-09-27 10:08:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 20:52:58 +04:00
|
|
|
nsArrayBase::RemoveElementAt(uint32_t aIndex)
|
2002-09-27 10:08:49 +04:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
bool result = mArray.RemoveObjectAt(aIndex);
|
|
|
|
return result ? NS_OK : NS_ERROR_FAILURE;
|
2002-09-27 10:08:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 20:52:58 +04:00
|
|
|
nsArrayBase::InsertElementAt(nsISupports* aElement, uint32_t aIndex, bool aWeak)
|
2002-09-27 10:08:49 +04:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
nsCOMPtr<nsISupports> elementRef;
|
|
|
|
if (aWeak) {
|
|
|
|
elementRef = do_GetWeakReference(aElement);
|
|
|
|
NS_ASSERTION(elementRef,
|
|
|
|
"InsertElementAt: Trying to use weak references on an object that doesn't support it");
|
|
|
|
if (!elementRef) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2002-10-03 01:00:42 +04:00
|
|
|
}
|
2014-07-09 19:15:21 +04:00
|
|
|
} else {
|
|
|
|
elementRef = aElement;
|
|
|
|
}
|
|
|
|
bool result = mArray.InsertObjectAt(elementRef, aIndex);
|
|
|
|
return result ? NS_OK : NS_ERROR_FAILURE;
|
2002-09-27 10:08:49 +04:00
|
|
|
}
|
|
|
|
|
2005-01-01 20:57:00 +03:00
|
|
|
NS_IMETHODIMP
|
2014-05-15 20:52:58 +04:00
|
|
|
nsArrayBase::ReplaceElementAt(nsISupports* aElement, uint32_t aIndex, bool aWeak)
|
2005-01-01 20:57:00 +03:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
nsCOMPtr<nsISupports> elementRef;
|
|
|
|
if (aWeak) {
|
|
|
|
elementRef = do_GetWeakReference(aElement);
|
|
|
|
NS_ASSERTION(elementRef,
|
|
|
|
"ReplaceElementAt: Trying to use weak references on an object that doesn't support it");
|
|
|
|
if (!elementRef) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2005-01-01 20:57:00 +03:00
|
|
|
}
|
2014-07-09 19:15:21 +04:00
|
|
|
} else {
|
|
|
|
elementRef = aElement;
|
|
|
|
}
|
|
|
|
bool result = mArray.ReplaceObjectAt(elementRef, aIndex);
|
|
|
|
return result ? NS_OK : NS_ERROR_FAILURE;
|
2005-01-01 20:57:00 +03:00
|
|
|
}
|
|
|
|
|
2002-09-27 10:08:49 +04:00
|
|
|
NS_IMETHODIMP
|
2014-05-15 20:52:58 +04:00
|
|
|
nsArrayBase::Clear()
|
2002-09-27 10:08:49 +04:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
mArray.Clear();
|
|
|
|
return NS_OK;
|
2002-09-27 10:08:49 +04:00
|
|
|
}
|
|
|
|
|
2002-10-04 00:20:13 +04:00
|
|
|
//
|
|
|
|
// static helper routines
|
|
|
|
//
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2014-07-09 19:15:21 +04:00
|
|
|
FindElementCallback(void* aElement, void* aClosure)
|
2002-09-27 10:08:49 +04:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
findIndexOfClosure* closure = static_cast<findIndexOfClosure*>(aClosure);
|
|
|
|
nsISupports* element = static_cast<nsISupports*>(aElement);
|
2002-09-27 10:08:49 +04:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
// don't start searching until we're past the startIndex
|
|
|
|
if (closure->resultIndex >= closure->startIndex &&
|
|
|
|
element == closure->targetElement) {
|
|
|
|
return false; // stop! We found it
|
|
|
|
}
|
|
|
|
closure->resultIndex++;
|
2014-05-15 20:52:58 +04:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
return true;
|
2002-09-27 10:08:49 +04:00
|
|
|
}
|
2009-01-16 07:02:22 +03:00
|
|
|
|
2010-06-10 22:11:11 +04:00
|
|
|
nsresult
|
2014-07-09 19:15:21 +04:00
|
|
|
nsArrayBase::XPCOMConstructor(nsISupports* aOuter, const nsIID& aIID,
|
|
|
|
void** aResult)
|
2010-06-10 22:11:11 +04:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
if (aOuter) {
|
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
|
|
|
}
|
2009-01-16 07:02:22 +03:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
nsCOMPtr<nsIMutableArray> inst = Create();
|
|
|
|
return inst->QueryInterface(aIID, aResult);
|
2009-01-16 07:02:22 +03:00
|
|
|
}
|
2012-09-21 20:12:32 +04:00
|
|
|
|
|
|
|
already_AddRefed<nsIMutableArray>
|
2014-05-15 20:52:58 +04:00
|
|
|
nsArrayBase::Create()
|
2012-09-21 20:12:32 +04:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
nsCOMPtr<nsIMutableArray> inst;
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
inst = new nsArrayCC;
|
|
|
|
} else {
|
|
|
|
inst = new nsArray;
|
|
|
|
}
|
|
|
|
return inst.forget();
|
2012-09-21 20:12:32 +04:00
|
|
|
}
|