iceoryx2
C++ Language Bindings
Loading...
Searching...
No Matches
iox2::bb::Duration Class Reference

#include <duration.hpp>

Public Member Functions

constexpr auto operator+ (const Duration &rhs) const noexcept -> Duration
 Creates Duration object by addition. On overflow duration saturates to Duration::max().
 
constexpr auto operator+= (const Duration &rhs) noexcept -> Duration &
 Adds a Duration to itself. On overflow duration saturates to Duration::max().
 
constexpr auto operator- (const Duration &rhs) const noexcept -> Duration
 Creates Duration object by subtraction. On underflow duration saturates to Duration::zero().
 
constexpr auto operator-= (const Duration &rhs) noexcept -> Duration &
 Subtracts a Duration from itself. On underflow duration saturates to Duration::zero().
 
template<typename T >
constexpr auto operator* (const T &rhs) const noexcept -> Duration
 Creates Duration object by multiplication.
 
template<typename T >
constexpr auto operator*= (const T &rhs) noexcept -> Duration &
 Multiplies a Duration with an arithmetic type and assigns the result to itself.
 
constexpr auto as_nanos () const noexcept -> uint64_t
 returns the duration in nanoseconds
 
constexpr auto as_micros () const noexcept -> uint64_t
 returns the duration in microseconds
 
constexpr auto as_millis () const noexcept -> uint64_t
 returns the duration in milliseconds
 
constexpr auto as_secs () const noexcept -> uint64_t
 returns the duration in seconds
 
constexpr auto as_mins () const noexcept -> uint64_t
 returns the duration in minutes
 
constexpr auto as_hours () const noexcept -> uint64_t
 returns the duration in hours
 
constexpr auto as_days () const noexcept -> uint64_t
 returns the duration in days
 
constexpr auto subsec_nanos () const noexcept -> uint32_t
 returns the subsecond part of the duration in nanoseconds
 
constexpr auto subsec_micros () const noexcept -> uint32_t
 returns the subsecond part of the duration in microseconds
 
constexpr auto subsec_millis () const noexcept -> uint32_t
 returns the subsecond part of the duration in milliseconds
 
constexpr Duration (SecondsT seconds, NanosecondsT nanoseconds) noexcept
 Constructs a Duration from seconds and nanoseconds.
 

Static Public Member Functions

template<typename T >
static constexpr auto from_nanos (T value) noexcept -> Duration
 Constructs a new Duration object from nanoseconds.
 
template<typename T >
static constexpr auto from_micros (T value) noexcept -> Duration
 Constructs a new Duration object from microseconds.
 
template<typename T >
static constexpr auto from_millis (T value) noexcept -> Duration
 Constructs a new Duration object from milliseconds.
 
template<typename T >
static constexpr auto from_secs (T value) noexcept -> Duration
 Constructs a new Duration object from seconds.
 
template<typename T >
static constexpr auto from_mins (T value) noexcept -> Duration
 Constructs a new Duration object from minutes.
 
template<typename T >
static constexpr auto from_hours (T value) noexcept -> Duration
 Constructs a new Duration object from hours.
 
template<typename T >
static constexpr auto from_days (T value) noexcept -> Duration
 Constructs a new Duration object from days.
 
static constexpr auto max () noexcept -> Duration
 Constructs a new Duration object of maximum allowed length. Useful for functions which should have an "infinite" timeout.
 
static constexpr auto zero () noexcept -> Duration
 Constructs a new Duration object with a duration of zero.
 
static constexpr auto create_duration (SecondsT seconds, NanosecondsT nanoseconds) noexcept -> Duration
 

Static Public Attributes

static constexpr uint32_t SECS_PER_MINUTE { 60U }
 
static constexpr uint32_t SECS_PER_HOUR { 3600U }
 
static constexpr uint32_t HOURS_PER_DAY { 24U }
 
static constexpr uint32_t MILLISECS_PER_SEC { 1000U }
 
static constexpr uint32_t MICROSECS_PER_SEC { MILLISECS_PER_SEC * 1000U }
 
static constexpr uint32_t NANOSECS_PER_MICROSEC { 1000U }
 
