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 : #ifndef nsSupportsArray_h__
39 : #define nsSupportsArray_h__
40 :
41 : //#define DEBUG_SUPPORTSARRAY 1
42 :
43 : #include "nsISupportsArray.h"
44 :
45 : static const PRUint32 kAutoArraySize = 8;
46 :
47 : class nsSupportsArray : public nsISupportsArray {
48 : public:
49 : nsSupportsArray(void);
50 : ~nsSupportsArray(void); // nonvirtual since we're not subclassed
51 :
52 : static nsresult
53 : Create(nsISupports *aOuter, REFNSIID aIID, void **aResult);
54 :
55 : NS_DECL_ISUPPORTS
56 :
57 : NS_DECL_NSISERIALIZABLE
58 :
59 : // nsICollection methods:
60 391481 : NS_IMETHOD Count(PRUint32 *result) { *result = mCount; return NS_OK; }
61 0 : NS_IMETHOD GetElementAt(PRUint32 aIndex, nsISupports* *result) {
62 0 : *result = ElementAt(aIndex);
63 0 : return NS_OK;
64 : }
65 0 : NS_IMETHOD QueryElementAt(PRUint32 aIndex, const nsIID & aIID, void * *aResult) {
66 0 : if (aIndex < mCount) {
67 0 : nsISupports* element = mArray[aIndex];
68 0 : if (nsnull != element)
69 0 : return element->QueryInterface(aIID, aResult);
70 : }
71 0 : return NS_ERROR_FAILURE;
72 : }
73 0 : NS_IMETHOD SetElementAt(PRUint32 aIndex, nsISupports* value) {
74 0 : return ReplaceElementAt(value, aIndex) ? NS_OK : NS_ERROR_FAILURE;
75 : }
76 137303 : NS_IMETHOD AppendElement(nsISupports *aElement) {
77 137303 : return InsertElementAt(aElement, mCount)/* ? NS_OK : NS_ERROR_FAILURE*/;
78 : }
79 : // XXX this is badly named - should be RemoveFirstElement
80 35 : NS_IMETHOD RemoveElement(nsISupports *aElement) {
81 35 : return RemoveElement(aElement, 0)/* ? NS_OK : NS_ERROR_FAILURE*/;
82 : }
83 : NS_IMETHOD_(bool) MoveElement(PRInt32 aFrom, PRInt32 aTo);
84 : NS_IMETHOD Enumerate(nsIEnumerator* *result);
85 : NS_IMETHOD Clear(void);
86 :
87 : // nsISupportsArray methods:
88 : NS_IMETHOD_(bool) Equals(const nsISupportsArray* aOther);
89 :
90 : NS_IMETHOD_(nsISupports*) ElementAt(PRUint32 aIndex);
91 :
92 : NS_IMETHOD_(PRInt32) IndexOf(const nsISupports* aPossibleElement);
93 : NS_IMETHOD_(PRInt32) IndexOfStartingAt(const nsISupports* aPossibleElement,
94 : PRUint32 aStartIndex = 0);
95 : NS_IMETHOD_(PRInt32) LastIndexOf(const nsISupports* aPossibleElement);
96 :
97 0 : NS_IMETHOD GetIndexOf(nsISupports *aPossibleElement, PRInt32 *_retval) {
98 0 : *_retval = IndexOf(aPossibleElement);
99 0 : return NS_OK;
100 : }
101 :
102 0 : NS_IMETHOD GetIndexOfStartingAt(nsISupports *aPossibleElement,
103 : PRUint32 aStartIndex, PRInt32 *_retval) {
104 0 : *_retval = IndexOfStartingAt(aPossibleElement, aStartIndex);
105 0 : return NS_OK;
106 : }
107 :
108 0 : NS_IMETHOD GetLastIndexOf(nsISupports *aPossibleElement, PRInt32 *_retval) {
109 0 : *_retval = LastIndexOf(aPossibleElement);
110 0 : return NS_OK;
111 : }
112 :
113 : NS_IMETHOD_(bool) InsertElementAt(nsISupports* aElement, PRUint32 aIndex);
114 :
115 : NS_IMETHOD_(bool) ReplaceElementAt(nsISupports* aElement, PRUint32 aIndex);
116 :
117 35 : NS_IMETHOD_(bool) RemoveElementAt(PRUint32 aIndex) {
118 35 : return RemoveElementsAt(aIndex,1);
119 : }
120 : NS_IMETHOD_(bool) RemoveElement(const nsISupports* aElement, PRUint32 aStartIndex = 0);
121 : NS_IMETHOD_(bool) RemoveLastElement(const nsISupports* aElement);
122 :
123 0 : NS_IMETHOD DeleteLastElement(nsISupports *aElement) {
124 0 : return (RemoveLastElement(aElement) ? NS_OK : NS_ERROR_FAILURE);
125 : }
126 :
127 0 : NS_IMETHOD DeleteElementAt(PRUint32 aIndex) {
128 0 : return (RemoveElementAt(aIndex) ? NS_OK : NS_ERROR_FAILURE);
129 : }
130 :
131 0 : NS_IMETHOD_(bool) AppendElements(nsISupportsArray* aElements) {
132 0 : return InsertElementsAt(aElements,mCount);
133 : }
134 :
135 : NS_IMETHOD Compact(void);
136 :
137 : NS_IMETHOD_(bool) EnumerateForwards(nsISupportsArrayEnumFunc aFunc, void* aData);
138 : NS_IMETHOD_(bool) EnumerateBackwards(nsISupportsArrayEnumFunc aFunc, void* aData);
139 :
140 : NS_IMETHOD Clone(nsISupportsArray **_retval);
141 :
142 : NS_IMETHOD_(bool) InsertElementsAt(nsISupportsArray *aOther, PRUint32 aIndex);
143 :
144 : NS_IMETHOD_(bool) RemoveElementsAt(PRUint32 aIndex, PRUint32 aCount);
145 :
146 : NS_IMETHOD_(bool) SizeTo(PRInt32 aSize);
147 : protected:
148 : void DeleteArray(void);
149 :
150 : NS_IMETHOD_(bool) GrowArrayBy(PRInt32 aGrowBy);
151 :
152 : nsISupports** mArray;
153 : PRUint32 mArraySize;
154 : PRUint32 mCount;
155 : nsISupports* mAutoArray[kAutoArraySize];
156 : #if DEBUG_SUPPORTSARRAY
157 : PRUint32 mMaxCount;
158 : PRUint32 mMaxSize;
159 : #endif
160 :
161 : private:
162 : // Copy constructors are not allowed
163 : nsSupportsArray(const nsISupportsArray& other);
164 : };
165 :
166 : #endif // nsSupportsArray_h__
|