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 <utility>
6
7namespace amrex {
8
10template <class... Ts>
12{
14 static constexpr std::size_t size () noexcept { return sizeof...(Ts); }
15};
16
17namespace detail {
18 template <std::size_t I, typename T> struct TypeListGet;
19
20 template <std::size_t I, typename Head, typename... Tail>
21 struct TypeListGet<I, TypeList<Head, Tail...> >
22 : TypeListGet<I-1, TypeList<Tail...> > {};
23
24 template <typename Head, typename... Tail>
25 struct TypeListGet<0, TypeList<Head, Tail...> > {
26 using type = Head;
27 };
28}
29
31template <std::size_t I, typename T>
33
34namespace detail
35{
36 template <typename TL, typename F, std::size_t...N>
37 constexpr void for_each_impl (F const&f, std::index_sequence<N...>)
38 {
39 (f(TypeAt<N,TL>{}), ...);
40 }
41
42 template <typename TL, typename F, std::size_t...N>
43 constexpr bool for_each_until_impl (F const&f, std::index_sequence<N...>)
44 {
45 return (f(TypeAt<N,TL>{}) || ...);
46 }
47}
48
76template <typename... Ts, typename F>
77constexpr void
79{
81 (std::forward<F>(f), std::make_index_sequence<sizeof...(Ts)>());
82}
83
113template <typename... Ts, typename F>
114constexpr bool
116{
118 (std::forward<F>(f), std::make_index_sequence<sizeof...(Ts)>());
119}
120
122template <typename... As, typename... Bs>
124 return TypeList<As..., Bs...>{};
125}
126
127template <typename... Ls, typename A>
128constexpr auto single_product (TypeList<Ls...>, A) {
129 return TypeList<decltype(Ls{} + TypeList<A>{})...>{};
130}
131
132template <typename LLs, typename... As>
133constexpr auto operator* (LLs, TypeList<As...>) {
134 return (TypeList<>{} + ... + single_product(LLs{}, As{}));
135}
136
149template <typename... Ls>
150constexpr auto CartesianProduct (Ls...) {
151 return (TypeList<TypeList<>>{} * ... * Ls{});
152}
153
154template <class T, std::size_t N>
155struct TypeArray {
156 using Type = T;
157 static constexpr std::size_t size () noexcept { return N; }
158};
159
160namespace detail {
161 // return TypeList<T, T, T, T, ... (N times)> by using the fast power algorithm
162 template <class T, std::size_t N>
163 constexpr auto SingleTypeMultiplier_impl () {
164 if constexpr (N == 0) {
165 return TypeList<>{};
166 } else if constexpr (N == 1) {
167 return TypeList<T>{};
168 } else if constexpr (N % 2 == 0) {
169 return SingleTypeMultiplier_impl<T, N / 2>() + SingleTypeMultiplier_impl<T, N / 2>();
170 } else {
171 return SingleTypeMultiplier_impl<T, N - 1>() + TypeList<T>{};
172 }
173 }
174
175 // overload of SingleTypeMultiplier for multiple types:
176 // convert T[N] to T, T, T, T, ... (N times with N >= 1)
177 template <class T, std::size_t N>
178 constexpr auto SingleTypeMultiplier (const T (&)[N]) {
179 return SingleTypeMultiplier_impl<T, N>();
180 }
181
182 // overload of SingleTypeMultiplier for multiple types:
183 // convert TypeArray<T,N> to T, T, T, T, ... (N times with N >= 0)
184 // Note that only this overload works with N = 0
185 template <class T, std::size_t N>
187 return SingleTypeMultiplier_impl<T, N>();
188 }
189
190 // overload of SingleTypeMultiplier for one regular type
191 template <class T>
192 constexpr auto SingleTypeMultiplier (T) {
193 return TypeList<T>{};
194 }
195
196 // apply the types of the input TypeList as template arguments to TParam
197 template <template <class...> class TParam, class... Args>
198 constexpr auto TApply (TypeList<Args...>) {
199 return TypeList<TParam<Args...>>{};
200 }
201}
202
211template <template <class...> class TParam, class... Types>
213 (TypeList<>{} + ... + detail::SingleTypeMultiplier(std::declval<Types>()))
214))>;
215
216}
217
218#endif
constexpr bool for_each_until_impl(F const &f, std::index_sequence< N... >)
Definition AMReX_TypeList.H:43
constexpr auto TApply(TypeList< Args... >)
Definition AMReX_TypeList.H:198
constexpr auto SingleTypeMultiplier_impl()
Definition AMReX_TypeList.H:163
constexpr auto SingleTypeMultiplier(const T(&)[N])
Definition AMReX_TypeList.H:178
constexpr void for_each_impl(F const &f, std::index_sequence< N... >)
Definition AMReX_TypeList.H:37
Definition AMReX_Amr.cpp:49
typename detail::TypeListGet< I, T >::type TypeAt
Type at position I of a TypeList.
Definition AMReX_TypeList.H:32
__host__ __device__ GpuComplex< T > operator*(const GpuComplex< T > &a_x, const GpuComplex< U > &a_y) noexcept
Multiply two complex numbers.
Definition AMReX_GpuComplex.H:256
constexpr auto CartesianProduct(Ls...)
Cartesian Product of TypeLists.
Definition AMReX_TypeList.H:150
constexpr auto single_product(TypeList< Ls... >, A)
Definition AMReX_TypeList.H:128
constexpr void ForEach(TypeList< Ts... >, F &&f)
For each type t in TypeList, call f(t)
Definition AMReX_TypeList.H:78
constexpr bool ForEachUntil(TypeList< Ts... >, F &&f)
For each type t in TypeList, call f(t) until true is returned.
Definition AMReX_TypeList.H:115
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:214
__host__ __device__ XDim3 operator+(XDim3 const &a, XDim3 const &b)
Definition AMReX_Dim3.H:28
Definition AMReX_FabArrayCommI.H:1000
Definition AMReX_TypeList.H:155
static constexpr std::size_t size() noexcept
Definition AMReX_TypeList.H:157
T Type
Definition AMReX_TypeList.H:156
Struct for holding types.
Definition AMReX_TypeList.H:12
static constexpr std::size_t size() noexcept
Number of types in the TypeList.
Definition AMReX_TypeList.H:14
Definition AMReX_TypeList.H:18