version 0.4
traits.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2021-2024 The Ikarus Developers mueller@ibb.uni-stuttgart.de
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
9#pragma once
10#include <optional>
11#include <tuple>
12#include <type_traits>
13
14#include <dune/common/hybridutilities.hh>
15namespace Ikarus::traits {
16
22 template <typename T>
23 concept Pointer = std::is_pointer_v<T> || std::is_same_v<T, std::nullptr_t>;
24
25#ifndef DOXYGEN
26 template <typename>
27 struct is_tuple : std::false_type {};
28#endif
34 template <typename... T>
35 struct is_tuple<std::tuple<T...>> : std::true_type {};
36
44 template <class Tuple, class Type>
45 requires is_tuple<Tuple>::value consteval int countType() {
46 int count = 0;
47 Dune::Hybrid::forEach(Dune::Hybrid::integralRange(Dune::index_constant<std::tuple_size_v<Tuple>>()), [&](auto i) {
48 using currentType = std::remove_cvref_t<std::tuple_element_t<i, Tuple>>;
49 if constexpr (std::is_same_v<currentType, Type>) ++count;
50 });
51 return count;
52 }
53
62 template <typename Fun, typename... Args>
63 using ReturnType = std::invoke_result_t<Fun, Args...>;
64
73 template <typename T, typename Tuple>
74 struct hasType : std::false_type {};
75
76#ifndef DOXYGEN
77 template <typename T>
78 struct hasType<T, std::tuple<>> : std::false_type {};
79
80 template <typename T>
81 struct hasType<T, T> : std::true_type {};
82
90 template <typename T, typename U, typename... Ts>
91 struct hasType<T, std::tuple<U, Ts...>> : hasType<T, std::tuple<Ts...>> {};
92
93 template <typename T, typename... Ts>
94 struct hasType<T, std::tuple<T, Ts...>> : std::true_type {};
95#endif
96
97#ifndef DOXYGEN
98 template <template <typename...> class, typename...>
99 struct isSpecialization : std::false_type {};
100#endif
101
108 template <template <typename...> class U, typename... T>
109 struct isSpecialization<U, U<T...>> : std::true_type {};
110
111#ifndef DOXYGEN
112 template <template <typename, auto...> class Type, typename>
113 struct isSpecializationTypeAndNonTypes : std::false_type {};
114
115 template <template <typename, auto...> class Type, typename T, auto... N>
116 struct isSpecializationTypeAndNonTypes<Type, Type<T, N...>> : std::true_type {};
117
118 template <template <auto, typename...> class Type, typename>
119 struct isSpecializationNonTypeAndTypes : std::false_type {};
120#endif
121
131 template <template <auto, typename...> class Type, auto T, typename... N>
132 struct isSpecializationNonTypeAndTypes<Type, Type<T, N...>> : std::true_type {};
133
134#ifndef DOXYGEN
135 template <template <typename, auto, typename> class Type, typename>
136 struct isSpecializationTypeNonTypeAndType : std::false_type {};
137#endif
148 template <template <typename, auto, typename> class Type, typename T, auto M, typename N>
149 struct isSpecializationTypeNonTypeAndType<Type, Type<T, M, N>> : std::true_type {};
150
151#ifndef DOXYGEN
152 template <template <auto...> class Type, typename>
153 struct isSpecializationNonTypes : std::false_type {};
154#endif
155
164 template <template <auto...> class Type, auto... N>
165 struct isSpecializationNonTypes<Type, Type<N...>> : std::true_type {};
166
184 template <class T, class Tuple>
185 struct Index;
186#ifndef DOXYGEN
187 template <class T>
188 struct Index<T, std::tuple<>> {
189 static const std::size_t value = 0;
190 };
191
192 template <class T, class... Types>
193 struct Index<T, std::tuple<T, Types...>> {
194 static constexpr std::size_t value = 0;
195 };
196
197 template <class T, class U, class... Types>
198 struct Index<T, std::tuple<U, Types...>> {
199 static const std::size_t value = 1 + Index<T, std::tuple<Types...>>::value;
200 };
201#endif
202
220 template <class Container, class NewType>
221 struct Rebind;
222
223#ifndef DOXYGEN
224 /*
225 * Specialization for types like std::vector<...> and nested std::vector<std::vector>
226 */
227 template <class OldType, class... Args, template <class...> class Container, class NewType>
228 struct Rebind<Container<OldType, Args...>, NewType> {
229 using other = Container<NewType, typename Rebind<Args, NewType>::other...>;
230 };
231
232 /*
233 * Specialization for types like std::array<...,N>
234 */
235 template <class OldType, std::size_t N, template <class, std::size_t> class Container, class NewType>
236 struct Rebind<Container<OldType, N>, NewType> {
237 using other = Container<NewType, N>;
238 };
239
240#endif
241
251 template <typename T, typename = void>
253
254#ifndef DOXYGEN
258 template <typename R, typename... Args>
259 struct FunctionTraits<R (*)(Args...)> {
260 using return_type = R;
261 template <int i>
262 using args_type = typename std::tuple_element<i, std::tuple<Args...>>::type;
263 static constexpr int numberOfArguments = sizeof...(Args);
264 };
265
269 template <typename R, typename C, typename... Args>
270 struct FunctionTraits<R (C::*)(Args...) const> {
271 using return_type = R;
272 template <int i>
273 using args_type = typename std::tuple_element<i, std::tuple<Args...>>::type;
274 static constexpr int numberOfArguments = sizeof...(Args);
275 };
276
280 template <typename R, typename C, typename... Args>
281 struct FunctionTraits<R (C::*)(Args...)> {
282 using return_type = R;
283 template <int i>
284 using args_type = typename std::tuple_element<i, std::tuple<Args...>>::type;
285 static constexpr int numberOfArguments = sizeof...(Args);
286 };
287
292 template <typename T>
293 struct FunctionTraits<T, Dune::void_t<decltype(&T::operator())>> : public FunctionTraits<decltype(&T::operator())> {};
294#endif
295
296} // namespace Ikarus::traits
std::invoke_result_t< Fun, Args... > ReturnType
Type trait to obtain the return type of a callable type when given specific arguments.
Definition: traits.hh:63
::value consteval int countType()
Metafunction to count the occurrences of a specific type in a tuple.
Definition: traits.hh:45
Definition: resultevaluators.hh:17
Definition: traits.hh:15
Type trait to check if a specified type is present in a tuple.
Definition: traits.hh:74
Type trait to get the index of a type in a tuple.
Definition: traits.hh:185
Type trait to rebind the underlying type of containers.
Definition: traits.hh:221
Type trait for extracting information about functions.
Definition: traits.hh:252
Concept to check if a type is a pointer or nullptr_t.
Definition: traits.hh:23