Visual Computing Library  devel
Loading...
Searching...
No Matches
predicates.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_MESH_COMPONENTS_BASE_PREDICATES_H
24#define VCL_MESH_COMPONENTS_BASE_PREDICATES_H
25
26#include <type_traits>
27
28/*
29 * Why this boilerplate of code?
30 * Because we need to check if a type is a specialization of a templated class,
31 * and this can be done in a generalized way only if template arguments are
32 * TYPES.
33 *
34 * If they are integers or booleans, they cannot be resolved using typenames, so
35 * we need to use a different approach.
36 *
37 * We created a set of namespaces that contain the predicates to check if a
38 * type is a specialization of a templated class with different order and
39 * combination of template arguments.
40 *
41 * This set of predicates involves only the combinations that are necessary for
42 * the components of the VCLib library.
43 *
44 * Waiting for a standard solution to check if a type is a specialization
45 * of a templated class, regardless of the template arguments :)
46 */
47
48namespace vcl::comp {
49
50// type-bool
51namespace TB {
52
53namespace detail {
54
55template<typename T, template<typename, bool> class Template>
56struct IsSpecializationOfComp : std::false_type
57{
58};
59
60template<template<typename, bool> class Template, typename T, bool B>
61struct IsSpecializationOfComp<Template<T, B>, Template> : std::true_type
62{
63};
64
65template<typename T, template<typename, bool> class Template>
66struct IsDerivedFromSpecializationOfComp
67{
68private:
69 template<typename TT, bool B>
70 static std::true_type test(const Template<TT, B>*);
71 static std::false_type test(...);
72
73public:
74 static constexpr bool value =
75 decltype(test(std::declval<std::remove_cvref_t<T>*>()))::value;
76};
77
78} // namespace detail
79
80template<typename T, template<typename, bool> class Template>
81constexpr bool IsDerivedFromSpecializationOfV =
82 detail::IsDerivedFromSpecializationOfComp<T, Template>::value;
83
84} // namespace TB
85
86// int-type-bool
87namespace ITB {
88
89namespace detail {
90
91template<typename T, template<int, typename, bool> class Template>
92struct IsSpecializationOfComp : std::false_type
93{
94};
95
96template<
97 template<int, typename, bool>
98 class Template,
99 int I,
100 typename T,
101 bool B>
102struct IsSpecializationOfComp<Template<I, T, B>, Template> : std::true_type
103{
104};
105
106template<typename T, template<int, typename, bool> class Template>
107struct IsDerivedFromSpecializationOfComp
108{
109private:
110 template<int I, typename TT, bool B>
111 static std::true_type test(const Template<I, TT, B>*);
112 static std::false_type test(...);
113
114public:
115 static constexpr bool value =
116 decltype(test(std::declval<std::remove_cvref_t<T>*>()))::value;
117};
118
119} // namespace detail
120
121template<typename T, template<int, typename, bool> class Template>
122constexpr bool IsDerivedFromSpecializationOfV =
123 detail::IsDerivedFromSpecializationOfComp<T, Template>::value;
124
125} // namespace ITB
126
127// type-type-bool
128namespace TTB {
129
130namespace detail {
131
132template<typename T, template<typename, typename, bool> class Template>
133struct IsSpecializationOfComp : std::false_type
134{
135};
136
137template<
138 template<typename, typename, bool>
139 class Template,
140 typename T1,
141 typename T2,
142 bool B>
143struct IsSpecializationOfComp<Template<T1, T2, B>, Template> : std::true_type
144{
145};
146
147template<typename T, template<typename, typename, bool> class Template>
148struct IsDerivedFromSpecializationOfComp
149{
150private:
151 template<typename T1, typename T2, bool B>
152 static std::true_type test(const Template<T1, T2, B>*);
153 static std::false_type test(...);
154
155public:
156 static constexpr bool value =
157 decltype(test(std::declval<std::remove_cvref_t<T>*>()))::value;
158};
159
160} // namespace detail
161
162template<typename T, template<typename, typename, bool> class Template>
163constexpr bool IsDerivedFromSpecializationOfV =
164 detail::IsDerivedFromSpecializationOfComp<T, Template>::value;
165
166} // namespace TTB
167
168// type-int-type-bool
169namespace TITB {
170
171namespace detail {
172
173template<typename T, template<typename, int, typename, bool> class Template>
174struct IsSpecializationOfComp : std::false_type
175{
176};
177
178template<
179 template<typename, int, typename, bool>
180 class Template,
181 typename T1,
182 int I,
183 typename T2,
184 bool B>
185struct IsSpecializationOfComp<Template<T1, I, T2, B>, Template> : std::true_type
186{
187};
188
189template<typename T, template<typename, int, typename, bool> class Template>
190struct IsDerivedFromSpecializationOfComp
191{
192private:
193 template<typename T1, int I, typename T2, bool B>
194 static std::true_type test(const Template<T1, I, T2, B>*);
195 static std::false_type test(...);
196
197public:
198 static constexpr bool value =
199 decltype(test(std::declval<std::remove_cvref_t<T>*>()))::value;
200};
201
202} // namespace detail
203
204template<typename T, template<typename, int, typename, bool> class Template>
205constexpr bool IsDerivedFromSpecializationOfV =
206 detail::IsDerivedFromSpecializationOfComp<T, Template>::value;
207
208} // namespace TITB
209
210// bool-type-int-type-bool
211namespace BTITB {
212
213namespace detail {
214
215template<
216 typename T,
217 template<bool, typename, int, typename, bool>
218 class Template>
219struct IsSpecializationOfComp : std::false_type
220{
221};
222
223template<
224 template<bool, typename, int, typename, bool>
225 class Template,
226 bool B1,
227 typename T1,
228 int I,
229 typename T2,
230 bool B2>
231struct IsSpecializationOfComp<Template<B1, T1, I, T2, B2>, Template> :
232 std::true_type
233{
234};
235
236template<
237 typename T,
238 template<bool, typename, int, typename, bool>
239 class Template>
240struct IsDerivedFromSpecializationOfComp
241{
242private:
243 template<bool B1, typename T1, int I, typename T2, bool B2>
244 static std::true_type test(const Template<B1, T1, I, T2, B2>*);
245 static std::false_type test(...);
246
247public:
248 static constexpr bool value =
249 decltype(test(std::declval<std::remove_cvref_t<T>*>()))::value;
250};
251
252} // namespace detail
253
254template<
255 typename T,
256 template<bool, typename, int, typename, bool>
257 class Template>
258constexpr bool IsDerivedFromSpecializationOfV =
259 detail::IsDerivedFromSpecializationOfComp<T, Template>::value;
260
261} // namespace BTITB
262
263// bool-type-type-bool-bool
264namespace BTTBB {
265
266namespace detail {
267
268template<
269 typename T,
270 template<bool, typename, typename, bool, bool>
271 class Template>
272struct IsSpecializationOfComp : std::false_type
273{
274};
275
276template<
277 template<bool, typename, typename, bool, bool>
278 class Template,
279 bool B1,
280 typename T1,
281 typename T2,
282 bool B2,
283 bool B3>
284struct IsSpecializationOfComp<Template<B1, T1, T2, B2, B3>, Template> :
285 std::true_type
286{
287};
288
289template<
290 typename T,
291 template<bool, typename, typename, bool, bool>
292 class Template>
293struct IsDerivedFromSpecializationOfComp
294{
295private:
296 template<bool B1, typename T1, typename T2, bool B2, bool B3>
297 static std::true_type test(const Template<B1, T1, T2, B2, B3>*);
298 static std::false_type test(...);
299
300public:
301 static constexpr bool value =
302 decltype(test(std::declval<std::remove_cvref_t<T>*>()))::value;
303};
304
305} // namespace detail
306
307template<
308 typename T,
309 template<bool, typename, typename, bool, bool>
310 class Template>
311constexpr bool IsDerivedFromSpecializationOfV =
312 detail::IsDerivedFromSpecializationOfComp<T, Template>::value;
313
314} // namespace BTTBB
315
316// bool-type-int-bool-type-bool-bool
317namespace BTIBTBB {
318
319namespace detail {
320
321template<
322 typename T,
323 template<bool, typename, int, bool, typename, bool, bool>
324 class Template>
325struct IsSpecializationOfComp : std::false_type
326{
327};
328
329template<
330 template<bool, typename, int, bool, typename, bool, bool>
331 class Template,
332 bool B1,
333 typename T1,
334 int I,
335 bool B2,
336 typename T2,
337 bool B3,
338 bool B4>
339struct IsSpecializationOfComp<Template<B1, T1, I, B2, T2, B3, B4>, Template> :
340 std::true_type
341{
342};
343
344template<
345 typename T,
346 template<bool, typename, int, bool, typename, bool, bool>
347 class Template>
348struct IsDerivedFromSpecializationOfComp
349{
350private:
351 template<
352 bool B1,
353 typename T1,
354 int I,
355 bool B2,
356 typename T2,
357 bool B3,
358 bool B4>
359 static std::true_type test(const Template<B1, T1, I, B2, T2, B3, B4>*);
360 static std::false_type test(...);
361
362public:
363 static constexpr bool value =
364 decltype(test(std::declval<std::remove_cvref_t<T>*>()))::value;
365};
366
367} // namespace detail
368
369template<
370 typename T,
371 template<bool, typename, int, bool, typename, bool, bool>
372 class Template>
373constexpr bool IsDerivedFromSpecializationOfV =
374 detail::IsDerivedFromSpecializationOfComp<T, Template>::value;
375
376} // namespace BTIBTBB
377
378} // namespace vcl::comp
379
380#endif // VCL_MESH_COMPONENTS_BASE_PREDICATES_H
constexpr bool IsDerivedFromSpecializationOfV
Alias for IsDerivedFromSpecializationOf trait.
Definition variadic_templates.h:285