WPILibC++  2019.1.1-beta-1-1-g0a2ab4f
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
Timer.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2018 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #ifndef WPIUTIL_WPI_UV_TIMER_H_
9 #define WPIUTIL_WPI_UV_TIMER_H_
10 
11 #include <uv.h>
12 
13 #include <chrono>
14 #include <functional>
15 #include <memory>
16 
17 #include "wpi/Signal.h"
18 #include "wpi/uv/Handle.h"
19 
20 namespace wpi {
21 namespace uv {
22 
23 class Loop;
24 
29 class Timer final : public HandleImpl<Timer, uv_timer_t> {
30  struct private_init {};
31 
32  public:
33  using Time = std::chrono::duration<uint64_t, std::milli>;
34 
35  explicit Timer(const private_init&) {}
36  ~Timer() noexcept override = default;
37 
43  static std::shared_ptr<Timer> Create(Loop& loop);
44 
50  static std::shared_ptr<Timer> Create(const std::shared_ptr<Loop>& loop) {
51  return Create(*loop);
52  }
53 
61  static void SingleShot(Loop& loop, Time timeout, std::function<void()> func);
62 
70  static void SingleShot(const std::shared_ptr<Loop>& loop, Time timeout,
71  std::function<void()> func) {
72  return SingleShot(*loop, timeout, func);
73  }
74 
87  void Start(Time timeout, Time repeat = Time{0});
88 
92  void Stop() { Invoke(&uv_timer_stop, GetRaw()); }
93 
101  void Again() { Invoke(&uv_timer_again, GetRaw()); }
102 
121  void SetRepeat(Time repeat) { uv_timer_set_repeat(GetRaw(), repeat.count()); }
122 
128  Time GetRepeat() const { return Time{uv_timer_get_repeat(GetRaw())}; }
129 
134 };
135 
136 } // namespace uv
137 } // namespace wpi
138 
139 #endif // WPIUTIL_WPI_UV_TIMER_H_
void SetRepeat(Time repeat)
Set the repeat interval value.
Definition: Timer.h:121
uv_timer_t * GetRaw() const noexcept
Get the underlying handle data structure.
Definition: Handle.h:264
void Start(Time timeout, Time repeat=Time{0})
Start the timer.
Handle.
Definition: Handle.h:249
static void SingleShot(Loop &loop, Time timeout, std::function< void()> func)
Create a timer that calls a functor after a given time interval.
Time GetRepeat() const
Get the timer repeat value.
Definition: Timer.h:128
void Again()
Stop the timer and restart it if it was repeating.
Definition: Timer.h:101
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:70
WPILib C++ utilities (wpiutil) namespace.
Definition: SmallString.h:21
void Stop()
Stop the timer.
Definition: Timer.h:92
static std::shared_ptr< Timer > Create(Loop &loop)
Create a timer handle.
Event loop.
Definition: Loop.h:39
static std::shared_ptr< Timer > Create(const std::shared_ptr< Loop > &loop)
Create a timer handle.
Definition: Timer.h:50
Timer handle.
Definition: Timer.h:29
SignalBase is an implementation of the observer pattern, through the use of an emitting object and sl...
Definition: Signal.h:495
sig::Signal timeout
Signal generated when the timeout event occurs.
Definition: Timer.h:133