1 #ifndef MSP_GAME_EVENTSOURCE_H_
2 #define MSP_GAME_EVENTSOURCE_H_
5 #include "eventobserver.h"
17 EventSourceBase(EventBus &b): bus(b) { }
19 virtual void remove_observer(EventObserver &) = 0;
23 template<typename... E>
24 class EventSource: public EventSourceBase, private EventDispatcher<E>...
27 EventSource(EventBus &b): EventSourceBase(b) { }
28 ~EventSource() { (cancel_observation<E>(), ...); }
31 void add_observer(EventObserver &obs, std::function<void(const T &)> cb)
32 { static_cast<EventDispatcher<T> &>(*this).add_observer(obs, std::move(cb)); }
34 void remove_observer(EventObserver &obs) override
35 { (static_cast<EventDispatcher<E> &>(*this).remove_observer(&obs), ...); }
39 void cancel_observation();
42 template<typename T, typename... Args>
43 void emit(Args &&...) const;
47 template<typename... E>
48 template<typename T, typename... Args>
49 inline void EventSource<E...>::emit(Args &&... args) const
51 T event(std::forward<Args>(args)...);
52 static_cast<const EventDispatcher<T> &>(*this).dispatch(event);
56 template<typename... E>
58 inline void EventSource<E...>::cancel_observation()
60 for(const auto &h: static_cast<EventDispatcher<T> &>(*this).handlers)
61 h.observer->remove_source(*this);
64 } // namespace Msp::Game