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 "nsArray.h"
|
|
|
|
#include "nsArrayEnumerator.h"
|
|
|
|
#include "nsWeakReference.h"
|
2009-01-15 20:02:22 -08:00
|
|
|
#include "nsThreadUtils.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// used by IndexOf()
|
|
|
|
struct findIndexOfClosure
|
|
|
|
{
|
|
|
|
nsISupports *targetElement;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t startIndex;
|
|
|
|
uint32_t resultIndex;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool FindElementCallback(void* aElement, void* aClosure);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-01-15 20:02:22 -08: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 12:39:20 -08:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIArray)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIMutableArray)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIMutableArray)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsArray::~nsArray()
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
}
|
2008-02-07 03:37:18 -08:00
|
|
|
|
|
|
|
|
2009-01-15 20:02:22 -08:00
|
|
|
NS_IMPL_ADDREF(nsArray)
|
|
|
|
NS_IMPL_RELEASE(nsArray)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsArrayCC)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsArrayCC)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsArrayCC)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsArrayCC)
|
2008-02-07 03:37:18 -08:00
|
|
|
tmp->Clear();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
2009-01-15 20:02:22 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsArrayCC)
|
2008-02-07 03:37:18 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMARRAY(mArray)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsArray::GetLength(uint32_t* aLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
*aLength = mArray.Count();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsArray::QueryElementAt(uint32_t aIndex,
|
2007-03-22 10:30:00 -07:00
|
|
|
const nsIID& aIID,
|
|
|
|
void ** aResult)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsArray::IndexOf(uint32_t aStartIndex, nsISupports* aElement,
|
|
|
|
uint32_t* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// optimize for the common case by forwarding to mArray
|
|
|
|
if (aStartIndex == 0) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t idx = mArray.IndexOf(aElement);
|
2012-09-27 23:57:33 -07:00
|
|
|
if (idx == UINT32_MAX)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2008-12-11 12:13:52 -08:00
|
|
|
|
|
|
|
*aResult = idx;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
findIndexOfClosure closure = { aElement, aStartIndex, 0 };
|
2011-09-28 23:19:26 -07:00
|
|
|
bool notFound = mArray.EnumerateForwards(FindElementCallback, &closure);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (notFound)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
*aResult = closure.resultIndex;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsArray::Enumerate(nsISimpleEnumerator **aResult)
|
|
|
|
{
|
2007-07-08 00:08:04 -07:00
|
|
|
return NS_NewArrayEnumerator(aResult, static_cast<nsIArray*>(this));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// nsIMutableArray implementation
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsArray::AppendElement(nsISupports* aElement, bool aWeak)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool result;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aWeak) {
|
2012-10-03 02:38:46 -07:00
|
|
|
nsCOMPtr<nsIWeakReference> elementRef = do_GetWeakReference(aElement);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ASSERTION(elementRef, "AppendElement: Trying to use weak references on an object that doesn't support it");
|
|
|
|
if (!elementRef)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
result = mArray.AppendObject(elementRef);
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
// add the object directly
|
|
|
|
result = mArray.AppendObject(aElement);
|
|
|
|
}
|
|
|
|
return result ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsArray::RemoveElementAt(uint32_t aIndex)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool result = mArray.RemoveObjectAt(aIndex);
|
2007-03-22 10:30:00 -07:00
|
|
|
return result ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsArray::InsertElementAt(nsISupports* aElement, uint32_t aIndex, bool aWeak)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsISupports> elementRef;
|
|
|
|
if (aWeak) {
|
2012-10-03 02:38:46 -07:00
|
|
|
elementRef = do_GetWeakReference(aElement);
|
2007-03-22 10:30:00 -07: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-28 23:19:26 -07:00
|
|
|
bool result = mArray.InsertObjectAt(elementRef, aIndex);
|
2007-03-22 10:30:00 -07:00
|
|
|
return result ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsArray::ReplaceElementAt(nsISupports* aElement, uint32_t aIndex, bool aWeak)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsISupports> elementRef;
|
|
|
|
if (aWeak) {
|
2012-10-03 02:38:46 -07:00
|
|
|
elementRef = do_GetWeakReference(aElement);
|
2007-03-22 10:30:00 -07: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-28 23:19:26 -07:00
|
|
|
bool result = mArray.ReplaceObjectAt(elementRef, aIndex);
|
2007-03-22 10:30:00 -07:00
|
|
|
return result ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsArray::Clear()
|
|
|
|
{
|
|
|
|
mArray.Clear();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// static helper routines
|
|
|
|
//
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
FindElementCallback(void *aElement, void* aClosure)
|
|
|
|
{
|
|
|
|
findIndexOfClosure* closure =
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<findIndexOfClosure*>(aClosure);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsISupports* element =
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<nsISupports*>(aElement);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// don't start searching until we're past the startIndex
|
|
|
|
if (closure->resultIndex >= closure->startIndex &&
|
|
|
|
element == closure->targetElement) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false; // stop! We found it
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
closure->resultIndex++;
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-01-15 20:02:22 -08:00
|
|
|
|
2010-06-10 11:11:11 -07:00
|
|
|
nsresult
|
2012-09-21 09:12:32 -07:00
|
|
|
nsArray::XPCOMConstructor(nsISupports *aOuter, const nsIID& aIID, void **aResult)
|
2010-06-10 11:11:11 -07:00
|
|
|
{
|
2009-01-15 20:02:22 -08:00
|
|
|
if (aOuter)
|
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
|
|
|
|
2012-09-21 09:12:32 -07:00
|
|
|
nsCOMPtr<nsIMutableArray> inst = Create();
|
2009-01-15 20:02:22 -08:00
|
|
|
return inst->QueryInterface(aIID, aResult);
|
|
|
|
}
|
2012-09-21 09:12:32 -07:00
|
|
|
|
|
|
|
already_AddRefed<nsIMutableArray>
|
|
|
|
nsArray::Create()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIMutableArray> inst = NS_IsMainThread() ? new nsArrayCC : new nsArray;
|
|
|
|
return inst.forget();
|
|
|
|
}
|