examples/Base/MemMan/Cleanup/Utilities/Utilities.cpp

00001 /*
00002 Copyright (c) 2000-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:  
00028 */
00029 
00030 
00031 #include "CommonFramework.h"
00032 
00033 //
00034 // Definition of the CTestOne class
00035 //
00036 class CTestOne : public CBase
00037         {       
00038 public:
00039         ~CTestOne();
00040         void     SetTextL(const TDesC& aData);
00041 private :
00042         HBufC*   iText;
00043         };
00044 
00045 
00046 //
00047 // Implementation of the CTestOne class
00048 //
00049 _LIT(KTxtInsideDestructor,"Executing the CTestOne destructor\n");
00050 CTestOne::~CTestOne()
00051         {
00052         delete iText;
00053         console->Printf(KTxtInsideDestructor);
00054         }
00055 
00056 void CTestOne::SetTextL(const TDesC& aData)
00057         {
00058         if (iText)
00059                 {
00060                 delete iText;
00061                 iText = NULL;
00062                 }
00063         iText = aData.AllocL(); 
00064         }
00065 
00066 
00067 
00068 //
00069 // Definition of the RTestTwo class
00070 //
00071 class RTestTwo
00072         {       
00073 public:
00074         RTestTwo(TInt aValue);
00075         void     Close();
00076 private :
00077         TInt     iX;
00078         };
00079 
00080 //
00081 // Implementation of the RTestTwo class
00082 //
00083 RTestTwo::RTestTwo(TInt aValue)
00084         : iX(aValue)
00085         {
00086         }
00087 
00088 
00089 _LIT(KTxtCloseRTestTwo,"RTestTwo closing\n");
00090 
00091 void RTestTwo::Close()
00092         {
00093         console->Printf(KTxtCloseRTestTwo);
00094         }
00095 
00096 
00097 
00098 //
00099 // Definition of the RTestThree class
00100 //
00101 class RTestThree
00102         {       
00103 public:
00104         RTestThree(TInt aValue);
00105         void     Release();
00106 private :
00107         TInt     iY;
00108         };
00109 
00110 //
00111 // Implementation of the RTestThree class
00112 //
00113 RTestThree::RTestThree(TInt aValue)
00114         : iY(aValue)
00115         {
00116         }
00117 
00118 
00119 _LIT(KTxtReleaseRTestThree,"RTestThree releasing\n");
00120 
00121 void RTestThree::Release()
00122         {
00123         console->Printf(KTxtReleaseRTestThree);
00124         }
00125 
00126 //
00127 // main body of the example
00128 //
00129 _LIT(KTxtHelloWorld,"Hello World!");
00130 LOCAL_C void doExampleL()
00131     {
00132                 // 1. Construct a CTestOne object on the heap
00133         CTestOne* one = new (ELeave) CTestOne;
00134         
00135                 // Use the CleanupDeletePushL() function to put a TCleanUpItem
00136                 // on the cleanup stack
00137         CleanupDeletePushL(one);
00138 
00139                 // Exercise the CTestOne object (just to show it doing something)
00140         one->SetTextL(KTxtHelloWorld);
00141 
00142                 // Pop and destroy the cleanup item off the cleanup stack.
00143                 // The cleanup operation deletes the CTestOne object
00144         CleanupStack::PopAndDestroy();
00145         
00146                 // 2. Construct a RTestTwo object on the program stack.
00147                 //
00148                 // The value passed is of no significance; it is just
00149                 // to show that the class is not trivial.
00150         RTestTwo  two(2);
00151                 
00152                 // Use the CleanupClosePushL() function to put a TCleanUpItem
00153                 // on the cleanup stack
00154         CleanupClosePushL(two);
00155 
00156                 // Pop and destroy the cleanup item off the cleanup stack.
00157                 // The cleanup operation calls the Close() member function of
00158                 // the RTestTwo object
00159         CleanupStack::PopAndDestroy();
00160                                 
00161 
00162                 // 3. Construct a RTestThree object on the program stack.
00163                 //
00164                 // The value passed is of no significance; it is just
00165                 // to show that the class is not trivial.
00166         RTestThree  three(3);
00167                 
00168                 // Use the CleanupClosePushL() function to put a TCleanUpItem
00169                 // on the cleanup stack
00170         CleanupReleasePushL(three);
00171 
00172                 // Pop and destroy the cleanup item off the cleanup stack.
00173                 // The cleanup operation calls the Release() member function of
00174                 // the RTestThree object
00175         CleanupStack::PopAndDestroy();
00176 
00177                 // 4. Construct an array of objects on the heap
00178         TInt heapSize1 = User::Heap().Count(); // take a count of the heap size now
00179         
00180         const TInt KNumObjects = 4;
00181         const TInt KStringLength = 10;
00182         TBuf<KStringLength>* four = new (ELeave) TBuf<KStringLength>[KNumObjects];
00183                 // Use the CleanupArrayDeletePushL() function to put a TCleanUpItem
00184                 // on the cleanup stack
00185         CleanupArrayDeletePushL(four);
00186 
00187                 // Do something that might leave - a simple memory allocation
00188         TInt* mem = (TInt*)User::Alloc(100);
00189         delete mem;
00190 
00191                 // Pop and destroy the cleanup item off the cleanup stack.
00192                 // The cleanup operation deletes the CTestOne object
00193         CleanupStack::PopAndDestroy();
00194         if ( User::Heap().Count() == heapSize1 )
00195                 {
00196                 _LIT(KFourComplete,"Array deleted\n");
00197                 console->Printf(KFourComplete);
00198                 }
00199         }

Generated by  doxygen 1.6.2