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