1#ifndef UVW_LOOP_INCLUDE_H
2#define UVW_LOOP_INCLUDE_H
36enum class UVLoopOption : std::underlying_type_t<uv_loop_option> {
37 BLOCK_SIGNAL = UV_LOOP_BLOCK_SIGNAL,
38 IDLE_TIME = UV_METRICS_IDLE_TIME
41enum class UVRunMode : std::underlying_type_t<uv_run_mode> {
42 DEFAULT = UV_RUN_DEFAULT,
44 NOWAIT = UV_RUN_NOWAIT
57class Loop final:
public Emitter<Loop>,
public std::enable_shared_from_this<Loop> {
58 using Deleter = void (*)(uv_loop_t *);
60 template<
typename,
typename>
63 template<
typename R,
typename... Args>
64 auto create_resource(
int, Args &&...args) ->
decltype(std::declval<R>().init(), std::shared_ptr<R>{}) {
65 auto ptr = R::create(shared_from_this(), std::forward<Args>(args)...);
66 ptr = ptr->init() ? ptr :
nullptr;
70 template<
typename R,
typename... Args>
71 std::shared_ptr<R> create_resource(
char, Args &&...args) {
72 return R::create(shared_from_this(), std::forward<Args>(args)...);
75 Loop(std::unique_ptr<uv_loop_t, Deleter> ptr)
noexcept;
78 using Time = std::chrono::duration<uint64_t, std::milli>;
79 using Configure = details::UVLoopOption;
80 using Mode = details::UVRunMode;
86 static std::shared_ptr<Loop>
create();
98 static std::shared_ptr<Loop>
create(uv_loop_t *loop);
117 Loop &operator=(
const Loop &) =
delete;
118 Loop &operator=(
Loop &&other) =
delete;
141 template<typename... Args>
143 auto option =
static_cast<std::underlying_type_t<Configure>
>(flag);
144 auto err = uv_loop_configure(loop.get(),
static_cast<uv_loop_option
>(option), std::forward<Args>(args)...);
158 template<
typename R,
typename... Args>
160 return create_resource<R>(0, std::forward<Args>(args)...);
191 template<Mode mode = Mode::DEFAULT>
227 std::pair<
bool, Time>
timeout() const noexcept;
248 Time
now() const noexcept;
268 template<typename Func>
270 auto func = [](uv_handle_t *handle,
void *func) {
272 auto &cb = *
static_cast<Func *
>(func);
275 case HandleType::ASYNC:
278 case HandleType::CHECK:
281 case HandleType::FS_EVENT:
284 case HandleType::FS_POLL:
287 case HandleType::IDLE:
290 case HandleType::PIPE:
293 case HandleType::POLL:
296 case HandleType::PREPARE:
299 case HandleType::PROCESS:
302 case HandleType::SIGNAL:
305 case HandleType::TCP:
306 cb(*
static_cast<TCPHandle *
>(handle->data));
308 case HandleType::TIMER:
311 case HandleType::TTY:
312 cb(*
static_cast<TTYHandle *
>(handle->data));
314 case HandleType::UDP:
315 cb(*
static_cast<UDPHandle *
>(handle->data));
324 uv_walk(loop.get(), func, &callback);
363 template<typename R =
void>
364 std::shared_ptr<R>
data()
const {
365 return std::static_pointer_cast<R>(userData);
372 void data(std::shared_ptr<void> uData);
389 const uv_loop_t *
raw() const noexcept;
406 uv_loop_t *
raw() noexcept;
409 std::unique_ptr<uv_loop_t, Deleter> loop;
410 std::shared_ptr<
void> userData{
nullptr};
416extern template
bool Loop::run<Loop::Mode::ONCE>() noexcept;
417extern template
bool Loop::run<Loop::Mode::NOWAIT>() noexcept;
Event emitter base class.
The FsEventHandle handle.
void update() const noexcept
Updates the event loop’s concept of now.
static std::shared_ptr< Loop > getDefault()
Gets the initialized default loop.
void fork() noexcept
Reinitialize any kernel state necessary in the child process after a fork(2) system call.
std::shared_ptr< R > resource(Args &&...args)
Creates resources of any type.
static std::shared_ptr< Loop > create(uv_loop_t *loop)
Initializes a new Loop instance from an existing resource.
const uv_loop_t * raw() const noexcept
Gets the underlying raw data structure.
void walk(Func callback)
Walks the list of handles.
static std::shared_ptr< Loop > create()
Initializes a new Loop instance.
void configure(Configure flag, Args &&...args)
Sets additional loop options.
void data(std::shared_ptr< void > uData)
Sets arbitrary data. uvw won't use this field in any case.
void stop() noexcept
Stops the event loop.
bool run() noexcept
Runs the event loop.
Time idleTime() const noexcept
Returns the amount of time the event loop has been idle. The call is thread safe.
std::shared_ptr< R > data() const
Gets user-defined data. uvw won't use this field in any case.
Time now() const noexcept
Returns the current timestamp in milliseconds.
int descriptor() const noexcept
Get backend file descriptor.
std::pair< bool, Time > timeout() const noexcept
Gets the poll timeout.
bool alive() const noexcept
Checks if there are active resources.
void close()
Releases all internal loop resources.
The PrepareHandle handle.
The ProcessHandle handle.
Common class for almost all the resources available in uvw.
details::UVTypeWrapper< uv_handle_type > HandleCategory
static HandleType guessHandle(HandleCategory category) noexcept
Gets the type of the handle given a category.