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