00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include <e32base.h>
00017 #include <e32cons.h>
00018
00019
00020
00021
00022 _LIT(KTextExampleCode,"Symbian OS Example Code");
00023 _LIT(KTextOK,"Test Passed OK!\n");
00024 _LIT(KTextPressAnyKey,"[Press any key]");
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 enum THerculeanLabours
00085 {
00086 ESlayNemeanLion = 1,
00087 ESlayHydra,
00088 ECaptureCeryneianHind,
00089 ESlayErymanthianBoar,
00090 ECleanAugeanStables,
00091 ECancelCleanAugeanStables,
00092 ESlayStymphalianBirds,
00093 ECancelSlayStymphalianBirds,
00094 ECaptureCretanBull,
00095 ECaptureMaresOfDiomedes,
00096 EObtainGirdleOfHippolyta,
00097 ECaptureOxenOfGeryon,
00098 ETakeGoldenApplesOfHesperides,
00099 ECaptureCerberus
00100 };
00101
00102 enum TTaskManagerPanic
00103 {
00104 EInvalidTaskId = 1,
00105 };
00106
00107 const TInt KTaskArrayGranularity = 4;
00108
00109 _LIT8(KTaskEntry, "\n\tTask: ");
00110
00111 class TTask
00112 {
00113 public:
00114 TTask(THerculeanLabours aLabour);
00115 public:
00116 static TInt CompareTaskNumbers(const TTask& aTask1, const TTask& aTask2);
00117 static TBool MatchTasks(const TTask& aTask1, const TTask& aTask2);
00118 public:
00119 TTask(const TTask&);
00120 TTask& operator=(const TTask&);
00121 public:
00122 inline const TDesC8& LabourName() const {return (iLabourName);};
00123 inline THerculeanLabours Labour() const {return (iLabour);};
00124 private:
00125 void Initialize();
00126 TTask();
00127 private:
00128 THerculeanLabours iLabour;
00129 TPtrC8 iLabourName;
00130 };
00131
00132
00133 class CHerculeanTaskManager : public CBase
00134 {
00135 public:
00136 virtual ~CHerculeanTaskManager();
00137 static CHerculeanTaskManager* NewLC();
00138 public:
00139 void AppendTaskL(THerculeanLabours aTaskNumber);
00140 void DeleteTask(THerculeanLabours aTaskNumber);
00141 void GetTask(THerculeanLabours aTaskNumber, TTask& aTask);
00142 void ListTasksAscendingL(RBuf8& aTaskList);
00143 void ListTasksL(RBuf8& aTaskList);
00144 public:
00145 inline TInt TaskCount() const {return (iTaskArray.Count());};
00146 private:
00147 void SortTasksAscending();
00148 TInt GetTaskListLength();
00149 private:
00150 CHerculeanTaskManager();
00151 void ConstructL();
00152 private:
00153 RArray<TTask> iTaskArray;
00154 };
00155
00156 void Panic(TTaskManagerPanic aPanicCode)
00157 {
00158 _LIT(KTaskManagerPanic, "TTaskManager");
00159 User::Panic(KTaskManagerPanic, aPanicCode);
00160 }
00161
00162 TTask::TTask(THerculeanLabours aLabour)
00163 : iLabour(aLabour)
00164 {
00165 __ASSERT_ALWAYS( ((aLabour>=ESlayNemeanLion)&&(aLabour<=ECaptureCerberus)), Panic(EInvalidTaskId));
00166 Initialize();
00167 }
00168
00169 TTask::TTask(const TTask& aTask)
00170 : iLabour(aTask.iLabour)
00171 {
00172 iLabourName.Set(aTask.iLabourName);
00173 }
00174
00175 TTask& TTask::operator=(const TTask& aTask)
00176 {
00177 iLabour=aTask.iLabour;
00178 iLabourName.Set(aTask.iLabourName);
00179 return (*this);
00180 }
00181
00182 void TTask::Initialize()
00183 {
00184 switch (iLabour)
00185 {
00186 case (ESlayNemeanLion):
00187 {
00188 _LIT8(KSlayNemeanLion, "SlayNemeanLion");
00189 iLabourName.Set(KSlayNemeanLion);
00190 }
00191 break;
00192 case (ESlayHydra):
00193 {
00194 _LIT8(KSlayHydra, "SlayHydra");
00195 iLabourName.Set(KSlayHydra);
00196 }
00197 break;
00198 case (ECaptureCeryneianHind):
00199 {
00200 _LIT8(KCaptureCeryneianHind, "CaptureCeryneianHind");
00201 iLabourName.Set(KCaptureCeryneianHind);
00202 }
00203 break;
00204 case (ESlayErymanthianBoar):
00205 {
00206 _LIT8(KSlayErymanthianBoar, "SlayErymanthianBoar");
00207 iLabourName.Set(KSlayErymanthianBoar);
00208 }
00209 break;
00210 case (ECleanAugeanStables):
00211 {
00212 _LIT8(KCleanAugeanStables, "CleanAugeanStables");
00213 iLabourName.Set(KCleanAugeanStables);
00214 }
00215 break;
00216 case (ECancelCleanAugeanStables):
00217 {
00218 _LIT8(KCancelCleanAugeanStables, "CancelCleanAugeanStables");
00219 iLabourName.Set(KCancelCleanAugeanStables);
00220 }
00221 break;
00222 case (ESlayStymphalianBirds):
00223 {
00224 _LIT8(KSlayStymphalianBirds, "SlayStymphalianBirds");
00225 iLabourName.Set(KSlayStymphalianBirds);
00226 }
00227 break;
00228 case (ECancelSlayStymphalianBirds):
00229 {
00230 _LIT8(KCancelSlayStymphalianBirds, "CancelSlayStymphalianBirds");
00231 iLabourName.Set(KCancelSlayStymphalianBirds);
00232 }
00233 break;
00234 case (ECaptureCretanBull):
00235 {
00236 _LIT8(KCaptureCretanBull, "CaptureCretanBull");
00237 iLabourName.Set(KCaptureCretanBull);
00238 }
00239 break;
00240 case (ECaptureMaresOfDiomedes):
00241 {
00242 _LIT8(KCaptureMaresOfDiomedes, "CaptureMaresOfDiomedes");
00243 iLabourName.Set(KCaptureMaresOfDiomedes);
00244 }
00245 break;
00246 case (EObtainGirdleOfHippolyta):
00247 {
00248 _LIT8(KObtainGirdleOfHippolyta, "ObtainGirdleOfHippolyta");
00249 iLabourName.Set(KObtainGirdleOfHippolyta);
00250 }
00251 break;
00252 case (ECaptureOxenOfGeryon):
00253 {
00254 _LIT8(KCaptureOxenOfGeryon, "CaptureOxenOfGeryon");
00255 iLabourName.Set(KCaptureOxenOfGeryon);
00256 }
00257 break;
00258 case (ETakeGoldenApplesOfHesperides):
00259 {
00260 _LIT8(KTakeGoldenApplesOfHesperides, "TakeGoldenApplesOfHesperides");
00261 iLabourName.Set(KTakeGoldenApplesOfHesperides);
00262 }
00263 break;
00264 case (ECaptureCerberus):
00265 {
00266 _LIT8(KCaptureCerberus, "CaptureCerberus");
00267 iLabourName.Set(KCaptureCerberus);
00268 }
00269 break;
00270 default:
00271 ASSERT(EFalse);
00272 }
00273 }
00274
00275
00276
00277
00278
00279
00280 TInt TTask::CompareTaskNumbers(const TTask& aTask1, const TTask& aTask2)
00281 {
00282 if (aTask1.iLabour > aTask2.iLabour)
00283 return (1);
00284 else if (aTask1.iLabour < aTask2.iLabour)
00285 return (-1);
00286 else
00287 {
00288 ASSERT(aTask1.iLabour==aTask2.iLabour);
00289 return (0);
00290 }
00291 }
00292
00293 TBool TTask::MatchTasks(const TTask& aTask1, const TTask& aTask2)
00294 {
00295 if (aTask1.iLabour==aTask2.iLabour)
00296 {
00297 ASSERT(aTask1.iLabourName.Compare(aTask2.iLabourName)==0);
00298 return ETrue;
00299 }
00300
00301 return (EFalse);
00302 }
00303
00304
00305 CHerculeanTaskManager* CHerculeanTaskManager::NewLC()
00306 {
00307 CHerculeanTaskManager* me = new (ELeave) CHerculeanTaskManager();
00308 CleanupStack::PushL(me);
00309 me->ConstructL();
00310 return (me);
00311 }
00312
00313 CHerculeanTaskManager::CHerculeanTaskManager()
00314 : iTaskArray(KTaskArrayGranularity)
00315 {}
00316
00317 void CHerculeanTaskManager::ConstructL()
00318 {
00319 }
00320
00321 CHerculeanTaskManager::~CHerculeanTaskManager()
00322 {
00323 iTaskArray.Close();
00324 }
00325
00326
00327 void CHerculeanTaskManager::AppendTaskL(THerculeanLabours aTaskNumber)
00328 {
00329 TTask tempTask(aTaskNumber);
00330 User::LeaveIfError(iTaskArray.Append(tempTask));
00331 }
00332
00333
00334
00335 void CHerculeanTaskManager::DeleteTask(THerculeanLabours aTaskNumber)
00336 {
00337 TTask tempTask(aTaskNumber);
00338 TInt foundIndex = iTaskArray.Find(tempTask, TTask::MatchTasks);
00339 while (foundIndex!=KErrNotFound)
00340 {
00341 iTaskArray.Remove(foundIndex);
00342 foundIndex = iTaskArray.Find(tempTask, TTask::MatchTasks);
00343 }
00344 }
00345
00346 void CHerculeanTaskManager::GetTask(THerculeanLabours aTaskNumber, TTask& aTask)
00347 {
00348 TTask tempTask(aTaskNumber);
00349 TInt foundIndex = iTaskArray.Find(tempTask, TTask::MatchTasks);
00350 aTask = iTaskArray[foundIndex];
00351 }
00352
00353
00354
00355 void CHerculeanTaskManager::ListTasksL(RBuf8& aTaskList)
00356 {
00357
00358 TInt listLength = GetTaskListLength();
00359 ASSERT(listLength>=0);
00360
00361 aTaskList.CreateL(listLength);
00362
00363 TInt count=iTaskArray.Count();
00364 for (TInt index = 0; index<count; index++)
00365 {
00366 TTask task = iTaskArray[index];
00367 aTaskList.Append(KTaskEntry);
00368 aTaskList.Append(task.LabourName());
00369 }
00370 }
00371
00372
00373
00374 void CHerculeanTaskManager::ListTasksAscendingL(RBuf8& aTaskList)
00375 {
00376 SortTasksAscending();
00377 ListTasksL(aTaskList);
00378 }
00379
00380 void CHerculeanTaskManager::SortTasksAscending()
00381 {
00382 iTaskArray.Sort(TTask::CompareTaskNumbers);
00383 }
00384
00385
00386 TInt CHerculeanTaskManager::GetTaskListLength()
00387 {
00388 TInt taskEntryLength = KTaskEntry().Length();
00389
00390 TInt listLength = 0;
00391 TInt count=iTaskArray.Count();
00392 for (TInt index = 0; index<count; index++)
00393 {
00394 TTask task = iTaskArray[index];
00395 listLength+=task.LabourName().Length();
00396 listLength+=taskEntryLength;
00397 }
00398
00399 return (listLength);
00400 }
00401
00402
00403 void TestArraysL()
00404 {
00405 __UHEAP_MARK;
00406 CConsoleBase* console=Console::NewL(KTextExampleCode,TSize(KConsFullScreen,KConsFullScreen));
00407 CleanupStack::PushL(console);
00408
00409 CHerculeanTaskManager* taskManager = CHerculeanTaskManager::NewLC();
00410
00411 taskManager->AppendTaskL(ESlayHydra);
00412 taskManager->AppendTaskL(ESlayNemeanLion);
00413 taskManager->AppendTaskL(ECaptureCeryneianHind);
00414 taskManager->AppendTaskL(ECleanAugeanStables);
00415 taskManager->AppendTaskL(ECaptureCerberus);
00416 taskManager->AppendTaskL(ECaptureMaresOfDiomedes);
00417 taskManager->AppendTaskL(ESlayHydra);
00418 taskManager->AppendTaskL(ESlayNemeanLion);
00419 taskManager->AppendTaskL(ECaptureOxenOfGeryon);
00420 taskManager->AppendTaskL(ESlayErymanthianBoar);
00421
00422 ASSERT(taskManager->TaskCount()==10);
00423 RBuf8 taskBuf;
00424
00425 taskManager->ListTasksL(taskBuf);
00426 taskBuf.Zero();
00427 taskBuf.ReAllocL(0);
00428
00429 taskManager->DeleteTask(ESlayHydra);
00430 ASSERT(taskManager->TaskCount()==8);
00431
00432 taskManager->ListTasksAscendingL(taskBuf);
00433 taskBuf.Zero();
00434 taskBuf.ReAlloc(0);
00435
00436 taskManager->AppendTaskL(ECleanAugeanStables);
00437 taskManager->AppendTaskL(ECancelCleanAugeanStables);
00438 ASSERT(taskManager->TaskCount()==10);
00439
00440 taskManager->ListTasksAscendingL(taskBuf);
00441 taskBuf.Zero();
00442 taskBuf.ReAlloc(0);
00443
00444 CleanupStack::PopAndDestroy(taskManager);
00445
00446 console->Printf(KTextOK);
00447 console->Printf(KTextPressAnyKey);
00448 console->Getch();
00449 CleanupStack::PopAndDestroy(console);
00450 __UHEAP_MARKEND;
00451 }
00452
00453
00454
00455 TInt E32Main()
00456 {
00457 __UHEAP_MARK;
00458
00459 CTrapCleanup* theCleanupStack = CTrapCleanup::New();
00460
00461 TRAPD(err, TestArraysL());
00462
00463
00464
00465
00466 User::After(500000);
00467
00468 ASSERT(KErrNone==err);
00469 delete theCleanupStack;
00470
00471 __UHEAP_MARKEND;
00472 return (err);
00473 }
00474