|
25 | 25 |
|
26 | 26 | #pragma once |
27 | 27 |
|
| 28 | +#include <concepts> |
28 | 29 | #include <cstddef> |
29 | 30 | #include <memory> |
30 | 31 | #include <type_traits> |
@@ -59,7 +60,7 @@ struct TypeCastTraits<ExpectedType, ArgType, true /* isBaseType */> { |
59 | 60 | template<typename ExpectedType, typename ArgType> |
60 | 61 | inline bool is(const ArgType& source) |
61 | 62 | { |
62 | | - static_assert(std::is_base_of_v<ArgType, ExpectedType>, "Unnecessary type check"); |
| 63 | + static_assert(std::derived_from<ExpectedType, ArgType>, "Unnecessary type check"); |
63 | 64 | return TypeCastTraits<const ExpectedType, const ArgType>::isOfType(source); |
64 | 65 | } |
65 | 66 |
|
@@ -89,52 +90,52 @@ using match_constness_t = |
89 | 90 | template<typename Target, typename Source> |
90 | 91 | inline match_constness_t<Source, Target>& uncheckedDowncast(Source& source LIFETIME_BOUND) |
91 | 92 | { |
92 | | - static_assert(!std::is_same_v<Source, Target>, "Unnecessary cast to same type"); |
93 | | - static_assert(std::is_base_of_v<Source, Target>, "Should be a downcast"); |
| 93 | + static_assert(!std::same_as<Source, Target>, "Unnecessary cast to same type"); |
| 94 | + static_assert(std::derived_from<Target, Source>, "Should be a downcast"); |
94 | 95 | ASSERT_WITH_SECURITY_IMPLICATION(is<Target>(source)); |
95 | 96 | SUPPRESS_MEMORY_UNSAFE_CAST return static_cast<match_constness_t<Source, Target>&>(source); |
96 | 97 | } |
97 | 98 |
|
98 | 99 | template<typename Target, typename Source> |
99 | 100 | inline match_constness_t<Source, Target>* uncheckedDowncast(Source* source LIFETIME_BOUND) |
100 | 101 | { |
101 | | - static_assert(!std::is_same_v<Source, Target>, "Unnecessary cast to same type"); |
102 | | - static_assert(std::is_base_of_v<Source, Target>, "Should be a downcast"); |
| 102 | + static_assert(!std::same_as<Source, Target>, "Unnecessary cast to same type"); |
| 103 | + static_assert(std::derived_from<Target, Source>, "Should be a downcast"); |
103 | 104 | ASSERT_WITH_SECURITY_IMPLICATION(!source || is<Target>(*source)); |
104 | 105 | SUPPRESS_MEMORY_UNSAFE_CAST return static_cast<match_constness_t<Source, Target>*>(source); |
105 | 106 | } |
106 | 107 |
|
107 | 108 | template<typename Target, typename Source> |
108 | 109 | inline match_constness_t<Source, Target>& downcast(Source& source LIFETIME_BOUND) |
109 | 110 | { |
110 | | - static_assert(!std::is_same_v<Source, Target>, "Unnecessary cast to same type"); |
111 | | - static_assert(std::is_base_of_v<Source, Target>, "Should be a downcast"); |
| 111 | + static_assert(!std::same_as<Source, Target>, "Unnecessary cast to same type"); |
| 112 | + static_assert(std::derived_from<Target, Source>, "Should be a downcast"); |
112 | 113 | RELEASE_ASSERT_WITH_UNQUALIFIED_FUNCTION_NAME(is<Target>(source)); |
113 | 114 | SUPPRESS_MEMORY_UNSAFE_CAST return static_cast<match_constness_t<Source, Target>&>(source); |
114 | 115 | } |
115 | 116 |
|
116 | 117 | template<typename Target, typename Source> |
117 | 118 | inline match_constness_t<Source, Target>* downcast(Source* source LIFETIME_BOUND) |
118 | 119 | { |
119 | | - static_assert(!std::is_same_v<Source, Target>, "Unnecessary cast to same type"); |
120 | | - static_assert(std::is_base_of_v<Source, Target>, "Should be a downcast"); |
| 120 | + static_assert(!std::same_as<Source, Target>, "Unnecessary cast to same type"); |
| 121 | + static_assert(std::derived_from<Target, Source>, "Should be a downcast"); |
121 | 122 | RELEASE_ASSERT_WITH_UNQUALIFIED_FUNCTION_NAME(!source || is<Target>(*source)); |
122 | 123 | SUPPRESS_MEMORY_UNSAFE_CAST return static_cast<match_constness_t<Source, Target>*>(source); |
123 | 124 | } |
124 | 125 |
|
125 | 126 | template<typename Target, typename Source> |
126 | 127 | inline match_constness_t<Source, Target>* dynamicDowncast(Source& source LIFETIME_BOUND) |
127 | 128 | { |
128 | | - static_assert(!std::is_same_v<Source, Target>, "Unnecessary cast to same type"); |
129 | | - static_assert(std::is_base_of_v<Source, Target>, "Should be a downcast"); |
| 129 | + static_assert(!std::same_as<Source, Target>, "Unnecessary cast to same type"); |
| 130 | + static_assert(std::derived_from<Target, Source>, "Should be a downcast"); |
130 | 131 | SUPPRESS_MEMORY_UNSAFE_CAST return is<Target>(source) ? &static_cast<match_constness_t<Source, Target>&>(source) : nullptr; |
131 | 132 | } |
132 | 133 |
|
133 | 134 | template<typename Target, typename Source> |
134 | 135 | inline match_constness_t<Source, Target>* dynamicDowncast(Source* source LIFETIME_BOUND) |
135 | 136 | { |
136 | | - static_assert(!std::is_same_v<Source, Target>, "Unnecessary cast to same type"); |
137 | | - static_assert(std::is_base_of_v<Source, Target>, "Should be a downcast"); |
| 137 | + static_assert(!std::same_as<Source, Target>, "Unnecessary cast to same type"); |
| 138 | + static_assert(std::derived_from<Target, Source>, "Should be a downcast"); |
138 | 139 | SUPPRESS_MEMORY_UNSAFE_CAST return is<Target>(source) ? static_cast<match_constness_t<Source, Target>*>(source) : nullptr; |
139 | 140 | } |
140 | 141 |
|
|
0 commit comments