-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetArgsFromStream
36 lines (31 loc) · 1.18 KB
/
GetArgsFromStream
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
//from yazi-rpc. for server to get func's args from data recieved from rpc client
//https://hkrb7870j3.feishu.cn/docx/M60ddvQh7oocLmxnPeQcTlpnnUD
template<typename R, typename F, typename Tuple>
typename std::enable_if<!std::is_same<R, void>::value, R>::type
FunctionHandler::call_impl(F func, Tuple args)
{
return invoke<R>(func, args);
}
template<typename R, typename F, typename Tuple>
auto FunctionHandler::invoke(F && func, Tuple && t)
{
constexpr auto size = std::tuple_size<typename std::decay<Tuple>::type>::value;
return invoke_impl<R>(std::forward<F>(func), std::forward<Tuple>(t), std::make_index_sequence<size>{});
}
template<typename R, typename F, typename Tuple, std::size_t... Index>
auto FunctionHandler::invoke_impl(F && func, Tuple && t, std::index_sequence<Index...>)
{
return func(std::get<Index>(std::forward<Tuple>(t))...);
}
template<typename Tuple, std::size_t... I>
Tuple FunctionHandler::get_args(DataStream & ds, std::index_sequence<I...>)
{
Tuple t;
initializer_list<int>{((get_arg<Tuple, I>(ds, t)), 0)...};
return t;
}
template<typename Tuple, std::size_t Id>
void FunctionHandler::get_arg(DataStream & ds, Tuple & t)
{
ds >> std::get<Id>(t);
}