Lely core libraries 2.3.4
clock.hpp
Go to the documentation of this file.
1
24#ifndef LELY_IO2_CLOCK_HPP_
25#define LELY_IO2_CLOCK_HPP_
26
27#include <lely/io2/clock.h>
28#include <lely/util/chrono.hpp>
29#include <lely/util/error.hpp>
30
31namespace lely {
32namespace io {
33
35class Clock {
36 public:
37 using duration = ::std::chrono::nanoseconds;
38 using time_point = ::std::chrono::time_point<Clock, duration>;
39
40 explicit constexpr Clock(io_clock_t* clock) noexcept : clock_(clock) {}
41
42 operator io_clock_t*() const noexcept { return clock_; }
43
45 duration
46 getres(::std::error_code& ec) const noexcept {
47 int errsv = get_errc();
48 set_errc(0);
49 timespec res = {0, 0};
50 if (!io_clock_getres(*this, &res))
51 ec.clear();
52 else
53 ec = util::make_error_code();
54 set_errc(errsv);
55 return util::from_timespec(res);
56 }
57
59 duration
60 getres() const {
61 ::std::error_code ec;
62 auto res = getres(ec);
63 if (ec) throw ::std::system_error(ec, "getres");
64 return res;
65 }
66
68 time_point
69 gettime(::std::error_code& ec) const noexcept {
70 int errsv = get_errc();
71 set_errc(0);
72 timespec ts = {0, 0};
73 if (!io_clock_gettime(*this, &ts))
74 ec.clear();
75 else
76 ec = util::make_error_code();
77 set_errc(errsv);
78 return time_point(util::from_timespec(ts));
79 }
80
82 time_point
83 gettime() const {
84 ::std::error_code ec;
85 auto t = gettime(ec);
86 if (ec) throw ::std::system_error(ec, "gettime");
87 return t;
88 }
89
91 void
92 settime(const time_point& t, ::std::error_code& ec) noexcept {
93 int errsv = get_errc();
94 set_errc(0);
95 auto ts = util::to_timespec(t);
96 if (!io_clock_settime(*this, &ts))
97 ec.clear();
98 else
99 ec = util::make_error_code();
100 set_errc(errsv);
101 }
102
104 void
105 settime(const time_point& t) {
106 ::std::error_code ec;
107 settime(t, ec);
108 if (ec) throw ::std::system_error(ec, "settime");
109 }
110
111 private:
112 io_clock_t* clock_{nullptr};
113};
114
115} // namespace io
116} // namespace lely
117
118#endif // !LELY_IO2_CLOCK_HPP_
A CANopen value.
Definition val.hpp:42
An abstract clock. This class is a wrapper around #io_clock_t*.
Definition clock.hpp:35
void settime(const time_point &t)
Definition clock.hpp:105
duration getres(::std::error_code &ec) const noexcept
Definition clock.hpp:46
time_point gettime() const
Definition clock.hpp:83
void settime(const time_point &t, ::std::error_code &ec) noexcept
Definition clock.hpp:92
duration getres() const
Definition clock.hpp:60
time_point gettime(::std::error_code &ec) const noexcept
Definition clock.hpp:69
This header file is part of the I/O library; it contains the abstract clock interface.
int io_clock_settime(io_clock_t *clock, const struct timespec *tp)
Sets the time value of the specified clock.
Definition clock.h:102
int io_clock_gettime(const io_clock_t *clock, struct timespec *tp)
Obtains the current time value of the specified clock.
Definition clock.h:96
int io_clock_getres(const io_clock_t *clock, struct timespec *res)
Obtains the resolution of the specified clock.
Definition clock.h:90
const struct io_clock_vtbl *const io_clock_t
An abstract clock.
Definition clock.h:36
int get_errc(void)
Returns the last (thread-specific) native error code set by a system call or library function.
Definition errnum.c:932
void set_errc(int errc)
Sets the current (thread-specific) native error code to errc.
Definition errnum.c:944
This header file is part of the utilities library; it contains C++ convenience functions for creating...
A time type with nanosecond resolution.
Definition time.h:88
This header file is part of the utilities library; it contains the time function declarations.