examples/Base/MemMan/Cleanup/NewLC/NewLC.cpp

00001 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // This component and the accompanying materials are made available
00004 // under the terms of "Eclipse Public License v1.0"
00005 // which accompanies this distribution, and is available
00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 // Example shows use of the NewLC() static function.
00015 // NOTE: the structure of this example is different to standard E32 examples
00016 //
00017 
00018 
00019 #include <e32cons.h>
00020 
00021   // All messages written to this
00022 LOCAL_D CConsoleBase* console;
00023   
00024   // Flag which determines whether the doSomething() member function
00025   // of the CExample class should leave when called.
00026 LOCAL_D TBool leaveFlag = ETrue;
00027 
00028   // Parameter for __UHEAP_SETFAIL
00029   // Allocation guaranteed to fail at this number of allocation attempts;
00030   // i.e. if set to n, allocation fails on the nth attempt. 
00031   // NB only used in debug mode
00032 #ifdef _DEBUG
00033 LOCAL_D TInt allocFailNumber = 1;
00034 #endif
00035   
00036   // Function prototypes
00037 LOCAL_C void doExampleL();      
00038 LOCAL_C void callExampleL();
00039 
00040 
00041 
00043 //
00044 // -----> CExample (definition)
00045 //
00046 // The class is used by the example code
00047 //
00049 class CExample : public CBase
00050         {
00051 public :
00052         static CExample* NewLC(TInt aVal);
00053         void DoSomethingL();
00054 public :
00055         TInt iInt;
00056         };
00057 
00058 
00060 //
00061 // -----> CExample (implementation)
00062 //
00064 CExample* CExample::NewLC(TInt aVal)
00065         {
00066         CExample* self = new (ELeave) CExample;
00067         CleanupStack::PushL(self);
00068         self->iInt = aVal;
00069         return self;
00070         }
00071 
00072 
00073 void CExample::DoSomethingL() 
00074         {
00075           // Leave if the global flag is set
00076         if (leaveFlag)
00077                 {
00078                 _LIT(KMsgLeaving,"DoSomethingL leaving.\n");
00079                 console->Printf(KMsgLeaving);
00080                 User::Leave(KErrGeneral);               
00081                 }
00082         _LIT(KFormat1,"Value of iInt is %d.\n");
00083         console->Printf(KFormat1,iInt);
00084         }
00085 
00087 //
00088 // Main function called by E32
00089 //
00091 GLDEF_C TInt E32Main()
00092     {
00093           // Get cleanup stack
00094         CTrapCleanup* cleanup=CTrapCleanup::New();
00095 
00096           // Some more initialization, then do the example
00097         TRAPD(error,callExampleL());
00098           
00099           // callExampleL() should never leave.
00100         _LIT(KMsgPanicEpoc32ex,"EPOC32EX");
00101         __ASSERT_ALWAYS(!error,User::Panic(KMsgPanicEpoc32ex,error));
00102 
00103           // destroy the cleanup stack
00104         delete cleanup;
00105         
00106           // return
00107         return 0;
00108     }
00109 
00110 
00112 //
00113 //
00114 //
00116 LOCAL_C void callExampleL() 
00117     {
00118           // Initialize and call the example code under cleanup stack.
00119         _LIT(KMsgExampleCode,"Symbian platform Example Code");
00120         console = Console::NewL(KMsgExampleCode,TSize(KConsFullScreen,KConsFullScreen));
00121           // Put console onto the cleanup stack.
00122         CleanupStack::PushL(console);
00123 
00124           // Perform the example function under the protection of a 
00125           // TRAP harness.
00126         TRAPD(error,doExampleL());
00127         _LIT(KMsgOK,"ok");
00128         _LIT(KFormat2,"failed: leave code = %d");
00129         if (error)
00130                 console->Printf(KFormat2,error);
00131         else
00132                 console->Printf(KMsgOK);
00133           
00134           // Continue
00135         _LIT(KMsgPressAnyKey," [press any key]");
00136         console->Printf(KMsgPressAnyKey);
00137         console->Getch();
00138 
00139           // Remove the console object from the cleanupstack
00140           // and destroy it. 
00141         CleanupStack::PopAndDestroy();
00142     }
00143 
00144 
00146 //
00147 // Do the example
00148 //
00149 // Enriched example UsingNewL by using NewLC() instead of NewL().
00150 // The function NewLC() both allocates a CExample object and pushes
00151 // the object onto the cleanup stack
00152 //
00154 void doExampleL()
00155         {
00156           // Memory alloc fails on the 'allocFailNumber' attempt. 
00157         __UHEAP_SETFAIL(RHeap::EDeterministic,allocFailNumber);
00158           // Allocate and push onto the cleanup stack - leave if 
00159           // allocation fails.
00160         CExample* myExample = CExample::NewLC(5);
00161           // Do something that can leave.
00162         myExample->DoSomethingL();
00163           // Pop from the cleanup stack and destroy
00164         CleanupStack::PopAndDestroy();
00165         }
00166 
00167 
00168 
00169 
00170 
00171 
00172 
00173 

Generated by  doxygen 1.6.2