Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/longnumber_8cpp-source.html
author Dominic Pinkman <Dominic.Pinkman@Nokia.com>
Thu, 11 Mar 2010 15:24:26 +0000
changeset 2 ebc84c812384
parent 1 25a17d01db0c
permissions -rw-r--r--
week 10 bug fix submission: Bug 1892, Bug 1897, Bug 1319. Also 3 or 4 documents were found to contain code blocks with SFL, which has been fixed. Partial fix for broken links, links to Forum Nokia, and the 'Symbian platform' terminology issues.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>TB10.1 Example Applications: examples/Base/ArraysAndLists/linkedlist/sgllist/src/longnumber.cpp Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.5.3 -->
<h1>examples/Base/ArraysAndLists/linkedlist/sgllist/src/longnumber.cpp</h1><a href="longnumber_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).</span>
<a name="l00002"></a>00002 <span class="comment">// All rights reserved.</span>
<a name="l00003"></a>00003 <span class="comment">// This component and the accompanying materials are made available</span>
<a name="l00004"></a>00004 <span class="comment">// under the terms of "Eclipse Public License v1.0"</span>
<a name="l00005"></a>00005 <span class="comment">// which accompanies this distribution, and is available</span>
<a name="l00006"></a>00006 <span class="comment">// at the URL "http://www.eclipse.org/legal/epl-v10.html".</span>
<a name="l00007"></a>00007 <span class="comment">//</span>
<a name="l00008"></a>00008 <span class="comment">// Initial Contributors:</span>
<a name="l00009"></a>00009 <span class="comment">// Nokia Corporation - initial contribution.</span>
<a name="l00010"></a>00010 <span class="comment">//</span>
<a name="l00011"></a>00011 <span class="comment">// Contributors:</span>
<a name="l00012"></a>00012 <span class="comment">//</span>
<a name="l00013"></a>00013 <span class="comment">// Description:</span>
<a name="l00014"></a>00014 <span class="comment">// Contains definition of functions defined in the CLongNumber class.</span>
<a name="l00015"></a>00015 <span class="comment">//</span>
<a name="l00016"></a>00016 
<a name="l00017"></a>00017 
<a name="l00018"></a>00018 
<a name="l00023"></a>00023 <span class="preprocessor">#include "<a class="code" href="longnumber_8h.html">longnumber.h</a>"</span>
<a name="l00024"></a>00024 
<a name="l00030"></a><a class="code" href="class_c_long_number.html#a6cb04e2afa3b0948941ab4910739331">00030</a> <a class="code" href="class_c_long_number.html">CLongNumber</a>* <a class="code" href="class_c_long_number.html#a6cb04e2afa3b0948941ab4910739331">CLongNumber::NewL</a>(CConsoleBase* aConsole)
<a name="l00031"></a>00031         {
<a name="l00032"></a>00032         <a class="code" href="class_c_long_number.html">CLongNumber</a>* <span class="keyword">self</span> = <span class="keyword">new</span> (ELeave) <a class="code" href="class_c_long_number.html">CLongNumber</a>(aConsole);
<a name="l00033"></a>00033         <span class="keyword">self</span>-&gt;<a class="code" href="class_c_long_number.html#1c0ebc1c9dbcc9c34250ffef859b8a2f">AddToScheduler</a>();
<a name="l00034"></a>00034         <span class="keywordflow">return</span> <span class="keyword">self</span>;
<a name="l00035"></a>00035         }
<a name="l00036"></a>00036 
<a name="l00041"></a>00041 CLongNumber::CLongNumber(CConsoleBase* aConsole):
<a name="l00042"></a>00042                                                                                                 <span class="comment">// Constructor of the base class.</span>
<a name="l00043"></a>00043                                                                                                 CActive(CActive::EPriorityUserInput),
<a name="l00044"></a>00044                                                                                                 <span class="comment">// Create the linked list.</span>
<a name="l00045"></a>00045                                                                                                 iNumber(_FOFF(<a class="code" href="class_t_digit.html">TDigit</a>,iSLink)),
<a name="l00046"></a>00046                                                                                                 iConsole(aConsole),
<a name="l00047"></a>00047                                                                                                 <span class="comment">// Initialize the iterator.</span>
<a name="l00048"></a>00048                                                                                                 iIterator(iNumber)
<a name="l00049"></a>00049         {
<a name="l00050"></a>00050         }
<a name="l00051"></a>00051 
<a name="l00052"></a>00052 
<a name="l00056"></a><a class="code" href="class_c_long_number.html#1c0ebc1c9dbcc9c34250ffef859b8a2f">00056</a> <span class="keywordtype">void</span> <a class="code" href="class_c_long_number.html#1c0ebc1c9dbcc9c34250ffef859b8a2f">CLongNumber::AddToScheduler</a>()
<a name="l00057"></a>00057         {
<a name="l00058"></a>00058         CActiveScheduler::Add(<span class="keyword">this</span>);
<a name="l00059"></a>00059         }
<a name="l00060"></a>00060 
<a name="l00064"></a><a class="code" href="class_c_long_number.html#1b7d3d5b97621f32d776d9b1a0d19d8a">00064</a> <span class="keywordtype">void</span> <a class="code" href="class_c_long_number.html#1b7d3d5b97621f32d776d9b1a0d19d8a">CLongNumber::RunL</a>()
<a name="l00065"></a>00065         {
<a name="l00066"></a>00066         <span class="comment">// Get the key code.</span>
<a name="l00067"></a>00067         TUint8 option = iConsole-&gt;KeyCode();
<a name="l00068"></a>00068         <span class="comment">// Print the selected option.</span>
<a name="l00069"></a>00069         _LIT(KTextFormat,<span class="stringliteral">"%c\n"</span>);
<a name="l00070"></a>00070         iConsole-&gt;Printf(KTextFormat,option);
<a name="l00071"></a>00071 
<a name="l00072"></a>00072         TInt number = option - (TUint)<span class="charliteral">'0'</span>;
<a name="l00073"></a>00073         <span class="comment">// The user has entered a number.</span>
<a name="l00074"></a>00074         <span class="keywordflow">if</span>(number &lt;10 &amp;&amp; number &gt; -1)
<a name="l00075"></a>00075                 {
<a name="l00076"></a>00076                 <span class="comment">// Append to the list based on iOption.</span>
<a name="l00077"></a>00077                 <span class="keywordflow">switch</span>(iOption)
<a name="l00078"></a>00078                         {
<a name="l00079"></a>00079                         <span class="comment">// Call the TSglQue::AddFirst() function.</span>
<a name="l00080"></a>00080                         <span class="keywordflow">case</span> EAddFirst:
<a name="l00081"></a>00081                                 {
<a name="l00082"></a>00082                                 <span class="comment">// Create an object of the TDigit class.</span>
<a name="l00083"></a>00083                                 <a class="code" href="class_t_digit.html">TDigit</a>* digit = <span class="keyword">new</span> (ELeave) <a class="code" href="class_t_digit.html">TDigit</a>;
<a name="l00084"></a>00084                                 <span class="comment">// Set TDigit::iDigit to the number entered by the user.</span>
<a name="l00085"></a>00085                                 digit-&gt;<a class="code" href="class_t_digit.html#47ece24e5d32c125a2afea13179c6b65">Set</a>(number);
<a name="l00086"></a>00086                                 <span class="comment">// Add TDigit to the start of the list.</span>
<a name="l00087"></a>00087                                 iNumber.AddFirst(*digit);
<a name="l00088"></a>00088                                 iOption = ETOptionNone;
<a name="l00089"></a>00089                                 }
<a name="l00090"></a>00090                                 <span class="comment">// Print the number.</span>
<a name="l00091"></a>00091                                 <a class="code" href="class_c_long_number.html#ec695d1da24cd27f36fed7c4f0ffe0a6">PrintNumber</a>();
<a name="l00092"></a>00092                                 <span class="keywordflow">break</span>;
<a name="l00093"></a>00093                         <span class="comment">// Call the TSglQue::AddLast() function.</span>
<a name="l00094"></a>00094                         <span class="keywordflow">case</span> EAddLast:
<a name="l00095"></a>00095                                 {
<a name="l00096"></a>00096                                 <span class="comment">// Create an object of the TDigit class.</span>
<a name="l00097"></a>00097                                 <a class="code" href="class_t_digit.html">TDigit</a>* digit = <span class="keyword">new</span> (ELeave) <a class="code" href="class_t_digit.html">TDigit</a>;
<a name="l00098"></a>00098                                 <span class="comment">// Set TDigit::iDigit to the number entered by the user.</span>
<a name="l00099"></a>00099                                 digit-&gt;<a class="code" href="class_t_digit.html#47ece24e5d32c125a2afea13179c6b65">Set</a>(number);
<a name="l00100"></a>00100                                 <span class="comment">// Add TDigit to the end of the list.</span>
<a name="l00101"></a>00101                                 iNumber.AddLast(*digit);
<a name="l00102"></a>00102                                 iOption = ETOptionNone;
<a name="l00103"></a>00103                                 }
<a name="l00104"></a>00104                                 <span class="comment">// Print the number.</span>
<a name="l00105"></a>00105                                 <a class="code" href="class_c_long_number.html#ec695d1da24cd27f36fed7c4f0ffe0a6">PrintNumber</a>();
<a name="l00106"></a>00106                                 <span class="keywordflow">break</span>;
<a name="l00107"></a>00107                         <span class="keywordflow">case</span> ETOptionNone:
<a name="l00108"></a>00108                                 _LIT(KInvalidOption,<span class="stringliteral">"Invalid Option!\n"</span>);
<a name="l00109"></a>00109                                 iConsole-&gt;Printf(KInvalidOption);
<a name="l00110"></a>00110                                 <span class="keywordflow">break</span>;
<a name="l00111"></a>00111                         <span class="keywordflow">default</span>:
<a name="l00112"></a>00112                                 iOption = ETOptionNone;
<a name="l00113"></a>00113                                 <span class="keywordflow">break</span>;
<a name="l00114"></a>00114                         }
<a name="l00115"></a>00115                 <span class="comment">// Generate an asynchronous read request.</span>
<a name="l00116"></a>00116                 <a class="code" href="class_c_long_number.html#1b5d110a5df39ccfeda083160370dc5c">ReadNumber</a>();
<a name="l00117"></a>00117                 }
<a name="l00118"></a>00118         <span class="comment">// The user has not entered a number.</span>
<a name="l00119"></a>00119         <span class="comment">// Hence, process this request as an option,</span>
<a name="l00120"></a>00120         <span class="comment">// which represented by the TOption enumeration.</span>
<a name="l00121"></a>00121         <span class="keywordflow">else</span>
<a name="l00122"></a>00122                 {
<a name="l00123"></a>00123                 <span class="keywordflow">switch</span>(option)
<a name="l00124"></a>00124                         {
<a name="l00125"></a>00125                         <span class="keywordflow">case</span> <span class="charliteral">'f'</span>:
<a name="l00126"></a>00126                                 {
<a name="l00127"></a>00127                                 <span class="comment">// Add first.</span>
<a name="l00128"></a>00128                                 iOption = EAddFirst;
<a name="l00129"></a>00129                                 _LIT(KTextEnterDigit,<span class="stringliteral">"\nEnter a digit\n"</span>);
<a name="l00130"></a>00130                                 iConsole-&gt;Printf(KTextEnterDigit);
<a name="l00131"></a>00131                                 <span class="comment">// Read a digit.</span>
<a name="l00132"></a>00132                                 ReadFunc();
<a name="l00133"></a>00133                                 }
<a name="l00134"></a>00134                                 <span class="keywordflow">break</span>;
<a name="l00135"></a>00135                         <span class="keywordflow">case</span> <span class="charliteral">'l'</span>:
<a name="l00136"></a>00136                                 {
<a name="l00137"></a>00137                                 <span class="comment">// Add last.</span>
<a name="l00138"></a>00138                                 iOption = EAddLast;
<a name="l00139"></a>00139                                 _LIT(KTextEnterDigit,<span class="stringliteral">"\nEnter a digit\n"</span>);
<a name="l00140"></a>00140                                 iConsole-&gt;Printf(KTextEnterDigit);
<a name="l00141"></a>00141                                 <span class="comment">// Read a digit.</span>
<a name="l00142"></a>00142                                 ReadFunc();
<a name="l00143"></a>00143                                 }
<a name="l00144"></a>00144                                 <span class="keywordflow">break</span>;
<a name="l00145"></a>00145                         <span class="keywordflow">case</span> <span class="charliteral">'r'</span>:
<a name="l00146"></a>00146                                 {
<a name="l00147"></a>00147                                 <span class="keywordflow">if</span>(iNumber.IsEmpty())
<a name="l00148"></a>00148                                         {
<a name="l00149"></a>00149                                         _LIT(KTextEmpty,<span class="stringliteral">"Empty Queue !\n"</span>);
<a name="l00150"></a>00150                                         iConsole-&gt;Printf(KTextEmpty);
<a name="l00151"></a>00151                                         <a class="code" href="class_c_long_number.html#1b5d110a5df39ccfeda083160370dc5c">ReadNumber</a>();
<a name="l00152"></a>00152                                         <span class="keywordflow">break</span>;
<a name="l00153"></a>00153                                         }
<a name="l00154"></a>00154                                 <span class="comment">// Remove the last element in the list.</span>
<a name="l00155"></a>00155                                 <a class="code" href="class_t_digit.html">TDigit</a>* ptr = iNumber.Last();
<a name="l00156"></a>00156                                 iNumber.Remove(*ptr);
<a name="l00157"></a>00157                                 <span class="keyword">delete</span> ptr;
<a name="l00158"></a>00158                                 <span class="comment">// Print the number.</span>
<a name="l00159"></a>00159                                 <a class="code" href="class_c_long_number.html#ec695d1da24cd27f36fed7c4f0ffe0a6">PrintNumber</a>();
<a name="l00160"></a>00160                                 <span class="comment">// Read a digit.</span>
<a name="l00161"></a>00161                                 <a class="code" href="class_c_long_number.html#1b5d110a5df39ccfeda083160370dc5c">ReadNumber</a>();
<a name="l00162"></a>00162                                 }
<a name="l00163"></a>00163                                 <span class="keywordflow">break</span>;
<a name="l00164"></a>00164                         <span class="keywordflow">default</span>:
<a name="l00165"></a>00165                                 CActiveScheduler::Stop();
<a name="l00166"></a>00166                         }
<a name="l00167"></a>00167                 }
<a name="l00168"></a>00168         }
<a name="l00169"></a>00169 
<a name="l00173"></a>00173 <span class="keywordtype">void</span> CLongNumber::ReadFunc()
<a name="l00174"></a>00174         {
<a name="l00175"></a>00175         <span class="comment">// Wait for a key press event.</span>
<a name="l00176"></a>00176         iConsole-&gt;Read(iStatus);
<a name="l00177"></a>00177         SetActive();
<a name="l00178"></a>00178         }
<a name="l00179"></a>00179 
<a name="l00183"></a><a class="code" href="class_c_long_number.html#1b5d110a5df39ccfeda083160370dc5c">00183</a> <span class="keywordtype">void</span> <a class="code" href="class_c_long_number.html#1b5d110a5df39ccfeda083160370dc5c">CLongNumber::ReadNumber</a>()
<a name="l00184"></a>00184         {
<a name="l00185"></a>00185         _LIT(KTextMenu,<span class="stringliteral">"\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"</span>);
<a name="l00186"></a>00186         iConsole-&gt;Printf(KTextMenu);
<a name="l00187"></a>00187         ReadFunc();
<a name="l00188"></a>00188         }
<a name="l00189"></a>00189 
<a name="l00193"></a><a class="code" href="class_c_long_number.html#ec695d1da24cd27f36fed7c4f0ffe0a6">00193</a> <span class="keywordtype">void</span> <a class="code" href="class_c_long_number.html#ec695d1da24cd27f36fed7c4f0ffe0a6">CLongNumber::PrintNumber</a>()
<a name="l00194"></a>00194         {
<a name="l00195"></a>00195         <span class="keywordflow">if</span>(iNumber.IsEmpty())
<a name="l00196"></a>00196                 {
<a name="l00197"></a>00197                 _LIT(KTextEmpty,<span class="stringliteral">"Empty Queue !\n"</span>);
<a name="l00198"></a>00198                 iConsole-&gt;Printf(KTextEmpty);                                                   
<a name="l00199"></a>00199                 }
<a name="l00200"></a>00200         _LIT(KTextNumber,<span class="stringliteral">"The number is: "</span>);
<a name="l00201"></a>00201         iConsole-&gt;Printf(KTextNumber);
<a name="l00202"></a>00202         <span class="comment">// Set the iterator to the first element of the list.</span>
<a name="l00203"></a>00203         iIterator.SetToFirst();
<a name="l00204"></a>00204 
<a name="l00205"></a>00205         <span class="comment">// Iterate the list.</span>
<a name="l00206"></a>00206         <span class="keywordflow">while</span>(iIterator != NULL)
<a name="l00207"></a>00207                 {
<a name="l00208"></a>00208                 <span class="comment">// Get the digit.</span>
<a name="l00209"></a>00209                 <a class="code" href="class_t_digit.html">TDigit</a> digit = *iIterator;
<a name="l00210"></a>00210                 <span class="comment">// Print the digit.</span>
<a name="l00211"></a>00211                 _LIT(KTextInt,<span class="stringliteral">"%d"</span>);
<a name="l00212"></a>00212                 iConsole-&gt;Printf(KTextInt,digit.<a class="code" href="class_t_digit.html#c59dd51bd32a1a8f614f96093ca29937">Get</a>());
<a name="l00213"></a>00213                 <span class="comment">// Go to the next element of the list.</span>
<a name="l00214"></a>00214                 iIterator++;
<a name="l00215"></a>00215                 }
<a name="l00216"></a>00216         }
<a name="l00217"></a>00217 
<a name="l00221"></a><a class="code" href="class_c_long_number.html#a7b7c59251aa6d2ffc25f0ec9312b59d">00221</a> <span class="keywordtype">void</span> <a class="code" href="class_c_long_number.html#a7b7c59251aa6d2ffc25f0ec9312b59d">CLongNumber::InitializeIter</a>()
<a name="l00222"></a>00222         {
<a name="l00223"></a>00223         iIterator.SetToFirst();
<a name="l00224"></a>00224         }
<a name="l00225"></a>00225 
<a name="l00232"></a><a class="code" href="class_c_long_number.html#5ef806e05880bcf07c0506bd36beefe0">00232</a> TBool <a class="code" href="class_c_long_number.html#5ef806e05880bcf07c0506bd36beefe0">CLongNumber::GetNumber</a>(TInt&amp; aValue)
<a name="l00233"></a>00233         {
<a name="l00234"></a>00234         <span class="keywordflow">if</span>(iIterator == NULL)
<a name="l00235"></a>00235                 {
<a name="l00236"></a>00236                 <span class="comment">// The iterator has reached the end of the list.</span>
<a name="l00237"></a>00237                 <span class="keywordflow">return</span> EFalse;
<a name="l00238"></a>00238                 }
<a name="l00239"></a>00239         <a class="code" href="class_t_digit.html">TDigit</a> digit = *iIterator;
<a name="l00240"></a>00240         aValue = digit.<a class="code" href="class_t_digit.html#c59dd51bd32a1a8f614f96093ca29937">Get</a>();
<a name="l00241"></a>00241         <span class="keywordflow">return</span> ETrue;
<a name="l00242"></a>00242         }
<a name="l00243"></a>00243 
<a name="l00248"></a><a class="code" href="class_c_long_number.html#e717f76bb1203c1c495b5e37fa4d8ff9">00248</a> TBool <a class="code" href="class_c_long_number.html#e717f76bb1203c1c495b5e37fa4d8ff9">CLongNumber::Slide</a>()
<a name="l00249"></a>00249         {
<a name="l00250"></a>00250         <span class="comment">// Set the iterator to point to the next element.</span>
<a name="l00251"></a>00251         iIterator++;
<a name="l00252"></a>00252         <span class="keywordflow">if</span>(iIterator == NULL)
<a name="l00253"></a>00253                 {
<a name="l00254"></a>00254                 <span class="comment">// The iterator has reached the end of the list.</span>
<a name="l00255"></a>00255                 <span class="keywordflow">return</span> EFalse;
<a name="l00256"></a>00256                 }
<a name="l00257"></a>00257         <span class="keywordflow">return</span> ETrue;
<a name="l00258"></a>00258         }
<a name="l00259"></a>00259 
<a name="l00263"></a><a class="code" href="class_c_long_number.html#7039dbe9ebbf42b59f919ba2755dd8a3">00263</a> <span class="keywordtype">void</span> <a class="code" href="class_c_long_number.html#7039dbe9ebbf42b59f919ba2755dd8a3">CLongNumber::DoCancel</a>()
<a name="l00264"></a>00264         {
<a name="l00265"></a>00265         <span class="keywordflow">if</span>(IsActive())
<a name="l00266"></a>00266                 {
<a name="l00267"></a>00267                 <span class="comment">// Cancel any outstanding read requests.</span>
<a name="l00268"></a>00268                 iConsole-&gt;ReadCancel();
<a name="l00269"></a>00269                 }
<a name="l00270"></a>00270         }
<a name="l00271"></a>00271 
<a name="l00277"></a><a class="code" href="class_c_long_number.html#2112ad5c07adc10adf5d63c33ef2d5cd">00277</a> <a class="code" href="class_c_long_number.html#2112ad5c07adc10adf5d63c33ef2d5cd">CLongNumber::~CLongNumber</a>()
<a name="l00278"></a>00278         {
<a name="l00279"></a>00279         <span class="comment">// Delete the elements from the list</span>
<a name="l00280"></a>00280         <span class="keywordflow">if</span>(iIterator)
<a name="l00281"></a>00281                 {
<a name="l00282"></a>00282                 iIterator.SetToFirst();
<a name="l00283"></a>00283                 <span class="comment">// Iterate the list and delete all TDigit objects</span>
<a name="l00284"></a>00284                 <a class="code" href="class_t_digit.html">TDigit</a> *ptr = iIterator++;
<a name="l00285"></a>00285                 <span class="keywordflow">while</span> (ptr != NULL)
<a name="l00286"></a>00286                         {
<a name="l00287"></a>00287                         <span class="keyword">delete</span> ptr;
<a name="l00288"></a>00288                         ptr = iIterator++;
<a name="l00289"></a>00289                         }
<a name="l00290"></a>00290                 }
<a name="l00291"></a>00291         }
</pre></div><hr size="1"><address style="text-align: right;"><small>Generated on Thu Jan 21 10:32:55 2010 for TB10.1 Example Applications by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.3 </small></address>
</body>
</html>