Removing the monostate pattern from uvw. (#242)

This commit is contained in:
Stefano Fiorentino 2021-05-09 22:49:33 +02:00 committed by GitHub
parent b0015be668
commit 010d8de944
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 36 deletions

View File

@ -35,6 +35,15 @@ option(USE_LIBCPP "Use libc++ by adding -stdlib=libc++ flag if availbale." ON)
option(USE_ASAN "Use address sanitizer by adding -fsanitize=address -fno-omit-frame-pointer flags" OFF) option(USE_ASAN "Use address sanitizer by adding -fsanitize=address -fno-omit-frame-pointer flags" OFF)
option(USE_UBSAN "Use address sanitizer by adding -fsanitize=undefined -fno-sanitize-recover=all -fno-omit-frame-pointer flags" OFF) option(USE_UBSAN "Use address sanitizer by adding -fsanitize=undefined -fno-sanitize-recover=all -fno-omit-frame-pointer flags" OFF)
option(BUILD_UVW_LIBS "Prepare targets for static library rather than for a header-only library." OFF) option(BUILD_UVW_LIBS "Prepare targets for static library rather than for a header-only library." OFF)
option(BUILD_UVW_SHARED_LIB "Prepare targets for shared library rather than for a header-only library (not available for WIN32)." OFF)
if(BUILD_UVW_SHARED_LIB AND WIN32)
message(FATAL_ERROR "This feature is not yet available for WIN32 targets.")
endif()
if(BUILD_UVW_SHARED_LIB)
set(BUILD_UVW_LIBS BOOL:ON)
endif()
# #
# Compiler stuff # Compiler stuff
@ -79,10 +88,14 @@ function(fetch_libuv)
add_subdirectory(${libuv_SOURCE_DIR} ${libuv_BINARY_DIR}) add_subdirectory(${libuv_SOURCE_DIR} ${libuv_BINARY_DIR})
endif() endif()
add_library(uv::uv-static ALIAS uv_a) if(BUILD_UVW_SHARED_LIB)
add_library(uv::uv-shared ALIAS uv)
set_target_properties(uv PROPERTIES POSITION_INDEPENDENT_CODE 1)
else()
add_library(uv::uv-static ALIAS uv_a)
set_target_properties(uv_a PROPERTIES POSITION_INDEPENDENT_CODE 1)
endif()
set_target_properties(uv_a PROPERTIES POSITION_INDEPENDENT_CODE 1)
set_target_properties(uv PROPERTIES POSITION_INDEPENDENT_CODE 1)
endif(FETCH_LIBUV) endif(FETCH_LIBUV)
endfunction() endfunction()

View File

@ -58,14 +58,22 @@ function(add_uvw_library LIB_NAME)
endfunction() endfunction()
# #
# Static library # Build libraries
# #
add_library(uvw-static STATIC) if (BUILD_UVW_SHARED_LIB)
add_library(uvw::uvw-static ALIAS uvw-static) add_library(uvw-shared SHARED)
target_link_libraries(uvw-static PUBLIC $<$<TARGET_EXISTS:uv::uv-static>:uv::uv-static> $<$<NOT:$<TARGET_EXISTS:uv::uv-static>>:uv_a dl>) add_library(uvw::uvw-shared ALIAS uvw-shared)
set_target_properties(uvw-static PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD 1) target_link_libraries(uvw-shared PUBLIC $<$<TARGET_EXISTS:uv::uv-shared>:uv::uv-shared>)
add_uvw_library(uvw-static) set_target_properties(uvw-shared PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD 1)
add_uvw_library(uvw-shared)
else()
add_library(uvw-static STATIC)
add_library(uvw::uvw-static ALIAS uvw-static)
target_link_libraries(uvw-static PUBLIC $<$<TARGET_EXISTS:uv::uv-static>:uv::uv-static> $<$<NOT:$<TARGET_EXISTS:uv::uv-static>>:uv_a dl>)
set_target_properties(uvw-static PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD 1)
add_uvw_library(uvw-static)
endif()
# #
# Install targets # Install targets

View File

@ -7,12 +7,23 @@
#include <algorithm> #include <algorithm>
#include <utility> #include <utility>
#include <cstddef> #include <cstddef>
#include <vector> #include <unordered_map>
#include <string>
#include <memory> #include <memory>
#include <list> #include <list>
#include <uv.h> #include <uv.h>
#if defined __clang__ || defined __GNUC__
# define UVW_PRETTY_FUNCTION __PRETTY_FUNCTION__
# define UVW_PRETTY_FUNCTION_PREFIX '='
# define UVW_PRETTY_FUNCTION_SUFFIX ']'
#elif defined _MSC_VER
# define UVW_PRETTY_FUNCTION __FUNCSIG__
# define UVW_PRETTY_FUNCTION_PREFIX '<'
# define UVW_PRETTY_FUNCTION_SUFFIX '>'
#endif
namespace uvw { namespace uvw {
@ -157,30 +168,20 @@ class Emitter {
ListenerList onL{}; ListenerList onL{};
}; };
static std::size_t next_type() noexcept { template <typename E>
static std::size_t counter = 0; std::string event_type() const noexcept {
return counter++; return UVW_PRETTY_FUNCTION;
}
template<typename>
static std::size_t event_type() noexcept {
static std::size_t value = next_type();
return value;
} }
template<typename E> template<typename E>
Handler<E> & handler() noexcept { Handler<E> & handler() noexcept {
std::size_t type = event_type<E>(); auto type = event_type<E>();
if(!(type < handlers.size())) { if(!handlers.count(type)) {
handlers.resize(type+1);
}
if(!handlers[type]) {
handlers[type] = std::make_unique<Handler<E>>(); handlers[type] = std::make_unique<Handler<E>>();
} }
return static_cast<Handler<E>&>(*handlers[type]); return static_cast<Handler<E>&>(*handlers.at(type));
} }
protected: protected:
@ -282,7 +283,7 @@ public:
*/ */
void clear() noexcept { void clear() noexcept {
std::for_each(handlers.begin(), handlers.end(), std::for_each(handlers.begin(), handlers.end(),
[](auto &&hdlr){ if(hdlr) { hdlr->clear(); } }); [](auto &&hdlr){ if(hdlr.second) { hdlr.second->clear(); } });
} }
/** /**
@ -292,11 +293,10 @@ public:
*/ */
template<typename E> template<typename E>
bool empty() const noexcept { bool empty() const noexcept {
std::size_t type = event_type<E>(); auto type = event_type<E>();
return (!(type < handlers.size()) || return (!handlers.count(type) ||
!handlers[type] || static_cast<Handler<E>&>(*handlers.at(type)).empty());
static_cast<Handler<E>&>(*handlers[type]).empty());
} }
/** /**
@ -306,11 +306,11 @@ public:
*/ */
bool empty() const noexcept { bool empty() const noexcept {
return std::all_of(handlers.cbegin(), handlers.cend(), return std::all_of(handlers.cbegin(), handlers.cend(),
[](auto &&hdlr){ return !hdlr || hdlr->empty(); }); [](auto &&hdlr){ return !hdlr.second || hdlr.second->empty(); });
} }
private: private:
std::vector<std::unique_ptr<BaseHandler>> handlers{}; std::unordered_map<std::string, std::unique_ptr<BaseHandler>> handlers{};
}; };

View File

@ -50,6 +50,8 @@ function(ADD_UVW_TEST TEST_NAME TEST_SOURCE)
$<$<TARGET_EXISTS:uvw::uvw>:uvw::uvw> $<$<TARGET_EXISTS:uvw::uvw>:uvw::uvw>
$<$<TARGET_EXISTS:uv::uv-static>:uv::uv-static> $<$<TARGET_EXISTS:uv::uv-static>:uv::uv-static>
$<$<TARGET_EXISTS:uvw::uvw-static>:uvw::uvw-static> $<$<TARGET_EXISTS:uvw::uvw-static>:uvw::uvw-static>
$<$<TARGET_EXISTS:uv::uv-shared>:uv::uv-shared>
$<$<TARGET_EXISTS:uvw::uvw-shared>:uvw::uvw-shared>
GTest::Main GTest::Main
Threads::Threads Threads::Threads
${LIBRT} ${LIBRT}