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                                 CActiveScheduler::Stop();
00178                         }
00179                 }
00180         }
00181 
00185 void CLongNumber::ReadFunc()
00186         {
00187         // Wait for a key press event.
00188         iConsole->Read(iStatus);
00189         SetActive();
00190         }
00191 
00195 void CLongNumber::ReadNumber()
00196         {
00197         _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");
00198         iConsole->Printf(KTextMenu);
00199         ReadFunc();
00200         }
00201 
00205 void CLongNumber::PrintNumber()
00206         {
00207         if(iNumber.IsEmpty())
00208                 {
00209                 _LIT(KTextEmpty,"Empty Queue !\n");
00210                 iConsole->Printf(KTextEmpty);                                                   
00211                 }
00212         _LIT(KTextNumber,"The number is: ");
00213         iConsole->Printf(KTextNumber);
00214         // Set the iterator to the first element of the list.
00215         iIterator.SetToFirst();
00216 
00217         // Iterate the list.
00218         while(iIterator != NULL)
00219                 {
00220                 // Get the digit.
00221                 TDigit digit = *iIterator;
00222                 // Print the digit.
00223                 _LIT(KTextInt,"%d");
00224                 iConsole->Printf(KTextInt,digit.Get());
00225                 // Go to the next element of the list.
00226                 iIterator++;
00227                 }
00228         }
00229 
00233 void CLongNumber::InitializeIter()
00234         {
00235         iIterator.SetToFirst();
00236         }
00237 
00244 TBool CLongNumber::GetNumber(TInt& aValue)
00245         {
00246         if(iIterator == NULL)
00247                 {
00248                 // The iterator has reached the end of the list.
00249                 return EFalse;
00250                 }
00251         TDigit digit = *iIterator;
00252         aValue = digit.Get();
00253         return ETrue;
00254         }
00255 
00260 TBool CLongNumber::Slide()
00261         {
00262         // Set the iterator to point to the next element.
00263         iIterator++;
00264         if(iIterator == NULL)
00265                 {
00266                 // The iterator has reached the end of the list.
00267                 return EFalse;
00268                 }
00269         return ETrue;
00270         }
00271 
00275 void CLongNumber::DoCancel()
00276         {
00277         if(IsActive())
00278                 {
00279                 // Cancel any outstanding read requests.
00280                 iConsole->ReadCancel();
00281                 }
00282         }
00283 
00289 CLongNumber::~CLongNumber()
00290         {
00291         // Delete the elements from the list
00292         if(iIterator)
00293                 {
00294                 iIterator.SetToFirst();
00295                 // Iterate the list and delete all TDigit objects
00296                 TDigit *ptr = iIterator++;
00297                 while (ptr != NULL)
00298                         {
00299                         delete ptr;
00300                         ptr = iIterator++;
00301                         }
00302                 }
00303         }

Generated by  doxygen 1.6.2