Visual Computing Library
Loading...
Searching...
No Matches
variadic_templates.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_TYPES_VARIADIC_TEMPLATES_H
24#define VCL_TYPES_VARIADIC_TEMPLATES_H
25
26#include "base.h"
27
28#include <tuple>
29#include <typeindex>
30
31namespace vcl {
32
33// TODO: write documentation for all the functions and classes in this file
34
47template<typename... Args>
49{
50 using type = std::tuple_element<0, std::tuple<Args...>>::type;
51};
52
64template<typename... Args>
65using FirstTypeT = typename FirstType<Args...>::type;
66
73// https://stackoverflow.com/a/71477756/5851101
74template<typename T, typename U, typename... Us>
75constexpr uint indexInTypePack()
76{
77 if constexpr (std::is_same_v<T, U>) {
78 return 0;
79 }
80 else {
81 if constexpr (sizeof...(Us)) {
82 // there is at least another type to check
83 constexpr uint res =
84 indexInTypePack<T, Us...>(); // look in the rest of the types
85 if constexpr (res == UINT_NULL) // not found in the rest
86 return UINT_NULL;
87 else
88 return 1 + res; // found
89 }
90 else { // not found
91 return UINT_NULL;
92 }
93 }
94}
95
105template<typename U, typename... Us>
106uint indexInTypePack(std::type_index ti)
107{
108 if (ti == typeid(U)) {
109 return 0;
110 }
111 else {
112 if constexpr (sizeof...(Us)) {
113 // there is at least another type to check
114 uint res = indexInTypePack<Us...>(ti);
115 if (res == UINT_NULL)
116 return UINT_NULL;
117 else
118 return 1 + res;
119 }
120 else { // not found
121 return UINT_NULL;
122 }
123 }
124}
125
126template<typename T, typename... Us>
128{
129 static constexpr uint value = indexInTypePack<T, Us...>();
130};
131
132template<uint I, typename... T>
133struct TypeAt
134{
135 using type = std::tuple_element_t<I, std::tuple<T...>>;
136};
137
138template<uint I, typename... T>
139using TypeAtT = typename TypeAt<I, T...>::type;
140
141template<typename... Args>
143{
144 static constexpr uint value = sizeof...(Args);
145};
146
164template<typename... T>
166{
167 template<typename F>
168 static void apply(F&& f)
169 {
170 (f.template operator()<T>(), ...);
171 }
172};
173
174} // namespace vcl
175
176#endif // VCL_TYPES_VARIADIC_TEMPLATES_H
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
typename FirstType< Args... >::type FirstTypeT
Alias for the type of the first type in a pack of types.
Definition variadic_templates.h:65
constexpr uint UINT_NULL
The UINT_NULL value represent a null value of uint that is the maximum value that can be represented ...
Definition base.h:48
constexpr uint indexInTypePack()
Function that returns the index of a Type T in a pack of types (variadic templates)....
Definition variadic_templates.h:75
Get the first type of a pack of types (variadic templates) or a TypeWrapper.
Definition variadic_templates.h:49
Allows to apply a function to each type in a variadic template pack.
Definition variadic_templates.h:166
Definition variadic_templates.h:128
Definition variadic_templates.h:143
Definition variadic_templates.h:134