sbCOMArray.cpp
Go to the documentation of this file.
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 "sbCOMArray.h"
40 #include "nsCOMPtr.h"
41 
42 PR_STATIC_CALLBACK(PRBool) ReleaseObjects(void* aElement, void*);
43 
44 // implementations of non-trivial methods in sbCOMArray_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()
50 {
51  // make sure we do only one allocation
52  mArray.SizeTo(aOther.Count());
53  AppendObjects(aOther);
54 }
55 
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
67  nsCOMPtr<nsISupports> supports = do_QueryInterface(aObject);
68  NS_ENSURE_TRUE(supports, -1);
69 
70  PRInt32 i, count;
71  PRInt32 retval = -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) {
77  retval = i;
78  break;
79  }
80  }
81  return retval;
82 }
83 
84 PRBool
85 sbCOMArray_base::InsertObjectAt(nsISupports* aObject, PRInt32 aIndex) {
86  PRBool result = mArray.InsertElementAt(aObject, aIndex);
87  if (result)
88  NS_IF_ADDREF(aObject);
89  return result;
90 }
91 
92 PRBool
93 sbCOMArray_base::InsertObjectsAt(const sbCOMArray_base& aObjects, PRInt32 aIndex) {
94  PRBool result = mArray.InsertElementsAt(aObjects.mArray, aIndex);
95  if (result) {
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));
100  }
101  }
102  return result;
103 }
104 
105 PRBool
107 {
108  // its ok if oldObject is null here
109  nsISupports *oldObject =
110  reinterpret_cast<nsISupports*>(mArray.SafeElementAt(aIndex));
111 
112  PRBool result = mArray.ReplaceElementAt(aObject, aIndex);
113 
114  // ReplaceElementAt could fail, such as if the array grows
115  // so only release the existing object if the replacement succeeded
116  if (result) {
117  // Make sure to addref first, in case aObject == oldObject
118  NS_IF_ADDREF(aObject);
119  NS_IF_RELEASE(oldObject);
120  }
121  return result;
122 }
123 
124 PRBool
126 {
127  PRBool result = mArray.RemoveElement(aObject);
128  if (result)
129  NS_IF_RELEASE(aObject);
130  return result;
131 }
132 
133 PRBool
135 {
136  if (PRUint32(aIndex) < PRUint32(Count())) {
137  nsISupports* element = ObjectAt(aIndex);
138  NS_IF_RELEASE(element);
139 
140  return mArray.RemoveElementAt(aIndex);
141  }
142 
143  return PR_FALSE;
144 }
145 
146 // useful for destructors
147 PRBool
148 ReleaseObjects(void* aElement, void*)
149 {
150  nsISupports* element = static_cast<nsISupports*>(aElement);
151  NS_IF_RELEASE(element);
152  return PR_TRUE;
153 }
154 
155 void
157 {
158  mArray.EnumerateForwards(ReleaseObjects, nsnull);
159  mArray.Clear();
160 }
161 
nsISupports * ObjectAt(PRInt32 aIndex) const
Definition: sbCOMArray.h:99
PRBool RemoveObject(nsISupports *aObject)
Definition: sbCOMArray.cpp:125
PRInt32 Count() const
Definition: sbCOMArray.h:95
PRBool InsertObjectAt(nsISupports *aObject, PRInt32 aIndex)
Definition: sbCOMArray.cpp:85
PRBool RemoveObjectAt(PRInt32 aIndex)
Definition: sbCOMArray.cpp:134
var count
Definition: test_bug7406.js:32
PRBool InsertObjectsAt(const sbCOMArray_base &aObjects, PRInt32 aIndex)
Definition: sbCOMArray.cpp:93
PRBool ReplaceObjectAt(nsISupports *aObject, PRInt32 aIndex)
Definition: sbCOMArray.cpp:106
PR_STATIC_CALLBACK(PRBool) ReleaseObjects(void *aElement
PRInt32 IndexOfObject(nsISupports *aObject) const
Definition: sbCOMArray.cpp:66
PRBool ReleaseObjects(void *aElement, void *)
Definition: sbCOMArray.cpp:148
_getSelectedPageStyle s i