Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
type_traits.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
17
18#include <type_traits>
19#include <string>
20
21namespace kcenon::thread::detail {
22
23 // JobType and JobCallable concepts are now imported from thread_concepts.h
24 // via the using declarations in that header's detail namespace section
25
31#ifdef USE_STD_CONCEPTS
32 template<JobType T>
33#else
34 template<typename T, typename = std::enable_if_t<JobType<T>>>
35#endif
37 using type = T;
38 using underlying_type = std::conditional_t<std::is_enum_v<T>,
39 std::underlying_type_t<T>,
40 T>;
41
42 static constexpr bool is_enum = std::is_enum_v<T>;
43 static constexpr bool is_integral = std::is_integral_v<T>;
44 static constexpr bool has_ordering = true;
45 static constexpr bool is_signed = std::is_signed_v<underlying_type>;
46
50 static constexpr underlying_type to_underlying(T value) noexcept {
51 if constexpr (is_enum) {
52 return static_cast<underlying_type>(value);
53 } else {
54 return value;
55 }
56 }
57
61 static constexpr T from_underlying(underlying_type value) noexcept {
62 if constexpr (is_enum) {
63 return static_cast<T>(value);
64 } else {
65 return value;
66 }
67 }
68 };
69
73 template<typename T>
74 constexpr bool can_compare_priority() {
75 if constexpr (JobType<T>) {
76 using traits = job_type_traits<T>;
77 return traits::has_ordering;
78 }
79 return false;
80 }
81
85#ifdef USE_STD_CONCEPTS
86 template<JobType T>
87#else
88 template<typename T, typename = std::enable_if_t<JobType<T>>>
89#endif
90 constexpr bool higher_priority(T lhs, T rhs) noexcept {
91 using traits = job_type_traits<T>;
92
93 // Assume lower numerical values = higher priority
94 return traits::to_underlying(lhs) < traits::to_underlying(rhs);
95 }
96
100#ifdef USE_STD_CONCEPTS
101 template<JobType T>
103#else
104 template<typename T, typename = std::enable_if_t<JobType<T>>>
106#endif
107
108} // namespace kcenon::thread::detail
constexpr bool can_compare_priority()
Helper to determine if a type can be used as a job priority.
Definition type_traits.h:74
typename job_type_traits< T, void >::underlying_type job_underlying_t
Type alias for job type conversion.
constexpr bool higher_priority(T lhs, T rhs) noexcept
Compile-time priority comparison.
Definition type_traits.h:90
Type traits for job types.
Definition type_traits.h:36
static constexpr T from_underlying(underlying_type value) noexcept
Creates job type from underlying representation.
Definition type_traits.h:61
std::conditional_t< std::is_enum_v< T >, std::underlying_type_t< T >, T > underlying_type
Definition type_traits.h:38
static constexpr underlying_type to_underlying(T value) noexcept
Converts job type to its underlying representation.
Definition type_traits.h:50
C++20 Concepts for thread_system.