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