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/. */
|
1998-05-22 21:56:30 +00:00
|
|
|
|
2016-10-07 16:55:32 +00:00
|
|
|
#include <stdint.h>
|
2001-07-31 19:05:34 +00:00
|
|
|
#include <string.h>
|
2016-10-18 18:36:41 +00:00
|
|
|
|
2016-10-18 18:52:08 +00:00
|
|
|
#include "nsArrayEnumerator.h"
|
2016-10-14 12:58:33 +00:00
|
|
|
#include "nsIObjectInputStream.h"
|
|
|
|
#include "nsIObjectOutputStream.h"
|
2016-10-18 18:36:41 +00:00
|
|
|
#include "nsSupportsArray.h"
|
|
|
|
#include "nsSupportsArrayEnumerator.h"
|
2001-08-07 06:23:40 +00:00
|
|
|
|
2016-11-10 21:15:33 +00:00
|
|
|
// Disable deprecation warnings generated by nsISupportsArray and associated
|
|
|
|
// classes.
|
|
|
|
#if defined(__GNUC__)
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
#pragma warning (push)
|
|
|
|
#pragma warning (disable : 4996)
|
|
|
|
#endif
|
|
|
|
|
2001-10-23 03:50:24 +00:00
|
|
|
nsresult
|
2014-07-09 15:15:21 +00:00
|
|
|
nsQueryElementAt::operator()(const nsIID& aIID, void** aResult) const
|
|
|
|
{
|
|
|
|
nsresult status =
|
|
|
|
mCollection ? mCollection->QueryElementAt(mIndex, aIID, aResult) :
|
|
|
|
NS_ERROR_NULL_POINTER;
|
2001-07-04 05:42:46 +00:00
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
if (mErrorPtr) {
|
|
|
|
*mErrorPtr = status;
|
2001-07-04 05:42:46 +00:00
|
|
|
}
|
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
1999-02-23 08:10:24 +00:00
|
|
|
nsSupportsArray::nsSupportsArray()
|
1998-05-22 21:56:30 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
1999-02-23 08:10:24 +00:00
|
|
|
nsSupportsArray::~nsSupportsArray()
|
1998-05-22 21:56:30 +00:00
|
|
|
{
|
2016-10-18 18:36:41 +00:00
|
|
|
Clear();
|
2001-08-07 06:23:40 +00:00
|
|
|
}
|
|
|
|
|
2010-06-10 18:11:11 +00:00
|
|
|
nsresult
|
2014-07-09 15:15:21 +00:00
|
|
|
nsSupportsArray::Create(nsISupports* aOuter, REFNSIID aIID, void** aResult)
|
1999-05-26 01:38:36 +00:00
|
|
|
{
|
2014-07-09 15:15:21 +00:00
|
|
|
if (aOuter) {
|
1999-05-26 01:38:36 +00:00
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
2014-07-09 15:15:21 +00:00
|
|
|
}
|
1999-05-26 01:38:36 +00:00
|
|
|
|
2001-08-21 08:51:19 +00:00
|
|
|
nsCOMPtr<nsISupportsArray> it = new nsSupportsArray();
|
1999-05-26 01:38:36 +00:00
|
|
|
|
2001-08-21 08:51:19 +00:00
|
|
|
return it->QueryInterface(aIID, aResult);
|
1999-05-26 01:38:36 +00:00
|
|
|
}
|
|
|
|
|
2016-10-18 18:52:08 +00:00
|
|
|
NS_IMPL_ISUPPORTS(nsSupportsArray, nsIArray, nsISupportsArray, nsICollection,
|
2014-07-09 15:15:21 +00:00
|
|
|
nsISerializable)
|
2008-02-21 20:39:20 +00:00
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
NS_IMETHODIMP
|
2014-07-09 15:15:21 +00:00
|
|
|
nsSupportsArray::Read(nsIObjectInputStream* aStream)
|
2001-07-31 19:05:34 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t newArraySize;
|
2001-07-31 19:05:34 +00:00
|
|
|
rv = aStream->Read32(&newArraySize);
|
2015-08-13 21:24:59 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2001-07-31 19:05:34 +00:00
|
|
|
|
2016-10-18 18:36:41 +00:00
|
|
|
uint32_t count;
|
|
|
|
rv = aStream->Read32(&count);
|
2016-10-14 12:58:33 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
2014-07-09 15:15:21 +00:00
|
|
|
}
|
2004-02-25 23:38:40 +00:00
|
|
|
|
2016-10-18 18:36:41 +00:00
|
|
|
NS_ASSERTION(count <= newArraySize, "overlarge mCount!");
|
|
|
|
if (count > newArraySize) {
|
|
|
|
count = newArraySize;
|
2016-10-14 05:04:42 +00:00
|
|
|
}
|
|
|
|
|
2016-10-18 18:36:41 +00:00
|
|
|
// Don't clear out our array until we know we have enough space for the new
|
|
|
|
// one and have successfully copied everything out of the stream.
|
|
|
|
nsCOMArray<nsISupports> tmp;
|
|
|
|
tmp.SetCapacity(newArraySize);
|
|
|
|
tmp.SetCount(count);
|
|
|
|
|
|
|
|
auto elems = tmp.Elements();
|
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
|
|
|
rv = aStream->ReadObject(true, &elems[i]);
|
2014-07-09 15:15:21 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2001-07-31 19:05:34 +00:00
|
|
|
}
|
|
|
|
|
2016-10-18 18:36:41 +00:00
|
|
|
// Now clear out existing refs and replace with the new array.
|
|
|
|
mArray.Clear();
|
|
|
|
mArray.SwapElements(tmp);
|
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-07-09 15:15:21 +00:00
|
|
|
nsSupportsArray::Write(nsIObjectOutputStream* aStream)
|
2001-07-31 19:05:34 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2016-10-18 18:36:41 +00:00
|
|
|
rv = aStream->Write32(mArray.Capacity());
|
2014-07-09 15:15:21 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2001-07-31 19:05:34 +00:00
|
|
|
|
2016-10-18 18:36:41 +00:00
|
|
|
rv = aStream->Write32(mArray.Length());
|
2014-07-09 15:15:21 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2001-07-31 19:05:34 +00:00
|
|
|
|
2016-10-18 18:36:41 +00:00
|
|
|
for (size_t i = 0; i < mArray.Length(); i++) {
|
2016-10-14 12:58:33 +00:00
|
|
|
rv = aStream->WriteObject(mArray[i], true);
|
2014-07-09 15:15:21 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2001-07-31 19:05:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1998-05-22 21:56:30 +00:00
|
|
|
|
2012-12-11 16:16:42 +00:00
|
|
|
NS_IMETHODIMP
|
2014-07-09 15:15:21 +00:00
|
|
|
nsSupportsArray::GetElementAt(uint32_t aIndex, nsISupports** aOutPtr)
|
1998-05-22 21:56:30 +00:00
|
|
|
{
|
2016-10-18 18:36:41 +00:00
|
|
|
nsCOMPtr<nsISupports> elm = mArray.SafeElementAt(aIndex);
|
|
|
|
elm.forget(aOutPtr);
|
2012-12-11 16:16:42 +00:00
|
|
|
return NS_OK;
|
1998-05-22 21:56:30 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
NS_IMETHODIMP_(int32_t)
|
1999-06-02 02:06:32 +00:00
|
|
|
nsSupportsArray::IndexOf(const nsISupports* aPossibleElement)
|
|
|
|
{
|
2016-10-18 18:36:41 +00:00
|
|
|
// nsCOMArray takes a non-const param, but it just passes through to
|
|
|
|
// nsTArray which takes a const param.
|
|
|
|
return mArray.IndexOf(const_cast<nsISupports*>(aPossibleElement));
|
1998-05-22 21:56:30 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
NS_IMETHODIMP_(bool)
|
2012-08-22 15:56:38 +00:00
|
|
|
nsSupportsArray::InsertElementAt(nsISupports* aElement, uint32_t aIndex)
|
1998-05-22 21:56:30 +00:00
|
|
|
{
|
2016-10-18 18:36:41 +00:00
|
|
|
return mArray.InsertObjectAt(aElement, aIndex);
|
2001-08-07 06:23:40 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
NS_IMETHODIMP_(bool)
|
2012-08-22 15:56:38 +00:00
|
|
|
nsSupportsArray::ReplaceElementAt(nsISupports* aElement, uint32_t aIndex)
|
1998-05-22 21:56:30 +00:00
|
|
|
{
|
2016-10-18 18:36:41 +00:00
|
|
|
// nsCOMArray::ReplaceObjectAt will grow the array if necessary. Instead
|
|
|
|
// we do the bounds check and only replace if it's in range.
|
|
|
|
if (aIndex < mArray.Length()) {
|
|
|
|
mArray.ReplaceElementAt(aIndex, aElement);
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
1998-05-22 21:56:30 +00:00
|
|
|
}
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
1998-05-22 21:56:30 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
NS_IMETHODIMP_(bool)
|
2016-10-18 18:36:37 +00:00
|
|
|
nsSupportsArray::RemoveElementAt(uint32_t aIndex)
|
1998-05-22 21:56:30 +00:00
|
|
|
{
|
2016-10-18 18:36:41 +00:00
|
|
|
return mArray.RemoveObjectAt(aIndex);
|
1998-05-22 21:56:30 +00:00
|
|
|
}
|
|
|
|
|
2015-01-02 06:09:28 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSupportsArray::RemoveElement(nsISupports* aElement)
|
1998-05-22 21:56:30 +00:00
|
|
|
{
|
2016-10-18 18:36:41 +00:00
|
|
|
return mArray.RemoveObject(aElement) ? NS_OK : NS_ERROR_FAILURE;
|
1998-05-22 21:56:30 +00:00
|
|
|
}
|
|
|
|
|
1999-02-23 08:10:24 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSupportsArray::Clear(void)
|
1998-05-22 21:56:30 +00:00
|
|
|
{
|
2016-10-18 18:36:41 +00:00
|
|
|
mArray.Clear();
|
1999-02-23 08:10:24 +00:00
|
|
|
return NS_OK;
|
1998-05-22 21:56:30 +00:00
|
|
|
}
|
|
|
|
|
1999-02-23 08:10:24 +00:00
|
|
|
NS_IMETHODIMP
|
2016-10-18 18:52:08 +00:00
|
|
|
nsSupportsArray::DeprecatedEnumerate(nsIEnumerator** aResult)
|
1999-02-23 08:10:24 +00:00
|
|
|
{
|
2016-10-18 18:36:41 +00:00
|
|
|
RefPtr<nsSupportsArrayEnumerator> e = new nsSupportsArrayEnumerator(this);
|
|
|
|
e.forget(aResult);
|
1999-02-23 08:10:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1998-05-22 21:56:30 +00:00
|
|
|
|
2000-06-09 07:51:13 +00:00
|
|
|
NS_IMETHODIMP
|
2012-12-02 08:54:55 +00:00
|
|
|
nsSupportsArray::Clone(nsISupportsArray** aResult)
|
2000-06-09 07:51:13 +00:00
|
|
|
{
|
2012-12-02 08:54:55 +00:00
|
|
|
nsCOMPtr<nsISupportsArray> newArray;
|
|
|
|
nsresult rv = NS_NewISupportsArray(getter_AddRefs(newArray));
|
2014-07-09 15:15:21 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2013-11-19 21:27:37 +00:00
|
|
|
return rv;
|
2014-07-09 15:15:21 +00:00
|
|
|
}
|
2012-12-02 08:54:55 +00:00
|
|
|
|
2016-10-18 18:36:41 +00:00
|
|
|
for (size_t i = 0; i < mArray.Length(); i++) {
|
|
|
|
// AppendElement does an odd cast of bool to nsresult, we just cast back
|
|
|
|
// here.
|
|
|
|
if (!(bool)newArray->AppendElement(mArray[i])) {
|
2012-12-10 10:27:14 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
2012-12-02 08:54:55 +00:00
|
|
|
|
|
|
|
newArray.forget(aResult);
|
2000-06-09 07:51:13 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-08-18 13:46:39 +00:00
|
|
|
nsresult
|
1999-02-23 08:10:24 +00:00
|
|
|
NS_NewISupportsArray(nsISupportsArray** aInstancePtrResult)
|
1998-05-22 21:56:30 +00:00
|
|
|
{
|
1999-05-26 01:38:36 +00:00
|
|
|
nsresult rv;
|
2013-10-10 20:41:00 +00:00
|
|
|
rv = nsSupportsArray::Create(nullptr, NS_GET_IID(nsISupportsArray),
|
1999-05-26 01:38:36 +00:00
|
|
|
(void**)aInstancePtrResult);
|
|
|
|
return rv;
|
1998-05-22 21:56:30 +00:00
|
|
|
}
|
2016-10-18 18:52:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* nsIArray adapters.
|
|
|
|
*/
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSupportsArray::GetLength(uint32_t* aLength) {
|
|
|
|
return Count(aLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSupportsArray::QueryElementAt(uint32_t aIndex, const nsIID& aIID, void** aResult)
|
|
|
|
{
|
|
|
|
nsISupports* element = mArray.SafeElementAt(aIndex);
|
|
|
|
if (element) {
|
|
|
|
return element->QueryInterface(aIID, aResult);
|
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSupportsArray::IndexOf(uint32_t aStartIndex, nsISupports* aElement, uint32_t* aResult)
|
|
|
|
{
|
|
|
|
int32_t idx = mArray.IndexOf(aElement, aStartIndex);
|
|
|
|
if (idx < 0) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = static_cast<uint32_t>(idx);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSupportsArray::Enumerate(nsISimpleEnumerator** aResult)
|
|
|
|
{
|
|
|
|
return NS_NewArrayEnumerator(aResult, this);
|
|
|
|
}
|
2016-11-10 21:15:33 +00:00
|
|
|
|
|
|
|
#if defined(__GNUC__)
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
#pragma warning (pop)
|
|
|
|
#endif
|
|
|
|
|