-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.cpp
105 lines (84 loc) · 2.6 KB
/
task.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "task.h"
#define nullptr NULL
namespace G8
{
TaskSystem::TaskSystem(char const * const pMenuTitle)
: nTasks(0)
{
// Alloc space for title and copy value
size_t s = strlen(pMenuTitle) + 1;
pTitle = reinterpret_cast<char *>(malloc(s));
memcpy(pTitle, pMenuTitle, s);
// Alloc space for function pointers and titles
pTasks = reinterpret_cast<TaskFunction*>(malloc(sizeof(TaskFunction) * MAX_TASKS));
ppNames = reinterpret_cast<char**>(malloc(sizeof(char*) * MAX_TASKS));
memset(pTasks, 0x00, sizeof(TaskFunction) * MAX_TASKS );
memset(ppNames, 0x00, sizeof(char*) * MAX_TASKS);
}
TaskSystem::~TaskSystem(void)
{
free(pTitle);
pTitle = nullptr;
free(pTasks);
pTasks = nullptr;
for(size_t i = 0; i < MAX_TASKS; i++)
{
if(ppNames[i])
{
free(ppNames[i]);
ppNames[i] = nullptr;
}
}
free(ppNames);
ppNames = nullptr;
}
size_t TaskSystem::Find(const char * const pName) const
{
for (size_t i = 0; i < nTasks; i++)
{
// Check if the names match
if (strcmp(ppNames[i], pName) == 0)
{
return i; // We found a match, return the index
}
}
// Return the UINT32_MAX
return __UINT32_MAX__;
}
TASK_RESULT TaskSystem::RunTask(const size_t index, Robot * const pRobot) const
{
if(index < nTasks)
return pTasks[index](pRobot);
return TASK_RESULT_FAIL;
}
TASK_RESULT TaskSystem::RunTask(const char * const pName, Robot * const pRobot) const
{
RunTask(Find(pName), pRobot);
}
size_t TaskSystem::SelectTaskFromMenu(void) const
{
return UI::MenuSelect(pTitle, ppNames, nTasks);
}
TASK_RESULT TaskSystem::RunTaskFromMenu(Robot * const pRobot) const
{
return RunTask(SelectTaskFromMenu(), pRobot);
}
bool TaskSystem::AddTask(const char * const pName, TaskFunction pFunc)
{
// Is there any space left to store this task
if(nTasks >= MAX_TASKS)
return false;
// Store the function pointer
pTasks[nTasks] = pFunc;
// Store the name
size_t sSize = strlen(pName) + 1;
if(ppNames[nTasks] = reinterpret_cast<char*>(malloc(sSize)))
memcpy(ppNames[nTasks], pName, sSize);
else
return false;
// Increment number of tasks
nTasks++;
// All done!
return true;
}
}