mal-packet-weaver
C++20 packet serialization/deserialization library.
Loading...
Searching...
No Matches
template.hpp
Go to the documentation of this file.
1#pragma once
7namespace mal_toolkit
8{
9 namespace _template_detail
10 {
16 template <typename A>
17 constexpr size_t CalculateSize()
18 {
19 return sizeof(A);
20 }
21
29 template <typename A, typename B, typename... Args>
30 constexpr size_t CalculateSize()
31 {
32 return sizeof(A) + CalculateSize<B, Args...>();
33 }
34
41 template <typename T>
42 consteval size_t index_of_helper(std::size_t)
43 {
44 return std::numeric_limits<std::size_t>::max();
45 }
46
54 template <typename T, typename U>
55 consteval size_t index_of_helper(std::size_t index)
56 {
57 if constexpr (std::is_same_v<T, U>)
58 {
59 return index;
60 }
61 return index_of_helper<T>(index + 1);
62 }
63
73 template <typename T, typename U, typename V, typename... Rest>
74 consteval size_t index_of_helper(std::size_t index)
75 {
76 if constexpr (std::is_same_v<T, U>)
77 {
78 return index;
79 }
80 else
81 {
82 return index_of_helper<T, V, Rest...>(index + 1);
83 }
84 }
85
90 template <typename Func>
92
98 template <typename Ret, typename... Args>
99 struct function_traits<std::function<Ret(Args...)>>
100 {
101 using ArgumentTypes = std::tuple<Args...>;
102 };
103
108 template <typename Func>
110
116 template <typename Ret, typename... Args>
117 struct index_of_function_arg<std::function<Ret(Args...)>>
118 {
124 template <typename T>
125 static consteval size_t index_of_type()
126 {
127 return index_of_helper<T, Args...>(0);
128 }
129 };
130 } // namespace _template_detail
131
135 template <typename... Args>
137 {
141 static constexpr size_t size = _template_detail::CalculateSize<Args...>();
145 static constexpr size_t amount = sizeof...(Args);
146
153 template <size_t index>
154 struct type_at
155 {
156 static_assert(index < size);
157 using type = std::tuple_element_t<index, std::tuple<Args...>>;
158 };
159 };
160
172 template <typename T, typename... Types>
173 consteval size_t index_of_type()
174 {
175 return _template_detail::index_of_helper<T, Types...>(0);
176 }
177
190 template <typename T, typename Func>
191 consteval std::size_t index_of_function_arg([[maybe_unused]] Func const &func = {})
192 {
193 return _template_detail::index_of_function_arg<Func>::template index_of_type<T>();
194 }
195
196 // based on https://artificial-mind.net/blog/2020/10/31/constexpr-for
197
201 template <auto begin, auto end, auto inc, class F>
202 constexpr void constexpr_for(F &&f)
203 {
204 f(std::integral_constant<decltype(begin), begin>{});
205 if constexpr (begin + inc >= end)
206 {
207 return;
208 }
209 else // else should be constexpr as well
210 {
211 constexpr_for<begin + inc, end, inc>(std::forward<F>(f));
212 }
213 }
214 template <auto begin, auto end, auto inc, class F>
215 constexpr void constexpr_for_break(F &&f)
216 {
217 if (not f(std::integral_constant<decltype(begin), begin>{}))
218 {
219 return;
220 }
221 if constexpr (begin + inc >= end)
222 {
223 return;
224 }
225 else // else should be constexpr as well
226 {
227 constexpr_for<begin + inc, end, inc>(std::forward<F>(f));
228 }
229 }
230
234 template <class iterator, class F>
235 constexpr bool for_each_true(iterator begin, iterator const &end, F &&f)
236 {
237 bool rv = true;
238 for (; begin != end;)
239 {
240 rv &= f(begin++);
241 }
242 return rv;
243 }
244
248 template <typename T>
249 concept EnumClassConcept = std::is_enum_v<T> && !std::convertible_to<T, int>;
250
254 template <EnumClassConcept Enumeration>
255 constexpr auto as_integer(Enumeration const value) -> typename std::underlying_type<Enumeration>::type
256 {
257 return static_cast<typename std::underlying_type<Enumeration>::type>(value);
258 }
259
263 template <typename T>
265
266 template <typename R, typename... Args>
267 struct return_type<R(Args...)>
268 {
269 using type = R;
270 };
271
272 template <typename R, typename... Args>
273 struct return_type<R (*)(Args...)>
274 {
275 using type = R;
276 };
277
278 template <typename R, typename C, typename... Args>
279 struct return_type<R (C::*)(Args...)>
280 {
281 using type = R;
282 };
283
284 template <typename R, typename C, typename... Args>
285 struct return_type<R (C::*)(Args...) &>
286 {
287 using type = R;
288 };
289
290 template <typename R, typename C, typename... Args>
291 struct return_type<R (C::*)(Args...) &&>
292 {
293 using type = R;
294 };
295
296 template <typename R, typename C, typename... Args>
297 struct return_type<R (C::*)(Args...) const>
298 {
299 using type = R;
300 };
301
302 template <typename R, typename C, typename... Args>
303 struct return_type<R (C::*)(Args...) const &>
304 {
305 using type = R;
306 };
307
308 template <typename R, typename C, typename... Args>
309 struct return_type<R (C::*)(Args...) const &&>
310 {
311 using type = R;
312 };
313
314 template <typename R, typename C, typename... Args>
315 struct return_type<R (C::*)(Args...) volatile>
316 {
317 using type = R;
318 };
319
320 template <typename R, typename C, typename... Args>
321 struct return_type<R (C::*)(Args...) volatile &>
322 {
323 using type = R;
324 };
325
326 template <typename R, typename C, typename... Args>
327 struct return_type<R (C::*)(Args...) volatile &&>
328 {
329 using type = R;
330 };
331
332 template <typename R, typename C, typename... Args>
333 struct return_type<R (C::*)(Args...) const volatile>
334 {
335 using type = R;
336 };
337
338 template <typename R, typename C, typename... Args>
339 struct return_type<R (C::*)(Args...) const volatile &>
340 {
341 using type = R;
342 };
343
344 template <typename R, typename C, typename... Args>
345 struct return_type<R (C::*)(Args...) const volatile &&>
346 {
347 using type = R;
348 };
349
350 template <typename T>
352} // namespace mal_toolkit
Concept to check if a type is an enumeration class.
Definition template.hpp:249
consteval size_t index_of_helper(std::size_t)
Calculates the index of a type within a parameter pack.
Definition template.hpp:42
constexpr size_t CalculateSize()
Calculates the total size of multiple types in bytes.
Definition template.hpp:17
Contains a collection of tools and utilities provided by the MAL Toolkit library.
Definition backoffs.hpp:7
consteval std::size_t index_of_function_arg(Func const &func={})
Function to find the index of a type within a function's argument list.
Definition template.hpp:191
consteval size_t index_of_type()
Function to find the index of a type in a parameter pack.
Definition template.hpp:173
constexpr bool for_each_true(iterator begin, iterator const &end, F &&f)
Applies a function to each element in a range and returns true if all return true.
Definition template.hpp:235
constexpr void constexpr_for(F &&f)
A constexpr loop for compile-time iteration.
Definition template.hpp:202
typename return_type< T >::type return_type_t
Definition template.hpp:351
constexpr void constexpr_for_break(F &&f)
Definition template.hpp:215
constexpr auto as_integer(Enumeration const value) -> typename std::underlying_type< Enumeration >::type
Converts an enumeration class value to its underlying integer value.
Definition template.hpp:255
STL namespace.
Traits for extracting information from a function type.
Definition template.hpp:91
static consteval size_t index_of_type()
Calculates the index of a type within the function's argument list.
Definition template.hpp:125
Helper for calculating the index of a type within a function's argument list.
Definition template.hpp:109
std::tuple_element_t< index, std::tuple< Args... > > type
Definition template.hpp:157
Provides information about a parameter pack.
Definition template.hpp:137
static constexpr size_t amount
total amount of provided types.
Definition template.hpp:145
static constexpr size_t size
sizeof all types.
Definition template.hpp:141
Extracts the return type of a function or function pointer.
Definition template.hpp:264