From 34902c481d3ef8af97a5d9094a7b7c969ec797ae Mon Sep 17 00:00:00 2001 From: Dawid Drozd Date: Sun, 20 Jan 2019 20:39:07 +0100 Subject: [PATCH] Fix type_id() helper function for Visual Studio Issue: #19 Visual Studio optimize not so portable version of type_id() so each time we get same id for different types. Added small test to check this behavior in future. --- lib/CMakeLists.txt | 2 +- lib/include/eventbus/AsyncEventBus.h | 4 +- lib/include/eventbus/EventBus.h | 6 +-- lib/include/eventbus/internal/common.h | 9 +++-- test/CMakeLists.txt | 1 + test/src/EventIdTest.cpp | 53 ++++++++++++++++++++++++++ 6 files changed, 66 insertions(+), 9 deletions(-) create mode 100644 test/src/EventIdTest.cpp diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 92ec81a..d65fa6d 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.8 FATAL_ERROR) # BUILD_SHARED_LIBS can controll build type! project(EventBus - VERSION 2.4.0 + VERSION 2.4.1 LANGUAGES CXX ) diff --git a/lib/include/eventbus/AsyncEventBus.h b/lib/include/eventbus/AsyncEventBus.h index 2515f10..103d8d3 100644 --- a/lib/include/eventbus/AsyncEventBus.h +++ b/lib/include/eventbus/AsyncEventBus.h @@ -79,7 +79,7 @@ public: assert(callback && "callback should be valid"); //Check for valid object std::unique_ptr& vector = - _callbacks[Internal::type_id]; + _callbacks[Internal::type_id()]; if(vector == nullptr) { vector.reset(new Vector{}); @@ -150,7 +150,7 @@ public: std::lock_guard guard{_callbacksMutex}; using Vector = Internal::AsyncCallbackVector; - auto found = _callbacks.find(Internal::type_id); + auto found = _callbacks.find(Internal::type_id()); if(found == _callbacks.end()) { return; // no such notifications diff --git a/lib/include/eventbus/EventBus.h b/lib/include/eventbus/EventBus.h index a738c9b..7c20009 100644 --- a/lib/include/eventbus/EventBus.h +++ b/lib/include/eventbus/EventBus.h @@ -59,7 +59,7 @@ public: assert(callback && "callback should be valid"); //Check for valid object - std::unique_ptr& vector = _callbacks[Internal::type_id]; + std::unique_ptr& vector = _callbacks[Internal::type_id()]; if(vector == nullptr) { vector.reset(new Vector{}); @@ -89,7 +89,7 @@ public: { static_assert(Internal::validateEvent(), "Invalid event"); - auto found = _callbacks.find(Internal::type_id); + auto found = _callbacks.find(Internal::type_id()); if(found != _callbacks.end()) { found->second->remove(token); @@ -108,7 +108,7 @@ public: static_assert(Internal::validateEvent(), "Invalid event"); using Vector = Internal::TransactionCallbackVector; - auto found = _callbacks.find(Internal::type_id); + auto found = _callbacks.find(Internal::type_id()); if(found == _callbacks.end()) { return; // no such notifications diff --git a/lib/include/eventbus/internal/common.h b/lib/include/eventbus/internal/common.h index 1a37916..7a4bd3f 100644 --- a/lib/include/eventbus/internal/common.h +++ b/lib/include/eventbus/internal/common.h @@ -7,12 +7,15 @@ namespace Dexode namespace Internal { -template -void type_id() // Helper for getting "type id" +using type_id_t = std::size_t; + +template +type_id_t type_id() // Helper for getting "type id" { + return typeid(T).hash_code(); } -using type_id_t = void (*)(); // Function pointer + template constexpr bool validateEvent() diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index bf21ce9..c87cabb 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -21,6 +21,7 @@ find_package(Threads REQUIRED) add_executable(EventBusTest src/AsyncEventBusTest.cpp src/EventCollectorTest.cpp + src/EventIdTest.cpp src/NotifierTest.cpp ) diff --git a/test/src/EventIdTest.cpp b/test/src/EventIdTest.cpp new file mode 100644 index 0000000..369bb2a --- /dev/null +++ b/test/src/EventIdTest.cpp @@ -0,0 +1,53 @@ +#include + +#include + +#include + +namespace +{ +struct Anonymous +{}; +} // namespace + +struct TestA +{ + int a; +}; + +namespace Test +{ +struct TestA +{ + bool b; +}; + +namespace TestN +{ +struct TestA +{ + long long c; +}; + +} // namespace TestN + +} // namespace Test + +TEST_CASE("Should return unique id for each event When using Internal::type_id", "[EventId]") +{ + std::set unique; + + REQUIRE(unique.insert(Dexode::Internal::type_id()).second); + REQUIRE_FALSE(unique.insert(Dexode::Internal::type_id()).second); //already there + + struct TestA + {}; + + ::TestA a; + a.a = 10; + + REQUIRE(unique.insert(Dexode::Internal::type_id()).second); + REQUIRE(unique.insert(Dexode::Internal::type_id<::TestA>()).second); + REQUIRE(unique.insert(Dexode::Internal::type_id()).second); + REQUIRE(unique.insert(Dexode::Internal::type_id()).second); +}