Lely core libraries  2.3.4
timer.hpp
Go to the documentation of this file.
1 
24 #ifndef LELY_IO2_USER_TIMER_HPP_
25 #define LELY_IO2_USER_TIMER_HPP_
26 
28 #include <lely/io2/user/timer.h>
29 #include <lely/io2/timer.hpp>
30 
31 #include <utility>
32 
33 namespace lely {
34 namespace io {
35 
37 class UserTimer : public TimerBase {
38  public:
41  io_user_timer_setnext_t* func = nullptr, void* arg = nullptr)
42  : TimerBase(io_user_timer_create(ctx, exec, func, arg)) {
43  if (!timer) util::throw_errc("UserTimer");
44  }
45 
46  UserTimer(const UserTimer&) = delete;
47 
48  UserTimer(UserTimer&& other) noexcept : TimerBase(other.timer) {
49  other.timer = nullptr;
50  other.dev = nullptr;
51  }
52 
53  UserTimer& operator=(const UserTimer&) = delete;
54 
55  UserTimer&
56  operator=(UserTimer&& other) noexcept {
57  using ::std::swap;
58  swap(timer, other.timer);
59  swap(dev, other.dev);
60  return *this;
61  }
62 
65 };
66 
67 template <class T, class U = typename ::std::decay<T>::type>
68 inline
69  typename ::std::enable_if<compat::is_invocable<U, const timespec*>::value,
70  UserTimer>::type
71  make_user_timer(io_ctx_t* ctx, ev_exec_t* exec,
72  ::std::reference_wrapper<T> obj) {
73  return UserTimer(
74  ctx, exec,
75  [](const timespec* tp, void* arg) noexcept {
76  auto* obj = static_cast<T*>(arg);
77  (*obj)(tp);
78  },
79  static_cast<void*>(const_cast<U*>(&obj.get())));
80 }
81 
82 template <class T, class U = typename ::std::decay<T>::type>
83 inline typename ::std::enable_if<
84  compat::is_invocable<U, const UserTimer::time_point&>::value,
85  UserTimer>::type
86 make_user_timer(io_ctx_t* ctx, ev_exec_t* exec,
87  ::std::reference_wrapper<T> obj) {
88  return UserTimer(
89  ctx, exec,
90  [](const timespec* tp, void* arg) noexcept {
91  auto* obj = static_cast<T*>(arg);
92  (*obj)(UserTimer::time_point{util::from_timespec(*tp)});
93  },
94  static_cast<void*>(const_cast<U*>(&obj.get())));
95 }
96 
97 template <class C, void (C::*M)(const timespec*)>
98 inline UserTimer
99 make_user_timer(io_ctx_t* ctx, ev_exec_t* exec, C* obj) {
100  return UserTimer(
101  ctx, exec,
102  [](const timespec* tp, void* arg) noexcept {
103  auto obj = static_cast<C*>(arg);
104  (obj->*M)(tp);
105  },
106  static_cast<void*>(obj));
107 }
108 
109 template <class C, void (C::*M)(const UserTimer::time_point&)>
110 inline UserTimer
111 make_user_timer(io_ctx_t* ctx, ev_exec_t* exec, C* obj) {
112  return UserTimer(
113  ctx, exec,
114  [](const timespec* tp, void* arg) noexcept {
115  auto obj = static_cast<C*>(arg);
116  (obj->*M)(UserTimer::time_point{util::from_timespec(*tp)});
117  },
118  static_cast<void*>(obj));
119 }
120 
121 } // namespace io
122 } // namespace lely
123 
124 #endif // !LELY_IO2_USER_TIMER_HPP_
ev_exec_t
const struct ev_exec_vtbl *const ev_exec_t
An abstract task executor.
Definition: ev.h:29
lely::io::TimerBase
A reference to an abstract timer.
Definition: timer.hpp:130
io_user_timer_create
io_timer_t * io_user_timer_create(io_ctx_t *ctx, ev_exec_t *exec, io_user_timer_setnext_t *func, void *arg)
Creates a new user-defined timer.
Definition: timer.c:213
type_traits.hpp
timer.h
lely::io::UserTimer::UserTimer
UserTimer(io_ctx_t *ctx, ev_exec_t *exec, io_user_timer_setnext_t *func=nullptr, void *arg=nullptr)
Definition: timer.hpp:40
timer.hpp
io_user_timer_destroy
void io_user_timer_destroy(io_timer_t *timer)
Destroys a user-defined timer.
Definition: timer.c:241
lely::io::UserTimer
A user-defined timer.
Definition: timer.hpp:37
io_user_timer_setnext_t
void io_user_timer_setnext_t(const struct timespec *tp, void *arg)
The type of function invoked by a user-defined timer when the expiration time is updated with io_time...
Definition: timer.h:45
io_ctx
Definition: ctx.c:38
lely::io::UserTimer::~UserTimer
~UserTimer()
Definition: timer.hpp:64