C++ wrapper for libuv, header only.
Copy include
into your project.
Of course, you also need to configure libuv
properly.
Enjoy using it.
#include <uvcxx.h>
int main() {
uv::timer_t().start(1000, 1000).call([](){
std::cout << "Hello~~~" << std::endl;
throw uvcxx::close_handle();
});
return uv::default_loop().run();
}
Note that all the handle
and request
data
members are used to store data related to C++ operations, so please do not use them.
If you need to use context, use the capture list of lambda
.
The handle
‘s data
will be recycled after close
, so be sure to call close
after the handle
is confirmed to not be used anymore.
The request
's data
will be recycled after the callback is completed, so most of the time there is no need to consider resource recycling issues.
libuvcxx
can be compatible with libuv: >= 1.0.0, <= 1.48.0
.
More version compatibility will be specified here after testing.
Notice: As the development proceeds, compatibility may change with new features.
This section mainly focuses on encapsulating options to facilitate the manipulation of starting processes.
Convert most operations to C++ interface style.
Q: Do we still need to C style thread
and mutex
since we have already used C++17?
A: For fun!
Note:
readdir
,scandir
require additional encapsulation to achieve C++style. So the interfaces are not currently supported and will be introduced in the future.
Most of the interfaces have been encapsulated or the reasons for not encapsulating have been explained.
The next work of libuvcxx
is mainly about the above content.
libuvcxx
uses exception to handle exceptions.
Most interfaces that don't often encounter errors will throw an uvcxx::errcode
exception upon failure.
This type of exception is thrown to facilitate the flexible use of interfaces in promise
or callback
contexts.
Certain interfaces that frequently encounter errors will not throw exceptions. In such cases, the success of the interface call should be judged by the return value, for example, uv::process_t::spawn.
If you're not sure whether an interface will throw an exception, please directly refer to the implementation code of the corresponding interface. After all, we are an open-source, header-only library.
You can use the macro definition UVCXX_NO_EXCEPTION
before #include <uvcxx.h>
to prevent interfaces from throwing exceptions.
However, in the current version, exceptions are still used to handle some transactions.
Future versions may consider finding suitable ways to completely eliminate the use of exceptions.
Most APIs have been changed from uv_xxx
to uv::xxx
,
and are placed in the necessary C++ classes according to their types.
Such as
uv_handle_t
->uv::handle_t
uv_fs_open
->uv::fs::open
uv_default_loop
->uv::default_loop
uv_close
->uv::handle_t::close
.
However, some APIs still cannot be fully migrated to C++ with consistent naming, so the following lists the interface names with obvious differences (this may not cover all cases).
uv_fs_get_type
->uv::fs_t::get_fs_type
uv_read_start
->uv::stream_t::alloc
+uv::stream_t::read_start