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