iceoryx_doc  1.0.1
Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Types | Protected Member Functions | Static Protected Member Functions | Friends | List of all members
iox::units::Duration Class Reference

#include <duration.hpp>

Public Member Functions

constexpr Duration (const struct timeval &value) noexcept
 Construct a Duration object from timeval. More...
 
constexpr Duration (const struct timespec &value) noexcept
 Construct a Duration object from timespec. More...
 
constexpr Duration (const struct itimerspec &value) noexcept
 Construct a Duration object from itimerspec. More...
 
constexpr Duration (const std::chrono::milliseconds &value) noexcept
 Construct a Duration object from std::chrono::milliseconds. More...
 
constexpr Duration (const std::chrono::nanoseconds &value) noexcept
 Construct a Duration object from std::chrono::nanoseconds. More...
 
Durationoperator= (const std::chrono::milliseconds &rhs) noexcept
 Assigns a std::chrono::milliseconds to an duration object. More...
 
constexpr bool operator== (const Duration &rhs) const noexcept
 Equal to operator. More...
 
constexpr bool operator!= (const Duration &rhs) const noexcept
 Not equal to operator. More...
 
constexpr bool operator< (const Duration &rhs) const noexcept
 Less than operator. More...
 
constexpr bool operator<= (const Duration &rhs) const noexcept
 Less than or equal to operator. More...
 
constexpr bool operator> (const Duration &rhs) const noexcept
 Greater than operator. More...
 
constexpr bool operator>= (const Duration &rhs) const noexcept
 Greater than or equal to operator. More...
 
constexpr Duration operator+ (const Duration &rhs) const noexcept
 Creates Duration object by addition. More...
 
constexpr Duration operator- (const Duration &rhs) const noexcept
 Creates Duration object by subtraction. More...
 
template<typename T >
constexpr Duration operator* (const T &rhs) const noexcept
 Creates Duration object by multiplication. More...
 
constexpr uint64_t toNanoseconds () const noexcept
 returns the duration in nanoseconds More...
 
constexpr uint64_t toMicroseconds () const noexcept
 returns the duration in microseconds More...
 
constexpr uint64_t toMilliseconds () const noexcept
 returns the duration in milliseconds More...
 
constexpr uint64_t toSeconds () const noexcept
 returns the duration in seconds More...
 
constexpr uint64_t toMinutes () const noexcept
 returns the duration in minutes More...
 
constexpr uint64_t toHours () const noexcept
 returns the duration in hours More...
 
constexpr uint64_t toDays () const noexcept
 returns the duration in days More...
 
struct timespec timespec (const TimeSpecReference &reference=TimeSpecReference::None) const noexcept
 converts duration in a timespec c struct
 
constexpr operator struct timeval () const noexcept
 converts duration in a timeval c struct timeval::tv_sec = seconds since the Epoch (01.01.1970) timeval::tv_usec = microseconds
 

Static Public Member Functions

template<typename T >
static constexpr Duration fromNanoseconds (const T value) noexcept
 Constructs a new Duration object from nanoseconds. More...
 
template<typename T >
static constexpr Duration fromMicroseconds (const T value) noexcept
 Constructs a new Duration object from microseconds. More...
 
template<typename T >
static constexpr Duration fromMilliseconds (const T value) noexcept
 Constructs a new Duration object from milliseconds. More...
 
template<typename T >
static constexpr Duration fromSeconds (const T value) noexcept
 Constructs a new Duration object from seconds. More...
 
template<typename T >
static constexpr Duration fromMinutes (const T value) noexcept
 Constructs a new Duration object from minutes. More...
 
template<typename T >
static constexpr Duration fromHours (const T value) noexcept
 Constructs a new Duration object from hours. More...
 
template<typename T >
static constexpr Duration fromDays (const T value) noexcept
 Constructs a new Duration object from days. More...
 

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 Seconds_t = uint64_t
 
using Nanoseconds_t = uint32_t
 

Protected Member Functions

