forked from ctlbox/controlbox-cpp
-
Notifications
You must be signed in to change notification settings - Fork 4
/
GenericContainer.h
286 lines (232 loc) · 6.03 KB
/
GenericContainer.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* Copyright 2014-2015 Matthew McGowan.
*
* This file is part of Nice Firmware.
*
* BrewPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with BrewPi. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "Values.h"
#include "Memops.h"
typedef void (*ObjectHandler)(Object*, void* data);
inline void do_update(Object* o, void* data) {
o->update();
}
inline void do_prepare(Object* o, void* data) {
prepare_t* result = (prepare_t*)data;
prepare_t p = o->prepare();
if (p>*result)
*result = p;
}
#define DYNAMIC_CONTAINER_MAINTAIN_SIZE 1
/**
* A container whose backing store is allocated dynamically as objects are added.
*/
class DynamicContainer: public OpenContainer
{
private:
Object** _items; // the items in this container.
#if DYNAMIC_CONTAINER_MAINTAIN_SIZE
container_id sz;
#endif
void assign(container_id id, Object* item) {
_items[id] = item;
}
bool expand(uint8_t sz);
public:
static Object* create(ObjectDefinition& def) { return new DynamicContainer(); }
void iterate_objects(void* data, ObjectHandler handler) {
for (container_id i=0; i<_size(); i++) {
// using the function to access the item is requires 10 bytes less space than
// directly using _items[i]
Object* obj = item(i);
if (obj)
handler(obj, data);
}
}
prepare_t prepare() {
prepare_t time = 0;
iterate_objects(&time, do_prepare);
return time;
}
virtual void update() {
iterate_objects(NULL, do_update);
}
DynamicContainer() {
_items = (Object**)malloc(sizeof(Object*));
assign(0, NULL);
setSize(0);
}
inline void setSize(container_id size) {
#if DYNAMIC_CONTAINER_MAINTAIN_SIZE
sz = size;
#endif
}
Object* item(container_id id);
container_id next();
bool add(container_id slot, Object* item);
void remove(container_id id);
/*
* The number of items in this container.
*/
container_id size() { return _size(); }
inline container_id _size() {
#if DYNAMIC_CONTAINER_MAINTAIN_SIZE // fetch the block size that preceeds the malloc'ed pointer
return sz;
#else
return container_id(((pointer_scalar)_items[-3])/sizeof(Object*));
#endif
}
#if OBJECT_VIRTUAL_DESTRUCTOR
// the contract says that before a container is deleted, the caller should ensure all contained objects
// are also deleted. (if they were added.)
~DynamicContainer() {
for (int i=0; i<size();i++)
delete_object(item(i));
free(_items);
}
#endif
};
/**
* A container with space statically allocated.
*/
template<int SIZE> class StaticTemplateContainer : public OpenContainer
{
private:
Object* _items[SIZE]; // the items in this container.
container_id freeSlot() {
for (int i=0; i<SIZE;i++)
if (!_items[i])
return i;
return -1;
}
void prepare(Object* item, prepare_t& time) {
if (item)
time = max(time, item->prepare());
}
public:
prepare_t prepare() {
prepare_t time = 0;
for (int i=0; i<size(); i++ ) {
prepare(item(i), time);
}
return time;
}
virtual void update() {
for (int i=0; i<size(); i++ ) {
Object* o = item(i);
if (o)
o->update();
}
}
StaticTemplateContainer() {
clear((uint8_t*)_items, SIZE*sizeof(Object*));
}
Object* item(container_id id) {
return _items[id];
}
container_id next() {
return freeSlot();
}
bool add(container_id slot, Object* item) {
if (slot>=SIZE)
return false;
remove(slot);
_items[slot] = item;
return true;
}
void remove(container_id id) {
delete_object(_items[id]);
_items[id] = NULL;
}
/*
* The number of items in this container.
*/
container_id size() { return SIZE; }
#if OBJECT_VIRTUAL_DESTRUCTOR
// the contract says that before a container is deleted, the caller should ensure all contained objects
// are also deleted. (if they were added.)
~StaticContainer() {
for (int i=0; i<SIZE;i++)
delete_object(_items[i]);
}
#endif
};
/**
* A container with space statically allocated.
*/
class FixedContainer : public OpenContainer
{
private:
container_id SIZE;
Object** _items;
container_id freeSlot() {
for (int i=0; i<size();i++)
if (!_items[i])
return i;
return -1;
}
void prepare(Object* item, prepare_t& time) {
if (item)
time = max(time, item->prepare());
}
public:
FixedContainer(container_id size, Object** items)
: SIZE(size), _items(items) {
clear((uint8_t*)_items, SIZE*sizeof(Object*));
}
#if OBJECT_VIRTUAL_DESTRUCTOR
// the contract says that before a container is deleted, the caller should ensure all contained objects
// are also deleted. (if they were added.)
~StaticContainer() {
for (int i=0; i<SIZE;i++)
delete_object(_items[i]);
}
#endif
prepare_t prepare() {
prepare_t time = 0;
for (int i=0; i<size(); i++ ) {
prepare(item(i), time);
}
return time;
}
virtual void update() {
for (int i=0; i<size(); i++ ) {
Object* o = item(i);
if (o)
o->update();
}
}
Object* item(container_id id) {
return _items[id];
}
container_id next() {
return freeSlot();
}
bool add(container_id slot, Object* item) {
if (slot>=SIZE)
return false;
remove(slot);
_items[slot] = item;
return true;
}
void remove(container_id id) {
delete_object(_items[id]);
_items[id] = NULL;
}
/*
* The number of items in this container.
*/
container_id size() { return SIZE; }
};