thread_util.hpp
2.34 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* Copyright (C) 2015 Hamburg University of Applied Sciences (HAW)
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup cpp11-compat
* @{
*
* @file
* @brief utility functions
*
* @author Dominik Charousset <dominik.charousset (at) haw-hamburg.de>
* @author Raphael Hiesgen <raphael.hiesgen (at) haw-hamburg.de>
*
* @}
*/
#ifndef RIOT_THREAD_UTILS_HPP
#define RIOT_THREAD_UTILS_HPP
#include <tuple>
#include <utility>
namespace riot {
namespace detail {
/**
* A list of integers (wraps a long... template parameter pack).
*/
template <long... Is>
struct int_list {};
/**
* Creates indices for from `Pos` to `Max`.
*/
template <long Max, long Pos = 0, typename Indices = int_list<>>
struct il_indices;
template <long Pos, long... Is>
struct il_indices<Pos, Pos, int_list<Is...>> {
using type = int_list<Is...>;
};
template <long Max, long Pos, long... Is>
struct il_indices<Max, Pos, int_list<Is...>> {
using type = typename il_indices<Max, Pos + 1, int_list<Is..., Pos>>::type;
};
template <long To, long From = 0>
typename il_indices<To, From>::type get_indices() {
return {};
}
/**
* apply arguments to function
*/
template <class F, long... Is, class Tuple>
inline auto apply_args(F& f, detail::int_list<Is...>, Tuple&& tup)
-> decltype(f(std::get<Is>(tup)...)) {
return f(std::get<Is>(tup)...);
}
template <class F, class Tuple, class... Ts>
inline auto apply_args_prefixed(F& f, detail::int_list<>, Tuple&, Ts&&... args)
-> decltype(f(std::forward<Ts>(args)...)) {
return f(std::forward<Ts>(args)...);
}
template <class F, long... Is, class Tuple, class... Ts>
inline auto apply_args_prefixed(F& f, detail::int_list<Is...>, Tuple& tup,
Ts&&... args)
-> decltype(f(std::forward<Ts>(args)..., std::get<Is>(tup)...)) {
return f(std::forward<Ts>(args)..., std::get<Is>(tup)...);
}
template <class F, long... Is, class Tuple, class... Ts>
inline auto apply_args_suffxied(F& f, detail::int_list<Is...>, Tuple& tup,
Ts&&... args)
-> decltype(f(std::get<Is>(tup)..., std::forward<Ts>(args)...)) {
return f(std::get<Is>(tup)..., std::forward<Ts>(args)...);
}
} // namespace detail
} // namespace riot
#endif // RIOT_THREAD_UTILS_HPP