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

Generated by  doxygen 1.6.2