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

Generated by  doxygen 1.6.2