MT Core (C++)
Core library for replacing C++ standard in project usage
Loading...
Searching...
No Matches
thread/mtcore_thread/traits.hpp
Go to the documentation of this file.
1/*
2
3Copyright 2025 Matthew Tolman
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16
17*/
18
19#ifndef MTCORE_THREAD_TRAITS_HPP
20#define MTCORE_THREAD_TRAITS_HPP
21
25#include <chrono>
26#include <concepts>
27
28namespace mtcore::thread {
29 enum class SelectOptionErrors;
30}
31
32namespace mtcore::thread {
38 template<typename T>
39 concept SelectOption = requires(T &t, const T &ct) {
40 { t.attempt() } -> std::same_as<Result<void, SelectOptionErrors>>;
41 { ct.prevents_deadlock() } -> std::same_as<bool>;
42 };
43
48 template<typename T>
49 concept Closeable = requires(T &t) {
50 { t.close() } -> std::same_as<void>;
51 };
52
58 template<typename T>
59 concept Sender = requires(T &t, const typename T::Message &m) {
60 { t.try_send(m) } -> ResultLikeWithVoid;
61 };
62
68 template<typename T>
69 concept Receiver = requires(T &t) {
71 };
72
77 template<typename T>
78 concept SenderBlock = requires(T &t) {
79 { t.send_block({}) } -> ResultLikeWithVoid;
80 };
81
86 template<typename T>
87 concept ReceiverBlock = requires(T &t) {
88 { t.receive_block() } -> std::same_as<Optional<typename T::Message>>;
89 };
90
95 template<typename T>
96 concept SenderBefore = requires(T &t, const std::chrono::time_point<std::chrono::system_clock> &sc) {
97 { t.send_before({}, sc) } -> ResultLikeWithVoid;
98 };
99
104 template<typename T>
105 concept ReceiverBefore = requires(T &t, const std::chrono::time_point<std::chrono::system_clock> &sc) {
106 { t.receive_before(sc) } -> ResultLikeWithValue<Optional<typename T::Message>>;
107 };
108
115 template<typename T>
116 concept SenderGrow = requires(T &t, Allocator &a) {
117 { t.try_send(a, {}) } -> ResultLikeWithVoid;
118 } && Sender<T>;
119
126 template<typename T>
127 concept SenderBlockGrow = requires(T &t, Allocator &a) {
128 { t.send_block(a, {}) } -> ResultLikeWithVoid;
129 } && SenderBlock<T>;
130
137 template<typename T>
139 requires(T &t, const std::chrono::time_point<std::chrono::system_clock> &sc, Allocator &a) {
140 { t.send_before(a, {}, sc) } -> ResultLikeWithVoid;
141 } && SenderBefore<T>;
142
147 template<typename T>
148 concept HasClosedError = requires {
149 { T::CHANNEL_CLOSED };
150 };
151
152#define MTCORE_THREAD_TRAIT_ISA(Concept) \
153 namespace impl { \
154 template<typename T> \
155 struct Is##Concept; \
156 template<Concept T> \
157 struct Is##Concept<T> : std::true_type {}; \
158 template<typename T> \
159 struct Is##Concept : std::false_type {}; \
160 }
161
166 template<typename T>
168
173 template<typename T>
175
180 template<typename T>
182
187 template<typename T>
189
194 template<typename T>
196
201 template<typename T>
203
213 template<typename T>
215
228 template<typename T>
231
232 MTCORE_THREAD_TRAIT_ISA(Sender);
233 MTCORE_THREAD_TRAIT_ISA(SenderGrow);
234 MTCORE_THREAD_TRAIT_ISA(SenderBlock);
235 MTCORE_THREAD_TRAIT_ISA(SenderBlockGrow);
236 MTCORE_THREAD_TRAIT_ISA(SenderBefore);
237 MTCORE_THREAD_TRAIT_ISA(SenderBeforeGrow);
238 MTCORE_THREAD_TRAIT_ISA(Receiver);
239 MTCORE_THREAD_TRAIT_ISA(ReceiverBlock);
240 MTCORE_THREAD_TRAIT_ISA(ReceiverBefore);
241 MTCORE_THREAD_TRAIT_ISA(Closeable);
242 MTCORE_THREAD_TRAIT_ISA(SenderReceiver);
243 MTCORE_THREAD_TRAIT_ISA(SenderReceiverBlock);
244 MTCORE_THREAD_TRAIT_ISA(SenderReceiverBefore);
245 MTCORE_THREAD_TRAIT_ISA(SenderReceiverGrow);
246 MTCORE_THREAD_TRAIT_ISA(SenderReceiverBlockGrow);
247 MTCORE_THREAD_TRAIT_ISA(SenderReceiverBeforeGrow);
248 MTCORE_THREAD_TRAIT_ISA(ChannelLike);
249 MTCORE_THREAD_TRAIT_ISA(ChannelLikeGrow);
250 MTCORE_THREAD_TRAIT_ISA(SelectOption);
251 MTCORE_THREAD_TRAIT_ISA(HasClosedError);
252
258 template<typename T>
259 constexpr bool has_closed_error = impl::IsHasClosedError<T>::value;
260
266 template<typename T>
267 constexpr bool is_closeable = impl::IsCloseable<T>::value;
268
274 template<typename T>
275 constexpr bool is_sender = impl::IsSender<T>::value;
276
282 template<typename T>
283 constexpr bool is_sender_grow = impl::IsSenderGrow<T>::value;
284
290 template<typename T>
291 constexpr bool is_sender_block = impl::IsSenderBlock<T>::value;
292
298 template<typename T>
299 constexpr bool is_sender_block_grow = impl::IsSenderBlockGrow<T>::value;
300
306 template<typename T>
307 constexpr bool is_sender_before = impl::IsSenderBefore<T>::value;
308
314 template<typename T>
315 constexpr bool is_sender_before_grow = impl::IsSenderBeforeGrow<T>::value;
316
322 template<typename T>
323 constexpr bool is_receiver = impl::IsReceiver<T>::value;
324
330 template<typename T>
331 constexpr bool is_receiver_block = impl::IsReceiverBlock<T>::value;
332
338 template<typename T>
339 constexpr bool is_receiver_before = impl::IsReceiverBefore<T>::value;
340
346 template<typename T>
347 constexpr bool is_sender_receiver = impl::IsSenderReceiver<T>::value;
348
354 template<typename T>
355 constexpr bool is_sender_receiver_grow = impl::IsSenderReceiverGrow<T>::value;
356
362 template<typename T>
363 constexpr bool is_sender_receiver_block = impl::IsSenderReceiverBlock<T>::value;
364
370 template<typename T>
371 constexpr bool is_sender_receiver_block_grow = impl::IsSenderReceiverBlockGrow<T>::value;
372
378 template<typename T>
379 constexpr bool is_sender_receiver_before = impl::IsSenderReceiverBefore<T>::value;
380
386 template<typename T>
387 constexpr bool is_sender_receiver_before_grow = impl::IsSenderReceiverBeforeGrow<T>::value;
388
394 template<typename T>
395 constexpr bool is_channel_like = impl::IsChannelLike<T>::value;
396
402 template<typename T>
403 constexpr bool is_channel_like_grow = impl::IsChannelLikeGrow<T>::value;
404
410 template<typename T>
411 constexpr bool is_select_option = impl::IsSelectOption<T>::value;
412} // namespace mtcore::thread
413
414#endif // MTCORE_THREAD_TRAITS_HPP
Represents a type that fulfills the "result" contract while having a success value of a specific type...
Represents a type that fulfills the "result" contract while having no success value Does NOT check th...
Checks if something qualifies as a growing channel Growing channels can:
Checks if something qualifies as a channel Channels can:
Checks if something is closeable (e.g.
Checks if an error type has a "Has Closed" error called CHANNEL_CLOSED.
Checks if messages can be received in a blocking manner with a timeout.
Checks if messages can be received in a blocking manner.
Checks if messages can be received from something (e.g.
Checks if something qualifies as an option to a select statement Allows defining custom options and c...
Checks if messages can be sent to something (e.g.
Checks if messages can be sent in a blocking manner with a timeout.
Checks if messages can be sent to something (e.g.
Checks if messages can be sent in a blocking manner.
Checks if messages can be sent to something (e.g.
Checks if blocking with timeout sends and receives are supported with allocation to grow.
Checks if blocking with timeout sends and receives are supported.
Checks if blocking sends and receives are supported with allocation to grow.
Checks if blocking sends and receives are supported.
Checks if non-blocking sends and receives are supported with allocation to grow.
Checks if non-blocking sends and receives are supported.
Checks if messages can be sent to something (e.g.
SelectOptionErrors
Errors indicating control flow from a select option ATTEMPT_FAILED indicates to try the next option S...
Definition select.hpp:42
constexpr bool is_sender_receiver_block
Checks if a type can be sent to and received from (blocking)
constexpr bool is_sender_receiver_block_grow
Checks if a type can be sent to and received from (blocking) with growing send.
constexpr bool is_channel_like
Checks if a type is channel-like.
constexpr bool is_receiver_block
Checks if a type can be received from (blocking)
constexpr bool is_sender_receiver_grow
Checks if a type can be sent to and received from with growing send.
constexpr bool is_sender_before_grow
Checks if a type can be sent to (blocking, timeout) with growing.
constexpr bool is_sender_receiver
Checks if a type can be sent to and received from.
constexpr bool is_channel_like_grow
Checks if a type is growing channel-like.
constexpr bool is_receiver
Checks if a type can be received from.
constexpr bool is_select_option
Checks if a type can be used in a select statement as an option.
constexpr bool is_sender_receiver_before_grow
Checks if a type can be sent to and received from (blocking, timeout) with growing send.
constexpr bool is_sender
Checks if a type can be sent to.
constexpr bool has_closed_error
Checks if an error type has a recognized closed error.
constexpr bool is_sender_before
Checks if a type can be sent to (blocking, timeout)
constexpr bool is_sender_receiver_before
Checks if a type can be sent to and received from (blocking, timeout)
constexpr bool is_sender_block
Checks if a type can be sent to (blocking)
constexpr bool is_sender_block_grow
Checks if a type can be sent to (blocking) with growing.
constexpr bool is_closeable
Checks if a type is closeable.
constexpr bool is_receiver_before
Checks if a type can be received from (blocking, timeout)
constexpr bool is_sender_grow
Checks if a type can be sent to with growing.
Thread-related namespace The methods and classes provided by this class are thread-safe Classes and m...
Represents a memory allocator Exact behavior depends on the underlying VTable used Should use the a_*...