Bug 545892 - Always pass WM_NCPAINT events to the default event procedure. r=bent...
[mozilla-central.git] / xpcom / glue / nsEnumeratorUtils.cpp
blobb5d915bf557318b8762a0cfc26f90d7cf662381c
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
13 * License.
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.
22 * Contributor(s):
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"
48 #include "nsCOMPtr.h"
50 class EmptyEnumeratorImpl : public nsISimpleEnumerator,
51 public nsIUTF8StringEnumerator,
52 public nsIStringEnumerator
54 public:
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);
69 private:
70 static const EmptyEnumeratorImpl kInstance;
73 // nsISupports interface
74 NS_IMETHODIMP_(nsrefcnt) EmptyEnumeratorImpl::AddRef(void)
76 return 2;
79 NS_IMETHODIMP_(nsrefcnt) EmptyEnumeratorImpl::Release(void)
81 return 1;
84 NS_IMPL_QUERY_INTERFACE3(EmptyEnumeratorImpl, nsISimpleEnumerator,
85 nsIUTF8StringEnumerator, nsIStringEnumerator)
87 // nsISimpleEnumerator interface
88 NS_IMETHODIMP EmptyEnumeratorImpl::HasMoreElements(PRBool* aResult)
90 *aResult = PR_FALSE;
91 return NS_OK;
94 NS_IMETHODIMP EmptyEnumeratorImpl::HasMore(PRBool* aResult)
96 *aResult = PR_FALSE;
97 return NS_OK;
100 NS_IMETHODIMP EmptyEnumeratorImpl::GetNext(nsISupports** aResult)
102 return NS_ERROR_UNEXPECTED;
105 NS_IMETHODIMP EmptyEnumeratorImpl::GetNext(nsACString& aResult)
107 return NS_ERROR_UNEXPECTED;
110 NS_IMETHODIMP EmptyEnumeratorImpl::GetNext(nsAString& aResult)
112 return NS_ERROR_UNEXPECTED;
115 const EmptyEnumeratorImpl EmptyEnumeratorImpl::kInstance;
117 nsresult
118 NS_NewEmptyEnumerator(nsISimpleEnumerator** aResult)
120 *aResult = EmptyEnumeratorImpl::GetInstance();
121 return NS_OK;
124 ////////////////////////////////////////////////////////////////////////////////
126 class nsSingletonEnumerator : public nsISimpleEnumerator
128 public:
129 NS_DECL_ISUPPORTS
131 // nsISimpleEnumerator methods
132 NS_IMETHOD HasMoreElements(PRBool* aResult);
133 NS_IMETHOD GetNext(nsISupports** aResult);
135 nsSingletonEnumerator(nsISupports* aValue);
137 private:
138 ~nsSingletonEnumerator();
140 protected:
141 nsISupports* mValue;
142 PRBool mConsumed;
145 nsSingletonEnumerator::nsSingletonEnumerator(nsISupports* aValue)
146 : mValue(aValue)
148 NS_IF_ADDREF(mValue);
149 mConsumed = (mValue ? PR_FALSE : PR_TRUE);
152 nsSingletonEnumerator::~nsSingletonEnumerator()
154 NS_IF_RELEASE(mValue);
157 NS_IMPL_ISUPPORTS1(nsSingletonEnumerator, nsISimpleEnumerator)
159 NS_IMETHODIMP
160 nsSingletonEnumerator::HasMoreElements(PRBool* aResult)
162 NS_PRECONDITION(aResult != 0, "null ptr");
163 if (! aResult)
164 return NS_ERROR_NULL_POINTER;
166 *aResult = !mConsumed;
167 return NS_OK;
171 NS_IMETHODIMP
172 nsSingletonEnumerator::GetNext(nsISupports** aResult)
174 NS_PRECONDITION(aResult != 0, "null ptr");
175 if (! aResult)
176 return NS_ERROR_NULL_POINTER;
178 if (mConsumed)
179 return NS_ERROR_UNEXPECTED;
181 mConsumed = PR_TRUE;
183 *aResult = mValue;
184 NS_ADDREF(*aResult);
185 return NS_OK;
188 nsresult
189 NS_NewSingletonEnumerator(nsISimpleEnumerator* *result,
190 nsISupports* singleton)
192 nsSingletonEnumerator* enumer = new nsSingletonEnumerator(singleton);
193 if (enumer == nsnull)
194 return NS_ERROR_OUT_OF_MEMORY;
195 *result = enumer;
196 NS_ADDREF(*result);
197 return NS_OK;
200 ////////////////////////////////////////////////////////////////////////////////
202 class nsUnionEnumerator : public nsISimpleEnumerator
204 public:
205 NS_DECL_ISUPPORTS
207 // nsISimpleEnumerator methods
208 NS_IMETHOD HasMoreElements(PRBool* aResult);
209 NS_IMETHOD GetNext(nsISupports** aResult);
211 nsUnionEnumerator(nsISimpleEnumerator* firstEnumerator,
212 nsISimpleEnumerator* secondEnumerator);
214 private:
215 ~nsUnionEnumerator();
217 protected:
218 nsCOMPtr<nsISimpleEnumerator> mFirstEnumerator, mSecondEnumerator;
219 PRBool mConsumed;
220 PRBool mAtSecond;
223 nsUnionEnumerator::nsUnionEnumerator(nsISimpleEnumerator* firstEnumerator,
224 nsISimpleEnumerator* secondEnumerator)
225 : mFirstEnumerator(firstEnumerator),
226 mSecondEnumerator(secondEnumerator),
227 mConsumed(PR_FALSE), mAtSecond(PR_FALSE)
231 nsUnionEnumerator::~nsUnionEnumerator()
235 NS_IMPL_ISUPPORTS1(nsUnionEnumerator, nsISimpleEnumerator)
237 NS_IMETHODIMP
238 nsUnionEnumerator::HasMoreElements(PRBool* aResult)
240 NS_PRECONDITION(aResult != 0, "null ptr");
241 if (! aResult)
242 return NS_ERROR_NULL_POINTER;
244 nsresult rv;
246 if (mConsumed) {
247 *aResult = PR_FALSE;
248 return NS_OK;
251 if (! mAtSecond) {
252 rv = mFirstEnumerator->HasMoreElements(aResult);
253 if (NS_FAILED(rv)) return rv;
255 if (*aResult)
256 return NS_OK;
258 mAtSecond = PR_TRUE;
261 rv = mSecondEnumerator->HasMoreElements(aResult);
262 if (NS_FAILED(rv)) return rv;
264 if (*aResult)
265 return NS_OK;
267 *aResult = PR_FALSE;
268 mConsumed = PR_TRUE;
269 return NS_OK;
272 NS_IMETHODIMP
273 nsUnionEnumerator::GetNext(nsISupports** aResult)
275 NS_PRECONDITION(aResult != 0, "null ptr");
276 if (! aResult)
277 return NS_ERROR_NULL_POINTER;
279 if (mConsumed)
280 return NS_ERROR_UNEXPECTED;
282 if (! mAtSecond)
283 return mFirstEnumerator->GetNext(aResult);
285 return mSecondEnumerator->GetNext(aResult);
288 nsresult
289 NS_NewUnionEnumerator(nsISimpleEnumerator* *result,
290 nsISimpleEnumerator* firstEnumerator,
291 nsISimpleEnumerator* secondEnumerator)
293 *result = nsnull;
294 if (! firstEnumerator) {
295 *result = secondEnumerator;
296 } else if (! secondEnumerator) {
297 *result = firstEnumerator;
298 } else {
299 nsUnionEnumerator* enumer = new nsUnionEnumerator(firstEnumerator, secondEnumerator);
300 if (enumer == nsnull)
301 return NS_ERROR_OUT_OF_MEMORY;
302 *result = enumer;
304 NS_ADDREF(*result);
305 return NS_OK;