1#ifndef UVW_LOOP_INCLUDE_H
2#define UVW_LOOP_INCLUDE_H
37enum class uvw_loop_option : std::underlying_type_t<uv_loop_option> {
38 BLOCK_SIGNAL = UV_LOOP_BLOCK_SIGNAL,
39 IDLE_TIME = UV_METRICS_IDLE_TIME
42enum class uvw_run_mode : std::underlying_type_t<uv_run_mode> {
43 DEFAULT = UV_RUN_DEFAULT,
45 NOWAIT = UV_RUN_NOWAIT
58class loop final:
public emitter<loop>,
public std::enable_shared_from_this<loop> {
59 using deleter = void (*)(uv_loop_t *);
61 template<
typename,
typename,
typename...>
66 explicit uv_token(
int) {}
69 loop(std::unique_ptr<uv_loop_t, deleter> ptr)
noexcept;
72 using token = uv_token;
73 using time = std::chrono::duration<uint64_t, std::milli>;
74 using option = details::uvw_loop_option;
75 using run_mode = details::uvw_run_mode;
81 static std::shared_ptr<loop>
create();
93 static std::shared_ptr<loop>
create(uv_loop_t *res);
112 loop &operator=(
const loop &) =
delete;
113 loop &operator=(
loop &&other) =
delete;
136 template<typename... Args>
138 return uv_loop_configure(uv_loop.get(),
static_cast<uv_loop_option
>(flag), std::forward<Args>(args)...);
151 template<
typename R,
typename... Args>
153 auto ptr = uninitialized_resource<R>(std::forward<Args>(args)...);
154 ptr = (ptr->init() == 0) ? ptr :
nullptr;
162 template<
typename R,
typename... Args>
164 return std::make_shared<R>(token{0}, shared_from_this(), std::forward<Args>(args)...);
195 int run(run_mode mode = run_mode::DEFAULT)
noexcept;
230 std::pair<
bool, time>
timeout() const noexcept;
251 time
now() const noexcept;
271 template<typename Func>
273 auto func = [](uv_handle_t *hndl,
void *func) {
275 auto &cb = *
static_cast<Func *
>(func);
278 case handle_type::ASYNC:
281 case handle_type::CHECK:
284 case handle_type::FS_EVENT:
287 case handle_type::FS_POLL:
290 case handle_type::IDLE:
293 case handle_type::PIPE:
296 case handle_type::POLL:
299 case handle_type::PREPARE:
302 case handle_type::PROCESS:
305 case handle_type::SIGNAL:
308 case handle_type::TCP:
311 case handle_type::TIMER:
314 case handle_type::TTY:
317 case handle_type::UDP:
327 uv_walk(uv_loop.get(), func, &callback);
366 template<typename R =
void>
367 std::shared_ptr<R>
data()
const {
368 return std::static_pointer_cast<R>(user_data);
375 void data(std::shared_ptr<void> ud);
392 const uv_loop_t *
raw() const noexcept;
409 uv_loop_t *
raw() noexcept;
412 std::unique_ptr<uv_loop_t, deleter> uv_loop;
413 std::shared_ptr<
void> user_data{
nullptr};
Event emitter base class.
int run(run_mode mode=run_mode::DEFAULT) noexcept
Runs the event loop.
std::shared_ptr< R > data() const
Gets user-defined data. uvw won't use this field in any case.
int 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.
const uv_loop_t * raw() const noexcept
Gets the underlying raw data structure.
bool alive() const noexcept
Checks if there are active resources.
static std::shared_ptr< loop > get_default()
Gets the initialized default loop.
void update() const noexcept
Updates the event loop’s concept of now.
static std::shared_ptr< loop > create()
Initializes a new loop instance.
void data(std::shared_ptr< void > ud)
Sets arbitrary data. uvw won't use this field in any case.
std::pair< bool, time > timeout() const noexcept
Gets the poll timeout.
void walk(Func callback)
Walks the list of handles.
void stop() noexcept
Stops the event loop.
time now() const noexcept
Returns the current timestamp in milliseconds.
int close()
Releases all internal loop resources.
int configure(option flag, Args &&...args)
Sets additional loop options.
static std::shared_ptr< loop > create(uv_loop_t *res)
Initializes a new loop instance from an existing resource.
int descriptor() const noexcept
Get backend file descriptor.
time idle_time() const noexcept
Returns the amount of time the event loop has been idle. The call is thread safe.
std::shared_ptr< R > uninitialized_resource(Args &&...args)
Creates uninitialized resources of any type.
Common class for almost all the resources available in uvw.
details::uv_type_wrapper< uv_handle_type > handle_category
static handle_type guess_handle(handle_category category) noexcept
Gets the type of the handle given a category.