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_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_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
@ -79,10 +88,14 @@ function(fetch_libuv)
add_subdirectory(${libuv_SOURCE_DIR} ${libuv_BINARY_DIR})
endif()
add_library(uv::uv-static ALIAS uv_a)
set_target_properties(uv_a PROPERTIES POSITION_INDEPENDENT_CODE 1)
set_target_properties(uv PROPERTIES POSITION_INDEPENDENT_CODE 1)
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()
endif(FETCH_LIBUV)
endfunction()

View File

@ -57,15 +57,23 @@ function(add_uvw_library LIB_NAME)
endif()
endfunction()
#
# Static library
#
# Build libraries
#
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)
if (BUILD_UVW_SHARED_LIB)
add_library(uvw-shared SHARED)
add_library(uvw::uvw-shared ALIAS uvw-shared)
target_link_libraries(uvw-shared PUBLIC $<$<TARGET_EXISTS:uv::uv-shared>:uv::uv-shared>)
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

View File

@ -7,12 +7,23 @@
#include <algorithm>
#include <utility>
#include <cstddef>
#include <vector>
#include <unordered_map>
#include <string>
#include <memory>
#include <list>
#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 {
@ -157,30 +168,20 @@ class Emitter {
ListenerList onL{};
};
static std::size_t next_type() noexcept {
static std::size_t counter = 0;
return counter++;
}
template<typename>
static std::size_t event_type() noexcept {
static std::size_t value = next_type();
return value;
template <typename E>
std::string event_type() const noexcept {
return UVW_PRETTY_FUNCTION;
}
template<typename E>
Handler<E> & handler() noexcept {
std::size_t type = event_type<E>();
auto type = event_type<E>();
if(!(type < handlers.size())) {
handlers.resize(type+1);
}
if(!handlers[type]) {
if(!handlers.count(type)) {
handlers[type] = std::make_unique<Handler<E>>();
}
return static_cast<Handler<E>&>(*handlers[type]);
return static_cast<Handler<E>&>(*handlers.at(type));
}
protected:
@ -282,7 +283,7 @@ public:
*/
void clear() noexcept {
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>
bool empty() const noexcept {
std::size_t type = event_type<E>();
auto type = event_type<E>();
return (!(type < handlers.size()) ||
!handlers[type] ||
static_cast<Handler<E>&>(*handlers[type]).empty());
return (!handlers.count(type) ||
static_cast<Handler<E>&>(*handlers.at(type)).empty());
}
/**
@ -306,11 +306,11 @@ public:
*/
bool empty() const noexcept {
return std::all_of(handlers.cbegin(), handlers.cend(),
[](auto &&hdlr){ return !hdlr || hdlr->empty(); });
[](auto &&hdlr){ return !hdlr.second || hdlr.second->empty(); });
}
private:
std::vector<std::unique_ptr<BaseHandler>> handlers{};
std::unordered_map<std::string, std::unique_ptr<BaseHandler>> handlers{};
};

View File

@ -49,7 +49,9 @@ function(ADD_UVW_TEST TEST_NAME TEST_SOURCE)
PRIVATE
$<$<TARGET_EXISTS:uvw::uvw>:uvw::uvw>
$<$<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
Threads::Threads
${LIBRT}