MCRO
C++23 utilities for Unreal Engine.
Loading...
Searching...
No Matches
Templates.h
Go to the documentation of this file.
1/** @noop License Comment
2 * @file
3 * @copyright
4 * This Source Code is subject to the terms of the Mozilla Public License, v2.0.
5 * If a copy of the MPL was not distributed with this file You can obtain one at
6 * https://mozilla.org/MPL/2.0/
7 *
8 * @author David Mórász
9 * @date 2025
10 */
11
12#pragma once
13
14#include "CoreMinimal.h"
15#include "Concepts.h"
16
17/**
18 * This namespace provide some introspection into template instantiations.
19 *
20 * @warning
21 * Members of this namespace is very limited in usage and therefore should be used with utmost care.
22 * Until this proposal https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p1985r0.pdf or equivalent is considered
23 * seriously, template traits only work with templates which only have type-parameters. Non-type parameters even when
24 * a default is specified for them will result in compile error.
25 */
27{
28 using namespace Mcro::Concepts;
29
30 /**
31 * Base struct containing traits of specified template (which only accepts type parameters)
32 *
33 * @warning
34 * Until this proposal https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p1985r0.pdf or equivalent is
35 * considered seriously, template traits only work with templates which only have type-parameters. Non-type
36 * parameters even when a default is specified for them will result in compile error.
37 */
38 template <template <typename...> typename Template>
39 struct TTemplate
40 {
41 template <typename T>
42 static constexpr bool Match = false;
43
44 template <typename... Params>
45 static constexpr bool Match<Template<Params...>> = true;
46
47 template <typename T>
49 {
50 using Type = TTuple<>;
51 };
52
53 template <typename... Params>
54 struct Parameters<Template<Params...>>
55 {
56 using Type = TTuple<Params...>;
57 };
58 };
59
60 /**
61 * Get template type parameters as a tuple
62 *
63 * @warning
64 * Until this proposal https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p1985r0.pdf or equivalent is
65 * considered seriously, template traits only work with templates which only have type-parameters. Non-type
66 * parameters even when a default is specified for them will result in compile error.
67 */
68 template <template <typename...> typename Template, typename T>
69 using TTemplate_Params = typename TTemplate<Template>::template Parameters<T>::Type;
70
71 /**
72 * Check if given type is an instantiation of a given template (which only accepts type parameters)
73 *
74 * @warning
75 * Until this proposal https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p1985r0.pdf or equivalent is
76 * considered seriously, template traits only work with templates which only have type-parameters. Non-type
77 * parameters even when a default is specified for them will result in compile error.
78 */
79 template <template <typename...> typename Template, typename T>
80 concept CIsTemplate = TTemplate<Template>::template Match<T>::Value;
81
82 template <CConstType T>
83 auto AsConst(T&& input) { return Forward<T>(input); }
84
85 template <CMutableType T>
86 auto AsConst(T&& input) { return Forward<T>(const_cast<const T>(input)); }
87
88 template <CMutableType T>
89 auto AsMutable(T&& input) { return Forward<T>(input); }
90
91 template <CConstType T>
92 auto AsMutable(T&& input) { return Forward<T>(const_cast<T>(input)); }
93
94 template <typename T>
95 auto AsConstPtr(const T* input) { return input; }
96
97 template <typename T>
98 auto AsConstPtr(T* input) { return const_cast<const T*>(input); }
99
100 template <typename T>
101 auto AsMutablePtr(T* input) { return input; }
102
103 template <typename T>
104 auto AsMutablePtr(const T* input) { return const_cast<T*>(input); }
105}
typename TTemplate< Template >::template Parameters< T >::Type TTemplate_Params
Definition Templates.h:69
auto AsConst(T &&input)
Definition Templates.h:83
auto AsConstPtr(const T *input)
Definition Templates.h:95
auto AsMutablePtr(T *input)
Definition Templates.h:101
auto AsMutable(T &&input)
Definition Templates.h:89
static constexpr bool Match
Definition Templates.h:42