examples/Basics/TAndRClasses/TAndRClasses.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 // TAndRClass.cpp
00015 //
00016 
00017 #include "CommonFramework.h"
00018 
00019 _LIT(KTxtTIntHolderDestructor,"TIntHolder destructor\n");
00020 _LIT(KCommonFormat1,"Int()=%d\n");
00021 
00022 // T class
00023 
00024 class TIntHolder
00025         {
00026 public: // data
00027         TInt iInt;
00028 public: // functions
00029         TInt Int() { return iInt; }; // get the integer
00030         void SetInt(TInt aInt) { iInt=aInt; }; // set the integer
00031         void Double() { iInt *= 2; }; // double the integer
00032         TBool IsNonZero() { return iInt!=0; }; // check whether zero
00033         // destructor, coded only to show when taken and when not
00034         ~TIntHolder() { console->Printf(KTxtTIntHolderDestructor); };
00035         };
00036 
00037 void demonstrateTTypeL(); // function to demonstrate T type
00038 
00039 // class RTimer - a typical R class
00040 
00041 // an R class with added clean-up support
00042 
00043 class RTimerWithCleanup : public RTimer
00044         {
00045 public: // no extra data, just inherit everything from base
00046 public: // functions
00047         // required for clean-up support
00048         operator TCleanupItem()
00049                 { // convert to clean-up item
00050                 return TCleanupItem(Cleanup,this);
00051                                 // clean-up = function + object
00052                 }
00053         static void Cleanup(TAny *aPtr)
00054                 { // static clean-up function returned by TCleanupItem
00055                 ((RTimerWithCleanup*)aPtr) -> DoCleanup();
00056                                 // do clean-up neatly, using regular member function
00057                 }
00058         void DoCleanup()
00059                 { // do clean-up neatly
00060                 Close(); // close timer
00061                 }
00062         // helpful for automatically putting onto clean-up stack
00063         void CreateLocalLC()
00064                 { // call RTImer::CreateLocal(), and push to clean-up stack
00065                 User::LeaveIfError(CreateLocal()); // create, but leave if couldn't
00066                 CleanupStack::PushL(*this); // push to clean-up stack
00067                 }
00068         // debug version of RTimer::Close()
00069         void Close()
00070                 { // debug version of close
00071                 _LIT(KTxtClosingRTimer,"Closing RTimerWithCleanup\n");
00072                 console->Printf(KTxtClosingRTimer);
00073                 RTimer::Close(); // base call
00074                 }
00075         };
00076 
00077 void demonstrateRTypeL(); // function to demonstrate R type
00078 
00079 // Do the example
00080 LOCAL_C void doExampleL()
00081     {
00082         demonstrateTTypeL(); // demonstrate T type
00083         demonstrateRTypeL(); // demonstrate R type
00084         }
00085 
00086 // stuff to demonstrate T type
00087 
00088 void useTTypeL(TIntHolder aIntHolder); // function using a T
00089 
00090 void demonstrateTTypeL()
00091         {
00092         _LIT(KTxtStartingDemoTTypeL,"Starting demonstrateTTypeL()\n");
00093         console->Printf(KTxtStartingDemoTTypeL);
00094         TIntHolder t; // declare variable
00095         // do a few things with it
00096         t.SetInt(3);
00097         t.Double();
00098         TInt i=t.Int();
00099         console->Printf(KCommonFormat1, i);
00100         // optionally leave
00101         //
00102         // Remove comments on next line to show this
00103         //User::Leave(KErrGeneral);
00104 
00105         // now use T type
00106         useTTypeL(t); // pass by value
00107         // end demo: T will be destructed (for what it's worth)
00108         _LIT(KTxtFinishingDemoTTypeL,"Finishing demonstrateTTypeL()\n");
00109         console->Printf(KTxtFinishingDemoTTypeL);
00110         }
00111 
00112 void useTTypeL(TIntHolder aIntHolder)
00113         { // function using a T
00114         _LIT(KTxtStartinguseTTypeL,"Starting useTTypeL()\n");
00115         console->Printf(KTxtStartinguseTTypeL);
00116         aIntHolder.Double(); // double the value
00117         TInt i=aIntHolder.Int(); // get value
00118         console->Printf(KCommonFormat1, i);
00119         // optionally leave
00120         //
00121         // Remove comments on next line to show this
00122         //User::Leave(KErrGeneral);
00123 
00124         // now finished
00125         _LIT(KTxtFinishinguseTTypeL,"Finishing useTTypeL()\n");
00126         console->Printf(KTxtFinishinguseTTypeL);
00127         }
00128 
00129 // stuff to demonstrate R type
00130 
00131 void useRTypeL(RTimerWithCleanup aTimer);
00132 
00133 void demonstrateRTypeL()
00134         {
00135         _LIT(KTxtStartDemoRTypeL,"Starting demonstrateRTypeL()\n");
00136         console->Printf(KTxtStartDemoRTypeL);
00137         // declare R type on the stack, and push to clean-up stack
00138         RTimerWithCleanup timer; // declare on stack
00139         timer.CreateLocalLC(); // initialize, push to clean-up stack
00140         // use the R type
00141         TRequestStatus status; // needed for asynchronous service
00142         timer.After(status,1000000); // request completion in 1,000,000 micro seconds
00143         User::WaitForRequest(status); // wait for completion
00144         // optionally leave
00145         //
00146         // Remove comments on next line to show this
00147         //User::Leave(KErrGeneral);
00148 
00149         // pass to another function
00150         useRTypeL(timer); // use in another function
00151         // now finished
00152         CleanupStack::PopAndDestroy(); // pop and destroy timer
00153         _LIT(KTxtFinishDemoRTypeL,"Finishing demonstrateRTypeL()\n");
00154         console->Printf(KTxtFinishDemoRTypeL);
00155         }
00156 
00157 void useRTypeL(RTimerWithCleanup aTimer)
00158         { // use an R type
00159         _LIT(KTxtStartuseRTypeL,"Starting useRTypeL()\n");
00160         console->Printf(KTxtStartuseRTypeL);
00161         // use the R type
00162         TRequestStatus status; // needed for asynchronous service
00163         aTimer.After(status,1000000); // request completion in 1000000 micro seconds
00164         User::WaitForRequest(status); // wait for completion
00165         // optionally leave
00166         //
00167         // Remove comments on next line to show this
00168         //User::Leave(KErrGeneral);
00169 
00170         // now finished
00171         _LIT(KTxtFinishuseRTypeL,"Finishing useRTypeL()\n");
00172         console->Printf(KTxtFinishuseRTypeL);
00173         }

Generated by  doxygen 1.6.2