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 
31 namespace lely {
32 namespace io {
33 
35 class 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_
lely::io::Clock::gettime
time_point gettime(::std::error_code &ec) const noexcept
Definition: clock.hpp:69
lely::io::Clock::settime
void settime(const time_point &t, ::std::error_code &ec) noexcept
Definition: clock.hpp:92
lely::io::Clock::settime
void settime(const time_point &t)
Definition: clock.hpp:105
get_errc
int get_errc(void)
Returns the last (thread-specific) native error code set by a system call or library function.
Definition: errnum.c:932
io_clock_gettime
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
lely::io::Clock::getres
duration getres() const
Definition: clock.hpp:60
clock.h
chrono.hpp
set_errc
void set_errc(int errc)
Sets the current (thread-specific) native error code to errc.
Definition: errnum.c:944
io_clock_settime
int io_clock_settime(io_clock_t *clock, const struct timespec *tp)
Sets the time value of the specified clock.
Definition: clock.h:102
lely::io::Clock
An abstract clock. This class is a wrapper around #io_clock_t*.
Definition: clock.hpp:35
lely::io::Clock::getres
duration getres(::std::error_code &ec) const noexcept
Definition: clock.hpp:46
io_clock_getres
int io_clock_getres(const io_clock_t *clock, struct timespec *res)
Obtains the resolution of the specified clock.
Definition: clock.h:90
lely::canopen::make_error_code
::std::error_code make_error_code(SdoErrc e) noexcept
Creates an error code corresponding to an SDO abort code.
Definition: sdo_error.cpp:170
io_clock_t
const struct io_clock_vtbl *const io_clock_t
An abstract clock.
Definition: clock.h:36
error.hpp
lely::io::Clock::gettime
time_point gettime() const
Definition: clock.hpp:83