2 #include <samchon/API.hpp> 5 #include <unordered_set> 6 #include <unordered_map> 10 #include <condition_variable> 13 #include <samchon/library/RWMutex.hpp> 14 #include <samchon/library/Event.hpp> 53 typedef void(*Listener)(std::shared_ptr<Event>,
void*);
60 std::unordered_map<int, std::unordered_map<Listener, std::unordered_set<void*>>>
listeners;
101 listeners = move(obj.listeners);
105 std::unique_lock<std::mutex> s_uk(sMtx());
107 for (
auto it = eventMap().begin(); it != eventMap().end(); it++)
108 if (it->first == &obj)
110 auto event = it->second;
112 it = eventMap().erase(it);
113 eventMap().insert(it, {
this,
event });
123 std::unique_lock<std::mutex> s_uk(sMtx());
125 for (
auto it = eventMap().begin(); it != eventMap().end();)
126 if (it->first ==
this)
127 eventMap().erase(it++);
158 listeners[type][listener].insert(addiction);
175 if (listeners.count(type) == 0)
179 if (listeners.count(type) == 0 ||
180 listeners[type].count(listener) == 0 ||
181 listeners[type][listener].count(addiction) == 0)
184 listeners[type][listener].erase(addiction);
186 if (listeners[type][listener].empty() ==
true)
187 listeners[type].erase(listener);
189 if (listeners[type].empty() ==
true)
190 listeners.erase(type);
211 if (listeners.count(event->getType()) == 0)
216 std::unique_lock<std::mutex> s_uk(sMtx());
217 eventMap().insert({
this,
event });
223 void deliver(std::shared_ptr<Event> event)
226 if (listeners.count(event->getType()) == 0)
229 auto listenerMap = listeners[
event->getType()];
232 for (
auto it = listenerMap.begin(); it != listenerMap.end(); it++)
234 Listener listener = it->first;
236 for (
auto s_it = it->second.begin(); s_it != it->second.end(); s_it++)
238 void *addiction = *s_it;
240 listener(event, addiction);
248 static bool& started()
250 static bool flag =
false;
253 static std::condition_variable& cv()
255 static std::condition_variable obj;
258 static std::mutex& cv_mtx()
260 static std::mutex obj;
264 static std::unordered_multimap<EventDispatcher*, std::shared_ptr<Event>>& eventMap()
266 static std::unordered_multimap<EventDispatcher*, std::shared_ptr<Event>> map;
270 static std::mutex& sMtx()
272 static std::mutex obj;
278 std::unique_lock<std::mutex> uk(sMtx());
279 if (started() ==
true)
292 std::unique_lock<std::mutex> uk(sMtx());
293 if (eventMap().empty() ==
true)
296 auto pair = *eventMap().begin();
297 eventMap().erase(eventMap().begin());
302 std::shared_ptr<Event> &
event = pair.second;
307 std::unique_lock<std::mutex> cv_uk(cv_mtx());
319 static size_t val = 2;
static size_t & THREAD_SIZE()
Numer of threads for background.
void dispatch(std::shared_ptr< Event > event)
Dispatches an event to all listeners.
EventDispatcher(EventDispatcher &&obj)
Move Constructor.
std::unordered_map< int, std::unordered_map< Listener, std::unordered_set< void * > > > listeners
A container storing listeners.
EventDispatcher(const EventDispatcher &)
Copy Constructor.
void unlock() const
Unlock of read.
void removeEventListener(int type, Listener listener, void *addiction=nullptr)
Remove a registered event listener.
void unlock()
Unlock on writing.
Abstract class for dispatching Event.
virtual ~EventDispatcher()
Default Destructor.
EventDispatcher()
Default Constructor.
void addEventListener(int type, Listener listener, void *addiction=nullptr)
Register an event listener.
RWMutex mtx
A rw_mutex for concurrency.