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