PhoenixSwarm
5.2.0
Library to ease communication between daemons
Toggle main menu visibility
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
21
template
<
int
First,
int
Last>
22
struct
static_for_deserialise_message
{
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
41
template
<
int
N>
42
struct
static_for_deserialise_message
<N, N>{
43
44
template
<
typename
T>
45
void
operator()
(PLog & log,
bool
& isOk, DataStreamIter & iter, T & mContainers)
const
{}
46
};
47
48
50
56
template
<
typename
R,
typename
... T>
57
bool
phoenix_function_call
(PLog & log,
Swarm::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
82
template
<
typename
... T>
83
bool
phoenix_function_call
(PLog & log,
Swarm::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
Swarm::Data
Basic Data exchanged in the swarm.
Definition
Representation.h:22
Swarm::Data::setType
void setType(const PString &type)
Sets the type of the Data.
Definition
Representation.cpp:80
phoenix_createData.h
phoenix_createData
Swarm::Data phoenix_createData(const T &value)
Create a Data from a value.
Definition
phoenix_createData_impl.h:20
phoenix_function_call
bool phoenix_function_call(PLog &log, Swarm::Data &result, const DataStreamMsg &input, R(*func)(T...))
Call the function with message.
Definition
phoenix_function_call.h:57
static_for_deserialise_message< N, N >::operator()
void operator()(PLog &log, bool &isOk, DataStreamIter &iter, T &mContainers) const
Definition
phoenix_function_call.h:45
static_for_deserialise_message
Iterate over function parameters.
Definition
phoenix_function_call.h:22
static_for_deserialise_message::operator()
void operator()(PLog &log, bool &isOk, DataStreamIter &iter, T &mContainers) const
Deserialise the message in the tuple.
Definition
phoenix_function_call.h:30
src
Function
phoenix_function_call.h
Generated by
1.17.0