constexpr Duration (const Seconds_t seconds, const Nanoseconds_t nanoseconds) noexcept
 Constructs a Duration from seconds and nanoseconds. More...
 

Static Protected Member Functions

static constexpr Duration createDuration (const Seconds_t seconds, const Nanoseconds_t nanoseconds) noexcept
 
static constexpr Duration max () noexcept
 
static constexpr Duration zero () noexcept
 

Friends

constexpr friend Duration duration_literals::operator""_ns (unsigned long long int) noexcept
 
constexpr friend Duration duration_literals::operator""_us (unsigned long long int) noexcept
 
constexpr friend Duration duration_literals::operator""_ms (unsigned long long int) noexcept
 
constexpr friend Duration duration_literals::operator""_s (unsigned long long int) noexcept
 
constexpr friend Duration duration_literals::operator""_m (unsigned long long int) noexcept
 
constexpr friend Duration duration_literals::operator""_h (unsigned long long int) noexcept
 
constexpr friend Duration duration_literals::operator""_d (unsigned long long int) noexcept
 
template<typename T >
constexpr friend Duration operator* (const T &lhs, const Duration &rhs) noexcept
 creates Duration object by multiplying object T with a duration More...
 
std::ostream & operator<< (std::ostream &stream, const Duration &t) noexcept
 stream operator for the Duration class
 

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;
std::cout << someDays << std::endl;
std::cout << someDays.nanoSeconds<uint64_t>() << " ns" << std::endl;
std::cout << someSeconds.milliSeconds<int64_t>() << " ms" << std::endl;

Constructor & Destructor Documentation

◆ Duration() [1/6]

constexpr iox::units::Duration::Duration ( const struct timeval &  value)
inlineexplicitconstexprnoexcept

Construct a Duration object from timeval.

Parameters
[in]valueas timeval

◆ Duration() [2/6]

constexpr iox::units::Duration::Duration ( const struct timespec value)
inlineexplicitconstexprnoexcept

Construct a Duration object from timespec.

Parameters
[in]valueas timespec

◆ Duration() [3/6]

constexpr iox::units::Duration::Duration ( const struct itimerspec &  value)
inlineexplicitconstexprnoexcept

Construct a Duration object from itimerspec.

Parameters
[in]valueas itimerspec
Note
only it_interval from the itimerspec is used

◆ Duration() [4/6]

constexpr iox::units::Duration::Duration ( const std::chrono::milliseconds &  value)
inlineexplicitconstexprnoexcept

Construct a Duration object from std::chrono::milliseconds.

Parameters
[in]valueas milliseconds
Attention
Since negative durations are not allowed, the duration will be clamped to 0

◆ Duration() [5/6]

constexpr iox::units::Duration::Duration ( const std::chrono::nanoseconds &  value)
inlineexplicitconstexprnoexcept

Construct a Duration object from std::chrono::nanoseconds.

Parameters
[in]valueas nanoseconds
Attention
Since negative durations are not allowed, the duration will be clamped to 0

◆ Duration() [6/6]

constexpr iox::units::Duration::Duration ( const Seconds_t  seconds,
const Nanoseconds_t  nanoseconds 
)
inlineconstexprprotectednoexcept

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
Todo:
#607 issue warning or fail

Member Function Documentation

◆ createDuration()

constexpr Duration iox::units::Duration::createDuration ( const Seconds_t  seconds,
const Nanoseconds_t  nanoseconds 
)
inlinestaticconstexprprotectednoexcept
Note
this is factory method is necessary to build with msvc due to issues calling a protected constexpr ctor from public methods

◆ fromDays()

template<typename T >
constexpr Duration iox::units::Duration::fromDays ( const T  value)
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

◆ fromHours()

template<typename T >
constexpr Duration iox::units::Duration::fromHours ( const T  value)
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

◆ fromMicroseconds()

template<typename T >
constexpr Duration iox::units::Duration::fromMicroseconds ( const T  value)
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

◆ fromMilliseconds()

