examples/Base/ArraysAndLists/linkedlist/sgllist/src/longnumber.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: Contains definition of functions defined in the CLongNumber class.  
00028 */
00029 
00030 
00035 #include "longnumber.h"
00036 
00042 CLongNumber* CLongNumber::NewL(CConsoleBase* aConsole)
00043         {
00044         CLongNumber* self = new (ELeave) CLongNumber(aConsole);
00045         self->AddToScheduler();
00046         return self;
00047         }
00048 
00053 CLongNumber::CLongNumber(CConsoleBase* aConsole):
00054                                                                                                 // Constructor of the base class.
00055                                                                                                 CActive(CActive::EPriorityUserInput),
00056                                                                                                 // Create the linked list.
00057                                                                                                 iNumber(_FOFF(TDigit,iSLink)),
00058                                                                                                 iConsole(aConsole),
00059                                                                                                 // Initialize the iterator.
00060                                                                                                 iIterator(iNumber)
00061         {
00062         }
00063 
00064 
00068 void CLongNumber::AddToScheduler()
00069         {
00070         CActiveScheduler::Add(this);
00071         }
00072 
00076 void CLongNumber::RunL()
00077         {
00078         // Get the key code.
00079         TUint8 option = iConsole->KeyCode();
00080         // Print the selected option.
00081         _LIT(KTextFormat,"%c\n");
00082         iConsole->Printf(KTextFormat,option);
00083 
00084         TInt number = option - (TUint)'0';
00085         // The user has entered a number.
00086         if(number <10 && number > -1)
00087                 {
00088                 // Append to the list based on iOption.
00089                 switch(iOption)
00090                         {
00091                         // Call the TSglQue::AddFirst() function.
00092                         case EAddFirst:
00093                                 {
00094                                 // Create an object of the TDigit class.
00095                                 TDigit* digit = new (ELeave) TDigit;
00096                                 // Set TDigit::iDigit to the number entered by the user.
00097                                 digit->Set(number);
00098                                 // Add TDigit to the start of the list.
00099                                 iNumber.AddFirst(*digit);
00100                                 iOption = ETOptionNone;
00101                                 }
00102                                 // Print the number.
00103                                 PrintNumber();
00104                                 break;
00105                         // Call the TSglQue::AddLast() function.
00106                         case EAddLast:
00107                                 {
00108                                 // Create an object of the TDigit class.
00109                                 TDigit* digit = new (ELeave) TDigit;
00110                                 // Set TDigit::iDigit to the number entered by the user.
00111                                 digit->Set(number);
00112                                 // Add TDigit to the end of the list.
00113                                 iNumber.AddLast(*digit);
00114                                 iOption = ETOptionNone;
00115                                 }
00116                                 // Print the number.
00117                                 PrintNumber();
00118                                 break;
00119                         case ETOptionNone:
00120                                 _LIT(KInvalidOption,"Invalid Option!\n");
00121                                 iConsole->Printf(KInvalidOption);
00122                                 break;
00123                         default:
00124                                 iOption = ETOptionNone;
00125                                 break;
00126                         }
00127                 // Generate an asynchronous read request.
00128                 ReadNumber();
00129                 }
00130         // The user has not entered a number.
00131         // Hence, process this request as an option,
00132         // which represented by the TOption enumeration.
00133         else
00134                 {
00135                 switch(option)
00136                         {
00137                         case 'f':
00138                                 {
00139                                 // Add first.
00140                                 iOption = EAddFirst;
00141                                 _LIT(KTextEnterDigit,"\nEnter a digit\n");
00142                                 iConsole->Printf(KTextEnterDigit);
00143                                 // Read a digit.
00144                                 ReadFunc();
00145                                 }
00146                                 break;
00147                         case 'l':
00148                                 {
00149                                 // Add last.
00150                                 iOption = EAddLast;
00151                                 _LIT(KTextEnterDigit,"\nEnter a digit\n");
00152                                 iConsole->Printf(KTextEnterDigit);
00153                                 // Read a digit.
00154                                 ReadFunc();
00155                                 }
00156                                 break;
00157                         case 'r':
00158                                 {
00159                                 if(iNumber.IsEmpty())
00160                                         {
00161                                         _LIT(KTextEmpty,"Empty Queue !\n");
00162                                         iConsole->Printf(KTextEmpty);
00163                                         ReadNumber();
00164                                         break;
00165                                         }
00166                                 // Remove the last element in the list.
00167                                 TDigit* ptr = iNumber.Last();
00168                                 iNumber.Remove(*ptr);
00169                                 delete ptr;
00170                                 // Print the number.
00171                                 PrintNumber();
00172                                 // Read a digit.
00173                                 ReadNumber();
00174                                 }
00175                                 break;
00176                         default:
00177                             // Freeing the elements in the case user
00178                             // presses any non-nummeric to exit the application
00179                             TInt sum = 0;
00180                             iIterator.SetToFirst();
00181                             // Iterate the list.
00182                             while(iIterator != NULL)
00183                                 {
00184                                 // Get the digit.
00185                                 TDigit digit = *iIterator;
00186                                 // Print the digit.                             
00187                                 sum += digit.Get();
00188                                 // Go to the next element of the list.
00189                                 iIterator++;
00190                                 }
00191                             _LIT(KTextInt,"The sum is %d");
00192                             iConsole->Printf(KTextInt,sum);
00193                             
00194                             
00195                             while(!iNumber.IsEmpty())
00196                                 {
00197                                 TDigit* ptr = iNumber.Last();
00198                                 iNumber.Remove(*ptr);
00199                                 delete ptr;
00200                                 }                            
00201                              
00202                                 CActiveScheduler::Stop();
00203                         }
00204                 }
00205         }
00206 
00210 void CLongNumber::ReadFunc()
00211         {
00212         // Wait for a key press event.
00213         iConsole->Read(iStatus);
00214         SetActive();
00215         }
00216 
00220 void CLongNumber::ReadNumber()
00221         {
00222         _LIT(KTextMenu,"\nMenu\n***\nAdd [f]irst digit to number\nAdd [l]ast digit to number\n[r]emove last digit from number\nAny other NON-NUMERIC key to stop\n***\n");
00223         iConsole->Printf(KTextMenu);
00224         ReadFunc();
00225         }
00226 
00230 void CLongNumber::PrintNumber()
00231         {
00232         if(iNumber.IsEmpty())
00233                 {
00234                 _LIT(KTextEmpty,"Empty Queue !\n");
00235                 iConsole->Printf(KTextEmpty);                                                   
00236                 }
00237         _LIT(KTextNumber,"The number is: ");
00238         iConsole->Printf(KTextNumber);
00239         // Set the iterator to the first element of the list.
00240         iIterator.SetToFirst();
00241 
00242         // Iterate the list.
00243         while(iIterator != NULL)
00244                 {
00245                 // Get the digit.
00246                 TDigit digit = *iIterator;
00247                 // Print the digit.
00248                 _LIT(KTextInt,"%d");
00249                 iConsole->Printf(KTextInt,digit.Get());
00250                 // Go to the next element of the list.
00251                 iIterator++;
00252                 }
00253         }
00254 
00258 void CLongNumber::InitializeIter()
00259         {
00260         iIterator.SetToFirst();
00261         }
00262 
00269 TBool CLongNumber::GetNumber(TInt& aValue)
00270         {
00271         if(iIterator == NULL)
00272                 {
00273                 // The iterator has reached the end of the list.
00274                 return EFalse;
00275                 }
00276         TDigit digit = *iIterator;
00277         aValue = digit.Get();
00278         return ETrue;
00279         }
00280 
00285 TBool CLongNumber::Slide()
00286         {
00287         // Set the iterator to point to the next element.
00288         iIterator++;
00289         if(iIterator == NULL)
00290                 {
00291                 // The iterator has reached the end of the list.
00292                 return EFalse;
00293                 }
00294         return ETrue;
00295         }
00296 
00300 void CLongNumber::DoCancel()
00301         {
00302         if(IsActive())
00303                 {
00304                 // Cancel any outstanding read requests.
00305                 iConsole->ReadCancel();
00306                 }
00307         }
00308 
00314 CLongNumber::~CLongNumber()
00315         {
00316         // Delete the elements from the list
00317         if(iIterator)
00318                 {
00319                 iIterator.SetToFirst();
00320                 // Iterate the list and delete all TDigit objects
00321                 TDigit *ptr = iIterator++;
00322                 while (ptr != NULL)
00323                         {
00324                         delete ptr;
00325                         ptr = iIterator++;
00326                         }
00327                 }
00328         }

Generated by  doxygen 1.6.2