Blame view

RIOT/sys/cpp11-compat/include/riot/detail/thread_util.hpp 2.96 KB
a752c7ab   elopes   add first test an...
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  /*
   * 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 {
  
  /**
   * @brief A list of integers (wraps a long... template parameter pack).
   */
  template <long... Is>
  struct int_list {};
  
  /**
   * @brief Creates indices from `Pos` to `Max`.
   */
  template <long Max, long Pos = 0, typename Indices = int_list<>>
  struct il_indices;
  
  /**
   * @brief End of recursion, `Pos` reached `Max`.
   */
  template <long Pos, long... Is>
  struct il_indices<Pos, Pos, int_list<Is...>> {
    /**
     * @brief Result is the list containing `Is...`.
     */
    using type = int_list<Is...>;
  };
  
  /**
   * @brief Recursion step.
   */
  template <long Max, long Pos, long... Is>
  struct il_indices<Max, Pos, int_list<Is...>> {
    /**
     * @brief Append `Pos` to list and increment for the next step.
     */
    using type = typename il_indices<Max, Pos + 1, int_list<Is..., Pos>>::type;
  };
  
  /**
   * @brief Function to create a list of indices from `From` to `To`.
   */
  template <long To, long From = 0>
  typename il_indices<To, From>::type get_indices() {
    return {};
  }
  
  /**
   * @brief Apply arguments in a tuple 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)...);
  }
  
  /**
   * @brief Prefix the argument tuple with additonal arguments.
   *        In this case the tuple is empty.
   */
  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)...);
  }
  
  /**
   * @brief Prefix the argument tuple with additonal arguments.
   *        In this case the tuple is contains arguments.
   */
  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)...);
  }
  
  /**
   * @brief Suffix the tuple with additonal arguments.
   */
  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