Samchon Framework for CPP  1.0.0
EventDispatcher.hpp
1 #pragma once
2 #include <samchon/API.hpp>
3 
4 #include <functional>
5 #include <unordered_set>
6 #include <unordered_map>
7 
8 #include <memory>
9 #include <queue>
10 
11 #include <condition_variable>
12 #include <mutex>
13 #include <samchon/library/RWMutex.hpp>
14 
15 namespace samchon
16 {
17 namespace library
18 {
19  class Event;
20 
51  class SAMCHON_FRAMEWORK_API EventDispatcher
52  {
53  public:
54  typedef void(*Listener)(std::shared_ptr<Event>, void*);
55  // typedef std::function<void(std::shared_ptr<Event>, void*)> Listener;
56 
57  private:
61  std::unordered_map<int, std::unordered_map<Listener, std::unordered_set<void*>>> listeners;
62 
67 
68  public:
69  /* ----------------------------------------------------------
70  CONSTRUCTORS
71  ---------------------------------------------------------- */
76 
87 
94 
98  virtual ~EventDispatcher();
99 
100  /* ----------------------------------------------------------
101  ADD-REMOVE EVENT LISTENERS
102  ---------------------------------------------------------- */
122  void addEventListener(int, Listener, void* = nullptr);
123 
135  void removeEventListener(int, Listener, void* = nullptr);
136 
147  void dispatch(std::shared_ptr<Event>);
148 
149  private:
150  void deliver(std::shared_ptr<Event>);
151 
152  /* ----------------------------------------------------------
153  MEMBERS OF STATIC
154  ---------------------------------------------------------- */
155  static bool started;
156  static std::condition_variable cv;
157  static std::mutex cv_mtx;
158 
159  static std::unordered_multimap<EventDispatcher*, std::shared_ptr<Event>> eventMap;
160  static std::mutex sMtx;
161 
162  static void start();
163 
164  public:
168  static size_t THREAD_SIZE;
169  };
170 };
171 };
std::unordered_map< int, std::unordered_map< Listener, std::unordered_set< void * > > > listeners
A container storing listeners.
Abstract class for dispatching Event.
RWMutex mtx
A rw_mutex for concurrency.
static size_t THREAD_SIZE
Numer of threads for background.
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7