diff --git a/src/EventBroker.cpp b/src/EventBroker.cpp new file mode 100644 index 0000000..f0b085a --- /dev/null +++ b/src/EventBroker.cpp @@ -0,0 +1,26 @@ +#include "PrecompiledHeader.h" +#include "EventBroker.h" + +BaseEventRelay::~BaseEventRelay() +{ + m_Broker->Unsubscribe(*this); +} + +void EventBroker::Unsubscribe(BaseEventRelay &relay) // ? +{ + auto itpair = m_Subscribers.equal_range(relay.m_TypeName); + for (auto it = itpair.first; it != itpair.second; ++it) + { + if (it->second == &relay) + { + m_Subscribers.erase(it); + break; + } + } +} + +void EventBroker::Subscribe(BaseEventRelay &relay) +{ + relay.m_Broker = this; + m_Subscribers.insert(std::make_pair(relay.m_TypeName, &relay)); +} \ No newline at end of file diff --git a/src/EventBroker.h b/src/EventBroker.h new file mode 100644 index 0000000..df84fd9 --- /dev/null +++ b/src/EventBroker.h @@ -0,0 +1,92 @@ +#ifndef MessageRelay_h__ +#define MessageRelay_h__ + +#include +#include +#include +#include + +struct Event +{ +protected: + Event() { } +}; + +class EventBroker; + +class BaseEventRelay +{ +friend class EventBroker; + +protected: + BaseEventRelay(std::string typeName) + : m_TypeName(typeName) { } + ~BaseEventRelay(); + +public: + virtual bool Receive(const Event &event) = 0; + +protected: + std::string m_TypeName; + EventBroker* m_Broker; +}; + +template +class EventRelay : public BaseEventRelay +{ +public: + typedef std::function CallbackType; + + EventRelay() + : m_Callback(nullptr) + , BaseEventRelay(typeid(EventType).name()) { } + EventRelay(CallbackType callback) + : m_Callback(callback) + , BaseEventRelay(typeid(EventType).name()) { } + +protected: + bool Receive(const Event &event) override; + +private: + CallbackType m_Callback; +}; + +template +bool EventRelay::Receive(const Event &event) +{ + if (m_Callback != nullptr) + { + return m_Callback(static_cast(event)); + } + else + { + return false; + } +} + +class EventBroker +{ +template friend class EventRelay; + +public: + template + void Publish(const EventType &event); + void Subscribe(BaseEventRelay &relay); + void Unsubscribe(BaseEventRelay &relay); + +private: + std::unordered_multimap m_Subscribers; +}; + + +template +void EventBroker::Publish(const EventType &event) +{ + auto itpair = m_Subscribers.equal_range(typeid(EventType).name()); + for (auto it = itpair.first; it != itpair.second; ++it) + { + it->second->Receive(event); + } +} + +#endif // MessageRelay_h__ diff --git a/src/EventRelay.h b/src/EventRelay.h deleted file mode 100644 index a7629af..0000000 --- a/src/EventRelay.h +++ /dev/null @@ -1,43 +0,0 @@ -#ifndef MessageRelay_h__ -#define MessageRelay_h__ - -#include -#include -#include - -struct Event -{ -protected: - Event() { } -}; - -class EventRelay -{ -private: - typedef std::string MessageType_t; - typedef std::function MessageCallback_t; - -public: - void Subscribe(std::string messageType, MessageCallback_t callback) - { - m_Callbacks[messageType].push_back(callback); - } - - void Raise(std::string messageType, Event message) - { - auto callbackIt = m_Callbacks.find(messageType); - if (callbackIt == m_Callbacks.end()) - return; - - for (auto &callback : callbackIt->second) - { - callback(message); - } - } - -private: - std::unordered_map> m_Callbacks; - -}; - -#endif // MessageRelay_h__ diff --git a/src/main.cpp b/src/main.cpp index 03b63b4..29f3c96 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,8 +1,36 @@ #include "PrecompiledHeader.h" #include "Engine.h" +#include "EventBroker.h" + +struct TestEvent : Event +{ + std::string Hello; +}; + +bool OnTestEvent(const TestEvent &event) +{ + std::cout << "TestEvent.Hello = " << event.Hello << std::endl; + return true; +} int main(int argc, char* argv[]) { + EventBroker broker; + + TestEvent e; + e.Hello = "Hello world"; + + { + EventRelay testRelay = &OnTestEvent; + broker.Subscribe(testRelay); + + broker.Publish(e); + } + + broker.Publish(e); + + return 0; + Engine engine(argc, argv); while (engine.Running()) engine.Tick(); diff --git a/vs11/Returngeance/Returngeance.vcxproj b/vs11/Returngeance/Returngeance.vcxproj index 64aea48..52a5283 100755 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -96,6 +96,7 @@ + @@ -140,7 +141,7 @@ - + diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index e716742..0527db8 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -50,6 +50,7 @@ + @@ -223,7 +224,7 @@ Util - +