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