Back to index

lightning-sunbird  0.9+nobinonly
nsSupportsArray.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef nsSupportsArray_h__
00039 #define nsSupportsArray_h__
00040 
00041 //#define DEBUG_SUPPORTSARRAY 1
00042 
00043 #include "nsISupportsArray.h"
00044 
00045 static const PRUint32 kAutoArraySize = 8;
00046 
00047 #undef  IMETHOD_VISIBILITY
00048 #define IMETHOD_VISIBILITY NS_VISIBILITY_DEFAULT
00049 
00050 class NS_COM nsSupportsArray : public nsISupportsArray {
00051 public:
00052   nsSupportsArray(void);
00053   ~nsSupportsArray(void); // nonvirtual since we're not subclassed
00054 
00055   static NS_METHOD
00056   Create(nsISupports *aOuter, REFNSIID aIID, void **aResult);
00057 
00058   NS_DECL_ISUPPORTS
00059 
00060   NS_DECL_NSISERIALIZABLE
00061 
00062   // nsICollection methods:
00063   NS_IMETHOD Count(PRUint32 *result) { *result = mCount; return NS_OK; }
00064   NS_IMETHOD GetElementAt(PRUint32 aIndex, nsISupports* *result) {
00065     *result = ElementAt(aIndex);
00066     return NS_OK;
00067   }
00068   NS_IMETHOD QueryElementAt(PRUint32 aIndex, const nsIID & aIID, void * *aResult) {
00069     if (aIndex < mCount) {
00070       nsISupports* element = mArray[aIndex];
00071       if (nsnull != element)
00072         return element->QueryInterface(aIID, aResult);
00073     }
00074     return NS_ERROR_FAILURE;
00075   }
00076   NS_IMETHOD SetElementAt(PRUint32 aIndex, nsISupports* value) {
00077     return ReplaceElementAt(value, aIndex) ? NS_OK : NS_ERROR_FAILURE;
00078   }
00079   NS_IMETHOD AppendElement(nsISupports *aElement) {
00080     return InsertElementAt(aElement, mCount)/* ? NS_OK : NS_ERROR_FAILURE*/;
00081   }
00082   // XXX this is badly named - should be RemoveFirstElement
00083   NS_IMETHOD RemoveElement(nsISupports *aElement) {
00084     return RemoveElement(aElement, 0)/* ? NS_OK : NS_ERROR_FAILURE*/;
00085   }
00086   NS_IMETHOD_(PRBool) MoveElement(PRInt32 aFrom, PRInt32 aTo);
00087   NS_IMETHOD Enumerate(nsIEnumerator* *result);
00088   NS_IMETHOD Clear(void);
00089 
00090   // nsISupportsArray methods:
00091   NS_IMETHOD_(PRBool) Equals(const nsISupportsArray* aOther);
00092 
00093   NS_IMETHOD_(nsISupports*) ElementAt(PRUint32 aIndex);
00094 
00095   NS_IMETHOD_(PRInt32) IndexOf(const nsISupports* aPossibleElement);
00096   NS_IMETHOD_(PRInt32) IndexOfStartingAt(const nsISupports* aPossibleElement,
00097                                          PRUint32 aStartIndex = 0);
00098   NS_IMETHOD_(PRInt32) LastIndexOf(const nsISupports* aPossibleElement);
00099 
00100   NS_IMETHOD GetIndexOf(nsISupports *aPossibleElement, PRInt32 *_retval) {
00101     *_retval = IndexOf(aPossibleElement);
00102     return NS_OK;
00103   }
00104   
00105   NS_IMETHOD GetIndexOfStartingAt(nsISupports *aPossibleElement,
00106                                   PRUint32 aStartIndex, PRInt32 *_retval) {
00107     *_retval = IndexOfStartingAt(aPossibleElement, aStartIndex);
00108     return NS_OK;
00109   }
00110   
00111   NS_IMETHOD GetLastIndexOf(nsISupports *aPossibleElement, PRInt32 *_retval) {
00112     *_retval = LastIndexOf(aPossibleElement);
00113     return NS_OK;
00114   }
00115   
00116   NS_IMETHOD_(PRBool) InsertElementAt(nsISupports* aElement, PRUint32 aIndex);
00117 
00118   NS_IMETHOD_(PRBool) ReplaceElementAt(nsISupports* aElement, PRUint32 aIndex);
00119 
00120   NS_IMETHOD_(PRBool) RemoveElementAt(PRUint32 aIndex) {
00121     return RemoveElementsAt(aIndex,1);
00122   }
00123   NS_IMETHOD_(PRBool) RemoveElement(const nsISupports* aElement, PRUint32 aStartIndex = 0);
00124   NS_IMETHOD_(PRBool) RemoveLastElement(const nsISupports* aElement);
00125 
00126   NS_IMETHOD DeleteLastElement(nsISupports *aElement) {
00127     return (RemoveLastElement(aElement) ? NS_OK : NS_ERROR_FAILURE);
00128   }
00129   
00130   NS_IMETHOD DeleteElementAt(PRUint32 aIndex) {
00131     return (RemoveElementAt(aIndex) ? NS_OK : NS_ERROR_FAILURE);
00132   }
00133   
00134   NS_IMETHOD_(PRBool) AppendElements(nsISupportsArray* aElements) {
00135     return InsertElementsAt(aElements,mCount);
00136   }
00137   
00138   NS_IMETHOD Compact(void);
00139 
00140   NS_IMETHOD_(PRBool) EnumerateForwards(nsISupportsArrayEnumFunc aFunc, void* aData);
00141   NS_IMETHOD_(PRBool) EnumerateBackwards(nsISupportsArrayEnumFunc aFunc, void* aData);
00142 
00143   NS_IMETHOD Clone(nsISupportsArray **_retval);
00144 
00145   NS_IMETHOD_(PRBool) InsertElementsAt(nsISupportsArray *aOther, PRUint32 aIndex);
00146 
00147   NS_IMETHOD_(PRBool) RemoveElementsAt(PRUint32 aIndex, PRUint32 aCount);
00148 
00149   NS_IMETHOD_(PRBool) SizeTo(PRInt32 aSize);
00150 protected:
00151   void DeleteArray(void);
00152 
00153   NS_IMETHOD_(PRBool) GrowArrayBy(PRInt32 aGrowBy);
00154 
00155   nsISupports** mArray;
00156   PRUint32 mArraySize;
00157   PRUint32 mCount;
00158   nsISupports*  mAutoArray[kAutoArraySize];
00159 #if DEBUG_SUPPORTSARRAY
00160   PRUint32 mMaxCount;
00161   PRUint32 mMaxSize;
00162 #endif
00163 
00164 private:
00165   // Copy constructors are not allowed
00166   nsSupportsArray(const nsISupportsArray& other);
00167 };
00168 
00169 #undef  IMETHOD_VISIBILITY
00170 #define IMETHOD_VISIBILITY NS_VISIBILITY_HIDDEN
00171 
00172 #endif // nsSupportsArray_h__