Directory: | ./ |
---|---|
File: | src/Function/phoenix_function_call.h |
Date: | 2025-03-14 12:18:05 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 16 | 29 | 55.2% |
Branches: | 6 | 38 | 15.8% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /*************************************** | ||
2 | Auteur : Pierre Aubert | ||
3 | Mail : aubertp7@gmail.com | ||
4 | Licence : CeCILL-C | ||
5 | ****************************************/ | ||
6 | |||
7 | #ifndef __PHOENIX_FUNCTION_CALL_H__ | ||
8 | #define __PHOENIX_FUNCTION_CALL_H__ | ||
9 | |||
10 | #include <tuple> | ||
11 | #include <vector> | ||
12 | #include <utility> | ||
13 | #include <iostream> | ||
14 | |||
15 | #include "data_all.h" | ||
16 | #include "PLog.h" | ||
17 | |||
18 | #include "phoenix_createData.h" | ||
19 | |||
20 | ///@brief Iterate over function parameters | ||
21 | template <int First, int Last> | ||
22 | struct static_for_deserialise_message{ | ||
23 | ///Deserialise the message in the tuple | ||
24 | /** @param[out] log : logger | ||
25 | * @param[out] isOk : true if the deserialisation is OK | ||
26 | * @param[out] iter : iterator of the message | ||
27 | * @param[out] mContainers : tuple to be initialised with the message | ||
28 | */ | ||
29 | template <typename T> | ||
30 | 2 | void operator()(PLog & log, bool & isOk, DataStreamIter & iter, T & mContainers) const{ | |
31 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if(First < Last && isOk){ |
32 | 2 | auto & value = std::get<First>(mContainers); | |
33 | 2 | isOk &= data_message_load(iter, value); | |
34 | // std::cout << "\tstatic_for_deserialise_message : args("<<First<<") : "<<phoenix_network_get_type_name(value)<<" " << value << std::endl; | ||
35 | 2 | static_for_deserialise_message<First+1, Last>()(log, isOk, iter, mContainers); | |
36 | } | ||
37 | 2 | } | |
38 | }; | ||
39 | |||
40 | ///@brief Iterate over function parameters (end) | ||
41 | template <int N> | ||
42 | struct static_for_deserialise_message<N, N>{ | ||
43 | |||
44 | template <typename T> | ||
45 | 2 | void operator()(PLog & log, bool & isOk, DataStreamIter & iter, T & mContainers) const{} | |
46 | }; | ||
47 | |||
48 | |||
49 | ///Call the function with message | ||
50 | /** @param[out] log : logger | ||
51 | * @param[out] result : result of the function call | ||
52 | * @param input : input parameters of the function | ||
53 | * @param func : function to be called | ||
54 | * @return true on success, false otherwise | ||
55 | */ | ||
56 | template <typename R, typename... T> | ||
57 | 2 | bool phoenix_function_call(PLog & log, Data & result, const DataStreamMsg & input, R (*func)(T...)){ | |
58 | 2 | std::tuple<T...> args = std::tuple<T...>(); | |
59 | 2 | DataStreamIter iter = (DataStreamIter)input.data(); | |
60 | 2 | bool isOk(true); | |
61 | //We have to deserialise the parameters of the function | ||
62 |
1/1✓ Branch 1 taken 2 times.
|
2 | static_for_deserialise_message<0, std::tuple_size<std::tuple<T... > >{} >()(log, isOk, iter, args); |
63 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if(isOk){ |
64 | //Then we can call the function | ||
65 |
1/1✓ Branch 1 taken 2 times.
|
2 | R res = std::apply(func, args); |
66 | //And get back the result, in the res message | ||
67 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
|
2 | result = phoenix_createData(res); |
68 | 2 | return isOk; | |
69 | }else{ | ||
70 | ✗ | log.getLogError() << "phoenix_function_call : cannot deserialise arguments to function of " << input.size() << " byte(s)" << std::endl; | |
71 | ✗ | return false; | |
72 | } | ||
73 | } | ||
74 | |||
75 | ///Call the function with message | ||
76 | /** @param[out] log : logger | ||
77 | * @param[out] result : result of the function call | ||
78 | * @param input : input parameters of the function | ||
79 | * @param func : function to be called | ||
80 | * @return true on success, false otherwise | ||
81 | */ | ||
82 | template <typename... T> | ||
83 | ✗ | bool phoenix_function_call(PLog & log, Data & result, const DataStreamMsg & input, void (*func)(T...)){ | |
84 | ✗ | std::tuple<T...> args = std::tuple<T...>(); | |
85 | ✗ | DataStreamIter iter = (DataStreamIter)input.data(); | |
86 | ✗ | bool isOk(true); | |
87 | //We have to deserialise the parameters of the function | ||
88 | ✗ | static_for_deserialise_message<0, std::tuple_size<std::tuple<T... > >{} >()(log, isOk, iter, args); | |
89 | ✗ | if(isOk){ | |
90 | //Then we can call the function | ||
91 | ✗ | std::apply(func, args); | |
92 | //And get back the result, in the res message | ||
93 | ✗ | result.setType("void"); | |
94 | ✗ | return isOk; | |
95 | }else{ | ||
96 | ✗ | log.getLogError() << "phoenix_function_call : cannot deserialise arguments to function of " << input.size() << " byte(s)" << std::endl; | |
97 | ✗ | return false; | |
98 | } | ||
99 | } | ||
100 | |||
101 | #endif | ||
102 | |||
103 |