static constexpr uint32_t NANOSECS_PER_MILLISEC { NANOSECS_PER_MICROSEC * 1000U }
 
static constexpr uint32_t NANOSECS_PER_SEC { NANOSECS_PER_MILLISEC * 1000U }
 

Protected Types

using SecondsT = uint64_t
 
using NanosecondsT = uint32_t
 

Friends

constexpr auto operator== (const Duration &lhs, const Duration &rhs) noexcept -> bool
 Equal to operator.
 
constexpr auto operator!= (const Duration &lhs, const Duration &rhs) noexcept -> bool
 Not equal to operator.
 
constexpr auto operator< (const Duration &lhs, const Duration &rhs) noexcept -> bool
 Less than operator.
 
constexpr auto operator<= (const Duration &lhs, const Duration &rhs) noexcept -> bool
 Less than or equal to operator.
 
constexpr auto operator> (const Duration &lhs, const Duration &rhs) noexcept -> bool
 Greater than operator.
 
constexpr auto operator>= (const Duration &lhs, const Duration &rhs) noexcept -> bool
 Greater than or equal to operator.
 
constexpr auto duration_literals::operator""_ns (unsigned long long int value) noexcept -> Duration
 
constexpr auto duration_literals::operator""_us (unsigned long long int value) noexcept -> Duration
 
constexpr auto duration_literals::operator""_ms (unsigned long long int value) noexcept -> Duration
 
constexpr auto duration_literals::operator""_s (unsigned long long int value) noexcept -> Duration
 
constexpr auto duration_literals::operator""_m (unsigned long long int value) noexcept -> Duration
 
constexpr auto duration_literals::operator""_h (unsigned long long int value) noexcept -> Duration
 
constexpr auto duration_literals::operator""_d (unsigned long long int value) noexcept -> Duration
 
template<typename T >
constexpr auto operator* (const T &lhs, const Duration &rhs) noexcept -> Duration
 creates Duration object by multiplying object T with a duration. On overflow duration will saturate to Duration::max()
 

Detailed Description

#include <iostream>
// ...
using namespace units;
using namespace units::duration_literals;
auto someDays = 2 * 7_d + 5_ns;
auto someSeconds = 42_s + 500_ms;
IOX2_LOG(Info, someDays);
IOX2_LOG(Info, someDays.nanoSeconds<uint64_t>() << " ns");
IOX2_LOG(Info, someSeconds.milliSeconds<int64_t>() << " ms");
#define IOX2_LOG(level, msg_stream)
Macro for logging.
Definition logging.hpp:63

Definition at line 69 of file duration.hpp.

Member Typedef Documentation

◆ NanosecondsT

using iox2::bb::Duration::NanosecondsT = uint32_t
protected

Definition at line 72 of file duration.hpp.

◆ SecondsT

using iox2::bb::Duration::SecondsT = uint64_t
protected

Definition at line 71 of file duration.hpp.

Constructor & Destructor Documentation

◆ Duration()

constexpr iox2::bb::Duration::Duration ( SecondsT  seconds,
NanosecondsT  nanoseconds 
)
constexprnoexcept

Constructs a Duration from seconds and nanoseconds.

Parameters
[in]secondsportion of the duration
[in]nanosecondsportion of the duration
Note
this is protected to be able to use it in unit tests

Definition at line 393 of file duration.hpp.

Member Function Documentation

◆ as_days()

constexpr auto iox2::bb::Duration::as_days ( ) const -> uint64_t
constexprnoexcept

returns the duration in days

Note
The remaining hours are truncated, similar to the casting behavior of a float to an int.

Definition at line 556 of file duration.hpp.

References HOURS_PER_DAY, and SECS_PER_HOUR.

◆ as_hours()

constexpr auto iox2::bb::Duration::as_hours ( ) const -> uint64_t
constexprnoexcept

returns the duration in hours

Note
The remaining minutes are truncated, similar to the casting behavior of a float to an int.

Definition at line 552 of file duration.hpp.

References SECS_PER_HOUR.

◆ as_micros()

constexpr auto iox2::bb::Duration::as_micros ( ) const -> uint64_t
constexprnoexcept

returns the duration in microseconds

