WPILibC++  2018.4.1-20180921134745-1191-g456d3e1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
Loop.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_LOOP_H_
9 #define WPIUTIL_WPI_UV_LOOP_H_
10 
11 #include <uv.h>
12 
13 #include <chrono>
14 #include <functional>
15 #include <memory>
16 #include <utility>
17 
18 #include "wpi/Signal.h"
19 #include "wpi/uv/Error.h"
20 
21 namespace wpi {
22 namespace uv {
23 
24 class Handle;
25 
37 class Loop final : public std::enable_shared_from_this<Loop> {
38  struct private_init {};
39 
40  public:
41  using Time = std::chrono::duration<uint64_t, std::milli>;
42 
43  enum Mode {
44  kDefault = UV_RUN_DEFAULT,
45  kOnce = UV_RUN_ONCE,
46  kNoWait = UV_RUN_NOWAIT
47  };
48 
49  explicit Loop(const private_init&) noexcept;
50 
51  Loop(const Loop&) = delete;
52  Loop& operator=(const Loop&) = delete;
53  Loop(Loop&& oth) = delete;
54  Loop& operator=(Loop&& oth) = delete;
55 
56  ~Loop() noexcept;
57 
63  static std::shared_ptr<Loop> Create();
64 
71  static std::shared_ptr<Loop> GetDefault();
72 
81  void Close();
82 
97  bool Run(Mode mode = kDefault) {
98  return uv_run(m_loop, static_cast<uv_run_mode>(static_cast<int>(mode))) ==
99  0;
100  }
101 
107  bool IsAlive() const noexcept { return uv_loop_alive(m_loop) != 0; }
108 
117  void Stop() noexcept { uv_stop(m_loop); }
118 
128  int GetDescriptor() const noexcept { return uv_backend_fd(m_loop); }
129 
137  std::pair<bool, Time> GetTimeout() const noexcept {
138  auto to = uv_backend_timeout(m_loop);
139  return std::make_pair(to == -1, Time{to});
140  }
141 
154  Time Now() const noexcept { return Time{uv_now(m_loop)}; }
155 
165  void UpdateTime() noexcept { uv_update_time(m_loop); }
166 
174  void Walk(std::function<void(Handle&)> callback);
175 
202  void Fork();
203 
209  uv_loop_t* GetRaw() const noexcept { return m_loop; }
210 
215  template <typename T = void>
216  std::shared_ptr<T> GetData() const {
217  return std::static_pointer_cast<T>(m_data);
218  }
219 
224  void SetData(std::shared_ptr<void> data) { m_data = std::move(data); }
225 
230 
235  void ReportError(int err) { error(Error(err)); }
236 
237  private:
238  std::shared_ptr<void> m_data;
239  uv_loop_t* m_loop;
240  uv_loop_t m_loopStruct;
241 };
242 
243 } // namespace uv
244 } // namespace wpi
245 
246 #endif // WPIUTIL_WPI_UV_LOOP_H_
static std::shared_ptr< Loop > GetDefault()
Create the default event loop.
void Stop() noexcept
Stop the event loop.
Definition: Loop.h:117
int GetDescriptor() const noexcept
Get backend file descriptor.
Definition: Loop.h:128
void ReportError(int err)
Reports error.
Definition: Loop.h:235
uv_loop_t * GetRaw() const noexcept
Get the underlying event loop data structure.
Definition: Loop.h:209
Definition: uv.h:1542
WPILib C++ utilities (wpiutil) namespace.
Definition: SmallString.h:21
std::shared_ptr< T > GetData() const
Gets user-defined data.
Definition: Loop.h:216
bool Run(Mode mode=kDefault)
Run the event loop.
Definition: Loop.h:97
bool IsAlive() const noexcept
Check if there are active resources.
Definition: Loop.h:107
sig::Signal< Error > error
Error signal.
Definition: Loop.h:229
Event loop.
Definition: Loop.h:37
std::pair< bool, Time > GetTimeout() const noexcept
Get the poll timeout.
Definition: Loop.h:137
static std::shared_ptr< Loop > Create()
Create a new event loop.
void Close()
Release all internal loop resources.
Time Now() const noexcept
Return the current timestamp in milliseconds.
Definition: Loop.h:154
Error code.
Definition: Error.h:19
SignalBase is an implementation of the observer pattern, through the use of an emitting object and sl...
Definition: Signal.h:495
void SetData(std::shared_ptr< void > data)
Sets user-defined data.
Definition: Loop.h:224
Handle.
Definition: Handle.h:32
void Walk(std::function< void(Handle &)> callback)
Walk the list of handles.
void Fork()
Reinitialize any kernel state necessary in the child process after a fork(2) system call...
void UpdateTime() noexcept
Update the event loop’s concept of now.
Definition: Loop.h:165