ThreadSchedule 1.0.0
Modern C++ thread management library
Loading...
Searching...
No Matches
concepts.hpp
Go to the documentation of this file.
1#pragma once
2
15
16#include <chrono>
17#include <functional>
18#include <set>
19#include <thread>
20#include <type_traits>
21#include <vector>
22
23namespace threadschedule
24{
25
36template <typename T, typename = void>
37struct is_duration_impl : std::false_type
38{
39};
40
42template <typename T>
43struct is_duration_impl<T, std::void_t<typename T::rep, typename T::period>> : std::true_type
44{
45};
46
47// C++20 concepts (with constexpr-bool fallbacks for older compilers)
48#if __cpp_concepts >= 201907L
49
56template <typename F, typename... Args>
57concept ThreadCallable = std::is_invocable_v<F, Args...>;
58
65template <typename T>
66concept ThreadIdentifiable = requires(T t) {
67 { t.get_id() } -> std::convertible_to<std::thread::id>;
68};
69
76template <typename T>
77concept Duration = requires {
78 typename T::rep;
79 typename T::period;
80};
81
86template <typename T>
87concept PriorityType = std::is_integral_v<T>;
88
93template <typename T>
94concept CPUSetType = requires(T t) {
95 { t.size() } -> std::convertible_to<std::size_t>;
96 { t.begin() };
97 { t.end() };
98};
99
100#else
101
106template <typename F, typename... Args>
107constexpr bool ThreadCallable = std::is_invocable_v<F, Args...>;
108
110template <typename T>
111constexpr bool ThreadIdentifiable = std::is_same_v<decltype(std::declval<T>().get_id()), std::thread::id>;
112
114template <typename T>
116
118template <typename T>
119constexpr bool PriorityType = std::is_integral_v<T>;
120
122template <typename T>
123constexpr bool CPUSetType = std::is_same_v<T, std::vector<int>> || std::is_same_v<T, std::set<int>>;
124
125#endif
126
143template <typename T>
144struct is_thread_like : std::false_type
145{
146};
147
149template <>
150struct is_thread_like<std::thread> : std::true_type
151{
152};
153
154#if __cplusplus >= 202002L || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L)
156template <>
157struct is_thread_like<std::jthread> : std::true_type
158{
159};
160#endif
161
163template <typename T>
165
169template <typename T>
170using enable_if_thread_callable_t = std::enable_if_t<std::is_invocable_v<T>, int>;
171
172template <typename T>
173using enable_if_duration_t = std::enable_if_t<is_duration_impl<T>::value, int>;
174
175} // namespace threadschedule
constexpr bool ThreadIdentifiable
Pre-C++20 fallback for ThreadIdentifiable (constexpr bool).
Definition concepts.hpp:111
constexpr bool is_thread_like_v
Convenience variable template for is_thread_like.
Definition concepts.hpp:164
constexpr bool ThreadCallable
Pre-C++20 fallback for ThreadCallable (constexpr bool).
Definition concepts.hpp:107
constexpr bool PriorityType
Pre-C++20 fallback for PriorityType (constexpr bool).
Definition concepts.hpp:119
std::enable_if_t< std::is_invocable_v< T >, int > enable_if_thread_callable_t
Helper type traits for thread operations.
Definition concepts.hpp:170
constexpr bool CPUSetType
Pre-C++20 fallback for CPUSetType (constexpr bool).
Definition concepts.hpp:123
constexpr bool Duration
Pre-C++20 fallback for Duration (constexpr bool).
Definition concepts.hpp:115
SFINAE trait that detects std::chrono::duration types.
Definition concepts.hpp:38
Type trait that identifies thread-like types.
Definition concepts.hpp:145