]> git.tdb.fi Git - libs/game.git/blobdiff - source/game/eventsource.h
Add unit tests for the event bus
[libs/game.git] / source / game / eventsource.h
index 5dacbeeed71603ec0418f3245a7fc82c39e449c5..1748f156f16588629b410dc030ac9326026ad487 100644 (file)
@@ -29,7 +29,7 @@ public:
 
        template<typename T>
        void add_observer(EventObserver &obs, std::function<void(const T &)> cb)
-       { static_cast<EventDispatcher<T> &>(*this).add_observer(obs, std::move(cb)); }
+       { static_cast<EventDispatcher<T> &>(*this).add_observer(&obs, std::move(cb)); }
 
        void remove_observer(EventObserver &obs) override
        { (static_cast<EventDispatcher<E> &>(*this).remove_observer(&obs), ...); }
@@ -41,9 +41,20 @@ private:
 public:
        template<typename T, typename... Args>
        void emit(Args &&...) const;
+
+       template<typename T, typename... Args>
+       void emit_to(EventObserver &, Args &&...) const;
 };
 
 
+template<typename... E>
+template<typename T>
+inline void EventSource<E...>::cancel_observation()
+{
+       for(const auto &h: static_cast<EventDispatcher<T> &>(*this).handlers)
+               h.observer->remove_source(*this);
+}
+
 template<typename... E>
 template<typename T, typename... Args>
 inline void EventSource<E...>::emit(Args &&... args) const
@@ -54,11 +65,12 @@ inline void EventSource<E...>::emit(Args &&... args) const
 }
 
 template<typename... E>
-template<typename T>
-inline void EventSource<E...>::cancel_observation()
+template<typename T, typename... Args>
+inline void EventSource<E...>::emit_to(EventObserver &obs, Args &&... args) const
 {
-       for(const auto &h: static_cast<EventDispatcher<T> &>(*this).handlers)
-               h.observer->remove_source(*this);
+       T event(std::forward<Args>(args)...);
+       static_cast<const EventDispatcher<T> &>(*this).dispatch_to(obs, event);
+       bus.dispatch_to(obs, event);
 }
 
 } // namespace Msp::Game