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
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/
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
15 * The Original Code is a COM aware array class.
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.
23 * Alec Flett <alecf@netscape.com>
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.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsCOMArray.h"
42 static PRBool
ReleaseObjects(void* aElement
, void*);
44 // implementations of non-trivial methods in nsCOMArray_base
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 nsCOMArray_base::nsCOMArray_base(const nsCOMArray_base
& aOther
)
51 // make sure we do only one allocation
52 mArray
.SizeTo(aOther
.Count());
53 AppendObjects(aOther
);
56 nsCOMArray_base::~nsCOMArray_base()
58 PRInt32 count
= Count(), i
;
59 for (i
= 0; i
< count
; ++i
) {
60 nsISupports
* obj
= ObjectAt(i
);
66 nsCOMArray_base::IndexOfObject(nsISupports
* aObject
) const {
67 nsCOMPtr
<nsISupports
> supports
= do_QueryInterface(aObject
);
68 NS_ENSURE_TRUE(supports
, -1);
72 count
= mArray
.Count();
73 for (i
= 0; i
< count
; ++i
) {
74 nsCOMPtr
<nsISupports
> arrayItem
=
75 do_QueryInterface(reinterpret_cast<nsISupports
*>(mArray
.ElementAt(i
)));
76 if (arrayItem
== supports
) {
85 nsCOMArray_base::InsertObjectAt(nsISupports
* aObject
, PRInt32 aIndex
) {
86 PRBool result
= mArray
.InsertElementAt(aObject
, aIndex
);
88 NS_IF_ADDREF(aObject
);
93 nsCOMArray_base::InsertObjectsAt(const nsCOMArray_base
& aObjects
, PRInt32 aIndex
) {
94 PRBool result
= mArray
.InsertElementsAt(aObjects
.mArray
, aIndex
);
96 // need to addref all these
97 PRInt32 count
= aObjects
.Count();
98 for (PRInt32 i
= 0; i
< count
; ++i
) {
99 NS_IF_ADDREF(aObjects
.ObjectAt(i
));
106 nsCOMArray_base::ReplaceObjectAt(nsISupports
* aObject
, PRInt32 aIndex
)
108 // its ok if oldObject is null here
109 nsISupports
*oldObject
=
110 reinterpret_cast<nsISupports
*>(mArray
.SafeElementAt(aIndex
));
112 PRBool result
= mArray
.ReplaceElementAt(aObject
, aIndex
);
114 // ReplaceElementAt could fail, such as if the array grows
115 // so only release the existing object if the replacement succeeded
117 // Make sure to addref first, in case aObject == oldObject
118 NS_IF_ADDREF(aObject
);
119 NS_IF_RELEASE(oldObject
);
125 nsCOMArray_base::RemoveObject(nsISupports
*aObject
)
127 PRBool result
= mArray
.RemoveElement(aObject
);
129 NS_IF_RELEASE(aObject
);
134 nsCOMArray_base::RemoveObjectAt(PRInt32 aIndex
)
136 if (PRUint32(aIndex
) < PRUint32(Count())) {
137 nsISupports
* element
= ObjectAt(aIndex
);
138 NS_IF_RELEASE(element
);
140 return mArray
.RemoveElementAt(aIndex
);
146 // useful for destructors
148 ReleaseObjects(void* aElement
, void*)
150 nsISupports
* element
= static_cast<nsISupports
*>(aElement
);
151 NS_IF_RELEASE(element
);
156 nsCOMArray_base::Clear()
158 mArray
.EnumerateForwards(ReleaseObjects
, nsnull
);