working dispatcher + factory
All checks were successful
Linux arm64 / Build (push) Successful in 1m17s
All checks were successful
Linux arm64 / Build (push) Successful in 1m17s
This commit is contained in:
43
include/sp/protocol/MessageDispatcherImpl.inl
Normal file
43
include/sp/protocol/MessageDispatcherImpl.inl
Normal file
@@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
|
||||
namespace sp {
|
||||
|
||||
template <typename MessageBase>
|
||||
void MessageDispatcher<MessageBase>::RegisterHandler(MessageIdType a_MessageType, MessageHandler* a_Handler) {
|
||||
assert(a_Handler);
|
||||
auto found = std::find(m_Handlers[a_MessageType].begin(), m_Handlers[a_MessageType].end(), a_Handler);
|
||||
if (found == m_Handlers[a_MessageType].end())
|
||||
m_Handlers[a_MessageType].push_back(a_Handler);
|
||||
}
|
||||
|
||||
template <typename MessageBase>
|
||||
void MessageDispatcher<MessageBase>::UnregisterHandler(MessageIdType a_MessageType, MessageHandler* a_Handler) {
|
||||
auto found = std::find(m_Handlers[a_MessageType].begin(), m_Handlers[a_MessageType].end(), a_Handler);
|
||||
if (found != m_Handlers[a_MessageType].end())
|
||||
m_Handlers[a_MessageType].erase(found);
|
||||
}
|
||||
|
||||
template <typename MessageBase>
|
||||
void MessageDispatcher<MessageBase>::UnregisterHandler(MessageHandler* a_Handler) {
|
||||
for (auto& pair : m_Handlers) {
|
||||
if (pair.second.empty())
|
||||
continue;
|
||||
|
||||
MessageIdType type = pair.first;
|
||||
|
||||
pair.second.erase(std::remove(pair.second.begin(), pair.second.end(), a_Handler), pair.second.end());
|
||||
}
|
||||
}
|
||||
|
||||
template <typename MessageBase>
|
||||
void MessageDispatcher<MessageBase>::Dispatch(const MessageBase& a_Message) {
|
||||
MessageIdType type = a_Message.GetId();
|
||||
for (auto& handler : m_Handlers[type]) {
|
||||
a_Message.Dispatch(*handler);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace sp
|
||||
Reference in New Issue
Block a user