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