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 attempt to construct an object and specifying 00029 (ELeave) after the 'new' operator. 00030 Specifying (ELeave) causes a leave on failure to allocate memory 00031 for the new object. 00032 NOTE: the structure of this example is different to standard E32 examples 00033 */ 00034 00035 00036 00037 #include <e32cons.h> 00038 00039 // 00040 // Common format text 00041 // 00042 00043 _LIT(KCommonFormat1,"Value of iInt is %d.\n"); 00044 00045 00046 // All messages written to this 00047 LOCAL_D CConsoleBase* console; 00048 00049 // Flag which determines whether the doSomething() member function 00050 // of the CExample class should leave when called. 00051 LOCAL_D TBool leaveFlag = ETrue; 00052 00053 // Parameter for __UHEAP_SETFAIL 00054 // Allocation guaranteed to fail at this number of allocation attempts; 00055 // i.e. if set to n, allocation fails on the nth attempt. 00056 // NB only used in debug mode 00057 #ifdef _DEBUG 00058 LOCAL_D TInt allocFailNumber = 1; 00059 #endif 00060 00061 // Function prototypes 00062 LOCAL_C void doExampleL(); 00063 LOCAL_C void callExampleL(); 00064 00065 00066 00068 // 00069 // -----> CExample (definition) 00070 // 00071 // The class is used by the example code 00072 // 00074 class CExample : public CBase 00075 { 00076 public : 00077 void DoSomethingL(); 00078 public : 00079 TInt iInt; 00080 }; 00081 00082 00084 // 00085 // -----> CExample (implementation) 00086 // 00088 void CExample::DoSomethingL() 00089 { 00090 // Leave if the global flag is set 00091 if (leaveFlag) 00092 { 00093 _LIT(KMsgLeaving,"DoSomethingL leaving.\n"); 00094 console->Printf(KMsgLeaving); 00095 User::Leave(KErrGeneral); 00096 } 00097 console->Printf(KCommonFormat1,iInt); 00098 } 00099 00101 // 00102 // Main function called by E32 00103 // 00105 GLDEF_C TInt E32Main() 00106 { 00107 // Get cleanup stack 00108 CTrapCleanup* cleanup=CTrapCleanup::New(); 00109 00110 // Some more initialization, then do the example 00111 TRAPD(error,callExampleL()); 00112 00113 // callExampleL() should never leave. 00114 _LIT(KMsgPanicEpoc32ex,"EPOC32EX"); 00115 __ASSERT_ALWAYS(!error,User::Panic(KMsgPanicEpoc32ex,error)); 00116 00117 // destroy the cleanup stack 00118 delete cleanup; 00119 00120 // return 00121 return 0; 00122 } 00123 00124 00126 // 00127 // 00128 // 00130 LOCAL_C void callExampleL() 00131 { 00132 // Initialize and call the example code under cleanup stack. 00133 _LIT(KMsgExampleCode,"Symbian platform Example Code"); 00134 console = Console::NewL(KMsgExampleCode,TSize(KConsFullScreen,KConsFullScreen)); 00135 // Put console onto the cleanup stack. 00136 CleanupStack::PushL(console); 00137 00138 // Perform the example function under the protection of a 00139 // TRAP harness. 00140 TRAPD(error,doExampleL()); 00141 _LIT(KMsgOK,"ok"); 00142 _LIT(KFormat2,"failed: leave code = %d"); 00143 if (error) 00144 console->Printf(KFormat2,error); 00145 else 00146 console->Printf(KMsgOK); 00147 00148 // Continue 00149 _LIT(KMsgPressAnyKey," [press any key]"); 00150 console->Printf(KMsgPressAnyKey); 00151 console->Getch(); 00152 00153 // Remove the console object from the cleanupstack 00154 // and destroy it. 00155 CleanupStack::PopAndDestroy(); 00156 } 00157 00158 00160 // 00161 // Do the example 00162 // 00164 void doExampleL() 00165 { 00166 // Memory alloc fails on the 'allocFailNumber' attempt. 00167 // 00168 // Note that if you set this to some low value sucah as 1 or 2, then as a side effect, 00169 // you may also see a dialog box stating "Not enough memory". 00170 __UHEAP_SETFAIL(RHeap::EDeterministic,allocFailNumber); 00171 // Allocate - leave if allocation fails. 00172 // The (ELeave) causes a leave if allocation fails; replaces 00173 // a call to User::LeaveIfNull(myExample); 00174 // Compare with the code in LeaveOnFailure 00175 // 00176 // Note that the memory allocation will fail and the "new" operation 00177 // will leave if allocFailNumber is 1. 00178 CExample* myExample = new (ELeave) CExample; 00179 // Do something with the CExample object 00180 myExample->iInt = 5; 00181 // 00182 console->Printf(KCommonFormat1,myExample->iInt); 00183 // Delete the CExample object 00184 delete myExample; 00185 } 00186 00187 00188 00189 00190 00191 00192 00193