12#ifndef ECS__EVENT_HANDLER_H__
13#define ECS__EVENT_HANDLER_H__
23namespace ECS {
namespace Event {
57 EventDispatcherMap::const_iterator iter = this->m_EventDispatcherMap.find(ETID);
58 if (iter == this->m_EventDispatcherMap.end())
62 kvp.second->AddEventCallback(eventDelegate);
64 this->m_EventDispatcherMap.insert(kvp);
68 this->m_EventDispatcherMap[ETID]->AddEventCallback(eventDelegate);
77 const EventDispatcherMap::const_iterator iter = this->m_EventDispatcherMap.find(typeId);
78 if (iter != this->m_EventDispatcherMap.end())
80 this->m_EventDispatcherMap[typeId]->RemoveEventCallback(eventDelegate);
94 this->m_EventMemoryAllocator->
clear();
95 this->m_EventStorage.clear();
100 this->m_EventDispatcherMap.clear();
103 template<
class E,
class... ARGS>
107 static_assert(std::is_trivially_copyable<E>::value,
"Event is not trivially copyable.");
111 void* pMem = this->m_EventMemoryAllocator->
allocate(
sizeof(E),
alignof(E));
116 this->m_EventStorage.push_back(
new (pMem)E(engine,
FWD(eventArgs)...));
118 LOG_INFO(
"\'{}\' event buffered.",
typeid(E).name());
122 LOG_WARNING(
"Event buffer is full! Cut off new incoming events !!!");
126 template<
class E,
class... ARGS>
129 void* pMem = this->m_EventMemoryAllocator->
allocate(
sizeof(E),
alignof(E));
130 auto event =
new (pMem)E(engine,
FWD(eventArgs)...);
131 auto it = this->m_EventDispatcherMap.find(event->GetEventTypeID());
132 if (it != eastl::cend(this->m_EventDispatcherMap)) {
133 it->second->Dispatch(event);
142 size_t lastIndex = this->m_EventStorage.size();
143 size_t thisIndex = 0;
145 while (thisIndex < lastIndex)
147 IEvent*
event = this->m_EventStorage[thisIndex++];
148 if (event !=
nullptr) {
149 const auto it = this->m_EventDispatcherMap.find(event->GetEventTypeID());
150 if (it != eastl::cend(this->m_EventDispatcherMap)) {
151 it->second->Dispatch(event);
153 lastIndex = this->m_EventStorage.size();
156 LOG_ERROR(
"Skip corrupted event [ {} ]", event->GetEventTypeID());
#define LOG_WARNING(format,...)
#define LOG_INFO(format,...)
#define LOG_ERROR(format,...)
#define PROFILE_SCOPE_AUTO(CATEGORY)
EventHandler & operator=(EventHandler &)=delete
void RemoveEventCallback(Internal::IEventDelegate *eventDelegate)
EventStorage m_EventStorage
void SendAndDispatchEvent(ECSEngine *engine, ARGS &&... eventArgs)
void ClearEventDispatcher()
EventMemoryAllocator * m_EventMemoryAllocator
Divide::hashMap< EventTypeId, Internal::IEventDispatcher * > EventDispatcherMap
Divide::vector< IEvent * > EventStorage
void AddEventCallback(Internal::IEventDelegate *const eventDelegate)
EventDispatcherMap m_EventDispatcherMap
DECLARE_LOGGER EventHandler(const EventHandler &)=delete
void Send(ECSEngine *engine, ARGS &&... eventArgs)
virtual u64 GetStaticEventTypeId() const =0
virtual void clear() override
virtual void * allocate(size_t size, u8 alignment) override
constexpr Optick::Category::Type GameLogic
eastl::vector< Type > vector
hashAlg::unordered_map< K, V, HashFun, Predicate > hashMap