From 8988bdf17f96d0a16b3c2cd8612dacd7b2a82dcd Mon Sep 17 00:00:00 2001 From: actboy168 Date: Tue, 23 Apr 2024 09:06:51 +0800 Subject: [PATCH] add noexcept --- bee/net/bpoll.h | 12 ++++---- bee/net/bpoll_linux.cpp | 12 ++++---- bee/net/bpoll_osx.cpp | 22 +++++++------- bee/net/bpoll_win.cpp | 12 ++++---- bee/net/ip.h | 4 +-- bee/utility/bitmask.h | 64 ++++++++++++++++++++--------------------- bee/utility/span.h | 2 +- 7 files changed, 64 insertions(+), 64 deletions(-) diff --git a/bee/net/bpoll.h b/bee/net/bpoll.h index a8af3c36..255e2535 100644 --- a/bee/net/bpoll.h +++ b/bee/net/bpoll.h @@ -47,10 +47,10 @@ namespace bee::net { }; #endif - fd_t bpoll_create(); - bool bpoll_close(fd_t fd); - bool bpoll_ctl_add(fd_t fd, fd_t socket, const bpoll_event_t& event); - bool bpoll_ctl_mod(fd_t fd, fd_t socket, const bpoll_event_t& event); - bool bpoll_ctl_del(fd_t fd, fd_t socket); - int bpoll_wait(fd_t fd, const span& events, int timeout); + fd_t bpoll_create() noexcept; + bool bpoll_close(fd_t fd) noexcept; + bool bpoll_ctl_add(fd_t fd, fd_t socket, const bpoll_event_t& event) noexcept; + bool bpoll_ctl_mod(fd_t fd, fd_t socket, const bpoll_event_t& event) noexcept; + bool bpoll_ctl_del(fd_t fd, fd_t socket) noexcept; + int bpoll_wait(fd_t fd, const span& events, int timeout) noexcept; } diff --git a/bee/net/bpoll_linux.cpp b/bee/net/bpoll_linux.cpp index 1e54d689..d4b71b28 100644 --- a/bee/net/bpoll_linux.cpp +++ b/bee/net/bpoll_linux.cpp @@ -22,27 +22,27 @@ namespace bee::net { static_assert(std::to_underlying(bpoll_event::rdhup) == EPOLLRDHUP); static_assert(std::to_underlying(bpoll_event::oneshot) == EPOLLONESHOT); - fd_t bpoll_create() { + fd_t bpoll_create() noexcept { return (fd_t)::epoll_create1(EPOLL_CLOEXEC); } - bool bpoll_close(fd_t epfd) { + bool bpoll_close(fd_t epfd) noexcept { return ::close(epfd) == 0; } - bool bpoll_ctl_add(fd_t epfd, fd_t socket, const bpoll_event_t& event) { + bool bpoll_ctl_add(fd_t epfd, fd_t socket, const bpoll_event_t& event) noexcept { return ::epoll_ctl(epfd, EPOLL_CTL_ADD, socket, (struct epoll_event*)&event) != -1; } - bool bpoll_ctl_mod(fd_t epfd, fd_t socket, const bpoll_event_t& event) { + bool bpoll_ctl_mod(fd_t epfd, fd_t socket, const bpoll_event_t& event) noexcept { return ::epoll_ctl(epfd, EPOLL_CTL_MOD, socket, (struct epoll_event*)&event) != -1; } - bool bpoll_ctl_del(fd_t epfd, fd_t socket) { + bool bpoll_ctl_del(fd_t epfd, fd_t socket) noexcept { return ::epoll_ctl(epfd, EPOLL_CTL_DEL, socket, NULL) != -1; } - int bpoll_wait(fd_t epfd, const span& events, int timeout) { + int bpoll_wait(fd_t epfd, const span& events, int timeout) noexcept { return ::epoll_wait(epfd, (struct epoll_event*)events.data(), (int)events.size(), timeout); } } diff --git a/bee/net/bpoll_osx.cpp b/bee/net/bpoll_osx.cpp index a7231d14..5a7a521e 100644 --- a/bee/net/bpoll_osx.cpp +++ b/bee/net/bpoll_osx.cpp @@ -17,11 +17,11 @@ namespace bee::net { constexpr uint16_t KQUEUE_STATE_REGISTERED = 0x0001; constexpr uint16_t KQUEUE_STATE_EPOLLRDHUP = 0x0002; - static bool invalid_fd(int fd) { + static bool invalid_fd(int fd) noexcept { return (fd < 0 || ((uint32_t)fd & ~(((uint32_t)1 << KEY_BITS) - 1))); } - static int set_state(int kq, uint32_t key, uint16_t val) { + static int set_state(int kq, uint32_t key, uint16_t val) noexcept { if ((key & ~(((uint32_t)1 << KEY_BITS) - 1)) || (val & ~(((uint16_t)1 << VAL_BITS) - 1))) { return EINVAL; } @@ -46,7 +46,7 @@ namespace bee::net { return 0; } - static bool get_state(int kq, uint32_t key, uint16_t* val) { + static bool get_state(int kq, uint32_t key, uint16_t* val) noexcept { if ((key & ~(((uint32_t)1 << KEY_BITS) - 1))) { errno = EINVAL; return false; @@ -79,7 +79,7 @@ namespace bee::net { return true; } - static bool set_kevent(int epfd, int fd, int read_flags, int write_flags, void* udata) { + static bool set_kevent(int epfd, int fd, int read_flags, int write_flags, void* udata) noexcept { struct kevent ev[2]; EV_SET(&ev[0], fd, EVFILT_READ, read_flags | EV_RECEIPT, 0, 0, udata); EV_SET(&ev[1], fd, EVFILT_WRITE, write_flags | EV_RECEIPT, 0, 0, udata); @@ -99,7 +99,7 @@ namespace bee::net { constexpr bpoll_event AllowBpollEvents = bpoll_event::in | bpoll_event::out | bpoll_event::hup | bpoll_event::rdhup | bpoll_event::err; - static bool bpoll_ctl(int kq, int fd, const bpoll_event_t& ev, bool add) { + static bool bpoll_ctl(int kq, int fd, const bpoll_event_t& ev, bool add) noexcept { int flags = 0; int read_flags, write_flags; if (bitmask_has(ev.events, ~AllowBpollEvents)) { @@ -147,23 +147,23 @@ namespace bee::net { return ok; } - fd_t bpoll_create() { + fd_t bpoll_create() noexcept { return kqueue(); } - bool bpoll_close(fd_t kq) { + bool bpoll_close(fd_t kq) noexcept { return ::close(kq) == 0; } - bool bpoll_ctl_add(fd_t kq, fd_t fd, const bpoll_event_t& event) { + bool bpoll_ctl_add(fd_t kq, fd_t fd, const bpoll_event_t& event) noexcept { return bpoll_ctl(kq, fd, event, true); } - bool bpoll_ctl_mod(fd_t kq, fd_t fd, const bpoll_event_t& event) { + bool bpoll_ctl_mod(fd_t kq, fd_t fd, const bpoll_event_t& event) noexcept { return bpoll_ctl(kq, fd, event, false); } - bool bpoll_ctl_del(fd_t kq, fd_t fd) { + bool bpoll_ctl_del(fd_t kq, fd_t fd) noexcept { if (invalid_fd(fd)) { errno = EBADF; return false; @@ -175,7 +175,7 @@ namespace bee::net { return ok; } - int bpoll_wait(fd_t kq, const span& events, int timeout) { + int bpoll_wait(fd_t kq, const span& events, int timeout) noexcept { struct kevent kev[events.size()]; struct timespec t, *timeop = &t; if (timeout < 0) { diff --git a/bee/net/bpoll_win.cpp b/bee/net/bpoll_win.cpp index 2ece51f1..ad76f03c 100644 --- a/bee/net/bpoll_win.cpp +++ b/bee/net/bpoll_win.cpp @@ -4,7 +4,7 @@ #include namespace bee::net { - fd_t bpoll_create() { + fd_t bpoll_create() noexcept { afd::afd_context ctx; if (!afd::afd_create(ctx)) { return retired_fd; @@ -18,7 +18,7 @@ namespace bee::net { return (fd_t)ep; } - bool bpoll_close(fd_t fd) { + bool bpoll_close(fd_t fd) noexcept { if (fd == retired_fd) { SetLastError(ERROR_INVALID_HANDLE); return false; @@ -28,7 +28,7 @@ namespace bee::net { return true; } - bool bpoll_ctl_add(fd_t fd, fd_t socket, const bpoll_event_t& event) { + bool bpoll_ctl_add(fd_t fd, fd_t socket, const bpoll_event_t& event) noexcept { if (fd == retired_fd) { SetLastError(ERROR_INVALID_HANDLE); return false; @@ -46,7 +46,7 @@ namespace bee::net { return true; } - bool bpoll_ctl_mod(fd_t fd, fd_t socket, const bpoll_event_t& event) { + bool bpoll_ctl_mod(fd_t fd, fd_t socket, const bpoll_event_t& event) noexcept { if (fd == retired_fd) { SetLastError(ERROR_INVALID_HANDLE); return false; @@ -64,7 +64,7 @@ namespace bee::net { return true; } - bool bpoll_ctl_del(fd_t fd, fd_t socket) { + bool bpoll_ctl_del(fd_t fd, fd_t socket) noexcept { if (fd == retired_fd) { SetLastError(ERROR_INVALID_HANDLE); return false; @@ -82,7 +82,7 @@ namespace bee::net { return true; } - int bpoll_wait(fd_t fd, const span& events, int timeout) { + int bpoll_wait(fd_t fd, const span& events, int timeout) noexcept { if (fd == retired_fd) { SetLastError(ERROR_INVALID_HANDLE); return -1; diff --git a/bee/net/ip.h b/bee/net/ip.h index 465ea93b..966f1708 100644 --- a/bee/net/ip.h +++ b/bee/net/ip.h @@ -9,7 +9,7 @@ namespace bee::net::ip { template - static constexpr T host_to_network(T v) { + static constexpr T host_to_network(T v) noexcept { static_assert(std::is_integral_v && (sizeof(T) == 2 || sizeof(T) == 4)); if constexpr (std::endian::native == std::endian::big) { return v; @@ -23,7 +23,7 @@ namespace bee::net::ip { } template - static constexpr ptrdiff_t rfind(const char (&str)[N], size_t from, char c) { + static constexpr ptrdiff_t rfind(const char (&str)[N], size_t from, char c) noexcept { for (ptrdiff_t i = from; i >= 0; i--) { if (str[i] == c) { return i; diff --git a/bee/utility/bitmask.h b/bee/utility/bitmask.h index f6e789d9..08864d5e 100644 --- a/bee/utility/bitmask.h +++ b/bee/utility/bitmask.h @@ -2,38 +2,38 @@ #include -#define BEE_BITMASK_OPERATORS(bitmask) \ - [[nodiscard]] constexpr bitmask operator~(bitmask a) { \ - return static_cast( \ - ~static_cast>(a) \ - ); \ - } \ - [[nodiscard]] constexpr bitmask operator|(bitmask a, bitmask b) { \ - return static_cast( \ - static_cast>(a) | \ - static_cast>(b) \ - ); \ - } \ - [[nodiscard]] constexpr bitmask operator&(bitmask a, bitmask b) { \ - return static_cast( \ - static_cast>(a) & \ - static_cast>(b) \ - ); \ - } \ - [[nodiscard]] constexpr bitmask operator^(bitmask a, bitmask b) { \ - return static_cast( \ - static_cast>(a) ^ \ - static_cast>(b) \ - ); \ - } \ - constexpr bitmask& operator|=(bitmask& a, bitmask b) { \ - return a = a | b; \ - } \ - constexpr bitmask& operator&=(bitmask& a, bitmask b) { \ - return a = a & b; \ - } \ - constexpr bitmask& operator^=(bitmask& a, bitmask b) { \ - return a = a ^ b; \ +#define BEE_BITMASK_OPERATORS(bitmask) \ + [[nodiscard]] constexpr bitmask operator~(bitmask a) noexcept { \ + return static_cast( \ + ~static_cast>(a) \ + ); \ + } \ + [[nodiscard]] constexpr bitmask operator|(bitmask a, bitmask b) noexcept { \ + return static_cast( \ + static_cast>(a) | \ + static_cast>(b) \ + ); \ + } \ + [[nodiscard]] constexpr bitmask operator&(bitmask a, bitmask b) noexcept { \ + return static_cast( \ + static_cast>(a) & \ + static_cast>(b) \ + ); \ + } \ + [[nodiscard]] constexpr bitmask operator^(bitmask a, bitmask b) noexcept { \ + return static_cast( \ + static_cast>(a) ^ \ + static_cast>(b) \ + ); \ + } \ + constexpr bitmask& operator|=(bitmask& a, bitmask b) noexcept { \ + return a = a | b; \ + } \ + constexpr bitmask& operator&=(bitmask& a, bitmask b) noexcept { \ + return a = a & b; \ + } \ + constexpr bitmask& operator^=(bitmask& a, bitmask b) noexcept { \ + return a = a ^ b; \ } namespace bee { diff --git a/bee/utility/span.h b/bee/utility/span.h index cb6ad342..2a8c3a46 100644 --- a/bee/utility/span.h +++ b/bee/utility/span.h @@ -35,7 +35,7 @@ namespace bee { : data_(nullptr) , size_(0) { } - constexpr span(pointer ptr, size_type count) + constexpr span(pointer ptr, size_type count) noexcept : data_(ptr) , size_(count) { }