Embedded Template Library 1.0
Loading...
Searching...
No Matches
concepts.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2025 BMW AG
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_CONCEPTS_INCLUDED
32#define ETL_CONCEPTS_INCLUDED
33
34#include "platform.h"
35
36#include "invoke.h"
37#include "type_traits.h"
38#include "utility.h"
39
40#if ETL_USING_CPP20
41
42 #if ETL_USING_STL
43 #include <concepts>
44 #endif
45
46namespace etl
47{
48
49 #if ETL_USING_STL
50
51 using std::assignable_from;
52 using std::common_reference_with;
53 using std::common_with;
54 using std::constructible_from;
55 using std::convertible_to;
56 using std::copy_constructible;
57 using std::copyable;
58 using std::default_initializable;
59 using std::derived_from;
60 using std::destructible;
61 using std::equality_comparable;
62 using std::equivalence_relation;
63 using std::floating_point;
64 using std::integral;
65 using std::invocable;
66 using std::movable;
67 using std::move_constructible;
68 using std::predicate;
69 using std::regular;
70 using std::regular_invocable;
71 using std::relation;
72 using std::same_as;
73 using std::semiregular;
74 using std::signed_integral;
75 using std::strict_weak_order;
76 using std::swappable;
77 using std::swappable_with;
78 using std::totally_ordered;
79 using std::unsigned_integral;
80
81 #else // not ETL_USING_STL
82
83 namespace private_concepts
84 {
85 template <typename T, typename U>
86 concept same_as_helper = etl::is_same_v<T, U>;
87 }
88
89 //***************************************************************************
90 template <typename T, typename U>
91 concept same_as = private_concepts::same_as_helper<T, U> && private_concepts::same_as_helper<U, T>;
92
93 //***************************************************************************
94 template <typename Derived, typename Base>
95 concept derived_from = etl::is_base_of_v<Base, Derived> && etl::is_convertible_v<const volatile Derived*, const volatile Base*>;
96
97 //***************************************************************************
98 template <typename From, typename To>
99 concept convertible_to = etl::is_convertible_v<From, To> && requires { static_cast<To>(etl::declval<From>()); };
100
101 //***************************************************************************
102 template < class T, typename U >
103 concept common_reference_with = etl::same_as<etl::common_reference_t<T, U>, etl::common_reference_t<U, T> >
104 && etl::convertible_to<T, etl::common_reference_t<T, U> > && etl::convertible_to<U, etl::common_reference_t<T, U> >;
105
106 //***************************************************************************
107 template <typename T, typename U>
108 concept common_with = etl::same_as<etl::common_type_t<T, U>, etl::common_type_t<U, T> > && requires {
109 static_cast<etl::common_type_t<T, U> >(etl::declval<T>());
110 static_cast<etl::common_type_t<T, U> >(etl::declval<U>());
111 } && etl::common_reference_with< etl::add_lvalue_reference_t<const T>, etl::add_lvalue_reference_t<const U> > && etl::common_reference_with< etl::add_lvalue_reference_t<etl::common_type_t<T, U> >, etl::common_reference_t< etl::add_lvalue_reference_t<const T>, etl::add_lvalue_reference_t<const U> > >;
112
113 //***************************************************************************
114 template <typename T>
115 concept integral = etl::is_integral_v<T>;
116
117 //***************************************************************************
118 template <typename T>
119 concept signed_integral = etl::integral<T> && etl::is_signed_v<T>;
120
121 //***************************************************************************
122 template <typename T>
123 concept unsigned_integral = etl::integral<T> && !etl::signed_integral<T>;
124
125 //***************************************************************************
126 template <typename T>
127 concept floating_point = etl::is_floating_point_v<T>;
128
129 //***************************************************************************
130 template <typename LHS, typename RHS>
131 concept assignable_from =
132 etl::is_lvalue_reference_v<LHS> && etl::common_reference_with< const etl::remove_reference_t<LHS>&, const etl::remove_reference_t<RHS>&>
133 && requires(LHS lhs, RHS&& rhs) {
134 { lhs = etl::forward<RHS>(rhs) } -> etl::same_as<LHS>;
135 };
136
137 //***************************************************************************
138 template <typename F, typename... Args>
139 concept invocable = etl::is_invocable_v<F, Args...>;
140
141 //***************************************************************************
142 template <typename F, typename... Args>
143 concept regular_invocable = etl::invocable<F, Args...>;
144
145 //***************************************************************************
146 template <typename T>
147 concept destructible = requires(T& t) {
148 { t.~T() } noexcept;
149 };
150
151 //***************************************************************************
152 template <typename T, typename... Args>
153 concept constructible_from = etl::destructible<T> && etl::is_constructible_v<T, Args...>;
154
155 //***************************************************************************
156 template <typename T>
157 concept default_initializable = etl::constructible_from<T> && requires {
158 T{};
159 ::new T;
160 };
161
162 //***************************************************************************
163 template <typename T>
164 concept move_constructible = etl::constructible_from<T, T> && etl::convertible_to<T, T>;
165
166 //***************************************************************************
167 template <typename T>
168 concept copy_constructible =
169 etl::move_constructible<T> && etl::constructible_from<T, T&> && etl::convertible_to<T&, T> && etl::constructible_from<T, const T&>
170 && etl::convertible_to<const T&, T> && etl::constructible_from<T, const T> && etl::convertible_to<const T, T>;
171
172 //***************************************************************************
173 namespace private_concepts
174 {
175 template <typename T>
176 concept boolean_testable = etl::convertible_to<T, bool> && requires(T&& t) {
177 { !etl::forward<T>(t) } -> etl::convertible_to<bool>;
178 };
179 } // namespace private_concepts
180
181 //***************************************************************************
182 template <typename T>
183 concept equality_comparable = requires(const etl::remove_reference_t<T>& a, const etl::remove_reference_t<T>& b) {
184 { a == b } -> private_concepts::boolean_testable;
185 { a != b } -> private_concepts::boolean_testable;
186 };
187
188 //***************************************************************************
189 template <typename T>
190 concept totally_ordered = etl::equality_comparable<T> && requires(const etl::remove_reference_t<T>& a, const etl::remove_reference_t<T>& b) {
191 { a < b } -> private_concepts::boolean_testable;
192 { a > b } -> private_concepts::boolean_testable;
193 { a <= b } -> private_concepts::boolean_testable;
194 { a >= b } -> private_concepts::boolean_testable;
195 };
196
197 //***************************************************************************
198 template <typename T>
199 concept swappable = requires(T& a, T& b) { etl::swap(a, b); };
200
201 //***************************************************************************
202 template <typename T, typename U>
203 concept swappable_with = etl::common_reference_with<etl::remove_reference_t<T>&, etl::remove_reference_t<U>&> && requires(T&& t, U&& u) {
204 etl::swap(etl::forward<T>(t), etl::forward<T>(t));
205 etl::swap(etl::forward<U>(u), etl::forward<U>(u));
206 etl::swap(etl::forward<T>(t), etl::forward<U>(u));
207 etl::swap(etl::forward<U>(u), etl::forward<T>(t));
208 };
209
210 //***************************************************************************
211 template <typename T>
212 concept movable = etl::is_object_v<T> && etl::move_constructible<T> && etl::assignable_from<T&, T> && etl::swappable<T>;
213
214 //***************************************************************************
215 template <typename T>
216 concept copyable = etl::copy_constructible<T> && etl::movable<T> && etl::assignable_from<T&, T&> && etl::assignable_from<T&, const T&>
217 && etl::assignable_from<T&, const T>;
218
219 //***************************************************************************
220 template <typename T>
221 concept semiregular = etl::copyable<T> && etl::default_initializable<T>;
222
223 //***************************************************************************
224 template <typename T>
225 concept regular = etl::semiregular<T> && etl::equality_comparable<T>;
226
227 //***************************************************************************
228 template <typename F, typename... Args>
229 concept predicate = etl::regular_invocable<F, Args...> && private_concepts::boolean_testable<etl::invoke_result_t<F, Args...> >;
230
231 //***************************************************************************
232 template <typename R, typename T, typename U>
233 concept relation = etl::predicate<R, T, T> && etl::predicate<R, U, U> && etl::predicate<R, T, U> && etl::predicate<R, U, T>;
234
235 //***************************************************************************
236 template <typename R, typename T, typename U>
237 concept equivalence_relation = etl::relation<R, T, U>;
238
239 //***************************************************************************
240 template <typename R, typename T, typename U>
241 concept strict_weak_order = etl::relation<R, T, U>;
242
243 #endif
244} // namespace etl
245#endif
246#endif
Definition absolute.h:40
ETL_CONSTEXPR14 void swap(etl::typed_storage_ext< T > &lhs, etl::typed_storage_ext< T > &rhs) ETL_NOEXCEPT
Swap two etl::typed_storage_ext.
Definition alignment.h:856