examples/Base/BufsAndStrings/circularbufferexample/circularbuffer.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description: This example program demonstrates the circular buffer API.  
00028 */
00029 
00030 
00034 #include "circularbuffer.h"
00035 
00036 // Literals.
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         // Creates a circular buffer containing integers.
00126         CCirBuffer* circularBuffer = new(ELeave)CCirBuffer;
00127         // Push the circular buffer onto the cleanup stack.
00128         CleanupStack::PushL(circularBuffer);
00129         const TInt KMaxElements = 4;
00130         // Sets the maximum capacity of this circular buffer.
00131         circularBuffer->SetLengthL(KMaxElements);  // Maximum capacity is 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]); // Add integers to the circular buffer.
00144         User::LeaveIfError(result);
00145         iConsole->Printf(KAddedElements,element[0]);
00146         iConsole->Printf(KNumberOfObjects, circularBuffer->Count());            
00147         
00148         // Add multiple integers to the circular buffer.
00149         TUint numberOfObjects= 3;
00150         for(TUint index=1;index<=numberOfObjects; index++)
00151                 {
00152                 result= circularBuffer->Put(element[index]);
00153                 User::LeaveIfError(result);
00154                 // Print the element added to the circular buffer.
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         // Remove integers from circular buffer.
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         // Print the element removed from the circular buffer.
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         // Print the element removed from the circular buffer.
00188         iConsole->Printf(KElementRemoved, result);
00189         iConsole->Printf(KNumberOfObjects, circularBuffer->Count());
00190         
00191         //Add remaining elements from array to buffer.
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         // Remove multiple integers from the circular buffer.
00204         for(TUint index=2;index<=5; index++)
00205                 {
00206                 result= circularBuffer->Get();// Removed integer is element[index].
00207                 User::LeaveIfError(result);
00208                 ASSERT((TUint)result == element[index]);
00209                 // Print the elements removed from the circular buffer.
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         //Pop the circular buffer off the cleanup stack and destroy it.
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         // Create a circular buffer containing instances of TMyClass.
00233         CCirBuf<TMyClass>* circularBuffer=new(ELeave) CCirBuf<TMyClass>;
00234         // Push the circular buffer onto the cleanup stack.
00235         CleanupStack::PushL(circularBuffer);
00236         // Set the maximum capacity of this circular buffer.
00237         const TInt KMaxElements = 4;
00238         circularBuffer->SetLengthL(KMaxElements); // Maximum capacity is 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         // Creates an array of object of TMyClass.      
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]); // Add a TMyClass to the circular buffer.
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         // Add multiple TMyClasss to the circular buffer
00274         result= circularBuffer->Add(&myObjectsToAdd[1], 3); // Add three objects to circular buffer.
00275         User::LeaveIfError(result);
00276         ASSERT(result == 3); //Making sure 3 objects are added.
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         //Array to hold removed elements from circular buffer.
00290         TMyClass myRemovedObjects[6];
00291         _LIT(KRemovedMultipleMyObjectElements,"\nTMyClass objects removed are: ");
00292         
00293         // Remove multiple TMyClasss from the circular buffer.
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         // Add two elements to circular buffer
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         // Remove multiple TMyClasss from the circular buffer.
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                 // The removed objects are same as added ones and are in order.
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         //Pop the circular buffer off the cleanup stack and destroy it.
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         // Creates a circular buffer containing RBuf.
00343         CCirBuf<RBuf>* circularBuffer=new(ELeave)CCirBuf<RBuf>;
00344         // Push the circular buffer onto the cleanup stack.
00345         CleanupStack::PushL(circularBuffer);
00346         
00347         const TInt KMaxElements = 2;
00348         // Sets the maximum capacity of this circular buffer.
00349         circularBuffer->SetLengthL(KMaxElements);// max capacity is 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         // Create an buffer descriptor and assign KBuffer1 data to the descriptor.
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); // Add a Rbuf object to the circular buffer.
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         // Create an buffer descriptor and assign KBuffer2 data to the descriptor.
00373         bufToAdd2.CreateL(KBuffer2);
00374         
00375         result=circularBuffer->Add(&bufToAdd2); // Add a Rbuf object to the circular buffer.
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); //bufToRemove will point to bufToAdd1 location.
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         // Circular buffer containing integers.
00411         app->CircularBufferOfIntsL();
00412 
00413         // Circular buffer containing TMyClasss.
00414         app->CircularBufferOfMyObjectsL();
00415         
00416         // Circular buffer containing RBuf 
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         }

Generated by  doxygen 1.6.2