-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVariableList.h
180 lines (155 loc) · 3.75 KB
/
VariableList.h
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#pragma once
#include <ConfigurableFirmata.h>
#include <FirmataFeature.h>
#include "ObjectVector.h"
#include "Exceptions.h"
#include "ClrException.h"
#include "interface/SystemException.h"
struct VariableListEntry
{
public:
VariableListEntry()
{
Token = 0;
Next = nullptr;
}
int Token;
VariableListEntry* Next;
Variable Data; // Stored inline. Length is variable (may reach beyond end of struct!)
};
/// <summary>
/// A List that can contain arbitrary sized variables. Access is O(n)
/// </summary>
class VariableList
{
private:
VariableListEntry* _first;
VariableListEntry* _tail;
public:
typedef VariableListEntry* iterator;
VariableList()
{
_first = nullptr;
_tail = nullptr;
}
~VariableList()
{
clear();
}
Variable& at(int token)
{
VariableListEntry* current = _first;
while (current != nullptr)
{
if (current->Token == token)
{
return current->Data;
}
current = current->Next;
}
throw ClrException(SystemException::ClassNotFound, token);
}
VariableListEntry* first()
{
return _first;
}
VariableListEntry* next(VariableListEntry* entry)
{
return entry->Next;
}
bool contains(int token) const
{
VariableListEntry* current = _first;;
while (current != nullptr)
{
if (current->Token == token)
{
return true;
}
current = current->Next;
}
return false;
}
/// <summary>
/// Appends a new entry to the list. Only use when the token is not already there.
/// The entry's data part is ignored in this method, but the full size is reserved.
/// </summary>
/// <param name="token">Token of the new entry</param>
/// <param name="entry">Description of the new entry</param>
/// <returns>Reference to the new entry</returns>
Variable& insert(int token, Variable& entry)
{
// Not found. Create new entry
// TODO: Should know how much we need to store the variable header alone
size_t size = sizeof(Variable) + entry.fieldSize() + sizeof(int) + sizeof(void*);
VariableListEntry* newMem = (VariableListEntry*)mallocEx(size);
memset(newMem, 0, size);
newMem->Token = token;
newMem->Next = nullptr;
if (_tail == nullptr)
{
// First entry in queue
_first = _tail = newMem;
}
else
{
_tail->Next = newMem;
_tail = newMem;
}
newMem->Data.setSize(entry.fieldSize());
newMem->Data.Marker = VARIABLE_DEFAULT_MARKER;
newMem->Data.Type = entry.Type;
return newMem->Data;
}
Variable& insertOrUpdate(int token, Variable& entry)
{
VariableListEntry* current = _first;
while (current != nullptr)
{
if (current->Token == token)
{
// Check size is equal. There's something seriously wrong if not
if (current->Data.fieldSize() != entry.fieldSize())
{
throw ClrException(SystemException::FieldAccess, token);
}
// Overwrite existing content
memcpy(¤t->Data.Int32, &entry.Int32, entry.fieldSize());
return current->Data;
}
current = current->Next;
}
// Not found. Create new entry
// TODO: Should know how much we need to store the variable header alone
size_t size = sizeof(Variable) + entry.fieldSize() + sizeof(int) + sizeof(void*);
VariableListEntry* newMem = (VariableListEntry*)mallocEx(size);
memset(newMem, 0, size);
newMem->Data.setSize(entry.fieldSize()); // so that the copy operator knows it's fine to copy there
newMem->Data = entry;// performs a full copy
newMem->Token = token;
newMem->Next = nullptr;
if (_tail == nullptr)
{
// First entry in queue
_first = _tail = newMem;
}
else
{
_tail->Next = newMem;
_tail = newMem;
}
return newMem->Data;
}
void clear()
{
VariableListEntry* current = _first;
while (current != nullptr)
{
VariableListEntry* last = current;
current = current->Next;
freeEx(last);
}
_first = nullptr;
_tail = nullptr;
}
};