00001 /* 00002 Copyright (c) 2000-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. 00003 00004 Redistribution and use in source and binary forms, with or without 00005 modification, are permitted provided that the following conditions are met: 00006 00007 * Redistributions of source code must retain the above copyright notice, this 00008 list of conditions and the following disclaimer. 00009 * Redistributions in binary form must reproduce the above copyright notice, 00010 this list of conditions and the following disclaimer in the documentation 00011 and/or other materials provided with the distribution. 00012 * Neither the name of Nokia Corporation nor the names of its contributors 00013 may be used to endorse or promote products derived from this software 00014 without specific prior written permission. 00015 00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00026 00027 Description: TAndRClass.cpp 00028 */ 00029 00030 00031 #include "CommonFramework.h" 00032 00033 _LIT(KTxtTIntHolderDestructor,"TIntHolder destructor\n"); 00034 _LIT(KCommonFormat1,"Int()=%d\n"); 00035 00036 // T class 00037 00038 class TIntHolder 00039 { 00040 public: // data 00041 TInt iInt; 00042 public: // functions 00043 TInt Int() { return iInt; }; // get the integer 00044 void SetInt(TInt aInt) { iInt=aInt; }; // set the integer 00045 void Double() { iInt *= 2; }; // double the integer 00046 TBool IsNonZero() { return iInt!=0; }; // check whether zero 00047 // destructor, coded only to show when taken and when not 00048 ~TIntHolder() { console->Printf(KTxtTIntHolderDestructor); }; 00049 }; 00050 00051 void demonstrateTTypeL(); // function to demonstrate T type 00052 00053 // class RTimer - a typical R class 00054 00055 // an R class with added clean-up support 00056 00057 class RTimerWithCleanup : public RTimer 00058 { 00059 public: // no extra data, just inherit everything from base 00060 public: // functions 00061 // required for clean-up support 00062 operator TCleanupItem() 00063 { // convert to clean-up item 00064 return TCleanupItem(Cleanup,this); 00065 // clean-up = function + object 00066 } 00067 static void Cleanup(TAny *aPtr) 00068 { // static clean-up function returned by TCleanupItem 00069 ((RTimerWithCleanup*)aPtr) -> DoCleanup(); 00070 // do clean-up neatly, using regular member function 00071 } 00072 void DoCleanup() 00073 { // do clean-up neatly 00074 Close(); // close timer 00075 } 00076 // helpful for automatically putting onto clean-up stack 00077 void CreateLocalLC() 00078 { // call RTImer::CreateLocal(), and push to clean-up stack 00079 User::LeaveIfError(CreateLocal()); // create, but leave if couldn't 00080 CleanupStack::PushL(*this); // push to clean-up stack 00081 } 00082 // debug version of RTimer::Close() 00083 void Close() 00084 { // debug version of close 00085 _LIT(KTxtClosingRTimer,"Closing RTimerWithCleanup\n"); 00086 console->Printf(KTxtClosingRTimer); 00087 RTimer::Close(); // base call 00088 } 00089 }; 00090 00091 void demonstrateRTypeL(); // function to demonstrate R type 00092 00093 // Do the example 00094 LOCAL_C void doExampleL() 00095 { 00096 demonstrateTTypeL(); // demonstrate T type 00097 demonstrateRTypeL(); // demonstrate R type 00098 } 00099 00100 // stuff to demonstrate T type 00101 00102 void useTTypeL(TIntHolder aIntHolder); // function using a T 00103 00104 void demonstrateTTypeL() 00105 { 00106 _LIT(KTxtStartingDemoTTypeL,"Starting demonstrateTTypeL()\n"); 00107 console->Printf(KTxtStartingDemoTTypeL); 00108 TIntHolder t; // declare variable 00109 // do a few things with it 00110 t.SetInt(3); 00111 t.Double(); 00112 TInt i=t.Int(); 00113 console->Printf(KCommonFormat1, i); 00114 // optionally leave 00115 // 00116 // Remove comments on next line to show this 00117 //User::Leave(KErrGeneral); 00118 00119 // now use T type 00120 useTTypeL(t); // pass by value 00121 // end demo: T will be destructed (for what it's worth) 00122 _LIT(KTxtFinishingDemoTTypeL,"Finishing demonstrateTTypeL()\n"); 00123 console->Printf(KTxtFinishingDemoTTypeL); 00124 } 00125 00126 void useTTypeL(TIntHolder aIntHolder) 00127 { // function using a T 00128 _LIT(KTxtStartinguseTTypeL,"Starting useTTypeL()\n"); 00129 console->Printf(KTxtStartinguseTTypeL); 00130 aIntHolder.Double(); // double the value 00131 TInt i=aIntHolder.Int(); // get value 00132 console->Printf(KCommonFormat1, i); 00133 // optionally leave 00134 // 00135 // Remove comments on next line to show this 00136 //User::Leave(KErrGeneral); 00137 00138 // now finished 00139 _LIT(KTxtFinishinguseTTypeL,"Finishing useTTypeL()\n"); 00140 console->Printf(KTxtFinishinguseTTypeL); 00141 } 00142 00143 // stuff to demonstrate R type 00144 00145 void useRTypeL(RTimerWithCleanup aTimer); 00146 00147 void demonstrateRTypeL() 00148 { 00149 _LIT(KTxtStartDemoRTypeL,"Starting demonstrateRTypeL()\n"); 00150 console->Printf(KTxtStartDemoRTypeL); 00151 // declare R type on the stack, and push to clean-up stack 00152 RTimerWithCleanup timer; // declare on stack 00153 timer.CreateLocalLC(); // initialize, push to clean-up stack 00154 // use the R type 00155 TRequestStatus status; // needed for asynchronous service 00156 timer.After(status,1000000); // request completion in 1,000,000 micro seconds 00157 User::WaitForRequest(status); // wait for completion 00158 // optionally leave 00159 // 00160 // Remove comments on next line to show this 00161 //User::Leave(KErrGeneral); 00162 00163 // pass to another function 00164 useRTypeL(timer); // use in another function 00165 // now finished 00166 CleanupStack::PopAndDestroy(); // pop and destroy timer 00167 _LIT(KTxtFinishDemoRTypeL,"Finishing demonstrateRTypeL()\n"); 00168 console->Printf(KTxtFinishDemoRTypeL); 00169 } 00170 00171 void useRTypeL(RTimerWithCleanup aTimer) 00172 { // use an R type 00173 _LIT(KTxtStartuseRTypeL,"Starting useRTypeL()\n"); 00174 console->Printf(KTxtStartuseRTypeL); 00175 // use the R type 00176 TRequestStatus status; // needed for asynchronous service 00177 aTimer.After(status,1000000); // request completion in 1000000 micro seconds 00178 User::WaitForRequest(status); // wait for completion 00179 // optionally leave 00180 // 00181 // Remove comments on next line to show this 00182 //User::Leave(KErrGeneral); 00183 00184 // now finished 00185 _LIT(KTxtFinishuseRTypeL,"Finishing useRTypeL()\n"); 00186 console->Printf(KTxtFinishuseRTypeL); 00187 }