diff --git a/src/uvw.hpp b/src/uvw.hpp index 421e8b18..23ecef23 100644 --- a/src/uvw.hpp +++ b/src/uvw.hpp @@ -1,7 +1,6 @@ #include "uvw/async.hpp" #include "uvw/check.hpp" #include "uvw/dns.hpp" -#include "uvw/event.hpp" #include "uvw/fs.hpp" #include "uvw/fs_event.hpp" #include "uvw/fs_poll.hpp" diff --git a/src/uvw/async.hpp b/src/uvw/async.hpp index 59530480..708dd4c8 100644 --- a/src/uvw/async.hpp +++ b/src/uvw/async.hpp @@ -4,7 +4,6 @@ #include #include #include -#include "event.hpp" #include "handle.hpp" #include "loop.hpp" @@ -17,7 +16,7 @@ namespace uvw { * * It will be emitted by AsyncHandle according with its functionalities. */ -struct AsyncEvent: Event {}; +struct AsyncEvent {}; /** diff --git a/src/uvw/check.hpp b/src/uvw/check.hpp index 36e63000..aa8ee16a 100644 --- a/src/uvw/check.hpp +++ b/src/uvw/check.hpp @@ -4,7 +4,6 @@ #include #include #include -#include "event.hpp" #include "handle.hpp" #include "loop.hpp" @@ -19,7 +18,7 @@ namespace uvw { * * To create a `CheckHandle` through a `Loop`, no arguments are required. */ -struct CheckEvent: Event {}; +struct CheckEvent {}; /** diff --git a/src/uvw/dns.hpp b/src/uvw/dns.hpp index 698e58ba..12855d8f 100644 --- a/src/uvw/dns.hpp +++ b/src/uvw/dns.hpp @@ -5,7 +5,6 @@ #include #include #include -#include "event.hpp" #include "request.hpp" #include "util.hpp" #include "loop.hpp" @@ -19,7 +18,7 @@ namespace uvw { * * It will be emitted by GetAddrInfoReq according with its functionalities. */ -struct AddrInfoEvent: Event { +struct AddrInfoEvent { using Deleter = void(*)(addrinfo *); AddrInfoEvent(std::unique_ptr addr) @@ -41,7 +40,7 @@ struct AddrInfoEvent: Event { * * It will be emitted by GetNameInfoReq according with its functionalities. */ -struct NameInfoEvent: Event { +struct NameInfoEvent { NameInfoEvent(const char *host, const char *serv) : hostname{host}, service{serv} {} diff --git a/src/uvw/emitter.hpp b/src/uvw/emitter.hpp index c75c041d..60f6b5e3 100644 --- a/src/uvw/emitter.hpp +++ b/src/uvw/emitter.hpp @@ -9,12 +9,50 @@ #include #include #include -#include "event.hpp" +#include namespace uvw { +/** + * @brief The ErrorEvent event. + * + * Custom wrapper around libuv's error constants. + */ +struct ErrorEvent { + template::value>> + explicit ErrorEvent(U val) noexcept + : ec{static_cast(val)}, str{uv_strerror(ec)} + {} + + /** + * @brief Returns the error message for the given error code. + * + * Leaks a few bytes of memory when you call it with an unknown error code. + * + * @return The error message for the given error code. + */ + const char * what() const noexcept { return str; } + + /** + * @brief Gets the underlying error code, that is a libuv's error constant. + * @return The underlying error code. + */ + int code() const noexcept { return ec; } + + /** + * @brief Checks if the event contains a valid error code. + * @return True in case of success, false otherwise. + */ + explicit operator bool() const noexcept { return ec < 0; } + +private: + const int ec; + const char *str; +}; + + /** * @brief Event emitter base class. * @@ -96,11 +134,20 @@ class Emitter { ListenerList onL{}; }; + static std::size_t next_type() noexcept { + static std::size_t counter = 0; + return counter++; + } + + template + static std::size_t event_type() noexcept { + static std::size_t value = next_type(); + return value; + } + template Handler & handler() noexcept { - static_assert(std::is_base_of, E>::value, "!"); - - std::size_t type = E::type(); + std::size_t type = event_type(); if(!(type < handlers.size())) { handlers.resize(type+1); @@ -222,7 +269,7 @@ public: */ template bool empty() const noexcept { - std::size_t type = E::type(); + std::size_t type = event_type(); return (!(type < handlers.size()) || !handlers[type] || diff --git a/src/uvw/event.hpp b/src/uvw/event.hpp deleted file mode 100644 index ad3b99be..00000000 --- a/src/uvw/event.hpp +++ /dev/null @@ -1,85 +0,0 @@ -#pragma once - - -#include -#include -#include - - -namespace uvw { - - -namespace details { - - -struct BaseEvent { - virtual ~BaseEvent() noexcept {} - - static std::size_t next() noexcept { - static std::size_t cnt = 0; - return cnt++; - } -}; - - -} - - -/** - * @brief Generic event type. - * - * Events in `uvw` must be associated with an unique numerical identifier. To do - * that, they shall inherit from this class. - */ -template -struct Event: details::BaseEvent { - /** - * @brief Gets the unique numerical identifier. - * @return An unique numerical identifier for the given event type. - */ - static std::size_t type() noexcept { - static std::size_t val = BaseEvent::next(); - return val; - } -}; - - -/** - * @brief The ErrorEvent event. - * - * Custom wrapper around libuv's error constants. - */ -struct ErrorEvent: Event { - template::value>> - explicit ErrorEvent(U val) noexcept - : ec{static_cast(val)}, str{uv_strerror(ec)} - {} - - /** - * @brief Returns the error message for the given error code. - * - * Leaks a few bytes of memory when you call it with an unknown error code. - * - * @return The error message for the given error code. - */ - const char * what() const noexcept { return str; } - - /** - * @brief Gets the underlying error code, that is a libuv's error constant. - * @return The underlying error code. - */ - int code() const noexcept { return ec; } - - /** - * @brief Checks if the event contains a valid error code. - * @return True in case of success, false otherwise. - */ - explicit operator bool() const noexcept { return ec < 0; } - -private: - const int ec; - const char *str; -}; - - -} diff --git a/src/uvw/fs.hpp b/src/uvw/fs.hpp index dd39a278..094af3b5 100644 --- a/src/uvw/fs.hpp +++ b/src/uvw/fs.hpp @@ -6,7 +6,6 @@ #include #include #include -#include "event.hpp" #include "request.hpp" #include "util.hpp" #include "loop.hpp" @@ -111,7 +110,7 @@ enum class UVDirentTypeT: std::underlying_type_t { * for further details. */ template -struct FsEvent: Event> { +struct FsEvent { FsEvent(const char *pathname) noexcept: path{pathname} {} const char * path; /*!< The path affecting the request. */ @@ -125,9 +124,7 @@ struct FsEvent: Event> { * functionalities. */ template<> -struct FsEvent - : Event> -{ +struct FsEvent { FsEvent(const char *pathname, std::unique_ptr buf, std::size_t sz) noexcept : path{pathname}, data{std::move(buf)}, size{sz} {} @@ -145,9 +142,7 @@ struct FsEvent * functionalities. */ template<> -struct FsEvent - : Event> -{ +struct FsEvent { FsEvent(const char *pathname, std::size_t sz) noexcept : path{pathname}, size{sz} {} @@ -164,9 +159,7 @@ struct FsEvent * functionalities. */ template<> -struct FsEvent - : Event> -{ +struct FsEvent { FsEvent(const char *pathname, std::size_t sz) noexcept : path{pathname}, size{sz} {} @@ -183,9 +176,7 @@ struct FsEvent * functionalities. */ template<> -struct FsEvent - : Event> -{ +struct FsEvent { FsEvent(const char *pathname, Stat curr) noexcept : path{pathname}, stat{std::move(curr)} {} @@ -202,9 +193,7 @@ struct FsEvent * functionalities. */ template<> -struct FsEvent - : Event> -{ +struct FsEvent { FsEvent(const char *pathname, Stat curr) noexcept : path{pathname}, stat{std::move(curr)} {} @@ -221,9 +210,7 @@ struct FsEvent * functionalities. */ template<> -struct FsEvent - : Event> -{ +struct FsEvent { FsEvent(const char *pathname, Stat curr) noexcept : path{pathname}, stat{std::move(curr)} {} @@ -240,9 +227,7 @@ struct FsEvent * functionalities. */ template<> -struct FsEvent - : Event> -{ +struct FsEvent { FsEvent(const char *pathname, std::size_t sz) noexcept : path{pathname}, size{sz} {} @@ -259,9 +244,7 @@ struct FsEvent * functionalities. */ template<> -struct FsEvent - : Event> -{ +struct FsEvent { explicit FsEvent(const char *pathname, const char *buf, std::size_t sz) noexcept : path{pathname}, data{buf}, size{sz} {} diff --git a/src/uvw/fs_event.hpp b/src/uvw/fs_event.hpp index d2e7e9dc..eea2727d 100644 --- a/src/uvw/fs_event.hpp +++ b/src/uvw/fs_event.hpp @@ -5,7 +5,6 @@ #include #include #include -#include "event.hpp" #include "handle.hpp" #include "util.hpp" #include "loop.hpp" @@ -38,7 +37,7 @@ enum class UVFsEvent: std::underlying_type_t { * * It will be emitted by FsEventHandle according with its functionalities. */ -struct FsEventEvent: Event { +struct FsEventEvent { FsEventEvent(const char * pathname, Flags events) : filename{pathname}, flags{std::move(events)} {} diff --git a/src/uvw/fs_poll.hpp b/src/uvw/fs_poll.hpp index f5b419b4..7d1cad88 100644 --- a/src/uvw/fs_poll.hpp +++ b/src/uvw/fs_poll.hpp @@ -5,7 +5,6 @@ #include #include #include -#include "event.hpp" #include "handle.hpp" #include "util.hpp" #include "loop.hpp" @@ -19,7 +18,7 @@ namespace uvw { * * It will be emitted by FsPollHandle according with its functionalities. */ -struct FsPollEvent: Event { +struct FsPollEvent { explicit FsPollEvent(Stat previous, Stat current) noexcept : prev{std::move(previous)}, curr{std::move(current)} {} diff --git a/src/uvw/handle.hpp b/src/uvw/handle.hpp index 7323a72b..4a2325b7 100644 --- a/src/uvw/handle.hpp +++ b/src/uvw/handle.hpp @@ -17,7 +17,7 @@ namespace uvw { * * It will be emitted by the handles according with their functionalities. */ -struct CloseEvent: Event {}; +struct CloseEvent {}; /** diff --git a/src/uvw/idle.hpp b/src/uvw/idle.hpp index 07d4f273..abfcdfce 100644 --- a/src/uvw/idle.hpp +++ b/src/uvw/idle.hpp @@ -4,7 +4,6 @@ #include #include #include -#include "event.hpp" #include "handle.hpp" #include "loop.hpp" @@ -17,7 +16,7 @@ namespace uvw { * * It will be emitted by IdleHandle according with its functionalities. */ -struct IdleEvent: Event {}; +struct IdleEvent {}; /** diff --git a/src/uvw/pipe.hpp b/src/uvw/pipe.hpp index c817f84a..41375ff9 100644 --- a/src/uvw/pipe.hpp +++ b/src/uvw/pipe.hpp @@ -6,7 +6,6 @@ #include #include #include -#include "event.hpp" #include "request.hpp" #include "stream.hpp" #include "util.hpp" diff --git a/src/uvw/poll.hpp b/src/uvw/poll.hpp index 34a05810..9a9f88ca 100644 --- a/src/uvw/poll.hpp +++ b/src/uvw/poll.hpp @@ -5,7 +5,6 @@ #include #include #include -#include "event.hpp" #include "handle.hpp" #include "util.hpp" @@ -31,7 +30,7 @@ enum class UVPollEvent: std::underlying_type_t { * * It will be emitted by PollHandle according with its functionalities. */ -struct PollEvent: Event { +struct PollEvent { explicit PollEvent(Flags events) noexcept : flags{std::move(events)} {} diff --git a/src/uvw/prepare.hpp b/src/uvw/prepare.hpp index 73582e02..b18bc8db 100644 --- a/src/uvw/prepare.hpp +++ b/src/uvw/prepare.hpp @@ -4,7 +4,6 @@ #include #include #include -#include "event.hpp" #include "handle.hpp" #include "loop.hpp" @@ -19,7 +18,7 @@ namespace uvw { * * To create a `PrepareHandle` through a `Loop`, no arguments are required. */ -struct PrepareEvent: Event {}; +struct PrepareEvent {}; /** diff --git a/src/uvw/process.hpp b/src/uvw/process.hpp index f4cf79a4..9299ab23 100644 --- a/src/uvw/process.hpp +++ b/src/uvw/process.hpp @@ -7,7 +7,6 @@ #include #include #include -#include "event.hpp" #include "handle.hpp" #include "stream.hpp" #include "util.hpp" @@ -47,7 +46,7 @@ enum class UVStdIOFlags: std::underlying_type_t { * * It will be emitted by ProcessHandle according with its functionalities. */ -struct ExitEvent: Event { +struct ExitEvent { explicit ExitEvent(int64_t code, int sig) noexcept : status{code}, signal{sig} {} diff --git a/src/uvw/signal.hpp b/src/uvw/signal.hpp index 4d411f3b..8b87683b 100644 --- a/src/uvw/signal.hpp +++ b/src/uvw/signal.hpp @@ -4,7 +4,6 @@ #include #include #include -#include "event.hpp" #include "handle.hpp" #include "loop.hpp" @@ -17,7 +16,7 @@ namespace uvw { * * It will be emitted by SignalHandle according with its functionalities. */ -struct SignalEvent: Event { +struct SignalEvent { explicit SignalEvent(int sig) noexcept: signum{sig} {} int signum; /*!< The signal being monitored by this handle. */ diff --git a/src/uvw/stream.hpp b/src/uvw/stream.hpp index b33f2bde..b1323ebb 100644 --- a/src/uvw/stream.hpp +++ b/src/uvw/stream.hpp @@ -7,7 +7,6 @@ #include #include #include -#include "event.hpp" #include "request.hpp" #include "handle.hpp" #include "loop.hpp" @@ -21,7 +20,7 @@ namespace uvw { * * It will be emitted by StreamHandle according with its functionalities. */ -struct ConnectEvent: Event {}; +struct ConnectEvent {}; /** @@ -29,7 +28,7 @@ struct ConnectEvent: Event {}; * * It will be emitted by StreamHandle according with its functionalities. */ -struct EndEvent: Event {}; +struct EndEvent {}; /** @@ -37,7 +36,7 @@ struct EndEvent: Event {}; * * It will be emitted by StreamHandle according with its functionalities. */ -struct ListenEvent: Event {}; +struct ListenEvent {}; /** @@ -45,7 +44,7 @@ struct ListenEvent: Event {}; * * It will be emitted by StreamHandle according with its functionalities. */ -struct ShutdownEvent: Event {}; +struct ShutdownEvent {}; /** @@ -53,7 +52,7 @@ struct ShutdownEvent: Event {}; * * It will be emitted by StreamHandle according with its functionalities. */ -struct WriteEvent: Event {}; +struct WriteEvent {}; /** @@ -61,7 +60,7 @@ struct WriteEvent: Event {}; * * It will be emitted by StreamHandle according with its functionalities. */ -struct DataEvent: Event { +struct DataEvent { explicit DataEvent(std::unique_ptr buf, std::size_t len) noexcept : data{std::move(buf)}, length{len} {} diff --git a/src/uvw/tcp.hpp b/src/uvw/tcp.hpp index c875368b..294aa548 100644 --- a/src/uvw/tcp.hpp +++ b/src/uvw/tcp.hpp @@ -7,7 +7,6 @@ #include #include #include -#include "event.hpp" #include "request.hpp" #include "stream.hpp" #include "util.hpp" diff --git a/src/uvw/timer.hpp b/src/uvw/timer.hpp index f456ed40..de0289a2 100644 --- a/src/uvw/timer.hpp +++ b/src/uvw/timer.hpp @@ -5,7 +5,6 @@ #include #include #include -#include "event.hpp" #include "handle.hpp" #include "loop.hpp" @@ -18,7 +17,7 @@ namespace uvw { * * It will be emitted by TimerHandle according with its functionalities. */ -struct TimerEvent: Event {}; +struct TimerEvent {}; /** diff --git a/src/uvw/tty.hpp b/src/uvw/tty.hpp index aa3b969e..b2d52df5 100644 --- a/src/uvw/tty.hpp +++ b/src/uvw/tty.hpp @@ -5,7 +5,6 @@ #include #include #include -#include "event.hpp" #include "stream.hpp" #include "util.hpp" diff --git a/src/uvw/udp.hpp b/src/uvw/udp.hpp index e0faf97e..8d8961b8 100644 --- a/src/uvw/udp.hpp +++ b/src/uvw/udp.hpp @@ -9,7 +9,6 @@ #include #include #include -#include "event.hpp" #include "request.hpp" #include "handle.hpp" #include "util.hpp" @@ -23,7 +22,7 @@ namespace uvw { * * It will be emitted by UDPHandle according with its functionalities. */ -struct SendEvent: Event {}; +struct SendEvent {}; /** @@ -31,7 +30,7 @@ struct SendEvent: Event {}; * * It will be emitted by UDPHandle according with its functionalities. */ -struct UDPDataEvent: Event { +struct UDPDataEvent { explicit UDPDataEvent(Addr sndr, std::unique_ptr buf, std::size_t len, bool part) noexcept : data{std::move(buf)}, length{len}, sender{std::move(sndr)}, partial{part} {} diff --git a/src/uvw/work.hpp b/src/uvw/work.hpp index 0d934fef..736c390e 100644 --- a/src/uvw/work.hpp +++ b/src/uvw/work.hpp @@ -5,7 +5,6 @@ #include #include #include -#include "event.hpp" #include "request.hpp" #include "loop.hpp" @@ -18,7 +17,7 @@ namespace uvw { * * It will be emitted by WorkReq according with its functionalities. */ -struct WorkEvent: Event {}; +struct WorkEvent {}; /** diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 9b2d76c8..6abcd872 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -36,7 +36,6 @@ set(TARGET_ASYNC async) set(TARGET_CHECK check) set(TARGET_DNS dns) set(TARGET_EMITTER emitter) -set(TARGET_EVENT event) set(TARGET_FS_REQ fs_req) set(TARGET_FILE_REQ file_req) set(TARGET_HANDLE handle) @@ -91,14 +90,6 @@ target_include_directories(${TARGET_EMITTER} PRIVATE ${COMMON_INCLUDE_DIRS}) target_link_libraries(${TARGET_EMITTER} PRIVATE ${COMMON_LINK_LIBS}) add_test(NAME ${TARGET_EMITTER} COMMAND ${TARGET_EMITTER}) -# Test TARGET_EVENT - -set(TARGET_EVENT_SOURCES $ uvw/event.cpp) -add_executable(${TARGET_EVENT} ${TARGET_EVENT_SOURCES}) -target_include_directories(${TARGET_EVENT} PRIVATE ${COMMON_INCLUDE_DIRS}) -target_link_libraries(${TARGET_EVENT} PRIVATE ${COMMON_LINK_LIBS}) -add_test(NAME ${TARGET_EVENT} COMMAND ${TARGET_EVENT}) - # Test TARGET_FS_REQ set(TARGET_FS_REQ_SOURCES $ uvw/fs_req.cpp) diff --git a/test/uvw/emitter.cpp b/test/uvw/emitter.cpp index 28f24203..2aa4bbd9 100644 --- a/test/uvw/emitter.cpp +++ b/test/uvw/emitter.cpp @@ -1,9 +1,8 @@ #include #include -#include -struct FakeEvent: uvw::Event { }; +struct FakeEvent { }; struct TestEmitter: uvw::Emitter { void emit() { publish(FakeEvent{}); } diff --git a/test/uvw/event.cpp b/test/uvw/event.cpp deleted file mode 100644 index 4588ef65..00000000 --- a/test/uvw/event.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include - - -struct EventA: uvw::Event {}; -struct EventB: uvw::Event {}; - - -TEST(Event, Uniqueness) { - ASSERT_EQ(EventA::type(), EventA::type()); - ASSERT_EQ(EventA::type(), EventA{}.type()); - - ASSERT_NE(EventA::type(), EventB::type()); - ASSERT_NE(EventA::type(), EventB{}.type()); -}