examples/SFExamples/Symbian_OS_Explained_Hercules-v9_Example_Code/testcode/task.cpp

00001 // 
00002 // Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
00003 // All rights reserved.
00004 // This component and the accompanying materials are made available
00005 // under the terms of the License "Eclipse Public License v1.0"
00006 // which accompanies this distribution, and is available
00007 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00008 // 
00009 // Initial Contributors:
00010 // Nokia Corporation - initial contribution.
00011 // 
00012 // Contributors:
00013 // 
00014 // Description:
00015 // 
00016 // task.cpp
00017 
00018 #include "task.h"
00019 
00020 void Panic(TTaskManagerPanic aPanicCode)
00021         {
00022         _LIT(KTaskManagerPanic, "TTaskManager");
00023         User::Panic(KTaskManagerPanic, aPanicCode);
00024         }
00025 
00026 TTask::TTask(THerculeanLabours aLabour)
00027 : iLabour(aLabour)
00028         {
00029         __ASSERT_ALWAYS( ((aLabour>=ESlayNemeanLion)&&(aLabour<=ECaptureCerberus)), Panic(EInvalidTaskId));
00030         Initialize();
00031         }
00032 
00033 TTask::TTask(const TTask& aTask)
00034 : iLabour(aTask.iLabour)
00035         {
00036         iLabourName.Set(aTask.iLabourName);
00037         }
00038 
00039 TTask& TTask::operator=(const TTask& aTask)
00040         {
00041         iLabour=aTask.iLabour;
00042         iLabourName.Set(aTask.iLabourName);
00043         return (*this);
00044         }
00045 
00046 void TTask::Initialize()
00047         {
00048         switch (iLabour)
00049                 {
00050                 case (ESlayNemeanLion):
00051                         {
00052                         _LIT(KSlayNemeanLion, "SlayNemeanLion");
00053                         iLabourName.Set(KSlayNemeanLion);
00054                         }
00055                 break;
00056                 case (ESlayHydra):
00057                         {
00058                         _LIT(KSlayHydra, "SlayHydra");
00059                         iLabourName.Set(KSlayHydra);
00060                         }
00061                 break;
00062                 case (ECaptureCeryneianHind):
00063                         {
00064                         _LIT(KCaptureCeryneianHind, "CaptureCeryneianHind");
00065                         iLabourName.Set(KCaptureCeryneianHind);
00066                         }
00067                 break;
00068                 case (ESlayErymanthianBoar):
00069                         {
00070                         _LIT(KSlayErymanthianBoar, "SlayErymanthianBoar");
00071                         iLabourName.Set(KSlayErymanthianBoar);
00072                         }
00073                 break;
00074                 case (ECleanAugeanStables):
00075                         {
00076                         _LIT(KCleanAugeanStables, "CleanAugeanStables");
00077                         iLabourName.Set(KCleanAugeanStables);
00078                         }
00079                 break;
00080                 case (ECancelCleanAugeanStables):
00081                         {
00082                         _LIT(KCancelCleanAugeanStables, "CancelCleanAugeanStables");
00083                         iLabourName.Set(KCancelCleanAugeanStables);
00084                         }
00085                 break;
00086                 case (ESlayStymphalianBirds):
00087                         {
00088                         _LIT(KSlayStymphalianBirds, "SlayStymphalianBirds");
00089                         iLabourName.Set(KSlayStymphalianBirds);
00090                         }
00091                 break;
00092                 case (ECancelSlayStymphalianBirds):
00093                         {
00094                         _LIT(KCancelSlayStymphalianBirds, "CancelSlayStymphalianBirds");
00095                         iLabourName.Set(KCancelSlayStymphalianBirds);
00096                         }
00097                 break;
00098                 case (ECaptureCretanBull):
00099                         {
00100                         _LIT(KCaptureCretanBull, "CaptureCretanBull");
00101                         iLabourName.Set(KCaptureCretanBull);
00102                         }
00103                 break;
00104                 case (ECaptureMaresOfDiomedes):
00105                         {
00106                         _LIT(KCaptureMaresOfDiomedes, "CaptureMaresOfDiomedes");
00107                         iLabourName.Set(KCaptureMaresOfDiomedes);
00108                         }
00109                 break;
00110                 case (EObtainGirdleOfHippolyta):
00111                         {
00112                         _LIT(KObtainGirdleOfHippolyta, "ObtainGirdleOfHippolyta");
00113                         iLabourName.Set(KObtainGirdleOfHippolyta);
00114                         }
00115                 break;
00116                 case (ECaptureOxenOfGeryon):
00117                         {
00118                         _LIT(KCaptureOxenOfGeryon, "CaptureOxenOfGeryon");
00119                         iLabourName.Set(KCaptureOxenOfGeryon);
00120                         }
00121                 break;
00122                 case (ETakeGoldenApplesOfHesperides):
00123                         {
00124                         _LIT(KTakeGoldenApplesOfHesperides, "TakeGoldenApplesOfHesperides");
00125                         iLabourName.Set(KTakeGoldenApplesOfHesperides);
00126                         }
00127                 break;
00128                 case (ECaptureCerberus):
00129                         {
00130                         _LIT(KCaptureCerberus, "CaptureCerberus");
00131                         iLabourName.Set(KCaptureCerberus);
00132                         }
00133                 break;
00134                 default:
00135                         ASSERT(EFalse); // Should never get here
00136         }
00137 }
00138 
00139 
00140 // If aTask1.iLabour < aTask2.iLabour return -ve value
00141 // If aTask1.iLabour > aTask2.iLabour return +ve value
00142 // If aTask1.iLabour == aTask2.iLabour return zero
00143 
00144 /*static*/ TInt TTask::CompareTaskNumbers(const TTask& aTask1, const TTask& aTask2)
00145         {
00146 if (aTask1.iLabour > aTask2.iLabour)
00147         return (1);
00148 else if (aTask1.iLabour < aTask2.iLabour)
00149         return (-1);
00150 else
00151         {
00152         ASSERT(aTask1.iLabour==aTask2.iLabour);
00153         return (0);
00154         }
00155 }
00156 
00157 /*static*/ TBool TTask::MatchTasks(const TTask& aTask1, const TTask& aTask2)
00158         {
00159         if (aTask1.iLabour==aTask2.iLabour)
00160                 {
00161                 ASSERT(aTask1.iLabourName.Compare(aTask2.iLabourName)==0);
00162                 return ETrue;
00163                 }
00164         
00165         return (EFalse);
00166         }
00167 
00168 
00169 /*static*/ CHerculeanTaskManager* CHerculeanTaskManager::NewLC()
00170         {
00171         CHerculeanTaskManager* me = new (ELeave) CHerculeanTaskManager();
00172         CleanupStack::PushL(me);
00173         me->ConstructL();
00174         return (me);
00175         }
00176 
00177 CHerculeanTaskManager::CHerculeanTaskManager()
00178 : iTaskArray(KTaskArrayGranularity)
00179 {}
00180 
00181 void CHerculeanTaskManager::ConstructL()
00182         {// Nothing to do
00183         }
00184 
00185 CHerculeanTaskManager::~CHerculeanTaskManager()
00186         {
00187         iTaskArray.Close();
00188         }
00189 
00190 
00191 void CHerculeanTaskManager::AppendTaskL(THerculeanLabours aTaskNumber)
00192         {
00193         TTask tempTask(aTaskNumber);
00194         User::LeaveIfError(iTaskArray.Append(tempTask));
00195         }
00196 
00197 
00198 // Deletes all tasks of aTaskNumber from the array
00199 void CHerculeanTaskManager::DeleteTask(THerculeanLabours aTaskNumber)
00200         {
00201         TTask tempTask(aTaskNumber);
00202         TInt foundIndex = iTaskArray.Find(tempTask, TTask::MatchTasks);
00203         while (foundIndex!=KErrNotFound)
00204                 {
00205                 iTaskArray.Remove(foundIndex);
00206                 foundIndex = iTaskArray.Find(tempTask, TTask::MatchTasks);
00207                 }
00208         }       
00209 
00210 void CHerculeanTaskManager::GetTask(THerculeanLabours aTaskNumber, TTask& aTask)
00211         {
00212         TTask tempTask(aTaskNumber);
00213         TInt foundIndex = iTaskArray.Find(tempTask, TTask::MatchTasks);
00214         aTask = iTaskArray[foundIndex];
00215         }
00216 
00217 // aTaskList is an empty RBuf
00218 // Lists the tasks as they found in the array
00219 void CHerculeanTaskManager::ListTasksL(RBuf     & aTaskList)
00220         {
00221 //      Get length of descriptor data required
00222         TInt listLength = GetTaskListLength();
00223         ASSERT(listLength>=0);
00224         
00225         aTaskList.CreateL(listLength);
00226         
00227         TInt count=iTaskArray.Count();
00228         for (TInt index = 0; index<count; index++)
00229                 {
00230                 TTask task = iTaskArray[index];
00231                 aTaskList.Append(KTaskEntry);
00232                 aTaskList.Append(task.LabourName());
00233                 aTaskList.Append(KNewLine);
00234                 }       
00235         }
00236 
00237 // aTaskList is an empty RBuf
00238 // Sorts the tasks into numerical order starting from the lowest value of iLabour
00239 void CHerculeanTaskManager::ListTasksAscendingL(RBuf    & aTaskList)
00240         {
00241         SortTasksAscending();
00242         ListTasksL(aTaskList);
00243         }
00244 
00245 void CHerculeanTaskManager::SortTasksAscending()
00246         {
00247         iTaskArray.Sort(TTask::CompareTaskNumbers);
00248         }
00249 
00250 // Returns the number of bytes required for a list of tasks
00251 TInt CHerculeanTaskManager::GetTaskListLength()
00252         {
00253         TInt taskEntryLength = (KTaskEntry().Length()) + (KNewLine().Length());
00254         
00255         TInt listLength = 0;
00256         TInt count=iTaskArray.Count();
00257         for (TInt index = 0; index<count; index++)
00258                 {
00259                 TTask task = iTaskArray[index];
00260                 listLength+=task.LabourName().Length();
00261                 listLength+=taskEntryLength;
00262                 }
00263         
00264         return (listLength);
00265         }

Generated by  doxygen 1.6.2