Taskolib  1.3.3
Timeout.h
Go to the documentation of this file.
1 
23 // SPDX-License-Identifier: LGPL-2.1-or-later
24 
25 #ifndef TASKOLIB_TIMEOUT_H_
26 #define TASKOLIB_TIMEOUT_H_
27 
28 #include <chrono>
29 #include <cmath>
30 #include <limits>
31 #include <ostream>
32 
33 #include "taskolib/exceptions.h"
34 
35 namespace task {
36 
71 class Timeout
72 {
73 public:
74  using Duration = std::chrono::milliseconds;
75 
77  Timeout() = default;
78 
84  template <typename Rep, typename Period>
85  constexpr
86  Timeout(std::chrono::duration<Rep, Period> duration)
87  {
88  if (duration < duration.zero())
89  throw Error("Negative timeout");
90 
91  if (duration >= infinite_duration)
93  else
94  timeout_ = std::chrono::round<Duration>(duration);
95  }
96 
104  template <typename FloatT,
105  std::enable_if_t<std::is_floating_point<FloatT>::value, bool> = true>
106  explicit constexpr
107  Timeout(FloatT seconds)
108  {
109  if (seconds < FloatT{ 0.0 })
110  throw Error("Negative timeout");
111 
112  if (std::isnan(seconds))
113  throw Error("Timeout is not-a-number");
114 
115  const auto desired_duration = std::chrono::duration<FloatT>{ seconds };
116 
117  if (not std::isfinite(seconds) || desired_duration >= infinite_duration)
119  else
120  timeout_ = std::chrono::round<Duration>(desired_duration);
121  }
122 
124  static constexpr Timeout infinity() { return Timeout{ infinite_duration }; };
125 
127  friend bool isfinite(Timeout timeout) noexcept
128  {
129  return timeout.timeout_ < infinite_duration;
130  }
131 
133  friend constexpr bool
134  operator==(Timeout lhs, Timeout rhs) { return lhs.timeout_ == rhs.timeout_; }
135 
137  friend constexpr bool
138  operator!=(Timeout lhs, Timeout rhs) { return lhs.timeout_ != rhs.timeout_; }
139 
141  friend constexpr bool
142  operator> (Timeout lhs, Timeout rhs) { return lhs.timeout_ > rhs.timeout_; }
143 
145  friend constexpr bool
146  operator< (Timeout lhs, Timeout rhs) { return lhs.timeout_ < rhs.timeout_; }
147 
149  friend constexpr bool
150  operator>=(Timeout lhs, Timeout rhs) { return lhs.timeout_ >= rhs.timeout_; }
151 
153  friend constexpr bool
154  operator<=(Timeout lhs, Timeout rhs) { return lhs.timeout_ <= rhs.timeout_; }
155 
160  template <typename FloatT,
161  std::enable_if_t<std::is_floating_point<FloatT>::value, bool> = true>
162  operator FloatT() const
163  {
164  if (isfinite(*this))
165  return std::chrono::duration_cast<std::chrono::duration<FloatT>>(timeout_).count();
166  else
167  return std::numeric_limits<FloatT>::infinity();
168  }
169 
174  template <typename DurationT,
175  std::enable_if_t<std::is_arithmetic_v<typename DurationT::rep>, bool> = true>
176  operator DurationT() const
177  {
178  if (isfinite(*this))
179  return std::chrono::duration_cast<DurationT>(timeout_);
180  else
181  return DurationT::max();
182  }
183 
184 private:
185  static constexpr Duration infinite_duration{ Duration::max() };
186 
188 };
189 
190 inline std::ostream& operator<<(std::ostream& stream, const Timeout& timeout) {
191  if (!isfinite(timeout))
192  stream << "infinite";
193  else
194  stream << static_cast<std::chrono::milliseconds>(timeout).count();
195  return stream;
196 }
197 
198 } // namespace task
199 
200 #endif
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