Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
AMReX_TypeList.H
Go to the documentation of this file.
1#ifndef AMREX_TYPELIST_H_
2#define AMREX_TYPELIST_H_
3#include <AMReX_Config.H>
4
5#include <type_traits>
6#include <utility>
7
8namespace amrex {
9
11template <class... Ts>
13{
15 static constexpr std::size_t size () noexcept { return sizeof...(Ts); }
16};
17
19namespace detail {
20 template <std::size_t I, typename T> struct TypeListGet;
21
22 template <std::size_t I, typename Head, typename... Tail>
23 struct TypeListGet<I, TypeList<Head, Tail...> >
24 : TypeListGet<I-1, TypeList<Tail...> > {};
25
26 template <typename Head, typename... Tail>
27 struct TypeListGet<0, TypeList<Head, Tail...> > {
28 using type = Head;
29 };
30}
32
34template <std::size_t I, typename T>
35using TypeAt = typename detail::TypeListGet<I,T>::type;
36
38namespace detail
39{
40 template <typename TL, typename F, std::size_t...N>
41 constexpr void for_each_impl (F const&f, std::index_sequence<N...>)
42 {
43 (f(TypeAt<N,TL>{}), ...);
44 }
45
46 template <typename TL, typename F, std::size_t...N>
47 constexpr bool for_each_until_impl (F const&f, std::index_sequence<N...>)
48 {
49 return (f(TypeAt<N,TL>{}) || ...);
50 }
51}
53
81template <typename... Ts, typename F>
82constexpr void
84{
85 detail::for_each_impl<TypeList<Ts...>>
86 (std::forward<F>(f), std::make_index_sequence<sizeof...(Ts)>());
87}
88
118template <typename... Ts, typename F>
119constexpr bool
121{
122 return detail::for_each_until_impl<TypeList<Ts...>>
123 (std::forward<F>(f), std::make_index_sequence<sizeof...(Ts)>());
124}
125
127template <typename... As, typename... Bs>
129 return TypeList<As..., Bs...>{};
130}
131
132template <typename... Ls, typename A>
133constexpr auto single_product (TypeList<Ls...>, A) {
134 return TypeList<decltype(Ls{} + TypeList<A>{})...>{};
135}
136
137template <typename LLs, typename... As>
138constexpr auto operator* (LLs, TypeList<As...>) {
139 return (TypeList<>{} + ... + single_product(LLs{}, As{}));
140}
141
154template <typename... Ls>
155constexpr auto CartesianProduct (Ls...) {
156 return (TypeList<TypeList<>>{} * ... * Ls{});
157}
158
159template <class T, std::size_t N>
160struct TypeArray {
161 using Type = T;
162 static constexpr std::size_t size () noexcept { return N; }
163};
164
166namespace detail {
167 // return TypeList<T, T, T, T, ... (N times)> by using the fast power algorithm
168 template <class T, std::size_t N>
169 constexpr auto SingleTypeMultiplier_impl () {
170 if constexpr (N == 0) {
171 return TypeList<>{};
172 } else if constexpr (N == 1) {
173 return TypeList<T>{};
174 } else if constexpr (N % 2 == 0) {
175 return SingleTypeMultiplier_impl<T, N / 2>() + SingleTypeMultiplier_impl<T, N / 2>();
176 } else {
177 return SingleTypeMultiplier_impl<T, N - 1>() + TypeList<T>{};
178 }
179 }
180
181 // overload of SingleTypeMultiplier for multiple types:
182 // convert T[N] to T, T, T, T, ... (N times with N >= 1)
183 template <class T, std::size_t N>
184 constexpr auto SingleTypeMultiplier (const T (&)[N]) {
185 return SingleTypeMultiplier_impl<T, N>();
186 }
187
188 // overload of SingleTypeMultiplier for multiple types:
189 // convert TypeArray<T,N> to T, T, T, T, ... (N times with N >= 0)
190 // Note that only this overload works with N = 0
191 template <class T, std::size_t N>
192 constexpr auto SingleTypeMultiplier (TypeArray<T, N>) {
193 return SingleTypeMultiplier_impl<T, N>();
194 }
195
196 // overload of SingleTypeMultiplier for one regular type
197 template <class T>
198 constexpr auto SingleTypeMultiplier (T) {
199 return TypeList<T>{};
200 }
201
202 // apply the types of the input TypeList as template arguments to TParam
203 template <template <class...> class TParam, class... Args>
204 constexpr auto TApply (TypeList<Args...>) {
205 return TypeList<TParam<Args...>>{};
206 }
207}
209
218template <template <class...> class TParam, class... Types>
220 (TypeList<>{} + ... + detail::SingleTypeMultiplier(std::declval<Types>()))
221))>;
222
226template <typename T>
227struct ToTypeList { using type = TypeList<T>; };
228
229template <typename... Ts>
230struct ToTypeList<TypeList<Ts...>> { using type = TypeList<Ts...>; };
231
232template <typename T>
234
235template <typename T> struct IsTypeList : std::false_type {};
236template <typename... Ts> struct IsTypeList<TypeList<Ts...>> : std::true_type {};
237template <typename T> inline constexpr bool IsTypeList_v = IsTypeList<T>::value;
238
239}
240
241#endif
Definition AMReX_Amr.cpp:49
typename detail::TypeListGet< I, T >::type TypeAt
Type at position I of a TypeList.
Definition AMReX_TypeList.H:35
__host__ __device__ GpuComplex< T > operator*(const GpuComplex< T > &a_x, const GpuComplex< U > &a_y) noexcept
Multiply two complex numbers.
Definition AMReX_GpuComplex.H:257
typename ToTypeList< T >::type ToTypeList_t
Definition AMReX_TypeList.H:233
constexpr bool IsTypeList_v
Definition AMReX_TypeList.H:237
constexpr auto CartesianProduct(Ls...)
Cartesian Product of TypeLists.
Definition AMReX_TypeList.H:155
constexpr auto single_product(TypeList< Ls... >, A)
Definition AMReX_TypeList.H:133
constexpr void ForEach(TypeList< Ts... >, F &&f)
For each type t in TypeList, call f(t)
Definition AMReX_TypeList.H:83
constexpr bool ForEachUntil(TypeList< Ts... >, F &&f)
For each type t in TypeList, call f(t) until true is returned.
Definition AMReX_TypeList.H:120
TypeAt< 0, decltype(detail::TApply< TParam >((TypeList<>{}+...+detail::SingleTypeMultiplier(std::declval< Types >()))))> TypeMultiplier
Return the first template argument with the later arguments applied to it. Types of the form T[N] are...
Definition AMReX_TypeList.H:221
__host__ __device__ XDim3 operator+(XDim3 const &a, XDim3 const &b)
Definition AMReX_Dim3.H:28
Definition AMReX_TypeList.H:235
Wrap non-TypeList into TypeList.
Definition AMReX_TypeList.H:227
Definition AMReX_TypeList.H:160
static constexpr std::size_t size() noexcept
Definition AMReX_TypeList.H:162
T Type
Definition AMReX_TypeList.H:161
Struct for holding types.
Definition AMReX_TypeList.H:13
static constexpr std::size_t size() noexcept
Number of types in the TypeList.
Definition AMReX_TypeList.H:15