Fix type_id<T>() helper function for Visual Studio

Issue: #19

Visual Studio optimize not so portable version of type_id<T>()
so each time we get same id for different types.

Added small test to check this behavior in future.
This commit is contained in:
Dawid Drozd 2019-01-20 20:39:07 +01:00
parent d10e3bf57b
commit 34902c481d
6 changed files with 66 additions and 9 deletions

View File

@ -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
)

View File

@ -79,7 +79,7 @@ public:
assert(callback && "callback should be valid"); //Check for valid object
std::unique_ptr<Internal::CallbackVector>& vector =
_callbacks[Internal::type_id<Event>];
_callbacks[Internal::type_id<Event>()];
if(vector == nullptr)
{
vector.reset(new Vector{});
@ -150,7 +150,7 @@ public:
std::lock_guard<std::mutex> guard{_callbacksMutex};
using Vector = Internal::AsyncCallbackVector<Event>;
auto found = _callbacks.find(Internal::type_id<Event>);
auto found = _callbacks.find(Internal::type_id<Event>());
if(found == _callbacks.end())
{
return; // no such notifications

View File

@ -59,7 +59,7 @@ public:
assert(callback && "callback should be valid"); //Check for valid object
std::unique_ptr<Internal::CallbackVector>& vector = _callbacks[Internal::type_id<Event>];
std::unique_ptr<Internal::CallbackVector>& vector = _callbacks[Internal::type_id<Event>()];
if(vector == nullptr)
{
vector.reset(new Vector{});
@ -89,7 +89,7 @@ public:
{
static_assert(Internal::validateEvent<Event>(), "Invalid event");
auto found = _callbacks.find(Internal::type_id<Event>);
auto found = _callbacks.find(Internal::type_id<Event>());
if(found != _callbacks.end())
{
found->second->remove(token);
@ -108,7 +108,7 @@ public:
static_assert(Internal::validateEvent<Event>(), "Invalid event");
using Vector = Internal::TransactionCallbackVector<CleanEventType>;
auto found = _callbacks.find(Internal::type_id<CleanEventType>);
auto found = _callbacks.find(Internal::type_id<CleanEventType>());
if(found == _callbacks.end())
{
return; // no such notifications

View File

@ -7,12 +7,15 @@ namespace Dexode
namespace Internal
{
template <typename>
void type_id() // Helper for getting "type id"
using type_id_t = std::size_t;
template <typename T>
type_id_t type_id() // Helper for getting "type id"
{
return typeid(T).hash_code();
}
using type_id_t = void (*)(); // Function pointer
template <class Event>
constexpr bool validateEvent()

View File

@ -21,6 +21,7 @@ find_package(Threads REQUIRED)
add_executable(EventBusTest
src/AsyncEventBusTest.cpp
src/EventCollectorTest.cpp
src/EventIdTest.cpp
src/NotifierTest.cpp
)

53
test/src/EventIdTest.cpp Normal file
View File

@ -0,0 +1,53 @@
#include <catch2/catch.hpp>
#include <set>
#include <eventbus/internal/common.h>
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<Event>", "[EventId]")
{
std::set<Dexode::Internal::type_id_t> unique;
REQUIRE(unique.insert(Dexode::Internal::type_id<Anonymous>()).second);
REQUIRE_FALSE(unique.insert(Dexode::Internal::type_id<Anonymous>()).second); //already there
struct TestA
{};
::TestA a;
a.a = 10;
REQUIRE(unique.insert(Dexode::Internal::type_id<TestA>()).second);
REQUIRE(unique.insert(Dexode::Internal::type_id<::TestA>()).second);
REQUIRE(unique.insert(Dexode::Internal::type_id<Test::TestA>()).second);
REQUIRE(unique.insert(Dexode::Internal::type_id<Test::TestN::TestA>()).second);
}