WPILibC++ 2025.3.2
Loading...
Searching...
No Matches
Timer.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#ifndef WPINET_UV_TIMER_H_
6#define WPINET_UV_TIMER_H_
7
8#include <uv.h>
9
10#include <chrono>
11#include <functional>
12#include <memory>
13#include <utility>
14
15#include <wpi/Signal.h>
16
17#include "wpinet/uv/Handle.h"
18
19namespace wpi::uv {
20
21class Loop;
22
23/**
24 * Timer handle.
25 * Timer handles are used to schedule signals to be called in the future.
26 */
27class Timer final : public HandleImpl<Timer, uv_timer_t> {
28 struct private_init {};
29
30 public:
31 using Time = std::chrono::duration<uint64_t, std::milli>;
32
33 explicit Timer(const private_init&) {}
34 ~Timer() noexcept override = default;
35
36 /**
37 * Create a timer handle.
38 *
39 * @param loop Loop object where this handle runs.
40 */
41 static std::shared_ptr<Timer> Create(Loop& loop);
42
43 /**
44 * Create a timer handle.
45 *
46 * @param loop Loop object where this handle runs.
47 */
48 static std::shared_ptr<Timer> Create(const std::shared_ptr<Loop>& loop) {
49 return Create(*loop);
50 }
51
52 /**
53 * Create a timer that calls a functor after a given time interval.
54 *
55 * @param loop Loop object where the timer should run.
56 * @param timeout Time interval
57 * @param func Functor
58 */
59 static void SingleShot(Loop& loop, Time timeout, std::function<void()> func);
60
61 /**
62 * Create a timer that calls a functor after a given time interval.
63 *
64 * @param loop Loop object where the timer should run.
65 * @param timeout Time interval
66 * @param func Functor
67 */
68 static void SingleShot(const std::shared_ptr<Loop>& loop, Time timeout,
69 std::function<void()> func) {
70 return SingleShot(*loop, timeout, std::move(func));
71 }
72
73 /**
74 * Start the timer.
75 *
76 * If timeout is zero, an event is emitted on the next event loop
77 * iteration. If repeat is non-zero, an event is emitted first
78 * after timeout milliseconds and then repeatedly after repeat milliseconds.
79 *
80 * @param timeout Milliseconds before to emit an event (use
81 * `std::chrono::duration<uint64_t, std::milli>`).
82 * @param repeat Milliseconds between successive events (use
83 * `std::chrono::duration<uint64_t, std::milli>`).
84 */
85 void Start(Time timeout, Time repeat = Time{0});
86
87 /**
88 * Stop the timer.
89 */
91
92 /**
93 * Stop the timer and restart it if it was repeating.
94 *
95 * Stop the timer, and if it is repeating restart it using the repeat value
96 * as the timeout.
97 * If the timer has never been started before it emits sigError.
98 */
100
101 /**
102 * Set the repeat interval value.
103 *
104 * The timer will be scheduled to run on the given interval and will follow
105 * normal timer semantics in the case of a time-slice overrun.
106 * For example, if a 50ms repeating timer first runs for 17ms, it will be
107 * scheduled to run again 33ms later. If other tasks consume more than the
108 * 33ms following the first timer event, then another event will be emitted
109 * as soon as possible.
110 *
111 * If the repeat value is set from a listener bound to an event, it does
112 * not immediately take effect. If the timer was non-repeating before, it
113 * will have been stopped. If it was repeating, then the old repeat value
114 * will have been used to schedule the next timeout.
115 *
116 * @param repeat Repeat interval in milliseconds (use
117 * `std::chrono::duration<uint64_t, std::milli>`).
118 */
119 void SetRepeat(Time repeat) { uv_timer_set_repeat(GetRaw(), repeat.count()); }
120
121 /**
122 * Get the timer repeat value.
123 * @return Timer repeat value in milliseconds (as a
124 * `std::chrono::duration<uint64_t, std::milli>`).
125 */
127
128 /**
129 * Get the timer due value or 0 if it has expired. The time is relative to
130 * uv_now().
131 *
132 * @return Timer due value in milliseconds (as a
133 * `std::chrono::duration<uint64_t, std::milli>`).
134 */
136
137 /**
138 * Signal generated when the timeout event occurs.
139 */
141};
142
143} // namespace wpi::uv
144
145#endif // WPINET_UV_TIMER_H_
SignalBase is an implementation of the observer pattern, through the use of an emitting object and sl...
Definition Signal.h:495
bool Invoke(F &&f, Args &&... args) const
Definition Handle.h:267
Handle.
Definition Handle.h:290
uv_timer_t * GetRaw() const noexcept
Definition Handle.h:305
Event loop.
Definition Loop.h:37
Timer handle.
Definition Timer.h:27
static void SingleShot(Loop &loop, Time timeout, std::function< void()> func)
Create a timer that calls a functor after a given time interval.
void SetRepeat(Time repeat)
Set the repeat interval value.
Definition Timer.h:119
static std::shared_ptr< Timer > Create(Loop &loop)
Create a timer handle.
sig::Signal timeout
Signal generated when the timeout event occurs.
Definition Timer.h:140
Time GetRepeat() const
Get the timer repeat value.
Definition Timer.h:126
static void SingleShot(const std::shared_ptr< Loop > &loop, Time timeout, std::function< void()> func)
Create a timer that calls a functor after a given time interval.
Definition Timer.h:68
Time GetDueIn() const
Get the timer due value or 0 if it has expired.
Definition Timer.h:135
Timer(const private_init &)
Definition Timer.h:33
void Stop()
Stop the timer.
Definition Timer.h:90
std::chrono::duration< uint64_t, std::milli > Time
Definition Timer.h:31
void Again()
Stop the timer and restart it if it was repeating.
Definition Timer.h:99
void Start(Time timeout, Time repeat=Time{0})
Start the timer.
~Timer() noexcept override=default
Implement std::hash so that hash_code can be used in STL containers.
Definition PointerIntPair.h:280
Definition Loop.h:22
UV_EXTERN int uv_timer_stop(uv_timer_t *handle)
UV_EXTERN uint64_t uv_timer_get_repeat(const uv_timer_t *handle)
UV_EXTERN int uv_timer_again(uv_timer_t *handle)
UV_EXTERN uint64_t uv_timer_get_due_in(const uv_timer_t *handle)
UV_EXTERN void uv_timer_set_repeat(uv_timer_t *handle, uint64_t repeat)