Commit d03bc5b7 authored by tqcq's avatar tqcq
Browse files

fix event_bus

parent 65fe7acd
Loading
Loading
Loading
Loading
+11 −2
Original line number Diff line number Diff line
@@ -124,11 +124,19 @@ public:
        EventRegistry<U>::Instance().Post(this, std::forward<Event>(event));
    }

    template<typename Event, typename From>
    void PostTo(From &&value)
    {
        using U = internal::RawType<Event>;
        EventRegistry<U>::Instance().Post(this, std::forward<From>(value));
    }

    // On<Event1> ([](const Event1 &){})
    template<typename Event, typename C, typename U = internal::RawType<Event>>
    template<typename Event, typename C>
    typename std::enable_if<std::is_base_of<sigslot::has_slots_interface, C>::value>::type
    Subscribe(C *instance, void (C::*method)(Event))
    {
        using U = internal::RawType<Event>;
        {
            sled::MutexLock lock(&mutex_);
            auto iter = cleanup_handlers_.find(std::type_index(typeid(U)));
@@ -140,9 +148,10 @@ public:
        EventRegistry<U>::Instance().Subscribe(this, instance, method);
    }

    template<typename Event, typename C, typename U = internal::RawType<Event>>
    template<typename Event, typename C>
    typename std::enable_if<std::is_base_of<sigslot::has_slots_interface, C>::value>::type Unsubscribe(C *instance)
    {
        using U = internal::RawType<Event>;
        EventRegistry<U>::Instance().Unsubscribe(this, instance);
        {
            sled::MutexLock lock(&mutex_);
+23 −1
Original line number Diff line number Diff line
@@ -37,7 +37,7 @@ struct Subscriber : public sled::EventBus::Subscriber<> {

TEST_SUITE("EventBus")
{
    TEST_CASE("RawType")
    TEST_CASE("RawType built-in type")
    {
        CHECK(std::is_same<int, sled::internal::RawType<int>>::value);
        CHECK(std::is_same<int, sled::internal::RawType<const int>>::value);
@@ -52,6 +52,23 @@ TEST_SUITE("EventBus")
        CHECK(std::is_same<int, sled::internal::RawType<volatile int &&>>::value);
    }

    TEST_CASE("RawType user-defined type")
    {
        struct A {};

        CHECK(std::is_same<A, sled::internal::RawType<A>>::value);
        CHECK(std::is_same<A, sled::internal::RawType<const A>>::value);
        CHECK(std::is_same<A, sled::internal::RawType<const A &>>::value);
        CHECK(std::is_same<A, sled::internal::RawType<A &>>::value);
        CHECK(std::is_same<A, sled::internal::RawType<A &&>>::value);

        CHECK(std::is_same<A, sled::internal::RawType<volatile A>>::value);
        CHECK(std::is_same<A, sled::internal::RawType<volatile const A>>::value);
        CHECK(std::is_same<A, sled::internal::RawType<volatile const A &>>::value);
        CHECK(std::is_same<A, sled::internal::RawType<volatile A &>>::value);
        CHECK(std::is_same<A, sled::internal::RawType<volatile A &&>>::value);
    }

    TEST_CASE("single thread")
    {
        sled::EventBus bus;
@@ -142,6 +159,8 @@ TEST_SUITE("EventBus")
    TEST_CASE("same type")
    {
        struct Event {
            Event(int v) : a(v) {}

            int a;
        };

@@ -163,5 +182,8 @@ TEST_SUITE("EventBus")
        bus.Post(e);
        CHECK_EQ(subscriber1.a, 2);
        CHECK_EQ(subscriber2.a, 2);
        bus.PostTo<Event>(1);
        CHECK_EQ(subscriber1.a, 3);
        CHECK_EQ(subscriber2.a, 3);
    }
}