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