Note
If the duration in microseconds is larger than an uint64_t can represent, it will be clamped to the uint64_t max value.
The remaining nanoseconds are truncated, similar to the casting behavior of a float to an int.

Definition at line 512 of file duration.hpp.

References create_duration(), MICROSECS_PER_SEC, and NANOSECS_PER_MICROSEC.

◆ as_millis()

constexpr auto iox2::bb::Duration::as_millis ( ) const -> uint64_t
constexprnoexcept

returns the duration in milliseconds

Note
If the duration in milliseconds is larger than an uint64_t can represent, it will be clamped to the uint64_t max value.
The remaining microseconds are truncated, similar to the casting behavior of a float to an int.

Definition at line 528 of file duration.hpp.

References create_duration(), MILLISECS_PER_SEC, and NANOSECS_PER_MILLISEC.

◆ as_mins()

constexpr auto iox2::bb::Duration::as_mins ( ) const -> uint64_t
constexprnoexcept

returns the duration in minutes

Note
The remaining seconds are truncated, similar to the casting behavior of a float to an int.

Definition at line 548 of file duration.hpp.

References SECS_PER_MINUTE.

◆ as_nanos()

constexpr auto iox2::bb::Duration::as_nanos ( ) const -> uint64_t
constexprnoexcept

returns the duration in nanoseconds

Note
If the duration in nanoseconds is larger than an uint64_t can represent, it will be clamped to the uint64_t max value.

Definition at line 497 of file duration.hpp.

References create_duration(), and NANOSECS_PER_SEC.

◆ as_secs()

constexpr auto iox2::bb::Duration::as_secs ( ) const -> uint64_t
constexprnoexcept

returns the duration in seconds

Note
The remaining milliseconds are truncated, similar to the casting behavior of a float to an int.

Definition at line 544 of file duration.hpp.

◆ create_duration()

constexpr auto iox2::bb::Duration::create_duration ( SecondsT  seconds,
NanosecondsT  nanoseconds 
) -> Duration
staticconstexprnoexcept
Note
this is factory method is necessary to build with msvc due to issues calling a protected constexpr ctor from public methods

Definition at line 409 of file duration.hpp.

Referenced by as_micros(), as_millis(), as_nanos(), and iox2::BackpressureInfo::elapsed_time().

◆ from_days()

template<typename T >
constexpr auto iox2::bb::Duration::from_days ( value) -> Duration
staticconstexprnoexcept

Constructs a new Duration object from days.

Template Parameters
Tis an integer type for the value
Parameters
[in]valueas days
Returns
a new Duration object
Attention
Since negative durations are not allowed, the duration will be clamped to 0

Definition at line 487 of file duration.hpp.

References HOURS_PER_DAY, max(), and SECS_PER_HOUR.

Referenced by iox2::bb::duration_literals::operator""_d().

◆ from_hours()

template<typename T >
constexpr auto iox2::bb::Duration::from_hours ( value) -> Duration
staticconstexprnoexcept

Constructs a new Duration object from hours.

Template Parameters
Tis an integer type for the value
Parameters
[in]valueas hours
Returns
a new Duration object
Attention
Since negative durations are not allowed, the duration will be clamped to 0

Definition at line 478 of file duration.hpp.

References max(), and SECS_PER_HOUR.

Referenced by iox2::bb::duration_literals::operator""_h().

◆ from_micros()

template<typename T >
constexpr auto iox2::bb::Duration::from_micros ( value) -> Duration
staticconstexprnoexcept

Constructs a new Duration object from microseconds.

Template Parameters
Tis an integer type for the value
Parameters
[in]valueas microseconds
Returns
a new Duration object
Attention
Since negative durations are not allowed, the duration will be clamped to 0

Definition at line 442 of file duration.hpp.

References MICROSECS_PER_SEC, and NANOSECS_PER_MICROSEC.

Referenced by iox2::bb::From< std::chrono::microseconds, Duration >::from(), and iox2::bb::duration_literals::operator""_us().

◆ from_millis()

template<typename T >
constexpr auto iox2::bb::Duration::from_millis ( value) -> Duration
staticconstexprnoexcept

Constructs a new Duration object from milliseconds.

