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 // Examples to demonstrate the use of descriptors in function interfaces. 00015 // 00016 00017 #include "CommonFramework.h" 00018 00019 // 00020 // Literal text 00021 // 00022 00023 _LIT(KDataTPtrC,"A TPtrC descriptor"); 00024 _LIT(KDataTPtr,"A TPtr descriptor"); 00025 _LIT(KDataTBufC,"A TBufC descriptor"); 00026 _LIT(KDataTBuf,"A TBuf descriptor"); 00027 _LIT(KDataHBufC,"An HBufC descriptor"); 00028 00029 // 00030 // Common format strings 00031 // 00032 00033 _LIT(KCommonFormat2,"0x%02x "); 00034 00035 // A set of functions called by the example code 00036 LOCAL_C void StringRead(const TDesC& aString); 00037 LOCAL_C void StringWrite(TDes& aString); 00038 LOCAL_C void BufferRead(const TDesC8& aBuffer); 00039 LOCAL_C void BufferWrite(TDes8& aBuffer); 00040 00041 // Do the example 00042 LOCAL_C void doExampleL() 00043 { 00044 // We create four functions: 00045 // StringRead() 00046 // StringWrite() 00047 // BufferRead() 00048 // BufferWrite() 00049 // to illustrate the use of descriptors 00050 // in function interfaces 00051 00052 TText area[48]; 00053 TPtrC ptrc; 00054 TPtr ptr(&area[0],48); 00055 00056 TBufC<48> bufc; 00057 TBuf<48> buf; 00058 HBufC* hbufcptr; 00059 00060 hbufcptr = HBufC::NewL(48); 00061 00062 // Set up some data for our 00063 // descriptors 00064 ptrc.Set(KDataTPtrC); // "A TPtrC descriptor" 00065 ptr = KDataTPtr; // "A TPtr descriptor" 00066 bufc = KDataTBufC; // "A TBufC descriptor" 00067 buf = KDataTBuf; // "A TBuf descriptor" 00068 *hbufcptr = KDataHBufC; // "An HBufC descriptor" 00069 00070 // We can pass a reference to all 00071 // descriptor types to StringRead() but the 00072 // function cannot change the 00073 // descriptor content 00074 StringRead(ptrc); // <-- a TPtrC 00075 StringRead(ptr); // <-- a TPtr 00076 StringRead(bufc); // <-- a TBufC 00077 StringRead(buf); // <-- a TBuf 00078 StringRead(*hbufcptr); // <-- an HBufC 00079 00080 // We can only pass a reference to those 00081 // descriptors which are derived 00082 // from TDes, to StringWrite(); 00083 // these are the modifiable 00084 // descriptors: TPtr and TBuf. 00085 // 00086 // The compiler will not permit a reference 00087 // to any other descriptor type to 00088 // be passed. 00089 //StringWrite(ptrc); <-- Illegal 00090 StringWrite(ptr); // <-- a TPtr 00091 //StringWrite(bufc); <-- Illegal 00092 StringWrite(buf); // <-- a TBuf 00093 //StringWrite(*hbufcptr); <-- Illegal 00094 00095 delete hbufcptr; 00096 00097 _LIT(KTxtPressToContinue," (press any key to continue)\n"); 00098 console->Printf(KTxtPressToContinue); 00099 console->Getch(); 00100 00101 TUint8 data1[3] = {0x00,0x01,0x02}; 00102 TUint8 data2[3] = {0x03,0x04,0x05}; 00103 TUint8 data3[3] = {0x06,0x07,0x08}; 00104 TUint8 data4[3] = {0x09,0x0A,0x0B}; 00105 TUint8 data5[3] = {0x0C,0x0D,0x0E}; 00106 00107 // Use the 8 bit variants explicitly for 00108 // general binary data. 00109 00110 // ptrc8's data area is data1[] 00111 // ptr8's data areais data2[] 00112 TPtrC8 ptrc8(&data1[0],3); 00113 TPtr8 ptr8(&data2[0],3,3); 00114 00115 // bufc8 contains a copy of data3[] data 00116 // buf8 contains a copy of data4[] data 00117 // hbufcptr8 contains a copy of data5[] data 00118 TBufC8<3> bufc8= TPtrC8(&data3[0],3); 00119 TBuf8<3> buf8= TPtrC8(&data4[0],3); 00120 HBufC8* hbufcptr8; 00121 00122 hbufcptr8 = HBufC8::NewL(3); 00123 *hbufcptr8 = TPtrC8(&data5[0],3); 00124 00125 // We can pass a reference to all 00126 // descriptor types to BufferRead() 00127 // but the function cannot change the 00128 // descriptor content 00129 BufferRead(ptrc8); // <-- a TPtrC 00130 BufferRead(ptr8); // <-- a TPtr 00131 BufferRead(bufc8); // <-- a TBufC 00132 BufferRead(buf8); // <-- a TBuf 00133 BufferRead(*hbufcptr8); // <-- an HBufC 00134 00135 // We can only pass a reference to those 00136 // descriptors which are derived 00137 // from TDes, to BufferWrite; these are 00138 // the modifiable descriptors: TPtr and TBuf. 00139 // 00140 // The compiler will not permit a reference 00141 // to any other descriptor type to 00142 // be passed. 00143 //BufferWrite(ptrc8); <-- Illegal 00144 BufferWrite(ptr8); // <-- a TPtr 00145 //BufferWrite(bufc8); <-- Illegal 00146 BufferWrite(buf8); // <-- a TBuf 00147 //BufferWrite(*hbufcptr8); <-- Illegal 00148 00149 delete hbufcptr8; 00150 } 00151 00152 00153 00154 // ************************************************ 00155 // Functions used by doInterface() 00156 // ************************************************ 00157 LOCAL_C void StringRead(const TDesC& aString) 00158 { 00159 // A function which handles the content 00160 // of ANY descriptor passed to it but CANNOT 00161 // change that descriptor. 00162 // 00163 // Function displays descriptor content, 00164 // length and size. 00165 // 00166 // All descriptors can be passed to this 00167 // function because all descriptors are 00168 // ultimately derived from TDesC. 00169 // 00170 // Note, however, that all data members and 00171 // function members in the TDes abstract class 00172 // cannot be accessed (recall that TDes is 00173 // derived from TDesC). 00174 // These include the data member containing 00175 // the maximum length and all modifying 00176 // member functions. 00177 00178 _LIT(KFormat5,"\"%S\"; %d; %d\n"); 00179 console->Printf(KFormat5,&aString,aString.Length(),aString.Size()); 00180 } 00181 00182 00183 LOCAL_C void StringWrite(TDes& aString) 00184 { 00185 // A function which handles the content 00186 // of a descriptor derived from TDes; 00187 // i.e. the modifiable descriptors TPtr and 00188 // TBuf. 00189 // 00190 // Function changes the content of the 00191 // referenced descriptors and then displays 00192 // their content, length, size and maximum 00193 // length. 00194 // 00195 // Note that the references to TPtrC, TBufC 00196 // and HBufC cannot be passed. 00197 _LIT(KTxtModStringWrite," modified by StringWrite"); 00198 aString.Append(KTxtModStringWrite); 00199 _LIT(KFormat3,"\"%S\"; %d; %d; %d\n"); 00200 console->Printf(KFormat3, 00201 &aString, 00202 aString.Length(), 00203 aString.Size(), 00204 aString.MaxLength() 00205 ); 00206 } 00207 00208 00209 LOCAL_C void BufferRead(const TDesC8& aBuffer) 00210 { 00211 // A function which handles the content 00212 // of ANY descriptor passed to it but 00213 // CANNOT change that descriptor. 00214 // 00215 // Function displays descriptor content, 00216 // length and size. 00217 // 00218 // All descriptors can be passed to this 00219 // function because all descriptors are 00220 // ultimately derived from TDesC. 00221 // 00222 // Note, however, that all data members 00223 // and function members in the TDes abstract 00224 // class cannot be accessed (recall that 00225 // TDes is derived from TDesC). 00226 // These include the data member containing 00227 // the maximum length and all modifying 00228 // member functions. 00229 for (TInt ix = 0; ix < aBuffer.Length(); ix++) 00230 console->Printf(KCommonFormat2,aBuffer[ix]); 00231 00232 _LIT(KFormat4,"; %d; %d\n"); 00233 console->Printf(KFormat4,aBuffer.Length(),aBuffer.Size()); 00234 } 00235 00236 00237 LOCAL_C void BufferWrite(TDes8& aBuffer) 00238 { 00239 // A function which handles the content 00240 // of a descriptor derived from TDes; 00241 // i.e. the modifiable descriptors TPtr 00242 // and TBuf 00243 // 00244 // Function changes the content of the 00245 // referenced descriptors and then displays 00246 // their content, length, size and maximum 00247 // length. 00248 // 00249 // Note that the references to TPtrC, TBufC 00250 // and HBufC cannot be passed. 00251 00252 _LIT(KTxtModBufferWrite,"Modified by BufferWrite "); 00253 console->Printf(KTxtModBufferWrite); 00254 00255 for (TInt ix = 0; ix < aBuffer.Length(); ix++) 00256 { 00257 aBuffer[ix] += 0xF0; 00258 console->Printf(KCommonFormat2,aBuffer[ix]); 00259 } 00260 00261 _LIT(KFormat1,"; %d; %d; %d\n"); 00262 console->Printf(KFormat1, 00263 aBuffer.Length(), 00264 aBuffer.Size(), 00265 aBuffer.MaxLength() 00266 ); 00267 } 00268 00269