1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
---|
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 mozilla.org code.
|
---|
16 | *
|
---|
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.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either of the GNU General Public License Version 2 or later (the "GPL"),
|
---|
26 | * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | #include "nsSupportsArrayEnumerator.h"
|
---|
39 | #include "nsISupportsArray.h"
|
---|
40 |
|
---|
41 | nsSupportsArrayEnumerator::nsSupportsArrayEnumerator(nsISupportsArray* array)
|
---|
42 | : mArray(array), mCursor(0)
|
---|
43 | {
|
---|
44 | NS_ASSERTION(array, "null array");
|
---|
45 | NS_ADDREF(mArray);
|
---|
46 | }
|
---|
47 |
|
---|
48 | nsSupportsArrayEnumerator::~nsSupportsArrayEnumerator()
|
---|
49 | {
|
---|
50 | NS_RELEASE(mArray);
|
---|
51 | }
|
---|
52 |
|
---|
53 | NS_IMPL_ISUPPORTS2(nsSupportsArrayEnumerator, nsIBidirectionalEnumerator, nsIEnumerator)
|
---|
54 |
|
---|
55 | NS_IMETHODIMP
|
---|
56 | nsSupportsArrayEnumerator::First()
|
---|
57 | {
|
---|
58 | mCursor = 0;
|
---|
59 | PRUint32 cnt;
|
---|
60 | nsresult rv = mArray->Count(&cnt);
|
---|
61 | if (NS_FAILED(rv)) return rv;
|
---|
62 | PRInt32 end = (PRInt32)cnt;
|
---|
63 | if (mCursor < end)
|
---|
64 | return NS_OK;
|
---|
65 | else
|
---|
66 | return NS_ERROR_FAILURE;
|
---|
67 | }
|
---|
68 |
|
---|
69 | NS_IMETHODIMP
|
---|
70 | nsSupportsArrayEnumerator::Next()
|
---|
71 | {
|
---|
72 | PRUint32 cnt;
|
---|
73 | nsresult rv = mArray->Count(&cnt);
|
---|
74 | if (NS_FAILED(rv)) return rv;
|
---|
75 | PRInt32 end = (PRInt32)cnt;
|
---|
76 | if (mCursor < end) // don't count upward forever
|
---|
77 | mCursor++;
|
---|
78 | if (mCursor < end)
|
---|
79 | return NS_OK;
|
---|
80 | else
|
---|
81 | return NS_ERROR_FAILURE;
|
---|
82 | }
|
---|
83 |
|
---|
84 | NS_IMETHODIMP
|
---|
85 | nsSupportsArrayEnumerator::CurrentItem(nsISupports **aItem)
|
---|
86 | {
|
---|
87 | NS_ASSERTION(aItem, "null out parameter");
|
---|
88 | PRUint32 cnt;
|
---|
89 | nsresult rv = mArray->Count(&cnt);
|
---|
90 | if (NS_FAILED(rv)) return rv;
|
---|
91 | if (mCursor >= 0 && mCursor < (PRInt32)cnt) {
|
---|
92 | *aItem = mArray->ElementAt(mCursor);
|
---|
93 | return NS_OK;
|
---|
94 | }
|
---|
95 | return NS_ERROR_FAILURE;
|
---|
96 | }
|
---|
97 |
|
---|
98 | NS_IMETHODIMP
|
---|
99 | nsSupportsArrayEnumerator::IsDone()
|
---|
100 | {
|
---|
101 | PRUint32 cnt;
|
---|
102 | nsresult rv = mArray->Count(&cnt);
|
---|
103 | if (NS_FAILED(rv)) return rv;
|
---|
104 | return (mCursor >= 0 && mCursor < (PRInt32)cnt)
|
---|
105 | ? NS_ENUMERATOR_FALSE : NS_OK;
|
---|
106 | }
|
---|
107 |
|
---|
108 | ////////////////////////////////////////////////////////////////////////////////
|
---|
109 |
|
---|
110 | NS_IMETHODIMP
|
---|
111 | nsSupportsArrayEnumerator::Last()
|
---|
112 | {
|
---|
113 | PRUint32 cnt;
|
---|
114 | nsresult rv = mArray->Count(&cnt);
|
---|
115 | if (NS_FAILED(rv)) return rv;
|
---|
116 | mCursor = cnt - 1;
|
---|
117 | return NS_OK;
|
---|
118 | }
|
---|
119 |
|
---|
120 | NS_IMETHODIMP
|
---|
121 | nsSupportsArrayEnumerator::Prev()
|
---|
122 | {
|
---|
123 | if (mCursor >= 0)
|
---|
124 | --mCursor;
|
---|
125 | if (mCursor >= 0)
|
---|
126 | return NS_OK;
|
---|
127 | else
|
---|
128 | return NS_ERROR_FAILURE;
|
---|
129 | }
|
---|
130 |
|
---|
131 | ////////////////////////////////////////////////////////////////////////////////
|
---|
132 |
|
---|
133 | NS_COM nsresult
|
---|
134 | NS_NewISupportsArrayEnumerator(nsISupportsArray* array,
|
---|
135 | nsIBidirectionalEnumerator* *aInstancePtrResult)
|
---|
136 | {
|
---|
137 | if (aInstancePtrResult == 0)
|
---|
138 | return NS_ERROR_NULL_POINTER;
|
---|
139 | nsSupportsArrayEnumerator* e = new nsSupportsArrayEnumerator(array);
|
---|
140 | if (e == 0)
|
---|
141 | return NS_ERROR_OUT_OF_MEMORY;
|
---|
142 | NS_ADDREF(e);
|
---|
143 | *aInstancePtrResult = e;
|
---|
144 | return NS_OK;
|
---|
145 | }
|
---|
146 |
|
---|
147 | ////////////////////////////////////////////////////////////////////////////////
|
---|
148 |
|
---|