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