Visual Computing Library  devel
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_BASE_VARIADIC_TEMPLATES_H
24#define VCL_BASE_VARIADIC_TEMPLATES_H
25
26#include "base.h"
27
28#include <tuple>
29#include <typeindex>
30
31namespace vcl {
32
45template<typename... Args>
47{
48 using type = std::tuple_element<0, std::tuple<Args...>>::type;
49};
50
62template<typename... Args>
63using FirstTypeT = typename FirstType<Args...>::type;
64
71// https://stackoverflow.com/a/71477756/5851101
72template<typename T, typename U, typename... Us>
73constexpr uint indexInTypePack()
74{
75 if constexpr (std::is_same_v<T, U>) {
76 return 0;
77 }
78 else {
79 if constexpr (sizeof...(Us)) {
80 // there is at least another type to check
81 constexpr uint res =
82 indexInTypePack<T, Us...>(); // look in the rest of the types
83 if constexpr (res == UINT_NULL) // not found in the rest
84 return UINT_NULL;
85 else
86 return 1 + res; // found
87 }
88 else { // not found
89 return UINT_NULL;
90 }
91 }
92}
93
103template<typename U, typename... Us>
104uint indexInTypePack(std::type_index ti)
105{
106 if (ti == typeid(U)) {
107 return 0;
108 }
109 else {
110 if constexpr (sizeof...(Us)) {
111 // there is at least another type to check
112 uint res = indexInTypePack<Us...>(ti);
113 if (res == UINT_NULL)
114 return UINT_NULL;
115 else
116 return 1 + res;
117 }
118 else { // not found
119 return UINT_NULL;
120 }
121 }
122}
123
136template<typename T, typename... Us>
138{
139 static constexpr uint value = indexInTypePack<T, Us...>();
140};
141
154template<uint I, typename... T>
155struct TypeAt
156{
157 using type = std::tuple_element_t<I, std::tuple<T...>>;
158};
159
172template<uint I, typename... T>
173using TypeAtT = typename TypeAt<I, T...>::type;
174
187template<typename... Args>
189{
190 static constexpr uint value = sizeof...(Args);
191};
192
210template<typename... T>
212{
213 template<typename F>
214 static void apply(F&& f)
215 {
216 (f.template operator()<T>(), ...);
217 }
218};
219
231template<typename T, template<typename...> class Template>
232struct IsSpecializationOf : std::false_type
233{
234};
235
236template<template<typename...> class Template, typename... Args>
237struct IsSpecializationOf<Template<Args...>, Template> : std::true_type
238{
239};
240
246template<typename T, template<typename...> class Template>
248
265template<typename T, template<typename...> class Template>
267{
268private:
269 // Helper to check if T is derived from Template<Args...> for any Args
270 template<typename... Args>
271 static std::true_type test(const Template<Args...>*);
272 static std::false_type test(...);
273
274public:
275 static constexpr bool value =
276 decltype(test(std::declval<std::remove_cvref_t<T>*>()))::value;
277};
278
284template<typename T, template<typename...> class Template>
287
288} // namespace vcl
289
290#endif // VCL_BASE_VARIADIC_TEMPLATES_H
A class representing a box in N-dimensional space.
Definition box.h:46
typename FirstType< Args... >::type FirstTypeT
Alias for the type of the first type in a pack of types.
Definition variadic_templates.h:63
constexpr bool IsDerivedFromSpecializationOfV
Alias for IsDerivedFromSpecializationOf trait.
Definition variadic_templates.h:285
typename TypeAt< I, T... >::type TypeAtT
Alias for the type at a given index in a pack of types (variadic templates) or a TypeWrapper.
Definition variadic_templates.h:173
constexpr bool IsSpecializationOfV
Alias for IsSpecializationOf trait.
Definition variadic_templates.h:247
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:73
Get the first type of a pack of types (variadic templates) or a TypeWrapper.
Definition variadic_templates.h:47
Allows to apply a function to each type in a variadic template pack.
Definition variadic_templates.h:212
Get the index of a type T in a pack of types (variadic templates) or a TypeWrapper.
Definition variadic_templates.h:138
Trait to check if a type is derived from a specialization of a given template.
Definition variadic_templates.h:267
Trait to check if a type is a specialization of a given template.
Definition variadic_templates.h:233
Get the number of types in a pack of types (variadic templates) or a TypeWrapper.
Definition variadic_templates.h:189
Get the type at a given index in a pack of types (variadic templates) or a TypeWrapper.
Definition variadic_templates.h:156