Taskolib  1.3.3
Public Types | Public Member Functions | Static Public Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
task::Timeout Class Reference

Detailed Description

A type for storing a timeout duration.

A timeout can be zero, positive, or infinite. A default-constructed Timeout is infinite. The free functions isfinite() can be used to test for infinity:

using namespace std::literals;
auto a = Timeout{ 1min }; // 1 minute via std::chrono duration parameter
assert(isfinite(a));
auto b = Timeout{ 60.0 }; // 60 seconds via floating-point parameter
assert(isfinite(b));
auto c = Timeout{}; // default-constructed Timeout is infinite
assert(!isfinite(c));
auto d = Timeout::infinity(); // static member function for generating infinite timeouts
assert(!isfinite(d));
Timeout()=default
Default-construct an infinite timeout.
friend bool isfinite(Timeout timeout) noexcept
Determine if the timeout has a finite duration.
Definition: Timeout.h:127
static constexpr Timeout infinity()
A constant to use for "infinite" timeout durations.
Definition: Timeout.h:124

For convenience, implicit conversion to floating-point types is supported, with an output in seconds:

double seconds = Timeout{ 2500ms };
assert(seconds == 2.5);
float a = Timeout::infinity();
assert(std::isinf(a));

Internally, a Timeout is stored as a std::chrono::duration with sub-second precision. The exact type used is made available as Timeout::Duration.

#include <Timeout.h>

Public Types

using Duration = std::chrono::milliseconds
 

Public Member Functions

 Timeout ()=default
 Default-construct an infinite timeout. More...
 
template<typename Rep , typename Period >
constexpr Timeout (std::chrono::duration< Rep, Period > duration)
 Construct a Timeout from a std::chrono::duration (implicit for convenience). More...
 
template<typename FloatT , std::enable_if_t< std::is_floating_point< FloatT >::value, bool > = true>
constexpr Timeout (FloatT seconds)
 Construct a Timeout from a floating-point value (only explicitly). More...
 
template<typename FloatT , std::enable_if_t< std::is_floating_point< FloatT >::value, bool > = true>
 operator FloatT () const
 Implicitly cast the timeout to a floating-point type. More...
 
template<typename DurationT , std::enable_if_t< std::is_arithmetic_v< typename DurationT::rep >, bool > = true>
 operator DurationT () const
 Explicitly cast the timeout to a std::chrono::duration type. More...
 

Static Public Member Functions

static constexpr Timeout infinity ()
 A constant to use for "infinite" timeout durations. More...
 

Private Attributes

Duration timeout_ { infinite_duration }
 

Static Private Attributes

static constexpr Duration infinite_duration { Duration::max() }
 

Friends

bool isfinite (Timeout timeout) noexcept
 Determine if the timeout has a finite duration. More...
 
constexpr friend bool operator== (Timeout lhs, Timeout rhs)
 Determine if two timeouts are equal. More...
 
constexpr friend bool operator!= (Timeout lhs, Timeout rhs)
 Determine if two timeouts are not equal. More...
 
constexpr friend bool operator> (Timeout lhs, Timeout rhs)
 Determine if the left timeout is longer than the right one. More...
 
constexpr friend bool operator< (Timeout lhs, Timeout rhs)
 Determine if the left timeout is shorter than the right one. More...
 
constexpr friend bool operator>= (Timeout lhs, Timeout rhs)
 Determine if the left timeout is longer than or equal to the right one. More...
 
constexpr friend bool operator<= (Timeout lhs, Timeout rhs)
 Determine if the left timeout is shorter than or equal to the right one. More...
 

Member Typedef Documentation

◆ Duration

using task::Timeout::Duration = std::chrono::milliseconds

Constructor & Destructor Documentation

◆ Timeout() [1/3]

task::Timeout::Timeout ( )
default

Default-construct an infinite timeout.

◆ Timeout() [2/3]

template<typename Rep , typename Period >
constexpr task::Timeout::Timeout ( std::chrono::duration< Rep, Period >  duration)
inlineconstexpr

Construct a Timeout from a std::chrono::duration (implicit for convenience).

Exceptions
Erroris thrown if the duration is negative.

References infinite_duration, and timeout_.

◆ Timeout() [3/3]

template<typename FloatT , std::enable_if_t< std::is_floating_point< FloatT >::value, bool > = true>
constexpr task::Timeout::Timeout ( FloatT  seconds)
inlineexplicitconstexpr

Construct a Timeout from a floating-point value (only explicitly).

Parameters
secondsThe timeout in seconds
Exceptions
Erroris thrown if seconds is negative or not-a-number.

References infinite_duration, and timeout_.

Member Function Documentation

◆ infinity()

static constexpr Timeout task::Timeout::infinity ( )
inlinestaticconstexpr

A constant to use for "infinite" timeout durations.

References infinite_duration.

◆ operator DurationT()

template<typename DurationT , std::enable_if_t< std::is_arithmetic_v< typename DurationT::rep >, bool > = true>
task::Timeout::operator DurationT ( ) const
inline

Explicitly cast the timeout to a std::chrono::duration type.

If the timeout is set to infinite, DurationT::max() is returned.

References isfinite, and timeout_.

◆ operator FloatT()

template<typename FloatT , std::enable_if_t< std::is_floating_point< FloatT >::value, bool > = true>
task::Timeout::operator FloatT ( ) const
inline

Implicitly cast the timeout to a floating-point type.

Infinity is returned for an infinite timeout.

References isfinite, and timeout_.

Friends And Related Function Documentation

◆ isfinite

bool isfinite ( Timeout  timeout)
friend

Determine if the timeout has a finite duration.

Referenced by operator DurationT(), and operator FloatT().

◆ operator!=

constexpr friend bool operator!= ( Timeout  lhs,
Timeout  rhs 
)
friend

Determine if two timeouts are not equal.

◆ operator<

constexpr friend bool operator< ( Timeout  lhs,
Timeout  rhs 
)
friend

Determine if the left timeout is shorter than the right one.

◆ operator<=

constexpr friend bool operator<= ( Timeout  lhs,
Timeout  rhs 
)
friend

Determine if the left timeout is shorter than or equal to the right one.

◆ operator==

constexpr friend bool operator== ( Timeout  lhs,
Timeout  rhs 
)
friend

Determine if two timeouts are equal.

◆ operator>

constexpr friend bool operator> ( Timeout  lhs,
Timeout  rhs 
)
friend

Determine if the left timeout is longer than the right one.

◆ operator>=

constexpr friend bool operator>= ( Timeout  lhs,
Timeout  rhs 
)
friend

Determine if the left timeout is longer than or equal to the right one.

Member Data Documentation

◆ infinite_duration

constexpr Duration task::Timeout::infinite_duration { Duration::max() }
staticconstexprprivate

Referenced by infinity(), and Timeout().

◆ timeout_

Duration task::Timeout::timeout_ { infinite_duration }
private

The documentation for this class was generated from the following file: