25 #ifndef TASKOLIB_TIMEOUT_H_
26 #define TASKOLIB_TIMEOUT_H_
84 template <
typename Rep,
typename Period>
86 Timeout(std::chrono::duration<Rep, Period> duration)
88 if (duration < duration.zero())
89 throw Error(
"Negative timeout");
94 timeout_ = std::chrono::round<Duration>(duration);
104 template <
typename FloatT,
105 std::enable_if_t<std::is_floating_point<FloatT>::value,
bool> =
true>
109 if (seconds < FloatT{ 0.0 })
110 throw Error(
"Negative timeout");
112 if (std::isnan(seconds))
113 throw Error(
"Timeout is not-a-number");
115 const auto desired_duration = std::chrono::duration<FloatT>{ seconds };
120 timeout_ = std::chrono::round<Duration>(desired_duration);
133 friend constexpr
bool
137 friend constexpr
bool
141 friend constexpr
bool
145 friend constexpr
bool
149 friend constexpr
bool
153 friend constexpr
bool
160 template <
typename FloatT,
161 std::enable_if_t<std::is_floating_point<FloatT>::value,
bool> =
true>
162 operator FloatT()
const
165 return std::chrono::duration_cast<std::chrono::duration<FloatT>>(
timeout_).count();
167 return std::numeric_limits<FloatT>::infinity();
174 template <
typename DurationT,
175 std::enable_if_t<std::is_arithmetic_v<typename DurationT::rep>,
bool> =
true>
176 operator DurationT()
const
179 return std::chrono::duration_cast<DurationT>(
timeout_);
181 return DurationT::max();
191 if (!isfinite(timeout))
192 stream <<
"infinite";
194 stream << static_cast<std::chrono::milliseconds>(timeout).count();
An exception class carrying an error message and, optionally, the index of the step in which the erro...
Definition: exceptions.h:73
A type for storing a timeout duration.
Definition: Timeout.h:72
Timeout()=default
Default-construct an infinite timeout.
constexpr friend bool operator==(Timeout lhs, Timeout rhs)
Determine if two timeouts are equal.
Definition: Timeout.h:134
constexpr Timeout(std::chrono::duration< Rep, Period > duration)
Construct a Timeout from a std::chrono::duration (implicit for convenience).
Definition: Timeout.h:86
static constexpr Duration infinite_duration
Definition: Timeout.h:185
constexpr Timeout(FloatT seconds)
Construct a Timeout from a floating-point value (only explicitly).
Definition: Timeout.h:107
constexpr friend bool operator!=(Timeout lhs, Timeout rhs)
Determine if two timeouts are not equal.
Definition: Timeout.h:138
Duration timeout_
Definition: Timeout.h:187
constexpr friend bool operator<=(Timeout lhs, Timeout rhs)
Determine if the left timeout is shorter than or equal to the right one.
Definition: Timeout.h:154
friend bool isfinite(Timeout timeout) noexcept
Determine if the timeout has a finite duration.
Definition: Timeout.h:127
std::chrono::milliseconds Duration
Definition: Timeout.h:74
constexpr friend bool operator<(Timeout lhs, Timeout rhs)
Determine if the left timeout is shorter than the right one.
Definition: Timeout.h:146
constexpr friend bool operator>(Timeout lhs, Timeout rhs)
Determine if the left timeout is longer than the right one.
Definition: Timeout.h:142
constexpr friend bool operator>=(Timeout lhs, Timeout rhs)
Determine if the left timeout is longer than or equal to the right one.
Definition: Timeout.h:150
static constexpr Timeout infinity()
A constant to use for "infinite" timeout durations.
Definition: Timeout.h:124
Definition of the Error exception class.
Namespace task contains all Taskolib functions and classes.
Definition: CommChannel.h:33
std::ostream & operator<<(std::ostream &stream, const Timeout &timeout)
Definition: Timeout.h:190