fix event_bus
All checks were successful
linux-arm-gcc / linux-gcc-armhf (push) Successful in 1m10s
linux-x64-gcc / linux-gcc (Release) (push) Successful in 1m23s
linux-mips64-gcc / linux-gcc-mips64el (Debug) (push) Successful in 1m30s
linux-aarch64-cpu-gcc / linux-gcc-aarch64 (push) Successful in 2m42s
linux-x64-gcc / linux-gcc (Debug) (push) Successful in 3m19s
linux-mips64-gcc / linux-gcc-mips64el (Release) (push) Successful in 1m22s

This commit is contained in:
tqcq 2024-04-04 00:19:20 +00:00
parent 65fe7acded
commit d03bc5b78d
2 changed files with 34 additions and 3 deletions

View File

@ -124,11 +124,19 @@ public:
EventRegistry<U>::Instance().Post(this, std::forward<Event>(event)); 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 &){}) // 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 typename std::enable_if<std::is_base_of<sigslot::has_slots_interface, C>::value>::type
Subscribe(C *instance, void (C::*method)(Event)) Subscribe(C *instance, void (C::*method)(Event))
{ {
using U = internal::RawType<Event>;
{ {
sled::MutexLock lock(&mutex_); sled::MutexLock lock(&mutex_);
auto iter = cleanup_handlers_.find(std::type_index(typeid(U))); auto iter = cleanup_handlers_.find(std::type_index(typeid(U)));
@ -140,9 +148,10 @@ public:
EventRegistry<U>::Instance().Subscribe(this, instance, method); 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) 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); EventRegistry<U>::Instance().Unsubscribe(this, instance);
{ {
sled::MutexLock lock(&mutex_); sled::MutexLock lock(&mutex_);

View File

@ -37,7 +37,7 @@ struct Subscriber : public sled::EventBus::Subscriber<> {
TEST_SUITE("EventBus") 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<int>>::value);
CHECK(std::is_same<int, sled::internal::RawType<const 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); 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") TEST_CASE("single thread")
{ {
sled::EventBus bus; sled::EventBus bus;
@ -142,6 +159,8 @@ TEST_SUITE("EventBus")
TEST_CASE("same type") TEST_CASE("same type")
{ {
struct Event { struct Event {
Event(int v) : a(v) {}
int a; int a;
}; };
@ -163,5 +182,8 @@ TEST_SUITE("EventBus")
bus.Post(e); bus.Post(e);
CHECK_EQ(subscriber1.a, 2); CHECK_EQ(subscriber1.a, 2);
CHECK_EQ(subscriber2.a, 2); CHECK_EQ(subscriber2.a, 2);
bus.PostTo<Event>(1);
CHECK_EQ(subscriber1.a, 3);
CHECK_EQ(subscriber2.a, 3);
} }
} }