00001
00002
00003
00004
00005 #include <e32std.h>
00006 #include <e32base.h>
00007 #include "DescriptorExamples.h"
00008 #include "StringRenderer.h"
00009
00010
00011
00012
00013 class TExample
00014 {
00015 public:
00016 TExample() : iNumber(0), iText() {}
00017 TExample(TInt aNumber, const TDesC &aText);
00018 public:
00019 TInt iNumber;
00020 TBuf<16> iText;
00021 };
00022
00023
00024
00025 LOCAL_C void ShowContents(const TDesC &aMsg,
00026 const TExample aExample,
00027 TPtr &aOutput );
00028
00029
00030
00031 LOCAL_C void ShowContents(const TDesC &aMsg,
00032 const TDesC &aTxt,
00033 TPtr &aOutput);
00034
00035
00036
00037 LOCAL_C void ShowContents(const TDesC &aMsg,
00038 const TDesC8 &aTxt,
00039 TPtr &aOutput);
00040
00041
00042
00043 template <class T>
00044 LOCAL_C void ShowContents(CCirBuf<T> *aFIFO, TPtr &aOutput);
00045
00046
00047
00048
00049
00050
00051
00052 LOCAL_C void ShowBuf(CBufFlat &aBuf, TDes &aOutput);
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 LOCAL_C void ShowBuf(CBufSeg &aBuf, TDes &aOutput);
00063
00064
00065
00066
00067
00068
00069
00070 LOCAL_C void ShowContents(TPckgBuf<TExample> &aPackageBuf, TPtr &aOutput);
00071
00072
00073
00074
00075
00076
00077
00078 LOCAL_C void ShowContents(TPckg<TExample> &aPackage, TPtr &aOutput);
00079
00080
00081 _LIT( KAddedMsg, "\nAdded" );
00082 _LIT( KRemovedMsg, "\nRemoved" );
00083 _LIT8( KChars8, "abcdefghijklmnopqrstuvwxyz0123456789" );
00084
00085
00086
00087
00088 void CDescriptorExamples::CircularBuffersL()
00089 {
00090 TPtr output( iViewer->GetViewBuffer() );
00091 RenderHeader( _L( "CircularBuffers:TText" ), output );
00092
00093
00094
00095
00096 TBuf<10> deletedChars;
00097 _LIT( KNumbersTxt, "0123456789" );
00098
00099 TText *charPtr = (TText*)KNumbersTxt().Ptr();
00100 CCirBuf<TText> *textFIFO;
00101
00102
00103
00104
00105
00106
00107 textFIFO = new (ELeave) CCirBuf<TText>();
00108 CleanupStack::PushL( textFIFO );
00109 textFIFO->SetLengthL( 10 );
00110
00111
00112
00113
00114
00115 textFIFO->Add( charPtr, 5 );
00116 ShowContents( KAddedMsg, KNumbersTxt().Left(5), output );
00117 ShowContents( textFIFO, output );
00118
00119
00120
00121
00122
00123
00124 textFIFO->Remove( (TText*)deletedChars.Ptr(), 3 );
00125 deletedChars.SetLength(3);
00126 ShowContents( KRemovedMsg, deletedChars, output );
00127 ShowContents( textFIFO, output );
00128
00129
00130
00131
00132
00133 textFIFO->Add( charPtr, 6 );
00134 ShowContents( KAddedMsg, KNumbersTxt().Left(6), output );
00135 ShowContents( textFIFO, output );
00136
00137
00138
00139
00140
00141
00142 TInt charsAdded = textFIFO->Add( charPtr, 4 );
00143 ShowContents( KAddedMsg, KNumbersTxt().Left(4), output );
00144 output.AppendFormat( _L("But only %d characters was really added\n"),
00145 charsAdded );
00146 ShowContents( textFIFO, output );
00147
00148
00149
00150
00151
00152 textFIFO->Remove( (TText*)deletedChars.Ptr(), 8 );
00153 deletedChars.SetLength(8);
00154 ShowContents( KRemovedMsg, deletedChars, output );
00155 ShowContents( textFIFO, output );
00156
00157
00158
00159
00160 RenderHeader( _L( "CircularBuffers:TExample" ), output );
00161
00162
00163
00164 CCirBuf<TExample> *exampleFIFO;
00165
00166
00167 TExample removedItem;
00168
00169
00170
00171 exampleFIFO = new (ELeave) CCirBuf<TExample>();
00172 CleanupStack::PushL( exampleFIFO );
00173 exampleFIFO->SetLengthL( 5 );
00174
00175
00176
00177 TExample one(1, _L("one"));
00178 exampleFIFO->Add( &one );
00179 ShowContents( KAddedMsg, one, output );
00180 ShowContents( exampleFIFO, output );
00181
00182 TExample two(2, _L("two"));
00183 exampleFIFO->Add( &two );
00184 ShowContents( KAddedMsg, two, output );
00185 ShowContents( exampleFIFO, output );
00186
00187 TExample three(3, _L("three"));
00188 exampleFIFO->Add( &three );
00189 ShowContents( KAddedMsg, three, output );
00190 ShowContents( exampleFIFO, output );
00191
00192
00193
00194
00195
00196 while( exampleFIFO->Count() > 0 )
00197 {
00198 exampleFIFO->Remove( &removedItem );
00199 ShowContents( KRemovedMsg, removedItem, output );
00200 ShowContents( exampleFIFO, output );
00201 }
00202
00203 iViewer->UpdateView();
00204 CleanupStack::PopAndDestroy(2);
00205 }
00206
00207
00208
00209
00210
00211 void CDescriptorExamples::FlatDynamicBuffersL()
00212 {
00213 TPtr output( iViewer->GetViewBuffer() );
00214 RenderHeader( _L( "FlatDynamicBuffers" ), output );
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233 CBufFlat *buf = CBufFlat::NewL( 10 );
00234 CleanupStack::PushL(buf);
00235
00236
00237 TPtrC8 charsToInsert(NULL, 0);
00238
00239 charsToInsert.Set( KChars8().Mid( 0, 8 ) );
00240 buf->InsertL( 0, charsToInsert );
00241 ShowContents( KAddedMsg, charsToInsert, output );
00242 ShowBuf( *buf, output );
00243
00244 charsToInsert.Set( KChars8().Mid( 8, 8 ) );
00245 buf->InsertL( 8, charsToInsert );
00246 ShowContents( KAddedMsg, charsToInsert, output );
00247 ShowBuf( *buf, output );
00248
00249 charsToInsert.Set( KChars8().Mid( 16, 12 ) );
00250 buf->InsertL( 16, charsToInsert );
00251 ShowContents( KAddedMsg, charsToInsert, output );
00252 ShowBuf( *buf, output );
00253
00254
00255
00256 HBufC *tmpBuf = HBufC::NewL( 50 );
00257 CleanupStack::PushL( tmpBuf );
00258 tmpBuf->Length();
00259
00260
00261
00262
00263
00264 charsToInsert.Set( KChars8().Mid( 4, 20 ) );
00265 buf->InsertL(28, charsToInsert );
00266 ShowContents( KAddedMsg, charsToInsert, output );
00267 ShowBuf( *buf, output );
00268
00269 iViewer->UpdateView();
00270 CleanupStack::PopAndDestroy(2);
00271 }
00272
00273
00274
00275
00276 void CDescriptorExamples::SegmentedDynamicBuffersL()
00277 {
00278 TPtr output( iViewer->GetViewBuffer() );
00279 RenderHeader( _L( "SegmentedDynamicBuffers" ), output );
00280
00281
00282 CBufSeg* buf = CBufSeg::NewL(10);
00283 CleanupStack::PushL( buf );
00284
00285 TPtrC8 charsToInsert(NULL, 0);
00286
00287
00288
00289 charsToInsert.Set( KChars8().Mid(0, 8) );
00290 buf->InsertL(0, charsToInsert );
00291 ShowContents( KAddedMsg, charsToInsert, output );
00292 ShowBuf( *buf, output );
00293
00294
00295
00296
00297 charsToInsert.Set( KChars8().Mid(8, 8) );
00298 buf->InsertL(8, charsToInsert );
00299 ShowContents( KAddedMsg, charsToInsert, output );
00300 ShowBuf( *buf, output );
00301
00302
00303
00304
00305 charsToInsert.Set( KChars8().Mid(16, 12) );
00306 buf->InsertL(16, charsToInsert );
00307 ShowContents( KAddedMsg, charsToInsert, output );
00308 ShowBuf( *buf, output );
00309
00310
00311
00312
00313 charsToInsert.Set( KChars8().Mid(4, 20) );
00314 buf->InsertL(28, charsToInsert );
00315 ShowContents( KAddedMsg, charsToInsert, output );
00316 ShowBuf( *buf, output );
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341 buf->Delete(3, 24);
00342 output.Append( _L("\nDeleted 24 bytes from index 4\n") );
00343 ShowBuf( *buf, output );
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364 TBuf8<20> tmpBuf;
00365 for( TInt i=0; i < buf->Size(); i += tmpBuf.MaxLength() )
00366 {
00367 TInt charsToCopy = buf->Size() - i;
00368
00369
00370 if( charsToCopy > tmpBuf.MaxLength() )
00371 {
00372 charsToCopy = tmpBuf.MaxLength();
00373 }
00374
00375
00376 buf->Read(i, tmpBuf, charsToCopy);
00377
00378
00379 for(TInt j=0; j<charsToCopy; j++)
00380 {
00381 if( j % 2 != 0 )
00382 {
00383 tmpBuf[j] = 'X';
00384 }
00385 }
00386
00387
00388 buf->Write(i, tmpBuf, charsToCopy);
00389 }
00390 output.Append( _L("\nReplaced characters at even index with 'X'\n") );
00391 ShowBuf( *buf, output );
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407 buf->Compress();
00408 output.Append( _L("\nCompressed buffer\n") );
00409 ShowBuf( *buf, output );
00410
00411 iViewer->UpdateView();
00412 CleanupStack::PopAndDestroy(1);
00413 }
00414
00415
00416
00417
00418
00419
00420 void CDescriptorExamples::PackageBuffers()
00421 {
00422 TPtr output( iViewer->GetViewBuffer() );
00423
00424
00425
00426 RenderHeader( _L( "PackageBuffers:TPckgBuf" ), output );
00427
00428
00429
00430
00431
00432 TPckgBuf<TExample> pckgBuf;
00433 output.Append( _L("\nCreated package buffer that stores an empty (by default) TExample object\n") );
00434
00435 ShowContents( pckgBuf, output );
00436
00437
00438
00439
00440
00441 TExample &exampleRef = pckgBuf();
00442 exampleRef.iNumber = 1234567;
00443 output.Append( _L("\nModified iNumber of TExample inside package buffer\n") );
00444 ShowContents( pckgBuf, output );
00445
00446
00447 exampleRef.iText.Copy( _L( "Hello!" ) );
00448 output.Append( _L("\nModified iText of TExample inside package buffer\n") );
00449 ShowContents( pckgBuf, output );
00450
00451
00452
00453
00454 RenderHeader( _L( "PackageBuffers:TPckg" ), output );
00455
00456 TExample example;
00457 TPckg<TExample> pckg( example );
00458 output.Append( _L("\nCreated package buffer that refers to an empty TExample object\n") );
00459 ShowContents(pckg, output);
00460
00461
00462
00463
00464 example.iNumber = 12345;
00465 example.iText.Copy( _L( "Hello!" ) );
00466 output.Append( _L("\nCreated package buffer that refers to an empty TExample object\n") );
00467 ShowContents(pckg, output);
00468
00469 iViewer->UpdateView();
00470 }
00471
00472 void CDescriptorExamples::RBufDemonstrations()
00473 {
00474
00475 RBuf modifiableBuf;
00476 modifiableBuf.CreateL(12);
00477 ASSERT(modifiableBuf.Length()==0);
00478 ASSERT(modifiableBuf.MaxLength()==12);
00479 modifiableBuf.Close();
00480
00481
00482 modifiableBuf.CreateMaxL(12);
00483 ASSERT(modifiableBuf.Length()==12);
00484 ASSERT(modifiableBuf.MaxLength()==12);
00485 modifiableBuf.Close();
00486
00487
00488 _LIT(KHelloWorld, "Hello World");
00489 modifiableBuf.CreateL(KHelloWorld());
00490 ASSERT(modifiableBuf.Length()==11);
00491 ASSERT(modifiableBuf.MaxLength()==11);
00492 modifiableBuf.Close();
00493
00494
00495 modifiableBuf.CreateL(KHelloWorld(), 15);
00496 ASSERT(modifiableBuf.Length()==11);
00497 ASSERT(modifiableBuf.MaxLength()==15);
00498 modifiableBuf.Close();
00499
00500
00501 _LIT(KHello, "Hello");
00502 _LIT(KWorld, " World");
00503 modifiableBuf.CreateL(5);
00504 modifiableBuf.Copy(KHello());
00505 modifiableBuf.CleanupClosePushL();
00506 modifiableBuf.ReAllocL(11);
00507 modifiableBuf.Append(KWorld);
00508 CleanupStack::PopAndDestroy();
00509
00510
00511
00512
00513
00514
00515
00516 HBufC* hBuf = KHello().AllocL();
00517 modifiableBuf.Assign(hBuf);
00518 ASSERT(modifiableBuf.Length()==5);
00519 modifiableBuf.Close();
00520
00521
00522 RBuf myRBuf1;
00523 RBuf myRBuf2;
00524 HBufC* myHBufC = HBufC::NewL(20);
00525 myRBuf1.Assign(myHBufC);
00526 myRBuf2.Assign(myRBuf1);
00527 myRBuf2.Close();
00528
00529
00530 TUint16* ptr = static_cast<TUint16*> (User::AllocL(5*sizeof(TText)));
00531 modifiableBuf.Assign(ptr,5);
00532 ASSERT(modifiableBuf.Length()==0);
00533 modifiableBuf.Copy(KHello());
00534
00535
00536 modifiableBuf.CleanupClosePushL();
00537 modifiableBuf.ReAllocL(12);
00538 modifiableBuf.Append(KWorld);
00539 CleanupStack::PopAndDestroy();
00540 }
00541
00542
00543
00544
00545 TExample::TExample(TInt aNumber, const TDesC &aText)
00546 {
00547 iNumber = aNumber;
00548 iText.Copy(aText.Left( iText.MaxLength() ) );
00549 }
00550
00551
00552
00553
00554 LOCAL_C void ShowContents(const TDesC& aMsg,
00555 const TExample aExample,
00556 TPtr &aOutput)
00557 {
00558 _LIT( KFormat, "%S: %d, \"%S\"\n" );
00559 aOutput.AppendFormat( KFormat, &aMsg, aExample.iNumber, &aExample.iText );
00560 }
00561
00562 LOCAL_C void ShowContents(const TDesC& aMsg, const TDesC &aTxt, TPtr &aOutput)
00563 {
00564 _LIT( KFormat, "%S: \"%S\"\n" );
00565 aOutput.AppendFormat( KFormat, &aMsg, &aTxt );
00566 }
00567
00568 LOCAL_C void ShowContents(const TDesC& aMsg, const TDesC8 &aTxt, TPtr &aOutput)
00569 {
00570 TBuf<128> buf;
00571 buf.Copy(aTxt.Left(buf.MaxLength()));
00572 _LIT( KFormat, "%S: \"%S\"\n" );
00573 aOutput.AppendFormat( KFormat, &aMsg, &buf );
00574 }
00575
00576 template <class T>
00577 LOCAL_C void ShowContents(CCirBuf<T> *aFIFO, TPtr &aOutput)
00578 {
00579 _LIT( KFIFOFormat, "fifo: size=%d, max=%d\n" );
00580 aOutput.AppendFormat( KFIFOFormat, aFIFO->Count(), aFIFO->Length() );
00581 }
00582
00583 LOCAL_C void ShowBuf(CBufFlat &aBuf, TDes &aOutput)
00584 {
00585 TPtrC8 data = aBuf.Ptr(0);
00586 aOutput.AppendFormat( _L("CBufFlat: size=%d, data @%d=\n\""), aBuf.Size(), data.Ptr() );
00587 Append(data, aOutput);
00588 aOutput.Append(_L("\"\n"));
00589 }
00590
00591 LOCAL_C void ShowBuf(CBufSeg &aBuf, TDes &aOutput)
00592 {
00593 aOutput.AppendFormat( _L("CBufSeg: size=%d, segments=\n"), aBuf.Size() );
00594 TInt pos = 0;
00595 while( pos < aBuf.Size() )
00596 {
00597 TPtrC8 ptr = aBuf.Ptr(pos);
00598 aOutput.Append( _L(" \"") );
00599 Append(ptr, aOutput);
00600 aOutput.AppendFormat( _L("\" @%d\n"), ptr.Ptr() );
00601 pos += ptr.Length();
00602 }
00603 }
00604
00605 LOCAL_C void ShowContents(TPckgBuf<TExample> &aPackageBuf, TPtr &aOutput)
00606 {
00607 aOutput.AppendFormat( _L( "TPckgBuf @%d, sizeof=%d, storing\n TExample @%d, sizeof=%d, iNumber=%d, iText=\"%S\"\n" ),
00608 &aPackageBuf, sizeof(aPackageBuf), &aPackageBuf(), sizeof(aPackageBuf()), aPackageBuf().iNumber, &aPackageBuf().iText );
00609 }
00610
00611 LOCAL_C void ShowContents(TPckg<TExample> &aPackage, TPtr &aOutput)
00612 {
00613 aOutput.AppendFormat( _L( "TPckg @%d, sizeof=%d, referring to\n TExample @%d, sizeof=%d, iNumber=%d, iText=\"%S\"\n" ),
00614 &aPackage, sizeof(aPackage), &aPackage(), sizeof(aPackage()), aPackage().iNumber, &aPackage().iText );
00615 }
00616