Accessories
Arduino for motors and lights library.
ActionsStack.cpp
1 /*************************************************************
2 project: <Accessories>
3 author: <Thierry PARIS>
4 description: <Class for a actions stack>
5 *************************************************************/
6 
7 #include "ActionsStack.hpp"
8 
9 Action::Action(unsigned long inId, ACCESSORIES_EVENT_TYPE inEvent, int inData)
10 {
11  this->Id = inId;
12  this->Event = inEvent;
13  this->Data = inData;
14 }
15 
17 bool ActionsStack::FillingStack(false);
18 
20 {
21  this->size = inSize;
22  this->pList = new Action*[inSize];
23 
24  for (int i = 0; i < this->size; i++)
25  {
26  this->pList[i] = NULL;
27  }
28 }
29 
30 // Returns the index of the new added action.
31 unsigned char ActionsStack::Add(unsigned long inId, ACCESSORIES_EVENT_TYPE inEvent, int inData)
32 {
33  for (int i = 0; i < this->size; i++)
34  {
35  if (this->pList[i] == NULL)
36  {
37  this->pList[i] = new Action(inId, inEvent, inData);
38  return i;
39  }
40  }
41 
42  return this->size + 1; // action lost, the stack is full !
43 }
44 
46 {
47  for (int i = 0; i < this->size; i++)
48  {
49  if (this->pList[i] != NULL)
50  {
51  Action *ret = this->pList[i];
52  this->pList[i] = NULL;
53  return ret;
54  }
55  }
56 
57  return NULL;
58 }
59 
60 // Returns the index of the new added action.
61 void ActionsStack::Delete(int inIndex)
62 {
63  if (this->pList[inIndex] != NULL)
64  {
65  delete this->pList[inIndex];
66  this->pList[inIndex] = NULL;
67  }
68 }
69 
70 // Returns the index of the new added action.
72 {
73  for (int i = 0; i < this->size; i++)
74  this->Delete(i);
75 }
76 
77 // Returns the number of stacked actions.
79 {
80  int count = 0;
81 
82  for (int i = 0; i < this->size; i++)
83  {
84  if (this->pList[i] != NULL)
85  count++;
86  }
87 
88  return count;
89 }
90 
91 Action *ActionsStack::operator[](unsigned char inIndex)
92 {
93  return this->pList[inIndex];
94 }
Action * operator[](unsigned char idx)
int GetNumber() const
Action * GetActionToExecute()
unsigned char Add(unsigned long inId, ACCESSORIES_EVENT_TYPE inEvent, int inData = 0)
ActionsStack(int inSize)
static bool FillingStack
#define ACTION_STACK_SIZE
Definition: Accessories.h:236
void Delete(int inIndex)
static ActionsStack Actions
ACCESSORIES_EVENT_TYPE Event
unsigned long Id
Action(unsigned long inId, ACCESSORIES_EVENT_TYPE inEvent, int inData = 0)
Definition: ActionsStack.cpp:9