VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/ds/nsArrayEnumerator.cpp@ 6542

Last change on this file since 6542 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.4 KB
Line 
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
4 *
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/
9 *
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.
14 *
15 * The Original Code is XPCOM Array implementation.
16 *
17 * The Initial Developer of the Original Code
18 * Netscape Communications Corp.
19 * Portions created by the Initial Developer are Copyright (C) 2002
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 * Alec Flett <[email protected]>
24 *
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.
36 *
37 * ***** END LICENSE BLOCK ***** */
38
39#include "nsArrayEnumerator.h"
40
41NS_IMPL_ISUPPORTS1(nsSimpleArrayEnumerator, nsISimpleEnumerator)
42
43NS_IMETHODIMP
44nsSimpleArrayEnumerator::HasMoreElements(PRBool* aResult)
45{
46 NS_PRECONDITION(aResult != 0, "null ptr");
47 if (! aResult)
48 return NS_ERROR_NULL_POINTER;
49
50 if (!mValueArray) {
51 *aResult = PR_FALSE;
52 return NS_OK;
53 }
54
55 PRUint32 cnt;
56 nsresult rv = mValueArray->GetLength(&cnt);
57 if (NS_FAILED(rv)) return rv;
58 *aResult = (mIndex < cnt);
59 return NS_OK;
60}
61
62NS_IMETHODIMP
63nsSimpleArrayEnumerator::GetNext(nsISupports** aResult)
64{
65 NS_PRECONDITION(aResult != 0, "null ptr");
66 if (! aResult)
67 return NS_ERROR_NULL_POINTER;
68
69 if (!mValueArray) {
70 *aResult = nsnull;
71 return NS_OK;
72 }
73
74 PRUint32 cnt;
75 nsresult rv = mValueArray->GetLength(&cnt);
76 if (NS_FAILED(rv)) return rv;
77 if (mIndex >= cnt)
78 return NS_ERROR_UNEXPECTED;
79
80 return mValueArray->QueryElementAt(mIndex++, NS_GET_IID(nsISupports), (void**)aResult);
81}
82
83extern NS_COM nsresult
84NS_NewArrayEnumerator(nsISimpleEnumerator* *result,
85 nsIArray* array)
86{
87 nsSimpleArrayEnumerator* enumer = new nsSimpleArrayEnumerator(array);
88 if (enumer == nsnull)
89 return NS_ERROR_OUT_OF_MEMORY;
90
91 NS_ADDREF(*result = enumer);
92 return NS_OK;
93}
94
95////////////////////////////////////////////////////////////////////////////////
96
97// enumerator implementation for nsCOMArray
98// creates a snapshot of the array in question
99// you MUST use NS_NewArrayEnumerator to create this, so that
100// allocation is done correctly
101class nsCOMArrayEnumerator : public nsISimpleEnumerator
102{
103public:
104 // nsISupports interface
105 NS_DECL_ISUPPORTS
106
107 // nsISimpleEnumerator interface
108 NS_DECL_NSISIMPLEENUMERATOR
109
110 // nsSimpleArrayEnumerator methods
111 nsCOMArrayEnumerator() : mIndex(0) {
112 }
113
114 // specialized operator to make sure we make room for mValues
115 void* operator new (size_t size, const nsCOMArray_base& aArray) CPP_THROW_NEW;
116 void operator delete(void* ptr) {
117 ::operator delete(ptr);
118 }
119
120private:
121 ~nsCOMArrayEnumerator(void);
122
123protected:
124 PRUint32 mIndex; // current position
125 PRUint32 mArraySize; // size of the array
126
127 // this is actually bigger
128 nsISupports* mValueArray[1];
129};
130
131NS_IMPL_ISUPPORTS1(nsCOMArrayEnumerator, nsISimpleEnumerator)
132
133nsCOMArrayEnumerator::~nsCOMArrayEnumerator()
134{
135 // only release the entries that we haven't visited yet
136 for (; mIndex < mArraySize; ++mIndex) {
137 NS_IF_RELEASE(mValueArray[mIndex]);
138 }
139}
140
141NS_IMETHODIMP
142nsCOMArrayEnumerator::HasMoreElements(PRBool* aResult)
143{
144 NS_PRECONDITION(aResult != 0, "null ptr");
145 if (! aResult)
146 return NS_ERROR_NULL_POINTER;
147
148 *aResult = (mIndex < mArraySize);
149 return NS_OK;
150}
151
152NS_IMETHODIMP
153nsCOMArrayEnumerator::GetNext(nsISupports** aResult)
154{
155 NS_PRECONDITION(aResult != 0, "null ptr");
156 if (! aResult)
157 return NS_ERROR_NULL_POINTER;
158
159 if (mIndex >= mArraySize)
160 return NS_ERROR_UNEXPECTED;
161
162 // pass the ownership of the reference to the caller. Since
163 // we AddRef'ed during creation of |this|, there is no need
164 // to AddRef here
165 *aResult = mValueArray[mIndex++];
166
167 // this really isn't necessary. just pretend this happens, since
168 // we'll never visit this value again!
169 // mValueArray[(mIndex-1)] = nsnull;
170
171 return NS_OK;
172}
173
174void*
175nsCOMArrayEnumerator::operator new (size_t size, const nsCOMArray_base& aArray)
176 CPP_THROW_NEW
177{
178 // create enough space such that mValueArray points to a large
179 // enough value. Note that the initial value of size gives us
180 // space for mValueArray[0], so we must subtract
181 size += (aArray.Count() - 1) * sizeof(aArray[0]);
182
183 // do the actual allocation
184 nsCOMArrayEnumerator * result =
185 NS_STATIC_CAST(nsCOMArrayEnumerator*, ::operator new(size));
186
187 // now need to copy over the values, and addref each one
188 // now this might seem like alot of work, but we're actually just
189 // doing all our AddRef's ahead of time since GetNext() doesn't
190 // need to AddRef() on the way out
191 PRUint32 i;
192 PRUint32 max = result->mArraySize = aArray.Count();
193 for (i = 0; i<max; i++) {
194 result->mValueArray[i] = aArray[i];
195 NS_IF_ADDREF(result->mValueArray[i]);
196 }
197
198 return result;
199}
200
201extern NS_COM nsresult
202NS_NewArrayEnumerator(nsISimpleEnumerator* *aResult,
203 const nsCOMArray_base& aArray)
204{
205 nsCOMArrayEnumerator *enumerator = new (aArray) nsCOMArrayEnumerator();
206 if (!enumerator) return NS_ERROR_OUT_OF_MEMORY;
207
208 NS_ADDREF(*aResult = enumerator);
209 return NS_OK;
210}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette