00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00022 #include "circularbuffer.h"
00023
00024
00025 _LIT(KPressAKey, "\n\tPress any key to continue....");
00026 _LIT(KConstruct,"\n\n ****Construct****");
00027 _LIT(KAdd,"\n ****Add objects****\n");
00028 _LIT(KNumberOfObjects,"\nNumber of objects in the circular buffer: %d\n");
00029 _LIT(KAddFailed,"\nElement cannot be added because the circular buffer is full\n");
00030 _LIT(KRemove,"\n ****Remove objects****\n");
00031
00032
00036 TMyClass::TMyClass(){};
00040 TMyClass::TMyClass(const TDesC& aDes)
00041 {
00042 iBuf = aDes;
00043 }
00048 const TDesC& TMyClass::GetBuf()
00049 {
00050 return iBuf;
00051 }
00056 void TMyClass::SetBuf(const TDesC& aDes)
00057 {
00058 iBuf = aDes;
00059 }
00060
00064 CCircularBufferExample::CCircularBufferExample()
00065 {
00066 }
00067
00071 void CCircularBufferExample::ConstructL()
00072 {
00073 _LIT(KTitle, "Circular Buffer Example" );
00074 iConsole = Console::NewL(KTitle, TSize(KConsFullScreen, KConsFullScreen));
00075
00076 _LIT(KWelcome, "\n Welcome to the circular buffer example application");
00077 iConsole->Printf(KWelcome);
00078
00079 _LIT(KPressAKeyMsg, "\n\n Press any key to step through the example\n");
00080 iConsole->Printf(KPressAKeyMsg );
00081 iConsole->Getch();
00082 }
00083
00087 CCircularBufferExample::~CCircularBufferExample()
00088 {
00089 delete iConsole;
00090 }
00091
00097 CCircularBufferExample* CCircularBufferExample::NewL()
00098 {
00099 CCircularBufferExample* self=new(ELeave)CCircularBufferExample();
00100 CleanupStack::PushL(self);
00101 self->ConstructL();
00102 CleanupStack::Pop(self);
00103 return self;
00104 }
00108 void CCircularBufferExample::CircularBufferOfIntsL()
00109 {
00110 _LIT(KCircularBufferForInt,"\n *****Circular buffer for integers*****");
00111 iConsole->Printf(KCircularBufferForInt);
00112
00113
00114 CCirBuffer* circularBuffer = new(ELeave)CCirBuffer;
00115
00116 CleanupStack::PushL(circularBuffer);
00117 const TInt KMaxElements = 4;
00118
00119 circularBuffer->SetLengthL(KMaxElements);
00120
00121 iConsole->Printf(KConstruct);
00122 _LIT(KConstructCircularBuffer, "\n Construction of circular buffer containing integers is successful");
00123 iConsole->Printf(KConstructCircularBuffer);
00124
00125
00126 iConsole->Printf(KAdd);
00127 TUint element[6]= {1, 2, 3, 4, 5, 6};
00128 iConsole->Printf(KNumberOfObjects, circularBuffer->Count());
00129
00130 _LIT(KAddedElements,"Added Element: %d\n");
00131 TInt result=circularBuffer->Put(element[0]);
00132 User::LeaveIfError(result);
00133 iConsole->Printf(KAddedElements,element[0]);
00134 iConsole->Printf(KNumberOfObjects, circularBuffer->Count());
00135
00136
00137 TUint numberOfObjects= 3;
00138 for(TUint index=1;index<=numberOfObjects; index++)
00139 {
00140 result= circularBuffer->Put(element[index]);
00141 User::LeaveIfError(result);
00142
00143 iConsole->Printf(KAddedElements,element[index]);
00144 iConsole->Printf(KNumberOfObjects, circularBuffer->Count());
00145 }
00146
00147 _LIT(KAddIntegersToCircularBuffer,"\nAdding integers to circular buffer is successful");
00148 iConsole->Printf(KAddIntegersToCircularBuffer);
00149
00150 iConsole->Printf(KPressAKey);
00151 iConsole->Getch();
00152
00153 _LIT(KTryingAddToCircularBuffer,"\nTrying to add when buffer is full.");
00154 iConsole->Printf(KTryingAddToCircularBuffer);
00155 result=circularBuffer->Put(element[4]);
00156 ASSERT(result == KErrGeneral);
00157 iConsole->Printf(KAddFailed);
00158
00159
00160 iConsole->Printf(KRemove);
00161 iConsole->Printf(KNumberOfObjects, circularBuffer->Count());
00162
00163 result = circularBuffer->Get();
00164 _LIT(KElementRemoved,"Removed Element: %d\n");
00165 User::LeaveIfError(result);
00166 ASSERT( (TUint) result == element[0]);
00167
00168 iConsole->Printf(KElementRemoved, result);
00169 iConsole->Printf(KNumberOfObjects, circularBuffer->Count());
00170
00171
00172 result = circularBuffer->Get();
00173 User::LeaveIfError(result);
00174 ASSERT((TUint)result == element[1]);
00175
00176 iConsole->Printf(KElementRemoved, result);
00177 iConsole->Printf(KNumberOfObjects, circularBuffer->Count());
00178
00179
00180 for(TUint index=4;index<=5; index++)
00181 {
00182 result= circularBuffer->Put(element[index]);
00183 User::LeaveIfError(result);
00184 iConsole->Printf(KAddedElements,element[index]);
00185 iConsole->Printf(KNumberOfObjects, circularBuffer->Count());
00186 }
00187 iConsole->Printf(KPressAKey);
00188 iConsole->Getch();
00189 _LIT(KNewLine,"\n");
00190 iConsole->Printf(KNewLine);
00191
00192 for(TUint index=2;index<=5; index++)
00193 {
00194 result= circularBuffer->Get();
00195 User::LeaveIfError(result);
00196 ASSERT((TUint)result == element[index]);
00197
00198 iConsole->Printf(KElementRemoved,result);
00199 iConsole->Printf(KNumberOfObjects, circularBuffer->Count());
00200 }
00201
00202 _LIT(KRemoveIntegersFromCircularBuffer,"\nRemoving integers from the circular buffer is successful");
00203 iConsole->Printf(KRemoveIntegersFromCircularBuffer);
00204 iConsole->Printf(KPressAKey);
00205 iConsole->Getch();
00206
00207
00208 CleanupStack::PopAndDestroy(circularBuffer);
00209 }
00210
00215 void CCircularBufferExample::CircularBufferOfMyObjectsL()
00216 {
00217 _LIT(KCircularBufferForMyObject,"\n *****Circular buffer of objects of user defined class TMyClass*****");
00218 iConsole->Printf(KCircularBufferForMyObject);
00219
00220
00221 CCirBuf<TMyClass>* circularBuffer=new(ELeave) CCirBuf<TMyClass>;
00222
00223 CleanupStack::PushL(circularBuffer);
00224
00225 const TInt KMaxElements = 4;
00226 circularBuffer->SetLengthL(KMaxElements);
00227 iConsole->Printf(KConstruct);
00228
00229 _LIT(KConstructCircularBufferForMyObject, "\n Construction of circular buffer of user defined class is successful\n");
00230 iConsole->Printf(KConstructCircularBufferForMyObject);
00231
00232 iConsole->Printf(KAdd);
00233
00234 TMyClass myObjectsToAdd[6];
00235
00236 _LIT(KBuffer1,"first");
00237 myObjectsToAdd[0].SetBuf(KBuffer1);
00238 _LIT(KBuffer2,"second");
00239 myObjectsToAdd[1].SetBuf(KBuffer2);
00240 _LIT(KBuffer3,"third");
00241 myObjectsToAdd[2].SetBuf(KBuffer3);
00242 _LIT(KBuffer4,"fourth");
00243 myObjectsToAdd[3].SetBuf(KBuffer4);
00244 _LIT(KBuffer5,"fifth");
00245 myObjectsToAdd[4].SetBuf(KBuffer5);
00246 _LIT(KBuffer6,"sixth");
00247 myObjectsToAdd[5].SetBuf(KBuffer6);
00248
00249 _LIT(KAddedMyObjectElements,"\nTMyClass object added is: ");
00250 _LIT(KPrintMsgFormat," \"%S\" ");
00251
00252 TInt result= circularBuffer->Add(&myObjectsToAdd[0]);
00253 User::LeaveIfError(result);
00254 ASSERT(result==1);
00255 iConsole->Printf(KAddedMyObjectElements);
00256 iConsole->Printf(KPrintMsgFormat,&(myObjectsToAdd[0].GetBuf()));
00257 iConsole->Printf(KNumberOfObjects, circularBuffer->Count());
00258
00259
00260 _LIT(KAddedMultipleMyObjectElements,"\nTMyClass objects added are : ");
00261
00262 result= circularBuffer->Add(&myObjectsToAdd[1], 3);
00263 User::LeaveIfError(result);
00264 ASSERT(result == 3);
00265 iConsole->Printf(KAddedMultipleMyObjectElements);
00266 iConsole->Printf(KPrintMsgFormat,&(myObjectsToAdd[1].GetBuf()));
00267 iConsole->Printf(KPrintMsgFormat,&(myObjectsToAdd[2].GetBuf()));
00268 iConsole->Printf(KPrintMsgFormat,&(myObjectsToAdd[3].GetBuf()));
00269 iConsole->Printf(KNumberOfObjects, circularBuffer->Count());
00270
00271 _LIT(KAddMyObjectsToCircularBuffer,"\nAdding objects of TMyClasss to circular buffer is successful");
00272 iConsole->Printf(KAddMyObjectsToCircularBuffer);
00273 iConsole->Printf(KPressAKey);
00274 iConsole->Getch();
00275
00276
00277
00278 TMyClass myRemovedObjects[6];
00279 _LIT(KRemovedMultipleMyObjectElements,"\nTMyClass objects removed are: ");
00280
00281
00282 result= circularBuffer->Remove(&myRemovedObjects[0],2);
00283 ASSERT(result == 2);
00284 iConsole->Printf(KRemovedMultipleMyObjectElements);
00285 iConsole->Printf(KPrintMsgFormat,&(myRemovedObjects[0].GetBuf()));
00286 iConsole->Printf(KPrintMsgFormat,&(myRemovedObjects[1].GetBuf()));
00287 iConsole->Printf(KNumberOfObjects, circularBuffer->Count());
00288
00289
00290 result= circularBuffer->Add(&myObjectsToAdd[4],2);
00291 User::LeaveIfError(result);
00292 ASSERT(result == 2);
00293 iConsole->Printf(KAddedMultipleMyObjectElements);
00294 iConsole->Printf(KPrintMsgFormat,&(myObjectsToAdd[4].GetBuf()));
00295 iConsole->Printf(KPrintMsgFormat,&(myObjectsToAdd[5].GetBuf()));
00296 iConsole->Printf(KNumberOfObjects, circularBuffer->Count());
00297
00298
00299 result= circularBuffer->Remove(&myRemovedObjects[2],4);
00300 ASSERT(result == 4);
00301 iConsole->Printf(KRemovedMultipleMyObjectElements);
00302 iConsole->Printf(KPrintMsgFormat,&(myRemovedObjects[2].GetBuf()));
00303 iConsole->Printf(KPrintMsgFormat,&(myRemovedObjects[3].GetBuf()));
00304 iConsole->Printf(KPrintMsgFormat,&(myRemovedObjects[4].GetBuf()));
00305 iConsole->Printf(KPrintMsgFormat,&(myRemovedObjects[5].GetBuf()));
00306 iConsole->Printf(KNumberOfObjects, circularBuffer->Count());
00307
00308 for(TInt index=0;index<6;index++)
00309 {
00310
00311 ASSERT(myRemovedObjects[index].GetBuf() == myObjectsToAdd[index].GetBuf());
00312 }
00313
00314 _LIT(KRemoveMyObjectsFromCircularBuffer,"\nRemoving TMyClass objects from circular buffer is successful");
00315 iConsole->Printf(KRemoveMyObjectsFromCircularBuffer);
00316 iConsole->Printf(KPressAKey);
00317 iConsole->Getch();
00318
00319
00320 CleanupStack::PopAndDestroy(circularBuffer);
00321 }
00322
00326 void CCircularBufferExample::CircularBufferOfRClasssL()
00327 {
00328 _LIT(KCircularBufferForRBuf,"\n *****Circular buffer of R Class*****");
00329 iConsole->Printf(KCircularBufferForRBuf);
00330
00331 CCirBuf<RBuf>* circularBuffer=new(ELeave)CCirBuf<RBuf>;
00332
00333 CleanupStack::PushL(circularBuffer);
00334
00335 const TInt KMaxElements = 2;
00336
00337 circularBuffer->SetLengthL(KMaxElements);
00338 iConsole->Printf(KConstruct);
00339 _LIT(KConstructCircularBufferForRBuf, "\n Construction of circular buffer of R Class is successful");
00340 iConsole->Printf(KConstructCircularBufferForRBuf);
00341
00342 RBuf bufToAdd1;
00343 _LIT(KBuffer1,"11111");
00344
00345 bufToAdd1.CreateL(KBuffer1);
00346
00347 iConsole->Printf(KNumberOfObjects, circularBuffer->Count());
00348
00349 _LIT(KAddedElements,"Added Element: ");
00350 iConsole->Printf(KAdd);
00351 TInt result=circularBuffer->Add(&bufToAdd1);
00352 User::LeaveIfError(result);
00353 ASSERT(result == 1);
00354 iConsole->Printf(KAddedElements);
00355 iConsole->Printf(bufToAdd1);
00356 iConsole->Printf(KNumberOfObjects, circularBuffer->Count());
00357
00358 _LIT(KBuffer2,"22222");
00359 RBuf bufToAdd2;
00360
00361 bufToAdd2.CreateL(KBuffer2);
00362
00363 result=circularBuffer->Add(&bufToAdd2);
00364 User::LeaveIfError(result);
00365 ASSERT(result == 1);
00366 iConsole->Printf(KAddedElements);
00367 iConsole->Printf(bufToAdd2);
00368 iConsole->Printf(KNumberOfObjects, circularBuffer->Count());
00369
00370 _LIT(KAddRBufferToCircularBuffer,"\nAdding to circular buffer of R Class is successful\n");
00371 iConsole->Printf(KAddRBufferToCircularBuffer);
00372
00373 RBuf bufToRemove;
00374 iConsole->Printf(KRemove);
00375 _LIT(KElementRemoved,"Removed Element: ");
00376
00377 result=circularBuffer->Remove(&bufToRemove);
00378 ASSERT(result == 1);
00379 iConsole->Printf(KElementRemoved);
00380 iConsole->Printf(bufToRemove);
00381 iConsole->Printf(KNumberOfObjects, circularBuffer->Count());
00382
00383
00384 bufToAdd2.Close();
00385 bufToRemove.Close();
00386 CleanupStack::PopAndDestroy(circularBuffer);
00387 _LIT(KPressAnyKeyToExit,"\nPress any key to exit ");
00388 iConsole->Printf(KPressAnyKeyToExit);
00389 iConsole->Getch();
00390 }
00391
00392
00393 void MainL()
00394 {
00395 CCircularBufferExample* app= CCircularBufferExample::NewL();
00396 CleanupStack::PushL(app);
00397
00398
00399 app->CircularBufferOfIntsL();
00400
00401
00402 app->CircularBufferOfMyObjectsL();
00403
00404
00405 app->CircularBufferOfRClasssL();
00406 CleanupStack::PopAndDestroy(app);
00407 }
00408
00409 GLDEF_C TInt E32Main()
00410 {
00411 __UHEAP_MARK;
00412
00413 CTrapCleanup* cleanup = CTrapCleanup::New();
00414 if(cleanup == NULL)
00415 {
00416 return KErrNoMemory;
00417 }
00418 TRAPD(err, MainL());
00419 if(err !=KErrNone)
00420 {
00421 _LIT(KFailed, "\nFailed to complete");
00422 User::Panic(KFailed, err);
00423 }
00424 delete cleanup;
00425
00426 __UHEAP_MARKEND;
00427 return KErrNone;
00428 }