forked from Klebert-Engineering/zsr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.hpp
391 lines (327 loc) · 7.46 KB
/
types.hpp
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#pragma once
#include <zserio/BitStreamReader.h>
#include <zserio/BitStreamWriter.h>
#include <zserio/PreWriteAction.h>
#include <zserio/IService.h>
#include "zsr/variant.hpp"
#include <any>
#include <functional>
#include <memory>
#include <string>
#include <deque>
#include <optional>
#if _MSC_VER
/* Disable warnings about unexported template instantiations */
#pragma warning(push)
#pragma warning(disable: 4251)
#endif
/* Delete copy & move operators */
#define META_TYPE_ROOT(TYPE) \
TYPE() {}; \
TYPE(const TYPE &) = delete; \
TYPE(TYPE &&) = delete; \
TYPE& operator=(const TYPE &) = delete; \
TYPE& operator=(TYPE &&) = delete;
#define META_TYPE(TYPE, PARENT) \
PARENT& parent; \
TYPE(PARENT* p) : parent(*p) {}; \
TYPE(const TYPE &) = delete; \
TYPE(TYPE &&) = delete; \
TYPE& operator=(const TYPE &) = delete; \
TYPE& operator=(TYPE &&) = delete;
namespace zsr {
struct Package;
struct Bitmask;
struct Enumeration;
struct Compound;
struct Service;
/**
* Generic parameter list.
* See `Parameter::set`.
*/
struct ZSR_EXPORT ParameterList
{
std::any list;
};
/**
* ZSerio type information.
*
* TODO: Take ZType data with grain of salt. Especially array types
* might be incomplete.
*
* Use information from CType for type detection/conversion.
* The ZType does not have to match the CType.
*/
struct ZSR_EXPORT ZType
{
enum Type
{
UInt,
Int,
VarUInt,
VarInt,
Float,
Bitmask,
Bitfield,
UBitfield,
VarBitfield,
VarUBitfield,
Bool,
Enum,
String,
Structure,
} type;
size_t size = 0; /* Size in bits */
bool array = false; /* Array of type */
};
/**
* Generated C++ type information.
*/
struct ZSR_EXPORT CType
{
enum Type
{
Bool,
UInt,
Int,
Float,
String,
Structure,
BitBuffer,
} type;
size_t size = 0; /* Size in bytes */
bool array = false; /* Array (vector) of type */
};
struct ZSR_EXPORT TypeRef
{
std::string package;
std::string ident;
ZType ztype;
CType ctype;
};
/**
* Subtype
*/
struct ZSR_EXPORT SubType
{
META_TYPE(SubType, Package)
std::string ident;
std::optional<TypeRef> type;
};
/**
* Package constant
*/
struct ZSR_EXPORT Constant
{
META_TYPE(Constant, Package)
std::string ident;
Variant value;
std::optional<TypeRef> type;
};
/**
* Bitmask value
*/
struct ZSR_EXPORT BitmaskValue
{
META_TYPE(BitmaskValue, Bitmask)
std::string ident;
Variant value;
};
/**
* Bitmask
*/
struct ZSR_EXPORT Bitmask
{
META_TYPE(Bitmask, Package)
std::string ident;
std::deque<BitmaskValue> values;
};
/**
* Enumeration item
*/
struct ZSR_EXPORT EnumerationItem
{
META_TYPE(EnumerationItem, Enumeration)
std::string ident;
Variant value;
};
/**
* Enumeration
*/
struct ZSR_EXPORT Enumeration
{
META_TYPE(Enumeration, Package)
std::string ident;
std::deque<EnumerationItem> items;
};
/**
* Compound field/member
*/
struct ZSR_EXPORT Field
{
META_TYPE(Field, Compound)
std::string ident;
std::optional<TypeRef> type;
/**
* Function that returns the fields value.
*/
std::function<Variant(const Introspectable&)> get = nullptr;
/**
* Optional function to set the fields value.
*/
std::function<void(Introspectable&, Variant)> set = nullptr;
/**
* Optional function to check if the field is set.
*/
std::function<bool(const Introspectable&)> has = nullptr;
/**
* Optional function to reset the optional field.
*/
std::function<void(Introspectable&)> reset = nullptr;
};
/**
* Compound parameter
*/
struct ZSR_EXPORT Parameter
{
META_TYPE(Parameter, Compound)
std::string ident;
const TypeRef* type = nullptr;
const Field* field = nullptr; /* Matching field (read-only) */
std::function<void(ParameterList&, Variant)> set = nullptr;
};
/**
* Function
*/
struct ZSR_EXPORT Function
{
META_TYPE(Function, Compound)
std::string ident;
std::optional<TypeRef> type;
std::function<Variant(const Introspectable&)> call = nullptr;
};
/**
* Compound (Structure, Choice or Union)
*/
struct ZSR_EXPORT Compound
{
META_TYPE(Compound, Package)
std::string ident;
enum class Type {
Structure,
Choice,
Union,
} type;
std::deque<Parameter> parameters;
std::deque<Field> fields;
std::deque<Function> functions;
/**
* Returns a new instance of the compound.
*
* For compounds that have parameters, `initialize`
* must be called.
*/
std::function<Introspectable()> alloc = nullptr;
/**
* Optional initialization function.
* Set for compounds that have parameters.
*
* @param Instance of the introspectable to initialize.
* @param Parameter list, see `Parameter::set`.
*/
std::function<void(Introspectable&, ParameterList)> initialize = nullptr;
/**
* Optional child initializator.
*
* Maps to zserio generated `initializeChildren` function.
*/
std::function<void(Introspectable&)> initializeChildren = nullptr;
/**
* Comparison function.
*
* Maps to zserio generated `operator==` function.
*/
std::function<bool(const Introspectable&, const Introspectable&)> compare =
nullptr;
/**
* Hash function.
*
* Maps to zserio generated `hashCode` function.
*/
std::function<int(const Introspectable&)> hash =
nullptr;
/**
* Bitsize function.
*
* Maps to zserio generated `bitSizeOf` function.
*/
std::function<size_t(const Introspectable&)> bitSize =
nullptr;
/**
* Deserialization function.
*
* Maps to zserio generated `read` function.
*/
std::function<void(Introspectable&, ::zserio::BitStreamReader&)> read =
nullptr;
/**
* Serialization function.
*
* Maps to zserio generated `write` function.
*/
std::function<void(Introspectable&, ::zserio::BitStreamWriter&)> write =
nullptr;
};
/**
* Service method
*/
struct ZSR_EXPORT ServiceMethod
{
META_TYPE(ServiceMethod, Service)
std::string ident;
TypeRef requestType;
TypeRef responseType;
std::function<Variant(::zserio::IService&, const Variant&)> call;
/**
* Object passed as user-data to zserio generated
* service functions.
*
* Contains the original, non-serialized, request data plus
* meta information about the service method being called.
*/
struct Context
{
const struct Service& service;
const struct ServiceMethod& method;
const zsr::Variant& request;
};
};
/**
* Service
*/
struct ZSR_EXPORT Service
{
META_TYPE(Service, Package)
std::string ident;
std::deque<ServiceMethod> methods;
};
/**
* Package
*/
struct ZSR_EXPORT Package
{
META_TYPE_ROOT(Package)
std::string ident;
std::deque<SubType> subTypes;
std::deque<Constant> constants;
std::deque<Enumeration> enumerations;
std::deque<Bitmask> bitmasks;
std::deque<Compound> compounds;
std::deque<Service> services;
};
} // namespace zsr
#undef META_TYPE_ROOT
#undef META_TYPE
#if _MSC_VER
#pragma warning(pop)
#endif