uvw  1.3.0
timer.hpp
1 #pragma once
2 
3 
4 #include <utility>
5 #include <memory>
6 #include <chrono>
7 #include <uv.h>
8 #include "handle.hpp"
9 #include "loop.hpp"
10 
11 
12 namespace uvw {
13 
14 
20 struct TimerEvent {};
21 
22 
30 class TimerHandle final: public Handle<TimerHandle, uv_timer_t> {
31  static void startCallback(uv_timer_t *handle) {
32  TimerHandle &timer = *(static_cast<TimerHandle*>(handle->data));
33  timer.publish(TimerEvent{});
34  }
35 
36 public:
37  using Time = std::chrono::duration<uint64_t, std::milli>;
38 
39  using Handle::Handle;
40 
45  bool init() {
46  return initialize(&uv_timer_init);
47  }
48 
61  void start(Time timeout, Time repeat) {
62  invoke(&uv_timer_start, get(), &startCallback, timeout.count(), repeat.count());
63  }
64 
68  void stop() {
69  invoke(&uv_timer_stop, get());
70  }
71 
79  void again() {
80  invoke(&uv_timer_again, get());
81  }
82 
101  void repeat(Time repeat) {
102  uv_timer_set_repeat(get(), repeat.count());
103  }
104 
110  Time repeat() {
111  return Time{uv_timer_get_repeat(get())};
112  }
113 };
114 
115 
116 }
Time repeat()
Gets the timer repeat value.
Definition: timer.hpp:110
Handle base class.
Definition: handle.hpp:29
The TimerHandle handle.
Definition: timer.hpp:30
bool init()
Initializes the handle.
Definition: timer.hpp:45
void stop()
Stops the handle.
Definition: timer.hpp:68
void again()
Stops the timer and restarts it if it was repeating.
Definition: timer.hpp:79
void repeat(Time repeat)
Sets the repeat interval value.
Definition: timer.hpp:101
void start(Time timeout, Time repeat)
Starts the timer.
Definition: timer.hpp:61
TimerEvent event.
Definition: timer.hpp:20
uvw default namespace.
Definition: async.hpp:11