refactor: move events and service locator into core project
This commit is contained in:
42
L2BotCore/Domain/Events/EventDispatcher.h
Normal file
42
L2BotCore/Domain/Events/EventDispatcher.h
Normal file
@@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include "Event.h"
|
||||
|
||||
namespace L2Bot::Domain::Events
|
||||
{
|
||||
class EventDispatcher
|
||||
{
|
||||
public:
|
||||
using Delegate = std::function<void(const Event&)>;
|
||||
|
||||
EventDispatcher() = default;
|
||||
virtual ~EventDispatcher() = default;
|
||||
|
||||
void Dispatch(const Event& evt)
|
||||
{
|
||||
const auto& name = evt.GetName();
|
||||
|
||||
if (m_Handlers.find(name) == m_Handlers.end())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (const auto& handler : m_Handlers[name])
|
||||
{
|
||||
handler(evt);
|
||||
}
|
||||
}
|
||||
|
||||
void Subscribe(const std::string& eventName, const Delegate handler)
|
||||
{
|
||||
m_Handlers[eventName].push_back(handler);
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<std::string, std::vector<Delegate>> m_Handlers;
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user