2006-08-08 17:09:19 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
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/. */
|
2006-08-08 17:09:04 +00:00
|
|
|
|
2011-11-21 06:21:16 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
|
2006-08-08 17:09:04 +00:00
|
|
|
#include "nsEnumeratorUtils.h"
|
|
|
|
|
2006-08-08 18:18:50 +00:00
|
|
|
#include "nsISimpleEnumerator.h"
|
|
|
|
#include "nsIStringEnumerator.h"
|
2006-08-08 17:09:04 +00:00
|
|
|
|
2006-08-08 18:18:50 +00:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
|
|
|
|
class EmptyEnumeratorImpl : public nsISimpleEnumerator,
|
|
|
|
public nsIUTF8StringEnumerator,
|
|
|
|
public nsIStringEnumerator
|
2006-08-08 17:09:04 +00:00
|
|
|
{
|
2006-08-08 18:18:50 +00:00
|
|
|
public:
|
2011-03-24 23:26:55 +00:00
|
|
|
EmptyEnumeratorImpl() {}
|
2006-08-08 18:18:50 +00:00
|
|
|
// 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() {
|
2013-05-30 21:20:02 +00:00
|
|
|
static const EmptyEnumeratorImpl kInstance;
|
|
|
|
return const_cast<EmptyEnumeratorImpl*>(&kInstance);
|
2006-08-08 18:18:50 +00:00
|
|
|
}
|
|
|
|
};
|
2006-08-08 17:09:04 +00:00
|
|
|
|
2006-08-08 18:18:50 +00:00
|
|
|
// nsISupports interface
|
|
|
|
NS_IMETHODIMP_(nsrefcnt) EmptyEnumeratorImpl::AddRef(void)
|
2006-08-08 17:09:04 +00:00
|
|
|
{
|
2006-08-08 18:18:50 +00:00
|
|
|
return 2;
|
2006-08-08 17:09:04 +00:00
|
|
|
}
|
|
|
|
|
2006-08-08 18:18:50 +00:00
|
|
|
NS_IMETHODIMP_(nsrefcnt) EmptyEnumeratorImpl::Release(void)
|
2006-08-08 17:09:04 +00:00
|
|
|
{
|
2006-08-08 18:18:50 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2006-08-08 17:09:04 +00:00
|
|
|
|
2009-04-06 00:26:25 +00:00
|
|
|
NS_IMPL_QUERY_INTERFACE3(EmptyEnumeratorImpl, nsISimpleEnumerator,
|
|
|
|
nsIUTF8StringEnumerator, nsIStringEnumerator)
|
2006-08-08 17:09:22 +00:00
|
|
|
|
2006-08-08 18:18:50 +00:00
|
|
|
// nsISimpleEnumerator interface
|
2011-09-29 06:19:26 +00:00
|
|
|
NS_IMETHODIMP EmptyEnumeratorImpl::HasMoreElements(bool* aResult)
|
2006-08-08 18:18:50 +00:00
|
|
|
{
|
2011-10-17 14:59:28 +00:00
|
|
|
*aResult = false;
|
2006-08-08 17:09:04 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
NS_IMETHODIMP EmptyEnumeratorImpl::HasMore(bool* aResult)
|
2006-08-08 17:09:04 +00:00
|
|
|
{
|
2011-10-17 14:59:28 +00:00
|
|
|
*aResult = false;
|
2006-08-08 18:18:50 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2006-08-08 17:09:04 +00:00
|
|
|
|
2006-08-08 18:18:50 +00:00
|
|
|
NS_IMETHODIMP EmptyEnumeratorImpl::GetNext(nsISupports** aResult)
|
|
|
|
{
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2006-08-08 17:09:22 +00:00
|
|
|
|
2006-08-08 18:18:50 +00:00
|
|
|
NS_IMETHODIMP EmptyEnumeratorImpl::GetNext(nsACString& aResult)
|
|
|
|
{
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2006-08-08 17:09:04 +00:00
|
|
|
|
2006-08-08 18:18:50 +00:00
|
|
|
NS_IMETHODIMP EmptyEnumeratorImpl::GetNext(nsAString& aResult)
|
|
|
|
{
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2006-08-08 17:09:04 +00:00
|
|
|
}
|
|
|
|
|
2006-08-08 18:18:50 +00:00
|
|
|
nsresult
|
|
|
|
NS_NewEmptyEnumerator(nsISimpleEnumerator** aResult)
|
2006-08-08 17:09:10 +00:00
|
|
|
{
|
2006-08-08 18:18:50 +00:00
|
|
|
*aResult = EmptyEnumeratorImpl::GetInstance();
|
2006-08-08 17:09:10 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2006-08-08 17:09:04 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-11-21 06:21:16 +00:00
|
|
|
class nsSingletonEnumerator MOZ_FINAL : public nsISimpleEnumerator
|
2006-08-08 18:18:50 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
// nsISimpleEnumerator methods
|
2011-09-29 06:19:26 +00:00
|
|
|
NS_IMETHOD HasMoreElements(bool* aResult);
|
2006-08-08 18:18:50 +00:00
|
|
|
NS_IMETHOD GetNext(nsISupports** aResult);
|
|
|
|
|
|
|
|
nsSingletonEnumerator(nsISupports* aValue);
|
|
|
|
|
|
|
|
private:
|
|
|
|
~nsSingletonEnumerator();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
nsISupports* mValue;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mConsumed;
|
2006-08-08 18:18:50 +00:00
|
|
|
};
|
|
|
|
|
2006-08-08 17:09:04 +00:00
|
|
|
nsSingletonEnumerator::nsSingletonEnumerator(nsISupports* aValue)
|
|
|
|
: mValue(aValue)
|
|
|
|
{
|
|
|
|
NS_IF_ADDREF(mValue);
|
2011-10-17 14:59:28 +00:00
|
|
|
mConsumed = (mValue ? false : true);
|
2006-08-08 17:09:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsSingletonEnumerator::~nsSingletonEnumerator()
|
|
|
|
{
|
|
|
|
NS_IF_RELEASE(mValue);
|
|
|
|
}
|
|
|
|
|
2006-08-08 17:09:13 +00:00
|
|
|
NS_IMPL_ISUPPORTS1(nsSingletonEnumerator, nsISimpleEnumerator)
|
2006-08-08 17:09:04 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsSingletonEnumerator::HasMoreElements(bool* aResult)
|
2006-08-08 17:09:04 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aResult != 0, "null ptr");
|
|
|
|
if (! aResult)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
*aResult = !mConsumed;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSingletonEnumerator::GetNext(nsISupports** aResult)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aResult != 0, "null ptr");
|
|
|
|
if (! aResult)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
if (mConsumed)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
mConsumed = true;
|
2006-08-08 17:09:04 +00:00
|
|
|
|
|
|
|
*aResult = mValue;
|
2006-08-08 17:09:28 +00:00
|
|
|
NS_ADDREF(*aResult);
|
2006-08-08 17:09:04 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2006-08-08 18:18:50 +00:00
|
|
|
nsresult
|
2006-08-08 17:09:10 +00:00
|
|
|
NS_NewSingletonEnumerator(nsISimpleEnumerator* *result,
|
|
|
|
nsISupports* singleton)
|
|
|
|
{
|
|
|
|
nsSingletonEnumerator* enumer = new nsSingletonEnumerator(singleton);
|
2012-07-30 14:20:58 +00:00
|
|
|
if (enumer == nullptr)
|
2006-08-08 17:09:10 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
*result = enumer;
|
2006-08-08 17:09:28 +00:00
|
|
|
NS_ADDREF(*result);
|
2006-08-08 17:09:10 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2006-08-08 17:09:04 +00:00
|
|
|
|
2006-08-08 17:09:10 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-06-05 23:51:58 +00:00
|
|
|
class nsUnionEnumerator MOZ_FINAL : public nsISimpleEnumerator
|
2006-08-08 18:18:50 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
// nsISimpleEnumerator methods
|
2011-09-29 06:19:26 +00:00
|
|
|
NS_IMETHOD HasMoreElements(bool* aResult);
|
2006-08-08 18:18:50 +00:00
|
|
|
NS_IMETHOD GetNext(nsISupports** aResult);
|
|
|
|
|
|
|
|
nsUnionEnumerator(nsISimpleEnumerator* firstEnumerator,
|
|
|
|
nsISimpleEnumerator* secondEnumerator);
|
|
|
|
|
|
|
|
private:
|
|
|
|
~nsUnionEnumerator();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
nsCOMPtr<nsISimpleEnumerator> mFirstEnumerator, mSecondEnumerator;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mConsumed;
|
|
|
|
bool mAtSecond;
|
2006-08-08 18:18:50 +00:00
|
|
|
};
|
|
|
|
|
2006-08-08 17:09:40 +00:00
|
|
|
nsUnionEnumerator::nsUnionEnumerator(nsISimpleEnumerator* firstEnumerator,
|
|
|
|
nsISimpleEnumerator* secondEnumerator)
|
|
|
|
: mFirstEnumerator(firstEnumerator),
|
|
|
|
mSecondEnumerator(secondEnumerator),
|
2011-10-17 14:59:28 +00:00
|
|
|
mConsumed(false), mAtSecond(false)
|
2006-08-08 17:09:40 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsUnionEnumerator::~nsUnionEnumerator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(nsUnionEnumerator, nsISimpleEnumerator)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsUnionEnumerator::HasMoreElements(bool* aResult)
|
2006-08-08 17:09:40 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aResult != 0, "null ptr");
|
|
|
|
if (! aResult)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (mConsumed) {
|
2011-10-17 14:59:28 +00:00
|
|
|
*aResult = false;
|
2006-08-08 17:09:40 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! mAtSecond) {
|
|
|
|
rv = mFirstEnumerator->HasMoreElements(aResult);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
if (*aResult)
|
|
|
|
return NS_OK;
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
mAtSecond = true;
|
2006-08-08 17:09:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rv = mSecondEnumerator->HasMoreElements(aResult);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
if (*aResult)
|
|
|
|
return NS_OK;
|
2006-08-08 17:09:04 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
*aResult = false;
|
|
|
|
mConsumed = true;
|
2006-08-08 17:09:40 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUnionEnumerator::GetNext(nsISupports** aResult)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aResult != 0, "null ptr");
|
|
|
|
if (! aResult)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
if (mConsumed)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
if (! mAtSecond)
|
|
|
|
return mFirstEnumerator->GetNext(aResult);
|
|
|
|
|
|
|
|
return mSecondEnumerator->GetNext(aResult);
|
|
|
|
}
|
|
|
|
|
2006-08-08 18:18:50 +00:00
|
|
|
nsresult
|
2006-08-08 17:09:40 +00:00
|
|
|
NS_NewUnionEnumerator(nsISimpleEnumerator* *result,
|
|
|
|
nsISimpleEnumerator* firstEnumerator,
|
|
|
|
nsISimpleEnumerator* secondEnumerator)
|
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
*result = nullptr;
|
2006-08-08 17:09:40 +00:00
|
|
|
if (! firstEnumerator) {
|
|
|
|
*result = secondEnumerator;
|
|
|
|
} else if (! secondEnumerator) {
|
|
|
|
*result = firstEnumerator;
|
|
|
|
} else {
|
|
|
|
nsUnionEnumerator* enumer = new nsUnionEnumerator(firstEnumerator, secondEnumerator);
|
2012-07-30 14:20:58 +00:00
|
|
|
if (enumer == nullptr)
|
2006-08-08 17:09:40 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
*result = enumer;
|
|
|
|
}
|
|
|
|
NS_ADDREF(*result);
|
|
|
|
return NS_OK;
|
|
|
|
}
|