Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
EventDispatcher.h
Go to the documentation of this file.
1/*
2 Author : Tobias Stein
3 Date : 10th July, 2016
4 File : EventDispatcher.h
5
6 Un/Registers subscribers for events and dispatches forwards incoming events.
7
8 All Rights Reserved. (c) Copyright 2016.
9*/
10
11#pragma once
12#ifndef ECS__EVENT_DISPATCHER_H__
13#define ECS__EVENT_DISPATCHER_H__
14
15#include "API.h"
16#include "IEventDispatcher.h"
17
18namespace ECS { namespace Event { namespace Internal {
19
20 template<class T>
22 {
24
26
27 //using PendingAddDelegates = Divide::vector<IEventDelegate*>;
29
30 //PendingAddDelegates m_PendingAddDelegates;
32
34
36
37 public:
38
39 // never use!
41 m_Locked(false)
42 {}
43
45 {
46 //this->m_PendingAddDelegates.clear();
47 this->m_PendingRemoveDelegates.clear();
48 this->m_EventCallbacks.clear();
49 }
50
51 // send event to all listener
52 inline void Dispatch(IEvent* event) override
53 {
54 this->m_Locked = true;
55 {
56 LOG_INFO("Dispatch event {}", typeid(T).name());
57
58 // remove pending delegates
59 if (this->m_PendingRemoveDelegates.empty() == false)
60 {
61 for (auto EC : this->m_PendingRemoveDelegates)
62 {
63 auto result = eastl::find_if(eastl::cbegin(m_EventCallbacks), eastl::cend(m_EventCallbacks),
64 [&](const IEventDelegate* other)
65 {
66 return other->operator==(EC);
67 });
68
69 if (result != eastl::cend(m_EventCallbacks))
70 {
71 IEventDelegate* ptrMem = (IEventDelegate*)(*result);
72
73 m_EventCallbacks.erase(result);
74
75 delete ptrMem;
76 ptrMem = nullptr;
77 }
78 }
79 this->m_PendingRemoveDelegates.clear();
80 }
81
82 for (auto EC : this->m_EventCallbacks)
83 {
84 assert(EC != nullptr && "Invalid event callback.");
85 EC->invoke(event);
86 }
87 }
88 this->m_Locked = false;
89 }
90
91 virtual void AddEventCallback(IEventDelegate* const eventDelegate) override
92 {
93 // if delegate wasn't deleted since last update, that is, delegate is still in pending list,
94 // remove it from pending list
95 auto result = eastl::find_if(this->m_PendingRemoveDelegates.begin(), this->m_PendingRemoveDelegates.end(),
96 [&](const IEventDelegate* other)
97 {
98 return other->operator==(eventDelegate);
99 });
100
101 if (result != this->m_PendingRemoveDelegates.end())
102 {
103 this->m_PendingRemoveDelegates.erase(result);
104 return;
105 }
106
107 this->m_EventCallbacks.push_back(eventDelegate);
108 }
109
110 virtual void RemoveEventCallback(IEventDelegate* eventDelegate) override
111 {
112 if (this->m_Locked == false)
113 {
114 auto result = eastl::find_if(this->m_EventCallbacks.begin(), this->m_EventCallbacks.end(),
115 [&](const IEventDelegate* other)
116 {
117 return other->operator==(eventDelegate);
118 });
119
120 if (result != this->m_EventCallbacks.end())
121 {
122 IEventDelegate* ptrMem = (IEventDelegate*)(*result);
123
124 this->m_EventCallbacks.erase(result);
125
126 delete ptrMem;
127 ptrMem = nullptr;
128 }
129 }
130 else
131 {
132 auto result = eastl::find_if(this->m_EventCallbacks.begin(), this->m_EventCallbacks.end(),
133 [&](const IEventDelegate* other)
134 {
135 return other->operator==(eventDelegate);
136 });
137
138 assert(result != this->m_EventCallbacks.end() && "");
139 this->m_PendingRemoveDelegates.push_back((*result));
140 }
141 }
142
143 virtual inline size_t GetEventCallbackCount() const override { return this->m_EventCallbacks.size(); }
144 };
145
147
148}}} // namespace ECS::Event::Internal
149
150#endif // ECS__EVENT_DISPATCHER_H__
#define DECLARE_STATIC_LOGGER
Definition: LoggerMacro.h:16
#define LOG_INFO(format,...)
Definition: LoggerMacro.h:25
#define DEFINE_STATIC_LOGGER_TEMPLATE(clazz, T, name)
Definition: LoggerMacro.h:20
PendingRemoveDelegates m_PendingRemoveDelegates
virtual size_t GetEventCallbackCount() const override
virtual void AddEventCallback(IEventDelegate *const eventDelegate) override
void Dispatch(IEvent *event) override
virtual void RemoveEventCallback(IEventDelegate *eventDelegate) override
Divide::vector< IEventDelegate * > PendingRemoveDelegates
Divide::vector< IEventDelegate * > EventDelegateList
eastl::vector< Type > vector
Definition: Vector.h:42