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 cleanup stack fucntions PushL() and Pop() 00015 // NOTE: the structure of this example is different to standard E32 examples 00016 // 00017 00018 00019 #include <e32cons.h> 00020 00021 00022 // All messages written to this 00023 LOCAL_D CConsoleBase* console; 00024 00025 // Flag which determines whether the doSomething() member function 00026 // of the CExample class should leave when called. 00027 LOCAL_D TBool leaveFlag = ETrue; 00028 00029 // Parameter for __UHEAP_SETFAIL 00030 // Allocation guaranteed to fail at this number of allocation attempts; 00031 // i.e. if set to n, allocation fails on the nth attempt. 00032 // NB only used in debug mode 00033 #ifdef _DEBUG 00034 LOCAL_D TInt allocFailNumber = 1; 00035 #endif 00036 00037 // Function prototypes 00038 LOCAL_C void doExampleL(); 00039 LOCAL_C void callExampleL(); 00040 00041 00042 00044 // 00045 // -----> CExample (definition) 00046 // 00047 // The class is used by the example code 00048 // 00050 class CExample : public CBase 00051 { 00052 public : 00053 void DoSomethingL(); 00054 public : 00055 TInt iInt; 00056 }; 00057 00058 00060 // 00061 // -----> CExample (implementation) 00062 // 00064 void CExample::DoSomethingL() 00065 { 00066 // Leave if the global flag is set 00067 if (leaveFlag) 00068 { 00069 _LIT(KMsgLeaving,"DoSomethingL leaving.\n"); 00070 console->Printf(KMsgLeaving); 00071 User::Leave(KErrGeneral); 00072 } 00073 _LIT(KFormat1,"Value of iInt is %d.\n"); 00074 console->Printf(KFormat1,iInt); 00075 } 00076 00078 // 00079 // Main function called by E32 00080 // 00082 GLDEF_C TInt E32Main() 00083 { 00084 // Get cleanup stack 00085 CTrapCleanup* cleanup=CTrapCleanup::New(); 00086 00087 // Some more initialization, then do the example 00088 TRAPD(error,callExampleL()); 00089 00090 // callExampleL() should never leave. 00091 _LIT(KMsgPanicEpoc32ex,"EPOC32EX"); 00092 __ASSERT_ALWAYS(!error,User::Panic(KMsgPanicEpoc32ex,error)); 00093 00094 // destroy the cleanup stack 00095 delete cleanup; 00096 00097 // return 00098 return 0; 00099 } 00100 00101 00103 // 00104 // 00105 // 00107 LOCAL_C void callExampleL() 00108 { 00109 // Initialize and call the example code under cleanup stack. 00110 _LIT(KMsgExampleCode,"Symbian platform Example Code"); 00111 console = Console::NewL(KMsgExampleCode,TSize(KConsFullScreen,KConsFullScreen)); 00112 // Put console onto the cleanup stack. 00113 CleanupStack::PushL(console); 00114 00115 // Perform the example function under the protection of a 00116 // TRAP harness. 00117 TRAPD(error,doExampleL()); 00118 _LIT(KMsgOK,"ok"); 00119 _LIT(KFormat2,"failed: leave code = %d"); 00120 if (error) 00121 console->Printf(KFormat2,error); 00122 else 00123 console->Printf(KMsgOK); 00124 00125 // Continue 00126 _LIT(KMsgPressAnyKey," [press any key]"); 00127 console->Printf(KMsgPressAnyKey); 00128 console->Getch(); 00129 00130 // Remove the console object from the cleanupstack 00131 // and destroy it. 00132 CleanupStack::PopAndDestroy(); 00133 } 00134 00135 00137 // 00138 // Do the example 00139 // 00140 // Surround something that might leave with a PushL() and a Pop() 00141 // Compare this with the example: UsingPushLAndPopAndDestroy 00142 // 00144 void doExampleL() 00145 { 00146 // Memory alloc fails on the 'allocFailNumber' attempt. 00147 __UHEAP_SETFAIL(RHeap::EDeterministic,allocFailNumber); 00148 // Allocate - leave if allocation fails. 00149 CExample* myExample = new (ELeave) CExample; 00150 // Do something that cannot leave (no protection needed) 00151 myExample->iInt = 5; 00152 // Do something that can leave. 00153 // Use the cleanup stack - put the pointer to the CExample object on 00154 // the cleanup stack 00155 CleanupStack::PushL(myExample); 00156 // This is something that might leave 00157 myExample->DoSomethingL(); 00158 // It didn't leave, so pop the pointer off the stack 00159 CleanupStack::Pop(); 00160 // Delete the CExample object 00161 delete myExample; 00162 } 00163 00164 00165 00166 00167 00168 00169 00170
Copyright ©2010 Nokia Corporation and/or its subsidiary(-ies).
All rights
reserved. Unless otherwise stated, these materials are provided under the terms of the Eclipse Public License
v1.0.