1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/Attributes.h"
9 #include "nsEnumeratorUtils.h"
11 #include "nsISimpleEnumerator.h"
12 #include "nsIStringEnumerator.h"
16 class EmptyEnumeratorImpl
17 : public nsISimpleEnumerator
18 , public nsIUTF8StringEnumerator
19 , public nsIStringEnumerator
22 EmptyEnumeratorImpl() {}
24 // nsISupports interface
25 NS_DECL_ISUPPORTS_INHERITED
// not really inherited, but no mRefCnt
27 // nsISimpleEnumerator
28 NS_DECL_NSISIMPLEENUMERATOR
29 NS_DECL_NSIUTF8STRINGENUMERATOR
30 // can't use NS_DECL_NSISTRINGENUMERATOR because they share the
31 // HasMore() signature
32 NS_IMETHOD
GetNext(nsAString
& aResult
) MOZ_OVERRIDE
;
34 static EmptyEnumeratorImpl
* GetInstance()
36 static const EmptyEnumeratorImpl kInstance
;
37 return const_cast<EmptyEnumeratorImpl
*>(&kInstance
);
41 // nsISupports interface
42 NS_IMETHODIMP_(MozExternalRefCountType
)
43 EmptyEnumeratorImpl::AddRef(void)
48 NS_IMETHODIMP_(MozExternalRefCountType
)
49 EmptyEnumeratorImpl::Release(void)
54 NS_IMPL_QUERY_INTERFACE(EmptyEnumeratorImpl
, nsISimpleEnumerator
,
55 nsIUTF8StringEnumerator
, nsIStringEnumerator
)
57 // nsISimpleEnumerator interface
59 EmptyEnumeratorImpl::HasMoreElements(bool* aResult
)
66 EmptyEnumeratorImpl::HasMore(bool* aResult
)
73 EmptyEnumeratorImpl::GetNext(nsISupports
** aResult
)
75 return NS_ERROR_UNEXPECTED
;
79 EmptyEnumeratorImpl::GetNext(nsACString
& aResult
)
81 return NS_ERROR_UNEXPECTED
;
85 EmptyEnumeratorImpl::GetNext(nsAString
& aResult
)
87 return NS_ERROR_UNEXPECTED
;
91 NS_NewEmptyEnumerator(nsISimpleEnumerator
** aResult
)
93 *aResult
= EmptyEnumeratorImpl::GetInstance();
97 ////////////////////////////////////////////////////////////////////////////////
99 class nsSingletonEnumerator MOZ_FINAL
: public nsISimpleEnumerator
104 // nsISimpleEnumerator methods
105 NS_IMETHOD
HasMoreElements(bool* aResult
) MOZ_OVERRIDE
;
106 NS_IMETHOD
GetNext(nsISupports
** aResult
) MOZ_OVERRIDE
;
108 explicit nsSingletonEnumerator(nsISupports
* aValue
);
111 ~nsSingletonEnumerator();
114 nsCOMPtr
<nsISupports
> mValue
;
118 nsSingletonEnumerator::nsSingletonEnumerator(nsISupports
* aValue
)
121 mConsumed
= (mValue
? false : true);
124 nsSingletonEnumerator::~nsSingletonEnumerator()
128 NS_IMPL_ISUPPORTS(nsSingletonEnumerator
, nsISimpleEnumerator
)
131 nsSingletonEnumerator::HasMoreElements(bool* aResult
)
133 NS_PRECONDITION(aResult
!= 0, "null ptr");
135 return NS_ERROR_NULL_POINTER
;
138 *aResult
= !mConsumed
;
144 nsSingletonEnumerator::GetNext(nsISupports
** aResult
)
146 NS_PRECONDITION(aResult
!= 0, "null ptr");
148 return NS_ERROR_NULL_POINTER
;
152 return NS_ERROR_UNEXPECTED
;
163 NS_NewSingletonEnumerator(nsISimpleEnumerator
** aResult
,
164 nsISupports
* aSingleton
)
166 nsSingletonEnumerator
* enumer
= new nsSingletonEnumerator(aSingleton
);
168 return NS_ERROR_OUT_OF_MEMORY
;
175 ////////////////////////////////////////////////////////////////////////////////
177 class nsUnionEnumerator MOZ_FINAL
: public nsISimpleEnumerator
182 // nsISimpleEnumerator methods
183 NS_IMETHOD
HasMoreElements(bool* aResult
) MOZ_OVERRIDE
;
184 NS_IMETHOD
GetNext(nsISupports
** aResult
) MOZ_OVERRIDE
;
186 nsUnionEnumerator(nsISimpleEnumerator
* aFirstEnumerator
,
187 nsISimpleEnumerator
* aSecondEnumerator
);
190 ~nsUnionEnumerator();
193 nsCOMPtr
<nsISimpleEnumerator
> mFirstEnumerator
, mSecondEnumerator
;
198 nsUnionEnumerator::nsUnionEnumerator(nsISimpleEnumerator
* aFirstEnumerator
,
199 nsISimpleEnumerator
* aSecondEnumerator
)
200 : mFirstEnumerator(aFirstEnumerator
)
201 , mSecondEnumerator(aSecondEnumerator
)
207 nsUnionEnumerator::~nsUnionEnumerator()
211 NS_IMPL_ISUPPORTS(nsUnionEnumerator
, nsISimpleEnumerator
)
214 nsUnionEnumerator::HasMoreElements(bool* aResult
)
216 NS_PRECONDITION(aResult
!= 0, "null ptr");
218 return NS_ERROR_NULL_POINTER
;
229 rv
= mFirstEnumerator
->HasMoreElements(aResult
);
241 rv
= mSecondEnumerator
->HasMoreElements(aResult
);
256 nsUnionEnumerator::GetNext(nsISupports
** aResult
)
258 NS_PRECONDITION(aResult
!= 0, "null ptr");
260 return NS_ERROR_NULL_POINTER
;
264 return NS_ERROR_UNEXPECTED
;
268 return mFirstEnumerator
->GetNext(aResult
);
271 return mSecondEnumerator
->GetNext(aResult
);
275 NS_NewUnionEnumerator(nsISimpleEnumerator
** aResult
,
276 nsISimpleEnumerator
* aFirstEnumerator
,
277 nsISimpleEnumerator
* aSecondEnumerator
)
280 if (!aFirstEnumerator
) {
281 *aResult
= aSecondEnumerator
;
282 } else if (!aSecondEnumerator
) {
283 *aResult
= aFirstEnumerator
;
285 nsUnionEnumerator
* enumer
= new nsUnionEnumerator(aFirstEnumerator
,
288 return NS_ERROR_OUT_OF_MEMORY
;