8 #ifndef WPIUTIL_WPI_UV_LOOP_H_
9 #define WPIUTIL_WPI_UV_LOOP_H_
18 #include "wpi/Signal.h"
19 #include "wpi/uv/Error.h"
37 class Loop final :
public std::enable_shared_from_this<Loop> {
38 struct private_init {};
41 using Time = std::chrono::duration<uint64_t, std::milli>;
44 kDefault = UV_RUN_DEFAULT,
46 kNoWait = UV_RUN_NOWAIT
49 explicit Loop(
const private_init&) noexcept;
52 Loop& operator=(
const Loop&) =
delete;
54 Loop& operator=(
Loop&& oth) =
delete;
63 static std::shared_ptr<Loop>
Create();
97 bool Run(Mode mode = kDefault) {
98 return uv_run(m_loop, static_cast<uv_run_mode>(static_cast<int>(mode))) ==
107 bool IsAlive() const noexcept {
return uv_loop_alive(m_loop) != 0; }
117 void Stop() noexcept { uv_stop(m_loop); }
138 auto to = uv_backend_timeout(m_loop);
139 return std::make_pair(to == -1, Time{to});
154 Time
Now() const noexcept {
return Time{uv_now(m_loop)}; }
174 void Walk(std::function<
void(
Handle&)> callback);
215 template <
typename T =
void>
217 return std::static_pointer_cast<T>(m_data);
224 void SetData(std::shared_ptr<void> data) { m_data = std::move(data); }
238 std::shared_ptr<void> m_data;
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
namespace to hold default to_json function
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