]> git.tdb.fi Git - libs/game.git/blobdiff - source/game/eventbus.h
Decorate things which constitute the public API of the library
[libs/game.git] / source / game / eventbus.h
index e09663589c8c99fe02180db76b94fbe6506af54b..95fc068254ebd297dc2103a4e15a6eed08fea9ba 100644 (file)
@@ -3,6 +3,8 @@
 
 #include <functional>
 #include <vector>
+#include <msp/core/noncopyable.h>
+#include "mspgame_api.h"
 
 namespace Msp::Game {
 
@@ -23,13 +25,14 @@ struct EventDispatcher
        { handlers.emplace_back(obs, std::move(cb)); }
 
        void remove_observer(EventObserver *obs)
-       { std::erase_if(handlers, [obs](const Handler &h){ return h.observer==obs; }); }
+       { std::erase_if(handlers, [obs](auto &h){ return h.observer==obs; }); }
 
        void dispatch(const T &) const;
+       void dispatch_to(EventObserver &, const T &) const;
 };
 
 
-class EventBus
+class MSPGAME_API EventBus: public NonCopyable
 {
 private:
        using DeleteFunc = void(void *);
@@ -44,6 +47,10 @@ private:
 
        std::vector<Dispatcher> dispatchers;
 
+public:
+       ~EventBus();
+
+private:
        static unsigned get_next_id();
 
 public:
@@ -52,18 +59,21 @@ public:
 
 private:
        template<typename T>
-       EventDispatcher<T> &get_emitter();
+       EventDispatcher<T> &get_dispatcher();
 
 public:
        template<typename T>
        void add_observer(EventObserver &obs, std::function<void(const T &)> cb)
-       { get_emitter<T>().add_observer(&obs, std::move(cb)); }
+       { get_dispatcher<T>().add_observer(&obs, std::move(cb)); }
 
        void replace_observer(EventObserver &, EventObserver &);
        void remove_observer(EventObserver &);
 
        template<typename T>
        void dispatch(const T &) const;
+
+       template<typename T>
+       void dispatch_to(EventObserver &, const T &) const;
 };
 
 
@@ -74,6 +84,14 @@ void EventDispatcher<T>::dispatch(const T &event) const
                h.callback(event);
 }
 
+template<typename T>
+void EventDispatcher<T>::dispatch_to(EventObserver &obs, const T &event) const
+{
+       for(const Handler &h: handlers)
+               if(h.observer==&obs)
+                       h.callback(event);
+}
+
 
 template<typename T>
 inline unsigned EventBus::get_event_id()
@@ -83,7 +101,7 @@ inline unsigned EventBus::get_event_id()
 }
 
 template<typename T>
-inline EventDispatcher<T> &EventBus::get_emitter()
+inline EventDispatcher<T> &EventBus::get_dispatcher()
 {
        unsigned id = get_event_id<T>();
        if(dispatchers.size()<=id)
@@ -108,6 +126,14 @@ inline void EventBus::dispatch(const T &event) const
                static_cast<EventDispatcher<T> *>(dispatchers[id].dispatcher)->dispatch(event);
 }
 
+template<typename T>
+inline void EventBus::dispatch_to(EventObserver &obs, const T &event) const
+{
+       unsigned id = get_event_id<T>();
+       if(id<dispatchers.size() && dispatchers[id].dispatcher)
+               static_cast<EventDispatcher<T> *>(dispatchers[id].dispatcher)->dispatch_to(obs, event);
+}
+
 } // namespace Msp::Game
 
 #endif