typelist.h
Go to the documentation of this file.
1 #ifndef TTG_UTIL_INPUTTYPES_H
2 #define TTG_UTIL_INPUTTYPES_H
3 
4 #include <tuple>
5 
6 namespace ttg {
7 
8  namespace meta {
9 
10  template <typename T>
11  struct type_identity {
12  using type = T;
13  };
14 
23  template <typename... Ts>
24  struct typelist {
26  constexpr auto size() const { return sizeof...(Ts); }
27 
28  template <std::size_t I>
29  constexpr auto get() const {
30  return type_identity<std::tuple_element_t<I, std::tuple<Ts...>>>{};
31  }
32  };
33 
34  template <typename T>
35  struct is_typelist : std::false_type {};
36 
37  template <typename... Ts>
38  struct is_typelist<typelist<Ts...>> : std::true_type {};
39 
40  template <typename T>
42 
43  template <typename T>
45 
46  template <typename... T>
47  struct typelist_to_tuple<typelist<T...>> {
48  using type = std::tuple<T...>;
49  };
50 
51  template <typename T>
53 
54  template <typename T>
55  struct typelist_size;
56 
57  template <typename... Ts>
58  struct typelist_size<typelist<Ts...>> {
59  constexpr static std::size_t value = sizeof...(Ts);
60  };
61 
62  template <typename T>
63  constexpr std::size_t typelist_size_v = typelist_size<T>::value;
64 
65  template <typename T>
66  constexpr bool typelist_is_empty_v = (typelist_size_v<T> == 0);
67 
68  template <typename T>
69  struct typelist_is_empty : std::bool_constant<typelist_is_empty_v<T>> {};
70 
71  template <typename T>
72  constexpr bool typelist_is_not_empty_v = (typelist_size_v<T> != 0);
73 
74  template <typename T>
75  struct typelist_is_not_empty : std::bool_constant<typelist_is_not_empty_v<T>> {};
76 
77  } // namespace meta
78 
79  // typelist is user-centric API
80  template <typename... Ts>
81  using typelist = meta::typelist<Ts...>;
82 
83 } // namespace ttg
84 
85 namespace std {
86 
87  template <typename... Ts>
88  struct tuple_size<ttg::meta::typelist<Ts...>> {
89  static constexpr auto value = sizeof...(Ts);
90  };
91 
92  template <std::size_t I, typename... Ts>
93  struct tuple_element<I, ttg::meta::typelist<Ts...>> {
94  using type = typename decltype(ttg::meta::typelist<Ts...>{}.template get<I>())::type;
95  };
96 
97 } // namespace std
98 
99 namespace ttg::meta {
100  template <std::size_t I, typename T, typename... RestOfTs>
101  constexpr auto get(typelist<T, RestOfTs...>) {
102  if constexpr (I == 0)
103  return type_identity<T>{};
104  else
105  return get<I - 1>(typelist<RestOfTs...>{});
106  }
107 } // namespace ttg::meta
108 
109 #endif // TTG_UTIL_INPUTTYPES_H
constexpr bool typelist_is_empty_v
Definition: typelist.h:66
constexpr bool is_typelist_v
Definition: typelist.h:41
constexpr std::size_t typelist_size_v
Definition: typelist.h:63
constexpr bool typelist_is_not_empty_v
Definition: typelist.h:72
typename typelist_to_tuple< T >::type typelist_to_tuple_t
Definition: typelist.h:52
constexpr auto get(typelist< T, RestOfTs... >)
Definition: typelist.h:101
top-level TTG namespace contains runtime-neutral functionality
Definition: keymap.h:8
meta::typelist< Ts... > typelist
Definition: typelist.h:81
typename decltype(ttg::meta::typelist< Ts... >{}.template get< I >())::type type
Definition: typelist.h:94
A container for types.
Definition: typelist.h:24
constexpr auto size() const
Definition: typelist.h:26
constexpr auto get() const
Definition: typelist.h:29