sbCOMArray.h
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 #ifndef sbCOMArray_h__
40 #define sbCOMArray_h__
41 
42 #include "nsVoidArray.h"
43 #include "nsISupports.h"
44 
45 // See below for the definition of sbCOMArray<T>
46 
47 // a class that's nsISupports-specific, so that we can contain the
48 // work of this class in the XPCOM dll
50 {
51  friend class sbArray;
52 protected:
54  sbCOMArray_base(PRInt32 aCount) : mArray(aCount) {}
55  sbCOMArray_base(const sbCOMArray_base& other);
57 
58  PRInt32 IndexOf(nsISupports* aObject) const {
59  return mArray.IndexOf(aObject);
60  }
61 
62  PRInt32 IndexOfObject(nsISupports* aObject) const;
63 
64  PRBool EnumerateForwards(nsVoidArrayEnumFunc aFunc, void* aData) {
65  return mArray.EnumerateForwards(aFunc, aData);
66  }
67 
68  PRBool EnumerateBackwards(nsVoidArrayEnumFunc aFunc, void* aData) {
69  return mArray.EnumerateBackwards(aFunc, aData);
70  }
71 
72  void Sort(nsVoidArrayComparatorFunc aFunc, void* aData) {
73  mArray.Sort(aFunc, aData);
74  }
75 
76  // any method which is not a direct forward to mArray should
77  // avoid inline bodies, so that the compiler doesn't inline them
78  // all over the place
79  void Clear();
80  PRBool InsertObjectAt(nsISupports* aObject, PRInt32 aIndex);
81  PRBool InsertObjectsAt(const sbCOMArray_base& aObjects, PRInt32 aIndex);
82  PRBool ReplaceObjectAt(nsISupports* aObject, PRInt32 aIndex);
83  PRBool AppendObject(nsISupports *aObject) {
84  return InsertObjectAt(aObject, Count());
85  }
86  PRBool AppendObjects(const sbCOMArray_base& aObjects) {
87  return InsertObjectsAt(aObjects, Count());
88  }
89  PRBool RemoveObject(nsISupports *aObject);
90  PRBool RemoveObjectAt(PRInt32 aIndex);
91 
92 public:
93  // override nsVoidArray stuff so that they can be accessed by
94  // consumers of nsCOMArray
95  PRInt32 Count() const {
96  return mArray.Count();
97  }
98 
99  nsISupports* ObjectAt(PRInt32 aIndex) const {
100  return static_cast<nsISupports*>(mArray.FastElementAt(aIndex));
101  }
102 
103  nsISupports* SafeObjectAt(PRInt32 aIndex) const {
104  return static_cast<nsISupports*>(mArray.SafeElementAt(aIndex));
105  }
106 
107  nsISupports* operator[](PRInt32 aIndex) const {
108  return ObjectAt(aIndex);
109  }
110 
111  // Ensures there is enough space to store a total of aCapacity objects.
112  // This method never deletes any objects.
113  PRBool SetCapacity(PRUint32 aCapacity) {
114  return aCapacity > 0 ? mArray.SizeTo(static_cast<PRInt32>(aCapacity))
115  : PR_TRUE;
116  }
117 
118 private:
119 
120  // the actual storage
121  nsVoidArray mArray;
122 
123  // don't implement these, defaults will muck with refcounts!
124  sbCOMArray_base& operator=(const sbCOMArray_base& other);
125 };
126 
127 #endif
PRBool AppendObjects(const sbCOMArray_base &aObjects)
Definition: sbCOMArray.h:86
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 EnumerateBackwards(nsVoidArrayEnumFunc aFunc, void *aData)
Definition: sbCOMArray.h:68
PRBool InsertObjectAt(nsISupports *aObject, PRInt32 aIndex)
Definition: sbCOMArray.cpp:85
sbCOMArray_base(PRInt32 aCount)
Definition: sbCOMArray.h:54
PRBool RemoveObjectAt(PRInt32 aIndex)
Definition: sbCOMArray.cpp:134
PRBool SetCapacity(PRUint32 aCapacity)
Definition: sbCOMArray.h:113
PRInt32 IndexOf(nsISupports *aObject) const
Definition: sbCOMArray.h:58
void Sort(nsVoidArrayComparatorFunc aFunc, void *aData)
Definition: sbCOMArray.h:72
PRBool InsertObjectsAt(const sbCOMArray_base &aObjects, PRInt32 aIndex)
Definition: sbCOMArray.cpp:93
PRBool ReplaceObjectAt(nsISupports *aObject, PRInt32 aIndex)
Definition: sbCOMArray.cpp:106
PRBool EnumerateForwards(nsVoidArrayEnumFunc aFunc, void *aData)
Definition: sbCOMArray.h:64
nsISupports * operator[](PRInt32 aIndex) const
Definition: sbCOMArray.h:107
PRBool AppendObject(nsISupports *aObject)
Definition: sbCOMArray.h:83
PRInt32 IndexOfObject(nsISupports *aObject) const
Definition: sbCOMArray.cpp:66
nsISupports * SafeObjectAt(PRInt32 aIndex) const
Definition: sbCOMArray.h:103
restoreHistoryPrecursor aCount
_updateTextAndScrollDataForFrame aData