Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to use different events in the same listener #19

Closed
daixian opened this issue Jan 20, 2019 · 4 comments
Closed

how to use different events in the same listener #19

daixian opened this issue Jan 20, 2019 · 4 comments
Labels

Comments

@daixian
Copy link

daixian commented Jan 20, 2019

Describe the bug
hi,I use the lib in my project... but i can't to use different events in the same listener.

To Reproduce

#include "pch.h"

#include "../eventbus/EventBus.h"
#include "../eventbus/EventCollector.h"

std::shared_ptr<Dexode::EventBus> bus = std::make_shared<Dexode::EventBus>();

namespace Event {
    struct Event1
    {
        int value = 0;
    };

    struct Event2
    {
        int value = 0;
    };
}

bool isEnter;
int enterCount;

void onEvent1(const Event::Event1& e)
{
    std::cout << "enter envent 1 " << std::endl;
    isEnter = true;
    enterCount++;
}
void onEvent2(const Event::Event2& e)
{
    std::cout << "enter envent 2 " << std::endl;
    isEnter = true;
    enterCount++;
}


TEST(Eventbus, Event1_Event2)
{
    //reset flag
    isEnter = false;
    enterCount = 0;

    Dexode::EventCollector _listener(bus);

    _listener.listen<Event::Event1>(std::bind(&onEvent1, std::placeholders::_1));
    _listener.listen<Event::Event2>(std::bind(&onEvent2, std::placeholders::_1));

    bus->notify(Event::Event1{ 1 });

    EXPECT_TRUE(isEnter);//pass
    EXPECT_TRUE(enterCount == 1);//fail! enterCount=2
}

Expected behavior
it will enter onEvent1() and onEvent2()...but I need it only enters onEvent1().

Build:

  • compiler: [vs2017]
  • Link type [I put all src file of the lib in my project...]
@gelldur
Copy link
Owner

gelldur commented Jan 20, 2019

Hello, nice to hear that.

It is odd I just tested this case and it works fine. Maybe you have other test that is running same time ?
I will check it on Windows in few mins.

@gelldur
Copy link
Owner

gelldur commented Jan 20, 2019

So after checking it on windows online compiler

You are right there is problem :/ first of all you need enable RTTI because assert can fail.
Ticket for that: #20

source_file.cpp(485): error C2440: 'initializing': cannot convert from 'initializer list' to 'Event::Event1'
source_file.cpp(485): note: No constructor could take the source type, or constructor overload resolution was ambiguous
source_file.cpp(485): error C2672: 'Dexode::EventBus::notify': no matching overloaded function found
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x64

Other issue is that Internal::type_id<Event> isn't returning unique ID (odd) so on VS I get:

type_id: 00007FF759676F00 for Event1
type_id: 00007FF759676F00 for Event2

This is because VS do some optimization trick.

So I will fix that asap ;)

gelldur added a commit that referenced this issue Jan 20, 2019
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.
@gelldur
Copy link
Owner

gelldur commented Jan 20, 2019

Fixed in commit: 34902c4
Version 2.4.1

Thanks!
I need to fix VS build on CI :)

@daixian
Copy link
Author

daixian commented Jan 21, 2019

Yes, it is working fine in my project now. thanks! :)

@gelldur gelldur closed this as completed Jan 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants