Re: nsCOMArray<T> and nsIArray now available

[email protected] (Patrick Beard) Fri, 11 Oct 2002 12:48:30 -0700
Newsgroups gmane.comp.mozilla.devel.xpcom
Message-ID <[email protected]>
Nice. I always wanted to be able to templatize this usage. So, getting 
an element from an nsCOMArray essentially has no refcounting overhead 
right?

Another thing I'm very interested in looking into to reduce our 
reference counting would be something that exists in Apple's Cocoa 
objective-C framework. It's called an "autorelease" pool and provides a 
kind of deferred reference counting. Basically, it's a pool of objects 
that gets cleared out everytime the event loop runs. When you return an 
XPCOM object from a getter, you call AddRef() as usual, but you then 
add the object to the autorelease pool, which promises to call 
Release() for you when the program bottoms out in the event loop. The 
caller to the getter needn't call release, and is free to return the 
object to other consumers of the object, which themselves needn't call 
release either. In objective C, one calls an object's autorelease 
method to insert the object into the current thread's autorelease pool.

- Patrick

On Monday, October 7, 2002, at 06:19  PM, Alec Flett wrote:

> I just checked in changes that turn on nsCOMArray<T> and nsIArray.
>
> These two classes REPLACE nsISupportsArray - no new code should be 
> using nsISupportsArray! In addition, nsIArray is a public interface 
> that will be frozen for embeddors.
>
> (Reviewers, please keep this in mind when reviewing new code)
>
> I'll be working on documentation this week, but I thought I'd give you 
> a preview.
>
> nsCOMArray<T>
> -------------
> similar to nsCOMPtr, this is a typed array of objects. For instance, 
> you can have an array nsCOMArray<nsIContent> contentObjects which is 
> an array of nsIContent* objects. This allows you to avoid calling 
> QueryInterface/QueryElementAt. nsCOMArray<T> is not refcounted, and 
> not scriptable. Beneath the covers, it is more or less a wrapper 
> around nsVoidArray. Its elements ARE refcounted, and the strong typing 
> will make it very easy to use.
>
> Sample usage:
>     // member variable
>     nsCOMArray<nsIObserver> mObserverList;
> ...
>     // add an element
>     mObserverList.AppendObject(aObserver);
> ...
>     // iterate through the array - note we can call 
> nsIObserver::Observe() directly!
>     for (i=0; i< count; i++) {
>       if (mObserverList[i])
>         mObserverList[i]->Observe(a,b,c);
>     }
>
>
> nsIArray
> --------
> A new, simplified array implementation based on 
> nsCOMArray<nsISupports> - this interface WILL BE FROZEN sometime in 
> the 1.2 or 1.3 timeframe. This array is safe for embeddors to use, and 
> may appear in frozen interfaces. It is scriptable, refcounted, and 
> supports both strong and weak pointers. The base nsIArray interface 
> does not allow consumers to modify the array. There is also 
> nsIMutableArray, which allows the array to be modified.
>
> Sample usage:
> the API is very similar to nsISupportsArray, and should hopefully be 
> self explanitory.
>
> Helper routines
> ---------------
> NS_NewArray(nsIMutableArray**) - create a new nsIArray to store 
> nsISupports* objects
> NS_NewArray(nsIMutableArray**, const nsCOMArray_base&) - convert an 
> nsCOMArray<T> into an nsIArray
> NS_NewArrayEnumerator(nsISimpleEnumerator**, nsIArray*) - create an 
> enumerator for an nsIArray
> NS_NewArrayEnumerator(nsISimpleEnumerator**, const nsCOMArray_base &) 
> - create an enumerator based on an nsCOMArray<T> - takes ownership of 
> all objects in the array at creation.
>
> do_QueryElementAt(nsIArray*, PRUint32, nsresult*) - do_QueryElementAt 
> implementation for nsIArray
>
>
> Please let me know if you have any questions. I will try to follow up 
> all answers to netscape.public.mozilla.xpcom
>
> Alec
>
> -- 
> Ask me about my 1999 Volkswagen GTI for sale in the San Francisco area!
>