Bug 545892 - Always pass WM_NCPAINT events to the default event procedure. r=bent...
[mozilla-central.git] / xpcom / glue / nsArrayEnumerator.cpp
blob9dc8274f7b86005266cfe95d2c59bf3d42f966d7
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 XPCOM Array implementation.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corp.
19 * Portions created by the Initial Developer are Copyright (C) 2002
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Alec Flett <alecf@netscape.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsArrayEnumerator.h"
41 #include "nsIArray.h"
42 #include "nsISimpleEnumerator.h"
44 #include "nsCOMArray.h"
45 #include "nsCOMPtr.h"
47 class nsSimpleArrayEnumerator : public nsISimpleEnumerator
49 public:
50 // nsISupports interface
51 NS_DECL_ISUPPORTS
53 // nsISimpleEnumerator interface
54 NS_DECL_NSISIMPLEENUMERATOR
56 // nsSimpleArrayEnumerator methods
57 nsSimpleArrayEnumerator(nsIArray* aValueArray) :
58 mValueArray(aValueArray), mIndex(0) {
61 private:
62 ~nsSimpleArrayEnumerator() {}
64 protected:
65 nsCOMPtr<nsIArray> mValueArray;
66 PRUint32 mIndex;
69 NS_IMPL_ISUPPORTS1(nsSimpleArrayEnumerator, nsISimpleEnumerator)
71 NS_IMETHODIMP
72 nsSimpleArrayEnumerator::HasMoreElements(PRBool* aResult)
74 NS_PRECONDITION(aResult != 0, "null ptr");
75 if (! aResult)
76 return NS_ERROR_NULL_POINTER;
78 if (!mValueArray) {
79 *aResult = PR_FALSE;
80 return NS_OK;
83 PRUint32 cnt;
84 nsresult rv = mValueArray->GetLength(&cnt);
85 if (NS_FAILED(rv)) return rv;
86 *aResult = (mIndex < cnt);
87 return NS_OK;
90 NS_IMETHODIMP
91 nsSimpleArrayEnumerator::GetNext(nsISupports** aResult)
93 NS_PRECONDITION(aResult != 0, "null ptr");
94 if (! aResult)
95 return NS_ERROR_NULL_POINTER;
97 if (!mValueArray) {
98 *aResult = nsnull;
99 return NS_OK;
102 PRUint32 cnt;
103 nsresult rv = mValueArray->GetLength(&cnt);
104 if (NS_FAILED(rv)) return rv;
105 if (mIndex >= cnt)
106 return NS_ERROR_UNEXPECTED;
108 return mValueArray->QueryElementAt(mIndex++, NS_GET_IID(nsISupports), (void**)aResult);
111 nsresult
112 NS_NewArrayEnumerator(nsISimpleEnumerator* *result,
113 nsIArray* array)
115 nsSimpleArrayEnumerator* enumer = new nsSimpleArrayEnumerator(array);
116 if (enumer == nsnull)
117 return NS_ERROR_OUT_OF_MEMORY;
119 NS_ADDREF(*result = enumer);
120 return NS_OK;
123 ////////////////////////////////////////////////////////////////////////////////
125 // enumerator implementation for nsCOMArray
126 // creates a snapshot of the array in question
127 // you MUST use NS_NewArrayEnumerator to create this, so that
128 // allocation is done correctly
129 class nsCOMArrayEnumerator : public nsISimpleEnumerator
131 public:
132 // nsISupports interface
133 NS_DECL_ISUPPORTS
135 // nsISimpleEnumerator interface
136 NS_DECL_NSISIMPLEENUMERATOR
138 // nsSimpleArrayEnumerator methods
139 nsCOMArrayEnumerator() : mIndex(0) {
142 // specialized operator to make sure we make room for mValues
143 void* operator new (size_t size, const nsCOMArray_base& aArray) CPP_THROW_NEW;
144 void operator delete(void* ptr) {
145 ::operator delete(ptr);
148 private:
149 ~nsCOMArrayEnumerator(void);
151 protected:
152 PRUint32 mIndex; // current position
153 PRUint32 mArraySize; // size of the array
155 // this is actually bigger
156 nsISupports* mValueArray[1];
159 NS_IMPL_ISUPPORTS1(nsCOMArrayEnumerator, nsISimpleEnumerator)
161 nsCOMArrayEnumerator::~nsCOMArrayEnumerator()
163 // only release the entries that we haven't visited yet
164 for (; mIndex < mArraySize; ++mIndex) {
165 NS_IF_RELEASE(mValueArray[mIndex]);
169 NS_IMETHODIMP
170 nsCOMArrayEnumerator::HasMoreElements(PRBool* aResult)
172 NS_PRECONDITION(aResult != 0, "null ptr");
173 if (! aResult)
174 return NS_ERROR_NULL_POINTER;
176 *aResult = (mIndex < mArraySize);
177 return NS_OK;
180 NS_IMETHODIMP
181 nsCOMArrayEnumerator::GetNext(nsISupports** aResult)
183 NS_PRECONDITION(aResult != 0, "null ptr");
184 if (! aResult)
185 return NS_ERROR_NULL_POINTER;
187 if (mIndex >= mArraySize)
188 return NS_ERROR_UNEXPECTED;
190 // pass the ownership of the reference to the caller. Since
191 // we AddRef'ed during creation of |this|, there is no need
192 // to AddRef here
193 *aResult = mValueArray[mIndex++];
195 // this really isn't necessary. just pretend this happens, since
196 // we'll never visit this value again!
197 // mValueArray[(mIndex-1)] = nsnull;
199 return NS_OK;
202 void*
203 nsCOMArrayEnumerator::operator new (size_t size, const nsCOMArray_base& aArray)
204 CPP_THROW_NEW
206 // create enough space such that mValueArray points to a large
207 // enough value. Note that the initial value of size gives us
208 // space for mValueArray[0], so we must subtract
209 size += (aArray.Count() - 1) * sizeof(aArray[0]);
211 // do the actual allocation
212 nsCOMArrayEnumerator * result =
213 static_cast<nsCOMArrayEnumerator*>(::operator new(size));
214 NS_ENSURE_TRUE(result, nsnull);
216 // now need to copy over the values, and addref each one
217 // now this might seem like a lot of work, but we're actually just
218 // doing all our AddRef's ahead of time since GetNext() doesn't
219 // need to AddRef() on the way out
220 PRUint32 i;
221 PRUint32 max = result->mArraySize = aArray.Count();
222 for (i = 0; i<max; i++) {
223 result->mValueArray[i] = aArray[i];
224 NS_IF_ADDREF(result->mValueArray[i]);
227 return result;
230 nsresult
231 NS_NewArrayEnumerator(nsISimpleEnumerator* *aResult,
232 const nsCOMArray_base& aArray)
234 nsCOMArrayEnumerator *enumerator = new (aArray) nsCOMArrayEnumerator();
235 if (!enumerator) return NS_ERROR_OUT_OF_MEMORY;
237 NS_ADDREF(*aResult = enumerator);
238 return NS_OK;