template<typename T >
constexpr Duration iox::units::Duration::fromMilliseconds ( const T  value)
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

◆ fromMinutes()

template<typename T >
constexpr Duration iox::units::Duration::fromMinutes ( const T  value)
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

◆ fromNanoseconds()

template<typename T >
constexpr Duration iox::units::Duration::fromNanoseconds ( const T  value)
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

◆ fromSeconds()

template<typename T >
constexpr Duration iox::units::Duration::fromSeconds ( const T  value)
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

◆ operator!=()

constexpr bool iox::units::Duration::operator!= ( const Duration rhs) const
inlineconstexprnoexcept

Not equal to operator.

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

◆ operator*()

template<typename T >
constexpr Duration iox::units::Duration::operator* ( const T &  rhs) const
inlineconstexprnoexcept

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
Todo:
#607 issue warning or fail

◆ operator+()

constexpr Duration iox::units::Duration::operator+ ( const Duration rhs) const
inlineconstexprnoexcept

Creates Duration object by addition.

Parameters
[in]rhsis the second summand
Returns
a new Duration object
Todo:
#607 issue warning or fail

◆ operator-()

constexpr Duration iox::units::Duration::operator- ( const Duration rhs) const
inlineconstexprnoexcept

Creates Duration object by subtraction.

Parameters
[in]rhsis the subtrahend
Returns
a new Duration object
Attention
Since negative durations are not allowed, the duration will be clamped to 0
Todo:
#607 issue warning or fail

◆ operator<()

constexpr bool iox::units::Duration::operator< ( const Duration rhs) const
inlineconstexprnoexcept

Less than operator.

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

◆ operator<=()

constexpr bool iox::units::Duration::operator<= ( const Duration rhs) const
inlineconstexprnoexcept

Less than or equal to operator.

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

◆ operator=()

Duration & iox::units::Duration::operator= ( const std::chrono::milliseconds &  rhs)
inlinenoexcept

Assigns a std::chrono::milliseconds to an duration object.

Parameters
[in]rhsis the right hand side of the assignment
Returns
a reference to the Duration object with the assigned millisecond value
Attention
Since negative durations are not allowed, the duration will be clamped to 0

◆ operator==()

constexpr bool iox::units::Duration::operator== ( const Duration rhs) const
inlineconstexprnoexcept

Equal to operator.

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

◆ operator>()

constexpr bool iox::units::Duration::operator> ( const Duration rhs) const
inlineconstexprnoexcept

Greater than operator.

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

◆ operator>=()

constexpr bool iox::units::Duration::operator>= ( const Duration rhs) const
inlineconstexprnoexcept

Greater than or equal to operator.

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

◆ toDays()

constexpr uint64_t iox::units::Duration::toDays ( ) const
inlineconstexprnoexcept

returns the duration in days

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

◆ toHours()

constexpr uint64_t iox::units::Duration::toHours ( ) const
inlineconstexprnoexcept

returns the duration in hours

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

◆ toMicroseconds()

constexpr uint64_t iox::units::Duration::toMicroseconds ( ) const
inlineconstexprnoexcept

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.
Todo:
#607 issue warning or fail

◆ toMilliseconds()

constexpr uint64_t iox::units::Duration::toMilliseconds ( ) const
inlineconstexprnoexcept

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.
Todo:
#607 issue warning or fail

◆ toMinutes()

constexpr uint64_t iox::units::Duration::toMinutes ( ) const
inlineconstexprnoexcept

returns the duration in minutes

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

◆ toNanoseconds()

constexpr uint64_t iox::units::Duration::toNanoseconds ( ) const
inlineconstexprnoexcept

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.
Todo:
#607 issue warning or fail

◆ toSeconds()

constexpr uint64_t iox::units::Duration::toSeconds ( ) const
inlineconstexprnoexcept

returns the duration in seconds

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

Friends And Related Function Documentation

◆ operator*

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

creates Duration object by multiplying object T with a duration

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

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