Template Parameters
Tis an integer type for the value
Parameters
[in]valueas milliseconds
Returns
a new Duration object
Attention
Since negative durations are not allowed, the duration will be clamped to 0

Definition at line 450 of file duration.hpp.

References MILLISECS_PER_SEC, and NANOSECS_PER_MILLISEC.

Referenced by iox2::bb::From< std::chrono::milliseconds, Duration >::from(), and iox2::bb::duration_literals::operator""_ms().

◆ from_mins()

template<typename T >
constexpr auto iox2::bb::Duration::from_mins ( value) -> Duration
staticconstexprnoexcept

Constructs a new Duration object from minutes.

Template Parameters
Tis an integer type for the value
Parameters
[in]valueas minutes
Returns
a new Duration object
Attention
Since negative durations are not allowed, the duration will be clamped to 0

Definition at line 469 of file duration.hpp.

References max(), and SECS_PER_MINUTE.

Referenced by iox2::bb::duration_literals::operator""_m().

◆ from_nanos()

template<typename T >
constexpr auto iox2::bb::Duration::from_nanos ( value) -> Duration
staticconstexprnoexcept

Constructs a new Duration object from nanoseconds.

Template Parameters
Tis an integer type for the value
Parameters
[in]valueas nanoseconds
Returns
a new Duration object
Attention
Since negative durations are not allowed, the duration will be clamped to 0

Definition at line 435 of file duration.hpp.

References NANOSECS_PER_SEC.

Referenced by iox2::Listener< ServiceType >::deadline(), iox2::bb::From< std::chrono::nanoseconds, Duration >::from(), and iox2::bb::duration_literals::operator""_ns().

◆ from_secs()

template<typename T >
constexpr auto iox2::bb::Duration::from_secs ( value) -> Duration
staticconstexprnoexcept

Constructs a new Duration object from seconds.

Template Parameters
Tis an integer type for the value
Parameters
[in]valueas seconds
Returns
a new Duration object
Attention
Since negative durations are not allowed, the duration will be clamped to 0

Definition at line 458 of file duration.hpp.

References max().

Referenced by iox2::Listener< ServiceType >::deadline(), iox2::bb::From< std::chrono::seconds, Duration >::from(), and iox2::bb::duration_literals::operator""_s().

◆ max()

constexpr auto iox2::bb::Duration::max ( ) -> Duration
staticconstexprnoexcept

Constructs a new Duration object of maximum allowed length. Useful for functions which should have an "infinite" timeout.

Definition at line 413 of file duration.hpp.

References NANOSECS_PER_SEC.

Referenced by from_days(), from_hours(), from_mins(), from_secs(), and operator+().

◆ operator*()

template<typename T >
constexpr auto iox2::bb::Duration::operator* ( const T &  rhs) const -> Duration
constexprnoexcept

Creates Duration object by multiplication.

Template Parameters
Tis an arithmetic type for the multiplicator
Parameters
[in]rhsis the multiplicator
Returns
a new Duration object
Attention
Since negative durations are not allowed, the duration will be clamped to 0
Note
A duration of 0 will always result in 0, no matter if multiplied with NaN or +Inf
There is no explicit division operator! This can be achieved by multiplication with the inverse of the divisor.
Multiplication of a non-zero duration with NaN and +Inf results in a saturated max duration

Definition at line 753 of file duration.hpp.

◆ operator*=()

template<typename T >
constexpr auto iox2::bb::Duration::operator*= ( const T &  rhs) -> Duration&
constexprnoexcept

Multiplies a Duration with an arithmetic type and assigns the result to itself.

Template Parameters
Tis an arithmetic type for the multiplicator
Parameters
[in]rhsis the multiplicator
Returns
a reference to itself
Attention
Since negative durations are not allowed, the duration will be clamped to 0
Note
A duration of 0 will always result in 0, no matter if multiplied with NaN or +Inf
There is no explicit division operator! This can be achieved by multiplication with the inverse of the divisor.
Multiplication of a non-zero duration with NaN and +Inf results in a saturated max duration

Definition at line 760 of file duration.hpp.

◆ operator+()

constexpr auto iox2::bb::Duration::operator+ ( const Duration rhs) const -> Duration
constexprnoexcept

