Back to index

lightning-sunbird  0.9+nobinonly
nsCOMArray.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 a COM aware array class.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corp.
00019  * Portions created by the Initial Developer are Copyright (C) 2002
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *  Alec Flett <alecf@netscape.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsCOMArray.h"
00040 #include "nsCOMPtr.h"
00041 
00042 PR_STATIC_CALLBACK(PRBool) ReleaseObjects(void* aElement, void*);
00043 
00044 // implementations of non-trivial methods in nsCOMArray_base
00045 
00046 // copy constructor - we can't just memcpy here, because
00047 // we have to make sure we own our own array buffer, and that each
00048 // object gets another AddRef()
00049 nsCOMArray_base::nsCOMArray_base(const nsCOMArray_base& aOther)
00050 {
00051     // make sure we do only one allocation
00052     mArray.SizeTo(aOther.Count());
00053     AppendObjects(aOther);
00054 }
00055 
00056 nsCOMArray_base::~nsCOMArray_base()
00057 {
00058     PRInt32 count = Count(), i;
00059     for (i = 0; i < count; ++i) {
00060         nsISupports* obj = ObjectAt(i);
00061         NS_IF_RELEASE(obj);
00062     }                        
00063 }
00064 
00065 PRInt32
00066 nsCOMArray_base::IndexOfObject(nsISupports* aObject) const {
00067     NS_ENSURE_TRUE(aObject, -1);
00068     nsCOMPtr<nsISupports> supports = do_QueryInterface(aObject);
00069     NS_ENSURE_TRUE(supports, -1);
00070 
00071     PRInt32 i, count;
00072     PRInt32 retval = -1;
00073     count = mArray.Count();
00074     for (i = 0; i < count; ++i) {
00075         nsCOMPtr<nsISupports> arrayItem =
00076             do_QueryInterface(NS_REINTERPRET_CAST(nsISupports*,mArray.ElementAt(i)));
00077         if (arrayItem == supports) {
00078             retval = i;
00079             break;
00080         }
00081     }
00082     return retval;
00083 }
00084 
00085 PRBool
00086 nsCOMArray_base::InsertObjectAt(nsISupports* aObject, PRInt32 aIndex) {
00087     PRBool result = mArray.InsertElementAt(aObject, aIndex);
00088     if (result)
00089         NS_IF_ADDREF(aObject);
00090     return result;
00091 }
00092 
00093 PRBool
00094 nsCOMArray_base::InsertObjectsAt(const nsCOMArray_base& aObjects, PRInt32 aIndex) {
00095     PRBool result = mArray.InsertElementsAt(aObjects.mArray, aIndex);
00096     if (result) {
00097         // need to addref all these
00098         PRInt32 count = aObjects.Count();
00099         for (PRInt32 i = 0; i < count; ++i) {
00100             NS_IF_ADDREF(aObjects.ObjectAt(i));
00101         }
00102     }
00103     return result;
00104 }
00105 
00106 PRBool
00107 nsCOMArray_base::ReplaceObjectAt(nsISupports* aObject, PRInt32 aIndex)
00108 {
00109     // its ok if oldObject is null here
00110     nsISupports *oldObject =
00111         NS_REINTERPRET_CAST(nsISupports*, mArray.SafeElementAt(aIndex));
00112 
00113     PRBool result = mArray.ReplaceElementAt(aObject, aIndex);
00114 
00115     // ReplaceElementAt could fail, such as if the array grows
00116     // so only release the existing object if the replacement succeeded
00117     if (result) {
00118         // Make sure to addref first, in case aObject == oldObject
00119         NS_IF_ADDREF(aObject);
00120         NS_IF_RELEASE(oldObject);
00121     }
00122     return result;
00123 }
00124 
00125 PRBool
00126 nsCOMArray_base::RemoveObject(nsISupports *aObject)
00127 {
00128     PRBool result = mArray.RemoveElement(aObject);
00129     if (result)
00130         NS_IF_RELEASE(aObject);
00131     return result;
00132 }
00133 
00134 PRBool
00135 nsCOMArray_base::RemoveObjectAt(PRInt32 aIndex)
00136 {
00137     if (PRUint32(aIndex) < PRUint32(Count())) {
00138         nsISupports* element = ObjectAt(aIndex);
00139         NS_IF_RELEASE(element);
00140 
00141         return mArray.RemoveElementAt(aIndex);
00142     }
00143 
00144     return PR_FALSE;
00145 }
00146 
00147 // useful for destructors
00148 PRBool
00149 ReleaseObjects(void* aElement, void*)
00150 {
00151     nsISupports* element = NS_STATIC_CAST(nsISupports*, aElement);
00152     NS_IF_RELEASE(element);
00153     return PR_TRUE;
00154 }
00155 
00156 void
00157 nsCOMArray_base::Clear()
00158 {
00159     mArray.EnumerateForwards(ReleaseObjects, nsnull);
00160     mArray.Clear();
00161 }
00162