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