|
2 | 2 | // This file is part of the CPP-GL project (https://github.com/SpectraL519/cpp-gl). |
3 | 3 | // Licensed under the MIT License. See the LICENSE file in the project root for full license information. |
4 | 4 |
|
| 5 | +/// @file gl/traits.hpp |
| 6 | +/// @brief Contains C++20 concepts and type traits used to constrain library templates. |
| 7 | + |
5 | 8 | #pragma once |
6 | 9 |
|
7 | 10 | #include "gl/types/core.hpp" |
|
11 | 14 | #include <ranges> |
12 | 15 | #include <type_traits> |
13 | 16 |
|
| 17 | +/// @namespace gl::traits |
| 18 | +/// @brief Contains C++20 concepts and type traits used to constrain library templates. |
14 | 19 | namespace gl::traits { |
15 | 20 |
|
16 | | -template <typename, template <typename...> typename> |
| 21 | +/// @ingroup GL GL-Traits |
| 22 | +/// @brief Type trait to check if a type is an instantiation of a specific class template. |
| 23 | +/// @tparam T The type to check. |
| 24 | +/// @tparam Template The class template to match against. |
| 25 | +template <typename T, template <typename...> typename Template> |
17 | 26 | struct is_instantiation_of : std::false_type {}; |
18 | 27 |
|
| 28 | +/// @ingroup GL GL-Traits |
| 29 | +/// @brief Specialization of the @ref gl::traits::is_instantiation_of "is_instantiation_of" trait |
| 30 | +/// for when the type is an instantiation of the provided class template. |
19 | 31 | template <template <typename...> typename U, typename... Args> |
20 | 32 | struct is_instantiation_of<U<Args...>, U> : std::true_type {}; |
21 | 33 |
|
| 34 | +/// @ingroup GL GL-Traits |
| 35 | +/// @brief Helper variable template for @ref gl::traits::is_instantiation_of "is_instantiation_of" trait. |
| 36 | +/// @tparam T The type to check. |
| 37 | +/// @tparam U The class template to match against. |
22 | 38 | template <typename T, template <typename...> typename U> |
23 | 39 | constexpr inline bool is_instantiation_of_v = is_instantiation_of<T, U>::value; |
24 | 40 |
|
| 41 | +/// @ingroup GL GL-Traits |
| 42 | +/// @brief Concept checking if a type is an instantiation of a specific class template. |
| 43 | +/// @tparam T The type to check. |
| 44 | +/// @tparam Template The class template to match against. |
25 | 45 | template <typename T, template <typename...> typename Template> |
26 | 46 | concept c_instantiation_of = is_instantiation_of_v<T, Template>; |
27 | 47 |
|
| 48 | +/// @ingroup GL GL-Traits |
| 49 | +/// @brief Concept checking if a type is exactly one of the specified types. |
| 50 | +/// @tparam T The type to check. |
| 51 | +/// @tparam Types The parameter pack of valid types. |
28 | 52 | template <typename T, typename... Types> |
29 | 53 | concept c_one_of = std::disjunction_v<std::is_same<T, Types>...>; |
30 | 54 |
|
| 55 | +/// @ingroup GL GL-Traits |
| 56 | +/// @brief Concept checking if a type satisfies `std::ranges::range`. |
| 57 | +/// @tparam R The type to check. |
31 | 58 | template <typename R> |
32 | 59 | concept c_range = std::ranges::range<R>; |
33 | 60 |
|
| 61 | +/// @ingroup GL GL-Traits |
| 62 | +/// @brief Concept checking if a type satisfies `std::ranges::forward_range`. |
| 63 | +/// @tparam R The type to check. |
34 | 64 | template <typename R> |
35 | 65 | concept c_forward_range = std::ranges::forward_range<R>; |
36 | 66 |
|
| 67 | +/// @ingroup GL GL-Traits |
| 68 | +/// @brief Concept checking if a type satisfies `std::ranges::sized_range`. |
| 69 | +/// @tparam R The type to check. |
37 | 70 | template <typename R> |
38 | 71 | concept c_sized_range = std::ranges::sized_range<R>; |
39 | 72 |
|
| 73 | +/// @ingroup GL GL-Traits |
| 74 | +/// @brief Concept checking if a type satisfies `std::ranges::random_access_range`. |
| 75 | +/// @tparam R The type to check. |
40 | 76 | template <typename R> |
41 | 77 | concept c_random_access_range = std::ranges::random_access_range<R>; |
42 | 78 |
|
| 79 | +/// @ingroup GL GL-Traits |
| 80 | +/// @brief Concept checking if a type is a range containing a specific value type. |
| 81 | +/// |
| 82 | +/// > [!NOTE] CV qualifiers |
| 83 | +/// > |
| 84 | +/// > This concept strips cv-qualifiers from the range's value type before comparison. |
| 85 | +/// |
| 86 | +/// @tparam R The type of the range. |
| 87 | +/// @tparam T The expected value type. |
43 | 88 | template <typename R, typename T> |
44 | 89 | concept c_range_of = |
45 | 90 | c_range<R> and std::same_as<T, std::remove_cv_t<std::ranges::range_value_t<R>>>; |
46 | 91 |
|
| 92 | +/// @ingroup GL GL-Traits |
| 93 | +/// @brief Concept checking if a type is a forward range containing a specific value type. |
| 94 | +/// |
| 95 | +/// > [!NOTE] CV qualifiers |
| 96 | +/// > |
| 97 | +/// > This concept strips cv-qualifiers from the range's value type before comparison. |
| 98 | +/// |
| 99 | +/// @tparam R The type of the range. |
| 100 | +/// @tparam T The expected value type. |
47 | 101 | template <typename R, typename T> |
48 | 102 | concept c_forward_range_of = |
49 | 103 | c_forward_range<R> and std::same_as<T, std::remove_cv_t<std::ranges::range_value_t<R>>>; |
50 | 104 |
|
| 105 | +/// @ingroup GL GL-Traits |
| 106 | +/// @brief Concept checking if a type is a sized range containing a specific value type. |
| 107 | +/// |
| 108 | +/// > [!NOTE] CV qualifiers |
| 109 | +/// > |
| 110 | +/// > This concept strips cv-qualifiers from the range's value type before comparison. |
| 111 | +/// |
| 112 | +/// @tparam R The type of the range. |
| 113 | +/// @tparam T The expected value type. |
51 | 114 | template <typename R, typename T> |
52 | 115 | concept c_sized_range_of = |
53 | 116 | c_sized_range<R> and std::same_as<T, std::remove_cv_t<std::ranges::range_value_t<R>>>; |
54 | 117 |
|
| 118 | +/// @ingroup GL GL-Traits |
| 119 | +/// @brief Concept checking if a type is a random access range containing a specific value type. |
| 120 | +/// |
| 121 | +/// > [!NOTE] CV qualifiers |
| 122 | +/// > |
| 123 | +/// > This concept strips cv-qualifiers from the range's value type before comparison. |
| 124 | +/// |
| 125 | +/// @tparam R The type of the range. |
| 126 | +/// @tparam T The expected value type. |
55 | 127 | template <typename R, typename T> |
56 | 128 | concept c_random_access_range_of = |
57 | 129 | c_random_access_range<R> and std::same_as<T, std::remove_cv_t<std::ranges::range_value_t<R>>>; |
58 | 130 |
|
59 | | -// preserves cv qualifiers |
| 131 | +/// @ingroup GL GL-Traits |
| 132 | +/// @brief Concept checking if a type is a range containing a specific value type, strictly matching cv-qualifiers. |
| 133 | +/// |
| 134 | +/// > [!NOTE] CV qualifiers |
| 135 | +/// > |
| 136 | +/// > This concept preserves cv-qualifiers. The value type of the range must match `T` exactly. |
| 137 | +/// |
| 138 | +/// @tparam R The type of the range. |
| 139 | +/// @tparam T The expected value type. |
60 | 140 | template <typename R, typename T> |
61 | 141 | concept c_range_of_cv = c_range<R> and std::same_as<T, std::ranges::range_value_t<R>>; |
62 | 142 |
|
63 | | -// preserves cv qualifiers |
| 143 | +/// @ingroup GL GL-Traits |
| 144 | +/// @brief Concept checking if a type is a forward range containing a specific value type, strictly matching cv-qualifiers. |
| 145 | +/// |
| 146 | +/// > [!NOTE] CV qualifiers |
| 147 | +/// > |
| 148 | +/// > This concept preserves cv-qualifiers. The value type of the range must match `T` exactly. |
| 149 | +/// |
| 150 | +/// @tparam R The type of the range. |
| 151 | +/// @tparam T The expected value type. |
64 | 152 | template <typename R, typename T> |
65 | 153 | concept c_forward_range_of_cv = |
66 | 154 | c_forward_range<R> and std::same_as<T, std::ranges::range_value_t<R>>; |
67 | 155 |
|
68 | | -// preserves cv qualifiers |
| 156 | +/// @ingroup GL GL-Traits |
| 157 | +/// @brief Concept checking if a type is a sized range containing a specific value type, strictly matching cv-qualifiers. |
| 158 | +/// |
| 159 | +/// > [!NOTE] CV qualifiers |
| 160 | +/// > |
| 161 | +/// > This concept preserves cv-qualifiers. The value type of the range must match `T` exactly. |
| 162 | +/// |
| 163 | +/// @tparam R The type of the range. |
| 164 | +/// @tparam T The expected value type. |
69 | 165 | template <typename R, typename T> |
70 | 166 | concept c_sized_range_of_cv = c_sized_range<R> and std::same_as<T, std::ranges::range_value_t<R>>; |
71 | 167 |
|
| 168 | +/// @ingroup GL GL-Traits |
| 169 | +/// @brief Concept checking if a type is a random access range containing a specific value type, strictly matching cv-qualifiers. |
| 170 | +/// |
| 171 | +/// > [!NOTE] CV qualifiers |
| 172 | +/// > |
| 173 | +/// > This concept preserves cv-qualifiers. The value type of the range must match `T` exactly. |
| 174 | +/// |
| 175 | +/// @tparam R The type of the range. |
| 176 | +/// @tparam T The expected value type. |
72 | 177 | template <typename R, typename T> |
73 | 178 | concept c_random_access_range_of_cv = |
74 | 179 | c_random_access_range<R> and std::same_as<T, std::ranges::range_value_t<R>>; |
75 | 180 |
|
| 181 | +/// @ingroup GL GL-Traits |
| 182 | +/// @brief Concept checking if a range provides valid const iterators via `cbegin()` and `cend()`. |
| 183 | +/// @tparam R The type of the range. |
76 | 184 | template <typename R> |
77 | 185 | concept c_const_range = requires(R& r) { |
78 | 186 | std::ranges::cbegin(r); |
79 | 187 | std::ranges::cend(r); |
80 | 188 | }; |
81 | 189 |
|
| 190 | +/// @ingroup GL GL-Traits |
| 191 | +/// @brief Concept checking if dereferencing an iterator yields a const reference. |
| 192 | +/// @tparam T The iterator type to check. |
82 | 193 | template <typename T> |
83 | 194 | concept c_const_iterator = requires(T iter) { |
84 | 195 | { *iter } -> std::same_as<const std::remove_cvref_t<decltype(*iter)>&>; |
85 | 196 | }; |
86 | 197 |
|
| 198 | +/// @ingroup GL GL-Traits |
| 199 | +/// @brief Concept checking if a type supports three-way comparison and equality operators. |
| 200 | +/// @tparam T The type to check. |
87 | 201 | template <typename T> |
88 | 202 | concept c_comparable = requires(const T lhs, const T rhs) { |
89 | 203 | { lhs <=> rhs } -> std::convertible_to<std::partial_ordering>; |
90 | 204 | { lhs == rhs } -> std::convertible_to<bool>; |
91 | 205 | }; |
92 | 206 |
|
| 207 | +/// @ingroup GL GL-Traits |
| 208 | +/// @brief Concept checking if a type is an arithmetic type (integral or floating-point). |
| 209 | +/// @tparam T The type to check. |
93 | 210 | template <typename T> |
94 | 211 | concept c_arithmetic = std::is_arithmetic_v<T>; |
95 | 212 |
|
| 213 | +/// @ingroup GL GL-Traits |
| 214 | +/// @brief Concept checking if a type provides a valid `std::numeric_limits<T>::max()` value. |
| 215 | +/// @tparam T The type to check. |
96 | 216 | template <typename T> |
97 | 217 | concept c_has_numeric_limits_max = requires { |
98 | 218 | { std::numeric_limits<T>::max() } -> std::same_as<T>; |
99 | 219 | }; |
100 | 220 |
|
| 221 | +/// @ingroup GL GL-Traits |
| 222 | +/// @brief Concept checking if a type can be extracted from a `std::istream`. |
| 223 | +/// @tparam T The type to check. |
101 | 224 | template <typename T> |
102 | 225 | concept c_readable = requires(T value, std::istream& is) { is >> value; }; |
103 | 226 |
|
| 227 | +/// @ingroup GL GL-Traits |
| 228 | +/// @brief Concept checking if a type can be inserted into a `std::ostream`. |
| 229 | +/// @tparam T The type to check. |
104 | 230 | template <typename T> |
105 | 231 | concept c_writable = requires(T value, std::ostream& os) { os << value; }; |
106 | 232 |
|
| 233 | +/// @ingroup GL GL-Traits |
| 234 | +/// @brief Concept checking if a type is an enumeration. |
| 235 | +/// @tparam T The type to check. |
107 | 236 | template <typename T> |
108 | 237 | concept c_enum = std::is_enum_v<T>; |
109 | 238 |
|
|
0 commit comments