thread_util.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Hamburg University of Applied Sciences (HAW)
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
22 #ifndef RIOT_THREAD_UTILS_HPP
23 #define RIOT_THREAD_UTILS_HPP
24 
25 #include <tuple>
26 #include <utility>
27 
28 namespace riot {
29 namespace detail {
30 
34 template <long... Is>
35 struct int_list {};
36 
40 template <long Max, long Pos = 0, typename Indices = int_list<>>
41 struct il_indices;
42 
46 template <long Pos, long... Is>
47 struct il_indices<Pos, Pos, int_list<Is...>> {
51  using type = int_list<Is...>;
52 };
53 
57 template <long Max, long Pos, long... Is>
58 struct il_indices<Max, Pos, int_list<Is...>> {
62  using type = typename il_indices<Max, Pos + 1, int_list<Is..., Pos>>::type;
63 };
64 
68 template <long To, long From = 0>
70  return {};
71 }
72 
76 template <class F, long... Is, class Tuple>
77 inline auto apply_args(F& f, detail::int_list<Is...>, Tuple&& tup)
78  -> decltype(f(std::get<Is>(tup)...)) {
79  return f(std::get<Is>(tup)...);
80 }
81 
86 template <class F, class Tuple, class... Ts>
87 inline auto apply_args_prefixed(F& f, detail::int_list<>, Tuple&, Ts&&... args)
88  -> decltype(f(std::forward<Ts>(args)...)) {
89  return f(std::forward<Ts>(args)...);
90 }
91 
96 template <class F, long... Is, class Tuple, class... Ts>
97 inline auto apply_args_prefixed(F& f, detail::int_list<Is...>, Tuple& tup,
98  Ts&&... args)
99  -> decltype(f(std::forward<Ts>(args)..., std::get<Is>(tup)...)) {
100  return f(std::forward<Ts>(args)..., std::get<Is>(tup)...);
101 }
102 
106 template <class F, long... Is, class Tuple, class... Ts>
107 inline auto apply_args_suffxied(F& f, detail::int_list<Is...>, Tuple& tup,
108  Ts&&... args)
109  -> decltype(f(std::get<Is>(tup)..., std::forward<Ts>(args)...)) {
110  return f(std::get<Is>(tup)..., std::forward<Ts>(args)...);
111 }
112 
113 } // namespace detail
114 } // namespace riot
115 
116 #endif // RIOT_THREAD_UTILS_HPP
riot::detail::il_indices
Creates indices from Pos to Max.
Definition: thread_util.hpp:41
riot::detail::apply_args_suffxied
auto apply_args_suffxied(F &f, detail::int_list< Is... >, Tuple &tup, Ts &&... args) -> decltype(f(std::get< Is >(tup)..., std::forward< Ts >(args)...))
Suffix the tuple with additional arguments.
Definition: thread_util.hpp:107
riot::detail::get_indices
il_indices< To, From >::type get_indices()
Function to create a list of indices from From to To.
Definition: thread_util.hpp:69
riot::detail::apply_args_prefixed
auto apply_args_prefixed(F &f, detail::int_list<>, Tuple &, Ts &&... args) -> decltype(f(std::forward< Ts >(args)...))
Prefix the argument tuple with additional arguments.
Definition: thread_util.hpp:87
riot::detail::il_indices< Max, Pos, int_list< Is... > >::type
typename il_indices< Max, Pos+1, int_list< Is..., Pos > >::type type
Append Pos to list and increment for the next step.
Definition: thread_util.hpp:62
riot::detail::apply_args
auto apply_args(F &f, detail::int_list< Is... >, Tuple &&tup) -> decltype(f(std::get< Is >(tup)...))
Apply arguments in a tuple to function.
Definition: thread_util.hpp:77
riot::detail::int_list
A list of integers (wraps a long...
Definition: thread_util.hpp:35