2015-04-09 20:25:05 +03: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 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"
|
2009-01-16 07:02:22 +03:00
|
|
|
#include "nsThreadUtils.h"
|
2002-09-27 10:08:49 +04:00
|
|
|
|
2009-01-16 07:02:22 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN(nsArray)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIArray)
|
2016-10-24 23:24:26 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIArrayExtensions)
|
2009-01-16 07:02:22 +03: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 23:39:20 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIArray)
|
2016-10-24 23:24:26 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIArrayExtensions)
|
2008-02-21 23:39:20 +03:00
|
|
|
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() { 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)
|
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) {
|
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) {
|
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) {
|
2017-11-15 03:34:42 +03:00
|
|
|
int32_t idx = mArray.IndexOf(aElement, aStartIndex);
|
|
|
|
if (idx == -1) {
|
2014-07-09 19:15:21 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2017-11-15 03:34:42 +03:00
|
|
|
*aResult = static_cast<uint32_t>(idx);
|
2014-07-09 19:15:21 +04:00
|
|
|
return NS_OK;
|
2002-09-27 10:08:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2018-10-24 06:14:04 +03:00
|
|
|
nsArrayBase::ScriptedEnumerate(const nsIID& aElemIID, uint8_t aArgc,
|
2018-08-19 07:06:32 +03:00
|
|
|
nsISimpleEnumerator** aResult) {
|
2018-10-24 06:14:04 +03:00
|
|
|
if (aArgc > 0) {
|
|
|
|
return NS_NewArrayEnumerator(aResult, static_cast<nsIArray*>(this),
|
|
|
|
aElemIID);
|
2018-08-19 07:06:32 +03: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
|
|
|
}
|
|
|
|
|
2018-08-19 07:06:32 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsArrayBase::EnumerateImpl(const nsID& aElemIID,
|
|
|
|
nsISimpleEnumerator** aResult) {
|
|
|
|
return NS_NewArrayEnumerator(aResult, static_cast<nsIArray*>(this), aElemIID);
|
|
|
|
}
|
|
|
|
|
2002-09-27 10:08:49 +04:00
|
|
|
// nsIMutableArray implementation
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2017-10-21 17:53:02 +03:00
|
|
|
nsArrayBase::AppendElement(nsISupports* aElement) {
|
|
|
|
bool result = mArray.AppendObject(aElement);
|
2014-07-09 19:15:21 +04:00
|
|
|
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) {
|
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
|
2017-10-21 17:53:02 +03:00
|
|
|
nsArrayBase::InsertElementAt(nsISupports* aElement, uint32_t aIndex) {
|
|
|
|
bool result = mArray.InsertObjectAt(aElement, aIndex);
|
2014-07-09 19:15:21 +04:00
|
|
|
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
|
2017-10-21 17:53:02 +03:00
|
|
|
nsArrayBase::ReplaceElementAt(nsISupports* aElement, uint32_t aIndex) {
|
|
|
|
mArray.ReplaceObjectAt(aElement, aIndex);
|
2016-08-02 17:20:00 +03:00
|
|
|
return NS_OK;
|
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() {
|
2014-07-09 19:15:21 +04:00
|
|
|
mArray.Clear();
|
|
|
|
return NS_OK;
|
2002-09-27 10:08:49 +04:00
|
|
|
}
|
|
|
|
|
2016-10-24 23:24:26 +03: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;
|
|
|
|
}
|
|
|
|
|
2022-05-17 23:24:19 +03:00
|
|
|
nsresult nsArrayBase::XPCOMConstructor(const nsIID& aIID, void** aResult) {
|
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> nsArrayBase::Create() {
|
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
|
|
|
}
|