Creates Duration object by addition. On overflow duration saturates to Duration::max().

Parameters
[in]rhsis the second summand
Returns
a new Duration object

Definition at line 573 of file duration.hpp.

References max().

◆ operator+=()

constexpr auto iox2::bb::Duration::operator+= ( const Duration rhs) -> Duration&
constexprnoexcept

Adds a Duration to itself. On overflow duration saturates to Duration::max().

Parameters
[in]rhsis the second summand
Returns
a reference to itself

Definition at line 589 of file duration.hpp.

◆ operator-()

constexpr auto iox2::bb::Duration::operator- ( const Duration rhs) const -> Duration
constexprnoexcept

Creates Duration object by subtraction. On underflow duration saturates to Duration::zero().

Parameters
[in]rhsis the subtrahend
Returns
a new Duration object
Attention
Since negative durations are not allowed, the duration will be clamped to 0

Definition at line 595 of file duration.hpp.

References zero().

◆ operator-=()

constexpr auto iox2::bb::Duration::operator-= ( const Duration rhs) -> Duration&
constexprnoexcept

Subtracts a Duration from itself. On underflow duration saturates to Duration::zero().

Parameters
[in]rhsis the subtrahend
Returns
a reference to itself
Attention
Since negative durations are not allowed, the duration will be clamped to 0

Definition at line 613 of file duration.hpp.

◆ subsec_micros()

constexpr auto iox2::bb::Duration::subsec_micros ( ) const -> uint32_t
constexprnoexcept

returns the subsecond part of the duration in microseconds

Note
The remaining nanoseconds are truncated, similar to the casting behavior of a float to an int.

Definition at line 564 of file duration.hpp.

References NANOSECS_PER_MICROSEC.

◆ subsec_millis()

constexpr auto iox2::bb::Duration::subsec_millis ( ) const -> uint32_t
constexprnoexcept

returns the subsecond part of the duration in milliseconds

Note
The remaining microseconds are truncated, similar to the casting behavior of a float to an int.

Definition at line 568 of file duration.hpp.

References NANOSECS_PER_MILLISEC.

◆ subsec_nanos()

constexpr auto iox2::bb::Duration::subsec_nanos ( ) const -> uint32_t
constexprnoexcept

returns the subsecond part of the duration in nanoseconds

Definition at line 560 of file duration.hpp.

◆ zero()

constexpr auto iox2::bb::Duration::zero ( ) -> Duration
staticconstexprnoexcept

Constructs a new Duration object with a duration of zero.

Definition at line 417 of file duration.hpp.

Referenced by operator-().

Friends And Related Symbol Documentation

◆ duration_literals::operator""_d

constexpr auto duration_literals::operator""_d ( unsigned long long int  value) -> Duration
friend

◆ duration_literals::operator""_h

constexpr auto duration_literals::operator""_h ( unsigned long long int  value) -> Duration
friend

◆ duration_literals::operator""_m

constexpr auto duration_literals::operator""_m ( unsigned long long int  value) -> Duration
friend

◆ duration_literals::operator""_ms

constexpr auto duration_literals::operator""_ms ( unsigned long long int  value) -> Duration
friend

◆ duration_literals::operator""_ns

constexpr auto duration_literals::operator""_ns ( unsigned long long int  value) -> Duration
friend

◆ duration_literals::operator""_s

constexpr auto duration_literals::operator""_s ( unsigned long long int  value) -> Duration
friend

◆ duration_literals::operator""_us

constexpr auto duration_literals::operator""_us ( unsigned long long int  value) -> Duration
friend

◆ operator!=

constexpr auto operator!= ( const Duration lhs,
const Duration rhs 
) -> bool
friend

Not equal to operator.

Parameters
[in]lhsis the left hand side of the comparison
[in]rhsis the right hand side of the comparison
Returns
true if duration not equal to rhs

Definition at line 349 of file duration.hpp.

◆ operator*

template<typename T >
constexpr auto operator* ( const T &  lhs,
const Duration rhs 
) -> Duration
friend

creates Duration object by multiplying object T with a duration. On overflow duration will saturate to Duration::max()

