Symbian3/SDK/Source/GUID-69D916D3-ED05-58DA-BA42-CE4D7E4F6482.dita
author Dominic Pinkman <dominic.pinkman@nokia.com>
Tue, 20 Jul 2010 12:00:49 +0100
changeset 13 48780e181b38
parent 0 89d6a7a84779
permissions -rw-r--r--
Week 28 contribution of SDK documentation content. See release notes for details. Fixes bugs Bug 1897 and Bug 1522.

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
<!-- This component and the accompanying materials are made available under the terms of the License 
"Eclipse Public License v1.0" which accompanies this distribution, 
and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
<!-- Initial Contributors:
    Nokia Corporation - initial contribution.
Contributors: 
-->
<!DOCTYPE task
  PUBLIC "-//OASIS//DTD DITA Task//EN" "task.dtd">
<task id="GUID-69D916D3-ED05-58DA-BA42-CE4D7E4F6482" xml:lang="en"><title>Automatic
Resource Management Class Templates Tutorial</title><abstract>The <codeph>LCleanedupX</codeph> 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.</abstract><prolog><metadata><keywords/></metadata></prolog><taskbody>
<prereq><p>Before beginning you must know the following: </p><ul>
<li id="GUID-90A3678E-CB31-5817-AB27-C115AC486A3A"><p> <b> Clean-up Stratergy:</b> Cleanup
strategies can be specified as an optional template parameter of the class
templates for automatic resource management. </p> </li>
</ul> </prereq>
<steps-unordered>
<step id="GUID-26218FF0-6994-4FD7-9609-357AFDB3C177"><cmd><b>Declaring an LCleanedupX object</b></cmd>
<info><p>An <codeph>LCleanedupX</codeph> 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. </p> <p>An example code snippet
for declaring <codeph>LCleanedupX</codeph> class is shown below: </p> <codeblock id="GUID-B9DFC2F9-0C7A-5CCA-9125-FC165BFA6C1B" xml:space="preserve">class LCleanedupPtr
            {
            CTicker* ticker1 = new(ELeave) CTicker;
            LCleanedupPtr CManagedUserTwoPhase one(CManagedUserTwoPhase::NewL(ticker1));

            CTicker* ticker2 = new(ELeave) CTicker;
            LCleanedupPtr CManagedUserSinglePhase two(CManagedUserSinglePhase::NewL(ticker2));
        }
</codeblock></info>
</step>
<step id="GUID-AF65DC71-78C1-4B4D-9546-A9F4921E89D9"><cmd><b>Disabling Automatic Cleanup</b></cmd>
<info><p>An object that is being managed by an <codeph>LCleanedupX</codeph> class
can be unmanaged by calling the <codeph>Unmanage()</codeph> method of the
LCleanedupX class. </p> <p>The managing object can be accessed using the .
operator as in the call to <codeph>Unmanage()</codeph>. The <codeph>Unmanage()</codeph> disables
cleanup and yields the previously managed pointer so that it can be safely
returned. </p> <p>An example code snippet is given below: </p> <codeblock id="GUID-75BA5FAF-4C81-5626-A1F6-8AF91FD5972E" xml:space="preserve">
        static CStringUserTwoPhase* NewL(const TDesC&amp; aName)
            {
                LCleanedupPtr CStringUserTwoPhase self(new(ELeave) CStringUserTwoPhase);
                self-&gt;ConstructL(aName);
                return self.Unmanage(); 
            }
</codeblock></info>
</step>
<step id="GUID-B9F7C183-E92E-47D6-8A6E-A1E1A76C1253"><cmd><b>Accessing the managed object</b></cmd>
<info><p>The managed object can be accessed using the -&gt; operator as in the
call to <codeph>ConstructL()</codeph> </p> <codeblock id="GUID-2C82F4C0-9E02-51C5-BF51-2C05EACB424C" xml:space="preserve">self-&gt;ConstructL(aName);</codeblock></info>
</step>
<step id="GUID-D070D636-665B-4023-A81A-C5AB0A5390CB"><cmd><b>Passing the managed object to a function</b></cmd>
<info><p>The managed object can be passed to a function by dereferencing the
managing object. An example code snippet is given below: </p> <codeblock id="GUID-5E5A3A0C-77E4-520A-BD0A-031C7D3CAAFD" xml:space="preserve">
void RegisterTicker(CTicker&amp; aTicker)
    {
    (void)aTicker;
    }

</codeblock><p>The <codeph>RegisterTicker</codeph> function defined above
takes a <codeph>CTicker&amp;</codeph> argument. If we have a <codeph>LCleanedupPtr&lt;CTicker&gt;</codeph> as
defined below, we can pass the <codeph>CTicker</codeph> object to the function
by dereferencing as shown below in the code snippet: </p> <codeblock id="GUID-EC93EC4F-3D3E-5A70-A0E1-CF73475A7309" xml:space="preserve">
LCleanedupPtr CTicker t(new(ELeave) CTicker);

RegisterTicker(*t); // Takes a CTicker&amp;
</codeblock></info>
</step>
</steps-unordered>
</taskbody><related-links>
<link href="GUID-B1D5B680-00E3-5702-985A-94256180E2D8.dita"><linktext>Automatic
Resource Management</linktext></link>
<link href="GUID-B419D99E-8312-5336-9693-3ED8DFCD0559.dita"><linktext>Automatic
Resource Management Tutorial</linktext></link>
</related-links></task>