2015-04-09 17:25:05 +00: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 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"
|
2009-01-16 04:02:22 +00:00
|
|
|
#include "nsThreadUtils.h"
|
2002-09-27 06:08:49 +00:00
|
|
|
|
2009-01-16 04:02:22 +00:00
|
|
|
NS_INTERFACE_MAP_BEGIN(nsArray)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIArray)
|
2016-10-24 20:24:26 +00:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIArrayExtensions)
|
2009-01-16 04:02:22 +00:00
|
|
|
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)
|
2016-10-24 20:24:26 +00:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIArrayExtensions)
|
2008-02-21 20:39:20 +00:00
|
|
|
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
|
|
|
{
|
2014-07-09 15:15:21 +00:00
|
|
|
Clear();
|
2002-09-27 06:08:49 +00:00
|
|
|
}
|
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)
|
2014-07-09 15:15:21 +00:00
|
|
|
tmp->Clear();
|
2008-02-07 11:37:18 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
2009-01-16 04:02:22 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsArrayCC)
|
2014-07-09 15:15:21 +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
|
|
|
{
|
2014-07-09 15:15:21 +00:00
|
|
|
*aLength = mArray.Count();
|
|
|
|
return NS_OK;
|
2002-09-27 06:08:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2014-07-09 15:15:21 +00: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 06:08:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2017-11-15 00:34:42 +00:00
|
|
|
int32_t idx = mArray.IndexOf(aElement, aStartIndex);
|
|
|
|
if (idx == -1) {
|
2014-07-09 15:15:21 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2017-11-15 00:34:42 +00:00
|
|
|
*aResult = static_cast<uint32_t>(idx);
|
2014-07-09 15:15:21 +00:00
|
|
|
return NS_OK;
|
2002-09-27 06:08:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 16:52:58 +00:00
|
|
|
nsArrayBase::Enumerate(nsISimpleEnumerator** aResult)
|
2002-09-27 06:08:49 +00:00
|
|
|
{
|
2014-07-09 15:15:21 +00:00
|
|
|
return NS_NewArrayEnumerator(aResult, static_cast<nsIArray*>(this));
|
2002-09-27 06:08:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// nsIMutableArray implementation
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2017-10-21 14:53:02 +00:00
|
|
|
nsArrayBase::AppendElement(nsISupports* aElement)
|
2002-09-27 06:08:49 +00:00
|
|
|
{
|
2017-10-21 14:53:02 +00:00
|
|
|
bool result = mArray.AppendObject(aElement);
|
2014-07-09 15:15:21 +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::RemoveElementAt(uint32_t aIndex)
|
2002-09-27 06:08:49 +00:00
|
|
|
{
|
2014-07-09 15:15:21 +00:00
|
|
|
bool result = mArray.RemoveObjectAt(aIndex);
|
|
|
|
return result ? NS_OK : NS_ERROR_FAILURE;
|
2002-09-27 06:08:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2017-10-21 14:53:02 +00:00
|
|
|
nsArrayBase::InsertElementAt(nsISupports* aElement, uint32_t aIndex)
|
2002-09-27 06:08:49 +00:00
|
|
|
{
|
2017-10-21 14:53:02 +00:00
|
|
|
bool result = mArray.InsertObjectAt(aElement, aIndex);
|
2014-07-09 15:15:21 +00:00
|
|
|
return result ? NS_OK : NS_ERROR_FAILURE;
|
2002-09-27 06:08:49 +00:00
|
|
|
}
|
|
|
|
|
2005-01-01 17:57:00 +00:00
|
|
|
NS_IMETHODIMP
|
2017-10-21 14:53:02 +00:00
|
|
|
nsArrayBase::ReplaceElementAt(nsISupports* aElement, uint32_t aIndex)
|
2005-01-01 17:57:00 +00:00
|
|
|
{
|
2017-10-21 14:53:02 +00:00
|
|
|
mArray.ReplaceObjectAt(aElement, aIndex);
|
2016-08-02 14:20:00 +00:00
|
|
|
return NS_OK;
|
2005-01-01 17:57:00 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2014-07-09 15:15:21 +00:00
|
|
|
mArray.Clear();
|
|
|
|
return NS_OK;
|
2002-09-27 06:08:49 +00:00
|
|
|
}
|
|
|
|
|
2016-10-24 20:24:26 +00:00
|
|
|
// nsIArrayExtensions implementation.
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsArrayBase::Count(uint32_t* aResult)
|
|
|
|
{
|
|
|
|
return GetLength(aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsArrayBase::GetElementAt(uint32_t aIndex, nsISupports** aResult)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISupports> obj = mArray.SafeObjectAt(aIndex);
|
|
|
|
obj.forget(aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-10 18:11:11 +00:00
|
|
|
nsresult
|
2014-07-09 15:15:21 +00:00
|
|
|
nsArrayBase::XPCOMConstructor(nsISupports* aOuter, const nsIID& aIID,
|
|
|
|
void** aResult)
|
2010-06-10 18:11:11 +00:00
|
|
|
{
|
2014-07-09 15:15:21 +00:00
|
|
|
if (aOuter) {
|
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
|
|
|
}
|
2009-01-16 04:02:22 +00:00
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
nsCOMPtr<nsIMutableArray> inst = Create();
|
|
|
|
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-07-09 15:15:21 +00:00
|
|
|
nsCOMPtr<nsIMutableArray> inst;
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
inst = new nsArrayCC;
|
|
|
|
} else {
|
|
|
|
inst = new nsArray;
|
|
|
|
}
|
|
|
|
return inst.forget();
|
2012-09-21 16:12:32 +00:00
|
|
|
}
|