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 0 : nsCOMArray_base::nsCOMArray_base(const nsCOMArray_base& aOther)
50 : {
51 : // make sure we do only one allocation
52 0 : mArray.SizeTo(aOther.Count());
53 0 : AppendObjects(aOther);
54 0 : }
55 :
56 2788 : nsCOMArray_base::~nsCOMArray_base()
57 : {
58 1394 : Clear();
59 1394 : }
60 :
61 : PRInt32
62 0 : nsCOMArray_base::IndexOfObject(nsISupports* aObject) const {
63 0 : nsCOMPtr<nsISupports> supports = do_QueryInterface(aObject);
64 0 : NS_ENSURE_TRUE(supports, -1);
65 :
66 : PRInt32 i, count;
67 0 : PRInt32 retval = -1;
68 0 : count = mArray.Count();
69 0 : for (i = 0; i < count; ++i) {
70 : nsCOMPtr<nsISupports> arrayItem =
71 0 : do_QueryInterface(reinterpret_cast<nsISupports*>(mArray.ElementAt(i)));
72 0 : if (arrayItem == supports) {
73 0 : retval = i;
74 : break;
75 : }
76 : }
77 0 : return retval;
78 : }
79 :
80 : bool
81 1466 : nsCOMArray_base::InsertObjectAt(nsISupports* aObject, PRInt32 aIndex) {
82 1466 : bool result = mArray.InsertElementAt(aObject, aIndex);
83 1466 : if (result)
84 1466 : NS_IF_ADDREF(aObject);
85 1466 : return result;
86 : }
87 :
88 : bool
89 0 : nsCOMArray_base::InsertObjectsAt(const nsCOMArray_base& aObjects, PRInt32 aIndex) {
90 0 : bool result = mArray.InsertElementsAt(aObjects.mArray, aIndex);
91 0 : if (result) {
92 : // need to addref all these
93 0 : PRInt32 count = aObjects.Count();
94 0 : for (PRInt32 i = 0; i < count; ++i) {
95 0 : NS_IF_ADDREF(aObjects.ObjectAt(i));
96 : }
97 : }
98 0 : return result;
99 : }
100 :
101 : bool
102 0 : nsCOMArray_base::ReplaceObjectAt(nsISupports* aObject, PRInt32 aIndex)
103 : {
104 : // its ok if oldObject is null here
105 : nsISupports *oldObject =
106 0 : reinterpret_cast<nsISupports*>(mArray.SafeElementAt(aIndex));
107 :
108 0 : 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 0 : if (result) {
113 : // Make sure to addref first, in case aObject == oldObject
114 0 : NS_IF_ADDREF(aObject);
115 0 : NS_IF_RELEASE(oldObject);
116 : }
117 0 : return result;
118 : }
119 :
120 : bool
121 41 : nsCOMArray_base::RemoveObject(nsISupports *aObject)
122 : {
123 41 : bool result = mArray.RemoveElement(aObject);
124 41 : if (result)
125 1 : NS_IF_RELEASE(aObject);
126 41 : return result;
127 : }
128 :
129 : bool
130 1 : nsCOMArray_base::RemoveObjectAt(PRInt32 aIndex)
131 : {
132 1 : if (PRUint32(aIndex) < PRUint32(Count())) {
133 1 : nsISupports* element = ObjectAt(aIndex);
134 :
135 1 : bool result = mArray.RemoveElementAt(aIndex);
136 1 : NS_IF_RELEASE(element);
137 1 : return result;
138 : }
139 :
140 0 : return false;
141 : }
142 :
143 : bool
144 2 : nsCOMArray_base::RemoveObjectsAt(PRInt32 aIndex, PRInt32 aCount)
145 : {
146 2 : if (PRUint32(aIndex) + PRUint32(aCount) <= PRUint32(Count())) {
147 4 : nsVoidArray elementsToDestroy(aCount);
148 6 : for (PRInt32 i = 0; i < aCount; ++i) {
149 4 : elementsToDestroy.InsertElementAt(mArray.FastElementAt(aIndex + i), i);
150 : }
151 2 : bool result = mArray.RemoveElementsAt(aIndex, aCount);
152 6 : for (PRInt32 i = 0; i < aCount; ++i) {
153 4 : nsISupports* element = static_cast<nsISupports*> (elementsToDestroy.FastElementAt(i));
154 4 : NS_IF_RELEASE(element);
155 : }
156 2 : return result;
157 : }
158 :
159 0 : return false;
160 : }
161 :
162 : // useful for destructors
163 : bool
164 1480 : ReleaseObjects(void* aElement, void*)
165 : {
166 1480 : nsISupports* element = static_cast<nsISupports*>(aElement);
167 1480 : NS_IF_RELEASE(element);
168 1480 : return true;
169 : }
170 :
171 : void
172 1395 : nsCOMArray_base::Clear()
173 : {
174 2790 : nsAutoVoidArray objects;
175 1395 : objects = mArray;
176 1395 : mArray.Clear();
177 1395 : objects.EnumerateForwards(ReleaseObjects, nsnull);
178 1395 : }
179 :
180 : bool
181 3 : nsCOMArray_base::SetCount(PRInt32 aNewCount)
182 : {
183 3 : NS_ASSERTION(aNewCount >= 0,"SetCount(negative index)");
184 3 : if (aNewCount < 0)
185 0 : return false;
186 :
187 3 : PRInt32 count = Count(), i;
188 6 : nsAutoVoidArray objects;
189 3 : if (count > aNewCount) {
190 2 : objects.SetCount(count - aNewCount);
191 22 : for (i = aNewCount; i < count; ++i) {
192 20 : objects.ReplaceElementAt(ObjectAt(i), i - aNewCount);
193 : }
194 : }
195 3 : bool result = mArray.SetCount(aNewCount);
196 3 : objects.EnumerateForwards(ReleaseObjects, nsnull);
197 3 : return result;
198 : }
199 :
|