diff --git a/src/sled/event_bus/event_bus.h b/src/sled/event_bus/event_bus.h index 48f6c9d..27ad886 100644 --- a/src/sled/event_bus/event_bus.h +++ b/src/sled/event_bus/event_bus.h @@ -1,6 +1,7 @@ #ifndef SLED_EVENT_BUS_EVENT_BUS_H #define SLED_EVENT_BUS_EVENT_BUS_H +#include "sled/exec/detail/invoke_result.h" #include "sled/sigslot.h" #include "sled/synchronization/mutex.h" #include @@ -13,7 +14,10 @@ class Subscriber; namespace internal { template using RawType = typename std::remove_cv::type>::type; -} +template +using EnableNotVolatile = typename std::enable_if::value>; +// using RawType = typename std::remove_const::type>::type; +}// namespace internal namespace { @@ -36,16 +40,16 @@ public: return cleanup_handler; } - void Post(EventBus *bus, Event event) + void Post(EventBus *bus, const Event &event) { sled::SharedMutexReadLock lock(&shared_mutex_); if (signals_.empty()) { return; } auto iter = signals_.find(bus); - if (iter != signals_.end()) { iter->second(std::forward(event)); } + if (iter != signals_.end()) { iter->second(event); } } - template - void Subscribe(EventBus *bus, C *instance, void (C::*method)(Event)) + template + void Subscribe(EventBus *bus, C *instance, F &&method) { sled::SharedMutexWriteLock lock(&shared_mutex_); auto iter = signals_.find(bus); @@ -118,16 +122,17 @@ public: EventBus(const EventBus &) = delete; EventBus &operator=(const EventBus &) = delete; - template> + template void Post(Event &&event) { + using U = typename internal::RawType; EventRegistry::Instance().Post(this, std::forward(event)); } template void PostTo(From &&value) { - using U = internal::RawType; + using U = typename internal::RawType; EventRegistry::Instance().Post(this, std::forward(value)); } @@ -136,7 +141,7 @@ public: typename std::enable_if::value>::type Subscribe(C *instance, void (C::*method)(Event)) { - using U = internal::RawType; + using U = typename internal::RawType; { sled::MutexLock lock(&mutex_); auto iter = cleanup_handlers_.find(std::type_index(typeid(U))); @@ -151,7 +156,7 @@ public: template typename std::enable_if::value>::type Unsubscribe(C *instance) { - using U = internal::RawType; + using U = typename internal::RawType; EventRegistry::Instance().Unsubscribe(this, instance); { sled::MutexLock lock(&mutex_); diff --git a/src/sled/event_bus/event_bus_test.cc b/src/sled/event_bus/event_bus_test.cc index 1c7c9ba..51ad8c9 100644 --- a/src/sled/event_bus/event_bus_test.cc +++ b/src/sled/event_bus/event_bus_test.cc @@ -186,4 +186,42 @@ TEST_SUITE("EventBus") CHECK_EQ(subscriber1.a, 3); CHECK_EQ(subscriber2.a, 3); } + + TEST_CASE("msg convert test") + { + struct Msg { + int value; + }; + + std::atomic counter; + + struct SubscribeCounter : public sled::EventBus::Subscriber<> { + SubscribeCounter(std::atomic *counter) : counter_(counter) {} + + void OnEvent(Msg msg) { counter_->fetch_add(msg.value); } + + std::atomic *counter_; + }; + + SubscribeCounter subscriber{&counter}; + sled::EventBus bus; + bus.Subscribe(&subscriber, &SubscribeCounter::OnEvent); + + Msg msg{1}; + bus.Post(msg); + CHECK_EQ(counter.load(), 1); + + bus.Post(const_cast(msg)); + CHECK_EQ(counter.load(), 2); + + bus.Post(std::move(msg)); + CHECK_EQ(counter.load(), 3); + + bus.Post(const_cast(msg)); + CHECK_EQ(counter.load(), 4); + + // volatile Msg volatile_msg{1}; + // bus.Post(volatile_msg); + // CHECK_EQ(counter.load(), 4); + } }