-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathenumoption.cpp
More file actions
69 lines (53 loc) · 1.54 KB
/
enumoption.cpp
File metadata and controls
69 lines (53 loc) · 1.54 KB
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
#include "enumoption.h"
#define SAVE(var) \
memcpy(ptr, &(var), sizeof((var))); \
ptr += sizeof((var))
#include <visstring.h>
#include <cstring>
namespace NaoControl {
EnumItem::EnumItem(const char* name, int value) {
VisString vName(name);
len = vName.size() + sizeof(value);
buffer = (uint8_t*)malloc(len);
uint8_t* ptr = buffer;
ptr = vName.save(ptr);
SAVE(value);
}
uint8_t* EnumItem::save(uint8_t* ptr) {
memcpy(ptr, buffer, len);
return ptr + len;
}
EnumOption::EnumOption(const char* cname, int* val, std::function<void(int)> callb)
: Option(cname), value(val), callback(std::move(callb)) {
VisString optionName(cname);
length = sizeof(uint8_t) /* type */ + optionName.size() + sizeof(*val) + sizeof(int32_t) /* item count */;
}
uint8_t* EnumOption::save(uint8_t* start) {
VisString optionName(name.c_str());
uint8_t* ptr = start;
*ptr = (uint8_t)O_ENUM;
ptr++;
ptr = optionName.save(ptr);
SAVE(*value);
int itemCount = enumItems.size();
SAVE(itemCount);
for (auto& enumItem : enumItems) {
ptr = enumItem->save(ptr);
}
return ptr;
}
void EnumOption::setValue(const uint8_t* buffer, int len) {
int newValue;
if (len != sizeof(newValue))
return;
memcpy(&newValue, buffer, len);
*value = newValue;
if (callback != nullptr) {
callback(*value);
}
}
void EnumOption::addEnumItem(EnumItem* item) {
enumItems.emplace_back(item);
length += item->size();
}
} /* namespace NaoControl */