PhoenixSwarm  3.5.0
Library to ease communication between daemons
Loading...
Searching...
No Matches
phoenix_function_call.h
Go to the documentation of this file.
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
21template <int First, int Last>
24
29 template <typename T>
30 void operator()(PLog & log, bool & isOk, DataStreamIter & iter, T & mContainers) const{
31 if(First < Last && isOk){
32 auto & value = std::get<First>(mContainers);
33 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 static_for_deserialise_message<First+1, Last>()(log, isOk, iter, mContainers);
36 }
37 }
38};
39
41template <int N>
43
44 template <typename T>
45 void operator()(PLog & log, bool & isOk, DataStreamIter & iter, T & mContainers) const{}
46};
47
48
50
56template <typename R, typename... T>
57bool phoenix_function_call(PLog & log, Data & result, const DataStreamMsg & input, R (*func)(T...)){
58 std::tuple<T...> args = std::tuple<T...>();
59 DataStreamIter iter = (DataStreamIter)input.data();
60 bool isOk(true);
61 //We have to deserialise the parameters of the function
62 static_for_deserialise_message<0, std::tuple_size<std::tuple<T... > >{} >()(log, isOk, iter, args);
63 if(isOk){
64 //Then we can call the function
65 R res = std::apply(func, args);
66 //And get back the result, in the res message
67 result = phoenix_createData(res);
68 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
76
82template <typename... T>
83bool 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
Basic Data exchanged in the swarm.
void setType(const PString &type)
Sets the type of the Data.
Data phoenix_createData(const T &value)
Create a Data from a value.
bool phoenix_function_call(PLog &log, Data &result, const DataStreamMsg &input, R(*func)(T...))
Call the function with message.
void operator()(PLog &log, bool &isOk, DataStreamIter &iter, T &mContainers) const
Iterate over function parameters.
void operator()(PLog &log, bool &isOk, DataStreamIter &iter, T &mContainers) const
Deserialise the message in the tuple.