2014-06-30 08:39:45 -07: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 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
|
|
|
|
2011-11-20 22:21:16 -08:00
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsEnumeratorUtils.h"
|
|
|
|
|
|
|
|
#include "nsISimpleEnumerator.h"
|
|
|
|
#include "nsIStringEnumerator.h"
|
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
class EmptyEnumeratorImpl
|
|
|
|
: public nsISimpleEnumerator
|
|
|
|
, public nsIUTF8StringEnumerator
|
|
|
|
, public nsIStringEnumerator
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
2014-06-26 18:35:39 -07:00
|
|
|
EmptyEnumeratorImpl() {}
|
|
|
|
|
|
|
|
// nsISupports interface
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED // not really inherited, but no mRefCnt
|
|
|
|
|
|
|
|
// nsISimpleEnumerator
|
|
|
|
NS_DECL_NSISIMPLEENUMERATOR
|
|
|
|
NS_DECL_NSIUTF8STRINGENUMERATOR
|
|
|
|
// can't use NS_DECL_NSISTRINGENUMERATOR because they share the
|
|
|
|
// HasMore() signature
|
|
|
|
NS_IMETHOD GetNext(nsAString& aResult);
|
|
|
|
|
|
|
|
static EmptyEnumeratorImpl* GetInstance()
|
|
|
|
{
|
|
|
|
static const EmptyEnumeratorImpl kInstance;
|
|
|
|
return const_cast<EmptyEnumeratorImpl*>(&kInstance);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// nsISupports interface
|
2014-06-26 18:35:39 -07:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
|
|
|
EmptyEnumeratorImpl::AddRef(void)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
return 2;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
|
|
|
EmptyEnumeratorImpl::Release(void)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
return 1;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_QUERY_INTERFACE(EmptyEnumeratorImpl, nsISimpleEnumerator,
|
|
|
|
nsIUTF8StringEnumerator, nsIStringEnumerator)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// nsISimpleEnumerator interface
|
2014-06-26 18:35:39 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
EmptyEnumeratorImpl::HasMoreElements(bool* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
*aResult = false;
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
EmptyEnumeratorImpl::HasMore(bool* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
*aResult = false;
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
EmptyEnumeratorImpl::GetNext(nsISupports** aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
EmptyEnumeratorImpl::GetNext(nsACString& aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
EmptyEnumeratorImpl::GetNext(nsAString& aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
NS_NewEmptyEnumerator(nsISimpleEnumerator** aResult)
|
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
*aResult = EmptyEnumeratorImpl::GetInstance();
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-11-20 22:21:16 -08:00
|
|
|
class nsSingletonEnumerator MOZ_FINAL : public nsISimpleEnumerator
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
2014-06-26 18:35:39 -07:00
|
|
|
NS_DECL_ISUPPORTS
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
// nsISimpleEnumerator methods
|
|
|
|
NS_IMETHOD HasMoreElements(bool* aResult);
|
|
|
|
NS_IMETHOD GetNext(nsISupports** aResult);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-07-29 17:43:56 -07:00
|
|
|
explicit nsSingletonEnumerator(nsISupports* aValue);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
private:
|
2014-06-26 18:35:39 -07:00
|
|
|
~nsSingletonEnumerator();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
protected:
|
2014-06-26 18:35:39 -07:00
|
|
|
nsISupports* mValue;
|
|
|
|
bool mConsumed;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
nsSingletonEnumerator::nsSingletonEnumerator(nsISupports* aValue)
|
2014-06-26 18:35:39 -07:00
|
|
|
: mValue(aValue)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
NS_IF_ADDREF(mValue);
|
|
|
|
mConsumed = (mValue ? false : true);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsSingletonEnumerator::~nsSingletonEnumerator()
|
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
NS_IF_RELEASE(mValue);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(nsSingletonEnumerator, nsISimpleEnumerator)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsSingletonEnumerator::HasMoreElements(bool* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
NS_PRECONDITION(aResult != 0, "null ptr");
|
|
|
|
if (!aResult) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
*aResult = !mConsumed;
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSingletonEnumerator::GetNext(nsISupports** aResult)
|
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
NS_PRECONDITION(aResult != 0, "null ptr");
|
|
|
|
if (!aResult) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
if (mConsumed) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
mConsumed = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
*aResult = mValue;
|
|
|
|
NS_ADDREF(*aResult);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-06-26 18:35:39 -07:00
|
|
|
NS_NewSingletonEnumerator(nsISimpleEnumerator** aResult,
|
|
|
|
nsISupports* aSingleton)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
nsSingletonEnumerator* enumer = new nsSingletonEnumerator(aSingleton);
|
|
|
|
if (!enumer) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
*aResult = enumer;
|
|
|
|
NS_ADDREF(*aResult);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-06-05 16:51:58 -07:00
|
|
|
class nsUnionEnumerator MOZ_FINAL : public nsISimpleEnumerator
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
2014-06-26 18:35:39 -07:00
|
|
|
NS_DECL_ISUPPORTS
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
// nsISimpleEnumerator methods
|
|
|
|
NS_IMETHOD HasMoreElements(bool* aResult);
|
|
|
|
NS_IMETHOD GetNext(nsISupports** aResult);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
nsUnionEnumerator(nsISimpleEnumerator* aFirstEnumerator,
|
|
|
|
nsISimpleEnumerator* aSecondEnumerator);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
private:
|
2014-06-26 18:35:39 -07:00
|
|
|
~nsUnionEnumerator();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
protected:
|
2014-06-26 18:35:39 -07:00
|
|
|
nsCOMPtr<nsISimpleEnumerator> mFirstEnumerator, mSecondEnumerator;
|
|
|
|
bool mConsumed;
|
|
|
|
bool mAtSecond;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
nsUnionEnumerator::nsUnionEnumerator(nsISimpleEnumerator* aFirstEnumerator,
|
|
|
|
nsISimpleEnumerator* aSecondEnumerator)
|
|
|
|
: mFirstEnumerator(aFirstEnumerator)
|
|
|
|
, mSecondEnumerator(aSecondEnumerator)
|
|
|
|
, mConsumed(false)
|
|
|
|
, mAtSecond(false)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsUnionEnumerator::~nsUnionEnumerator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(nsUnionEnumerator, nsISimpleEnumerator)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsUnionEnumerator::HasMoreElements(bool* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
NS_PRECONDITION(aResult != 0, "null ptr");
|
|
|
|
if (!aResult) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
nsresult rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
if (mConsumed) {
|
|
|
|
*aResult = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-08-25 12:17:15 -07:00
|
|
|
if (!mAtSecond) {
|
2014-06-26 18:35:39 -07:00
|
|
|
rv = mFirstEnumerator->HasMoreElements(aResult);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
if (*aResult) {
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
mAtSecond = true;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
rv = mSecondEnumerator->HasMoreElements(aResult);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
if (*aResult) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
2014-06-26 18:35:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = false;
|
|
|
|
mConsumed = true;
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUnionEnumerator::GetNext(nsISupports** aResult)
|
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
NS_PRECONDITION(aResult != 0, "null ptr");
|
|
|
|
if (!aResult) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
if (mConsumed) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-08-25 12:17:15 -07:00
|
|
|
if (!mAtSecond) {
|
2014-06-26 18:35:39 -07:00
|
|
|
return mFirstEnumerator->GetNext(aResult);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
return mSecondEnumerator->GetNext(aResult);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-06-26 18:35:39 -07:00
|
|
|
NS_NewUnionEnumerator(nsISimpleEnumerator** aResult,
|
|
|
|
nsISimpleEnumerator* aFirstEnumerator,
|
|
|
|
nsISimpleEnumerator* aSecondEnumerator)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
*aResult = nullptr;
|
2014-08-25 12:17:15 -07:00
|
|
|
if (!aFirstEnumerator) {
|
2014-06-26 18:35:39 -07:00
|
|
|
*aResult = aSecondEnumerator;
|
2014-08-25 12:17:15 -07:00
|
|
|
} else if (!aSecondEnumerator) {
|
2014-06-26 18:35:39 -07:00
|
|
|
*aResult = aFirstEnumerator;
|
|
|
|
} else {
|
|
|
|
nsUnionEnumerator* enumer = new nsUnionEnumerator(aFirstEnumerator,
|
|
|
|
aSecondEnumerator);
|
|
|
|
if (!enumer) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-06-26 18:35:39 -07:00
|
|
|
*aResult = enumer;
|
|
|
|
}
|
|
|
|
NS_ADDREF(*aResult);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|