GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
concepts.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_CORE_CONCEPTS_HPP
5#define GREM_CORE_CONCEPTS_HPP
6
8
9#include <iterator> // std::begin, std::cbegin, std::end, std::cend, std::size, std::data, std::...
10#include <type_traits> // std::is_..._v, std::common_reference_t, std::common_type_t, std::add_lvalue_reference_t, std::remove_reference_t, std::false_type, std::true_type
11#include <utility> // std::declval, std::forward
12
13namespace grem {
14
15namespace detail {
16
17template <typename T, template <typename...> typename Template>
18struct is_template_specialization : std::false_type {};
19
20template <template <typename...> typename Template, typename... Args>
21struct is_template_specialization<Template<Args...>, Template> : std::true_type {};
22
23template <typename T, template <typename...> typename Template>
24inline constexpr bool is_template_specialization_v = is_template_specialization<T, Template>::value;
25
26template <template <typename...> typename Template, typename... TemplateArgs>
27consteval void derivedFromTemplateSpecializationTest(const Template<TemplateArgs...>&);
28
29template <template <template <typename...> typename...> typename TemplateTemplate, template <typename...> typename... TemplateTemplateArgs>
30consteval void derivedFromTemplateTemplateSpecializationTest(const TemplateTemplate<TemplateTemplateArgs...>&);
31
32} // namespace detail
33
34template <typename T, typename U>
35concept same_as = std::is_same_v<T, U>;
36
37template <typename T, typename Base>
38concept derived_from = std::is_base_of_v<Base, T> && std::is_convertible_v<const volatile T*, const volatile Base*>;
39
40template <typename T, template <typename...> typename Template>
41concept template_specialization_of = detail::is_template_specialization_v<T, Template>;
42
43template <typename T, template <typename...> typename Template>
44concept derived_from_template_specialization_of = requires(const T t) { detail::derivedFromTemplateSpecializationTest<Template>(t); };
45
46template <typename T, typename U>
47concept convertible_to = std::is_convertible_v<T, U> && requires { static_cast<U>(std::declval<T>()); };
48
49template <typename T, typename U>
51 same_as<std::common_reference_t<T, U>, std::common_reference_t<U, T>> && //
54
55template <typename T, typename U>
56concept common_with =
57 same_as<std::common_type_t<T, U>, std::common_type_t<U, T>> && //
58 requires {
59 static_cast<std::common_type_t<T, U>>(std::declval<T>());
60 static_cast<std::common_type_t<T, U>>(std::declval<U>());
61 } && //
62 common_reference_with<std::add_lvalue_reference_t<const T>, std::add_lvalue_reference_t<const U>> && //
64 std::common_reference_t<std::add_lvalue_reference_t<const T>, std::add_lvalue_reference_t<const U>>>;
65
66template <typename T>
67concept integral = std::is_integral_v<T>;
68
69template <typename T>
71
72template <typename T>
73concept signed_integral = integral<T> && std::is_signed_v<T>;
74
75template <typename T>
76concept strict_signed_integral = strict_integral<T> && std::is_signed_v<T>;
77
78template <typename T>
80
81template <typename T>
83
84template <typename T>
85concept floating_point = std::is_floating_point_v<T>;
86
87template <typename T>
89
90template <typename T>
92
93template <typename T>
94concept enumeration = std::is_enum_v<T>;
95
96template <typename T>
98
99template <typename T>
100concept aggregate = std::is_aggregate_v<T>;
101
102template <typename T>
103concept scalar = std::is_scalar_v<T>;
104
105template <typename T>
106concept pointer = std::is_pointer_v<T>;
107
108template <typename T>
109concept standard_layout = std::is_standard_layout_v<T>;
110
111template <typename T, typename U>
113 std::is_lvalue_reference_v<T> && //
114 common_reference_with<const std::remove_reference_t<T>&, const std::remove_reference_t<U>&> && //
115 requires(T t, U&& u) {
116 { t = std::forward<U>(u) } -> same_as<T>;
117 };
118
119template <typename T>
120concept swappable = []() -> bool {
121 using std::swap;
122 return requires(T& a, T& b) { swap(a, b); };
123}();
124
125template <typename T, typename U>
127 []() -> bool {
128 using std::swap;
129 return requires(T&& t, U&& u) {
130 swap(std::forward<T>(t), std::forward<T>(t));
131 swap(std::forward<U>(u), std::forward<U>(u));
132 swap(std::forward<T>(t), std::forward<U>(u));
133 swap(std::forward<U>(u), std::forward<T>(t));
134 };
135}();
136
137template <typename T>
138concept destructible = std::is_nothrow_destructible_v<T>;
139
140template <typename T, typename... Args>
141concept constructible_from = destructible<T> && std::is_constructible_v<T, Args...>;
142
143template <typename T>
145 T{};
146 ::new T;
147};
148
149template <typename T>
151
152template <typename T>
158
159template <typename A, typename B = A>
160concept equality_comparable = requires(const A a, const B b) { a == b; };
161
162template <typename A, typename B = A>
163concept three_way_comparable = requires(const A a, const B b) { a <=> b; };
164
165template <typename T>
167
168template <typename T>
169concept nothrow_movable = movable<T> && std::is_nothrow_move_constructible_v<T> && std::is_nothrow_move_assignable_v<T> && std::is_nothrow_swappable_v<T>;
170
171template <typename T>
173
174template <typename T>
175concept nothrow_copyable = copyable<T> && nothrow_movable<T> && std::is_nothrow_copy_constructible_v<T> && std::is_nothrow_copy_assignable_v<T>;
176
177template <typename T>
179
180template <typename T>
182
183template <typename T>
184concept trivially_destructible = std::is_trivially_destructible_v<T>;
185
186template <typename T>
187concept trivially_copyable = trivially_destructible<T> && std::is_trivially_copyable_v<T>;
188
189using std::bidirectional_iterator;
190using std::contiguous_iterator;
191using std::forward_iterator;
192using std::incrementable;
193using std::indirectly_readable;
194using std::indirectly_writable;
195using std::input_iterator;
196using std::input_or_output_iterator;
197using std::output_iterator;
198using std::random_access_iterator;
199using std::sentinel_for;
200using std::sized_sentinel_for;
201using std::weakly_incrementable;
202
203using std::iter_common_reference_t;
204using std::iter_difference_t;
205using std::iter_reference_t;
206using std::iter_rvalue_reference_t;
207using std::iter_value_t;
208
209template <typename T>
210using iterator_t = decltype(std::begin(std::declval<T&>()));
211
212template <typename T>
213using const_iterator_t = decltype(std::cbegin(std::declval<T&>()));
214
215template <typename T>
216using sentinel_t = decltype(std::end(std::declval<T&>()));
217
218template <typename T>
219using const_sentinel_t = decltype(std::cend(std::declval<T&>()));
220
221template <typename T>
222using range_value_t = iter_value_t<iterator_t<T>>;
223
224template <typename T>
225using range_reference_t = iter_reference_t<iterator_t<T>>;
226
227template <typename T>
228using range_reference_t = iter_reference_t<iterator_t<T>>;
229
230template <typename T>
231using range_difference_t = iter_difference_t<iterator_t<T>>;
232
233template <typename T>
234using range_rvalue_reference_t = iter_rvalue_reference_t<iterator_t<T>>;
235
236template <typename T>
237using range_common_reference_t = iter_common_reference_t<iterator_t<T>>;
238
239template <typename T>
240using range_size_t = decltype(std::size(std::declval<T&>()));
241
242template <typename T>
243concept range = requires(T t) {
244 std::begin(t);
245 std::end(t);
246};
247
248template <typename T>
249concept input_range = range<T> && input_iterator<iterator_t<T>>;
250
251template <typename T, typename V>
252concept output_range = range<T> && output_iterator<iterator_t<T>, V>;
253
254template <typename T>
255concept forward_range = input_range<T> && forward_iterator<iterator_t<T>>;
256
257template <typename T>
258concept bidirectional_range = forward_range<T> && bidirectional_iterator<iterator_t<T>>;
259
260template <typename T>
261concept random_access_range = bidirectional_range<T> && random_access_iterator<iterator_t<T>>;
262
263template <typename T>
264concept contiguous_range = random_access_range<T> && contiguous_iterator<iterator_t<T>> && requires(T t) { std::data(t); };
265
266template <typename T>
268
269template <typename T>
270concept sized_range = range<T> && requires(T t) { std::size(t); };
271
272} // namespace grem
273
274#endif
Definition concepts.hpp:100
Definition concepts.hpp:88
Definition concepts.hpp:112
Definition concepts.hpp:258
Definition concepts.hpp:267
Definition concepts.hpp:50
Definition concepts.hpp:56
Definition concepts.hpp:141
Definition concepts.hpp:264
Definition concepts.hpp:47
Definition concepts.hpp:153
Definition concepts.hpp:172
Definition concepts.hpp:144
Definition concepts.hpp:38
Definition concepts.hpp:138
Definition concepts.hpp:94
Definition concepts.hpp:160
Definition concepts.hpp:85
Definition concepts.hpp:255
Definition concepts.hpp:249
Definition concepts.hpp:67
Definition concepts.hpp:166
Definition concepts.hpp:150
Definition concepts.hpp:175
Definition concepts.hpp:169
Definition concepts.hpp:252
Definition concepts.hpp:106
Definition concepts.hpp:261
Definition concepts.hpp:243
Definition concepts.hpp:181
Definition concepts.hpp:35
Definition concepts.hpp:103
Definition concepts.hpp:97
Definition concepts.hpp:178
Definition concepts.hpp:73
Definition concepts.hpp:270
Definition concepts.hpp:109
Definition concepts.hpp:91
Definition concepts.hpp:70
Definition concepts.hpp:76
Definition concepts.hpp:82
Definition concepts.hpp:126
Definition concepts.hpp:120
Definition concepts.hpp:41
Definition concepts.hpp:163
Definition concepts.hpp:187
Definition concepts.hpp:184
Definition concepts.hpp:79
@ T
't'
Definition keyboard.hpp:90
Definition aliases.hpp:9
decltype(std::end(std::declval< T & >())) sentinel_t
Definition concepts.hpp:216
decltype(std::begin(std::declval< T & >())) iterator_t
Definition concepts.hpp:210
iter_rvalue_reference_t< iterator_t< T > > range_rvalue_reference_t
Definition concepts.hpp:234
decltype(std::size(std::declval< T & >())) range_size_t
Definition concepts.hpp:240
iter_value_t< iterator_t< T > > range_value_t
Definition concepts.hpp:222
iter_common_reference_t< iterator_t< T > > range_common_reference_t
Definition concepts.hpp:237
iter_difference_t< iterator_t< T > > range_difference_t
Definition concepts.hpp:231
decltype(std::cbegin(std::declval< T & >())) const_iterator_t
Definition concepts.hpp:213
iter_reference_t< iterator_t< T > > range_reference_t
Definition concepts.hpp:225
decltype(std::cend(std::declval< T & >())) const_sentinel_t
Definition concepts.hpp:219