1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * Benjamin Smedberg <benjamin@smedbergs.us>
25 * Code moved from nsEmptyEnumerator.cpp:
26 * L. David Baron <dbaron@dbaron.org>
27 * Pierre Phaneuf <pp@ludusdesign.com>
29 * Alternatively, the contents of this file may be used under the terms of
30 * either of the GNU General Public License Version 2 or later (the "GPL"),
31 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
32 * in which case the provisions of the GPL or the LGPL are applicable instead
33 * of those above. If you wish to allow use of your version of this file only
34 * under the terms of either the GPL or the LGPL, and not to allow others to
35 * use your version of this file under the terms of the MPL, indicate your
36 * decision by deleting the provisions above and replace them with the notice
37 * and other provisions required by the GPL or the LGPL. If you do not delete
38 * the provisions above, a recipient may use your version of this file under
39 * the terms of any one of the MPL, the GPL or the LGPL.
41 * ***** END LICENSE BLOCK ***** */
43 #include "nsEnumeratorUtils.h"
45 #include "nsISimpleEnumerator.h"
46 #include "nsIStringEnumerator.h"
50 class EmptyEnumeratorImpl
: public nsISimpleEnumerator
,
51 public nsIUTF8StringEnumerator
,
52 public nsIStringEnumerator
55 // nsISupports interface
56 NS_DECL_ISUPPORTS_INHERITED
// not really inherited, but no mRefCnt
58 // nsISimpleEnumerator
59 NS_DECL_NSISIMPLEENUMERATOR
60 NS_DECL_NSIUTF8STRINGENUMERATOR
61 // can't use NS_DECL_NSISTRINGENUMERATOR because they share the
62 // HasMore() signature
63 NS_IMETHOD
GetNext(nsAString
& aResult
);
65 static EmptyEnumeratorImpl
* GetInstance() {
66 return const_cast<EmptyEnumeratorImpl
*>(&kInstance
);
70 static const EmptyEnumeratorImpl kInstance
;
73 // nsISupports interface
74 NS_IMETHODIMP_(nsrefcnt
) EmptyEnumeratorImpl::AddRef(void)
79 NS_IMETHODIMP_(nsrefcnt
) EmptyEnumeratorImpl::Release(void)
84 NS_IMPL_QUERY_INTERFACE1(EmptyEnumeratorImpl
, nsISimpleEnumerator
)
86 // nsISimpleEnumerator interface
87 NS_IMETHODIMP
EmptyEnumeratorImpl::HasMoreElements(PRBool
* aResult
)
93 NS_IMETHODIMP
EmptyEnumeratorImpl::HasMore(PRBool
* aResult
)
99 NS_IMETHODIMP
EmptyEnumeratorImpl::GetNext(nsISupports
** aResult
)
101 return NS_ERROR_UNEXPECTED
;
104 NS_IMETHODIMP
EmptyEnumeratorImpl::GetNext(nsACString
& aResult
)
106 return NS_ERROR_UNEXPECTED
;
109 NS_IMETHODIMP
EmptyEnumeratorImpl::GetNext(nsAString
& aResult
)
111 return NS_ERROR_UNEXPECTED
;
114 const EmptyEnumeratorImpl
EmptyEnumeratorImpl::kInstance
;
117 NS_NewEmptyEnumerator(nsISimpleEnumerator
** aResult
)
119 *aResult
= EmptyEnumeratorImpl::GetInstance();
123 ////////////////////////////////////////////////////////////////////////////////
125 class nsSingletonEnumerator
: public nsISimpleEnumerator
130 // nsISimpleEnumerator methods
131 NS_IMETHOD
HasMoreElements(PRBool
* aResult
);
132 NS_IMETHOD
GetNext(nsISupports
** aResult
);
134 nsSingletonEnumerator(nsISupports
* aValue
);
137 ~nsSingletonEnumerator();
144 nsSingletonEnumerator::nsSingletonEnumerator(nsISupports
* aValue
)
147 NS_IF_ADDREF(mValue
);
148 mConsumed
= (mValue
? PR_FALSE
: PR_TRUE
);
151 nsSingletonEnumerator::~nsSingletonEnumerator()
153 NS_IF_RELEASE(mValue
);
156 NS_IMPL_ISUPPORTS1(nsSingletonEnumerator
, nsISimpleEnumerator
)
159 nsSingletonEnumerator::HasMoreElements(PRBool
* aResult
)
161 NS_PRECONDITION(aResult
!= 0, "null ptr");
163 return NS_ERROR_NULL_POINTER
;
165 *aResult
= !mConsumed
;
171 nsSingletonEnumerator::GetNext(nsISupports
** aResult
)
173 NS_PRECONDITION(aResult
!= 0, "null ptr");
175 return NS_ERROR_NULL_POINTER
;
178 return NS_ERROR_UNEXPECTED
;
188 NS_NewSingletonEnumerator(nsISimpleEnumerator
* *result
,
189 nsISupports
* singleton
)
191 nsSingletonEnumerator
* enumer
= new nsSingletonEnumerator(singleton
);
192 if (enumer
== nsnull
)
193 return NS_ERROR_OUT_OF_MEMORY
;
199 ////////////////////////////////////////////////////////////////////////////////
201 class nsUnionEnumerator
: public nsISimpleEnumerator
206 // nsISimpleEnumerator methods
207 NS_IMETHOD
HasMoreElements(PRBool
* aResult
);
208 NS_IMETHOD
GetNext(nsISupports
** aResult
);
210 nsUnionEnumerator(nsISimpleEnumerator
* firstEnumerator
,
211 nsISimpleEnumerator
* secondEnumerator
);
214 ~nsUnionEnumerator();
217 nsCOMPtr
<nsISimpleEnumerator
> mFirstEnumerator
, mSecondEnumerator
;
222 nsUnionEnumerator::nsUnionEnumerator(nsISimpleEnumerator
* firstEnumerator
,
223 nsISimpleEnumerator
* secondEnumerator
)
224 : mFirstEnumerator(firstEnumerator
),
225 mSecondEnumerator(secondEnumerator
),
226 mConsumed(PR_FALSE
), mAtSecond(PR_FALSE
)
230 nsUnionEnumerator::~nsUnionEnumerator()
234 NS_IMPL_ISUPPORTS1(nsUnionEnumerator
, nsISimpleEnumerator
)
237 nsUnionEnumerator::HasMoreElements(PRBool
* aResult
)
239 NS_PRECONDITION(aResult
!= 0, "null ptr");
241 return NS_ERROR_NULL_POINTER
;
251 rv
= mFirstEnumerator
->HasMoreElements(aResult
);
252 if (NS_FAILED(rv
)) return rv
;
260 rv
= mSecondEnumerator
->HasMoreElements(aResult
);
261 if (NS_FAILED(rv
)) return rv
;
272 nsUnionEnumerator::GetNext(nsISupports
** aResult
)
274 NS_PRECONDITION(aResult
!= 0, "null ptr");
276 return NS_ERROR_NULL_POINTER
;
279 return NS_ERROR_UNEXPECTED
;
282 return mFirstEnumerator
->GetNext(aResult
);
284 return mSecondEnumerator
->GetNext(aResult
);
288 NS_NewUnionEnumerator(nsISimpleEnumerator
* *result
,
289 nsISimpleEnumerator
* firstEnumerator
,
290 nsISimpleEnumerator
* secondEnumerator
)
293 if (! firstEnumerator
) {
294 *result
= secondEnumerator
;
295 } else if (! secondEnumerator
) {
296 *result
= firstEnumerator
;
298 nsUnionEnumerator
* enumer
= new nsUnionEnumerator(firstEnumerator
, secondEnumerator
);
299 if (enumer
== nsnull
)
300 return NS_ERROR_OUT_OF_MEMORY
;