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