-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathintoption.cpp
More file actions
98 lines (76 loc) · 2.41 KB
/
intoption.cpp
File metadata and controls
98 lines (76 loc) · 2.41 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
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
#define INT_SAVE(var) \
memcpy(ptr, &(var), sizeof((var))); \
ptr += sizeof((var))
#include <intoption.h>
#include <visstring.h>
#include <cstring>
#include <utility>
namespace NaoControl {
template <typename T>
IntOption<T>::IntOption(const char* cname, T* val, T min, T max, T step, std::function<void(T)> callb)
: Option(cname), value(val), getCallback(nullptr), setCallback(std::move(callb)) {
VisString optionName(cname);
length = sizeof(uint8_t) /* type */ + optionName.size() + sizeof(int8_t) /* type size */ + sizeof(min) +
sizeof(max) + sizeof(step) + sizeof(*val);
int8_t intSize = sizeof(*val);
buffer = (uint8_t*)malloc(length);
uint8_t* ptr = buffer;
*ptr = (uint8_t)O_INT;
ptr++;
ptr = optionName.save(ptr);
INT_SAVE(intSize);
INT_SAVE(min);
INT_SAVE(max);
INT_SAVE(step);
}
template <typename T>
IntOption<T>::IntOption(const char* cname, T min, T max, T step, std::function<T(void)> getCallback,
std::function<void(T)> setCallback)
: Option(cname),
value(nullptr),
getCallback(std::move(std::move(getCallback))),
setCallback(std::move(setCallback)) {
VisString optionName(cname);
length = sizeof(uint8_t) /* type */ + optionName.size() + sizeof(int8_t) /* type size */ + sizeof(min) +
sizeof(max) + sizeof(step) + sizeof(T);
int8_t intSize = sizeof(T);
buffer = (uint8_t*)malloc(length);
uint8_t* ptr = buffer;
*ptr = (uint8_t)O_INT;
ptr++;
ptr = optionName.save(ptr);
INT_SAVE(intSize);
INT_SAVE(min);
INT_SAVE(max);
INT_SAVE(step);
}
template <typename T>
uint8_t* IntOption<T>::save(uint8_t* start) {
uint8_t* ptr = buffer;
ptr += length - sizeof(*value);
T tmpVal = 0;
if (value != nullptr) {
tmpVal = *value;
} else {
tmpVal = getCallback();
}
INT_SAVE(tmpVal);
return Option::save(start);
}
template <typename T>
void IntOption<T>::setValue(const uint8_t* buffer, int len) {
T newValue;
if (len != sizeof(newValue))
return;
memcpy(&newValue, buffer, len);
if (value != nullptr)
*value = newValue;
if (setCallback != nullptr) {
setCallback(newValue);
}
}
template class IntOption<int>;
template class IntOption<int16_t>;
template class IntOption<unsigned char>;
template class IntOption<signed char>;
} // namespace NaoControl