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 <[email protected]>
|
---|
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 | PR_STATIC_CALLBACK(PRBool) 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 | nsCOMArray_base::nsCOMArray_base(const nsCOMArray_base& aOther)
|
---|
50 | {
|
---|
51 | // make sure we do only one allocation
|
---|
52 | mArray.SizeTo(aOther.Count());
|
---|
53 | AppendObjects(aOther);
|
---|
54 | }
|
---|
55 |
|
---|
56 | nsCOMArray_base::~nsCOMArray_base()
|
---|
57 | {
|
---|
58 | PRInt32 count = Count(), i;
|
---|
59 | for (i = 0; i < count; ++i) {
|
---|
60 | nsISupports* obj = ObjectAt(i);
|
---|
61 | NS_IF_RELEASE(obj);
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | PRInt32
|
---|
66 | nsCOMArray_base::IndexOfObject(nsISupports* aObject) const {
|
---|
67 | NS_ENSURE_TRUE(aObject, -1);
|
---|
68 | nsCOMPtr<nsISupports> supports = do_QueryInterface(aObject);
|
---|
69 | NS_ENSURE_TRUE(supports, -1);
|
---|
70 |
|
---|
71 | PRInt32 i, count;
|
---|
72 | PRInt32 retval = -1;
|
---|
73 | count = mArray.Count();
|
---|
74 | for (i = 0; i < count; ++i) {
|
---|
75 | nsCOMPtr<nsISupports> arrayItem =
|
---|
76 | do_QueryInterface(NS_REINTERPRET_CAST(nsISupports*,mArray.ElementAt(i)));
|
---|
77 | if (arrayItem == supports) {
|
---|
78 | retval = i;
|
---|
79 | break;
|
---|
80 | }
|
---|
81 | }
|
---|
82 | return retval;
|
---|
83 | }
|
---|
84 |
|
---|
85 | PRBool
|
---|
86 | nsCOMArray_base::InsertObjectAt(nsISupports* aObject, PRInt32 aIndex) {
|
---|
87 | PRBool result = mArray.InsertElementAt(aObject, aIndex);
|
---|
88 | if (result)
|
---|
89 | NS_IF_ADDREF(aObject);
|
---|
90 | return result;
|
---|
91 | }
|
---|
92 |
|
---|
93 | PRBool
|
---|
94 | nsCOMArray_base::InsertObjectsAt(const nsCOMArray_base& aObjects, PRInt32 aIndex) {
|
---|
95 | PRBool result = mArray.InsertElementsAt(aObjects.mArray, aIndex);
|
---|
96 | if (result) {
|
---|
97 | // need to addref all these
|
---|
98 | PRInt32 count = aObjects.Count();
|
---|
99 | for (PRInt32 i = 0; i < count; ++i) {
|
---|
100 | NS_IF_ADDREF(aObjects.ObjectAt(i));
|
---|
101 | }
|
---|
102 | }
|
---|
103 | return result;
|
---|
104 | }
|
---|
105 |
|
---|
106 | PRBool
|
---|
107 | nsCOMArray_base::ReplaceObjectAt(nsISupports* aObject, PRInt32 aIndex)
|
---|
108 | {
|
---|
109 | // its ok if oldObject is null here
|
---|
110 | nsISupports *oldObject =
|
---|
111 | NS_REINTERPRET_CAST(nsISupports*, mArray.SafeElementAt(aIndex));
|
---|
112 |
|
---|
113 | PRBool result = mArray.ReplaceElementAt(aObject, aIndex);
|
---|
114 |
|
---|
115 | // ReplaceElementAt could fail, such as if the array grows
|
---|
116 | // so only release the existing object if the replacement succeeded
|
---|
117 | if (result) {
|
---|
118 | // Make sure to addref first, in case aObject == oldObject
|
---|
119 | NS_IF_ADDREF(aObject);
|
---|
120 | NS_IF_RELEASE(oldObject);
|
---|
121 | }
|
---|
122 | return result;
|
---|
123 | }
|
---|
124 |
|
---|
125 | PRBool
|
---|
126 | nsCOMArray_base::RemoveObject(nsISupports *aObject)
|
---|
127 | {
|
---|
128 | PRBool result = mArray.RemoveElement(aObject);
|
---|
129 | if (result)
|
---|
130 | NS_IF_RELEASE(aObject);
|
---|
131 | return result;
|
---|
132 | }
|
---|
133 |
|
---|
134 | PRBool
|
---|
135 | nsCOMArray_base::RemoveObjectAt(PRInt32 aIndex)
|
---|
136 | {
|
---|
137 | nsISupports* element = ObjectAt(aIndex);
|
---|
138 | if (element) {
|
---|
139 | PRBool result = mArray.RemoveElementAt(aIndex);
|
---|
140 | if (result)
|
---|
141 | NS_IF_RELEASE(element);
|
---|
142 | return result;
|
---|
143 | }
|
---|
144 | return PR_FALSE;
|
---|
145 | }
|
---|
146 |
|
---|
147 | // useful for destructors
|
---|
148 | PRBool
|
---|
149 | ReleaseObjects(void* aElement, void*)
|
---|
150 | {
|
---|
151 | nsISupports* element = NS_STATIC_CAST(nsISupports*, aElement);
|
---|
152 | NS_IF_RELEASE(element);
|
---|
153 | return PR_TRUE;
|
---|
154 | }
|
---|
155 |
|
---|
156 | void
|
---|
157 | nsCOMArray_base::Clear()
|
---|
158 | {
|
---|
159 | mArray.EnumerateForwards(ReleaseObjects, nsnull);
|
---|
160 | mArray.Clear();
|
---|
161 | }
|
---|
162 |
|
---|