25 #ifndef SRC_CORE_INCLUDE_CORBO_CORE_TIME_H_ 26 #define SRC_CORE_INCLUDE_CORBO_CORE_TIME_H_ 38 #undef min // this macro conflicts with std::min 39 #undef max // this macro conflicts with std::max 51 static constexpr
bool value =
false;
54 template <
typename Rep,
typename Period>
57 static constexpr
bool value =
true;
63 static constexpr
bool value =
false;
66 template <
typename Clock>
69 static constexpr
bool value =
true;
114 explicit Duration(
int t) { fromSec(static_cast<double>(t)); }
131 template <
typename ChronoDuration>
134 fromChrono(duration);
138 double toSec()
const {
return std::chrono::duration_cast<std::chrono::duration<double>>(_duration).count(); }
140 void fromSec(
double t) { _duration = std::chrono::duration_cast<
DurationType>(std::chrono::duration<double>(t)); }
143 template <
typename ChronoDuration>
147 "Template parameter ChronoDuration must be of type std::chrono::duration");
148 _duration = std::chrono::duration_cast<
DurationType>(duration);
163 template <
typename DurType = std::chrono::duration<
double>>
166 return std::chrono::duration_cast<DurType>(_duration);
169 Time toTime(
double basis_time = 0);
178 void sleep() { std::this_thread::sleep_for(_duration); }
188 return *
static_cast<Duration*
>(
this);
193 return *
static_cast<Duration*
>(
this);
197 fromSec(toSec() * scale);
198 return *
static_cast<Duration*
>(
this);
258 explicit Time(
int t) { fromSec(static_cast<double>(t)); }
260 explicit Time(
double t) { fromSec(t); }
268 template <
typename ChronoTimePo
int>
269 explicit Time(
const ChronoTimePoint& timepoint)
271 fromChrono(timepoint);
281 double toSec()
const {
return std::chrono::duration_cast<std::chrono::duration<double>>(_timepoint.time_since_epoch()).count(); }
283 void fromSec(
double t) { _timepoint =
TimePoint(std::chrono::duration_cast<TimePoint::duration>(std::chrono::duration<double>(t))); }
286 template <
typename ChronoTimePo
int>
290 "Template parameter ChronoTimePoint must be of type std::chrono::time_point");
291 _timepoint = std::chrono::time_point_cast<Clock::duration>(timepoint);
301 return *
static_cast<Time*
>(
this);
306 return *
static_cast<Time*
>(
this);
324 using Clock = std::chrono::high_resolution_clock;
357 explicit Rate(
double frequency) : _cycle_time(1.0 / frequency) {}
378 #endif // SRC_CORE_INCLUDE_CORBO_CORE_TIME_H_ Time(int t)
Construct time object from integer (seconds)
std::chrono::nanoseconds DurationType
bool operator<(const Time &rhs) const
Duration & operator+=(const Duration &rhs)
Duration operator+(const Duration &rhs) const
static void sleepUntil(const Time &time)
Sleep (current thread) until the specified timestamp.
std::chrono::high_resolution_clock Clock
Datatype for the clock.
Time(double t)
Construct time object from double (seconds)
Duration lastCycleTime() const
Return actual execution time of the last cycle.
static Time now()
Retrieve current system time.
Time & operator-=(const Duration &rhs)
bool operator!=(const Duration &rhs) const
Clock::time_point TimePoint
Datatype for the timepoint.
void fromSec(double t)
Set duration from seconds (see Duration(double t))
Time()
Default constructor.
Duration(int t)
Construct duration from integer (seconds)
Duration operator-() const
Time(const ChronoTimePoint &timepoint)
Construct time from std::chrono::time_point object.
Duration operator-(const Duration &rhs) const
bool operator<=(const Duration &rhs) const
static constexpr bool value
Time operator+(const Duration &rhs) const
Duration operator*(double scale) const
Representation of time stamps.
Time & operator+=(const Duration &rhs)
bool operator>(const Duration &rhs) const
Duration & operator-=(const Duration &rhs)
bool operator>(const Time &rhs) const
Rate objects can be used to run loops at a desired rate.
void fromChrono(const ChronoTimePoint &timepoint)
Set time stamp from std::chrono::time_stamp type.
friend std::ostream & operator<<(std::ostream &os, const Time &rhs)
Duration operator-(const Time &rhs) const
void sleep()
Sleep (current thread) for the specified duration.
bool operator!=(const Time &rhs) const
bool operator>=(const Time &rhs) const
Duration()
Default constructor.
bool operator<=(const Time &rhs) const
bool operator>=(const Duration &rhs) const
double toSec() const
Return duration in seconds.
void fromChrono(const ChronoDuration &duration)
Set duration from std::chrono::duration object (see Duration(const ChronoDuration& duration)) ...
Rate(const Duration &dt)
Cosntruct rate object from duration object (interval length)
double toSec() const
Cast time stamp to seconds.
DurType toChrono()
Convert duration to std::chrono::duration object.
Duration(const ChronoDuration &duration)
Construct duration from std::chrono::duration object.
Duration(double t)
Construct duration from double (seconds)
Time operator-(const Duration &rhs) const
void fromSec(double t)
Set time stamp from seconds.
void updateCycleTime(const Duration &dt)
Update cycle time without resetting start time.
bool operator==(const Time &rhs) const
friend std::ostream & operator<<(std::ostream &os, const Duration &rhs)
bool operator==(const Duration &rhs) const
Rate(double frequency)
Construct rate object from frequency [1/s].
Representation of time durations.
Duration & operator*=(double scale)
bool operator<(const Duration &rhs) const