ThreadSchedule 3.0.0
Modern C++ thread management library
Loading...
Searching...
No Matches
cpu_id.hpp
Go to the documentation of this file.
1#pragma once
2
8#include "result.hpp"
9
10#include <cstdint>
11#include <stdexcept>
12
13namespace threadschedule
14{
15
22class cpu_id
23{
24public:
30 constexpr explicit cpu_id(int value) : value_(checked(value)) {}
31
37 [[nodiscard]] static auto
38 create(int value) noexcept -> result<cpu_id>
39 {
40 if (value < 0)
41 return unexpected(std::make_error_code(std::errc::invalid_argument));
42 return cpu_id(unchecked_tag{}, static_cast<std::uint32_t>(value));
43 }
44
46 [[nodiscard]] constexpr auto
47 value() const noexcept -> std::uint32_t
48 {
49 return value_;
50 }
51
52 friend constexpr auto
53 operator==(cpu_id lhs, cpu_id rhs) noexcept -> bool
54 {
55 return lhs.value_ == rhs.value_;
56 }
57 friend constexpr auto
58 operator!=(cpu_id lhs, cpu_id rhs) noexcept -> bool
59 {
60 return !(lhs == rhs);
61 }
62 friend constexpr auto
63 operator<(cpu_id lhs, cpu_id rhs) noexcept -> bool
64 {
65 return lhs.value_ < rhs.value_;
66 }
67
68private:
69 struct unchecked_tag
70 {
71 };
72
73 constexpr cpu_id(unchecked_tag, std::uint32_t value) noexcept : value_(value) {}
74
75 [[nodiscard]] static constexpr auto
76 checked(int value) -> std::uint32_t
77 {
78 if (value < 0)
79 throw std::invalid_argument("cpu_id must not be negative");
80 return static_cast<std::uint32_t>(value);
81 }
82
83 std::uint32_t value_;
84};
85
86} // namespace threadschedule
Validated CPU identifier.
Definition cpu_id.hpp:23
static auto create(int value) noexcept -> result< cpu_id >
Construct a CPU id without exceptions.
Definition cpu_id.hpp:38
friend constexpr auto operator<(cpu_id lhs, cpu_id rhs) noexcept -> bool
Definition cpu_id.hpp:63
friend constexpr auto operator!=(cpu_id lhs, cpu_id rhs) noexcept -> bool
Definition cpu_id.hpp:58
constexpr cpu_id(int value)
Construct a CPU id and throw on invalid input.
Definition cpu_id.hpp:30
friend constexpr auto operator==(cpu_id lhs, cpu_id rhs) noexcept -> bool
Definition cpu_id.hpp:53
constexpr auto value() const noexcept -> std::uint32_t
Return the underlying CPU index.
Definition cpu_id.hpp:47
Error-returning result type used by the portable ThreadSchedule API.