ThreadSchedule 3.0.0
Modern C++ thread management library
Loading...
Searching...
No Matches
nice_value.hpp
Go to the documentation of this file.
1#pragma once
2
8#include "result.hpp"
9
10#include <stdexcept>
11
12namespace threadschedule
13{
14
19{
20public:
22 static constexpr int minimum = -20;
24 static constexpr int maximum = 19;
25
31 constexpr explicit nice_value(int value) : value_(checked(value)) {}
32
38 [[nodiscard]] static auto
40 {
41 if (value < minimum || value > maximum)
42 return unexpected(std::make_error_code(std::errc::invalid_argument));
43 return nice_value(unchecked_tag{}, value);
44 }
45
47 [[nodiscard]] constexpr auto
48 value() const noexcept -> int
49 {
50 return value_;
51 }
52
53 friend constexpr auto
54 operator==(nice_value lhs, nice_value rhs) noexcept -> bool
55 {
56 return lhs.value_ == rhs.value_;
57 }
58 friend constexpr auto
59 operator!=(nice_value lhs, nice_value rhs) noexcept -> bool
60 {
61 return !(lhs == rhs);
62 }
63
64private:
65 struct unchecked_tag
66 {
67 };
68 constexpr nice_value(unchecked_tag, int value) noexcept : value_(value) {}
69
70 [[nodiscard]] static constexpr auto
71 checked(int value) -> int
72 {
73 if (value < minimum || value > maximum)
74 throw std::invalid_argument("nice_value must be in the range -20..19");
75 return value;
76 }
77
78 int value_;
79};
80
81} // namespace threadschedule
Strongly-typed nice value in the POSIX range $[-20, 19]$.
static constexpr int minimum
Lowest accepted nice value (highest priority).
friend constexpr auto operator!=(nice_value lhs, nice_value rhs) noexcept -> bool
constexpr auto value() const noexcept -> int
Return the wrapped nice value.
friend constexpr auto operator==(nice_value lhs, nice_value rhs) noexcept -> bool
constexpr nice_value(int value)
Construct and validate a nice value.
static auto create(int value) noexcept -> result< nice_value >
Construct a nice value without exceptions.
static constexpr int maximum
Highest accepted nice value (lowest priority).
Error-returning result type used by the portable ThreadSchedule API.