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
60class loop final:
public emitter<loop>,
public std::enable_shared_from_this<loop> {
61 using deleter = void (*)(uv_loop_t *);
63 template<
typename,
typename,
typename...>
64 friend class resource;
68 explicit uv_token(
int) {}
71 template<
typename Type>
72 auto init(
int, Type &value) ->
decltype(value.init()) {
76 template<
typename Type>
77 int init(
char, Type &) {
81 loop(std::unique_ptr<uv_loop_t, deleter> ptr)
noexcept;
84 using token = uv_token;
85 using time = std::chrono::duration<uint64_t, std::milli>;
86 using option = details::uvw_loop_option;
87 using run_mode = details::uvw_run_mode;
93 static std::shared_ptr<loop>
create();
105 static std::shared_ptr<loop>
create(uv_loop_t *res);
121 loop(
const loop &) =
delete;
122 loop(loop &&other) =
delete;
124 loop &operator=(
const loop &) =
delete;
125 loop &operator=(loop &&other) =
delete;
148 template<typename... Args>
150 return uv_loop_configure(uv_loop.get(),
static_cast<uv_loop_option
>(flag), std::forward<Args>(args)...);
163 template<
typename R,
typename... Args>
166 return (init(0, *ptr) == 0) ? ptr :
nullptr;
173 template<
typename R,
typename... Args>
175 return std::make_shared<R>(token{0}, shared_from_this(), std::forward<Args>(args)...);
206 int run(run_mode mode = run_mode::DEFAULT)
noexcept;
241 std::pair<
bool, time>
timeout() const noexcept;
268 time
now() const noexcept;
288 template<typename Func>
290 auto func = [](uv_handle_t *hndl,
void *callback_func) {
292 auto &cb = *
static_cast<Func *
>(callback_func);
295 case handle_type::ASYNC:
298 case handle_type::CHECK:
301 case handle_type::FS_EVENT:
304 case handle_type::FS_POLL:
307 case handle_type::IDLE:
310 case handle_type::PIPE:
313 case handle_type::POLL:
316 case handle_type::PREPARE:
319 case handle_type::PROCESS:
322 case handle_type::SIGNAL:
325 case handle_type::TCP:
328 case handle_type::TIMER:
331 case handle_type::TTY:
334 case handle_type::UDP:
344 uv_walk(uv_loop.get(), func, &callback);
383 template<typename R =
void>
384 std::shared_ptr<R>
data()
const {
385 return std::static_pointer_cast<R>(user_data);
392 void data(std::shared_ptr<void> ud);
409 const uv_loop_t *
raw() const noexcept;
426 uv_loop_t *
raw() noexcept;
429 std::unique_ptr<uv_loop_t, deleter> uv_loop;
430 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.
metrics_type metrics() const noexcept
Tracks various internal operations of the event loop.
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.
details::uv_type_wrapper< uv_handle_type > handle_category
uv_metrics_t metrics_type
static handle_type guess_handle(handle_category category) noexcept
Gets the type of the handle given a category.