-
Notifications
You must be signed in to change notification settings - Fork 0
/
dstccallback.hpp
724 lines (682 loc) · 33.7 KB
/
dstccallback.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
// Copyright (C) 2019, Jaguar Land Rover
// This program is licensed under the terms and conditions of the
// Mozilla Public License, version 2.0. The full text of the
// Mozilla Public License is at https://www.mozilla.org/MPL/2.0/
//
// Author:Steven Martin ([email protected])
#pragma once
#include <mutex>
#include <functional>
#include <unordered_map>
#include <memory>
#include <iostream> // debug only, should be removed
extern "C" {
#include "dstc.h"
}
namespace dstc {
/*!
* Base class of all derived templates of CallbackFunction. This exists to allow template
* magic check if an argument is of a CallbackFunction type to be treated correctly during
* serialization and deserialization.
*/
class CallbackFunctionBase {
public:
virtual void execute(uint8_t* payload, uint16_t payload_len) = 0;
};
namespace utils {
template <typename TYPE>
TYPE parseArg(uint8_t** ptr) {
TYPE ret_val;
if constexpr (std::is_same<dstc_dynamic_data_t, TYPE>::value) {
memcpy(&ret_val.length, (*ptr), sizeof(uint16_t));
(*ptr) += sizeof(uint16_t);
ret_val.data = (*ptr);
(*ptr) += ret_val.length;
}
else if constexpr (std::is_array<TYPE>::value) {
memcpy((void*)ret_val, (*ptr), sizeof(TYPE));
(*ptr) += sizeof(TYPE);
}
else if constexpr (std::is_base_of<CallbackFunctionBase, TYPE>::value) {
memcpy((void*)&ret_val, (*ptr), sizeof(dstc_callback_t));
(*ptr) += sizeof(dstc_callback_t);
}
else {
memcpy((void*)&ret_val, (*ptr), sizeof(TYPE));
(*ptr) += sizeof(TYPE);
}
return ret_val;
}
template<typename TYPE>
typename std::remove_extent<TYPE>::type* parseArgArray(uint8_t** ptr) {
auto p = new typename std::remove_extent<TYPE>::type [std::extent<TYPE>::value];
memcpy((void*) p, (*ptr), sizeof(TYPE));
(*ptr) += sizeof(TYPE);
return p;
}
// Unless there ends up being a cleaner way, we use these macro-like
// templates to extract data as the correct type so we can use the callback
// function as appropriate. These definitions are generated in gen_getargtypefunctions.py
template <typename ARG0_T,
typename ... OTHERS>
auto getArgType0(uint8_t** ptr) {
if constexpr (std::is_array<ARG0_T>::value)
return parseArgArray<ARG0_T>(ptr);
else
return parseArg<ARG0_T>(ptr);
}
template <typename ARG0_T,
typename ARG1_T,
typename ... OTHERS>
auto getArgType1(uint8_t** ptr) {
if constexpr (std::is_array<ARG1_T>::value)
return parseArgArray<ARG1_T>(ptr);
else
return parseArg<ARG1_T>(ptr);
}
template <typename ARG0_T,
typename ARG1_T,
typename ARG2_T,
typename ... OTHERS>
auto getArgType2(uint8_t** ptr) {
if constexpr (std::is_array<ARG2_T>::value)
return parseArgArray<ARG2_T>(ptr);
else
return parseArg<ARG2_T>(ptr);
}
template <typename ARG0_T,
typename ARG1_T,
typename ARG2_T,
typename ARG3_T,
typename ... OTHERS>
auto getArgType3(uint8_t** ptr) {
if constexpr (std::is_array<ARG3_T>::value)
return parseArgArray<ARG3_T>(ptr);
else
return parseArg<ARG3_T>(ptr);
}
template <typename ARG0_T,
typename ARG1_T,
typename ARG2_T,
typename ARG3_T,
typename ARG4_T,
typename ... OTHERS>
auto getArgType4(uint8_t** ptr) {
if constexpr (std::is_array<ARG4_T>::value)
return parseArgArray<ARG4_T>(ptr);
else
return parseArg<ARG4_T>(ptr);
}
template <typename ARG0_T,
typename ARG1_T,
typename ARG2_T,
typename ARG3_T,
typename ARG4_T,
typename ARG5_T,
typename ... OTHERS>
auto getArgType5(uint8_t** ptr) {
if constexpr (std::is_array<ARG5_T>::value)
return parseArgArray<ARG5_T>(ptr);
else
return parseArg<ARG5_T>(ptr);
}
template <typename ARG0_T,
typename ARG1_T,
typename ARG2_T,
typename ARG3_T,
typename ARG4_T,
typename ARG5_T,
typename ARG6_T,
typename ... OTHERS>
auto getArgType6(uint8_t** ptr) {
if constexpr (std::is_array<ARG6_T>::value)
return parseArgArray<ARG6_T>(ptr);
else
return parseArg<ARG6_T>(ptr);
}
template <typename ARG0_T,
typename ARG1_T,
typename ARG2_T,
typename ARG3_T,
typename ARG4_T,
typename ARG5_T,
typename ARG6_T,
typename ARG7_T,
typename ... OTHERS>
auto getArgType7(uint8_t** ptr) {
if constexpr (std::is_array<ARG7_T>::value)
return parseArgArray<ARG7_T>(ptr);
else
return parseArg<ARG7_T>(ptr);
}
template <typename ARG0_T,
typename ARG1_T,
typename ARG2_T,
typename ARG3_T,
typename ARG4_T,
typename ARG5_T,
typename ARG6_T,
typename ARG7_T,
typename ARG8_T,
typename ... OTHERS>
auto getArgType8(uint8_t** ptr) {
if constexpr (std::is_array<ARG8_T>::value)
return parseArgArray<ARG8_T>(ptr);
else
return parseArg<ARG8_T>(ptr);
}
template <typename ARG0_T,
typename ARG1_T,
typename ARG2_T,
typename ARG3_T,
typename ARG4_T,
typename ARG5_T,
typename ARG6_T,
typename ARG7_T,
typename ARG8_T,
typename ARG9_T,
typename ... OTHERS>
auto getArgType9(uint8_t** ptr) {
if constexpr (std::is_array<ARG9_T>::value)
return parseArgArray<ARG9_T>(ptr);
else
return parseArg<ARG9_T>(ptr);
}
template <typename ARG0_T,
typename ARG1_T,
typename ARG2_T,
typename ARG3_T,
typename ARG4_T,
typename ARG5_T,
typename ARG6_T,
typename ARG7_T,
typename ARG8_T,
typename ARG9_T,
typename ARG10_T,
typename ... OTHERS>
auto getArgType10(uint8_t** ptr) {
if constexpr (std::is_array<ARG10_T>::value)
return parseArgArray<ARG10_T>(ptr);
else
return parseArg<ARG10_T>(ptr);
}
template <typename ARG0_T,
typename ARG1_T,
typename ARG2_T,
typename ARG3_T,
typename ARG4_T,
typename ARG5_T,
typename ARG6_T,
typename ARG7_T,
typename ARG8_T,
typename ARG9_T,
typename ARG10_T,
typename ARG11_T,
typename ... OTHERS>
auto getArgType11(uint8_t** ptr) {
if constexpr (std::is_array<ARG11_T>::value)
return parseArgArray<ARG11_T>(ptr);
else
return parseArg<ARG11_T>(ptr);
}
template <typename ARG0_T,
typename ARG1_T,
typename ARG2_T,
typename ARG3_T,
typename ARG4_T,
typename ARG5_T,
typename ARG6_T,
typename ARG7_T,
typename ARG8_T,
typename ARG9_T,
typename ARG10_T,
typename ARG11_T,
typename ARG12_T,
typename ... OTHERS>
auto getArgType12(uint8_t** ptr) {
if constexpr (std::is_array<ARG12_T>::value)
return parseArgArray<ARG12_T>(ptr);
else
return parseArg<ARG12_T>(ptr);
}
template <typename ARG0_T,
typename ARG1_T,
typename ARG2_T,
typename ARG3_T,
typename ARG4_T,
typename ARG5_T,
typename ARG6_T,
typename ARG7_T,
typename ARG8_T,
typename ARG9_T,
typename ARG10_T,
typename ARG11_T,
typename ARG12_T,
typename ARG13_T,
typename ... OTHERS>
auto getArgType13(uint8_t** ptr) {
if constexpr (std::is_array<ARG13_T>::value)
return parseArgArray<ARG13_T>(ptr);
else
return parseArg<ARG13_T>(ptr);
}
template <typename ARG0_T,
typename ARG1_T,
typename ARG2_T,
typename ARG3_T,
typename ARG4_T,
typename ARG5_T,
typename ARG6_T,
typename ARG7_T,
typename ARG8_T,
typename ARG9_T,
typename ARG10_T,
typename ARG11_T,
typename ARG12_T,
typename ARG13_T,
typename ARG14_T,
typename ... OTHERS>
auto getArgType14(uint8_t** ptr) {
if constexpr (std::is_array<ARG14_T>::value)
return parseArgArray<ARG14_T>(ptr);
else
return parseArg<ARG14_T>(ptr);
}
template <typename ARG0_T,
typename ARG1_T,
typename ARG2_T,
typename ARG3_T,
typename ARG4_T,
typename ARG5_T,
typename ARG6_T,
typename ARG7_T,
typename ARG8_T,
typename ARG9_T,
typename ARG10_T,
typename ARG11_T,
typename ARG12_T,
typename ARG13_T,
typename ARG14_T,
typename ARG15_T,
typename ... OTHERS>
auto getArgType15(uint8_t** ptr) {
if constexpr (std::is_array<ARG15_T>::value)
return parseArgArray<ARG15_T>(ptr);
else
return parseArg<ARG15_T>(ptr);
}
} // utils
template <class T>
constexpr bool isVarPointer(T&& val) {
return std::is_pointer<T>::value;
}
void dstcCallbackHandlerRoutine(dstc_callback_t callback_ref,
rmc_node_id_t node_id,
uint8_t *name,
uint8_t* payload,
uint16_t payload_len);
/*!
* Callback function for a DSTC callback activation
* /tparam Types Argument types in the callback return
*/
template<class ... Types>
class CallbackFunction : public CallbackFunctionBase{
public:
// This function can be automatically generated using the gen_callbackfunction_execute.py
// script if number of parameters needs to be expanded
void execute(uint8_t* payload, uint16_t payload_len) override {
if constexpr (sizeof...(Types) == 0) {
return _function();
}
else if constexpr (sizeof...(Types) == 1) {
auto arg0 = utils::getArgType0<Types...>(&payload);
_function(arg0);
if constexpr (isVarPointer(arg0)) delete[] arg0;
}
else if constexpr (sizeof...(Types) == 2) {
auto arg0 = utils::getArgType0<Types...>(&payload);
auto arg1 = utils::getArgType1<Types...>(&payload);
_function(arg0, arg1);
if constexpr (isVarPointer(arg0)) delete[] arg0;
if constexpr (isVarPointer(arg1)) delete[] arg1;
}
else if constexpr (sizeof...(Types) == 3) {
auto arg0 = utils::getArgType0<Types...>(&payload);
auto arg1 = utils::getArgType1<Types...>(&payload);
auto arg2 = utils::getArgType2<Types...>(&payload);
_function(arg0, arg1, arg2);
if constexpr (isVarPointer(arg0)) delete[] arg0;
if constexpr (isVarPointer(arg1)) delete[] arg1;
if constexpr (isVarPointer(arg2)) delete[] arg2;
}
else if constexpr (sizeof...(Types) == 4) {
auto arg0 = utils::getArgType0<Types...>(&payload);
auto arg1 = utils::getArgType1<Types...>(&payload);
auto arg2 = utils::getArgType2<Types...>(&payload);
auto arg3 = utils::getArgType3<Types...>(&payload);
_function(arg0, arg1, arg2, arg3);
if constexpr (isVarPointer(arg0)) delete[] arg0;
if constexpr (isVarPointer(arg1)) delete[] arg1;
if constexpr (isVarPointer(arg2)) delete[] arg2;
if constexpr (isVarPointer(arg3)) delete[] arg3;
}
else if constexpr (sizeof...(Types) == 5) {
auto arg0 = utils::getArgType0<Types...>(&payload);
auto arg1 = utils::getArgType1<Types...>(&payload);
auto arg2 = utils::getArgType2<Types...>(&payload);
auto arg3 = utils::getArgType3<Types...>(&payload);
auto arg4 = utils::getArgType4<Types...>(&payload);
_function(arg0, arg1, arg2, arg3, arg4);
if constexpr (isVarPointer(arg0)) delete[] arg0;
if constexpr (isVarPointer(arg1)) delete[] arg1;
if constexpr (isVarPointer(arg2)) delete[] arg2;
if constexpr (isVarPointer(arg3)) delete[] arg3;
if constexpr (isVarPointer(arg4)) delete[] arg4;
}
else if constexpr (sizeof...(Types) == 6) {
auto arg0 = utils::getArgType0<Types...>(&payload);
auto arg1 = utils::getArgType1<Types...>(&payload);
auto arg2 = utils::getArgType2<Types...>(&payload);
auto arg3 = utils::getArgType3<Types...>(&payload);
auto arg4 = utils::getArgType4<Types...>(&payload);
auto arg5 = utils::getArgType5<Types...>(&payload);
_function(arg0, arg1, arg2, arg3, arg4, arg5);
if constexpr (isVarPointer(arg0)) delete[] arg0;
if constexpr (isVarPointer(arg1)) delete[] arg1;
if constexpr (isVarPointer(arg2)) delete[] arg2;
if constexpr (isVarPointer(arg3)) delete[] arg3;
if constexpr (isVarPointer(arg4)) delete[] arg4;
if constexpr (isVarPointer(arg5)) delete[] arg5;
}
else if constexpr (sizeof...(Types) == 7) {
auto arg0 = utils::getArgType0<Types...>(&payload);
auto arg1 = utils::getArgType1<Types...>(&payload);
auto arg2 = utils::getArgType2<Types...>(&payload);
auto arg3 = utils::getArgType3<Types...>(&payload);
auto arg4 = utils::getArgType4<Types...>(&payload);
auto arg5 = utils::getArgType5<Types...>(&payload);
auto arg6 = utils::getArgType6<Types...>(&payload);
_function(arg0, arg1, arg2, arg3, arg4, arg5, arg6);
if constexpr (isVarPointer(arg0)) delete[] arg0;
if constexpr (isVarPointer(arg1)) delete[] arg1;
if constexpr (isVarPointer(arg2)) delete[] arg2;
if constexpr (isVarPointer(arg3)) delete[] arg3;
if constexpr (isVarPointer(arg4)) delete[] arg4;
if constexpr (isVarPointer(arg5)) delete[] arg5;
if constexpr (isVarPointer(arg6)) delete[] arg6;
}
else if constexpr (sizeof...(Types) == 8) {
auto arg0 = utils::getArgType0<Types...>(&payload);
auto arg1 = utils::getArgType1<Types...>(&payload);
auto arg2 = utils::getArgType2<Types...>(&payload);
auto arg3 = utils::getArgType3<Types...>(&payload);
auto arg4 = utils::getArgType4<Types...>(&payload);
auto arg5 = utils::getArgType5<Types...>(&payload);
auto arg6 = utils::getArgType6<Types...>(&payload);
auto arg7 = utils::getArgType7<Types...>(&payload);
_function(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
if constexpr (isVarPointer(arg0)) delete[] arg0;
if constexpr (isVarPointer(arg1)) delete[] arg1;
if constexpr (isVarPointer(arg2)) delete[] arg2;
if constexpr (isVarPointer(arg3)) delete[] arg3;
if constexpr (isVarPointer(arg4)) delete[] arg4;
if constexpr (isVarPointer(arg5)) delete[] arg5;
if constexpr (isVarPointer(arg6)) delete[] arg6;
if constexpr (isVarPointer(arg7)) delete[] arg7;
}
else if constexpr (sizeof...(Types) == 9) {
auto arg0 = utils::getArgType0<Types...>(&payload);
auto arg1 = utils::getArgType1<Types...>(&payload);
auto arg2 = utils::getArgType2<Types...>(&payload);
auto arg3 = utils::getArgType3<Types...>(&payload);
auto arg4 = utils::getArgType4<Types...>(&payload);
auto arg5 = utils::getArgType5<Types...>(&payload);
auto arg6 = utils::getArgType6<Types...>(&payload);
auto arg7 = utils::getArgType7<Types...>(&payload);
auto arg8 = utils::getArgType8<Types...>(&payload);
_function(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
if constexpr (isVarPointer(arg0)) delete[] arg0;
if constexpr (isVarPointer(arg1)) delete[] arg1;
if constexpr (isVarPointer(arg2)) delete[] arg2;
if constexpr (isVarPointer(arg3)) delete[] arg3;
if constexpr (isVarPointer(arg4)) delete[] arg4;
if constexpr (isVarPointer(arg5)) delete[] arg5;
if constexpr (isVarPointer(arg6)) delete[] arg6;
if constexpr (isVarPointer(arg7)) delete[] arg7;
if constexpr (isVarPointer(arg8)) delete[] arg8;
}
else if constexpr (sizeof...(Types) == 10) {
auto arg0 = utils::getArgType0<Types...>(&payload);
auto arg1 = utils::getArgType1<Types...>(&payload);
auto arg2 = utils::getArgType2<Types...>(&payload);
auto arg3 = utils::getArgType3<Types...>(&payload);
auto arg4 = utils::getArgType4<Types...>(&payload);
auto arg5 = utils::getArgType5<Types...>(&payload);
auto arg6 = utils::getArgType6<Types...>(&payload);
auto arg7 = utils::getArgType7<Types...>(&payload);
auto arg8 = utils::getArgType8<Types...>(&payload);
auto arg9 = utils::getArgType9<Types...>(&payload);
_function(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9);
if constexpr (isVarPointer(arg0)) delete[] arg0;
if constexpr (isVarPointer(arg1)) delete[] arg1;
if constexpr (isVarPointer(arg2)) delete[] arg2;
if constexpr (isVarPointer(arg3)) delete[] arg3;
if constexpr (isVarPointer(arg4)) delete[] arg4;
if constexpr (isVarPointer(arg5)) delete[] arg5;
if constexpr (isVarPointer(arg6)) delete[] arg6;
if constexpr (isVarPointer(arg7)) delete[] arg7;
if constexpr (isVarPointer(arg8)) delete[] arg8;
if constexpr (isVarPointer(arg9)) delete[] arg9;
}
else if constexpr (sizeof...(Types) == 11) {
auto arg0 = utils::getArgType0<Types...>(&payload);
auto arg1 = utils::getArgType1<Types...>(&payload);
auto arg2 = utils::getArgType2<Types...>(&payload);
auto arg3 = utils::getArgType3<Types...>(&payload);
auto arg4 = utils::getArgType4<Types...>(&payload);
auto arg5 = utils::getArgType5<Types...>(&payload);
auto arg6 = utils::getArgType6<Types...>(&payload);
auto arg7 = utils::getArgType7<Types...>(&payload);
auto arg8 = utils::getArgType8<Types...>(&payload);
auto arg9 = utils::getArgType9<Types...>(&payload);
auto arg10 = utils::getArgType10<Types...>(&payload);
_function(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10);
if constexpr (isVarPointer(arg0)) delete[] arg0;
if constexpr (isVarPointer(arg1)) delete[] arg1;
if constexpr (isVarPointer(arg2)) delete[] arg2;
if constexpr (isVarPointer(arg3)) delete[] arg3;
if constexpr (isVarPointer(arg4)) delete[] arg4;
if constexpr (isVarPointer(arg5)) delete[] arg5;
if constexpr (isVarPointer(arg6)) delete[] arg6;
if constexpr (isVarPointer(arg7)) delete[] arg7;
if constexpr (isVarPointer(arg8)) delete[] arg8;
if constexpr (isVarPointer(arg9)) delete[] arg9;
if constexpr (isVarPointer(arg10)) delete[] arg10;
}
else if constexpr (sizeof...(Types) == 12) {
auto arg0 = utils::getArgType0<Types...>(&payload);
auto arg1 = utils::getArgType1<Types...>(&payload);
auto arg2 = utils::getArgType2<Types...>(&payload);
auto arg3 = utils::getArgType3<Types...>(&payload);
auto arg4 = utils::getArgType4<Types...>(&payload);
auto arg5 = utils::getArgType5<Types...>(&payload);
auto arg6 = utils::getArgType6<Types...>(&payload);
auto arg7 = utils::getArgType7<Types...>(&payload);
auto arg8 = utils::getArgType8<Types...>(&payload);
auto arg9 = utils::getArgType9<Types...>(&payload);
auto arg10 = utils::getArgType10<Types...>(&payload);
auto arg11 = utils::getArgType11<Types...>(&payload);
_function(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11);
if constexpr (isVarPointer(arg0)) delete[] arg0;
if constexpr (isVarPointer(arg1)) delete[] arg1;
if constexpr (isVarPointer(arg2)) delete[] arg2;
if constexpr (isVarPointer(arg3)) delete[] arg3;
if constexpr (isVarPointer(arg4)) delete[] arg4;
if constexpr (isVarPointer(arg5)) delete[] arg5;
if constexpr (isVarPointer(arg6)) delete[] arg6;
if constexpr (isVarPointer(arg7)) delete[] arg7;
if constexpr (isVarPointer(arg8)) delete[] arg8;
if constexpr (isVarPointer(arg9)) delete[] arg9;
if constexpr (isVarPointer(arg10)) delete[] arg10;
if constexpr (isVarPointer(arg11)) delete[] arg11;
}
else if constexpr (sizeof...(Types) == 13) {
auto arg0 = utils::getArgType0<Types...>(&payload);
auto arg1 = utils::getArgType1<Types...>(&payload);
auto arg2 = utils::getArgType2<Types...>(&payload);
auto arg3 = utils::getArgType3<Types...>(&payload);
auto arg4 = utils::getArgType4<Types...>(&payload);
auto arg5 = utils::getArgType5<Types...>(&payload);
auto arg6 = utils::getArgType6<Types...>(&payload);
auto arg7 = utils::getArgType7<Types...>(&payload);
auto arg8 = utils::getArgType8<Types...>(&payload);
auto arg9 = utils::getArgType9<Types...>(&payload);
auto arg10 = utils::getArgType10<Types...>(&payload);
auto arg11 = utils::getArgType11<Types...>(&payload);
auto arg12 = utils::getArgType12<Types...>(&payload);
_function(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12);
if constexpr (isVarPointer(arg0)) delete[] arg0;
if constexpr (isVarPointer(arg1)) delete[] arg1;
if constexpr (isVarPointer(arg2)) delete[] arg2;
if constexpr (isVarPointer(arg3)) delete[] arg3;
if constexpr (isVarPointer(arg4)) delete[] arg4;
if constexpr (isVarPointer(arg5)) delete[] arg5;
if constexpr (isVarPointer(arg6)) delete[] arg6;
if constexpr (isVarPointer(arg7)) delete[] arg7;
if constexpr (isVarPointer(arg8)) delete[] arg8;
if constexpr (isVarPointer(arg9)) delete[] arg9;
if constexpr (isVarPointer(arg10)) delete[] arg10;
if constexpr (isVarPointer(arg11)) delete[] arg11;
if constexpr (isVarPointer(arg12)) delete[] arg12;
}
else if constexpr (sizeof...(Types) == 14) {
auto arg0 = utils::getArgType0<Types...>(&payload);
auto arg1 = utils::getArgType1<Types...>(&payload);
auto arg2 = utils::getArgType2<Types...>(&payload);
auto arg3 = utils::getArgType3<Types...>(&payload);
auto arg4 = utils::getArgType4<Types...>(&payload);
auto arg5 = utils::getArgType5<Types...>(&payload);
auto arg6 = utils::getArgType6<Types...>(&payload);
auto arg7 = utils::getArgType7<Types...>(&payload);
auto arg8 = utils::getArgType8<Types...>(&payload);
auto arg9 = utils::getArgType9<Types...>(&payload);
auto arg10 = utils::getArgType10<Types...>(&payload);
auto arg11 = utils::getArgType11<Types...>(&payload);
auto arg12 = utils::getArgType12<Types...>(&payload);
auto arg13 = utils::getArgType13<Types...>(&payload);
_function(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13);
if constexpr (isVarPointer(arg0)) delete[] arg0;
if constexpr (isVarPointer(arg1)) delete[] arg1;
if constexpr (isVarPointer(arg2)) delete[] arg2;
if constexpr (isVarPointer(arg3)) delete[] arg3;
if constexpr (isVarPointer(arg4)) delete[] arg4;
if constexpr (isVarPointer(arg5)) delete[] arg5;
if constexpr (isVarPointer(arg6)) delete[] arg6;
if constexpr (isVarPointer(arg7)) delete[] arg7;
if constexpr (isVarPointer(arg8)) delete[] arg8;
if constexpr (isVarPointer(arg9)) delete[] arg9;
if constexpr (isVarPointer(arg10)) delete[] arg10;
if constexpr (isVarPointer(arg11)) delete[] arg11;
if constexpr (isVarPointer(arg12)) delete[] arg12;
if constexpr (isVarPointer(arg13)) delete[] arg13;
}
else if constexpr (sizeof...(Types) == 15) {
auto arg0 = utils::getArgType0<Types...>(&payload);
auto arg1 = utils::getArgType1<Types...>(&payload);
auto arg2 = utils::getArgType2<Types...>(&payload);
auto arg3 = utils::getArgType3<Types...>(&payload);
auto arg4 = utils::getArgType4<Types...>(&payload);
auto arg5 = utils::getArgType5<Types...>(&payload);
auto arg6 = utils::getArgType6<Types...>(&payload);
auto arg7 = utils::getArgType7<Types...>(&payload);
auto arg8 = utils::getArgType8<Types...>(&payload);
auto arg9 = utils::getArgType9<Types...>(&payload);
auto arg10 = utils::getArgType10<Types...>(&payload);
auto arg11 = utils::getArgType11<Types...>(&payload);
auto arg12 = utils::getArgType12<Types...>(&payload);
auto arg13 = utils::getArgType13<Types...>(&payload);
auto arg14 = utils::getArgType14<Types...>(&payload);
_function(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14);
if constexpr (isVarPointer(arg0)) delete[] arg0;
if constexpr (isVarPointer(arg1)) delete[] arg1;
if constexpr (isVarPointer(arg2)) delete[] arg2;
if constexpr (isVarPointer(arg3)) delete[] arg3;
if constexpr (isVarPointer(arg4)) delete[] arg4;
if constexpr (isVarPointer(arg5)) delete[] arg5;
if constexpr (isVarPointer(arg6)) delete[] arg6;
if constexpr (isVarPointer(arg7)) delete[] arg7;
if constexpr (isVarPointer(arg8)) delete[] arg8;
if constexpr (isVarPointer(arg9)) delete[] arg9;
if constexpr (isVarPointer(arg10)) delete[] arg10;
if constexpr (isVarPointer(arg11)) delete[] arg11;
if constexpr (isVarPointer(arg12)) delete[] arg12;
if constexpr (isVarPointer(arg13)) delete[] arg13;
if constexpr (isVarPointer(arg14)) delete[] arg14;
}
else if constexpr (sizeof...(Types) == 16) {
auto arg0 = utils::getArgType0<Types...>(&payload);
auto arg1 = utils::getArgType1<Types...>(&payload);
auto arg2 = utils::getArgType2<Types...>(&payload);
auto arg3 = utils::getArgType3<Types...>(&payload);
auto arg4 = utils::getArgType4<Types...>(&payload);
auto arg5 = utils::getArgType5<Types...>(&payload);
auto arg6 = utils::getArgType6<Types...>(&payload);
auto arg7 = utils::getArgType7<Types...>(&payload);
auto arg8 = utils::getArgType8<Types...>(&payload);
auto arg9 = utils::getArgType9<Types...>(&payload);
auto arg10 = utils::getArgType10<Types...>(&payload);
auto arg11 = utils::getArgType11<Types...>(&payload);
auto arg12 = utils::getArgType12<Types...>(&payload);
auto arg13 = utils::getArgType13<Types...>(&payload);
auto arg14 = utils::getArgType14<Types...>(&payload);
auto arg15 = utils::getArgType15<Types...>(&payload);
_function(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15);
if constexpr (isVarPointer(arg0)) delete[] arg0;
if constexpr (isVarPointer(arg1)) delete[] arg1;
if constexpr (isVarPointer(arg2)) delete[] arg2;
if constexpr (isVarPointer(arg3)) delete[] arg3;
if constexpr (isVarPointer(arg4)) delete[] arg4;
if constexpr (isVarPointer(arg5)) delete[] arg5;
if constexpr (isVarPointer(arg6)) delete[] arg6;
if constexpr (isVarPointer(arg7)) delete[] arg7;
if constexpr (isVarPointer(arg8)) delete[] arg8;
if constexpr (isVarPointer(arg9)) delete[] arg9;
if constexpr (isVarPointer(arg10)) delete[] arg10;
if constexpr (isVarPointer(arg11)) delete[] arg11;
if constexpr (isVarPointer(arg12)) delete[] arg12;
if constexpr (isVarPointer(arg13)) delete[] arg13;
if constexpr (isVarPointer(arg14)) delete[] arg14;
if constexpr (isVarPointer(arg15)) delete[] arg15;
}
static_assert (sizeof...(Types) <= 16, "Currently only up to 16 paramater for callbacks in supported, although this can be expanded");
}
/*!
* Constructs a CallbackFunction using an arbitrary function that matches the arguments list
* \param function Function (or lambda) that accepts Types... args, and returns void
*/
CallbackFunction(std::function<void(Types...)> function)
: _function(function)
{}
private:
std::function<void(Types...)> _function;
};
class CallbackHandler {
public:
CallbackHandler();
void execute(dstc_callback_t callback_ref, uint8_t* payload, uint16_t payload_len);
template<class ... Types>
dstc_callback_t registerCallback( CallbackFunction<Types...>& callback_function ) {
auto callback_shared = std::make_shared<CallbackFunction<Types...>>(callback_function);
std::lock_guard<std::mutex> lock(_callback_mtx);
auto callback_id = _getUniqueCallbackID();
_callbacks[callback_id] = callback_shared;
dstc_activate_callback(nullptr, callback_id, dstcCallbackHandlerRoutine);
return callback_id;
}
private:
dstc_callback_t _getUniqueCallbackID();
std::mutex _callback_mtx;
dstc_callback_t _next_id;
std::unordered_map<dstc_callback_t, std::shared_ptr<CallbackFunctionBase>> _callbacks;
};
}