-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmessages.cpp
More file actions
108 lines (68 loc) · 2.64 KB
/
Copy pathmessages.cpp
File metadata and controls
108 lines (68 loc) · 2.64 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
99
100
101
102
103
104
105
106
107
108
// Message types
@ for message in graph_type['message_types']
@ set msg_class = get_msg_class(graph_type["id"], message['id'])
@ set fields = message['fields']
class {{ msg_class }}: public msg_t {
public:
{{ lmap(declare_variable, fields['scalars']) }}
{{ msg_class }} (){
this->index = {{ loop.index0 }};
this->nscalars = {{ fields['scalars'] | count }};
@ for scalar in fields['scalars']
this->{{ scalar['name'] }} = 0;
@ endfor
}
{{ msg_class }}(int *arr) {
int index = 0;
@ for scalar in fields['scalars']
this->{{ scalar['name'] }} = ({{ scalar['type'] }}) arr[index++];
@ endfor
}
void print() {
@ for scalar in fields['scalars']
cprintf(" - {{ scalar['name']}} = %d\n", this->{{ scalar['name']}});
@ endfor
}
const char* getName() {
return "{{ message['id'] }}";
}
void serialize(char *buf) {
int index = 0;
@ for scalar in fields['scalars']
*(({{ scalar['type'] }}*) (buf + index)) = this->{{ scalar['name'] }};
index += sizeof({{ scalar['type'] }});
@ endfor
}
int getByteCount() {
@ set types = fields['scalars'] | map(attribute='type')
@ set terms = mformat("sizeof(%s)", types) or ["0"]
return {{ terms | join(' + ') }};
}
void deserialize(char *buf) {
int index = 0;
@ for scalar in fields['scalars']
this->{{ scalar['name'] }} = *(({{ scalar['type'] }}*) (buf + index));
index += sizeof({{ scalar['type'] }});
@ endfor
}
void read_debug() {
@ for scalar in fields['scalars']
scanf("%d", &(this->{{ scalar['name']}}));
@ endfor
}
int to_arr(int *arr, int arr_length) {
// Read (scalar) fields of a message from a given array.
// The argument `nfields` must equal the class `nscalars` field,
// otherwise an error is returned.
if (arr_length < this->nscalars) {
printf("Function {{ message['id'] }}.to_arr requires array with %d elements (%d provided)\n", this->nscalars, arr_length);
return 1;
}
int index = 0;
@ for scalar in fields['scalars']
arr[index++] = this->{{ scalar['name']}};
@ endfor
return 0; // exit successfully
}
};
@ endfor