Tatooine
template_helper.h
Go to the documentation of this file.
1#ifndef TATOOINE_TEMPLATE_HELPER_H
2#define TATOOINE_TEMPLATE_HELPER_H
3//==============================================================================
4#include <type_traits>
5//==============================================================================
7//==============================================================================
8template <typename T, typename... Ts>
9struct front_t {
10 using type = T;
11};
12template <typename... Ts>
13using front = typename front_t<Ts...>::type;
14
15
16
17//==============================================================================
18
19template <typename... Ts>
20struct back_t;
21
22template <typename T>
23struct back_t<T> {
24 using type = T;
25};
26
27template <typename T0, typename T1, typename... Ts>
28struct back_t<T0, T1, Ts...> {
29 using type = typename back_t<T1, Ts...>::type;
30};
31
32template <typename... Ts>
33using back = typename back_t<Ts...>::type;
34
35//==============================================================================
36template <std::size_t i, typename... Ts>
37struct get_t;
38
39template <typename T, typename... Ts>
40struct get_t<0, T, Ts...> {
41 using type = T;
42};
43
44template <std::size_t i, typename T, typename... Ts>
45struct get_t<i, T, Ts...> {
46 using type = typename get_t<i - 1, Ts...>::type;
47};
48
49template <std::size_t i, typename... Ts>
50using get = typename get_t<i, Ts...>::type;
51
52//==============================================================================
53template <std::size_t i, typename T, T... Vs>
55
56template <typename T, T V0, T... Vs>
57struct getval_impl<0, T, V0, Vs...> {
58 static constexpr T value = V0;
59};
60
61template <std::size_t i, typename T, T V0, T... Vs>
62struct getval_impl<i, T,V0, Vs...> {
63 static constexpr T value = getval_impl<i - 1, T, Vs...>::value;
64};
65
66template <std::size_t i, typename T, T... Vs>
67static constexpr auto getval_v = getval_impl<i, T, Vs...>::value;
68
69template <typename T>
70constexpr auto getval(const unsigned int /*i*/) -> T {
71 throw T{};
72}
73template <typename T, typename T0, typename... Ts>
74constexpr auto getval(const unsigned int i, T0&& t0, Ts&&... ts) {
75 if (i == 0) { return static_cast<T>(t0); }
76 return getval<T>(i - 1, std::forward<Ts>(ts)...);
77}
78//==============================================================================
79
80template <typename... Ts>
81struct flipped {};
82
83template <typename... Ts>
84struct flip_t;
85
86template <typename T0, typename T1, typename... Ts>
87struct flip_t<T0, T1, Ts...> {
88 template <typename... Flipped_Ts>
89 static constexpr auto flip(flipped<Flipped_Ts...>&&) {
90 return flipped<Flipped_Ts..., T0>{};
91 }
92 static constexpr auto flip() { return flip(flip_t<T1, Ts...>::flip()); }
93};
94
95template <typename T>
96struct flip_t<T> {
97 static constexpr auto flip() { return flipped<T>{}; }
98};
99
100template <typename... Ts>
101inline auto flip() {
102 return flip_t<Ts...>::flip();
103}
104//==============================================================================
105template <typename... Ts>
107
108template <typename T>
109struct all_same_t<T> {
110 static constexpr bool value = true;
111};
112
113template <typename T0, typename T1, typename... Ts>
114struct all_same_t<T0, T1, Ts...> {
115 static constexpr bool value =
116 std::is_same<T0, T1>::value && all_same_t<T1, Ts...>::value;
117};
118
119template <typename... Ts>
120static constexpr bool all_same = all_same_t<Ts...>::value;
121//==============================================================================
122} // namespace tatooine::template_helper
123//==============================================================================
124#endif
Definition: template_helper.h:6
typename back_t< Ts... >::type back
Definition: template_helper.h:33
auto flip()
Definition: template_helper.h:101
static constexpr auto getval_v
Definition: template_helper.h:67
typename front_t< Ts... >::type front
Definition: template_helper.h:13
constexpr auto getval(const unsigned int) -> T
Definition: template_helper.h:70
typename get_t< i, Ts... >::type get
Definition: template_helper.h:50
static constexpr bool all_same
Definition: template_helper.h:120
Definition: template_helper.h:106
typename back_t< T1, Ts... >::type type
Definition: template_helper.h:29
T type
Definition: template_helper.h:24
Definition: template_helper.h:20
static constexpr auto flip(flipped< Flipped_Ts... > &&)
Definition: template_helper.h:89
static constexpr auto flip()
Definition: template_helper.h:92
static constexpr auto flip()
Definition: template_helper.h:97
Definition: template_helper.h:84
Definition: template_helper.h:81
Definition: template_helper.h:9
T type
Definition: template_helper.h:10
T type
Definition: template_helper.h:41
typename get_t< i - 1, Ts... >::type type
Definition: template_helper.h:46
Definition: template_helper.h:37
Definition: template_helper.h:54