Template Parameters
Tis an arithmetic type for the multiplicator
Parameters
[in]lhsis the multiplicator
[in]rhsis the multiplicant
Returns
a new Duration object
Attention
Since negative durations are not allowed, the duration will be clamped to 0

Definition at line 320 of file duration.hpp.

◆ operator<

constexpr auto operator< ( const Duration lhs,
const Duration rhs 
) -> bool
friend

Less than operator.

Parameters
[in]lhsis the left hand side of the comparison
[in]rhsis the right hand side of the comparison
Returns
true if duration is less than rhs

Definition at line 358 of file duration.hpp.

◆ operator<=

constexpr auto operator<= ( const Duration lhs,
const Duration rhs 
) -> bool
friend

Less than or equal to operator.

Parameters
[in]lhsis the left hand side of the comparison
[in]rhsis the right hand side of the comparison
Returns
true if duration is less than or equal to rhs

Definition at line 378 of file duration.hpp.

◆ operator==

constexpr auto operator== ( const Duration lhs,
const Duration rhs 
) -> bool
friend

Equal to operator.

Parameters
[in]lhsis the left hand side of the comparison
[in]rhsis the right hand side of the comparison
Returns
true if duration equal to rhs

Definition at line 340 of file duration.hpp.

◆ operator>

constexpr auto operator> ( const Duration lhs,
const Duration rhs 
) -> bool
friend

Greater than operator.

Parameters
[in]lhsis the left hand side of the comparison
[in]rhsis the right hand side of the comparison
Returns
true if duration is greater than rhs

Definition at line 368 of file duration.hpp.

◆ operator>=

constexpr auto operator>= ( const Duration lhs,
const Duration rhs 
) -> bool
friend

Greater than or equal to operator.

Parameters
[in]lhsis the left hand side of the comparison
[in]rhsis the right hand side of the comparison
Returns
true if duration is greater than or equal to rhs

Definition at line 387 of file duration.hpp.

Member Data Documentation

◆ HOURS_PER_DAY

constexpr uint32_t iox2::bb::Duration::HOURS_PER_DAY { 24U }
staticconstexpr

Definition at line 274 of file duration.hpp.

Referenced by as_days(), and from_days().

◆ MICROSECS_PER_SEC

constexpr uint32_t iox2::bb::Duration::MICROSECS_PER_SEC { MILLISECS_PER_SEC * 1000U }
staticconstexpr

Definition at line 277 of file duration.hpp.

Referenced by as_micros(), and from_micros().

◆ MILLISECS_PER_SEC

constexpr uint32_t iox2::bb::Duration::MILLISECS_PER_SEC { 1000U }
staticconstexpr

Definition at line 276 of file duration.hpp.

Referenced by as_millis(), and from_millis().

◆ NANOSECS_PER_MICROSEC

constexpr uint32_t iox2::bb::Duration::NANOSECS_PER_MICROSEC { 1000U }
staticconstexpr

Definition at line 279 of file duration.hpp.

Referenced by as_micros(), from_micros(), and subsec_micros().

◆ NANOSECS_PER_MILLISEC

constexpr uint32_t iox2::bb::Duration::NANOSECS_PER_MILLISEC { NANOSECS_PER_MICROSEC * 1000U }
staticconstexpr

Definition at line 280 of file duration.hpp.

Referenced by as_millis(), from_millis(), and subsec_millis().

◆ NANOSECS_PER_SEC

constexpr uint32_t iox2::bb::Duration::NANOSECS_PER_SEC { NANOSECS_PER_MILLISEC * 1000U }
staticconstexpr

Definition at line 281 of file duration.hpp.

Referenced by as_nanos(), from_nanos(), and max().

◆ SECS_PER_HOUR

constexpr uint32_t iox2::bb::Duration::SECS_PER_HOUR { 3600U }
staticconstexpr

Definition at line 273 of file duration.hpp.

Referenced by as_days(), as_hours(), from_days(), and from_hours().

◆ SECS_PER_MINUTE

constexpr uint32_t iox2::bb::Duration::SECS_PER_MINUTE { 60U }
staticconstexpr

Definition at line 272 of file duration.hpp.

Referenced by as_mins(), and from_mins().


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