diff --git a/CMakeLists.txt b/CMakeLists.txt index 254e7df2..65735026 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ed48f7ca..6e401ee7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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 $<$: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 $<$: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 $<$: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 diff --git a/src/uvw/emitter.h b/src/uvw/emitter.h index a189f3a6..b5446e69 100644 --- a/src/uvw/emitter.h +++ b/src/uvw/emitter.h @@ -7,12 +7,23 @@ #include #include #include -#include +#include +#include #include #include #include +#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 - static std::size_t event_type() noexcept { - static std::size_t value = next_type(); - return value; + template + std::string event_type() const noexcept { + return UVW_PRETTY_FUNCTION; } template Handler & handler() noexcept { - std::size_t type = event_type(); + auto type = event_type(); - if(!(type < handlers.size())) { - handlers.resize(type+1); - } - - if(!handlers[type]) { + if(!handlers.count(type)) { handlers[type] = std::make_unique>(); } - return static_cast&>(*handlers[type]); + return static_cast&>(*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 bool empty() const noexcept { - std::size_t type = event_type(); + auto type = event_type(); - return (!(type < handlers.size()) || - !handlers[type] || - static_cast&>(*handlers[type]).empty()); + return (!handlers.count(type) || + static_cast&>(*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> handlers{}; + std::unordered_map> handlers{}; }; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 892b4aa8..b4825230 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -49,7 +49,9 @@ function(ADD_UVW_TEST TEST_NAME TEST_SOURCE) PRIVATE $<$:uvw::uvw> $<$:uv::uv-static> - $<$:uvw::uvw-static> + $<$:uvw::uvw-static> + $<$:uv::uv-shared> + $<$:uvw::uvw-shared> GTest::Main Threads::Threads ${LIBRT}