Public Member Functions | Private Types | Private Attributes | Friends | List of all members
corbo::Duration Class Reference

Representation of time durations. More...

#include <time.h>

Public Member Functions

 Duration ()
 Default constructor. More...
 
 Duration (int t)
 Construct duration from integer (seconds) More...
 
 Duration (double t)
 Construct duration from double (seconds) More...
 
template<typename ChronoDuration >
 Duration (const ChronoDuration &duration)
 Construct duration from std::chrono::duration object. More...
 
template<typename ChronoDuration >
void fromChrono (const ChronoDuration &duration)
 Set duration from std::chrono::duration object (see Duration(const ChronoDuration& duration)) More...
 
void fromSec (double t)
 Set duration from seconds (see Duration(double t)) More...
 
bool operator!= (const Duration &rhs) const
 
Duration operator* (double scale) const
 
Durationoperator*= (double scale)
 
Duration operator+ (const Duration &rhs) const
 
Durationoperator+= (const Duration &rhs)
 
Duration operator- (const Duration &rhs) const
 
Duration operator- () const
 
Durationoperator-= (const Duration &rhs)
 
bool operator< (const Duration &rhs) const
 
bool operator<= (const Duration &rhs) const
 
bool operator== (const Duration &rhs) const
 
bool operator> (const Duration &rhs) const
 
bool operator>= (const Duration &rhs) const
 
void sleep ()
 Sleep (current thread) for the specified duration. More...
 
template<typename DurType = std::chrono::duration<double>>
DurType toChrono ()
 Convert duration to std::chrono::duration object. More...
 
double toSec () const
 Return duration in seconds. More...
 
Time toTime (double basis_time=0)
 

Private Types

using DurationType = std::chrono::nanoseconds
 

Private Attributes

DurationType _duration
 

Friends

std::ostream & operator<< (std::ostream &os, const Duration &rhs)
 
class Time
 

Detailed Description

Representation of time durations.

This object stores time durations and provides convenient methods and operator overloads for common operations with time and durations.

A duration object can be constructed from a value given in seconds or in a generic way from a std::chrono<> type.

In comparison to durations, actual time stamps are managed with the Time class.

The following arithmetic operators (in combination with Time objects) are provided:

Duration + Duration = Duration Duration - Duration = Duration Duration * double = Duration Duration == Duration = bool Duration > Duration == bool Duration < Duration == bool

This class is inspired and based on the ROS class ros::Duration http://wiki.ros.org/roscpp/Overview/Time

See also
Time Rate
Author
Christoph Rösmann (chris.nosp@m.toph.nosp@m..roes.nosp@m.mann.nosp@m.@tu-d.nosp@m.ortm.nosp@m.und.d.nosp@m.e)

Definition at line 106 of file time.h.

Member Typedef Documentation

◆ DurationType

using corbo::Duration::DurationType = std::chrono::nanoseconds
private

Definition at line 215 of file time.h.

Constructor & Destructor Documentation

◆ Duration() [1/4]

corbo::Duration::Duration ( )
inline

Default constructor.

Definition at line 112 of file time.h.

◆ Duration() [2/4]

corbo::Duration::Duration ( int  t)
inlineexplicit

Construct duration from integer (seconds)

Definition at line 114 of file time.h.

◆ Duration() [3/4]

corbo::Duration::Duration ( double  t)
inlineexplicit

Construct duration from double (seconds)

Definition at line 116 of file time.h.

◆ Duration() [4/4]

template<typename ChronoDuration >
corbo::Duration::Duration ( const ChronoDuration &  duration)
inlineexplicit

Construct duration from std::chrono::duration object.

E.g.:

Duration my_duration1 = Duration(std::chrono::milliseconds(10));
Duration my_duration2 = Duration(std::chrono::microseconds(50));
Duration my_duration3 = Duration(std::chrono::hours(1));
Parameters
[in]durationobject of template type ChronoDuration
Template Parameters
ChronoDurationTemplate for a specific std::chrono::duration<> type

Definition at line 132 of file time.h.

Member Function Documentation

◆ fromChrono()

template<typename ChronoDuration >
void corbo::Duration::fromChrono ( const ChronoDuration &  duration)
inline

Set duration from std::chrono::duration object (see Duration(const ChronoDuration& duration))

Definition at line 144 of file time.h.

◆ fromSec()

void corbo::Duration::fromSec ( double  t)
inline

Set duration from seconds (see Duration(double t))

Definition at line 140 of file time.h.

◆ operator!=()

bool corbo::Duration::operator!= ( const Duration rhs) const
inline

Definition at line 201 of file time.h.

◆ operator*()

Duration corbo::Duration::operator* ( double  scale) const
inline

Definition at line 184 of file time.h.

◆ operator*=()

Duration& corbo::Duration::operator*= ( double  scale)
inline

Definition at line 195 of file time.h.

◆ operator+()

Duration corbo::Duration::operator+ ( const Duration rhs) const
inline

Definition at line 181 of file time.h.

◆ operator+=()

Duration& corbo::Duration::operator+= ( const Duration rhs)
inline

Definition at line 185 of file time.h.

◆ operator-() [1/2]

Duration corbo::Duration::operator- ( const Duration rhs) const
inline

Definition at line 182 of file time.h.

◆ operator-() [2/2]

Duration corbo::Duration::operator- ( ) const
inline

Definition at line 183 of file time.h.

◆ operator-=()

Duration& corbo::Duration::operator-= ( const Duration rhs)
inline

Definition at line 190 of file time.h.

◆ operator<()

bool corbo::Duration::operator< ( const Duration rhs) const
inline

Definition at line 203 of file time.h.

◆ operator<=()

bool corbo::Duration::operator<= ( const Duration rhs) const
inline

Definition at line 205 of file time.h.

◆ operator==()

bool corbo::Duration::operator== ( const Duration rhs) const
inline

Definition at line 200 of file time.h.

◆ operator>()

bool corbo::Duration::operator> ( const Duration rhs) const
inline

Definition at line 202 of file time.h.

◆ operator>=()

bool corbo::Duration::operator>= ( const Duration rhs) const
inline

Definition at line 204 of file time.h.

◆ sleep()

void corbo::Duration::sleep ( )
inline

Sleep (current thread) for the specified duration.

Duration(2).sleep(); // pause current thread for 2 seconds

Definition at line 178 of file time.h.

◆ toChrono()

template<typename DurType = std::chrono::duration<double>>
DurType corbo::Duration::toChrono ( )
inline

Convert duration to std::chrono::duration object.

The exact chrono type might be specified as template argument, e.g.:

auto dur = Duration(5).toChrono<std::chrono::duration<double>>(); // value in seconds, floating point
auto dur = Duration(5).toChrono<std::chrono::microseconds>();
auto dur = Duration(5).toChrono<std::chrono::milliseconds>();
Returns
desired std::chrono::duration<> instance

Definition at line 164 of file time.h.

◆ toSec()

double corbo::Duration::toSec ( ) const
inline

Return duration in seconds.

Definition at line 138 of file time.h.

◆ toTime()

Time corbo::Duration::toTime ( double  basis_time = 0)

Definition at line 54 of file time.cpp.

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  os,
const Duration rhs 
)
friend

Definition at line 207 of file time.h.

◆ Time

friend class Time
friend

Definition at line 108 of file time.h.

Member Data Documentation

◆ _duration

DurationType corbo::Duration::_duration
private

Definition at line 216 of file time.h.


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


control_box_rst
Author(s): Christoph Rösmann
autogenerated on Mon Feb 28 2022 22:08:02