OLReaper Class Reference

A class for automatically freeing objects. More...

#import <ObjectiveLib/Reaper.h>

List of all members.

Public Member Functions

(id) - reap:
 Add the given object to this reaper.
(unsigned) - size
 Return the number of objects currently scheduled for destruction.
Initializers and Deallocators
(id) - free
 Free all objects that have been added to this reaper and free the reaper.
(id) - init
 Initialize a new reaper.

Static Public Member Functions

(OLReaper *) + currentReaper
 Retrieve the reaper currently in use.
(id) + initialize
 Initialize the class.

Protected Attributes

OLVectorJunior * undead
 The array of as-yet-uncleaned-up objects.


Detailed Description

A class for automatically freeing objects.

This class is fundamentally equivalent to NSAutoreleasePool from OpenStep. Each thread contains a list of active reapers. Calling currentReaper (OLReaper) will return the head of the current thread's list, thus the most recently defined reaper. When a reaper is freed it will free all of the objects that it has been given to reap.

Typcial usage would be to create a reaper at some point in the code at a high level, for example in main(), and then to add allocated objects to the reaper instead of trying to manage all the memory by hand. For example, imagine in the following example that a reaper has been created in the application's main function.

 OLReaper reaper = [OLReaper currentReaper];
 OLTextBuffer* buffer = [reaper reap: [OLTextBuffer textBuffer]];
 [buffer appendChars: charBuf fromOffset: 0 count: 8192];
 [buffer appendText: [reaper reap: [OLText textWithCString: "Hi, there"]]]
 // Do something with the text from the buffer
 ... [reaper reap: [buffer text]] ...

Note that in the above example memory management is handled by the reaper. All of the allocated objects added to the reaper will be cleaned up when the reaper is freed.


Member Function Documentation

+ (OLReaper*) currentReaper  

Retrieve the reaper currently in use.

This reaper will be the most recently defined OLReaper in the current thread.

Returns:
the current reaper

- (id) free  

Free all objects that have been added to this reaper and free the reaper.

That's free the reaper, not fear the reaper.

- (id) init  

Initialize a new reaper.

The reaper contains no objects to free.

Returns:
a reference to this reaper

+ (id) initialize  

Initialize the class.

This for the runtime system to call. Please don't call this method.

- (id) reap: (id)  object  

Add the given object to this reaper.

The object will be freed when the reaper is freed.

Parameters:
object the object to reap
Returns:
a reference to object

- (unsigned) size  

Return the number of objects currently scheduled for destruction.

Returns:
the size of the reapers cache


Member Data Documentation

- (OLVectorJunior*) undead [protected]

The array of as-yet-uncleaned-up objects.


The documentation for this class was generated from the following file:
ObjectiveLibGenerated Sun Apr 22 15:19:00 2007, © 2004-2007 Will Mason
SourceForge.net Logo