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...>
68 explicit uv_token(
int) {}
71 loop(std::unique_ptr<uv_loop_t, deleter> ptr)
noexcept;
74 using token = uv_token;
75 using time = std::chrono::duration<uint64_t, std::milli>;
76 using option = details::uvw_loop_option;
77 using run_mode = details::uvw_run_mode;
83 static std::shared_ptr<loop>
create();
95 static std::shared_ptr<loop>
create(uv_loop_t *res);
114 loop &operator=(
const loop &) =
delete;
115 loop &operator=(
loop &&other) =
delete;
138 template<typename... Args>
140 return uv_loop_configure(uv_loop.get(),
static_cast<uv_loop_option
>(flag), std::forward<Args>(args)...);
153 template<
typename R,
typename... Args>
155 auto ptr = uninitialized_resource<R>(std::forward<Args>(args)...);
156 ptr = (ptr->init() == 0) ? ptr :
nullptr;
164 template<
typename R,
typename... Args>
166 return std::make_shared<R>(token{0}, shared_from_this(), std::forward<Args>(args)...);
197 int run(run_mode mode = run_mode::DEFAULT)
noexcept;
232 std::pair<
bool, time>
timeout() const noexcept;
259 time
now() const noexcept;
279 template<typename Func>
281 auto func = [](uv_handle_t *hndl,
void *func) {
283 auto &cb = *
static_cast<Func *
>(func);
286 case handle_type::ASYNC:
289 case handle_type::CHECK:
292 case handle_type::FS_EVENT:
295 case handle_type::FS_POLL:
298 case handle_type::IDLE:
301 case handle_type::PIPE:
304 case handle_type::POLL:
307 case handle_type::PREPARE:
310 case handle_type::PROCESS:
313 case handle_type::SIGNAL:
316 case handle_type::TCP:
319 case handle_type::TIMER:
322 case handle_type::TTY:
325 case handle_type::UDP:
335 uv_walk(uv_loop.get(), func, &callback);
374 template<typename R =
void>
375 std::shared_ptr<R>
data()
const {
376 return std::static_pointer_cast<R>(user_data);
383 void data(std::shared_ptr<void> ud);
400 const uv_loop_t *
raw() const noexcept;
417 uv_loop_t *
raw() noexcept;
420 std::unique_ptr<uv_loop_t, deleter> uv_loop;
421 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.
Common class for almost all the resources available in uvw.
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.