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 306 : nsSupportsArrayEnumerator::nsSupportsArrayEnumerator(nsISupportsArray* array)
42 306 : : mArray(array), mCursor(0)
43 : {
44 306 : NS_ASSERTION(array, "null array");
45 306 : NS_ADDREF(mArray);
46 306 : }
47 :
48 306 : nsSupportsArrayEnumerator::~nsSupportsArrayEnumerator()
49 : {
50 306 : NS_RELEASE(mArray);
51 306 : }
52 :
53 1836 : NS_IMPL_ISUPPORTS2(nsSupportsArrayEnumerator, nsIBidirectionalEnumerator, nsIEnumerator)
54 :
55 : NS_IMETHODIMP
56 306 : nsSupportsArrayEnumerator::First()
57 : {
58 306 : mCursor = 0;
59 : PRUint32 cnt;
60 306 : nsresult rv = mArray->Count(&cnt);
61 306 : if (NS_FAILED(rv)) return rv;
62 306 : PRInt32 end = (PRInt32)cnt;
63 306 : if (mCursor < end)
64 306 : return NS_OK;
65 : else
66 0 : return NS_ERROR_FAILURE;
67 : }
68 :
69 : NS_IMETHODIMP
70 130050 : nsSupportsArrayEnumerator::Next()
71 : {
72 : PRUint32 cnt;
73 130050 : nsresult rv = mArray->Count(&cnt);
74 130050 : if (NS_FAILED(rv)) return rv;
75 130050 : PRInt32 end = (PRInt32)cnt;
76 130050 : if (mCursor < end) // don't count upward forever
77 130050 : mCursor++;
78 130050 : if (mCursor < end)
79 129744 : return NS_OK;
80 : else
81 306 : return NS_ERROR_FAILURE;
82 : }
83 :
84 : NS_IMETHODIMP
85 130050 : nsSupportsArrayEnumerator::CurrentItem(nsISupports **aItem)
86 : {
87 130050 : NS_ASSERTION(aItem, "null out parameter");
88 : PRUint32 cnt;
89 130050 : nsresult rv = mArray->Count(&cnt);
90 130050 : if (NS_FAILED(rv)) return rv;
91 130050 : if (mCursor >= 0 && mCursor < (PRInt32)cnt) {
92 130050 : *aItem = mArray->ElementAt(mCursor);
93 130050 : return NS_OK;
94 : }
95 0 : return NS_ERROR_FAILURE;
96 : }
97 :
98 : NS_IMETHODIMP
99 130356 : nsSupportsArrayEnumerator::IsDone()
100 : {
101 : PRUint32 cnt;
102 130356 : nsresult rv = mArray->Count(&cnt);
103 130356 : if (NS_FAILED(rv)) return rv;
104 : return (mCursor >= 0 && mCursor < (PRInt32)cnt)
105 130356 : ? NS_ENUMERATOR_FALSE : NS_OK;
106 : }
107 :
108 : ////////////////////////////////////////////////////////////////////////////////
109 :
110 : NS_IMETHODIMP
111 0 : nsSupportsArrayEnumerator::Last()
112 : {
113 : PRUint32 cnt;
114 0 : nsresult rv = mArray->Count(&cnt);
115 0 : if (NS_FAILED(rv)) return rv;
116 0 : mCursor = cnt - 1;
117 0 : return NS_OK;
118 : }
119 :
120 : NS_IMETHODIMP
121 0 : nsSupportsArrayEnumerator::Prev()
122 : {
123 0 : if (mCursor >= 0)
124 0 : --mCursor;
125 0 : if (mCursor >= 0)
126 0 : return NS_OK;
127 : else
128 0 : return NS_ERROR_FAILURE;
129 : }
130 :
131 : ////////////////////////////////////////////////////////////////////////////////
132 :
133 : nsresult
134 0 : NS_NewISupportsArrayEnumerator(nsISupportsArray* array,
135 : nsIBidirectionalEnumerator* *aInstancePtrResult)
136 : {
137 0 : if (aInstancePtrResult == 0)
138 0 : return NS_ERROR_NULL_POINTER;
139 0 : nsSupportsArrayEnumerator* e = new nsSupportsArrayEnumerator(array);
140 0 : if (e == 0)
141 0 : return NS_ERROR_OUT_OF_MEMORY;
142 0 : NS_ADDREF(e);
143 0 : *aInstancePtrResult = e;
144 0 : return NS_OK;
145 : }
146 :
147 : ////////////////////////////////////////////////////////////////////////////////
148 :
|