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 a COM aware array class.
16 : *
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.
21 : *
22 : * Contributor(s):
23 : * Alec Flett <alecf@netscape.com>
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 "nsCOMArray.h"
40 : #include "nsCOMPtr.h"
41 :
42 : static bool ReleaseObjects(void* aElement, void*);
43 :
44 : // implementations of non-trivial methods in nsCOMArray_base
45 :
46 : // copy constructor - we can't just memcpy here, because
47 : // we have to make sure we own our own array buffer, and that each
48 : // object gets another AddRef()
49 1353 : nsCOMArray_base::nsCOMArray_base(const nsCOMArray_base& aOther)
50 : {
51 : // make sure we do only one allocation
52 1353 : mArray.SizeTo(aOther.Count());
53 1353 : AppendObjects(aOther);
54 1353 : }
55 :
56 520466 : nsCOMArray_base::~nsCOMArray_base()
57 : {
58 260233 : Clear();
59 260233 : }
60 :
61 : PRInt32
62 198 : nsCOMArray_base::IndexOfObject(nsISupports* aObject) const {
63 396 : nsCOMPtr<nsISupports> supports = do_QueryInterface(aObject);
64 198 : NS_ENSURE_TRUE(supports, -1);
65 :
66 : PRInt32 i, count;
67 198 : PRInt32 retval = -1;
68 198 : count = mArray.Count();
69 201 : for (i = 0; i < count; ++i) {
70 : nsCOMPtr<nsISupports> arrayItem =
71 6 : do_QueryInterface(reinterpret_cast<nsISupports*>(mArray.ElementAt(i)));
72 3 : if (arrayItem == supports) {
73 0 : retval = i;
74 : break;
75 : }
76 : }
77 198 : return retval;
78 : }
79 :
80 : bool
81 461598 : nsCOMArray_base::InsertObjectAt(nsISupports* aObject, PRInt32 aIndex) {
82 461598 : bool result = mArray.InsertElementAt(aObject, aIndex);
83 461597 : if (result)
84 461597 : NS_IF_ADDREF(aObject);
85 461598 : return result;
86 : }
87 :
88 : bool
89 4721 : nsCOMArray_base::InsertObjectsAt(const nsCOMArray_base& aObjects, PRInt32 aIndex) {
90 4721 : bool result = mArray.InsertElementsAt(aObjects.mArray, aIndex);
91 4721 : if (result) {
92 : // need to addref all these
93 4721 : PRInt32 count = aObjects.Count();
94 6265 : for (PRInt32 i = 0; i < count; ++i) {
95 1544 : NS_IF_ADDREF(aObjects.ObjectAt(i));
96 : }
97 : }
98 4721 : return result;
99 : }
100 :
101 : bool
102 238263 : nsCOMArray_base::ReplaceObjectAt(nsISupports* aObject, PRInt32 aIndex)
103 : {
104 : // its ok if oldObject is null here
105 : nsISupports *oldObject =
106 238263 : reinterpret_cast<nsISupports*>(mArray.SafeElementAt(aIndex));
107 :
108 238263 : bool result = mArray.ReplaceElementAt(aObject, aIndex);
109 :
110 : // ReplaceElementAt could fail, such as if the array grows
111 : // so only release the existing object if the replacement succeeded
112 238263 : if (result) {
113 : // Make sure to addref first, in case aObject == oldObject
114 238263 : NS_IF_ADDREF(aObject);
115 238263 : NS_IF_RELEASE(oldObject);
116 : }
117 238263 : return result;
118 : }
119 :
120 : bool
121 14061 : nsCOMArray_base::RemoveObject(nsISupports *aObject)
122 : {
123 14061 : bool result = mArray.RemoveElement(aObject);
124 14061 : if (result)
125 2069 : NS_IF_RELEASE(aObject);
126 14061 : return result;
127 : }
128 :
129 : bool
130 296 : nsCOMArray_base::RemoveObjectAt(PRInt32 aIndex)
131 : {
132 296 : if (PRUint32(aIndex) < PRUint32(Count())) {
133 296 : nsISupports* element = ObjectAt(aIndex);
134 :
135 296 : bool result = mArray.RemoveElementAt(aIndex);
136 296 : NS_IF_RELEASE(element);
137 296 : return result;
138 : }
139 :
140 0 : return false;
141 : }
142 :
143 : bool
144 0 : nsCOMArray_base::RemoveObjectsAt(PRInt32 aIndex, PRInt32 aCount)
145 : {
146 0 : if (PRUint32(aIndex) + PRUint32(aCount) <= PRUint32(Count())) {
147 0 : nsVoidArray elementsToDestroy(aCount);
148 0 : for (PRInt32 i = 0; i < aCount; ++i) {
149 0 : elementsToDestroy.InsertElementAt(mArray.FastElementAt(aIndex + i), i);
150 : }
151 0 : bool result = mArray.RemoveElementsAt(aIndex, aCount);
152 0 : for (PRInt32 i = 0; i < aCount; ++i) {
153 0 : nsISupports* element = static_cast<nsISupports*> (elementsToDestroy.FastElementAt(i));
154 0 : NS_IF_RELEASE(element);
155 : }
156 0 : return result;
157 : }
158 :
159 0 : return false;
160 : }
161 :
162 : // useful for destructors
163 : bool
164 698660 : ReleaseObjects(void* aElement, void*)
165 : {
166 698660 : nsISupports* element = static_cast<nsISupports*>(aElement);
167 698660 : NS_IF_RELEASE(element);
168 698660 : return true;
169 : }
170 :
171 : void
172 292388 : nsCOMArray_base::Clear()
173 : {
174 584774 : nsAutoVoidArray objects;
175 292388 : objects = mArray;
176 292386 : mArray.Clear();
177 292388 : objects.EnumerateForwards(ReleaseObjects, nsnull);
178 292388 : }
179 :
180 : bool
181 0 : nsCOMArray_base::SetCount(PRInt32 aNewCount)
182 : {
183 0 : NS_ASSERTION(aNewCount >= 0,"SetCount(negative index)");
184 0 : if (aNewCount < 0)
185 0 : return false;
186 :
187 0 : PRInt32 count = Count(), i;
188 0 : nsAutoVoidArray objects;
189 0 : if (count > aNewCount) {
190 0 : objects.SetCount(count - aNewCount);
191 0 : for (i = aNewCount; i < count; ++i) {
192 0 : objects.ReplaceElementAt(ObjectAt(i), i - aNewCount);
193 : }
194 : }
195 0 : bool result = mArray.SetCount(aNewCount);
196 0 : objects.EnumerateForwards(ReleaseObjects, nsnull);
197 0 : return result;
198 : }
199 :
|