PageRenderTime 36ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/gecko_api/include/nsCOMArray.h

http://firefox-mac-pdf.googlecode.com/
C Header | 274 lines | 127 code | 45 blank | 102 comment | 0 complexity | fa11b6b39fae240c8b9056f506b92401 MD5 | raw 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. #ifndef nsCOMArray_h__
  39. #define nsCOMArray_h__
  40. #include "nsVoidArray.h"
  41. #include "nsISupports.h"
  42. // See below for the definition of nsCOMArray<T>
  43. // a class that's nsISupports-specific, so that we can contain the
  44. // work of this class in the XPCOM dll
  45. class NS_COM_GLUE nsCOMArray_base
  46. {
  47. friend class nsArray;
  48. protected:
  49. nsCOMArray_base() {}
  50. nsCOMArray_base(PRInt32 aCount) : mArray(aCount) {}
  51. nsCOMArray_base(const nsCOMArray_base& other);
  52. ~nsCOMArray_base();
  53. PRInt32 IndexOf(nsISupports* aObject) const {
  54. return mArray.IndexOf(aObject);
  55. }
  56. PRInt32 IndexOfObject(nsISupports* aObject) const;
  57. PRBool EnumerateForwards(nsVoidArrayEnumFunc aFunc, void* aData) {
  58. return mArray.EnumerateForwards(aFunc, aData);
  59. }
  60. PRBool EnumerateBackwards(nsVoidArrayEnumFunc aFunc, void* aData) {
  61. return mArray.EnumerateBackwards(aFunc, aData);
  62. }
  63. void Sort(nsVoidArrayComparatorFunc aFunc, void* aData) {
  64. mArray.Sort(aFunc, aData);
  65. }
  66. // any method which is not a direct forward to mArray should
  67. // avoid inline bodies, so that the compiler doesn't inline them
  68. // all over the place
  69. void Clear();
  70. PRBool InsertObjectAt(nsISupports* aObject, PRInt32 aIndex);
  71. PRBool InsertObjectsAt(const nsCOMArray_base& aObjects, PRInt32 aIndex);
  72. PRBool ReplaceObjectAt(nsISupports* aObject, PRInt32 aIndex);
  73. PRBool AppendObject(nsISupports *aObject) {
  74. return InsertObjectAt(aObject, Count());
  75. }
  76. PRBool AppendObjects(const nsCOMArray_base& aObjects) {
  77. return InsertObjectsAt(aObjects, Count());
  78. }
  79. PRBool RemoveObject(nsISupports *aObject);
  80. PRBool RemoveObjectAt(PRInt32 aIndex);
  81. public:
  82. // override nsVoidArray stuff so that they can be accessed by
  83. // consumers of nsCOMArray
  84. PRInt32 Count() const {
  85. return mArray.Count();
  86. }
  87. nsISupports* ObjectAt(PRInt32 aIndex) const {
  88. return static_cast<nsISupports*>(mArray.FastElementAt(aIndex));
  89. }
  90. nsISupports* SafeObjectAt(PRInt32 aIndex) const {
  91. return static_cast<nsISupports*>(mArray.SafeElementAt(aIndex));
  92. }
  93. nsISupports* operator[](PRInt32 aIndex) const {
  94. return ObjectAt(aIndex);
  95. }
  96. // Ensures there is enough space to store a total of aCapacity objects.
  97. // This method never deletes any objects.
  98. PRBool SetCapacity(PRUint32 aCapacity) {
  99. return aCapacity > 0 ? mArray.SizeTo(static_cast<PRInt32>(aCapacity))
  100. : PR_TRUE;
  101. }
  102. private:
  103. // the actual storage
  104. nsVoidArray mArray;
  105. // don't implement these, defaults will muck with refcounts!
  106. nsCOMArray_base& operator=(const nsCOMArray_base& other);
  107. };
  108. // a non-XPCOM, refcounting array of XPCOM objects
  109. // used as a member variable or stack variable - this object is NOT
  110. // refcounted, but the objects that it holds are
  111. //
  112. // most of the read-only accessors like ObjectAt()/etc do NOT refcount
  113. // on the way out. This means that you can do one of two things:
  114. //
  115. // * does an addref, but holds onto a reference
  116. // nsCOMPtr<T> foo = array[i];
  117. //
  118. // * avoids the refcount, but foo might go stale if array[i] is ever
  119. // * modified/removed. Be careful not to NS_RELEASE(foo)!
  120. // T* foo = array[i];
  121. //
  122. // This array will accept null as an argument for any object, and will
  123. // store null in the array, just like nsVoidArray. But that also means
  124. // that methods like ObjectAt() may return null when referring to an
  125. // existing, but null entry in the array.
  126. template <class T>
  127. class nsCOMArray : public nsCOMArray_base
  128. {
  129. public:
  130. nsCOMArray() {}
  131. nsCOMArray(PRInt32 aCount) : nsCOMArray_base(aCount) {}
  132. // only to be used by trusted classes who are going to pass us the
  133. // right type!
  134. nsCOMArray(const nsCOMArray<T>& aOther) : nsCOMArray_base(aOther) { }
  135. ~nsCOMArray() {}
  136. // these do NOT refcount on the way out, for speed
  137. T* ObjectAt(PRInt32 aIndex) const {
  138. return static_cast<T*>(nsCOMArray_base::ObjectAt(aIndex));
  139. }
  140. // these do NOT refcount on the way out, for speed
  141. T* SafeObjectAt(PRInt32 aIndex) const {
  142. return static_cast<T*>(nsCOMArray_base::SafeObjectAt(aIndex));
  143. }
  144. // indexing operator for syntactic sugar
  145. T* operator[](PRInt32 aIndex) const {
  146. return ObjectAt(aIndex);
  147. }
  148. // index of the element in question.. does NOT refcount
  149. // note: this does not check COM object identity. Use
  150. // IndexOfObject() for that purpose
  151. PRInt32 IndexOf(T* aObject) const {
  152. return nsCOMArray_base::IndexOf(static_cast<nsISupports*>(aObject));
  153. }
  154. // index of the element in question.. be careful!
  155. // this is much slower than IndexOf() because it uses
  156. // QueryInterface to determine actual COM identity of the object
  157. // if you need to do this frequently then consider enforcing
  158. // COM object identity before adding/comparing elements
  159. PRInt32 IndexOfObject(T* aObject) const {
  160. return nsCOMArray_base::IndexOfObject(static_cast<nsISupports*>(aObject));
  161. }
  162. // inserts aObject at aIndex, shifting the objects at aIndex and
  163. // later to make space
  164. PRBool InsertObjectAt(T* aObject, PRInt32 aIndex) {
  165. return nsCOMArray_base::InsertObjectAt(static_cast<nsISupports*>(aObject), aIndex);
  166. }
  167. // inserts the objects from aObject at aIndex, shifting the
  168. // objects at aIndex and later to make space
  169. PRBool InsertObjectsAt(const nsCOMArray<T>& aObjects, PRInt32 aIndex) {
  170. return nsCOMArray_base::InsertObjectsAt(aObjects, aIndex);
  171. }
  172. // replaces an existing element. Warning: if the array grows,
  173. // the newly created entries will all be null
  174. PRBool ReplaceObjectAt(T* aObject, PRInt32 aIndex) {
  175. return nsCOMArray_base::ReplaceObjectAt(static_cast<nsISupports*>(aObject), aIndex);
  176. }
  177. // override nsVoidArray stuff so that they can be accessed by
  178. // other methods
  179. // elements in the array (including null elements!)
  180. PRInt32 Count() const {
  181. return nsCOMArray_base::Count();
  182. }
  183. // remove all elements in the array, and call NS_RELEASE on each one
  184. void Clear() {
  185. nsCOMArray_base::Clear();
  186. }
  187. // Enumerator callback function. Return PR_FALSE to stop
  188. // Here's a more readable form:
  189. // PRBool PR_CALLBACK enumerate(T* aElement, void* aData)
  190. typedef PRBool (* PR_CALLBACK nsCOMArrayEnumFunc)
  191. (T* aElement, void *aData);
  192. // enumerate through the array with a callback.
  193. PRBool EnumerateForwards(nsCOMArrayEnumFunc aFunc, void* aData) {
  194. return nsCOMArray_base::EnumerateForwards(nsVoidArrayEnumFunc(aFunc),
  195. aData);
  196. }
  197. PRBool EnumerateBackwards(nsCOMArrayEnumFunc aFunc, void* aData) {
  198. return nsCOMArray_base::EnumerateBackwards(nsVoidArrayEnumFunc(aFunc),
  199. aData);
  200. }
  201. typedef int (* PR_CALLBACK nsCOMArrayComparatorFunc)
  202. (T* aElement1, T* aElement2, void* aData);
  203. void Sort(nsCOMArrayComparatorFunc aFunc, void* aData) {
  204. nsCOMArray_base::Sort(nsVoidArrayComparatorFunc(aFunc), aData);
  205. }
  206. // append an object, growing the array as necessary
  207. PRBool AppendObject(T *aObject) {
  208. return nsCOMArray_base::AppendObject(static_cast<nsISupports*>(aObject));
  209. }
  210. // append objects, growing the array as necessary
  211. PRBool AppendObjects(const nsCOMArray<T>& aObjects) {
  212. return nsCOMArray_base::AppendObjects(aObjects);
  213. }
  214. // remove the first instance of the given object and shrink the
  215. // array as necessary
  216. // Warning: if you pass null here, it will remove the first null element
  217. PRBool RemoveObject(T *aObject) {
  218. return nsCOMArray_base::RemoveObject(static_cast<nsISupports*>(aObject));
  219. }
  220. // remove an element at a specific position, shrinking the array
  221. // as necessary
  222. PRBool RemoveObjectAt(PRInt32 aIndex) {
  223. return nsCOMArray_base::RemoveObjectAt(aIndex);
  224. }
  225. private:
  226. // don't implement these!
  227. nsCOMArray<T>& operator=(const nsCOMArray<T>& other);
  228. };
  229. #endif