ThreadSchedule 3.0.0
Modern C++ thread management library
Loading...
Searching...
No Matches
thread_id.hpp
Go to the documentation of this file.
1#pragma once
2
8#include "result.hpp"
9
10#include <cstdint>
11#include <functional>
12#include <stdexcept>
13
14namespace threadschedule
15{
16namespace detail
17{
18struct thread_id_access;
19}
20
22{
23public:
29 constexpr explicit thread_id(std::int64_t value) : value_(checked(value)) {}
30
36 [[nodiscard]] static auto
37 create(std::int64_t value) noexcept -> result<thread_id>
38 {
39 if (value <= 0)
40 return unexpected(std::make_error_code(std::errc::invalid_argument));
41 return thread_id(unchecked_tag{}, static_cast<std::uint64_t>(value));
42 }
43
44 friend constexpr auto
45 operator==(thread_id lhs, thread_id rhs) noexcept -> bool
46 {
47 return lhs.value_ == rhs.value_;
48 }
49 friend constexpr auto
50 operator!=(thread_id lhs, thread_id rhs) noexcept -> bool
51 {
52 return !(lhs == rhs);
53 }
54 friend constexpr auto
55 operator<(thread_id lhs, thread_id rhs) noexcept -> bool
56 {
57 return lhs.value_ < rhs.value_;
58 }
59
60private:
62 struct unchecked_tag
63 {
64 };
65
66 constexpr thread_id(unchecked_tag, std::uint64_t value) noexcept : value_(value) {}
67
68 [[nodiscard]] static constexpr auto
69 checked(std::int64_t value) -> std::uint64_t
70 {
71 if (value <= 0)
72 throw std::invalid_argument("thread_id must be positive");
73 return static_cast<std::uint64_t>(value);
74 }
75
76 std::uint64_t value_;
77};
78
79namespace detail
80{
82{
83 [[nodiscard]] static constexpr auto
84 make(std::uint64_t value) -> thread_id
85 {
86 if (value == 0)
87 throw std::invalid_argument("thread_id must be positive");
88 return thread_id(thread_id::unchecked_tag{}, value);
89 }
90 [[nodiscard]] static constexpr auto
91 value(thread_id id) noexcept -> std::uint64_t
92 {
93 return id.value_;
94 }
95};
96} // namespace detail
97
98} // namespace threadschedule
99
100namespace std
101{
102template <>
103struct hash<threadschedule::thread_id>
104{
105 auto
106 operator()(threadschedule::thread_id value) const noexcept -> size_t
107 {
108 return hash<std::uint64_t>{}(threadschedule::detail::thread_id_access::value(value));
109 }
110};
111} // namespace std
static auto create(std::int64_t value) noexcept -> result< thread_id >
Construct a thread id without exceptions.
Definition thread_id.hpp:37
friend constexpr auto operator<(thread_id lhs, thread_id rhs) noexcept -> bool
Definition thread_id.hpp:55
friend constexpr auto operator!=(thread_id lhs, thread_id rhs) noexcept -> bool
Definition thread_id.hpp:50
constexpr thread_id(std::int64_t value)
Construct and validate a thread id.
Definition thread_id.hpp:29
friend constexpr auto operator==(thread_id lhs, thread_id rhs) noexcept -> bool
Definition thread_id.hpp:45
Error-returning result type used by the portable ThreadSchedule API.
auto operator()(threadschedule::thread_id value) const noexcept -> size_t
static constexpr auto value(thread_id id) noexcept -> std::uint64_t
Definition thread_id.hpp:91
static constexpr auto make(std::uint64_t value) -> thread_id
Definition thread_id.hpp:84