LCleanedupX
class provide a means of automatically
cleaning up local variables on scope exit. Variants are provided for cleaning
up pointers, references, handles and arrays, as well as generic cleanup items.
The user is able to define a cleanup strategy to clean up the resource on
scope exit.Before beginning you must know the following:
Declaring an LCleanedupX object
An LCleanedupX
object is declared using one of the
class template constructors. The type to be managed is declared as the first
template parameter. An optional second template parameter can be declared
to implement a non-default cleanup strategy.
An example code snippet
for declaring LCleanedupX
class is shown below:
class LCleanedupPtr { CTicker* ticker1 = new(ELeave) CTicker; LCleanedupPtr CManagedUserTwoPhase one(CManagedUserTwoPhase::NewL(ticker1)); CTicker* ticker2 = new(ELeave) CTicker; LCleanedupPtr CManagedUserSinglePhase two(CManagedUserSinglePhase::NewL(ticker2)); }
Disabling Automatic Cleanup
An object that is being managed by an LCleanedupX
class
can be unmanaged by calling the Unmanage()
method of the
LCleanedupX class.
The managing object can be accessed using the .
operator as in the call to Unmanage()
. The Unmanage()
disables
cleanup and yields the previously managed pointer so that it can be safely
returned.
An example code snippet is given below:
static CStringUserTwoPhase* NewL(const TDesC& aName) { LCleanedupPtr CStringUserTwoPhase self(new(ELeave) CStringUserTwoPhase); self->ConstructL(aName); return self.Unmanage(); }
Accessing the managed object
The managed object can be accessed using the -> operator as in the
call to ConstructL()
self->ConstructL(aName);
Passing the managed object to a function
The managed object can be passed to a function by dereferencing the managing object. An example code snippet is given below:
void RegisterTicker(CTicker& aTicker) { (void)aTicker; }
The RegisterTicker
function defined above
takes a CTicker&
argument. If we have a LCleanedupPtr<CTicker>
as
defined below, we can pass the CTicker
object to the function
by dereferencing as shown below in the code snippet:
LCleanedupPtr CTicker t(new(ELeave) CTicker); RegisterTicker(*t); // Takes a CTicker&