Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
template_helpers.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
15#include "type_traits.h"
16#include <type_traits>
17#include <utility>
18#include <tuple>
19#include <functional>
20
21namespace kcenon::thread::detail {
22
26 template<typename T, typename = void>
27 struct has_priority_method : std::false_type {};
28
29 template<typename T>
30 struct has_priority_method<T, std::void_t<decltype(std::declval<T>().priority())>>
31 : std::true_type {};
32
33 template<typename T>
35
39 template<typename T, typename = void>
40 struct has_do_work_method : std::false_type {};
41
42 template<typename T>
43 struct has_do_work_method<T, std::void_t<decltype(std::declval<T>().do_work())>>
44 : std::true_type {};
45
46 template<typename T>
48
52 template<typename Job>
54 using type = void; // Default for non-job types
55 };
56
57 // Specialization for typed jobs
58 template<typename T>
60 using type = std::conditional_t<
62 decltype(std::declval<T>().priority()),
63 void
64 >;
65 };
66
67 template<typename Job>
69
73 template<typename Job, typename ExpectedJobType>
74 struct is_compatible_job : std::false_type {};
75
76 template<typename Job, typename ExpectedJobType>
77 struct is_compatible_job<Job, ExpectedJobType> {
78 static constexpr bool value =
81 std::is_same_v<job_type_t<Job>, ExpectedJobType>;
82 };
83
84 template<typename Job, typename ExpectedJobType>
86
90 template<typename F>
91 struct function_traits;
92
93 // Specialization for function pointers
94 template<typename R, typename... Args>
95 struct function_traits<R(*)(Args...)> {
96 using return_type = R;
97 using argument_types = std::tuple<Args...>;
98 static constexpr size_t arity = sizeof...(Args);
99 };
100
101 // Specialization for member function pointers
102 template<typename C, typename R, typename... Args>
103 struct function_traits<R(C::*)(Args...)> {
104 using return_type = R;
105 using class_type = C;
106 using argument_types = std::tuple<Args...>;
107 static constexpr size_t arity = sizeof...(Args);
108 };
109
110 // Specialization for const member function pointers
111 template<typename C, typename R, typename... Args>
112 struct function_traits<R(C::*)(Args...) const> {
113 using return_type = R;
114 using class_type = C;
115 using argument_types = std::tuple<Args...>;
116 static constexpr size_t arity = sizeof...(Args);
117 };
118
119 // Specialization for function objects and lambdas
120 template<typename F>
121 struct function_traits : function_traits<decltype(&F::operator())> {};
122
126 template<size_t N>
128 char data[N];
129
130 constexpr constexpr_string(const char (&str)[N]) {
131 for (size_t i = 0; i < N; ++i) {
132 data[i] = str[i];
133 }
134 }
135
136 constexpr operator const char*() const { return data; }
137 constexpr size_t size() const { return N - 1; } // Exclude null terminator
138 };
139
143 template<typename F>
144 constexpr auto generate_job_name() {
145 // This would ideally use compile-time type name extraction
146 // For now, return a generic name
147 return "generated_job";
148 }
149
153 template<JobType T, typename Comparator = std::less<>>
155 Comparator comp;
156
157 constexpr priority_comparator(Comparator c = {}) : comp(c) {}
158
159 constexpr bool operator()(T lhs, T rhs) const {
160 using traits = job_type_traits<T>;
161 return comp(traits::to_underlying(lhs), traits::to_underlying(rhs));
162 }
163 };
164
168 template<bool Condition>
170 template<typename T>
171 using type = std::conditional_t<Condition, T, void>;
172 };
173
177 template<JobType... Types>
179 static constexpr size_t size = sizeof...(Types);
180
181 template<size_t Index>
182 using at = std::tuple_element_t<Index, std::tuple<std::integral_constant<Types, Types>...>>;
183
184 static constexpr std::array<std::common_type_t<Types...>, size> values = {Types...};
185 };
186
191 public:
192 template<typename Job>
194 : vtable_(&vtable_for<std::decay_t<Job>>)
195 , storage_(std::forward<Job>(job)) {}
196
197 void execute() {
199 }
200
201 void destroy() {
203 }
204
205 private:
206 struct vtable_t {
207 void (*execute)(void*);
208 void (*destroy)(void*);
209 };
210
211 template<typename Job>
212 static constexpr vtable_t vtable_for = {
213 [](void* ptr) { static_cast<Job*>(ptr)->do_work(); },
214 [](void* ptr) { static_cast<Job*>(ptr)->~Job(); }
215 };
216
218 alignas(std::max_align_t) char storage_[64]; // Adjust size as needed
219 };
220
224 template<typename JobType, typename... Args>
225 constexpr auto make_job_args(Args&&... args) {
226 return std::forward_as_tuple(std::forward<Args>(args)...);
227 }
228
232 template<typename Job, JobType ExpectedType>
234 static_assert(has_priority_method_v<Job>,
235 "Job type must have a priority() method");
236 static_assert(has_do_work_method_v<Job>,
237 "Job type must have a do_work() method");
238 static_assert(std::is_same_v<job_type_t<Job>, ExpectedType>,
239 "Job priority type must match expected type");
240
241 static constexpr bool value = true;
242 };
243
244} // namespace kcenon::thread::detail
Type erasure helper for heterogeneous job storage.
static constexpr vtable_t vtable_for
Represents a unit of work (task) to be executed, typically by a job queue.
Definition job.h:136
typename job_type_extractor< Job >::type job_type_t
constexpr bool is_compatible_job_v
constexpr bool has_priority_method_v
constexpr bool has_do_work_method_v
constexpr auto generate_job_name()
Template for automatic job naming based on function signature.
constexpr auto make_job_args(Args &&... args)
Perfect forwarding helper for job construction.
STL namespace.
Template for conditional compilation based on feature flags.
std::conditional_t< Condition, T, void > type
Constexpr string utilities for compile-time job naming.
constexpr constexpr_string(const char(&str)[N])
Type trait for function signature analysis.
SFINAE helper to detect if a type has a do_work method.
SFINAE helper to detect if a type has a specific member function.
std::conditional_t< has_priority_method_v< T >, decltype(std::declval< T >().priority()), void > type
Template to extract job type from job classes.
Variadic template helper for job type lists.
static constexpr std::array< std::common_type_t< Types... >, size > values
std::tuple_element_t< Index, std::tuple< std::integral_constant< Types, Types >... > > at
Type traits for job types.
Definition type_traits.h:36
Compile-time job validation with detailed error messages.
Priority comparison helper with custom comparison functions.
constexpr bool operator()(T lhs, T rhs) const
Type traits for typed_thread_pool module.