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

Generated by  doxygen 1.6.2