Symbian3/PDK/Source/GUID-3799F0DA-B99C-55BB-B44F-63B971DF1865.dita
changeset 1 25a17d01db0c
child 3 46218c8b8afa
equal deleted inserted replaced
0:89d6a7a84779 1:25a17d01db0c
       
     1 <?xml version="1.0" encoding="utf-8"?>
       
     2 <!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
       
     3 <!-- This component and the accompanying materials are made available under the terms of the License 
       
     4 "Eclipse Public License v1.0" which accompanies this distribution, 
       
     5 and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
       
     6 <!-- Initial Contributors:
       
     7     Nokia Corporation - initial contribution.
       
     8 Contributors: 
       
     9 -->
       
    10 <!DOCTYPE task
       
    11   PUBLIC "-//OASIS//DTD DITA Task//EN" "task.dtd">
       
    12 <task id="GUID-3799F0DA-B99C-55BB-B44F-63B971DF1865" xml:lang="en"><title>Cleanup
       
    13 Strategy Tutorial</title><shortdesc>This tutorial describes the cleanup strategy and provides details
       
    14 on alternative cleanup strategies. </shortdesc><prolog><metadata><keywords/></metadata></prolog><taskbody>
       
    15 <prereq><p>Before you start, you must: </p> <ul>
       
    16 <li id="GUID-18CD0776-EEA9-56AF-9E5F-E5101BB88643"><p> <b>Deafult cleanup
       
    17 strategies:</b> These are based on a template-based implementation of the
       
    18 strategy design pattern. </p> </li>
       
    19 <li id="GUID-598FF730-0C91-51C4-B9AA-16B74137BB6E"><p> <b>Alternative cleanup
       
    20 strategies:</b> These can be specified as an optional template parameter of
       
    21 the class templates for automatic resource management. </p> </li>
       
    22 </ul> </prereq>
       
    23 <context><p>Cleanup strategy is based on a template-based implementation of
       
    24 the Strategy design pattern. The strategy design pattern is one of the most
       
    25 popular, highly recommended design patterns. The vast majority of the competent
       
    26 C++ developers are already familiar with the strategy (Policy) design pattern
       
    27 and Policy-based design, which should improve the usability of these class
       
    28 templates. </p> <p>Each
       
    29 resource management class has a default cleanup strategy that is invoked when
       
    30 the managing object goes out of scope. It is however possible to define an
       
    31 alternative cleanup strategy when constructing the managing object. It is
       
    32 also possible to define a custom cleanup strategy using the <xref href="GUID-CE0F22BA-8789-3659-87C4-164EB5862B4C.dita"><apiname>DEFINE_CLEANUP_FUNCTION</apiname></xref> macro. </p> <p> <b>Note:</b> Although
       
    33 the examples below use LCleanedupX classes, the method for definfing how the
       
    34 object is cleaned up is equally applicable to LManagedX classes. </p> <p>An
       
    35 example code snippet is given below: </p> <codeblock id="GUID-DE45C250-C8C5-5210-B53C-D9256F00194B" xml:space="preserve">
       
    36 //Class definition of trivial R-Class
       
    37 class RSimple
       
    38     {
       
    39 public:
       
    40 
       
    41     RSimple(){iData = NULL;}
       
    42 
       
    43     void Open(TInt aValue)
       
    44         {
       
    45         iData = new(ELeave) TInt(aValue);
       
    46         }
       
    47 
       
    48     //Cleanup function – frees resource
       
    49     void Close()
       
    50         {
       
    51         delete iData;
       
    52         iData = NULL;
       
    53         }
       
    54 
       
    55     //Cleanup function – frees resource
       
    56     void Free()
       
    57         {
       
    58         delete iData;
       
    59         iData = NULL;
       
    60         }
       
    61 
       
    62     //Cleanup function – frees resource
       
    63     void ReleaseData()
       
    64         {
       
    65         delete iData;
       
    66         iData = NULL;
       
    67         }
       
    68 
       
    69     //static cleanup function – frees aRSimple resources
       
    70     static void Cleanup(TAny* aRSimple)
       
    71         {
       
    72         static_cast RSimple* (aRSimple)-&gt;Close();
       
    73         }
       
    74 private:
       
    75     Tint* iData;
       
    76 
       
    77     };
       
    78 
       
    79 </codeblock> <p>The above RSimple class has three cleanup member functions,
       
    80 Close, Free and ReleaseData. </p> <p>Each resource management class has a
       
    81 default cleanup strategy that is invoked when the managing object goes out
       
    82 of scope. The default cleanup strategy is dependent on the managing type.
       
    83 Consider the example code snippet below: </p> <p> <b>Note:</b> Although the
       
    84 examples below use LManagedX classes, the method for defining a custom cleanup
       
    85 strategy is equally applicable to LCleanedupX classes. </p> <codeblock id="GUID-7D4F639F-3BEA-59CD-BA75-DF1C25175BA3" xml:space="preserve">
       
    86 class CTicker : public CBase
       
    87     {
       
    88 public:
       
    89     void Tick() { ++iTicks; }
       
    90     void Tock() { ++iTocks; }
       
    91 
       
    92     void Zap() { delete this; }
       
    93 
       
    94 public:
       
    95     TInt iTicks;
       
    96     TInt iTocks;
       
    97     };
       
    98 
       
    99 
       
   100 class CManagedUserSinglePhase : public CBase
       
   101     {
       
   102 public:
       
   103     . . .
       
   104 
       
   105     ~CManagedUserSinglePhase()
       
   106         {
       
   107         // The iTicker manager will automatically delete the CTicker
       
   108         // The iTimer manager will automatically Close() the RTimer
       
   109         }
       
   110 
       
   111     . . .
       
   112 private:
       
   113     // We have to use LManagedXxx for fields, not LCleanedupXxx
       
   114     LManagedPtr CTicker iTicker;
       
   115     LManagedHandle RTimer iTimer;
       
   116     };
       
   117 </codeblock> <p>The default cleanup strategy for an LManagedPtr is to delete
       
   118 the pointer and this is the action taken in the destructor for CManagedUserSinglePhase. </p> <p>Alternate
       
   119 cleanup strategies are pre-defined in <filepath>emanaged.h</filepath> file.
       
   120 It is also possible to define a custom alternate cleanup strategy that can
       
   121 be passed as the second template parameter to the constructor of an LManagedX
       
   122 or LCleanedupX object. </p> <p>Although the examples below use LManagedX classes,
       
   123 the method for defining an alternate cleanup strategy is equally applicable
       
   124 to LCleanedupX classes. </p> <p>The default cleanup strategy is to call <xref href="GUID-91F3C4E0-5687-366D-9D50-D710E763F0CF.dita#GUID-91F3C4E0-5687-366D-9D50-D710E763F0CF/GUID-29E73AC7-D397-3237-ABF9-C93C0AD39543"><apiname>CTicker::Zap()</apiname></xref> function.
       
   125 It is possible however to define a custom alternate cleanup strategy as shown
       
   126 below: </p> <codeblock id="GUID-5B268C52-2708-50CF-8070-0896C8CE80C0" xml:space="preserve">
       
   127 // Defines a custom pointer cleanup policy that calls the Zap member
       
   128 template &lt;class T&gt;
       
   129 class TTickerZap 
       
   130     {
       
   131 public:
       
   132     static void Cleanup(T* aPtr)
       
   133         {
       
   134         // The general template/class scaffolding remains the same
       
   135         // for all custom cleanups, just this cleanup body varies
       
   136         aPtr-&gt;Zap();
       
   137         test.Printf(_L("Zapped CTicker\n"));
       
   138         }
       
   139     };
       
   140 </codeblock> </context>
       
   141 </taskbody><related-links>
       
   142 <link href="GUID-B007634D-4D55-528A-8B85-6120C633AC8B.dita"><linktext>EUser High
       
   143 Level Overview</linktext></link>
       
   144 <link href="GUID-A18153C0-230C-51FB-9384-A48BB4E42F03.dita"><linktext>Clean-up
       
   145 Strategy</linktext></link>
       
   146 </related-links></task>