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

Generated by  doxygen 1.6.2