2014-06-30 19:39:45 +04: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/. */
|
2006-08-08 21:08:31 +04:00
|
|
|
|
2011-11-21 10:21:16 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
|
2006-08-08 21:08:31 +04:00
|
|
|
#include "nsArrayEnumerator.h"
|
|
|
|
|
2006-08-08 22:18:50 +04:00
|
|
|
#include "nsIArray.h"
|
2018-08-19 00:22:47 +03:00
|
|
|
#include "nsSimpleEnumerator.h"
|
2006-08-08 22:18:50 +04:00
|
|
|
|
|
|
|
#include "nsCOMArray.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2018-02-22 23:38:15 +03:00
|
|
|
#include "mozilla/OperatorNewExtensions.h"
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2006-08-08 22:18:50 +04:00
|
|
|
|
2018-08-19 00:22:47 +03:00
|
|
|
class nsSimpleArrayEnumerator final : public nsSimpleEnumerator {
|
2006-08-08 22:18:50 +04:00
|
|
|
public:
|
2014-06-27 05:35:39 +04:00
|
|
|
// nsISimpleEnumerator interface
|
|
|
|
NS_DECL_NSISIMPLEENUMERATOR
|
2006-08-08 22:18:50 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
// nsSimpleArrayEnumerator methods
|
2018-08-19 07:06:32 +03:00
|
|
|
explicit nsSimpleArrayEnumerator(nsIArray* aValueArray, const nsID& aEntryIID)
|
2014-06-27 05:35:39 +04:00
|
|
|
: mValueArray(aValueArray), mEntryIID(aEntryIID), mIndex(0) {}
|
2006-08-08 22:18:50 +04:00
|
|
|
|
2018-08-19 07:06:32 +03:00
|
|
|
const nsID& DefaultInterface() override { return mEntryIID; }
|
|
|
|
|
2006-08-08 22:18:50 +04:00
|
|
|
private:
|
2018-08-19 00:22:47 +03:00
|
|
|
~nsSimpleArrayEnumerator() override = default;
|
2006-08-08 22:18:50 +04:00
|
|
|
|
|
|
|
protected:
|
2014-06-27 05:35:39 +04:00
|
|
|
nsCOMPtr<nsIArray> mValueArray;
|
2018-08-19 07:06:32 +03:00
|
|
|
const nsID mEntryIID;
|
2014-06-27 05:35:39 +04:00
|
|
|
uint32_t mIndex;
|
2006-08-08 22:18:50 +04:00
|
|
|
};
|
|
|
|
|
2006-08-08 21:08:31 +04:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsSimpleArrayEnumerator::HasMoreElements(bool* aResult) {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(aResult != 0, "null ptr");
|
2014-06-27 05:35:39 +04:00
|
|
|
if (!aResult) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mValueArray) {
|
|
|
|
*aResult = false;
|
2006-08-08 21:08:31 +04:00
|
|
|
return NS_OK;
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t cnt;
|
|
|
|
nsresult rv = mValueArray->GetLength(&cnt);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
*aResult = (mIndex < cnt);
|
|
|
|
return NS_OK;
|
2006-08-08 21:08:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimpleArrayEnumerator::GetNext(nsISupports** aResult) {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(aResult != 0, "null ptr");
|
2014-06-27 05:35:39 +04:00
|
|
|
if (!aResult) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mValueArray) {
|
|
|
|
*aResult = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t cnt;
|
|
|
|
nsresult rv = mValueArray->GetLength(&cnt);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
if (mIndex >= cnt) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mValueArray->QueryElementAt(mIndex++, NS_GET_IID(nsISupports),
|
|
|
|
(void**)aResult);
|
2006-08-08 21:08:31 +04:00
|
|
|
}
|
|
|
|
|
2018-08-19 07:06:32 +03:00
|
|
|
nsresult NS_NewArrayEnumerator(nsISimpleEnumerator** aResult, nsIArray* aArray,
|
|
|
|
const nsID& aEntryIID) {
|
|
|
|
RefPtr<nsSimpleArrayEnumerator> enumer =
|
|
|
|
new nsSimpleArrayEnumerator(aArray, aEntryIID);
|
2015-07-01 19:27:43 +03:00
|
|
|
enumer.forget(aResult);
|
2014-06-27 05:35:39 +04:00
|
|
|
return NS_OK;
|
2006-08-08 21:08:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// enumerator implementation for nsCOMArray
|
|
|
|
// creates a snapshot of the array in question
|
|
|
|
// you MUST use NS_NewArrayEnumerator to create this, so that
|
|
|
|
// allocation is done correctly
|
2018-08-19 00:22:47 +03:00
|
|
|
class nsCOMArrayEnumerator final : public nsSimpleEnumerator {
|
2006-08-08 21:08:31 +04:00
|
|
|
public:
|
2014-06-27 05:35:39 +04:00
|
|
|
// nsISimpleEnumerator interface
|
|
|
|
NS_DECL_NSISIMPLEENUMERATOR
|
2006-08-08 21:08:31 +04:00
|
|
|
|
2018-02-22 23:38:15 +03:00
|
|
|
// Use this instead of `new`.
|
2018-08-19 07:06:32 +03:00
|
|
|
static nsCOMArrayEnumerator* Allocate(const nsCOMArray_base& aArray,
|
|
|
|
const nsID& aEntryIID);
|
2006-08-08 21:08:31 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
// specialized operator to make sure we make room for mValues
|
2018-02-22 23:38:15 +03:00
|
|
|
void operator delete(void* aPtr) { free(aPtr); }
|
2006-08-08 21:08:31 +04:00
|
|
|
|
2018-08-19 07:06:32 +03:00
|
|
|
const nsID& DefaultInterface() override { return mEntryIID; }
|
|
|
|
|
2006-08-08 21:08:49 +04:00
|
|
|
private:
|
2018-02-22 23:38:15 +03:00
|
|
|
// nsSimpleArrayEnumerator methods
|
2018-08-19 07:06:32 +03:00
|
|
|
explicit nsCOMArrayEnumerator(const nsID& aEntryIID)
|
|
|
|
: mIndex(0), mArraySize(0), mEntryIID(aEntryIID) {
|
2018-02-22 23:38:15 +03:00
|
|
|
mValueArray[0] = nullptr;
|
|
|
|
}
|
|
|
|
|
2018-08-19 00:22:47 +03:00
|
|
|
~nsCOMArrayEnumerator(void) override;
|
2006-08-08 21:08:49 +04:00
|
|
|
|
2006-08-08 21:08:31 +04:00
|
|
|
protected:
|
2014-06-27 05:35:39 +04:00
|
|
|
uint32_t mIndex; // current position
|
|
|
|
uint32_t mArraySize; // size of the array
|
|
|
|
|
2018-08-19 07:06:32 +03:00
|
|
|
const nsID& mEntryIID;
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
// this is actually bigger
|
|
|
|
nsISupports* mValueArray[1];
|
2006-08-08 21:08:31 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
nsCOMArrayEnumerator::~nsCOMArrayEnumerator() {
|
2014-06-27 05:35:39 +04:00
|
|
|
// only release the entries that we haven't visited yet
|
|
|
|
for (; mIndex < mArraySize; ++mIndex) {
|
|
|
|
NS_IF_RELEASE(mValueArray[mIndex]);
|
|
|
|
}
|
2006-08-08 21:08:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsCOMArrayEnumerator::HasMoreElements(bool* aResult) {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(aResult != 0, "null ptr");
|
2014-06-27 05:35:39 +04:00
|
|
|
if (!aResult) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
2006-08-08 21:08:31 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
*aResult = (mIndex < mArraySize);
|
|
|
|
return NS_OK;
|
2006-08-08 21:08:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCOMArrayEnumerator::GetNext(nsISupports** aResult) {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(aResult != 0, "null ptr");
|
2014-06-27 05:35:39 +04:00
|
|
|
if (!aResult) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mIndex >= mArraySize) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// pass the ownership of the reference to the caller. Since
|
|
|
|
// we AddRef'ed during creation of |this|, there is no need
|
|
|
|
// to AddRef here
|
|
|
|
*aResult = mValueArray[mIndex++];
|
|
|
|
|
|
|
|
// this really isn't necessary. just pretend this happens, since
|
|
|
|
// we'll never visit this value again!
|
|
|
|
// mValueArray[(mIndex-1)] = nullptr;
|
|
|
|
|
|
|
|
return NS_OK;
|
2006-08-08 21:08:31 +04:00
|
|
|
}
|
|
|
|
|
2018-08-19 07:06:32 +03:00
|
|
|
nsCOMArrayEnumerator* nsCOMArrayEnumerator::Allocate(
|
|
|
|
const nsCOMArray_base& aArray, const nsID& aEntryIID) {
|
2014-06-27 05:35:39 +04:00
|
|
|
// create enough space such that mValueArray points to a large
|
|
|
|
// enough value. Note that the initial value of aSize gives us
|
|
|
|
// space for mValueArray[0], so we must subtract
|
2018-02-22 23:38:15 +03:00
|
|
|
size_t size = sizeof(nsCOMArrayEnumerator);
|
|
|
|
uint32_t count;
|
|
|
|
if (aArray.Count() > 0) {
|
|
|
|
count = static_cast<uint32_t>(aArray.Count());
|
|
|
|
size += (count - 1) * sizeof(aArray[0]);
|
|
|
|
} else {
|
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate a buffer large enough to contain our object and its array.
|
|
|
|
void* mem = moz_xmalloc(size);
|
2018-08-19 07:06:32 +03:00
|
|
|
auto result =
|
|
|
|
new (mozilla::KnownNotNull, mem) nsCOMArrayEnumerator(aEntryIID);
|
2014-06-27 05:35:39 +04:00
|
|
|
|
2018-02-22 23:38:15 +03:00
|
|
|
result->mArraySize = count;
|
2014-06-27 05:35:39 +04:00
|
|
|
|
|
|
|
// now need to copy over the values, and addref each one
|
|
|
|
// now this might seem like a lot of work, but we're actually just
|
|
|
|
// doing all our AddRef's ahead of time since GetNext() doesn't
|
|
|
|
// need to AddRef() on the way out
|
2018-02-22 23:38:15 +03:00
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
2014-06-27 05:35:39 +04:00
|
|
|
result->mValueArray[i] = aArray[i];
|
|
|
|
NS_IF_ADDREF(result->mValueArray[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2006-08-08 21:08:31 +04:00
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
nsresult NS_NewArrayEnumerator(nsISimpleEnumerator** aResult,
|
2018-08-19 07:06:32 +03:00
|
|
|
const nsCOMArray_base& aArray,
|
|
|
|
const nsID& aEntryIID) {
|
|
|
|
RefPtr<nsCOMArrayEnumerator> enumerator =
|
|
|
|
nsCOMArrayEnumerator::Allocate(aArray, aEntryIID);
|
2015-07-01 19:27:43 +03:00
|
|
|
enumerator.forget(aResult);
|
2014-06-27 05:35:39 +04:00
|
|
|
return NS_OK;
|
2006-08-08 21:08:31 +04:00
|
|
|
}
|