4 #include "sol3/core/msg/stamp.h"
12 std::chrono::system_clock::time_point
const& time_point) {
14 out.mutate_clock(msg::ClockType_WALL_UTC);
15 out.mutate_ns(time_point.time_since_epoch().count());
21 std::chrono::steady_clock::time_point
const& time_point) {
23 out.mutate_clock(msg::ClockType_MONOTONIC);
24 out.mutate_ns(time_point.time_since_epoch().count());
29 msg::TimePoint time_point) {
31 time_point.clock() == msg::ClockType_WALL_UTC,
32 "Wrong clock type {}",
33 msg::EnumNameClockType(time_point.clock()));
34 return std::chrono::system_clock::time_point() +
35 std::chrono::nanoseconds(time_point.ns());
39 template <
typename TClock = std::chrono::system_clock>
41 typename TClock::time_point
const& time_point,
42 msg::StampType stamp_type,
44 return msg::Stamp(
timePoint(time_point), stamp_type, host_id);
48 template <
typename TClock = std::chrono::system_clock>
50 msg::StampType stamp_type,
53 return stampFrom<TClock>(TClock::now(), stamp_type, host_id);
57 template <
typename TClock = std::chrono::system_clock>
60 return stampNow<TClock>(msg::StampType_SEND, host_id);
64 template <
typename TClock = std::chrono::system_clock>
67 return stampNow<TClock>(msg::StampType_RECV, host_id);
71 template <
typename TClock = std::chrono::system_clock>
73 typename TClock::time_point time_point,
75 return stampFrom<TClock>(time_point, msg::StampType_MEASUREMENT, host_id);
78 inline std::chrono::nanoseconds
toDuration(
double seconds) {
79 return std::chrono::duration_cast<std::chrono::nanoseconds>(
80 std::chrono::duration<double>(seconds));
83 template <
typename TRep,
typename TPeriod>
84 double toSeconds(std::chrono::duration<TRep, TPeriod>
const& duration) {
85 return std::chrono::duration_cast<std::chrono::duration<double>>(duration)
89 template <
typename TClock>
90 double toSeconds(std::chrono::time_point<TClock>
const& time_point) {
91 return toSeconds(time_point.time_since_epoch());
95 template <
class TClock = std::chrono::steady_clock>
102 : period_(period), anchor_(anchor), deadline_(Duration::zero()) {
103 assert(period_ > Duration::zero());
112 if (now < deadline_) {
125 return anchor_ + ((now - anchor_) / period_ + 1) * period_;
135 template <
class TP0,
class... TPn>
137 return (std::min)({first, rest...});
Fixed-rate, phase-aligned ticker for periodic scheduling.
Definition: time.h:96
TimePoint nextDeadline(TimePoint now) const
Compute the next aligned deadline strictly after now
Definition: time.h:120
bool due(TimePoint now)
Definition: time.h:111
typename TClock::time_point TimePoint
Definition: time.h:98
Periodic(Duration period, TimePoint anchor={})
Definition: time.h:101
#define SOL3_EXPECT(condition,...)
Definition: except.h:222
Definition: any_message_input.h:15
constexpr TP0 minTimePoint(const TP0 &first, TPn const &... rest)
Returns the earliest of the given time points.
Definition: time.h:136
msg::Stamp stampFrom(typename TClock::time_point const &time_point, msg::StampType stamp_type, uint16_t host_id=hostIdentity().host_id_numeric)
Creates a msg::Stamp from std::chrono::stead_block::time_point.
Definition: time.h:40
msg::Stamp recvStampNow(uint16_t host_id=hostIdentity().host_id_numeric)
Creates a receive timestamp using the specified clock.
Definition: time.h:65
msg::TimePoint timePoint(std::chrono::system_clock::time_point const &time_point)
Creates a msg::TimePoint from std::chrono::system_clock::time_point.
Definition: time.h:11
msg::Stamp stampNow(msg::StampType stamp_type, uint16_t host_id=hostIdentity().host_id_numeric)
Creates a send timestamp using the specified clock.
Definition: time.h:49
msg::Stamp sendStampNow(uint16_t host_id=hostIdentity().host_id_numeric)
Creates a send timestamp using the specified clock.
Definition: time.h:58
msg::Stamp measurementStampFrom(typename TClock::time_point time_point, uint16_t host_id=hostIdentity().host_id_numeric)
Creates a receive timestamp using the specified clock.
Definition: time.h:72
msg::HostIdConfigT const & hostIdentity()
std::chrono::system_clock::time_point toChronoSystemClock(msg::TimePoint time_point)
Definition: time.h:28
double toSeconds(std::chrono::duration< TRep, TPeriod > const &duration)
Definition: time.h:84
std::chrono::nanoseconds toDuration(double seconds)
Definition: time.h:78
Duration type.
Definition: stamp.fbs:4