From 21372c17e81c06dfd33fbdecbbf74a8c5adb9dc2 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Mon, 21 Oct 2024 10:57:36 +0200 Subject: [PATCH] build: split h/ipp/cpp and update clang-tidy config --- .clang-tidy | 3 + src/uvw/async.cpp | 24 +- src/uvw/async.h | 2 +- src/uvw/async.ipp | 18 ++ src/uvw/check.cpp | 28 +-- src/uvw/check.h | 2 +- src/uvw/check.ipp | 22 ++ src/uvw/dns.cpp | 95 +------- src/uvw/dns.h | 2 +- src/uvw/dns.ipp | 89 +++++++ src/uvw/emitter.cpp | 31 +-- src/uvw/emitter.h | 2 +- src/uvw/emitter.ipp | 25 ++ src/uvw/fs.cpp | 555 +------------------------------------------ src/uvw/fs.h | 2 +- src/uvw/fs.ipp | 549 ++++++++++++++++++++++++++++++++++++++++++ src/uvw/fs_event.cpp | 39 +-- src/uvw/fs_event.h | 2 +- src/uvw/fs_event.ipp | 33 +++ src/uvw/fs_poll.cpp | 39 +-- src/uvw/fs_poll.h | 2 +- src/uvw/fs_poll.ipp | 33 +++ src/uvw/idle.cpp | 28 +-- src/uvw/idle.h | 2 +- src/uvw/idle.ipp | 22 ++ src/uvw/lib.cpp | 29 +-- src/uvw/lib.h | 6 +- src/uvw/lib.ipp | 23 ++ src/uvw/loop.cpp | 122 +--------- src/uvw/loop.h | 2 +- src/uvw/loop.ipp | 116 +++++++++ src/uvw/pipe.cpp | 65 +---- src/uvw/pipe.h | 2 +- src/uvw/pipe.ipp | 59 +++++ src/uvw/poll.cpp | 45 +--- src/uvw/poll.h | 2 +- src/uvw/poll.ipp | 39 +++ src/uvw/prepare.cpp | 28 +-- src/uvw/prepare.h | 2 +- src/uvw/prepare.ipp | 22 ++ src/uvw/process.cpp | 113 +-------- src/uvw/process.h | 2 +- src/uvw/process.ipp | 107 +++++++++ src/uvw/signal.cpp | 39 +-- src/uvw/signal.h | 2 +- src/uvw/signal.ipp | 33 +++ src/uvw/stream.cpp | 35 +-- src/uvw/stream.h | 2 +- src/uvw/stream.ipp | 29 +++ src/uvw/tcp.cpp | 88 +------ src/uvw/tcp.h | 2 +- src/uvw/tcp.ipp | 82 +++++++ src/uvw/thread.cpp | 191 +-------------- src/uvw/thread.h | 2 +- src/uvw/thread.ipp | 185 +++++++++++++++ src/uvw/timer.cpp | 44 +--- src/uvw/timer.h | 4 +- src/uvw/timer.ipp | 38 +++ src/uvw/tty.cpp | 69 +----- src/uvw/tty.h | 2 +- src/uvw/tty.ipp | 65 +++++ src/uvw/udp.cpp | 218 +---------------- src/uvw/udp.h | 2 +- src/uvw/udp.ipp | 212 +++++++++++++++++ src/uvw/util.cpp | 376 +---------------------------- src/uvw/util.h | 2 +- src/uvw/util.ipp | 370 +++++++++++++++++++++++++++++ src/uvw/work.cpp | 32 +-- src/uvw/work.h | 2 +- src/uvw/work.ipp | 25 ++ 70 files changed, 2272 insertions(+), 2312 deletions(-) create mode 100644 src/uvw/async.ipp create mode 100644 src/uvw/check.ipp create mode 100644 src/uvw/dns.ipp create mode 100644 src/uvw/emitter.ipp create mode 100644 src/uvw/fs.ipp create mode 100644 src/uvw/fs_event.ipp create mode 100644 src/uvw/fs_poll.ipp create mode 100644 src/uvw/idle.ipp create mode 100644 src/uvw/lib.ipp create mode 100644 src/uvw/loop.ipp create mode 100644 src/uvw/pipe.ipp create mode 100644 src/uvw/poll.ipp create mode 100644 src/uvw/prepare.ipp create mode 100644 src/uvw/process.ipp create mode 100644 src/uvw/signal.ipp create mode 100644 src/uvw/stream.ipp create mode 100644 src/uvw/tcp.ipp create mode 100644 src/uvw/thread.ipp create mode 100644 src/uvw/timer.ipp create mode 100644 src/uvw/tty.ipp create mode 100644 src/uvw/udp.ipp create mode 100644 src/uvw/util.ipp create mode 100644 src/uvw/work.ipp diff --git a/.clang-tidy b/.clang-tidy index ddcad619..8432d978 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,2 +1,5 @@ Checks: > bugprone-*, +CheckOptions: + - key: bugprone-suspicious-include.HeaderFileExtensions + value: ";h;hpp;ipp" diff --git a/src/uvw/async.cpp b/src/uvw/async.cpp index fabbe453..e708f0f9 100644 --- a/src/uvw/async.cpp +++ b/src/uvw/async.cpp @@ -1,22 +1,2 @@ -#ifdef UVW_AS_LIB -# include "async.h" -#endif - -#include "config.h" - -namespace uvw { - -UVW_INLINE void async_handle::send_callback(uv_async_t *hndl) { - async_handle &async = *(static_cast(hndl->data)); - async.publish(async_event{}); -} - -UVW_INLINE int async_handle::init() { - return leak_if(uv_async_init(parent().raw(), raw(), &send_callback)); -} - -UVW_INLINE int async_handle::send() { - return uv_async_send(raw()); -} - -} // namespace uvw +#include "async.h" +#include "async.ipp" diff --git a/src/uvw/async.h b/src/uvw/async.h index 4a49585a..bb6fe67e 100644 --- a/src/uvw/async.h +++ b/src/uvw/async.h @@ -52,7 +52,7 @@ public: } // namespace uvw #ifndef UVW_AS_LIB -# include "async.cpp" +# include "async.ipp" #endif #endif // UVW_ASYNC_INCLUDE_H diff --git a/src/uvw/async.ipp b/src/uvw/async.ipp new file mode 100644 index 00000000..b4f2fe56 --- /dev/null +++ b/src/uvw/async.ipp @@ -0,0 +1,18 @@ +#include "config.h" + +namespace uvw { + +UVW_INLINE void async_handle::send_callback(uv_async_t *hndl) { + async_handle &async = *(static_cast(hndl->data)); + async.publish(async_event{}); +} + +UVW_INLINE int async_handle::init() { + return leak_if(uv_async_init(parent().raw(), raw(), &send_callback)); +} + +UVW_INLINE int async_handle::send() { + return uv_async_send(raw()); +} + +} // namespace uvw diff --git a/src/uvw/check.cpp b/src/uvw/check.cpp index 3608c0d2..66829549 100644 --- a/src/uvw/check.cpp +++ b/src/uvw/check.cpp @@ -1,26 +1,2 @@ -#ifdef UVW_AS_LIB -# include "check.h" -#endif - -#include "config.h" - -namespace uvw { - -UVW_INLINE void check_handle::start_callback(uv_check_t *hndl) { - check_handle &check = *(static_cast(hndl->data)); - check.publish(check_event{}); -} - -UVW_INLINE int check_handle::init() { - return leak_if(uv_check_init(parent().raw(), raw())); -} - -UVW_INLINE int check_handle::start() { - return uv_check_start(raw(), &start_callback); -} - -UVW_INLINE int check_handle::stop() { - return uv_check_stop(raw()); -} - -} // namespace uvw +#include "check.h" +#include "check.ipp" diff --git a/src/uvw/check.h b/src/uvw/check.h index b1bbaff1..57b15c48 100644 --- a/src/uvw/check.h +++ b/src/uvw/check.h @@ -50,7 +50,7 @@ public: } // namespace uvw #ifndef UVW_AS_LIB -# include "check.cpp" +# include "check.ipp" #endif #endif // UVW_CHECK_INCLUDE_H diff --git a/src/uvw/check.ipp b/src/uvw/check.ipp new file mode 100644 index 00000000..a390e517 --- /dev/null +++ b/src/uvw/check.ipp @@ -0,0 +1,22 @@ +#include "config.h" + +namespace uvw { + +UVW_INLINE void check_handle::start_callback(uv_check_t *hndl) { + check_handle &check = *(static_cast(hndl->data)); + check.publish(check_event{}); +} + +UVW_INLINE int check_handle::init() { + return leak_if(uv_check_init(parent().raw(), raw())); +} + +UVW_INLINE int check_handle::start() { + return uv_check_start(raw(), &start_callback); +} + +UVW_INLINE int check_handle::stop() { + return uv_check_stop(raw()); +} + +} // namespace uvw diff --git a/src/uvw/dns.cpp b/src/uvw/dns.cpp index 4c706292..1c00901c 100644 --- a/src/uvw/dns.cpp +++ b/src/uvw/dns.cpp @@ -1,93 +1,2 @@ -#ifdef UVW_AS_LIB -# include "dns.h" -#endif - -#include "config.h" - -namespace uvw { - -UVW_INLINE addr_info_event::addr_info_event(std::unique_ptr addr) - : data{std::move(addr)} {} - -UVW_INLINE name_info_event::name_info_event(const char *host, const char *serv) - : hostname{host}, service{serv} {} - -UVW_INLINE void get_addr_info_req::addr_info_callback(uv_getaddrinfo_t *req, int status, addrinfo *res) { - if(auto ptr = reserve(req); status) { - ptr->publish(error_event{status}); - } else { - auto data = std::unique_ptr{res, [](addrinfo *addr) { uv_freeaddrinfo(addr); }}; - ptr->publish(addr_info_event{std::move(data)}); - } -} - -UVW_INLINE int get_addr_info_req::node_addr_info(const char *node, const char *service, addrinfo *hints) { - return this->leak_if(uv_getaddrinfo(parent().raw(), raw(), &addr_info_callback, node, service, hints)); -} - -UVW_INLINE auto get_addr_info_req::node_addr_info_sync(const char *node, const char *service, addrinfo *hints) { - auto req = raw(); - auto err = uv_getaddrinfo(parent().raw(), req, nullptr, node, service, hints); - auto data = std::unique_ptr{req->addrinfo, [](addrinfo *addr) { uv_freeaddrinfo(addr); }}; - return std::make_pair(!err, std::move(data)); -} - -UVW_INLINE int get_addr_info_req::node_addr_info(const std::string &node, addrinfo *hints) { - return node_addr_info(node.data(), nullptr, hints); -} - -UVW_INLINE std::pair> get_addr_info_req::node_addr_info_sync(const std::string &node, addrinfo *hints) { - return node_addr_info_sync(node.data(), nullptr, hints); -} - -UVW_INLINE int get_addr_info_req::service_addr_info(const std::string &service, addrinfo *hints) { - return node_addr_info(nullptr, service.data(), hints); -} - -UVW_INLINE std::pair> get_addr_info_req::service_addr_info_sync(const std::string &service, addrinfo *hints) { - return node_addr_info_sync(nullptr, service.data(), hints); -} - -UVW_INLINE int get_addr_info_req::addr_info(const std::string &node, const std::string &service, addrinfo *hints) { - return node_addr_info(node.data(), service.data(), hints); -} - -UVW_INLINE std::pair> get_addr_info_req::addr_info_sync(const std::string &node, const std::string &service, addrinfo *hints) { - return node_addr_info_sync(node.data(), service.data(), hints); -} - -UVW_INLINE void get_name_info_req::name_info_callback(uv_getnameinfo_t *req, int status, const char *hostname, const char *service) { - if(auto ptr = reserve(req); status) { - ptr->publish(error_event{status}); - } else { - ptr->publish(name_info_event{hostname, service}); - } -} - -UVW_INLINE int get_name_info_req::name_info(const sockaddr &addr, int flags) { - return this->leak_if(uv_getnameinfo(parent().raw(), raw(), &name_info_callback, &addr, flags)); -} - -UVW_INLINE int get_name_info_req::name_info(const std::string &ip, unsigned int port, int flags) { - return name_info(details::ip_addr(ip.data(), port), flags); -} - -UVW_INLINE int get_name_info_req::name_info(socket_address addr, int flags) { - return name_info(std::move(addr.ip), addr.port, flags); -} - -UVW_INLINE std::pair> get_name_info_req::name_info_sync(const sockaddr &addr, int flags) { - auto req = raw(); - auto err = uv_getnameinfo(parent().raw(), req, nullptr, &addr, flags); - return std::make_pair(!err, std::make_pair(req->host, req->service)); -} - -UVW_INLINE std::pair> get_name_info_req::name_info_sync(const std::string &ip, unsigned int port, int flags) { - return name_info_sync(details::ip_addr(ip.data(), port), flags); -} - -UVW_INLINE std::pair> get_name_info_req::name_info_sync(socket_address addr, int flags) { - return name_info_sync(addr.ip, addr.port, flags); -} - -} // namespace uvw +#include "dns.h" +#include "dns.ipp" diff --git a/src/uvw/dns.h b/src/uvw/dns.h index 8bfb4a9d..f82af0c0 100644 --- a/src/uvw/dns.h +++ b/src/uvw/dns.h @@ -220,7 +220,7 @@ public: } // namespace uvw #ifndef UVW_AS_LIB -# include "dns.cpp" +# include "dns.ipp" #endif #endif // UVW_DNS_INCLUDE_H diff --git a/src/uvw/dns.ipp b/src/uvw/dns.ipp new file mode 100644 index 00000000..1397e150 --- /dev/null +++ b/src/uvw/dns.ipp @@ -0,0 +1,89 @@ +#include "config.h" + +namespace uvw { + +UVW_INLINE addr_info_event::addr_info_event(std::unique_ptr addr) + : data{std::move(addr)} {} + +UVW_INLINE name_info_event::name_info_event(const char *host, const char *serv) + : hostname{host}, service{serv} {} + +UVW_INLINE void get_addr_info_req::addr_info_callback(uv_getaddrinfo_t *req, int status, addrinfo *res) { + if(auto ptr = reserve(req); status) { + ptr->publish(error_event{status}); + } else { + auto data = std::unique_ptr{res, [](addrinfo *addr) { uv_freeaddrinfo(addr); }}; + ptr->publish(addr_info_event{std::move(data)}); + } +} + +UVW_INLINE int get_addr_info_req::node_addr_info(const char *node, const char *service, addrinfo *hints) { + return this->leak_if(uv_getaddrinfo(parent().raw(), raw(), &addr_info_callback, node, service, hints)); +} + +UVW_INLINE auto get_addr_info_req::node_addr_info_sync(const char *node, const char *service, addrinfo *hints) { + auto req = raw(); + auto err = uv_getaddrinfo(parent().raw(), req, nullptr, node, service, hints); + auto data = std::unique_ptr{req->addrinfo, [](addrinfo *addr) { uv_freeaddrinfo(addr); }}; + return std::make_pair(!err, std::move(data)); +} + +UVW_INLINE int get_addr_info_req::node_addr_info(const std::string &node, addrinfo *hints) { + return node_addr_info(node.data(), nullptr, hints); +} + +UVW_INLINE std::pair> get_addr_info_req::node_addr_info_sync(const std::string &node, addrinfo *hints) { + return node_addr_info_sync(node.data(), nullptr, hints); +} + +UVW_INLINE int get_addr_info_req::service_addr_info(const std::string &service, addrinfo *hints) { + return node_addr_info(nullptr, service.data(), hints); +} + +UVW_INLINE std::pair> get_addr_info_req::service_addr_info_sync(const std::string &service, addrinfo *hints) { + return node_addr_info_sync(nullptr, service.data(), hints); +} + +UVW_INLINE int get_addr_info_req::addr_info(const std::string &node, const std::string &service, addrinfo *hints) { + return node_addr_info(node.data(), service.data(), hints); +} + +UVW_INLINE std::pair> get_addr_info_req::addr_info_sync(const std::string &node, const std::string &service, addrinfo *hints) { + return node_addr_info_sync(node.data(), service.data(), hints); +} + +UVW_INLINE void get_name_info_req::name_info_callback(uv_getnameinfo_t *req, int status, const char *hostname, const char *service) { + if(auto ptr = reserve(req); status) { + ptr->publish(error_event{status}); + } else { + ptr->publish(name_info_event{hostname, service}); + } +} + +UVW_INLINE int get_name_info_req::name_info(const sockaddr &addr, int flags) { + return this->leak_if(uv_getnameinfo(parent().raw(), raw(), &name_info_callback, &addr, flags)); +} + +UVW_INLINE int get_name_info_req::name_info(const std::string &ip, unsigned int port, int flags) { + return name_info(details::ip_addr(ip.data(), port), flags); +} + +UVW_INLINE int get_name_info_req::name_info(socket_address addr, int flags) { + return name_info(std::move(addr.ip), addr.port, flags); +} + +UVW_INLINE std::pair> get_name_info_req::name_info_sync(const sockaddr &addr, int flags) { + auto req = raw(); + auto err = uv_getnameinfo(parent().raw(), req, nullptr, &addr, flags); + return std::make_pair(!err, std::make_pair(req->host, req->service)); +} + +UVW_INLINE std::pair> get_name_info_req::name_info_sync(const std::string &ip, unsigned int port, int flags) { + return name_info_sync(details::ip_addr(ip.data(), port), flags); +} + +UVW_INLINE std::pair> get_name_info_req::name_info_sync(socket_address addr, int flags) { + return name_info_sync(addr.ip, addr.port, flags); +} + +} // namespace uvw diff --git a/src/uvw/emitter.cpp b/src/uvw/emitter.cpp index b0cde511..d7498977 100644 --- a/src/uvw/emitter.cpp +++ b/src/uvw/emitter.cpp @@ -1,29 +1,2 @@ -#ifdef UVW_AS_LIB -# include "emitter.h" -#endif - -#include "config.h" - -namespace uvw { - -UVW_INLINE int error_event::translate(int sys) noexcept { - return uv_translate_sys_error(sys); -} - -UVW_INLINE const char *error_event::what() const noexcept { - return uv_strerror(ec); -} - -UVW_INLINE const char *error_event::name() const noexcept { - return uv_err_name(ec); -} - -UVW_INLINE int error_event::code() const noexcept { - return ec; -} - -UVW_INLINE error_event::operator bool() const noexcept { - return ec < 0; -} - -} // namespace uvw +#include "emitter.h" +#include "emitter.ipp" diff --git a/src/uvw/emitter.h b/src/uvw/emitter.h index a2f17d3e..7cf95421 100644 --- a/src/uvw/emitter.h +++ b/src/uvw/emitter.h @@ -153,7 +153,7 @@ private: } // namespace uvw #ifndef UVW_AS_LIB -# include "emitter.cpp" +# include "emitter.ipp" #endif #endif // UVW_EMITTER_INCLUDE_H diff --git a/src/uvw/emitter.ipp b/src/uvw/emitter.ipp new file mode 100644 index 00000000..f6ee54fa --- /dev/null +++ b/src/uvw/emitter.ipp @@ -0,0 +1,25 @@ +#include "config.h" + +namespace uvw { + +UVW_INLINE int error_event::translate(int sys) noexcept { + return uv_translate_sys_error(sys); +} + +UVW_INLINE const char *error_event::what() const noexcept { + return uv_strerror(ec); +} + +UVW_INLINE const char *error_event::name() const noexcept { + return uv_err_name(ec); +} + +UVW_INLINE int error_event::code() const noexcept { + return ec; +} + +UVW_INLINE error_event::operator bool() const noexcept { + return ec < 0; +} + +} // namespace uvw diff --git a/src/uvw/fs.cpp b/src/uvw/fs.cpp index 5921d818..2a69f086 100644 --- a/src/uvw/fs.cpp +++ b/src/uvw/fs.cpp @@ -1,553 +1,2 @@ -#ifdef UVW_AS_LIB -# include "fs.h" -#endif - -#include "config.h" - -namespace uvw { - -UVW_INLINE void file_req::fs_open_callback(uv_fs_t *req) { - if(auto ptr = reserve(req); req->result < 0) { - ptr->publish(error_event{req->result}); - } else { - ptr->file = static_cast(req->result); - ptr->publish(fs_event{*req}); - } -} - -UVW_INLINE void file_req::fs_close_callback(uv_fs_t *req) { - if(auto ptr = reserve(req); req->result < 0) { - ptr->publish(error_event{req->result}); - } else { - ptr->file = BAD_FD; - ptr->publish(fs_event{*req}); - } -} - -UVW_INLINE void file_req::fs_read_callback(uv_fs_t *req) { - if(auto ptr = reserve(req); req->result < 0) { - ptr->publish(error_event{req->result}); - } else { - ptr->publish(fs_event{*req, std::move(ptr->current)}); - } -} - -UVW_INLINE file_req::~file_req() noexcept { - uv_fs_req_cleanup(raw()); -} - -UVW_INLINE void file_req::close() { - uv_fs_req_cleanup(this->raw()); - uv_fs_close(parent().raw(), raw(), file, &fs_close_callback); -} - -UVW_INLINE bool file_req::close_sync() { - auto req = raw(); - - uv_fs_req_cleanup(this->raw()); - uv_fs_close(parent().raw(), req, file, nullptr); - - if(req->result >= 0) { - file = BAD_FD; - } - - return !(req->result < 0); -} - -UVW_INLINE void file_req::open(const std::string &path, file_open_flags flags, int mode) { - uv_fs_req_cleanup(this->raw()); - uv_fs_open(parent().raw(), raw(), path.data(), static_cast(flags), mode, &fs_open_callback); -} - -UVW_INLINE bool file_req::open_sync(const std::string &path, file_open_flags flags, int mode) { - auto req = raw(); - - uv_fs_req_cleanup(this->raw()); - uv_fs_open(parent().raw(), req, path.data(), static_cast(flags), mode, nullptr); - - if(req->result >= 0) { - file = static_cast(req->result); - } - - return !(req->result < 0); -} - -UVW_INLINE void file_req::read(int64_t offset, unsigned int len) { - current = std::unique_ptr{new char[len]}; - buffer = uv_buf_init(current.get(), len); - uv_buf_t bufs[] = {buffer}; - uv_fs_req_cleanup(this->raw()); - uv_fs_read(parent().raw(), raw(), file, bufs, 1, offset, &fs_read_callback); -} - -UVW_INLINE std::pair, std::size_t>> file_req::read_sync(int64_t offset, unsigned int len) { - current = std::unique_ptr{new char[len]}; - buffer = uv_buf_init(current.get(), len); - uv_buf_t bufs[] = {buffer}; - auto req = raw(); - uv_fs_req_cleanup(this->raw()); - uv_fs_read(parent().raw(), req, file, bufs, 1, offset, nullptr); - bool err = req->result < 0; - return std::make_pair(!err, std::make_pair(std::move(current), err ? 0 : std::size_t(req->result))); -} - -UVW_INLINE void file_req::write(std::unique_ptr buf, unsigned int len, int64_t offset) { - current = std::move(buf); - uv_buf_t bufs[] = {uv_buf_init(current.get(), len)}; - uv_fs_req_cleanup(this->raw()); - uv_fs_write(parent().raw(), raw(), file, bufs, 1, offset, &fs_request_callback); -} - -UVW_INLINE void file_req::write(char *buf, unsigned int len, int64_t offset) { - uv_buf_t bufs[] = {uv_buf_init(buf, len)}; - uv_fs_req_cleanup(this->raw()); - uv_fs_write(parent().raw(), raw(), file, bufs, 1, offset, &fs_request_callback); -} - -UVW_INLINE std::pair file_req::write_sync(std::unique_ptr buf, unsigned int len, int64_t offset) { - current = std::move(buf); - uv_buf_t bufs[] = {uv_buf_init(current.get(), len)}; - auto req = raw(); - uv_fs_req_cleanup(this->raw()); - uv_fs_write(parent().raw(), req, file, bufs, 1, offset, nullptr); - bool err = req->result < 0; - return std::make_pair(!err, err ? 0 : std::size_t(req->result)); -} - -UVW_INLINE void file_req::stat() { - uv_fs_req_cleanup(this->raw()); - uv_fs_fstat(parent().raw(), raw(), file, &fs_request_callback); -} - -UVW_INLINE std::pair file_req::stat_sync() { - auto req = raw(); - uv_fs_req_cleanup(this->raw()); - uv_fs_fstat(parent().raw(), req, file, nullptr); - return std::make_pair(!(req->result < 0), req->statbuf); -} - -UVW_INLINE void file_req::sync() { - uv_fs_req_cleanup(this->raw()); - uv_fs_fsync(parent().raw(), raw(), file, &fs_request_callback); -} - -UVW_INLINE bool file_req::sync_sync() { - auto req = raw(); - uv_fs_req_cleanup(this->raw()); - uv_fs_fsync(parent().raw(), req, file, nullptr); - return !(req->result < 0); -} - -UVW_INLINE void file_req::datasync() { - uv_fs_req_cleanup(this->raw()); - uv_fs_fdatasync(parent().raw(), raw(), file, &fs_request_callback); -} - -UVW_INLINE bool file_req::datasync_sync() { - auto req = raw(); - uv_fs_req_cleanup(this->raw()); - uv_fs_fdatasync(parent().raw(), req, file, nullptr); - return !(req->result < 0); -} - -UVW_INLINE void file_req::truncate(int64_t offset) { - uv_fs_req_cleanup(this->raw()); - uv_fs_ftruncate(parent().raw(), raw(), file, offset, &fs_request_callback); -} - -UVW_INLINE bool file_req::truncate_sync(int64_t offset) { - auto req = raw(); - uv_fs_req_cleanup(this->raw()); - uv_fs_ftruncate(parent().raw(), req, file, offset, nullptr); - return !(req->result < 0); -} - -UVW_INLINE void file_req::sendfile(file_handle out, int64_t offset, std::size_t length) { - uv_fs_req_cleanup(this->raw()); - uv_fs_sendfile(parent().raw(), raw(), out, file, offset, length, &fs_request_callback); -} - -UVW_INLINE std::pair file_req::sendfile_sync(file_handle out, int64_t offset, std::size_t length) { - auto req = raw(); - uv_fs_req_cleanup(this->raw()); - uv_fs_sendfile(parent().raw(), req, out, file, offset, length, nullptr); - bool err = req->result < 0; - return std::make_pair(!err, err ? 0 : std::size_t(req->result)); -} - -UVW_INLINE void file_req::chmod(int mode) { - uv_fs_req_cleanup(this->raw()); - uv_fs_fchmod(parent().raw(), raw(), file, mode, &fs_request_callback); -} - -UVW_INLINE bool file_req::chmod_sync(int mode) { - auto req = raw(); - uv_fs_req_cleanup(this->raw()); - uv_fs_fchmod(parent().raw(), req, file, mode, nullptr); - return !(req->result < 0); -} - -UVW_INLINE void file_req::futime(fs_request::time atime, fs_request::time mtime) { - uv_fs_req_cleanup(this->raw()); - uv_fs_futime(parent().raw(), raw(), file, atime.count(), mtime.count(), &fs_request_callback); -} - -UVW_INLINE bool file_req::futime_sync(fs_request::time atime, fs_request::time mtime) { - auto req = raw(); - uv_fs_req_cleanup(this->raw()); - uv_fs_futime(parent().raw(), req, file, atime.count(), mtime.count(), nullptr); - return !(req->result < 0); -} - -UVW_INLINE void file_req::chown(uid_type uid, gid_type gid) { - uv_fs_req_cleanup(this->raw()); - uv_fs_fchown(parent().raw(), raw(), file, uid, gid, &fs_request_callback); -} - -UVW_INLINE bool file_req::chown_sync(uid_type uid, gid_type gid) { - auto req = raw(); - uv_fs_req_cleanup(this->raw()); - uv_fs_fchown(parent().raw(), req, file, uid, gid, nullptr); - return !(req->result < 0); -} - -UVW_INLINE file_req::operator file_handle() const noexcept { - return file; -} - -UVW_INLINE fs_req::~fs_req() noexcept { - uv_fs_req_cleanup(raw()); -} - -UVW_INLINE void fs_req::unlink(const std::string &path) { - uv_fs_req_cleanup(this->raw()); - uv_fs_unlink(parent().raw(), raw(), path.data(), &fs_request_callback); -} - -UVW_INLINE bool fs_req::unlink_sync(const std::string &path) { - auto req = raw(); - uv_fs_req_cleanup(this->raw()); - uv_fs_unlink(parent().raw(), req, path.data(), nullptr); - return !(req->result < 0); -} - -UVW_INLINE void fs_req::mkdir(const std::string &path, int mode) { - uv_fs_req_cleanup(this->raw()); - uv_fs_mkdir(parent().raw(), raw(), path.data(), mode, &fs_request_callback); -} - -UVW_INLINE bool fs_req::mkdir_sync(const std::string &path, int mode) { - auto req = raw(); - uv_fs_req_cleanup(this->raw()); - uv_fs_mkdir(parent().raw(), req, path.data(), mode, nullptr); - return !(req->result < 0); -} - -UVW_INLINE void fs_req::mkdtemp(const std::string &tpl) { - uv_fs_req_cleanup(this->raw()); - uv_fs_mkdtemp(parent().raw(), raw(), tpl.data(), &fs_request_callback); -} - -UVW_INLINE std::pair fs_req::mkdtemp_sync(const std::string &tpl) { - auto req = raw(); - uv_fs_req_cleanup(this->raw()); - uv_fs_mkdtemp(parent().raw(), req, tpl.data(), nullptr); - return std::make_pair(!(req->result < 0), req->path); -} - -UVW_INLINE void fs_req::mkstemp(const std::string &tpl) { - uv_fs_req_cleanup(this->raw()); - uv_fs_mkstemp(parent().raw(), raw(), tpl.data(), &fs_request_callback); -} - -UVW_INLINE std::pair> fs_req::mkstemp_sync(const std::string &tpl) { - std::pair> ret{false, {}}; - auto req = raw(); - uv_fs_req_cleanup(this->raw()); - uv_fs_mkstemp(parent().raw(), req, tpl.data(), nullptr); - ret.first = !(req->result < 0); - - if(ret.first) { - ret.second.first = req->path; - ret.second.second = static_cast(req->result); - } - - return ret; -} - -UVW_INLINE void fs_req::lutime(const std::string &path, time atime, time mtime) { - uv_fs_req_cleanup(this->raw()); - uv_fs_lutime(parent().raw(), raw(), path.data(), atime.count(), mtime.count(), &fs_request_callback); -} - -UVW_INLINE bool fs_req::lutime_sync(const std::string &path, time atime, time mtime) { - auto req = raw(); - uv_fs_req_cleanup(this->raw()); - uv_fs_lutime(parent().raw(), req, path.data(), atime.count(), mtime.count(), nullptr); - return !(req->result < 0); -} - -UVW_INLINE void fs_req::rmdir(const std::string &path) { - uv_fs_req_cleanup(this->raw()); - uv_fs_rmdir(parent().raw(), raw(), path.data(), &fs_request_callback); -} - -UVW_INLINE bool fs_req::rmdir_sync(const std::string &path) { - auto req = raw(); - uv_fs_req_cleanup(this->raw()); - uv_fs_rmdir(parent().raw(), req, path.data(), nullptr); - return !(req->result < 0); -} - -UVW_INLINE void fs_req::scandir(const std::string &path, int flags) { - uv_fs_req_cleanup(this->raw()); - uv_fs_scandir(parent().raw(), raw(), path.data(), flags, &fs_request_callback); -} - -UVW_INLINE std::pair fs_req::scandir_sync(const std::string &path, int flags) { - auto req = raw(); - uv_fs_req_cleanup(this->raw()); - uv_fs_scandir(parent().raw(), req, path.data(), flags, nullptr); - bool err = req->result < 0; - return std::make_pair(!err, err ? 0 : std::size_t(req->result)); -} - -UVW_INLINE std::pair> fs_req::scandir_next() { - std::pair> ret{false, {entry_type::UNKNOWN, nullptr}}; - - uv_fs_req_cleanup(raw()); - auto res = uv_fs_scandir_next(raw(), dirents); - - if(UV_EOF != res) { - ret.second.first = static_cast(dirents[0].type); - ret.second.second = dirents[0].name; - ret.first = true; - } - - return ret; -} - -UVW_INLINE void fs_req::stat(const std::string &path) { - uv_fs_req_cleanup(this->raw()); - uv_fs_stat(parent().raw(), raw(), path.data(), &fs_request_callback); -} - -UVW_INLINE std::pair fs_req::stat_sync(const std::string &path) { - auto req = raw(); - uv_fs_req_cleanup(this->raw()); - uv_fs_stat(parent().raw(), req, path.data(), nullptr); - return std::make_pair(!(req->result < 0), req->statbuf); -} - -UVW_INLINE void fs_req::lstat(const std::string &path) { - uv_fs_req_cleanup(this->raw()); - uv_fs_lstat(parent().raw(), raw(), path.data(), &fs_request_callback); -} - -UVW_INLINE std::pair fs_req::lstat_sync(const std::string &path) { - auto req = raw(); - uv_fs_req_cleanup(this->raw()); - uv_fs_lstat(parent().raw(), req, path.data(), nullptr); - return std::make_pair(!(req->result < 0), req->statbuf); -} - -UVW_INLINE void fs_req::statfs(const std::string &path) { - uv_fs_req_cleanup(this->raw()); - uv_fs_statfs(parent().raw(), raw(), path.data(), &fs_request_callback); -} - -UVW_INLINE std::pair fs_req::statfs_sync(const std::string &path) { - auto req = raw(); - uv_fs_req_cleanup(this->raw()); - uv_fs_statfs(parent().raw(), req, path.data(), nullptr); - return std::make_pair(!(req->result < 0), *static_cast(req->ptr)); -} - -UVW_INLINE void fs_req::rename(const std::string &old, const std::string &path) { - uv_fs_req_cleanup(this->raw()); - uv_fs_rename(parent().raw(), raw(), old.data(), path.data(), &fs_request_callback); -} - -UVW_INLINE bool fs_req::rename_sync(const std::string &old, const std::string &path) { - auto req = raw(); - uv_fs_req_cleanup(this->raw()); - uv_fs_rename(parent().raw(), req, old.data(), path.data(), nullptr); - return !(req->result < 0); -} - -UVW_INLINE void fs_req::copyfile(const std::string &old, const std::string &path, copy_file_flags flags) { - uv_fs_req_cleanup(this->raw()); - uv_fs_copyfile(parent().raw(), raw(), old.data(), path.data(), static_cast(flags), &fs_request_callback); -} - -UVW_INLINE bool fs_req::copyfile_sync(const std::string &old, const std::string &path, copy_file_flags flags) { - auto req = raw(); - uv_fs_req_cleanup(this->raw()); - uv_fs_copyfile(parent().raw(), raw(), old.data(), path.data(), static_cast(flags), nullptr); - return !(req->result < 0); -} - -UVW_INLINE void fs_req::access(const std::string &path, int mode) { - uv_fs_req_cleanup(this->raw()); - uv_fs_access(parent().raw(), raw(), path.data(), mode, &fs_request_callback); -} - -UVW_INLINE bool fs_req::access_sync(const std::string &path, int mode) { - auto req = raw(); - uv_fs_req_cleanup(this->raw()); - uv_fs_access(parent().raw(), req, path.data(), mode, nullptr); - return !(req->result < 0); -} - -UVW_INLINE void fs_req::chmod(const std::string &path, int mode) { - uv_fs_req_cleanup(this->raw()); - uv_fs_chmod(parent().raw(), raw(), path.data(), mode, &fs_request_callback); -} - -UVW_INLINE bool fs_req::chmod_sync(const std::string &path, int mode) { - auto req = raw(); - uv_fs_req_cleanup(this->raw()); - uv_fs_chmod(parent().raw(), req, path.data(), mode, nullptr); - return !(req->result < 0); -} - -UVW_INLINE void fs_req::utime(const std::string &path, fs_request::time atime, fs_request::time mtime) { - uv_fs_req_cleanup(this->raw()); - uv_fs_utime(parent().raw(), raw(), path.data(), atime.count(), mtime.count(), &fs_request_callback); -} - -UVW_INLINE bool fs_req::utime_sync(const std::string &path, fs_request::time atime, fs_request::time mtime) { - auto req = raw(); - uv_fs_req_cleanup(this->raw()); - uv_fs_utime(parent().raw(), req, path.data(), atime.count(), mtime.count(), nullptr); - return !(req->result < 0); -} - -UVW_INLINE void fs_req::link(const std::string &old, const std::string &path) { - uv_fs_req_cleanup(this->raw()); - uv_fs_link(parent().raw(), raw(), old.data(), path.data(), &fs_request_callback); -} - -UVW_INLINE bool fs_req::link_sync(const std::string &old, const std::string &path) { - auto req = raw(); - uv_fs_req_cleanup(this->raw()); - uv_fs_link(parent().raw(), req, old.data(), path.data(), nullptr); - return !(req->result < 0); -} - -UVW_INLINE void fs_req::symlink(const std::string &old, const std::string &path, symlink_flags flags) { - uv_fs_req_cleanup(this->raw()); - uv_fs_symlink(parent().raw(), raw(), old.data(), path.data(), static_cast(flags), &fs_request_callback); -} - -UVW_INLINE bool fs_req::symlink_sync(const std::string &old, const std::string &path, symlink_flags flags) { - auto req = raw(); - uv_fs_req_cleanup(this->raw()); - uv_fs_symlink(parent().raw(), req, old.data(), path.data(), static_cast(flags), nullptr); - return !(req->result < 0); -} - -UVW_INLINE void fs_req::readlink(const std::string &path) { - uv_fs_req_cleanup(this->raw()); - uv_fs_readlink(parent().raw(), raw(), path.data(), &fs_request_callback); -} - -UVW_INLINE std::pair> fs_req::readlink_sync(const std::string &path) { - auto req = raw(); - uv_fs_req_cleanup(this->raw()); - uv_fs_readlink(parent().raw(), req, path.data(), nullptr); - bool err = req->result < 0; - return std::make_pair(!err, std::make_pair(static_cast(req->ptr), err ? 0 : std::size_t(req->result))); -} - -UVW_INLINE void fs_req::realpath(const std::string &path) { - uv_fs_req_cleanup(this->raw()); - uv_fs_realpath(parent().raw(), raw(), path.data(), &fs_request_callback); -} - -UVW_INLINE std::pair fs_req::realpath_sync(const std::string &path) { - auto req = raw(); - uv_fs_req_cleanup(this->raw()); - uv_fs_realpath(parent().raw(), req, path.data(), nullptr); - return std::make_pair(!(req->result < 0), req->path); -} - -UVW_INLINE void fs_req::chown(const std::string &path, uid_type uid, gid_type gid) { - uv_fs_req_cleanup(this->raw()); - uv_fs_chown(parent().raw(), raw(), path.data(), uid, gid, &fs_request_callback); -} - -UVW_INLINE bool fs_req::chown_sync(const std::string &path, uid_type uid, gid_type gid) { - auto req = raw(); - uv_fs_req_cleanup(this->raw()); - uv_fs_chown(parent().raw(), req, path.data(), uid, gid, nullptr); - return !(req->result < 0); -} - -UVW_INLINE void fs_req::lchown(const std::string &path, uid_type uid, gid_type gid) { - uv_fs_req_cleanup(this->raw()); - uv_fs_lchown(parent().raw(), raw(), path.data(), uid, gid, &fs_request_callback); -} - -UVW_INLINE bool fs_req::lchown_sync(const std::string &path, uid_type uid, gid_type gid) { - auto req = raw(); - uv_fs_req_cleanup(this->raw()); - uv_fs_lchown(parent().raw(), req, path.data(), uid, gid, nullptr); - return !(req->result < 0); -} - -UVW_INLINE void fs_req::opendir(const std::string &path) { - uv_fs_req_cleanup(this->raw()); - uv_fs_opendir(parent().raw(), raw(), path.data(), &fs_request_callback); -} - -UVW_INLINE bool fs_req::opendir_sync(const std::string &path) { - auto req = raw(); - uv_fs_req_cleanup(this->raw()); - uv_fs_opendir(parent().raw(), req, path.data(), nullptr); - return !(req->result < 0); -} - -UVW_INLINE void fs_req::closedir() { - auto req = raw(); - auto *dir = static_cast(req->ptr); - uv_fs_req_cleanup(this->raw()); - uv_fs_closedir(parent().raw(), req, dir, &fs_request_callback); -} - -UVW_INLINE bool fs_req::closedir_sync() { - auto req = raw(); - auto *dir = static_cast(req->ptr); - uv_fs_req_cleanup(this->raw()); - uv_fs_closedir(parent().raw(), req, dir, nullptr); - return !(req->result < 0); -} - -UVW_INLINE void fs_req::readdir() { - auto req = raw(); - auto *dir = static_cast(req->ptr); - dir->dirents = dirents; - dir->nentries = 1; - uv_fs_req_cleanup(this->raw()); - uv_fs_readdir(parent().raw(), req, dir, &fs_request_callback); -} - -UVW_INLINE std::pair> fs_req::readdir_sync() { - auto req = raw(); - auto *dir = static_cast(req->ptr); - dir->dirents = dirents; - dir->nentries = 1; - uv_fs_req_cleanup(this->raw()); - uv_fs_readdir(parent().raw(), req, dir, nullptr); - return {req->result != 0, {static_cast(dirents[0].type), dirents[0].name}}; -} - -UVW_INLINE os_file_descriptor fs_helper::handle(file_handle file) noexcept { - return uv_get_osfhandle(file); -} - -UVW_INLINE file_handle fs_helper::open(os_file_descriptor descriptor) noexcept { - return uv_open_osfhandle(descriptor); -} - -} // namespace uvw +#include "fs.h" +#include "fs.ipp" diff --git a/src/uvw/fs.h b/src/uvw/fs.h index 878ad643..5a429591 100644 --- a/src/uvw/fs.h +++ b/src/uvw/fs.h @@ -1229,7 +1229,7 @@ struct fs_helper { } // namespace uvw #ifndef UVW_AS_LIB -# include "fs.cpp" +# include "fs.ipp" #endif #endif // UVW_FS_INCLUDE_H diff --git a/src/uvw/fs.ipp b/src/uvw/fs.ipp new file mode 100644 index 00000000..7ba8e79e --- /dev/null +++ b/src/uvw/fs.ipp @@ -0,0 +1,549 @@ +#include "config.h" + +namespace uvw { + +UVW_INLINE void file_req::fs_open_callback(uv_fs_t *req) { + if(auto ptr = reserve(req); req->result < 0) { + ptr->publish(error_event{req->result}); + } else { + ptr->file = static_cast(req->result); + ptr->publish(fs_event{*req}); + } +} + +UVW_INLINE void file_req::fs_close_callback(uv_fs_t *req) { + if(auto ptr = reserve(req); req->result < 0) { + ptr->publish(error_event{req->result}); + } else { + ptr->file = BAD_FD; + ptr->publish(fs_event{*req}); + } +} + +UVW_INLINE void file_req::fs_read_callback(uv_fs_t *req) { + if(auto ptr = reserve(req); req->result < 0) { + ptr->publish(error_event{req->result}); + } else { + ptr->publish(fs_event{*req, std::move(ptr->current)}); + } +} + +UVW_INLINE file_req::~file_req() noexcept { + uv_fs_req_cleanup(raw()); +} + +UVW_INLINE void file_req::close() { + uv_fs_req_cleanup(this->raw()); + uv_fs_close(parent().raw(), raw(), file, &fs_close_callback); +} + +UVW_INLINE bool file_req::close_sync() { + auto req = raw(); + + uv_fs_req_cleanup(this->raw()); + uv_fs_close(parent().raw(), req, file, nullptr); + + if(req->result >= 0) { + file = BAD_FD; + } + + return !(req->result < 0); +} + +UVW_INLINE void file_req::open(const std::string &path, file_open_flags flags, int mode) { + uv_fs_req_cleanup(this->raw()); + uv_fs_open(parent().raw(), raw(), path.data(), static_cast(flags), mode, &fs_open_callback); +} + +UVW_INLINE bool file_req::open_sync(const std::string &path, file_open_flags flags, int mode) { + auto req = raw(); + + uv_fs_req_cleanup(this->raw()); + uv_fs_open(parent().raw(), req, path.data(), static_cast(flags), mode, nullptr); + + if(req->result >= 0) { + file = static_cast(req->result); + } + + return !(req->result < 0); +} + +UVW_INLINE void file_req::read(int64_t offset, unsigned int len) { + current = std::unique_ptr{new char[len]}; + buffer = uv_buf_init(current.get(), len); + uv_buf_t bufs[] = {buffer}; + uv_fs_req_cleanup(this->raw()); + uv_fs_read(parent().raw(), raw(), file, bufs, 1, offset, &fs_read_callback); +} + +UVW_INLINE std::pair, std::size_t>> file_req::read_sync(int64_t offset, unsigned int len) { + current = std::unique_ptr{new char[len]}; + buffer = uv_buf_init(current.get(), len); + uv_buf_t bufs[] = {buffer}; + auto req = raw(); + uv_fs_req_cleanup(this->raw()); + uv_fs_read(parent().raw(), req, file, bufs, 1, offset, nullptr); + bool err = req->result < 0; + return std::make_pair(!err, std::make_pair(std::move(current), err ? 0 : std::size_t(req->result))); +} + +UVW_INLINE void file_req::write(std::unique_ptr buf, unsigned int len, int64_t offset) { + current = std::move(buf); + uv_buf_t bufs[] = {uv_buf_init(current.get(), len)}; + uv_fs_req_cleanup(this->raw()); + uv_fs_write(parent().raw(), raw(), file, bufs, 1, offset, &fs_request_callback); +} + +UVW_INLINE void file_req::write(char *buf, unsigned int len, int64_t offset) { + uv_buf_t bufs[] = {uv_buf_init(buf, len)}; + uv_fs_req_cleanup(this->raw()); + uv_fs_write(parent().raw(), raw(), file, bufs, 1, offset, &fs_request_callback); +} + +UVW_INLINE std::pair file_req::write_sync(std::unique_ptr buf, unsigned int len, int64_t offset) { + current = std::move(buf); + uv_buf_t bufs[] = {uv_buf_init(current.get(), len)}; + auto req = raw(); + uv_fs_req_cleanup(this->raw()); + uv_fs_write(parent().raw(), req, file, bufs, 1, offset, nullptr); + bool err = req->result < 0; + return std::make_pair(!err, err ? 0 : std::size_t(req->result)); +} + +UVW_INLINE void file_req::stat() { + uv_fs_req_cleanup(this->raw()); + uv_fs_fstat(parent().raw(), raw(), file, &fs_request_callback); +} + +UVW_INLINE std::pair file_req::stat_sync() { + auto req = raw(); + uv_fs_req_cleanup(this->raw()); + uv_fs_fstat(parent().raw(), req, file, nullptr); + return std::make_pair(!(req->result < 0), req->statbuf); +} + +UVW_INLINE void file_req::sync() { + uv_fs_req_cleanup(this->raw()); + uv_fs_fsync(parent().raw(), raw(), file, &fs_request_callback); +} + +UVW_INLINE bool file_req::sync_sync() { + auto req = raw(); + uv_fs_req_cleanup(this->raw()); + uv_fs_fsync(parent().raw(), req, file, nullptr); + return !(req->result < 0); +} + +UVW_INLINE void file_req::datasync() { + uv_fs_req_cleanup(this->raw()); + uv_fs_fdatasync(parent().raw(), raw(), file, &fs_request_callback); +} + +UVW_INLINE bool file_req::datasync_sync() { + auto req = raw(); + uv_fs_req_cleanup(this->raw()); + uv_fs_fdatasync(parent().raw(), req, file, nullptr); + return !(req->result < 0); +} + +UVW_INLINE void file_req::truncate(int64_t offset) { + uv_fs_req_cleanup(this->raw()); + uv_fs_ftruncate(parent().raw(), raw(), file, offset, &fs_request_callback); +} + +UVW_INLINE bool file_req::truncate_sync(int64_t offset) { + auto req = raw(); + uv_fs_req_cleanup(this->raw()); + uv_fs_ftruncate(parent().raw(), req, file, offset, nullptr); + return !(req->result < 0); +} + +UVW_INLINE void file_req::sendfile(file_handle out, int64_t offset, std::size_t length) { + uv_fs_req_cleanup(this->raw()); + uv_fs_sendfile(parent().raw(), raw(), out, file, offset, length, &fs_request_callback); +} + +UVW_INLINE std::pair file_req::sendfile_sync(file_handle out, int64_t offset, std::size_t length) { + auto req = raw(); + uv_fs_req_cleanup(this->raw()); + uv_fs_sendfile(parent().raw(), req, out, file, offset, length, nullptr); + bool err = req->result < 0; + return std::make_pair(!err, err ? 0 : std::size_t(req->result)); +} + +UVW_INLINE void file_req::chmod(int mode) { + uv_fs_req_cleanup(this->raw()); + uv_fs_fchmod(parent().raw(), raw(), file, mode, &fs_request_callback); +} + +UVW_INLINE bool file_req::chmod_sync(int mode) { + auto req = raw(); + uv_fs_req_cleanup(this->raw()); + uv_fs_fchmod(parent().raw(), req, file, mode, nullptr); + return !(req->result < 0); +} + +UVW_INLINE void file_req::futime(fs_request::time atime, fs_request::time mtime) { + uv_fs_req_cleanup(this->raw()); + uv_fs_futime(parent().raw(), raw(), file, atime.count(), mtime.count(), &fs_request_callback); +} + +UVW_INLINE bool file_req::futime_sync(fs_request::time atime, fs_request::time mtime) { + auto req = raw(); + uv_fs_req_cleanup(this->raw()); + uv_fs_futime(parent().raw(), req, file, atime.count(), mtime.count(), nullptr); + return !(req->result < 0); +} + +UVW_INLINE void file_req::chown(uid_type uid, gid_type gid) { + uv_fs_req_cleanup(this->raw()); + uv_fs_fchown(parent().raw(), raw(), file, uid, gid, &fs_request_callback); +} + +UVW_INLINE bool file_req::chown_sync(uid_type uid, gid_type gid) { + auto req = raw(); + uv_fs_req_cleanup(this->raw()); + uv_fs_fchown(parent().raw(), req, file, uid, gid, nullptr); + return !(req->result < 0); +} + +UVW_INLINE file_req::operator file_handle() const noexcept { + return file; +} + +UVW_INLINE fs_req::~fs_req() noexcept { + uv_fs_req_cleanup(raw()); +} + +UVW_INLINE void fs_req::unlink(const std::string &path) { + uv_fs_req_cleanup(this->raw()); + uv_fs_unlink(parent().raw(), raw(), path.data(), &fs_request_callback); +} + +UVW_INLINE bool fs_req::unlink_sync(const std::string &path) { + auto req = raw(); + uv_fs_req_cleanup(this->raw()); + uv_fs_unlink(parent().raw(), req, path.data(), nullptr); + return !(req->result < 0); +} + +UVW_INLINE void fs_req::mkdir(const std::string &path, int mode) { + uv_fs_req_cleanup(this->raw()); + uv_fs_mkdir(parent().raw(), raw(), path.data(), mode, &fs_request_callback); +} + +UVW_INLINE bool fs_req::mkdir_sync(const std::string &path, int mode) { + auto req = raw(); + uv_fs_req_cleanup(this->raw()); + uv_fs_mkdir(parent().raw(), req, path.data(), mode, nullptr); + return !(req->result < 0); +} + +UVW_INLINE void fs_req::mkdtemp(const std::string &tpl) { + uv_fs_req_cleanup(this->raw()); + uv_fs_mkdtemp(parent().raw(), raw(), tpl.data(), &fs_request_callback); +} + +UVW_INLINE std::pair fs_req::mkdtemp_sync(const std::string &tpl) { + auto req = raw(); + uv_fs_req_cleanup(this->raw()); + uv_fs_mkdtemp(parent().raw(), req, tpl.data(), nullptr); + return std::make_pair(!(req->result < 0), req->path); +} + +UVW_INLINE void fs_req::mkstemp(const std::string &tpl) { + uv_fs_req_cleanup(this->raw()); + uv_fs_mkstemp(parent().raw(), raw(), tpl.data(), &fs_request_callback); +} + +UVW_INLINE std::pair> fs_req::mkstemp_sync(const std::string &tpl) { + std::pair> ret{false, {}}; + auto req = raw(); + uv_fs_req_cleanup(this->raw()); + uv_fs_mkstemp(parent().raw(), req, tpl.data(), nullptr); + ret.first = !(req->result < 0); + + if(ret.first) { + ret.second.first = req->path; + ret.second.second = static_cast(req->result); + } + + return ret; +} + +UVW_INLINE void fs_req::lutime(const std::string &path, time atime, time mtime) { + uv_fs_req_cleanup(this->raw()); + uv_fs_lutime(parent().raw(), raw(), path.data(), atime.count(), mtime.count(), &fs_request_callback); +} + +UVW_INLINE bool fs_req::lutime_sync(const std::string &path, time atime, time mtime) { + auto req = raw(); + uv_fs_req_cleanup(this->raw()); + uv_fs_lutime(parent().raw(), req, path.data(), atime.count(), mtime.count(), nullptr); + return !(req->result < 0); +} + +UVW_INLINE void fs_req::rmdir(const std::string &path) { + uv_fs_req_cleanup(this->raw()); + uv_fs_rmdir(parent().raw(), raw(), path.data(), &fs_request_callback); +} + +UVW_INLINE bool fs_req::rmdir_sync(const std::string &path) { + auto req = raw(); + uv_fs_req_cleanup(this->raw()); + uv_fs_rmdir(parent().raw(), req, path.data(), nullptr); + return !(req->result < 0); +} + +UVW_INLINE void fs_req::scandir(const std::string &path, int flags) { + uv_fs_req_cleanup(this->raw()); + uv_fs_scandir(parent().raw(), raw(), path.data(), flags, &fs_request_callback); +} + +UVW_INLINE std::pair fs_req::scandir_sync(const std::string &path, int flags) { + auto req = raw(); + uv_fs_req_cleanup(this->raw()); + uv_fs_scandir(parent().raw(), req, path.data(), flags, nullptr); + bool err = req->result < 0; + return std::make_pair(!err, err ? 0 : std::size_t(req->result)); +} + +UVW_INLINE std::pair> fs_req::scandir_next() { + std::pair> ret{false, {entry_type::UNKNOWN, nullptr}}; + + uv_fs_req_cleanup(raw()); + auto res = uv_fs_scandir_next(raw(), dirents); + + if(UV_EOF != res) { + ret.second.first = static_cast(dirents[0].type); + ret.second.second = dirents[0].name; + ret.first = true; + } + + return ret; +} + +UVW_INLINE void fs_req::stat(const std::string &path) { + uv_fs_req_cleanup(this->raw()); + uv_fs_stat(parent().raw(), raw(), path.data(), &fs_request_callback); +} + +UVW_INLINE std::pair fs_req::stat_sync(const std::string &path) { + auto req = raw(); + uv_fs_req_cleanup(this->raw()); + uv_fs_stat(parent().raw(), req, path.data(), nullptr); + return std::make_pair(!(req->result < 0), req->statbuf); +} + +UVW_INLINE void fs_req::lstat(const std::string &path) { + uv_fs_req_cleanup(this->raw()); + uv_fs_lstat(parent().raw(), raw(), path.data(), &fs_request_callback); +} + +UVW_INLINE std::pair fs_req::lstat_sync(const std::string &path) { + auto req = raw(); + uv_fs_req_cleanup(this->raw()); + uv_fs_lstat(parent().raw(), req, path.data(), nullptr); + return std::make_pair(!(req->result < 0), req->statbuf); +} + +UVW_INLINE void fs_req::statfs(const std::string &path) { + uv_fs_req_cleanup(this->raw()); + uv_fs_statfs(parent().raw(), raw(), path.data(), &fs_request_callback); +} + +UVW_INLINE std::pair fs_req::statfs_sync(const std::string &path) { + auto req = raw(); + uv_fs_req_cleanup(this->raw()); + uv_fs_statfs(parent().raw(), req, path.data(), nullptr); + return std::make_pair(!(req->result < 0), *static_cast(req->ptr)); +} + +UVW_INLINE void fs_req::rename(const std::string &old, const std::string &path) { + uv_fs_req_cleanup(this->raw()); + uv_fs_rename(parent().raw(), raw(), old.data(), path.data(), &fs_request_callback); +} + +UVW_INLINE bool fs_req::rename_sync(const std::string &old, const std::string &path) { + auto req = raw(); + uv_fs_req_cleanup(this->raw()); + uv_fs_rename(parent().raw(), req, old.data(), path.data(), nullptr); + return !(req->result < 0); +} + +UVW_INLINE void fs_req::copyfile(const std::string &old, const std::string &path, copy_file_flags flags) { + uv_fs_req_cleanup(this->raw()); + uv_fs_copyfile(parent().raw(), raw(), old.data(), path.data(), static_cast(flags), &fs_request_callback); +} + +UVW_INLINE bool fs_req::copyfile_sync(const std::string &old, const std::string &path, copy_file_flags flags) { + auto req = raw(); + uv_fs_req_cleanup(this->raw()); + uv_fs_copyfile(parent().raw(), raw(), old.data(), path.data(), static_cast(flags), nullptr); + return !(req->result < 0); +} + +UVW_INLINE void fs_req::access(const std::string &path, int mode) { + uv_fs_req_cleanup(this->raw()); + uv_fs_access(parent().raw(), raw(), path.data(), mode, &fs_request_callback); +} + +UVW_INLINE bool fs_req::access_sync(const std::string &path, int mode) { + auto req = raw(); + uv_fs_req_cleanup(this->raw()); + uv_fs_access(parent().raw(), req, path.data(), mode, nullptr); + return !(req->result < 0); +} + +UVW_INLINE void fs_req::chmod(const std::string &path, int mode) { + uv_fs_req_cleanup(this->raw()); + uv_fs_chmod(parent().raw(), raw(), path.data(), mode, &fs_request_callback); +} + +UVW_INLINE bool fs_req::chmod_sync(const std::string &path, int mode) { + auto req = raw(); + uv_fs_req_cleanup(this->raw()); + uv_fs_chmod(parent().raw(), req, path.data(), mode, nullptr); + return !(req->result < 0); +} + +UVW_INLINE void fs_req::utime(const std::string &path, fs_request::time atime, fs_request::time mtime) { + uv_fs_req_cleanup(this->raw()); + uv_fs_utime(parent().raw(), raw(), path.data(), atime.count(), mtime.count(), &fs_request_callback); +} + +UVW_INLINE bool fs_req::utime_sync(const std::string &path, fs_request::time atime, fs_request::time mtime) { + auto req = raw(); + uv_fs_req_cleanup(this->raw()); + uv_fs_utime(parent().raw(), req, path.data(), atime.count(), mtime.count(), nullptr); + return !(req->result < 0); +} + +UVW_INLINE void fs_req::link(const std::string &old, const std::string &path) { + uv_fs_req_cleanup(this->raw()); + uv_fs_link(parent().raw(), raw(), old.data(), path.data(), &fs_request_callback); +} + +UVW_INLINE bool fs_req::link_sync(const std::string &old, const std::string &path) { + auto req = raw(); + uv_fs_req_cleanup(this->raw()); + uv_fs_link(parent().raw(), req, old.data(), path.data(), nullptr); + return !(req->result < 0); +} + +UVW_INLINE void fs_req::symlink(const std::string &old, const std::string &path, symlink_flags flags) { + uv_fs_req_cleanup(this->raw()); + uv_fs_symlink(parent().raw(), raw(), old.data(), path.data(), static_cast(flags), &fs_request_callback); +} + +UVW_INLINE bool fs_req::symlink_sync(const std::string &old, const std::string &path, symlink_flags flags) { + auto req = raw(); + uv_fs_req_cleanup(this->raw()); + uv_fs_symlink(parent().raw(), req, old.data(), path.data(), static_cast(flags), nullptr); + return !(req->result < 0); +} + +UVW_INLINE void fs_req::readlink(const std::string &path) { + uv_fs_req_cleanup(this->raw()); + uv_fs_readlink(parent().raw(), raw(), path.data(), &fs_request_callback); +} + +UVW_INLINE std::pair> fs_req::readlink_sync(const std::string &path) { + auto req = raw(); + uv_fs_req_cleanup(this->raw()); + uv_fs_readlink(parent().raw(), req, path.data(), nullptr); + bool err = req->result < 0; + return std::make_pair(!err, std::make_pair(static_cast(req->ptr), err ? 0 : std::size_t(req->result))); +} + +UVW_INLINE void fs_req::realpath(const std::string &path) { + uv_fs_req_cleanup(this->raw()); + uv_fs_realpath(parent().raw(), raw(), path.data(), &fs_request_callback); +} + +UVW_INLINE std::pair fs_req::realpath_sync(const std::string &path) { + auto req = raw(); + uv_fs_req_cleanup(this->raw()); + uv_fs_realpath(parent().raw(), req, path.data(), nullptr); + return std::make_pair(!(req->result < 0), req->path); +} + +UVW_INLINE void fs_req::chown(const std::string &path, uid_type uid, gid_type gid) { + uv_fs_req_cleanup(this->raw()); + uv_fs_chown(parent().raw(), raw(), path.data(), uid, gid, &fs_request_callback); +} + +UVW_INLINE bool fs_req::chown_sync(const std::string &path, uid_type uid, gid_type gid) { + auto req = raw(); + uv_fs_req_cleanup(this->raw()); + uv_fs_chown(parent().raw(), req, path.data(), uid, gid, nullptr); + return !(req->result < 0); +} + +UVW_INLINE void fs_req::lchown(const std::string &path, uid_type uid, gid_type gid) { + uv_fs_req_cleanup(this->raw()); + uv_fs_lchown(parent().raw(), raw(), path.data(), uid, gid, &fs_request_callback); +} + +UVW_INLINE bool fs_req::lchown_sync(const std::string &path, uid_type uid, gid_type gid) { + auto req = raw(); + uv_fs_req_cleanup(this->raw()); + uv_fs_lchown(parent().raw(), req, path.data(), uid, gid, nullptr); + return !(req->result < 0); +} + +UVW_INLINE void fs_req::opendir(const std::string &path) { + uv_fs_req_cleanup(this->raw()); + uv_fs_opendir(parent().raw(), raw(), path.data(), &fs_request_callback); +} + +UVW_INLINE bool fs_req::opendir_sync(const std::string &path) { + auto req = raw(); + uv_fs_req_cleanup(this->raw()); + uv_fs_opendir(parent().raw(), req, path.data(), nullptr); + return !(req->result < 0); +} + +UVW_INLINE void fs_req::closedir() { + auto req = raw(); + auto *dir = static_cast(req->ptr); + uv_fs_req_cleanup(this->raw()); + uv_fs_closedir(parent().raw(), req, dir, &fs_request_callback); +} + +UVW_INLINE bool fs_req::closedir_sync() { + auto req = raw(); + auto *dir = static_cast(req->ptr); + uv_fs_req_cleanup(this->raw()); + uv_fs_closedir(parent().raw(), req, dir, nullptr); + return !(req->result < 0); +} + +UVW_INLINE void fs_req::readdir() { + auto req = raw(); + auto *dir = static_cast(req->ptr); + dir->dirents = dirents; + dir->nentries = 1; + uv_fs_req_cleanup(this->raw()); + uv_fs_readdir(parent().raw(), req, dir, &fs_request_callback); +} + +UVW_INLINE std::pair> fs_req::readdir_sync() { + auto req = raw(); + auto *dir = static_cast(req->ptr); + dir->dirents = dirents; + dir->nentries = 1; + uv_fs_req_cleanup(this->raw()); + uv_fs_readdir(parent().raw(), req, dir, nullptr); + return {req->result != 0, {static_cast(dirents[0].type), dirents[0].name}}; +} + +UVW_INLINE os_file_descriptor fs_helper::handle(file_handle file) noexcept { + return uv_get_osfhandle(file); +} + +UVW_INLINE file_handle fs_helper::open(os_file_descriptor descriptor) noexcept { + return uv_open_osfhandle(descriptor); +} + +} // namespace uvw diff --git a/src/uvw/fs_event.cpp b/src/uvw/fs_event.cpp index 19a25743..bedaea6f 100644 --- a/src/uvw/fs_event.cpp +++ b/src/uvw/fs_event.cpp @@ -1,37 +1,2 @@ -#ifdef UVW_AS_LIB -# include "fs_event.h" -#endif - -#include -#include "config.h" - -namespace uvw { - -UVW_INLINE fs_event_event::fs_event_event(const char *pathname, details::uvw_fs_event events) - : filename{pathname}, flags{std::move(events)} {} - -UVW_INLINE void fs_event_handle::start_callback(uv_fs_event_t *hndl, const char *filename, int events, int status) { - if(fs_event_handle &fsEvent = *(static_cast(hndl->data)); status) { - fsEvent.publish(error_event{status}); - } else { - fsEvent.publish(fs_event_event{filename, details::uvw_fs_event(events)}); - } -} - -UVW_INLINE int fs_event_handle::init() { - return leak_if(uv_fs_event_init(parent().raw(), raw())); -} - -UVW_INLINE int fs_event_handle::start(const std::string &path, event_flags flags) { - return uv_fs_event_start(raw(), &start_callback, path.data(), static_cast(flags)); -} - -UVW_INLINE int fs_event_handle::stop() { - return uv_fs_event_stop(raw()); -} - -UVW_INLINE std::string fs_event_handle::path() noexcept { - return details::try_read(&uv_fs_event_getpath, raw()); -} - -} // namespace uvw +#include "fs_event.h" +#include "fs_event.ipp" diff --git a/src/uvw/fs_event.h b/src/uvw/fs_event.h index 55b351b8..aa8cf243 100644 --- a/src/uvw/fs_event.h +++ b/src/uvw/fs_event.h @@ -114,7 +114,7 @@ public: } // namespace uvw #ifndef UVW_AS_LIB -# include "fs_event.cpp" +# include "fs_event.ipp" #endif #endif // UVW_FS_EVENT_INCLUDE_H diff --git a/src/uvw/fs_event.ipp b/src/uvw/fs_event.ipp new file mode 100644 index 00000000..e24c1c9b --- /dev/null +++ b/src/uvw/fs_event.ipp @@ -0,0 +1,33 @@ +#include +#include "config.h" + +namespace uvw { + +UVW_INLINE fs_event_event::fs_event_event(const char *pathname, details::uvw_fs_event events) + : filename{pathname}, flags{std::move(events)} {} + +UVW_INLINE void fs_event_handle::start_callback(uv_fs_event_t *hndl, const char *filename, int events, int status) { + if(fs_event_handle &fsEvent = *(static_cast(hndl->data)); status) { + fsEvent.publish(error_event{status}); + } else { + fsEvent.publish(fs_event_event{filename, details::uvw_fs_event(events)}); + } +} + +UVW_INLINE int fs_event_handle::init() { + return leak_if(uv_fs_event_init(parent().raw(), raw())); +} + +UVW_INLINE int fs_event_handle::start(const std::string &path, event_flags flags) { + return uv_fs_event_start(raw(), &start_callback, path.data(), static_cast(flags)); +} + +UVW_INLINE int fs_event_handle::stop() { + return uv_fs_event_stop(raw()); +} + +UVW_INLINE std::string fs_event_handle::path() noexcept { + return details::try_read(&uv_fs_event_getpath, raw()); +} + +} // namespace uvw diff --git a/src/uvw/fs_poll.cpp b/src/uvw/fs_poll.cpp index fe00e680..23294af3 100644 --- a/src/uvw/fs_poll.cpp +++ b/src/uvw/fs_poll.cpp @@ -1,37 +1,2 @@ -#ifdef UVW_AS_LIB -# include "fs_poll.h" -#endif - -#include -#include "config.h" - -namespace uvw { - -UVW_INLINE fs_poll_event::fs_poll_event(file_info previous, file_info current) noexcept - : prev{std::move(previous)}, curr{std::move(current)} {} - -UVW_INLINE void fs_poll_handle::start_callback(uv_fs_poll_t *hndl, int status, const uv_stat_t *prev, const uv_stat_t *curr) { - if(fs_poll_handle &fsPoll = *(static_cast(hndl->data)); status) { - fsPoll.publish(error_event{status}); - } else { - fsPoll.publish(fs_poll_event{*prev, *curr}); - } -} - -UVW_INLINE int fs_poll_handle::init() { - return leak_if(uv_fs_poll_init(parent().raw(), raw())); -} - -UVW_INLINE int fs_poll_handle::start(const std::string &file, fs_poll_handle::time interval) { - return uv_fs_poll_start(raw(), &start_callback, file.data(), interval.count()); -} - -UVW_INLINE int fs_poll_handle::stop() { - return uv_fs_poll_stop(raw()); -} - -UVW_INLINE std::string fs_poll_handle::path() noexcept { - return details::try_read(&uv_fs_poll_getpath, raw()); -} - -} // namespace uvw +#include "fs_poll.h" +#include "fs_poll.ipp" diff --git a/src/uvw/fs_poll.h b/src/uvw/fs_poll.h index 591a6d2d..16830666 100644 --- a/src/uvw/fs_poll.h +++ b/src/uvw/fs_poll.h @@ -70,7 +70,7 @@ public: } // namespace uvw #ifndef UVW_AS_LIB -# include "fs_poll.cpp" +# include "fs_poll.ipp" #endif #endif // UVW_FS_POLL_INCLUDE_H diff --git a/src/uvw/fs_poll.ipp b/src/uvw/fs_poll.ipp new file mode 100644 index 00000000..3f3b167d --- /dev/null +++ b/src/uvw/fs_poll.ipp @@ -0,0 +1,33 @@ +#include +#include "config.h" + +namespace uvw { + +UVW_INLINE fs_poll_event::fs_poll_event(file_info previous, file_info current) noexcept + : prev{std::move(previous)}, curr{std::move(current)} {} + +UVW_INLINE void fs_poll_handle::start_callback(uv_fs_poll_t *hndl, int status, const uv_stat_t *prev, const uv_stat_t *curr) { + if(fs_poll_handle &fsPoll = *(static_cast(hndl->data)); status) { + fsPoll.publish(error_event{status}); + } else { + fsPoll.publish(fs_poll_event{*prev, *curr}); + } +} + +UVW_INLINE int fs_poll_handle::init() { + return leak_if(uv_fs_poll_init(parent().raw(), raw())); +} + +UVW_INLINE int fs_poll_handle::start(const std::string &file, fs_poll_handle::time interval) { + return uv_fs_poll_start(raw(), &start_callback, file.data(), interval.count()); +} + +UVW_INLINE int fs_poll_handle::stop() { + return uv_fs_poll_stop(raw()); +} + +UVW_INLINE std::string fs_poll_handle::path() noexcept { + return details::try_read(&uv_fs_poll_getpath, raw()); +} + +} // namespace uvw diff --git a/src/uvw/idle.cpp b/src/uvw/idle.cpp index 03a0025b..6fd740fa 100644 --- a/src/uvw/idle.cpp +++ b/src/uvw/idle.cpp @@ -1,26 +1,2 @@ -#ifdef UVW_AS_LIB -# include "idle.h" -#endif - -#include "config.h" - -namespace uvw { - -UVW_INLINE void idle_handle::start_callback(uv_idle_t *hndl) { - idle_handle &idle = *(static_cast(hndl->data)); - idle.publish(idle_event{}); -} - -UVW_INLINE int idle_handle::init() { - return leak_if(uv_idle_init(parent().raw(), raw())); -} - -UVW_INLINE int idle_handle::start() { - return uv_idle_start(raw(), &start_callback); -} - -UVW_INLINE int idle_handle::stop() { - return uv_idle_stop(raw()); -} - -} // namespace uvw +#include "idle.h" +#include "idle.ipp" diff --git a/src/uvw/idle.h b/src/uvw/idle.h index fb7bcbd7..caad7d03 100644 --- a/src/uvw/idle.h +++ b/src/uvw/idle.h @@ -59,7 +59,7 @@ public: } // namespace uvw #ifndef UVW_AS_LIB -# include "idle.cpp" +# include "idle.ipp" #endif #endif // UVW_IDLE_INCLUDE_H diff --git a/src/uvw/idle.ipp b/src/uvw/idle.ipp new file mode 100644 index 00000000..7473a1c3 --- /dev/null +++ b/src/uvw/idle.ipp @@ -0,0 +1,22 @@ +#include "config.h" + +namespace uvw { + +UVW_INLINE void idle_handle::start_callback(uv_idle_t *hndl) { + idle_handle &idle = *(static_cast(hndl->data)); + idle.publish(idle_event{}); +} + +UVW_INLINE int idle_handle::init() { + return leak_if(uv_idle_init(parent().raw(), raw())); +} + +UVW_INLINE int idle_handle::start() { + return uv_idle_start(raw(), &start_callback); +} + +UVW_INLINE int idle_handle::stop() { + return uv_idle_stop(raw()); +} + +} // namespace uvw diff --git a/src/uvw/lib.cpp b/src/uvw/lib.cpp index 5322a7c8..d7619b49 100644 --- a/src/uvw/lib.cpp +++ b/src/uvw/lib.cpp @@ -1,27 +1,2 @@ -#ifdef UVW_AS_LIB -# include "lib.h" -#endif - -#include -#include "config.h" - -namespace uvw { - -UVW_INLINE shared_lib::shared_lib(loop::token token, std::shared_ptr ref, const std::string &filename) noexcept - : uv_type{token, std::move(ref)} { - opened = (0 == uv_dlopen(filename.data(), raw())); -} - -UVW_INLINE shared_lib::~shared_lib() noexcept { - uv_dlclose(raw()); -} - -UVW_INLINE shared_lib::operator bool() const noexcept { - return opened; -} - -UVW_INLINE const char *shared_lib::error() const noexcept { - return uv_dlerror(raw()); -} - -} // namespace uvw +#include "lib.h" +#include "lib.ipp" diff --git a/src/uvw/lib.h b/src/uvw/lib.h index 7bb720ca..d27182c7 100644 --- a/src/uvw/lib.h +++ b/src/uvw/lib.h @@ -43,7 +43,9 @@ public: static_assert(std::is_function_v); F *func; auto err = uv_dlsym(raw(), name.data(), reinterpret_cast(&func)); - if(err) { func = nullptr; } + if(err) { + func = nullptr; + } return func; } @@ -60,7 +62,7 @@ private: } // namespace uvw #ifndef UVW_AS_LIB -# include "lib.cpp" +# include "lib.ipp" #endif #endif // UVW_LIB_INCLUDE_H diff --git a/src/uvw/lib.ipp b/src/uvw/lib.ipp new file mode 100644 index 00000000..660fda5d --- /dev/null +++ b/src/uvw/lib.ipp @@ -0,0 +1,23 @@ +#include +#include "config.h" + +namespace uvw { + +UVW_INLINE shared_lib::shared_lib(loop::token token, std::shared_ptr ref, const std::string &filename) noexcept + : uv_type{token, std::move(ref)} { + opened = (0 == uv_dlopen(filename.data(), raw())); +} + +UVW_INLINE shared_lib::~shared_lib() noexcept { + uv_dlclose(raw()); +} + +UVW_INLINE shared_lib::operator bool() const noexcept { + return opened; +} + +UVW_INLINE const char *shared_lib::error() const noexcept { + return uv_dlerror(raw()); +} + +} // namespace uvw diff --git a/src/uvw/loop.cpp b/src/uvw/loop.cpp index e3ab8702..5137fe68 100644 --- a/src/uvw/loop.cpp +++ b/src/uvw/loop.cpp @@ -1,120 +1,2 @@ -#ifdef UVW_AS_LIB -# include "loop.h" -#endif - -#include "config.h" - -namespace uvw { - -UVW_INLINE loop::loop(std::unique_ptr ptr) noexcept - : uv_loop{std::move(ptr)} {} - -UVW_INLINE std::shared_ptr loop::create() { - auto ptr = std::unique_ptr{new uv_loop_t, [](uv_loop_t *l) { delete l; }}; - auto curr = std::shared_ptr{new loop{std::move(ptr)}}; - - if(uv_loop_init(curr->uv_loop.get())) { - curr = nullptr; - } - - return curr; -} - -UVW_INLINE std::shared_ptr loop::create(uv_loop_t *res) { - auto ptr = std::unique_ptr{res, [](uv_loop_t *) {}}; - return std::shared_ptr{new loop{std::move(ptr)}}; -} - -UVW_INLINE std::shared_ptr loop::get_default() { - static std::weak_ptr ref; - std::shared_ptr curr; - - if(ref.expired()) { - auto def = uv_default_loop(); - - if(def) { - auto ptr = std::unique_ptr(def, [](uv_loop_t *) {}); - curr = std::shared_ptr{new loop{std::move(ptr)}}; - } - - ref = curr; - } else { - curr = ref.lock(); - } - - return curr; -} - -UVW_INLINE loop::~loop() noexcept { - if(uv_loop) { - close(); - } -} - -UVW_INLINE int loop::close() { - int ret = 0; - - if(uv_loop) { - ret = uv_loop_close(uv_loop.get()); - uv_loop.reset(); - } - - return ret; -} - -UVW_INLINE int loop::run(run_mode mode) noexcept { - return uv_run(uv_loop.get(), static_cast(mode)); -} - -UVW_INLINE bool loop::alive() const noexcept { - return !!uv_loop_alive(uv_loop.get()); -} - -UVW_INLINE void loop::stop() noexcept { - uv_stop(uv_loop.get()); -} - -UVW_INLINE int loop::descriptor() const noexcept { - return uv_backend_fd(uv_loop.get()); -} - -UVW_INLINE std::pair loop::timeout() const noexcept { - auto to = uv_backend_timeout(uv_loop.get()); - return std::make_pair(to == -1, time{to}); -} - -UVW_INLINE loop::time loop::idle_time() const noexcept { - return time{uv_metrics_idle_time(uv_loop.get())}; -} - -UVW_INLINE metrics_type loop::metrics() const noexcept { - metrics_type res{}; - uv_metrics_info(uv_loop.get(), &res); - return res; -} - -UVW_INLINE loop::time loop::now() const noexcept { - return time{uv_now(uv_loop.get())}; -} - -UVW_INLINE void loop::update() const noexcept { - return uv_update_time(uv_loop.get()); -} - -UVW_INLINE int loop::fork() noexcept { - return uv_loop_fork(uv_loop.get()); -} - -UVW_INLINE void loop::data(std::shared_ptr ud) { - user_data = std::move(ud); -} - -UVW_INLINE const uv_loop_t *loop::raw() const noexcept { - return uv_loop.get(); -} - -UVW_INLINE uv_loop_t *loop::raw() noexcept { - return const_cast(const_cast(this)->raw()); -} - -} // namespace uvw +#include "loop.h" +#include "loop.ipp" diff --git a/src/uvw/loop.h b/src/uvw/loop.h index 47b3175a..cf30ac8b 100644 --- a/src/uvw/loop.h +++ b/src/uvw/loop.h @@ -433,7 +433,7 @@ private: } // namespace uvw #ifndef UVW_AS_LIB -# include "loop.cpp" +# include "loop.ipp" #endif #endif // UVW_LOOP_INCLUDE_H diff --git a/src/uvw/loop.ipp b/src/uvw/loop.ipp new file mode 100644 index 00000000..0a238611 --- /dev/null +++ b/src/uvw/loop.ipp @@ -0,0 +1,116 @@ +#include "config.h" + +namespace uvw { + +UVW_INLINE loop::loop(std::unique_ptr ptr) noexcept + : uv_loop{std::move(ptr)} {} + +UVW_INLINE std::shared_ptr loop::create() { + auto ptr = std::unique_ptr{new uv_loop_t, [](uv_loop_t *l) { delete l; }}; + auto curr = std::shared_ptr{new loop{std::move(ptr)}}; + + if(uv_loop_init(curr->uv_loop.get())) { + curr = nullptr; + } + + return curr; +} + +UVW_INLINE std::shared_ptr loop::create(uv_loop_t *res) { + auto ptr = std::unique_ptr{res, [](uv_loop_t *) {}}; + return std::shared_ptr{new loop{std::move(ptr)}}; +} + +UVW_INLINE std::shared_ptr loop::get_default() { + static std::weak_ptr ref; + std::shared_ptr curr; + + if(ref.expired()) { + auto def = uv_default_loop(); + + if(def) { + auto ptr = std::unique_ptr(def, [](uv_loop_t *) {}); + curr = std::shared_ptr{new loop{std::move(ptr)}}; + } + + ref = curr; + } else { + curr = ref.lock(); + } + + return curr; +} + +UVW_INLINE loop::~loop() noexcept { + if(uv_loop) { + close(); + } +} + +UVW_INLINE int loop::close() { + int ret = 0; + + if(uv_loop) { + ret = uv_loop_close(uv_loop.get()); + uv_loop.reset(); + } + + return ret; +} + +UVW_INLINE int loop::run(run_mode mode) noexcept { + return uv_run(uv_loop.get(), static_cast(mode)); +} + +UVW_INLINE bool loop::alive() const noexcept { + return !!uv_loop_alive(uv_loop.get()); +} + +UVW_INLINE void loop::stop() noexcept { + uv_stop(uv_loop.get()); +} + +UVW_INLINE int loop::descriptor() const noexcept { + return uv_backend_fd(uv_loop.get()); +} + +UVW_INLINE std::pair loop::timeout() const noexcept { + auto to = uv_backend_timeout(uv_loop.get()); + return std::make_pair(to == -1, time{to}); +} + +UVW_INLINE loop::time loop::idle_time() const noexcept { + return time{uv_metrics_idle_time(uv_loop.get())}; +} + +UVW_INLINE metrics_type loop::metrics() const noexcept { + metrics_type res{}; + uv_metrics_info(uv_loop.get(), &res); + return res; +} + +UVW_INLINE loop::time loop::now() const noexcept { + return time{uv_now(uv_loop.get())}; +} + +UVW_INLINE void loop::update() const noexcept { + return uv_update_time(uv_loop.get()); +} + +UVW_INLINE int loop::fork() noexcept { + return uv_loop_fork(uv_loop.get()); +} + +UVW_INLINE void loop::data(std::shared_ptr ud) { + user_data = std::move(ud); +} + +UVW_INLINE const uv_loop_t *loop::raw() const noexcept { + return uv_loop.get(); +} + +UVW_INLINE uv_loop_t *loop::raw() noexcept { + return const_cast(const_cast(this)->raw()); +} + +} // namespace uvw diff --git a/src/uvw/pipe.cpp b/src/uvw/pipe.cpp index 49652282..ac44cada 100644 --- a/src/uvw/pipe.cpp +++ b/src/uvw/pipe.cpp @@ -1,63 +1,2 @@ -#ifdef UVW_AS_LIB -# include "pipe.h" -#endif - -#include -#include "config.h" - -namespace uvw { - -UVW_INLINE pipe_handle::pipe_handle(loop::token token, std::shared_ptr ref, bool pass) - : stream_handle{token, std::move(ref)}, ipc{pass} {} - -UVW_INLINE int pipe_handle::init() { - return leak_if(uv_pipe_init(parent().raw(), raw(), ipc)); -} - -UVW_INLINE int pipe_handle::open(file_handle file) { - return uv_pipe_open(raw(), file); -} - -UVW_INLINE int pipe_handle::bind(const std::string &name, const bool no_truncate) { - return uv_pipe_bind2(raw(), name.data(), name.size(), no_truncate * UV_PIPE_NO_TRUNCATE); -} - -UVW_INLINE int pipe_handle::connect(const std::string &name, const bool no_truncate) { - auto listener = [ptr = shared_from_this()](const auto &event, const auto &) { - ptr->publish(event); - }; - - auto connect = parent().resource(); - connect->on(listener); - connect->on(listener); - - unsigned int flags = no_truncate * UV_PIPE_NO_TRUNCATE; - return connect->connect(&uv_pipe_connect2, raw(), name.data(), name.size(), flags); -} - -UVW_INLINE std::string pipe_handle::sock() const noexcept { - return details::try_read(&uv_pipe_getsockname, raw()); -} - -UVW_INLINE std::string pipe_handle::peer() const noexcept { - return details::try_read(&uv_pipe_getpeername, raw()); -} - -UVW_INLINE void pipe_handle::pending(int count) noexcept { - uv_pipe_pending_instances(raw(), count); -} - -UVW_INLINE int pipe_handle::pending() noexcept { - return uv_pipe_pending_count(raw()); -} - -UVW_INLINE handle_type pipe_handle::receive() noexcept { - handle_category category = uv_pipe_pending_type(raw()); - return utilities::guess_handle(category); -} - -UVW_INLINE int pipe_handle::chmod(chmod_flags flags) noexcept { - return uv_pipe_chmod(raw(), static_cast(flags)); -} - -} // namespace uvw +#include "pipe.h" +#include "pipe.ipp" diff --git a/src/uvw/pipe.h b/src/uvw/pipe.h index 26bd0268..b2775869 100644 --- a/src/uvw/pipe.h +++ b/src/uvw/pipe.h @@ -158,7 +158,7 @@ private: } // namespace uvw #ifndef UVW_AS_LIB -# include "pipe.cpp" +# include "pipe.ipp" #endif #endif // UVW_PIPE_INCLUDE_H diff --git a/src/uvw/pipe.ipp b/src/uvw/pipe.ipp new file mode 100644 index 00000000..3bfd660d --- /dev/null +++ b/src/uvw/pipe.ipp @@ -0,0 +1,59 @@ +#include +#include "config.h" + +namespace uvw { + +UVW_INLINE pipe_handle::pipe_handle(loop::token token, std::shared_ptr ref, bool pass) + : stream_handle{token, std::move(ref)}, ipc{pass} {} + +UVW_INLINE int pipe_handle::init() { + return leak_if(uv_pipe_init(parent().raw(), raw(), ipc)); +} + +UVW_INLINE int pipe_handle::open(file_handle file) { + return uv_pipe_open(raw(), file); +} + +UVW_INLINE int pipe_handle::bind(const std::string &name, const bool no_truncate) { + return uv_pipe_bind2(raw(), name.data(), name.size(), no_truncate * UV_PIPE_NO_TRUNCATE); +} + +UVW_INLINE int pipe_handle::connect(const std::string &name, const bool no_truncate) { + auto listener = [ptr = shared_from_this()](const auto &event, const auto &) { + ptr->publish(event); + }; + + auto connect = parent().resource(); + connect->on(listener); + connect->on(listener); + + unsigned int flags = no_truncate * UV_PIPE_NO_TRUNCATE; + return connect->connect(&uv_pipe_connect2, raw(), name.data(), name.size(), flags); +} + +UVW_INLINE std::string pipe_handle::sock() const noexcept { + return details::try_read(&uv_pipe_getsockname, raw()); +} + +UVW_INLINE std::string pipe_handle::peer() const noexcept { + return details::try_read(&uv_pipe_getpeername, raw()); +} + +UVW_INLINE void pipe_handle::pending(int count) noexcept { + uv_pipe_pending_instances(raw(), count); +} + +UVW_INLINE int pipe_handle::pending() noexcept { + return uv_pipe_pending_count(raw()); +} + +UVW_INLINE handle_type pipe_handle::receive() noexcept { + handle_category category = uv_pipe_pending_type(raw()); + return utilities::guess_handle(category); +} + +UVW_INLINE int pipe_handle::chmod(chmod_flags flags) noexcept { + return uv_pipe_chmod(raw(), static_cast(flags)); +} + +} // namespace uvw diff --git a/src/uvw/poll.cpp b/src/uvw/poll.cpp index e71fccbb..3c2d04a8 100644 --- a/src/uvw/poll.cpp +++ b/src/uvw/poll.cpp @@ -1,43 +1,2 @@ -#ifdef UVW_AS_LIB -# include "poll.h" -#endif - -#include -#include "config.h" - -namespace uvw { - -UVW_INLINE poll_event::poll_event(details::uvw_poll_event events) noexcept - : flags{std::move(events)} {} - -UVW_INLINE poll_handle::poll_handle(loop::token token, std::shared_ptr ref, int desc) - : handle{token, std::move(ref)}, tag{FD}, file_desc{desc} {} - -UVW_INLINE poll_handle::poll_handle(loop::token token, std::shared_ptr ref, os_socket_handle sock) - : handle{token, std::move(ref)}, tag{SOCKET}, socket{sock} {} - -UVW_INLINE void poll_handle::start_callback(uv_poll_t *hndl, int status, int events) { - if(poll_handle &poll = *(static_cast(hndl->data)); status) { - poll.publish(error_event{status}); - } else { - poll.publish(poll_event{poll_event_flags(events)}); - } -} - -UVW_INLINE int poll_handle::init() { - if(tag == SOCKET) { - return leak_if(uv_poll_init_socket(parent().raw(), raw(), socket)); - } else { - return leak_if(uv_poll_init(parent().raw(), raw(), file_desc)); - } -} - -UVW_INLINE int poll_handle::start(poll_event_flags flags) { - return uv_poll_start(raw(), static_cast(flags), &start_callback); -} - -UVW_INLINE int poll_handle::stop() { - return uv_poll_stop(raw()); -} - -} // namespace uvw +#include "poll.h" +#include "poll.ipp" diff --git a/src/uvw/poll.h b/src/uvw/poll.h index cceb012d..1470b316 100644 --- a/src/uvw/poll.h +++ b/src/uvw/poll.h @@ -113,7 +113,7 @@ private: } // namespace uvw #ifndef UVW_AS_LIB -# include "poll.cpp" +# include "poll.ipp" #endif #endif // UVW_POLL_INCLUDE_H diff --git a/src/uvw/poll.ipp b/src/uvw/poll.ipp new file mode 100644 index 00000000..421335c3 --- /dev/null +++ b/src/uvw/poll.ipp @@ -0,0 +1,39 @@ +#include +#include "config.h" + +namespace uvw { + +UVW_INLINE poll_event::poll_event(details::uvw_poll_event events) noexcept + : flags{std::move(events)} {} + +UVW_INLINE poll_handle::poll_handle(loop::token token, std::shared_ptr ref, int desc) + : handle{token, std::move(ref)}, tag{FD}, file_desc{desc} {} + +UVW_INLINE poll_handle::poll_handle(loop::token token, std::shared_ptr ref, os_socket_handle sock) + : handle{token, std::move(ref)}, tag{SOCKET}, socket{sock} {} + +UVW_INLINE void poll_handle::start_callback(uv_poll_t *hndl, int status, int events) { + if(poll_handle &poll = *(static_cast(hndl->data)); status) { + poll.publish(error_event{status}); + } else { + poll.publish(poll_event{poll_event_flags(events)}); + } +} + +UVW_INLINE int poll_handle::init() { + if(tag == SOCKET) { + return leak_if(uv_poll_init_socket(parent().raw(), raw(), socket)); + } else { + return leak_if(uv_poll_init(parent().raw(), raw(), file_desc)); + } +} + +UVW_INLINE int poll_handle::start(poll_event_flags flags) { + return uv_poll_start(raw(), static_cast(flags), &start_callback); +} + +UVW_INLINE int poll_handle::stop() { + return uv_poll_stop(raw()); +} + +} // namespace uvw diff --git a/src/uvw/prepare.cpp b/src/uvw/prepare.cpp index dee7186e..faeabfb7 100644 --- a/src/uvw/prepare.cpp +++ b/src/uvw/prepare.cpp @@ -1,26 +1,2 @@ -#ifdef UVW_AS_LIB -# include "prepare.h" -#endif - -#include "config.h" - -namespace uvw { - -UVW_INLINE void prepare_handle::start_callback(uv_prepare_t *hndl) { - prepare_handle &prepare = *(static_cast(hndl->data)); - prepare.publish(prepare_event{}); -} - -UVW_INLINE int prepare_handle::init() { - return leak_if(uv_prepare_init(parent().raw(), raw())); -} - -UVW_INLINE int prepare_handle::start() { - return uv_prepare_start(raw(), &start_callback); -} - -UVW_INLINE int prepare_handle::stop() { - return uv_prepare_stop(raw()); -} - -} // namespace uvw +#include "prepare.h" +#include "prepare.ipp" diff --git a/src/uvw/prepare.h b/src/uvw/prepare.h index 8546acfd..91467217 100644 --- a/src/uvw/prepare.h +++ b/src/uvw/prepare.h @@ -52,7 +52,7 @@ public: } // namespace uvw #ifndef UVW_AS_LIB -# include "prepare.cpp" +# include "prepare.ipp" #endif #endif // UVW_PREPARE_INCLUDE_H diff --git a/src/uvw/prepare.ipp b/src/uvw/prepare.ipp new file mode 100644 index 00000000..7ca87f94 --- /dev/null +++ b/src/uvw/prepare.ipp @@ -0,0 +1,22 @@ +#include "config.h" + +namespace uvw { + +UVW_INLINE void prepare_handle::start_callback(uv_prepare_t *hndl) { + prepare_handle &prepare = *(static_cast(hndl->data)); + prepare.publish(prepare_event{}); +} + +UVW_INLINE int prepare_handle::init() { + return leak_if(uv_prepare_init(parent().raw(), raw())); +} + +UVW_INLINE int prepare_handle::start() { + return uv_prepare_start(raw(), &start_callback); +} + +UVW_INLINE int prepare_handle::stop() { + return uv_prepare_stop(raw()); +} + +} // namespace uvw diff --git a/src/uvw/process.cpp b/src/uvw/process.cpp index 3790dee9..ecc72e1f 100644 --- a/src/uvw/process.cpp +++ b/src/uvw/process.cpp @@ -1,111 +1,2 @@ -#ifdef UVW_AS_LIB -# include "process.h" -#endif - -#include -#include "config.h" - -namespace uvw { - -UVW_INLINE exit_event::exit_event(int64_t code, int sig) noexcept - : status{code}, signal{sig} {} - -UVW_INLINE void process_handle::exit_callback(uv_process_t *hndl, int64_t exit_status, int term_signal) { - process_handle &process = *(static_cast(hndl->data)); - process.publish(exit_event{exit_status, term_signal}); -} - -UVW_INLINE process_handle::process_handle(loop::token token, std::shared_ptr ref) - : handle{token, std::move(ref)} {} - -UVW_INLINE void process_handle::disable_stdio_inheritance() noexcept { - uv_disable_stdio_inheritance(); -} - -UVW_INLINE bool process_handle::kill(int pid, int signum) noexcept { - return (0 == uv_kill(pid, signum)); -} - -UVW_INLINE int process_handle::init() { - // deferred initialization: libuv initializes process handles only when - // uv_spawn is invoked and uvw stays true to the underlying library - return 0; -} - -UVW_INLINE int process_handle::spawn(const char *file, char **args, char **env) { - uv_process_options_t po; - - po.exit_cb = &exit_callback; - po.file = file; - po.args = args; - po.env = env; - po.cwd = po_cwd.empty() ? nullptr : po_cwd.data(); - po.flags = static_cast(po_flags); - po.uid = po_uid; - po.gid = po_gid; - - std::vector poStdio; - poStdio.reserve(po_fd_stdio.size() + po_stream_stdio.size()); - poStdio.insert(poStdio.begin(), po_fd_stdio.cbegin(), po_fd_stdio.cend()); - poStdio.insert(poStdio.end(), po_stream_stdio.cbegin(), po_stream_stdio.cend()); - - po.stdio_count = static_cast(poStdio.size()); - po.stdio = poStdio.data(); - - // see init member function for more details - leak_if(0); - - return uv_spawn(parent().raw(), raw(), &po); -} - -UVW_INLINE int process_handle::kill(int signum) { - return uv_process_kill(raw(), signum); -} - -UVW_INLINE int process_handle::pid() noexcept { - return raw()->pid; -} - -UVW_INLINE process_handle &process_handle::cwd(const std::string &path) noexcept { - po_cwd = path; - return *this; -} - -UVW_INLINE process_handle &process_handle::flags(process_flags flags) noexcept { - po_flags = flags; - return *this; -} - -UVW_INLINE process_handle &process_handle::stdio(file_handle fd, stdio_flags flags) { - auto fgs = static_cast(flags); - - auto actual = uvw::file_handle{fd}; - - auto it = std::find_if(po_fd_stdio.begin(), po_fd_stdio.end(), [actual](auto &&container) { - return static_cast>(container.data.fd) == static_cast>(actual); - }); - - if(it == po_fd_stdio.cend()) { - uv_stdio_container_t container; - container.flags = fgs; - container.data.fd = actual; - po_fd_stdio.push_back(std::move(container)); - } else { - it->flags = fgs; - it->data.fd = actual; - } - - return *this; -} - -UVW_INLINE process_handle &process_handle::uid(uid_type id) { - po_uid = id; - return *this; -} - -UVW_INLINE process_handle &process_handle::gid(gid_type id) { - po_gid = id; - return *this; -} - -} // namespace uvw +#include "process.h" +#include "process.ipp" diff --git a/src/uvw/process.h b/src/uvw/process.h index f14d12ba..7767c35a 100644 --- a/src/uvw/process.h +++ b/src/uvw/process.h @@ -239,7 +239,7 @@ private: } // namespace uvw #ifndef UVW_AS_LIB -# include "process.cpp" +# include "process.ipp" #endif #endif // UVW_PROCESS_INCLUDE_H diff --git a/src/uvw/process.ipp b/src/uvw/process.ipp new file mode 100644 index 00000000..339b876f --- /dev/null +++ b/src/uvw/process.ipp @@ -0,0 +1,107 @@ +#include +#include "config.h" + +namespace uvw { + +UVW_INLINE exit_event::exit_event(int64_t code, int sig) noexcept + : status{code}, signal{sig} {} + +UVW_INLINE void process_handle::exit_callback(uv_process_t *hndl, int64_t exit_status, int term_signal) { + process_handle &process = *(static_cast(hndl->data)); + process.publish(exit_event{exit_status, term_signal}); +} + +UVW_INLINE process_handle::process_handle(loop::token token, std::shared_ptr ref) + : handle{token, std::move(ref)} {} + +UVW_INLINE void process_handle::disable_stdio_inheritance() noexcept { + uv_disable_stdio_inheritance(); +} + +UVW_INLINE bool process_handle::kill(int pid, int signum) noexcept { + return (0 == uv_kill(pid, signum)); +} + +UVW_INLINE int process_handle::init() { + // deferred initialization: libuv initializes process handles only when + // uv_spawn is invoked and uvw stays true to the underlying library + return 0; +} + +UVW_INLINE int process_handle::spawn(const char *file, char **args, char **env) { + uv_process_options_t po; + + po.exit_cb = &exit_callback; + po.file = file; + po.args = args; + po.env = env; + po.cwd = po_cwd.empty() ? nullptr : po_cwd.data(); + po.flags = static_cast(po_flags); + po.uid = po_uid; + po.gid = po_gid; + + std::vector poStdio; + poStdio.reserve(po_fd_stdio.size() + po_stream_stdio.size()); + poStdio.insert(poStdio.begin(), po_fd_stdio.cbegin(), po_fd_stdio.cend()); + poStdio.insert(poStdio.end(), po_stream_stdio.cbegin(), po_stream_stdio.cend()); + + po.stdio_count = static_cast(poStdio.size()); + po.stdio = poStdio.data(); + + // see init member function for more details + leak_if(0); + + return uv_spawn(parent().raw(), raw(), &po); +} + +UVW_INLINE int process_handle::kill(int signum) { + return uv_process_kill(raw(), signum); +} + +UVW_INLINE int process_handle::pid() noexcept { + return raw()->pid; +} + +UVW_INLINE process_handle &process_handle::cwd(const std::string &path) noexcept { + po_cwd = path; + return *this; +} + +UVW_INLINE process_handle &process_handle::flags(process_flags flags) noexcept { + po_flags = flags; + return *this; +} + +UVW_INLINE process_handle &process_handle::stdio(file_handle fd, stdio_flags flags) { + auto fgs = static_cast(flags); + + auto actual = uvw::file_handle{fd}; + + auto it = std::find_if(po_fd_stdio.begin(), po_fd_stdio.end(), [actual](auto &&container) { + return static_cast>(container.data.fd) == static_cast>(actual); + }); + + if(it == po_fd_stdio.cend()) { + uv_stdio_container_t container; + container.flags = fgs; + container.data.fd = actual; + po_fd_stdio.push_back(std::move(container)); + } else { + it->flags = fgs; + it->data.fd = actual; + } + + return *this; +} + +UVW_INLINE process_handle &process_handle::uid(uid_type id) { + po_uid = id; + return *this; +} + +UVW_INLINE process_handle &process_handle::gid(gid_type id) { + po_gid = id; + return *this; +} + +} // namespace uvw diff --git a/src/uvw/signal.cpp b/src/uvw/signal.cpp index 3e04e62e..c069846a 100644 --- a/src/uvw/signal.cpp +++ b/src/uvw/signal.cpp @@ -1,37 +1,2 @@ -#ifdef UVW_AS_LIB -# include "signal.h" -#endif - -#include "config.h" - -namespace uvw { - -UVW_INLINE signal_event::signal_event(int sig) noexcept - : signum{sig} {} - -UVW_INLINE void signal_handle::start_callback(uv_signal_t *hndl, int signum) { - signal_handle &signal = *(static_cast(hndl->data)); - signal.publish(signal_event{signum}); -} - -UVW_INLINE int signal_handle::init() { - return leak_if(uv_signal_init(parent().raw(), raw())); -} - -UVW_INLINE int signal_handle::start(int signum) { - return uv_signal_start(raw(), &start_callback, signum); -} - -UVW_INLINE int signal_handle::one_shot(int signum) { - return uv_signal_start_oneshot(raw(), &start_callback, signum); -} - -UVW_INLINE int signal_handle::stop() { - return uv_signal_stop(raw()); -} - -UVW_INLINE int signal_handle::signal() const noexcept { - return raw()->signum; -} - -} // namespace uvw +#include "signal.h" +#include "signal.ipp" diff --git a/src/uvw/signal.h b/src/uvw/signal.h index 66060966..704005cf 100644 --- a/src/uvw/signal.h +++ b/src/uvw/signal.h @@ -77,7 +77,7 @@ public: } // namespace uvw #ifndef UVW_AS_LIB -# include "signal.cpp" +# include "signal.ipp" #endif #endif // UVW_SIGNAL_INCLUDE_H diff --git a/src/uvw/signal.ipp b/src/uvw/signal.ipp new file mode 100644 index 00000000..8f434541 --- /dev/null +++ b/src/uvw/signal.ipp @@ -0,0 +1,33 @@ +#include "config.h" + +namespace uvw { + +UVW_INLINE signal_event::signal_event(int sig) noexcept + : signum{sig} {} + +UVW_INLINE void signal_handle::start_callback(uv_signal_t *hndl, int signum) { + signal_handle &signal = *(static_cast(hndl->data)); + signal.publish(signal_event{signum}); +} + +UVW_INLINE int signal_handle::init() { + return leak_if(uv_signal_init(parent().raw(), raw())); +} + +UVW_INLINE int signal_handle::start(int signum) { + return uv_signal_start(raw(), &start_callback, signum); +} + +UVW_INLINE int signal_handle::one_shot(int signum) { + return uv_signal_start_oneshot(raw(), &start_callback, signum); +} + +UVW_INLINE int signal_handle::stop() { + return uv_signal_stop(raw()); +} + +UVW_INLINE int signal_handle::signal() const noexcept { + return raw()->signum; +} + +} // namespace uvw diff --git a/src/uvw/stream.cpp b/src/uvw/stream.cpp index 12b041b1..1c099b71 100644 --- a/src/uvw/stream.cpp +++ b/src/uvw/stream.cpp @@ -1,33 +1,2 @@ -#ifdef UVW_AS_LIB -# include "stream.h" -#endif - -#include "config.h" - -namespace uvw { - -UVW_INLINE data_event::data_event(std::unique_ptr buf, std::size_t len) noexcept - : data{std::move(buf)}, - length{len} {} - -UVW_INLINE void details::connect_req::connect_callback(uv_connect_t *req, int status) { - if(auto ptr = reserve(req); status) { - ptr->publish(error_event{status}); - } else { - ptr->publish(connect_event{}); - } -} - -UVW_INLINE void details::shutdown_req::shoutdown_callback(uv_shutdown_t *req, int status) { - if(auto ptr = reserve(req); status) { - ptr->publish(error_event{status}); - } else { - ptr->publish(shutdown_event{}); - } -} - -UVW_INLINE int details::shutdown_req::shutdown(uv_stream_t *hndl) { - return this->leak_if(uv_shutdown(raw(), hndl, &shoutdown_callback)); -} - -} // namespace uvw +#include "stream.h" +#include "stream.ipp" diff --git a/src/uvw/stream.h b/src/uvw/stream.h index 04fcad73..a7b26983 100644 --- a/src/uvw/stream.h +++ b/src/uvw/stream.h @@ -475,7 +475,7 @@ public: } // namespace uvw #ifndef UVW_AS_LIB -# include "stream.cpp" +# include "stream.ipp" #endif #endif // UVW_STREAM_INCLUDE_H diff --git a/src/uvw/stream.ipp b/src/uvw/stream.ipp new file mode 100644 index 00000000..3c610831 --- /dev/null +++ b/src/uvw/stream.ipp @@ -0,0 +1,29 @@ +#include "config.h" + +namespace uvw { + +UVW_INLINE data_event::data_event(std::unique_ptr buf, std::size_t len) noexcept + : data{std::move(buf)}, + length{len} {} + +UVW_INLINE void details::connect_req::connect_callback(uv_connect_t *req, int status) { + if(auto ptr = reserve(req); status) { + ptr->publish(error_event{status}); + } else { + ptr->publish(connect_event{}); + } +} + +UVW_INLINE void details::shutdown_req::shoutdown_callback(uv_shutdown_t *req, int status) { + if(auto ptr = reserve(req); status) { + ptr->publish(error_event{status}); + } else { + ptr->publish(shutdown_event{}); + } +} + +UVW_INLINE int details::shutdown_req::shutdown(uv_stream_t *hndl) { + return this->leak_if(uv_shutdown(raw(), hndl, &shoutdown_callback)); +} + +} // namespace uvw diff --git a/src/uvw/tcp.cpp b/src/uvw/tcp.cpp index 37e98029..3d058fe0 100644 --- a/src/uvw/tcp.cpp +++ b/src/uvw/tcp.cpp @@ -1,86 +1,2 @@ -#ifdef UVW_AS_LIB -# include "tcp.h" -#endif - -#include "config.h" - -namespace uvw { - -UVW_INLINE tcp_handle::tcp_handle(loop::token token, std::shared_ptr ref, unsigned int f) - : stream_handle{token, std::move(ref)}, tag{f ? FLAGS : DEFAULT}, flags{f} {} - -UVW_INLINE int tcp_handle::init() { - if(tag == FLAGS) { - return leak_if(uv_tcp_init_ex(parent().raw(), raw(), flags)); - } else { - return leak_if(uv_tcp_init(parent().raw(), raw())); - } -} - -UVW_INLINE int tcp_handle::open(os_socket_handle socket) { - return uv_tcp_open(raw(), socket); -} - -UVW_INLINE bool tcp_handle::no_delay(bool value) { - return (0 == uv_tcp_nodelay(raw(), value)); -} - -UVW_INLINE bool tcp_handle::keep_alive(bool enable, tcp_handle::time val) { - return (0 == uv_tcp_keepalive(raw(), enable, val.count())); -} - -UVW_INLINE bool tcp_handle::simultaneous_accepts(bool enable) { - return (0 == uv_tcp_simultaneous_accepts(raw(), enable)); -} - -UVW_INLINE int tcp_handle::bind(const sockaddr &addr, tcp_flags opts) { - return uv_tcp_bind(raw(), &addr, static_cast(opts)); -} - -UVW_INLINE int tcp_handle::bind(const std::string &ip, unsigned int port, tcp_flags opts) { - return bind(details::ip_addr(ip.data(), port), opts); -} - -UVW_INLINE int tcp_handle::bind(socket_address addr, tcp_flags opts) { - return bind(addr.ip, addr.port, opts); -} - -UVW_INLINE socket_address tcp_handle::sock() const noexcept { - sockaddr_storage storage; - int len = sizeof(sockaddr_storage); - uv_tcp_getsockname(raw(), reinterpret_cast(&storage), &len); - return details::sock_addr(storage); -} - -UVW_INLINE socket_address tcp_handle::peer() const noexcept { - sockaddr_storage storage; - int len = sizeof(sockaddr_storage); - uv_tcp_getpeername(raw(), reinterpret_cast(&storage), &len); - return details::sock_addr(storage); -} - -UVW_INLINE int tcp_handle::connect(const std::string &ip, unsigned int port) { - return connect(details::ip_addr(ip.data(), port)); -} - -UVW_INLINE int tcp_handle::connect(socket_address addr) { - return connect(addr.ip, addr.port); -} - -UVW_INLINE int tcp_handle::connect(const sockaddr &addr) { - auto listener = [ptr = shared_from_this()](const auto &event, const auto &) { - ptr->publish(event); - }; - - auto req = parent().resource(); - req->on(listener); - req->on(listener); - - return req->connect(&uv_tcp_connect, raw(), &addr); -} - -UVW_INLINE int tcp_handle::close_reset() { - return uv_tcp_close_reset(raw(), &this->close_callback); -} - -} // namespace uvw +#include "tcp.h" +#include "tcp.ipp" diff --git a/src/uvw/tcp.h b/src/uvw/tcp.h index 36f12892..e1a251a5 100644 --- a/src/uvw/tcp.h +++ b/src/uvw/tcp.h @@ -227,7 +227,7 @@ private: } // namespace uvw #ifndef UVW_AS_LIB -# include "tcp.cpp" +# include "tcp.ipp" #endif #endif // UVW_TCP_INCLUDE_H diff --git a/src/uvw/tcp.ipp b/src/uvw/tcp.ipp new file mode 100644 index 00000000..dfaa9b59 --- /dev/null +++ b/src/uvw/tcp.ipp @@ -0,0 +1,82 @@ +#include "config.h" + +namespace uvw { + +UVW_INLINE tcp_handle::tcp_handle(loop::token token, std::shared_ptr ref, unsigned int f) + : stream_handle{token, std::move(ref)}, tag{f ? FLAGS : DEFAULT}, flags{f} {} + +UVW_INLINE int tcp_handle::init() { + if(tag == FLAGS) { + return leak_if(uv_tcp_init_ex(parent().raw(), raw(), flags)); + } else { + return leak_if(uv_tcp_init(parent().raw(), raw())); + } +} + +UVW_INLINE int tcp_handle::open(os_socket_handle socket) { + return uv_tcp_open(raw(), socket); +} + +UVW_INLINE bool tcp_handle::no_delay(bool value) { + return (0 == uv_tcp_nodelay(raw(), value)); +} + +UVW_INLINE bool tcp_handle::keep_alive(bool enable, tcp_handle::time val) { + return (0 == uv_tcp_keepalive(raw(), enable, val.count())); +} + +UVW_INLINE bool tcp_handle::simultaneous_accepts(bool enable) { + return (0 == uv_tcp_simultaneous_accepts(raw(), enable)); +} + +UVW_INLINE int tcp_handle::bind(const sockaddr &addr, tcp_flags opts) { + return uv_tcp_bind(raw(), &addr, static_cast(opts)); +} + +UVW_INLINE int tcp_handle::bind(const std::string &ip, unsigned int port, tcp_flags opts) { + return bind(details::ip_addr(ip.data(), port), opts); +} + +UVW_INLINE int tcp_handle::bind(socket_address addr, tcp_flags opts) { + return bind(addr.ip, addr.port, opts); +} + +UVW_INLINE socket_address tcp_handle::sock() const noexcept { + sockaddr_storage storage; + int len = sizeof(sockaddr_storage); + uv_tcp_getsockname(raw(), reinterpret_cast(&storage), &len); + return details::sock_addr(storage); +} + +UVW_INLINE socket_address tcp_handle::peer() const noexcept { + sockaddr_storage storage; + int len = sizeof(sockaddr_storage); + uv_tcp_getpeername(raw(), reinterpret_cast(&storage), &len); + return details::sock_addr(storage); +} + +UVW_INLINE int tcp_handle::connect(const std::string &ip, unsigned int port) { + return connect(details::ip_addr(ip.data(), port)); +} + +UVW_INLINE int tcp_handle::connect(socket_address addr) { + return connect(addr.ip, addr.port); +} + +UVW_INLINE int tcp_handle::connect(const sockaddr &addr) { + auto listener = [ptr = shared_from_this()](const auto &event, const auto &) { + ptr->publish(event); + }; + + auto req = parent().resource(); + req->on(listener); + req->on(listener); + + return req->connect(&uv_tcp_connect, raw(), &addr); +} + +UVW_INLINE int tcp_handle::close_reset() { + return uv_tcp_close_reset(raw(), &this->close_callback); +} + +} // namespace uvw diff --git a/src/uvw/thread.cpp b/src/uvw/thread.cpp index 994878fa..e5be6a96 100644 --- a/src/uvw/thread.cpp +++ b/src/uvw/thread.cpp @@ -1,189 +1,2 @@ -#ifdef UVW_AS_LIB -# include "thread.h" -#endif - -#include "config.h" - -namespace uvw { - -UVW_INLINE thread::thread(loop::token token, std::shared_ptr ref, task t, std::shared_ptr d) noexcept - : uv_type{token, std::move(ref)}, - data{std::move(d)}, - func{std::move(t)} {} - -UVW_INLINE void thread::create_callback(void *arg) { - thread &curr = *(static_cast(arg)); - curr.func(curr.data); -} - -UVW_INLINE thread::type thread::self() noexcept { - return uv_thread_self(); -} - -UVW_INLINE int thread::getcpu() noexcept { - return uv_thread_getcpu(); -} - -UVW_INLINE bool thread::equal(const thread &tl, const thread &tr) noexcept { - return !(0 == uv_thread_equal(tl.raw(), tr.raw())); -} - -UVW_INLINE bool thread::priority(const thread &tl, thread_priority val) noexcept { - return (uv_thread_setpriority(*tl.raw(), static_cast>(val)) == 0); -} - -UVW_INLINE std::pair thread::priority(const thread &tl) noexcept { - int prio{}; - const bool res = (uv_thread_getpriority(*tl.raw(), &prio) == 0); - return {res, thread_priority{static_cast>(prio)}}; -} - -UVW_INLINE thread::~thread() noexcept { - join(); -} - -UVW_INLINE bool thread::run() noexcept { - return (0 == uv_thread_create(raw(), &create_callback, this)); -} - -UVW_INLINE bool thread::run(create_flags opts, std::size_t stack) noexcept { - uv_thread_options_t params{static_cast(opts), stack}; - return (0 == uv_thread_create_ex(raw(), ¶ms, &create_callback, this)); -} - -UVW_INLINE bool thread::join() noexcept { - return (0 == uv_thread_join(raw())); -} - -UVW_INLINE thread_local_storage::thread_local_storage(loop::token token, std::shared_ptr ref) noexcept - : uv_type{token, std::move(ref)} { - uv_key_create(uv_type::raw()); -} - -UVW_INLINE thread_local_storage::~thread_local_storage() noexcept { - uv_key_delete(uv_type::raw()); -} - -UVW_INLINE uv_once_t *once::guard() noexcept { - static uv_once_t once = UV_ONCE_INIT; - return &once; -} - -UVW_INLINE mutex::mutex(loop::token token, std::shared_ptr ref, bool recursive) noexcept - : uv_type{token, std::move(ref)} { - if(recursive) { - uv_mutex_init_recursive(raw()); - } else { - uv_mutex_init(raw()); - } -} - -UVW_INLINE mutex::~mutex() noexcept { - uv_mutex_destroy(raw()); -} - -UVW_INLINE void mutex::lock() noexcept { - uv_mutex_lock(raw()); -} - -UVW_INLINE bool mutex::try_lock() noexcept { - return (0 == uv_mutex_trylock(raw())); -} - -UVW_INLINE void mutex::unlock() noexcept { - uv_mutex_unlock(raw()); -} - -UVW_INLINE rwlock::rwlock(loop::token token, std::shared_ptr ref) noexcept - : uv_type{token, std::move(ref)} { - uv_rwlock_init(raw()); -} - -UVW_INLINE rwlock::~rwlock() noexcept { - uv_rwlock_destroy(raw()); -} - -UVW_INLINE void rwlock::rdlock() noexcept { - uv_rwlock_rdlock(raw()); -} - -UVW_INLINE bool rwlock::try_rdlock() noexcept { - return (0 == uv_rwlock_tryrdlock(raw())); -} - -UVW_INLINE void rwlock::rdunlock() noexcept { - uv_rwlock_rdunlock(raw()); -} - -UVW_INLINE void rwlock::wrlock() noexcept { - uv_rwlock_wrlock(raw()); -} - -UVW_INLINE bool rwlock::try_wrlock() noexcept { - return (0 == uv_rwlock_trywrlock(raw())); -} - -UVW_INLINE void rwlock::wrunlock() noexcept { - uv_rwlock_wrunlock(raw()); -} - -UVW_INLINE semaphore::semaphore(loop::token token, std::shared_ptr ref, unsigned int value) noexcept - : uv_type{token, std::move(ref)} { - uv_sem_init(raw(), value); -} - -UVW_INLINE semaphore::~semaphore() noexcept { - uv_sem_destroy(raw()); -} - -UVW_INLINE void semaphore::post() noexcept { - uv_sem_post(raw()); -} - -UVW_INLINE void semaphore::wait() noexcept { - uv_sem_wait(raw()); -} - -UVW_INLINE bool semaphore::try_wait() noexcept { - return (0 == uv_sem_trywait(raw())); -} - -UVW_INLINE condition::condition(loop::token token, std::shared_ptr ref) noexcept - : uv_type{token, std::move(ref)} { - uv_cond_init(raw()); -} - -UVW_INLINE condition::~condition() noexcept { - uv_cond_destroy(raw()); -} - -UVW_INLINE void condition::signal() noexcept { - uv_cond_signal(raw()); -} - -UVW_INLINE void condition::broadcast() noexcept { - uv_cond_broadcast(raw()); -} - -UVW_INLINE void condition::wait(mutex &mtx) noexcept { - uv_cond_wait(raw(), mtx.raw()); -} - -UVW_INLINE bool condition::timed_wait(mutex &mtx, uint64_t timeout) noexcept { - return (0 == uv_cond_timedwait(raw(), mtx.raw(), timeout)); -} - -UVW_INLINE barrier::barrier(loop::token token, std::shared_ptr ref, unsigned int count) noexcept - : uv_type{token, std::move(ref)} { - uv_barrier_init(raw(), count); -} - -UVW_INLINE barrier::~barrier() noexcept { - uv_barrier_destroy(raw()); -} - -UVW_INLINE bool barrier::wait() noexcept { - return (0 == uv_barrier_wait(raw())); -} - -} // namespace uvw +#include "thread.h" +#include "thread.ipp" diff --git a/src/uvw/thread.h b/src/uvw/thread.h index 16192a6f..3483ad75 100644 --- a/src/uvw/thread.h +++ b/src/uvw/thread.h @@ -381,7 +381,7 @@ public: } // namespace uvw #ifndef UVW_AS_LIB -# include "thread.cpp" +# include "thread.ipp" #endif #endif // UVW_THREAD_INCLUDE_H diff --git a/src/uvw/thread.ipp b/src/uvw/thread.ipp new file mode 100644 index 00000000..443df1ba --- /dev/null +++ b/src/uvw/thread.ipp @@ -0,0 +1,185 @@ +#include "config.h" + +namespace uvw { + +UVW_INLINE thread::thread(loop::token token, std::shared_ptr ref, task t, std::shared_ptr d) noexcept + : uv_type{token, std::move(ref)}, + data{std::move(d)}, + func{std::move(t)} {} + +UVW_INLINE void thread::create_callback(void *arg) { + thread &curr = *(static_cast(arg)); + curr.func(curr.data); +} + +UVW_INLINE thread::type thread::self() noexcept { + return uv_thread_self(); +} + +UVW_INLINE int thread::getcpu() noexcept { + return uv_thread_getcpu(); +} + +UVW_INLINE bool thread::equal(const thread &tl, const thread &tr) noexcept { + return !(0 == uv_thread_equal(tl.raw(), tr.raw())); +} + +UVW_INLINE bool thread::priority(const thread &tl, thread_priority val) noexcept { + return (uv_thread_setpriority(*tl.raw(), static_cast>(val)) == 0); +} + +UVW_INLINE std::pair thread::priority(const thread &tl) noexcept { + int prio{}; + const bool res = (uv_thread_getpriority(*tl.raw(), &prio) == 0); + return {res, thread_priority{static_cast>(prio)}}; +} + +UVW_INLINE thread::~thread() noexcept { + join(); +} + +UVW_INLINE bool thread::run() noexcept { + return (0 == uv_thread_create(raw(), &create_callback, this)); +} + +UVW_INLINE bool thread::run(create_flags opts, std::size_t stack) noexcept { + uv_thread_options_t params{static_cast(opts), stack}; + return (0 == uv_thread_create_ex(raw(), ¶ms, &create_callback, this)); +} + +UVW_INLINE bool thread::join() noexcept { + return (0 == uv_thread_join(raw())); +} + +UVW_INLINE thread_local_storage::thread_local_storage(loop::token token, std::shared_ptr ref) noexcept + : uv_type{token, std::move(ref)} { + uv_key_create(uv_type::raw()); +} + +UVW_INLINE thread_local_storage::~thread_local_storage() noexcept { + uv_key_delete(uv_type::raw()); +} + +UVW_INLINE uv_once_t *once::guard() noexcept { + static uv_once_t once = UV_ONCE_INIT; + return &once; +} + +UVW_INLINE mutex::mutex(loop::token token, std::shared_ptr ref, bool recursive) noexcept + : uv_type{token, std::move(ref)} { + if(recursive) { + uv_mutex_init_recursive(raw()); + } else { + uv_mutex_init(raw()); + } +} + +UVW_INLINE mutex::~mutex() noexcept { + uv_mutex_destroy(raw()); +} + +UVW_INLINE void mutex::lock() noexcept { + uv_mutex_lock(raw()); +} + +UVW_INLINE bool mutex::try_lock() noexcept { + return (0 == uv_mutex_trylock(raw())); +} + +UVW_INLINE void mutex::unlock() noexcept { + uv_mutex_unlock(raw()); +} + +UVW_INLINE rwlock::rwlock(loop::token token, std::shared_ptr ref) noexcept + : uv_type{token, std::move(ref)} { + uv_rwlock_init(raw()); +} + +UVW_INLINE rwlock::~rwlock() noexcept { + uv_rwlock_destroy(raw()); +} + +UVW_INLINE void rwlock::rdlock() noexcept { + uv_rwlock_rdlock(raw()); +} + +UVW_INLINE bool rwlock::try_rdlock() noexcept { + return (0 == uv_rwlock_tryrdlock(raw())); +} + +UVW_INLINE void rwlock::rdunlock() noexcept { + uv_rwlock_rdunlock(raw()); +} + +UVW_INLINE void rwlock::wrlock() noexcept { + uv_rwlock_wrlock(raw()); +} + +UVW_INLINE bool rwlock::try_wrlock() noexcept { + return (0 == uv_rwlock_trywrlock(raw())); +} + +UVW_INLINE void rwlock::wrunlock() noexcept { + uv_rwlock_wrunlock(raw()); +} + +UVW_INLINE semaphore::semaphore(loop::token token, std::shared_ptr ref, unsigned int value) noexcept + : uv_type{token, std::move(ref)} { + uv_sem_init(raw(), value); +} + +UVW_INLINE semaphore::~semaphore() noexcept { + uv_sem_destroy(raw()); +} + +UVW_INLINE void semaphore::post() noexcept { + uv_sem_post(raw()); +} + +UVW_INLINE void semaphore::wait() noexcept { + uv_sem_wait(raw()); +} + +UVW_INLINE bool semaphore::try_wait() noexcept { + return (0 == uv_sem_trywait(raw())); +} + +UVW_INLINE condition::condition(loop::token token, std::shared_ptr ref) noexcept + : uv_type{token, std::move(ref)} { + uv_cond_init(raw()); +} + +UVW_INLINE condition::~condition() noexcept { + uv_cond_destroy(raw()); +} + +UVW_INLINE void condition::signal() noexcept { + uv_cond_signal(raw()); +} + +UVW_INLINE void condition::broadcast() noexcept { + uv_cond_broadcast(raw()); +} + +UVW_INLINE void condition::wait(mutex &mtx) noexcept { + uv_cond_wait(raw(), mtx.raw()); +} + +UVW_INLINE bool condition::timed_wait(mutex &mtx, uint64_t timeout) noexcept { + return (0 == uv_cond_timedwait(raw(), mtx.raw(), timeout)); +} + +UVW_INLINE barrier::barrier(loop::token token, std::shared_ptr ref, unsigned int count) noexcept + : uv_type{token, std::move(ref)} { + uv_barrier_init(raw(), count); +} + +UVW_INLINE barrier::~barrier() noexcept { + uv_barrier_destroy(raw()); +} + +UVW_INLINE bool barrier::wait() noexcept { + return (0 == uv_barrier_wait(raw())); +} + +} // namespace uvw diff --git a/src/uvw/timer.cpp b/src/uvw/timer.cpp index 6db6dec6..90f27828 100644 --- a/src/uvw/timer.cpp +++ b/src/uvw/timer.cpp @@ -1,42 +1,2 @@ -#ifdef UVW_AS_LIB -# include "timer.h" -#endif - -#include "config.h" - -namespace uvw { - -UVW_INLINE void timer_handle::start_callback(uv_timer_t *hndl) { - timer_handle &timer = *(static_cast(hndl->data)); - timer.publish(timer_event{}); -} - -UVW_INLINE int timer_handle::init() { - return leak_if(uv_timer_init(parent().raw(), raw())); -} - -UVW_INLINE int timer_handle::start(timer_handle::time timeout, timer_handle::time repeat) { - return uv_timer_start(raw(), &start_callback, timeout.count(), repeat.count()); -} - -UVW_INLINE int timer_handle::stop() { - return uv_timer_stop(raw()); -} - -UVW_INLINE int timer_handle::again() { - return uv_timer_again(raw()); -} - -UVW_INLINE void timer_handle::repeat(timer_handle::time repeat) { - uv_timer_set_repeat(raw(), repeat.count()); -} - -UVW_INLINE timer_handle::time timer_handle::repeat() { - return time{uv_timer_get_repeat(raw())}; -} - -UVW_INLINE timer_handle::time timer_handle::due_in() { - return time{uv_timer_get_due_in(raw())}; -} - -} // namespace uvw +#include "timer.h" +#include "timer.ipp" diff --git a/src/uvw/timer.h b/src/uvw/timer.h index d95814a9..98e9546a 100644 --- a/src/uvw/timer.h +++ b/src/uvw/timer.h @@ -1,8 +1,8 @@ #ifndef UVW_TIMER_INCLUDE_H #define UVW_TIMER_INCLUDE_H -#include #include +#include #include #include "handle.hpp" #include "loop.h" @@ -105,7 +105,7 @@ public: } // namespace uvw #ifndef UVW_AS_LIB -# include "timer.cpp" +# include "timer.ipp" #endif #endif // UVW_TIMER_INCLUDE_H diff --git a/src/uvw/timer.ipp b/src/uvw/timer.ipp new file mode 100644 index 00000000..c91f4fe3 --- /dev/null +++ b/src/uvw/timer.ipp @@ -0,0 +1,38 @@ +#include "config.h" + +namespace uvw { + +UVW_INLINE void timer_handle::start_callback(uv_timer_t *hndl) { + timer_handle &timer = *(static_cast(hndl->data)); + timer.publish(timer_event{}); +} + +UVW_INLINE int timer_handle::init() { + return leak_if(uv_timer_init(parent().raw(), raw())); +} + +UVW_INLINE int timer_handle::start(timer_handle::time timeout, timer_handle::time repeat) { + return uv_timer_start(raw(), &start_callback, timeout.count(), repeat.count()); +} + +UVW_INLINE int timer_handle::stop() { + return uv_timer_stop(raw()); +} + +UVW_INLINE int timer_handle::again() { + return uv_timer_again(raw()); +} + +UVW_INLINE void timer_handle::repeat(timer_handle::time repeat) { + uv_timer_set_repeat(raw(), repeat.count()); +} + +UVW_INLINE timer_handle::time timer_handle::repeat() { + return time{uv_timer_get_repeat(raw())}; +} + +UVW_INLINE timer_handle::time timer_handle::due_in() { + return time{uv_timer_get_due_in(raw())}; +} + +} // namespace uvw diff --git a/src/uvw/tty.cpp b/src/uvw/tty.cpp index 32a8859c..06bb865b 100644 --- a/src/uvw/tty.cpp +++ b/src/uvw/tty.cpp @@ -1,67 +1,2 @@ -#ifdef UVW_AS_LIB -# include "tty.h" -#endif - -#include -#include "config.h" - -namespace uvw { - -UVW_INLINE details::reset_mode_memo::~reset_mode_memo() { - uv_tty_reset_mode(); -} - -UVW_INLINE tty_handle::tty_handle(loop::token token, std::shared_ptr ref, file_handle desc, bool readable) - : stream_handle{token, std::move(ref)}, - memo{mode_memo_handler()}, - fd{desc}, - rw{readable} {} - -UVW_INLINE std::shared_ptr tty_handle::mode_memo_handler() { - static std::weak_ptr weak; - auto shared = weak.lock(); - if(!shared) { weak = shared = std::make_shared(); } - return shared; -} - -UVW_INLINE int tty_handle::init() { - return leak_if(uv_tty_init(parent().raw(), raw(), fd, rw)); -} - -UVW_INLINE bool tty_handle::mode(tty_handle::tty_mode m) { - return (0 == uv_tty_set_mode(raw(), static_cast(m))); -} - -UVW_INLINE bool tty_handle::reset_mode() noexcept { - return (0 == uv_tty_reset_mode()); -} - -UVW_INLINE win_size tty_handle::get_win_size() { - win_size size; - - if(0 != uv_tty_get_winsize(raw(), &size.width, &size.height)) { - size.width = -1; - size.height = -1; - } - - return size; -} - -UVW_INLINE void tty_handle::vterm_state(tty_handle::tty_vtermstate s) const noexcept { - switch(s) { - case tty_vtermstate::SUPPORTED: - uv_tty_set_vterm_state(uv_tty_vtermstate_t::UV_TTY_SUPPORTED); - break; - case tty_vtermstate::UNSUPPORTED: - uv_tty_set_vterm_state(uv_tty_vtermstate_t::UV_TTY_UNSUPPORTED); - break; - } -} - -UVW_INLINE tty_handle::tty_vtermstate tty_handle::vterm_state() const noexcept { - uv_tty_vtermstate_t state; - uv_tty_get_vterm_state(&state); - return tty_vtermstate{state}; -} - -} // namespace uvw +#include "tty.h" +#include "tty.ipp" diff --git a/src/uvw/tty.h b/src/uvw/tty.h index a30a4a6d..77001319 100644 --- a/src/uvw/tty.h +++ b/src/uvw/tty.h @@ -140,7 +140,7 @@ private: } // namespace uvw #ifndef UVW_AS_LIB -# include "tty.cpp" +# include "tty.ipp" #endif #endif // UVW_TTY_INCLUDE_H diff --git a/src/uvw/tty.ipp b/src/uvw/tty.ipp new file mode 100644 index 00000000..21ca255a --- /dev/null +++ b/src/uvw/tty.ipp @@ -0,0 +1,65 @@ +#include +#include "config.h" + +namespace uvw { + +UVW_INLINE details::reset_mode_memo::~reset_mode_memo() { + uv_tty_reset_mode(); +} + +UVW_INLINE tty_handle::tty_handle(loop::token token, std::shared_ptr ref, file_handle desc, bool readable) + : stream_handle{token, std::move(ref)}, + memo{mode_memo_handler()}, + fd{desc}, + rw{readable} {} + +UVW_INLINE std::shared_ptr tty_handle::mode_memo_handler() { + static std::weak_ptr weak; + auto shared = weak.lock(); + if(!shared) { + weak = shared = std::make_shared(); + } + return shared; +} + +UVW_INLINE int tty_handle::init() { + return leak_if(uv_tty_init(parent().raw(), raw(), fd, rw)); +} + +UVW_INLINE bool tty_handle::mode(tty_handle::tty_mode m) { + return (0 == uv_tty_set_mode(raw(), static_cast(m))); +} + +UVW_INLINE bool tty_handle::reset_mode() noexcept { + return (0 == uv_tty_reset_mode()); +} + +UVW_INLINE win_size tty_handle::get_win_size() { + win_size size; + + if(0 != uv_tty_get_winsize(raw(), &size.width, &size.height)) { + size.width = -1; + size.height = -1; + } + + return size; +} + +UVW_INLINE void tty_handle::vterm_state(tty_handle::tty_vtermstate s) const noexcept { + switch(s) { + case tty_vtermstate::SUPPORTED: + uv_tty_set_vterm_state(uv_tty_vtermstate_t::UV_TTY_SUPPORTED); + break; + case tty_vtermstate::UNSUPPORTED: + uv_tty_set_vterm_state(uv_tty_vtermstate_t::UV_TTY_UNSUPPORTED); + break; + } +} + +UVW_INLINE tty_handle::tty_vtermstate tty_handle::vterm_state() const noexcept { + uv_tty_vtermstate_t state; + uv_tty_get_vterm_state(&state); + return tty_vtermstate{state}; +} + +} // namespace uvw diff --git a/src/uvw/udp.cpp b/src/uvw/udp.cpp index fa48bbee..89a1cc90 100644 --- a/src/uvw/udp.cpp +++ b/src/uvw/udp.cpp @@ -1,216 +1,2 @@ -#ifdef UVW_AS_LIB -# include "udp.h" -#endif - -#include "config.h" - -namespace uvw { - -UVW_INLINE udp_data_event::udp_data_event(socket_address sndr, std::unique_ptr buf, std::size_t len, bool part) noexcept - : data{std::move(buf)}, - length{len}, - sender{std::move(sndr)}, - partial{part} {} - -UVW_INLINE void details::send_req::udp_send_callback(uv_udp_send_t *req, int status) { - if(auto ptr = reserve(req); status) { - ptr->publish(error_event{status}); - } else { - ptr->publish(send_event{}); - } -} - -UVW_INLINE details::send_req::send_req(loop::token token, std::shared_ptr parent, std::unique_ptr dt, unsigned int len) - : request{token, std::move(parent)}, - data{std::move(dt)}, - buf{uv_buf_init(data.get(), len)} {} - -UVW_INLINE int details::send_req::send(uv_udp_t *hndl, const struct sockaddr *addr) { - return this->leak_if(uv_udp_send(raw(), hndl, &buf, 1, addr, &udp_send_callback)); -} - -UVW_INLINE void udp_handle::recv_callback(uv_udp_t *hndl, ssize_t nread, const uv_buf_t *buf, const sockaddr *addr, unsigned flags) { - udp_handle &udp = *(static_cast(hndl->data)); - // data will be destroyed no matter of what the value of nread is - std::unique_ptr data{buf->base}; - - if(nread > 0) { - // data available (can be truncated) - udp.publish(udp_data_event{details::sock_addr(*addr), std::move(data), static_cast(nread), !(0 == (flags & UV_UDP_PARTIAL))}); - } else if(nread == 0 && addr == nullptr) { - // no more data to be read, doing nothing is fine - } else if(nread == 0 && addr != nullptr) { - // empty udp packet - udp.publish(udp_data_event{details::sock_addr(*addr), std::move(data), static_cast(nread), false}); - } else { - // transmission error - udp.publish(error_event(nread)); - } -} - -UVW_INLINE udp_handle::udp_handle(loop::token token, std::shared_ptr ref, unsigned int f) - : handle{token, std::move(ref)}, tag{FLAGS}, flags{f} {} - -UVW_INLINE int udp_handle::init() { - if(tag == FLAGS) { - return leak_if(uv_udp_init_ex(parent().raw(), raw(), flags)); - } else { - return leak_if(uv_udp_init(parent().raw(), raw())); - } -} - -UVW_INLINE int udp_handle::open(os_socket_handle socket) { - return uv_udp_open(raw(), socket); -} - -UVW_INLINE int udp_handle::connect(const sockaddr &addr) { - return uv_udp_connect(raw(), &addr); -} - -UVW_INLINE int udp_handle::connect(const std::string &ip, unsigned int port) { - return connect(details::ip_addr(ip.data(), port)); -} - -UVW_INLINE int udp_handle::connect(socket_address addr) { - return connect(addr.ip, addr.port); -} - -UVW_INLINE int udp_handle::disconnect() { - return uv_udp_connect(raw(), nullptr); -} - -UVW_INLINE socket_address udp_handle::peer() const noexcept { - sockaddr_storage storage; - int len = sizeof(sockaddr_storage); - uv_udp_getpeername(raw(), reinterpret_cast(&storage), &len); - return details::sock_addr(storage); -} - -UVW_INLINE int udp_handle::bind(const sockaddr &addr, udp_handle::udp_flags opts) { - return uv_udp_bind(raw(), &addr, static_cast(opts)); -} - -UVW_INLINE int udp_handle::bind(const std::string &ip, unsigned int port, udp_flags opts) { - return bind(details::ip_addr(ip.data(), port), opts); -} - -UVW_INLINE int udp_handle::bind(socket_address addr, udp_flags opts) { - return bind(addr.ip, addr.port, opts); -} - -UVW_INLINE socket_address udp_handle::sock() const noexcept { - sockaddr_storage storage; - int len = sizeof(sockaddr_storage); - uv_udp_getsockname(raw(), reinterpret_cast(&storage), &len); - return details::sock_addr(storage); -} - -UVW_INLINE bool udp_handle::multicast_membership(const std::string &multicast, const std::string &iface, membership ms) { - return (0 == uv_udp_set_membership(raw(), multicast.data(), iface.data(), static_cast(ms))); -} - -UVW_INLINE bool udp_handle::multicast_loop(bool enable) { - return (0 == uv_udp_set_multicast_loop(raw(), enable)); -} - -UVW_INLINE bool udp_handle::multicast_ttl(int val) { - return (0 == uv_udp_set_multicast_ttl(raw(), val > 255 ? 255 : val)); -} - -UVW_INLINE bool udp_handle::multicast_interface(const std::string &iface) { - return (0 == uv_udp_set_multicast_interface(raw(), iface.data())); -} - -UVW_INLINE bool udp_handle::broadcast(bool enable) { - return (0 == uv_udp_set_broadcast(raw(), enable)); -} - -UVW_INLINE bool udp_handle::ttl(int val) { - return (0 == uv_udp_set_ttl(raw(), val > 255 ? 255 : val)); -} - -UVW_INLINE int udp_handle::send(const sockaddr &addr, std::unique_ptr data, unsigned int len) { - auto req = parent().resource(std::unique_ptr{data.release(), [](char *ptr) { delete[] ptr; }}, len); - - auto listener = [ptr = shared_from_this()](const auto &event, const auto &) { - ptr->publish(event); - }; - - req->on(listener); - req->on(listener); - - return req->send(raw(), &addr); -} - -UVW_INLINE int udp_handle::send(const std::string &ip, unsigned int port, std::unique_ptr data, unsigned int len) { - return send(details::ip_addr(ip.data(), port), std::move(data), len); -} - -UVW_INLINE int udp_handle::send(socket_address addr, std::unique_ptr data, unsigned int len) { - return send(addr.ip, addr.port, std::move(data), len); -} - -UVW_INLINE int udp_handle::send(const sockaddr &addr, char *data, unsigned int len) { - auto req = parent().resource(std::unique_ptr{data, [](char *) {}}, len); - - auto listener = [ptr = shared_from_this()](const auto &event, const auto &) { - ptr->publish(event); - }; - - req->on(listener); - req->on(listener); - - return req->send(raw(), &addr); -} - -UVW_INLINE int udp_handle::send(const std::string &ip, unsigned int port, char *data, unsigned int len) { - return send(details::ip_addr(ip.data(), port), data, len); -} - -UVW_INLINE int udp_handle::send(socket_address addr, char *data, unsigned int len) { - return send(addr.ip, addr.port, data, len); -} - -UVW_INLINE int udp_handle::try_send(const sockaddr &addr, std::unique_ptr data, unsigned int len) { - uv_buf_t bufs[] = {uv_buf_init(data.get(), len)}; - return uv_udp_try_send(raw(), bufs, 1, &addr); -} - -UVW_INLINE int udp_handle::try_send(const std::string &ip, unsigned int port, std::unique_ptr data, unsigned int len) { - return try_send(details::ip_addr(ip.data(), port), std::move(data), len); -} - -UVW_INLINE int udp_handle::try_send(socket_address addr, std::unique_ptr data, unsigned int len) { - return try_send(addr.ip, addr.port, std::move(data), len); -} - -UVW_INLINE int udp_handle::try_send(const sockaddr &addr, char *data, unsigned int len) { - uv_buf_t bufs[] = {uv_buf_init(data, len)}; - return uv_udp_try_send(raw(), bufs, 1, &addr); -} - -UVW_INLINE int udp_handle::try_send(const std::string &ip, unsigned int port, char *data, unsigned int len) { - return try_send(details::ip_addr(ip.data(), port), data, len); -} - -UVW_INLINE int udp_handle::try_send(socket_address addr, char *data, unsigned int len) { - return try_send(addr.ip, addr.port, data, len); -} - -UVW_INLINE int udp_handle::recv() { - return uv_udp_recv_start(raw(), &details::common_alloc_callback, &recv_callback); -} - -UVW_INLINE int udp_handle::stop() { - return uv_udp_recv_stop(raw()); -} - -UVW_INLINE size_t udp_handle::send_queue_size() const noexcept { - return uv_udp_get_send_queue_size(raw()); -} - -UVW_INLINE size_t udp_handle::send_queue_count() const noexcept { - return uv_udp_get_send_queue_count(raw()); -} - -} // namespace uvw +#include "udp.h" +#include "udp.ipp" diff --git a/src/uvw/udp.h b/src/uvw/udp.h index a5568ce2..1f975cbd 100644 --- a/src/uvw/udp.h +++ b/src/uvw/udp.h @@ -556,7 +556,7 @@ private: } // namespace uvw #ifndef UVW_AS_LIB -# include "udp.cpp" +# include "udp.ipp" #endif #endif // UVW_UDP_INCLUDE_H diff --git a/src/uvw/udp.ipp b/src/uvw/udp.ipp new file mode 100644 index 00000000..633f1326 --- /dev/null +++ b/src/uvw/udp.ipp @@ -0,0 +1,212 @@ +#include "config.h" + +namespace uvw { + +UVW_INLINE udp_data_event::udp_data_event(socket_address sndr, std::unique_ptr buf, std::size_t len, bool part) noexcept + : data{std::move(buf)}, + length{len}, + sender{std::move(sndr)}, + partial{part} {} + +UVW_INLINE void details::send_req::udp_send_callback(uv_udp_send_t *req, int status) { + if(auto ptr = reserve(req); status) { + ptr->publish(error_event{status}); + } else { + ptr->publish(send_event{}); + } +} + +UVW_INLINE details::send_req::send_req(loop::token token, std::shared_ptr parent, std::unique_ptr dt, unsigned int len) + : request{token, std::move(parent)}, + data{std::move(dt)}, + buf{uv_buf_init(data.get(), len)} {} + +UVW_INLINE int details::send_req::send(uv_udp_t *hndl, const struct sockaddr *addr) { + return this->leak_if(uv_udp_send(raw(), hndl, &buf, 1, addr, &udp_send_callback)); +} + +UVW_INLINE void udp_handle::recv_callback(uv_udp_t *hndl, ssize_t nread, const uv_buf_t *buf, const sockaddr *addr, unsigned flags) { + udp_handle &udp = *(static_cast(hndl->data)); + // data will be destroyed no matter of what the value of nread is + std::unique_ptr data{buf->base}; + + if(nread > 0) { + // data available (can be truncated) + udp.publish(udp_data_event{details::sock_addr(*addr), std::move(data), static_cast(nread), !(0 == (flags & UV_UDP_PARTIAL))}); + } else if(nread == 0 && addr == nullptr) { + // no more data to be read, doing nothing is fine + } else if(nread == 0 && addr != nullptr) { + // empty udp packet + udp.publish(udp_data_event{details::sock_addr(*addr), std::move(data), static_cast(nread), false}); + } else { + // transmission error + udp.publish(error_event(nread)); + } +} + +UVW_INLINE udp_handle::udp_handle(loop::token token, std::shared_ptr ref, unsigned int f) + : handle{token, std::move(ref)}, tag{FLAGS}, flags{f} {} + +UVW_INLINE int udp_handle::init() { + if(tag == FLAGS) { + return leak_if(uv_udp_init_ex(parent().raw(), raw(), flags)); + } else { + return leak_if(uv_udp_init(parent().raw(), raw())); + } +} + +UVW_INLINE int udp_handle::open(os_socket_handle socket) { + return uv_udp_open(raw(), socket); +} + +UVW_INLINE int udp_handle::connect(const sockaddr &addr) { + return uv_udp_connect(raw(), &addr); +} + +UVW_INLINE int udp_handle::connect(const std::string &ip, unsigned int port) { + return connect(details::ip_addr(ip.data(), port)); +} + +UVW_INLINE int udp_handle::connect(socket_address addr) { + return connect(addr.ip, addr.port); +} + +UVW_INLINE int udp_handle::disconnect() { + return uv_udp_connect(raw(), nullptr); +} + +UVW_INLINE socket_address udp_handle::peer() const noexcept { + sockaddr_storage storage; + int len = sizeof(sockaddr_storage); + uv_udp_getpeername(raw(), reinterpret_cast(&storage), &len); + return details::sock_addr(storage); +} + +UVW_INLINE int udp_handle::bind(const sockaddr &addr, udp_handle::udp_flags opts) { + return uv_udp_bind(raw(), &addr, static_cast(opts)); +} + +UVW_INLINE int udp_handle::bind(const std::string &ip, unsigned int port, udp_flags opts) { + return bind(details::ip_addr(ip.data(), port), opts); +} + +UVW_INLINE int udp_handle::bind(socket_address addr, udp_flags opts) { + return bind(addr.ip, addr.port, opts); +} + +UVW_INLINE socket_address udp_handle::sock() const noexcept { + sockaddr_storage storage; + int len = sizeof(sockaddr_storage); + uv_udp_getsockname(raw(), reinterpret_cast(&storage), &len); + return details::sock_addr(storage); +} + +UVW_INLINE bool udp_handle::multicast_membership(const std::string &multicast, const std::string &iface, membership ms) { + return (0 == uv_udp_set_membership(raw(), multicast.data(), iface.data(), static_cast(ms))); +} + +UVW_INLINE bool udp_handle::multicast_loop(bool enable) { + return (0 == uv_udp_set_multicast_loop(raw(), enable)); +} + +UVW_INLINE bool udp_handle::multicast_ttl(int val) { + return (0 == uv_udp_set_multicast_ttl(raw(), val > 255 ? 255 : val)); +} + +UVW_INLINE bool udp_handle::multicast_interface(const std::string &iface) { + return (0 == uv_udp_set_multicast_interface(raw(), iface.data())); +} + +UVW_INLINE bool udp_handle::broadcast(bool enable) { + return (0 == uv_udp_set_broadcast(raw(), enable)); +} + +UVW_INLINE bool udp_handle::ttl(int val) { + return (0 == uv_udp_set_ttl(raw(), val > 255 ? 255 : val)); +} + +UVW_INLINE int udp_handle::send(const sockaddr &addr, std::unique_ptr data, unsigned int len) { + auto req = parent().resource(std::unique_ptr{data.release(), [](char *ptr) { delete[] ptr; }}, len); + + auto listener = [ptr = shared_from_this()](const auto &event, const auto &) { + ptr->publish(event); + }; + + req->on(listener); + req->on(listener); + + return req->send(raw(), &addr); +} + +UVW_INLINE int udp_handle::send(const std::string &ip, unsigned int port, std::unique_ptr data, unsigned int len) { + return send(details::ip_addr(ip.data(), port), std::move(data), len); +} + +UVW_INLINE int udp_handle::send(socket_address addr, std::unique_ptr data, unsigned int len) { + return send(addr.ip, addr.port, std::move(data), len); +} + +UVW_INLINE int udp_handle::send(const sockaddr &addr, char *data, unsigned int len) { + auto req = parent().resource(std::unique_ptr{data, [](char *) {}}, len); + + auto listener = [ptr = shared_from_this()](const auto &event, const auto &) { + ptr->publish(event); + }; + + req->on(listener); + req->on(listener); + + return req->send(raw(), &addr); +} + +UVW_INLINE int udp_handle::send(const std::string &ip, unsigned int port, char *data, unsigned int len) { + return send(details::ip_addr(ip.data(), port), data, len); +} + +UVW_INLINE int udp_handle::send(socket_address addr, char *data, unsigned int len) { + return send(addr.ip, addr.port, data, len); +} + +UVW_INLINE int udp_handle::try_send(const sockaddr &addr, std::unique_ptr data, unsigned int len) { + uv_buf_t bufs[] = {uv_buf_init(data.get(), len)}; + return uv_udp_try_send(raw(), bufs, 1, &addr); +} + +UVW_INLINE int udp_handle::try_send(const std::string &ip, unsigned int port, std::unique_ptr data, unsigned int len) { + return try_send(details::ip_addr(ip.data(), port), std::move(data), len); +} + +UVW_INLINE int udp_handle::try_send(socket_address addr, std::unique_ptr data, unsigned int len) { + return try_send(addr.ip, addr.port, std::move(data), len); +} + +UVW_INLINE int udp_handle::try_send(const sockaddr &addr, char *data, unsigned int len) { + uv_buf_t bufs[] = {uv_buf_init(data, len)}; + return uv_udp_try_send(raw(), bufs, 1, &addr); +} + +UVW_INLINE int udp_handle::try_send(const std::string &ip, unsigned int port, char *data, unsigned int len) { + return try_send(details::ip_addr(ip.data(), port), data, len); +} + +UVW_INLINE int udp_handle::try_send(socket_address addr, char *data, unsigned int len) { + return try_send(addr.ip, addr.port, data, len); +} + +UVW_INLINE int udp_handle::recv() { + return uv_udp_recv_start(raw(), &details::common_alloc_callback, &recv_callback); +} + +UVW_INLINE int udp_handle::stop() { + return uv_udp_recv_stop(raw()); +} + +UVW_INLINE size_t udp_handle::send_queue_size() const noexcept { + return uv_udp_get_send_queue_size(raw()); +} + +UVW_INLINE size_t udp_handle::send_queue_count() const noexcept { + return uv_udp_get_send_queue_count(raw()); +} + +} // namespace uvw diff --git a/src/uvw/util.cpp b/src/uvw/util.cpp index 6d90b8d2..64b66e1c 100644 --- a/src/uvw/util.cpp +++ b/src/uvw/util.cpp @@ -1,374 +1,2 @@ -#ifdef UVW_AS_LIB -# include "util.h" -#endif - -#include "config.h" - -namespace uvw { - -UVW_INLINE passwd_info::passwd_info(std::shared_ptr pwd) - : value{pwd} {} - -UVW_INLINE std::string passwd_info::username() const noexcept { - return ((value && value->username) ? value->username : ""); -} - -UVW_INLINE decltype(uv_passwd_t::uid) passwd_info::uid() const noexcept { - return (value ? value->uid : decltype(uv_passwd_t::uid){}); -} - -UVW_INLINE decltype(uv_passwd_t::gid) passwd_info::gid() const noexcept { - return (value ? value->gid : decltype(uv_passwd_t::gid){}); -} - -UVW_INLINE std::string passwd_info::shell() const noexcept { - return ((value && value->shell) ? value->shell : ""); -} - -UVW_INLINE std::string passwd_info::homedir() const noexcept { - return ((value && value->homedir) ? value->homedir : ""); -} - -UVW_INLINE passwd_info::operator bool() const noexcept { - return static_cast(value); -} - -UVW_INLINE uts_name::uts_name(std::shared_ptr init) - : uname{init} {} - -UVW_INLINE std::string uts_name::sysname() const noexcept { - return uname ? uname->sysname : ""; -} - -UVW_INLINE std::string uts_name::release() const noexcept { - return uname ? uname->release : ""; -} - -UVW_INLINE std::string uts_name::version() const noexcept { - return uname ? uname->version : ""; -} - -UVW_INLINE std::string uts_name::machine() const noexcept { - return uname ? uname->machine : ""; -} - -namespace details { - -UVW_INLINE void common_alloc_callback(uv_handle_t *, std::size_t suggested, uv_buf_t *buf) { - auto size = static_cast(suggested); - *buf = uv_buf_init(new char[size], size); -} - -UVW_INLINE sockaddr ip_addr(const char *addr, unsigned int port) { - // explicitly cast to avoid `-Wsign-conversion` warnings - // libuv internally just casts to an `unsigned short` anyway - auto signed_port = static_cast(port); - if(sockaddr_in addr_in; uv_ip4_addr(addr, signed_port, &addr_in) == 0) { - return reinterpret_cast(addr_in); - } else if(sockaddr_in6 addr_in6; uv_ip6_addr(addr, signed_port, &addr_in6) == 0) { - return reinterpret_cast(addr_in6); - } - - return {}; -} - -UVW_INLINE socket_address sock_addr(const sockaddr_in &addr) { - if(char name[details::DEFAULT_SIZE]; uv_ip4_name(&addr, name, details::DEFAULT_SIZE) == 0) { - return socket_address{std::string{name}, ntohs(addr.sin_port)}; - } - - return socket_address{}; -} - -UVW_INLINE socket_address sock_addr(const sockaddr_in6 &addr) { - if(char name[details::DEFAULT_SIZE]; uv_ip6_name(&addr, name, details::DEFAULT_SIZE) == 0) { - return socket_address{std::string{name}, ntohs(addr.sin6_port)}; - } - - return socket_address{}; -} - -UVW_INLINE socket_address sock_addr(const sockaddr &addr) { - if(addr.sa_family == AF_INET) { - return sock_addr(reinterpret_cast(addr)); - } else if(addr.sa_family == AF_INET6) { - return sock_addr(reinterpret_cast(addr)); - } - - return socket_address{}; -} - -UVW_INLINE socket_address sock_addr(const sockaddr_storage &storage) { - if(storage.ss_family == AF_INET) { - return sock_addr(reinterpret_cast(storage)); - } else if(storage.ss_family == AF_INET6) { - return sock_addr(reinterpret_cast(storage)); - } - - return socket_address{}; -} - -} // namespace details - -UVW_INLINE pid_type utilities::os::pid() noexcept { - return uv_os_getpid(); -} - -UVW_INLINE pid_type utilities::os::ppid() noexcept { - return uv_os_getppid(); -} - -UVW_INLINE std::string utilities::os::homedir() noexcept { - return details::try_read(&uv_os_homedir); -} - -UVW_INLINE std::string utilities::os::tmpdir() noexcept { - return details::try_read(&uv_os_tmpdir); -} - -UVW_INLINE std::string utilities::os::env(const std::string &name) noexcept { - return details::try_read(&uv_os_getenv, name.c_str()); -} - -UVW_INLINE bool utilities::os::env(const std::string &name, const std::string &value) noexcept { - return (0 == (value.empty() ? uv_os_unsetenv(name.c_str()) : uv_os_setenv(name.c_str(), value.c_str()))); -} - -UVW_INLINE std::string utilities::os::hostname() noexcept { - return details::try_read(&uv_os_gethostname); -} - -UVW_INLINE uts_name utilities::os::uname() noexcept { - auto ptr = std::make_shared(); - uv_os_uname(ptr.get()); - return ptr; -} - -UVW_INLINE passwd_info utilities::os::passwd() noexcept { - auto deleter = [](uv_passwd_t *passwd) { - uv_os_free_passwd(passwd); - delete passwd; - }; - - std::shared_ptr ptr{new uv_passwd_t, std::move(deleter)}; - uv_os_get_passwd(ptr.get()); - return ptr; -} - -UVW_INLINE int utilities::os::priority(pid_type pid) { - int prio = 0; - - if(uv_os_getpriority(pid, &prio)) { - prio = UV_PRIORITY_LOW + 1; - } - - return prio; -} - -UVW_INLINE bool utilities::os::priority(pid_type pid, int prio) { - return 0 == uv_os_setpriority(pid, prio); -} - -UVW_INLINE handle_type utilities::guess_handle(handle_category category) noexcept { - switch(category) { - case UV_ASYNC: - return handle_type::ASYNC; - case UV_CHECK: - return handle_type::CHECK; - case UV_FS_EVENT: - return handle_type::FS_EVENT; - case UV_FS_POLL: - return handle_type::FS_POLL; - case UV_HANDLE: - return handle_type::HANDLE; - case UV_IDLE: - return handle_type::IDLE; - case UV_NAMED_PIPE: - return handle_type::PIPE; - case UV_POLL: - return handle_type::POLL; - case UV_PREPARE: - return handle_type::PREPARE; - case UV_PROCESS: - return handle_type::PROCESS; - case UV_STREAM: - return handle_type::STREAM; - case UV_TCP: - return handle_type::TCP; - case UV_TIMER: - return handle_type::TIMER; - case UV_TTY: - return handle_type::TTY; - case UV_UDP: - return handle_type::UDP; - case UV_SIGNAL: - return handle_type::SIGNAL; - case UV_FILE: - return handle_type::FILE; - default: - return handle_type::UNKNOWN; - } -} - -UVW_INLINE handle_type utilities::guess_handle(file_handle file) noexcept { - handle_category category = uv_guess_handle(file); - return guess_handle(category); -} - -UVW_INLINE std::vector utilities::cpu() noexcept { - std::vector cpuinfos; - - uv_cpu_info_t *infos; - int count; - - if(0 == uv_cpu_info(&infos, &count)) { - for(int next = 0; next < count; ++next) { - cpuinfos.push_back({infos[next].model, infos[next].speed, infos[next].cpu_times}); - } - - uv_free_cpu_info(infos, count); - } - - return cpuinfos; -} - -UVW_INLINE std::vector utilities::interface_addresses() noexcept { - std::vector interfaces; - - uv_interface_address_t *ifaces{nullptr}; - int count{0}; - - if(0 == uv_interface_addresses(&ifaces, &count)) { - for(int next = 0; next < count; ++next) { - interface_address iface_addr; - - iface_addr.name = ifaces[next].name; - std::copy(ifaces[next].phys_addr, (ifaces[next].phys_addr + 6), iface_addr.physical); - iface_addr.internal = ifaces[next].is_internal == 0 ? false : true; - - if(ifaces[next].address.address4.sin_family == AF_INET) { - iface_addr.address = details::sock_addr(ifaces[next].address.address4); - iface_addr.netmask = details::sock_addr(ifaces[next].netmask.netmask4); - } else if(ifaces[next].address.address4.sin_family == AF_INET6) { - iface_addr.address = details::sock_addr(ifaces[next].address.address6); - iface_addr.netmask = details::sock_addr(ifaces[next].netmask.netmask6); - } - - interfaces.push_back(std::move(iface_addr)); - } - - uv_free_interface_addresses(ifaces, count); - } - - return interfaces; -} - -UVW_INLINE std::string utilities::index_to_name(unsigned int index) noexcept { - return details::try_read(&uv_if_indextoname, index); -} - -UVW_INLINE std::string utilities::index_to_iid(unsigned int index) noexcept { - return details::try_read(&uv_if_indextoiid, index); -} - -UVW_INLINE bool utilities::replace_allocator(malloc_func_type malloc_func, realloc_func_type realloc_func, calloc_func_type calloc_func, free_func_type free_func) noexcept { - return (0 == uv_replace_allocator(malloc_func, realloc_func, calloc_func, free_func)); -} - -UVW_INLINE std::array utilities::load_average() noexcept { - std::array avg; - uv_loadavg(avg.data()); - return avg; -} - -UVW_INLINE char **utilities::setup_args(int argc, char **argv) { - return uv_setup_args(argc, argv); -} - -UVW_INLINE std::string utilities::process_title() { - std::size_t size = details::DEFAULT_SIZE; - char buf[details::DEFAULT_SIZE]; - std::string str{}; - - if(0 == uv_get_process_title(buf, size)) { - str.assign(buf, size); - } - - return str; -} - -UVW_INLINE bool utilities::process_title(const std::string &title) { - return (0 == uv_set_process_title(title.c_str())); -} - -UVW_INLINE uint64_t utilities::total_memory() noexcept { - return uv_get_total_memory(); -} - -UVW_INLINE uint64_t utilities::constrained_memory() noexcept { - return uv_get_constrained_memory(); -} - -UVW_INLINE uint64_t utilities::available_memory() noexcept { - return uv_get_available_memory(); -} - -UVW_INLINE int64_t utilities::resident_set_memory() noexcept { - size_t res{}; - const auto err = uv_resident_set_memory(&res); - return (err == 0) ? static_cast(res) : static_cast(err); -} - -UVW_INLINE double utilities::uptime() noexcept { - double ret; - - if(0 != uv_uptime(&ret)) { - ret = 0; - } - - return ret; -} - -UVW_INLINE resource_usage utilities::rusage() noexcept { - resource_usage ru; - auto err = uv_getrusage(&ru); - return err ? resource_usage{} : ru; -} - -UVW_INLINE timespec64 utilities::gettime(clock_id source) noexcept { - timespec64 ts; - auto err = uv_clock_gettime(static_cast(source), &ts); - return err ? timespec64{} : ts; -} - -UVW_INLINE uint64_t utilities::hrtime() noexcept { - return uv_hrtime(); -} - -UVW_INLINE std::string utilities::path() noexcept { - return details::try_read(&uv_exepath); -} - -UVW_INLINE std::string utilities::cwd() noexcept { - return details::try_read(&uv_cwd); -} - -UVW_INLINE bool utilities::chdir(const std::string &dir) noexcept { - return (0 == uv_chdir(dir.data())); -} - -UVW_INLINE timeval64 utilities::time_of_day() noexcept { - uv_timeval64_t ret; - uv_gettimeofday(&ret); - return ret; -} - -UVW_INLINE void utilities::sleep(unsigned int msec) noexcept { - uv_sleep(msec); -} - -UVW_INLINE unsigned int utilities::available_parallelism() noexcept { - return uv_available_parallelism(); -} - -} // namespace uvw +#include "util.h" +#include "util.ipp" diff --git a/src/uvw/util.h b/src/uvw/util.h index 0aec1933..a6e4ffb3 100644 --- a/src/uvw/util.h +++ b/src/uvw/util.h @@ -707,7 +707,7 @@ overloaded(Func...) -> overloaded; } // namespace uvw #ifndef UVW_AS_LIB -# include "util.cpp" +# include "util.ipp" #endif #endif // UVW_UTIL_INCLUDE_H diff --git a/src/uvw/util.ipp b/src/uvw/util.ipp new file mode 100644 index 00000000..1819fbf1 --- /dev/null +++ b/src/uvw/util.ipp @@ -0,0 +1,370 @@ +#include "config.h" + +namespace uvw { + +UVW_INLINE passwd_info::passwd_info(std::shared_ptr pwd) + : value{pwd} {} + +UVW_INLINE std::string passwd_info::username() const noexcept { + return ((value && value->username) ? value->username : ""); +} + +UVW_INLINE decltype(uv_passwd_t::uid) passwd_info::uid() const noexcept { + return (value ? value->uid : decltype(uv_passwd_t::uid){}); +} + +UVW_INLINE decltype(uv_passwd_t::gid) passwd_info::gid() const noexcept { + return (value ? value->gid : decltype(uv_passwd_t::gid){}); +} + +UVW_INLINE std::string passwd_info::shell() const noexcept { + return ((value && value->shell) ? value->shell : ""); +} + +UVW_INLINE std::string passwd_info::homedir() const noexcept { + return ((value && value->homedir) ? value->homedir : ""); +} + +UVW_INLINE passwd_info::operator bool() const noexcept { + return static_cast(value); +} + +UVW_INLINE uts_name::uts_name(std::shared_ptr init) + : uname{init} {} + +UVW_INLINE std::string uts_name::sysname() const noexcept { + return uname ? uname->sysname : ""; +} + +UVW_INLINE std::string uts_name::release() const noexcept { + return uname ? uname->release : ""; +} + +UVW_INLINE std::string uts_name::version() const noexcept { + return uname ? uname->version : ""; +} + +UVW_INLINE std::string uts_name::machine() const noexcept { + return uname ? uname->machine : ""; +} + +namespace details { + +UVW_INLINE void common_alloc_callback(uv_handle_t *, std::size_t suggested, uv_buf_t *buf) { + auto size = static_cast(suggested); + *buf = uv_buf_init(new char[size], size); +} + +UVW_INLINE sockaddr ip_addr(const char *addr, unsigned int port) { + // explicitly cast to avoid `-Wsign-conversion` warnings + // libuv internally just casts to an `unsigned short` anyway + auto signed_port = static_cast(port); + if(sockaddr_in addr_in; uv_ip4_addr(addr, signed_port, &addr_in) == 0) { + return reinterpret_cast(addr_in); + } else if(sockaddr_in6 addr_in6; uv_ip6_addr(addr, signed_port, &addr_in6) == 0) { + return reinterpret_cast(addr_in6); + } + + return {}; +} + +UVW_INLINE socket_address sock_addr(const sockaddr_in &addr) { + if(char name[details::DEFAULT_SIZE]; uv_ip4_name(&addr, name, details::DEFAULT_SIZE) == 0) { + return socket_address{std::string{name}, ntohs(addr.sin_port)}; + } + + return socket_address{}; +} + +UVW_INLINE socket_address sock_addr(const sockaddr_in6 &addr) { + if(char name[details::DEFAULT_SIZE]; uv_ip6_name(&addr, name, details::DEFAULT_SIZE) == 0) { + return socket_address{std::string{name}, ntohs(addr.sin6_port)}; + } + + return socket_address{}; +} + +UVW_INLINE socket_address sock_addr(const sockaddr &addr) { + if(addr.sa_family == AF_INET) { + return sock_addr(reinterpret_cast(addr)); + } else if(addr.sa_family == AF_INET6) { + return sock_addr(reinterpret_cast(addr)); + } + + return socket_address{}; +} + +UVW_INLINE socket_address sock_addr(const sockaddr_storage &storage) { + if(storage.ss_family == AF_INET) { + return sock_addr(reinterpret_cast(storage)); + } else if(storage.ss_family == AF_INET6) { + return sock_addr(reinterpret_cast(storage)); + } + + return socket_address{}; +} + +} // namespace details + +UVW_INLINE pid_type utilities::os::pid() noexcept { + return uv_os_getpid(); +} + +UVW_INLINE pid_type utilities::os::ppid() noexcept { + return uv_os_getppid(); +} + +UVW_INLINE std::string utilities::os::homedir() noexcept { + return details::try_read(&uv_os_homedir); +} + +UVW_INLINE std::string utilities::os::tmpdir() noexcept { + return details::try_read(&uv_os_tmpdir); +} + +UVW_INLINE std::string utilities::os::env(const std::string &name) noexcept { + return details::try_read(&uv_os_getenv, name.c_str()); +} + +UVW_INLINE bool utilities::os::env(const std::string &name, const std::string &value) noexcept { + return (0 == (value.empty() ? uv_os_unsetenv(name.c_str()) : uv_os_setenv(name.c_str(), value.c_str()))); +} + +UVW_INLINE std::string utilities::os::hostname() noexcept { + return details::try_read(&uv_os_gethostname); +} + +UVW_INLINE uts_name utilities::os::uname() noexcept { + auto ptr = std::make_shared(); + uv_os_uname(ptr.get()); + return ptr; +} + +UVW_INLINE passwd_info utilities::os::passwd() noexcept { + auto deleter = [](uv_passwd_t *passwd) { + uv_os_free_passwd(passwd); + delete passwd; + }; + + std::shared_ptr ptr{new uv_passwd_t, std::move(deleter)}; + uv_os_get_passwd(ptr.get()); + return ptr; +} + +UVW_INLINE int utilities::os::priority(pid_type pid) { + int prio = 0; + + if(uv_os_getpriority(pid, &prio)) { + prio = UV_PRIORITY_LOW + 1; + } + + return prio; +} + +UVW_INLINE bool utilities::os::priority(pid_type pid, int prio) { + return 0 == uv_os_setpriority(pid, prio); +} + +UVW_INLINE handle_type utilities::guess_handle(handle_category category) noexcept { + switch(category) { + case UV_ASYNC: + return handle_type::ASYNC; + case UV_CHECK: + return handle_type::CHECK; + case UV_FS_EVENT: + return handle_type::FS_EVENT; + case UV_FS_POLL: + return handle_type::FS_POLL; + case UV_HANDLE: + return handle_type::HANDLE; + case UV_IDLE: + return handle_type::IDLE; + case UV_NAMED_PIPE: + return handle_type::PIPE; + case UV_POLL: + return handle_type::POLL; + case UV_PREPARE: + return handle_type::PREPARE; + case UV_PROCESS: + return handle_type::PROCESS; + case UV_STREAM: + return handle_type::STREAM; + case UV_TCP: + return handle_type::TCP; + case UV_TIMER: + return handle_type::TIMER; + case UV_TTY: + return handle_type::TTY; + case UV_UDP: + return handle_type::UDP; + case UV_SIGNAL: + return handle_type::SIGNAL; + case UV_FILE: + return handle_type::FILE; + default: + return handle_type::UNKNOWN; + } +} + +UVW_INLINE handle_type utilities::guess_handle(file_handle file) noexcept { + handle_category category = uv_guess_handle(file); + return guess_handle(category); +} + +UVW_INLINE std::vector utilities::cpu() noexcept { + std::vector cpuinfos; + + uv_cpu_info_t *infos; + int count; + + if(0 == uv_cpu_info(&infos, &count)) { + for(int next = 0; next < count; ++next) { + cpuinfos.push_back({infos[next].model, infos[next].speed, infos[next].cpu_times}); + } + + uv_free_cpu_info(infos, count); + } + + return cpuinfos; +} + +UVW_INLINE std::vector utilities::interface_addresses() noexcept { + std::vector interfaces; + + uv_interface_address_t *ifaces{nullptr}; + int count{0}; + + if(0 == uv_interface_addresses(&ifaces, &count)) { + for(int next = 0; next < count; ++next) { + interface_address iface_addr; + + iface_addr.name = ifaces[next].name; + std::copy(ifaces[next].phys_addr, (ifaces[next].phys_addr + 6), iface_addr.physical); + iface_addr.internal = ifaces[next].is_internal == 0 ? false : true; + + if(ifaces[next].address.address4.sin_family == AF_INET) { + iface_addr.address = details::sock_addr(ifaces[next].address.address4); + iface_addr.netmask = details::sock_addr(ifaces[next].netmask.netmask4); + } else if(ifaces[next].address.address4.sin_family == AF_INET6) { + iface_addr.address = details::sock_addr(ifaces[next].address.address6); + iface_addr.netmask = details::sock_addr(ifaces[next].netmask.netmask6); + } + + interfaces.push_back(std::move(iface_addr)); + } + + uv_free_interface_addresses(ifaces, count); + } + + return interfaces; +} + +UVW_INLINE std::string utilities::index_to_name(unsigned int index) noexcept { + return details::try_read(&uv_if_indextoname, index); +} + +UVW_INLINE std::string utilities::index_to_iid(unsigned int index) noexcept { + return details::try_read(&uv_if_indextoiid, index); +} + +UVW_INLINE bool utilities::replace_allocator(malloc_func_type malloc_func, realloc_func_type realloc_func, calloc_func_type calloc_func, free_func_type free_func) noexcept { + return (0 == uv_replace_allocator(malloc_func, realloc_func, calloc_func, free_func)); +} + +UVW_INLINE std::array utilities::load_average() noexcept { + std::array avg; + uv_loadavg(avg.data()); + return avg; +} + +UVW_INLINE char **utilities::setup_args(int argc, char **argv) { + return uv_setup_args(argc, argv); +} + +UVW_INLINE std::string utilities::process_title() { + std::size_t size = details::DEFAULT_SIZE; + char buf[details::DEFAULT_SIZE]; + std::string str{}; + + if(0 == uv_get_process_title(buf, size)) { + str.assign(buf, size); + } + + return str; +} + +UVW_INLINE bool utilities::process_title(const std::string &title) { + return (0 == uv_set_process_title(title.c_str())); +} + +UVW_INLINE uint64_t utilities::total_memory() noexcept { + return uv_get_total_memory(); +} + +UVW_INLINE uint64_t utilities::constrained_memory() noexcept { + return uv_get_constrained_memory(); +} + +UVW_INLINE uint64_t utilities::available_memory() noexcept { + return uv_get_available_memory(); +} + +UVW_INLINE int64_t utilities::resident_set_memory() noexcept { + size_t res{}; + const auto err = uv_resident_set_memory(&res); + return (err == 0) ? static_cast(res) : static_cast(err); +} + +UVW_INLINE double utilities::uptime() noexcept { + double ret; + + if(0 != uv_uptime(&ret)) { + ret = 0; + } + + return ret; +} + +UVW_INLINE resource_usage utilities::rusage() noexcept { + resource_usage ru; + auto err = uv_getrusage(&ru); + return err ? resource_usage{} : ru; +} + +UVW_INLINE timespec64 utilities::gettime(clock_id source) noexcept { + timespec64 ts; + auto err = uv_clock_gettime(static_cast(source), &ts); + return err ? timespec64{} : ts; +} + +UVW_INLINE uint64_t utilities::hrtime() noexcept { + return uv_hrtime(); +} + +UVW_INLINE std::string utilities::path() noexcept { + return details::try_read(&uv_exepath); +} + +UVW_INLINE std::string utilities::cwd() noexcept { + return details::try_read(&uv_cwd); +} + +UVW_INLINE bool utilities::chdir(const std::string &dir) noexcept { + return (0 == uv_chdir(dir.data())); +} + +UVW_INLINE timeval64 utilities::time_of_day() noexcept { + uv_timeval64_t ret; + uv_gettimeofday(&ret); + return ret; +} + +UVW_INLINE void utilities::sleep(unsigned int msec) noexcept { + uv_sleep(msec); +} + +UVW_INLINE unsigned int utilities::available_parallelism() noexcept { + return uv_available_parallelism(); +} + +} // namespace uvw diff --git a/src/uvw/work.cpp b/src/uvw/work.cpp index f6fc68d0..149a704a 100644 --- a/src/uvw/work.cpp +++ b/src/uvw/work.cpp @@ -1,30 +1,2 @@ -#ifdef UVW_AS_LIB -# include "work.h" -#endif - -#include - -#include "config.h" - -namespace uvw { - -UVW_INLINE work_req::work_req(loop::token token, std::shared_ptr ref, task t) - : request{token, std::move(ref)}, func{t} {} - -UVW_INLINE void work_req::work_callback(uv_work_t *req) { - static_cast(req->data)->func(); -} - -UVW_INLINE void work_req::after_work_callback(uv_work_t* req, int status) { - if(auto ptr = reserve(req); status) { - ptr->publish(error_event{status}); - } else { - ptr->publish(work_event{}); - } -} - -UVW_INLINE int work_req::queue() { - return this->leak_if(uv_queue_work(parent().raw(), raw(), &work_callback, &after_work_callback)); -} - -} // namespace uvw +#include "work.h" +#include "work.ipp" diff --git a/src/uvw/work.h b/src/uvw/work.h index d6a4c1e2..b308dd89 100644 --- a/src/uvw/work.h +++ b/src/uvw/work.h @@ -53,7 +53,7 @@ private: } // namespace uvw #ifndef UVW_AS_LIB -# include "work.cpp" +# include "work.ipp" #endif #endif // UVW_WORK_INCLUDE_H diff --git a/src/uvw/work.ipp b/src/uvw/work.ipp new file mode 100644 index 00000000..7c23a9cf --- /dev/null +++ b/src/uvw/work.ipp @@ -0,0 +1,25 @@ +#include +#include "config.h" + +namespace uvw { + +UVW_INLINE work_req::work_req(loop::token token, std::shared_ptr ref, task t) + : request{token, std::move(ref)}, func{t} {} + +UVW_INLINE void work_req::work_callback(uv_work_t *req) { + static_cast(req->data)->func(); +} + +UVW_INLINE void work_req::after_work_callback(uv_work_t *req, int status) { + if(auto ptr = reserve(req); status) { + ptr->publish(error_event{status}); + } else { + ptr->publish(work_event{}); + } +} + +UVW_INLINE int work_req::queue() { + return this->leak_if(uv_queue_work(parent().raw(), raw(), &work_callback, &after_work_callback)); +} + +} // namespace uvw