hmbdc
simplify-high-performance-messaging-programming
ForwardTupleToFunc.hpp
1 #include "hmbdc/Copyright.hpp"
2 #pragma once
3 
4 #include <tuple>
5 
6 namespace hmbdc {
7 
8 using namespace std;
9 
10 namespace detail {
11 template<unsigned...> struct index_tuple{};
12 template<unsigned I, typename IndexTuple, typename... Types>
14 
15 template<unsigned I, unsigned... Indexes, typename T, typename... Types>
16 struct make_indices_impl<I, index_tuple<Indexes...>, T, Types...> {
17  typedef typename
18  make_indices_impl<I + 1,
19  index_tuple<Indexes..., I>,
20  Types...>::type type;
21 };
22 
23 template<unsigned I, unsigned... Indexes>
24 struct make_indices_impl<I, index_tuple<Indexes...> > {
25  typedef index_tuple<Indexes...> type;
26 };
27 
28 template<typename... Types>
29 struct make_indices
30  : make_indices_impl<0, index_tuple<>, Types...>
31 {};
32 
33 template <unsigned... Indexes, class... Args, class Ret>
34 Ret forward_impl(index_tuple<Indexes...>,
35  tuple<Args...> tuple,
36  Ret (*func) (Args...)) {
37  return func(forward<Args>(std::get<Indexes>(tuple))...);
38 }
39 
40 } //detail
41 
42 /**
43  * @brief perfect forward a tuple into a function
44  * @details the tuple value types need to match the func signature
45  *
46  * @param tuple tuple containing args for the function
47  * @param func func accepting the args to execute
48  * @tparam Args arg types
49  * @return return of the executiong
50  */
51 template<class... Args, class Ret>
52 Ret forward_tuple_to_func(std::tuple<Args...>& tuple, Ret (*func) (Args...)) {
53  typedef typename detail::make_indices<Args...>::type Indexes;
54  return detail::forward_impl(Indexes(), tuple, func);
55 }
56 
57 
58 /**
59  * @brief perfect forward a tuple into a function
60  * @details the tuple value types need to match the func signature
61  *
62  * @param tuple r reference tuple containing args for the function
63  * @param func func accepting the args to execute
64  * @tparam Args arg types
65  * @return return of the executiong
66  */
67 template<class... Args, class Ret>
68 Ret forward_tuple_to_func(std::tuple<Args...>&& tuple, Ret (*func) (Args...)) {
69  typedef typename detail::make_indices<Args...>::type Indexes;
70  return detail::forward_impl(Indexes(), move(tuple), func);
71 }
72 
73 }
Definition: ForwardTupleToFunc.hpp:13
Definition: TypedString.hpp:74
Definition: ForwardTupleToFunc.hpp:11
Definition: Client.hpp:11
Definition: ForwardTupleToFunc.hpp:29