blob: 0e3097012c9aab95a0bc9a2760fc324140447e01 [file] [log] [blame]
Junxiao Shi1a2a8562014-01-23 10:07:59 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -08007#ifndef NFD_UTIL_EVENT_EMITTER_HPP
8#define NFD_UTIL_EVENT_EMITTER_HPP
9
10#include "common.hpp"
11
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080012namespace nfd {
Junxiao Shi1a2a8562014-01-23 10:07:59 -070013
14struct empty {};
15
16/** \class EventEmitter
17 * \brief provides a lightweight event system
18 *
19 * To declare an event:
20 * EventEmitter<TArgs> m_eventName;
21 * To subscribe to an event:
22 * eventSource->m_eventName += eventHandler;
23 * Multiple functions can subscribe to the same event.
24 * To trigger an event:
25 * m_eventName(args);
26 * To clear event subscriptions:
27 * m_eventName.clear();
28 */
29
30// four arguments
31template<typename T1 = empty, typename T2 = empty,
32 typename T3 = empty, typename T4 = empty>
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080033class EventEmitter : noncopyable
Junxiao Shi1a2a8562014-01-23 10:07:59 -070034{
35public:
36 /// represents a handler that can subscribe to the event
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080037 typedef function<void(const T1&, const T2&,
Junxiao Shi1a2a8562014-01-23 10:07:59 -070038 const T3&, const T4&)> Handler;
39
40 /// adds an subscription
41 void
42 operator+=(Handler handler);
43
44 /// returns true if there is no subscription,
45 /// otherwise returns false
46 bool
47 isEmpty();
48
49 /// clears all subscriptions
50 void
51 clear();
52
53 /// triggers the event
54 void
55 operator()(const T1& a1, const T2& a2, const T3& a3, const T4& a4);
56
57private:
58 /// stores all subscribed handlers
59 std::vector<Handler> m_handlers;
60};
61
62// zero argument
63template<>
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080064class EventEmitter<empty, empty, empty, empty> : noncopyable
Junxiao Shi1a2a8562014-01-23 10:07:59 -070065{
66public:
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080067 typedef function<void()> Handler;
Junxiao Shi1a2a8562014-01-23 10:07:59 -070068
69 void
70 operator+=(Handler handler);
71
72 bool
73 isEmpty();
74
75 void
76 clear();
77
78 void
79 operator()();
80
81private:
82 std::vector<Handler> m_handlers;
83};
84
85
86// one argument
87template<typename T1>
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080088class EventEmitter<T1, empty, empty, empty> : noncopyable
Junxiao Shi1a2a8562014-01-23 10:07:59 -070089{
90public:
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080091 typedef function<void(const T1&)> Handler;
Junxiao Shi1a2a8562014-01-23 10:07:59 -070092
93 void
94 operator+=(Handler handler);
95
96 bool
97 isEmpty();
98
99 void
100 clear();
101
102 void
103 operator()(const T1& a1);
104
105private:
106 std::vector<Handler> m_handlers;
107};
108
109
110// two arguments
111template<typename T1, typename T2>
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800112class EventEmitter<T1, T2, empty, empty> : noncopyable
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700113{
114public:
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800115 typedef function<void(const T1&, const T2&)> Handler;
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700116
117 void
118 operator+=(Handler handler);
119
120 bool
121 isEmpty();
122
123 void
124 clear();
125
126 void
127 operator()(const T1& a1, const T2& a2);
128
129private:
130 std::vector<Handler> m_handlers;
131};
132
133
134// three arguments
135template<typename T1, typename T2, typename T3>
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800136class EventEmitter<T1, T2, T3, empty> : noncopyable
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700137{
138public:
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800139 typedef function<void(const T1&, const T2&, const T3&)> Handler;
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700140
141 void
142 operator+=(Handler handler);
143
144 bool
145 isEmpty();
146
147 void
148 clear();
149
150 void
151 operator()(const T1& a1, const T2& a2, const T3& a3);
152
153private:
154 std::vector<Handler> m_handlers;
155};
156
157
158// zero argument
159
160inline void
161EventEmitter<empty, empty, empty, empty>::operator+=(Handler handler)
162{
163 m_handlers.push_back(handler);
164}
165
166inline bool
167EventEmitter<empty, empty, empty, empty>::isEmpty()
168{
169 return m_handlers.empty();
170}
171
172inline void
173EventEmitter<empty, empty, empty, empty>::clear()
174{
175 return m_handlers.clear();
176}
177
178inline void
179EventEmitter<empty, empty, empty, empty>::operator()()
180{
181 std::vector<Handler>::iterator it;
182 for (it = m_handlers.begin(); it != m_handlers.end(); ++it) {
183 (*it)();
184 }
185}
186
187// one argument
188
189template<typename T1>
190inline void
191EventEmitter<T1, empty, empty, empty>::operator+=(Handler handler)
192{
193 m_handlers.push_back(handler);
194}
195
196template<typename T1>
197inline bool
198EventEmitter<T1, empty, empty, empty>::isEmpty()
199{
200 return m_handlers.empty();
201}
202
203template<typename T1>
204inline void
205EventEmitter<T1, empty, empty, empty>::clear()
206{
207 return m_handlers.clear();
208}
209
210template<typename T1>
211inline void
212EventEmitter<T1, empty, empty, empty>::operator()(const T1& a1)
213{
214 typename std::vector<Handler>::iterator it;
215 for (it = m_handlers.begin(); it != m_handlers.end(); ++it) {
216 (*it)(a1);
217 }
218}
219
220// two arguments
221
222template<typename T1, typename T2>
223inline void
224EventEmitter<T1, T2, empty, empty>::operator+=(Handler handler)
225{
226 m_handlers.push_back(handler);
227}
228
229template<typename T1, typename T2>
230inline bool
231EventEmitter<T1, T2, empty, empty>::isEmpty()
232{
233 return m_handlers.empty();
234}
235
236template<typename T1, typename T2>
237inline void
238EventEmitter<T1, T2, empty, empty>::clear()
239{
240 return m_handlers.clear();
241}
242
243template<typename T1, typename T2>
244inline void
245EventEmitter<T1, T2, empty, empty>::operator()
246 (const T1& a1, const T2& a2)
247{
248 typename std::vector<Handler>::iterator it;
249 for (it = m_handlers.begin(); it != m_handlers.end(); ++it) {
250 (*it)(a1, a2);
251 }
252}
253
254// three arguments
255
256template<typename T1, typename T2, typename T3>
257inline void
258EventEmitter<T1, T2, T3, empty>::operator+=(Handler handler)
259{
260 m_handlers.push_back(handler);
261}
262
263template<typename T1, typename T2, typename T3>
264inline bool
265EventEmitter<T1, T2, T3, empty>::isEmpty()
266{
267 return m_handlers.empty();
268}
269
270template<typename T1, typename T2, typename T3>
271inline void
272EventEmitter<T1, T2, T3, empty>::clear()
273{
274 return m_handlers.clear();
275}
276
277template<typename T1, typename T2, typename T3>
278inline void
279EventEmitter<T1, T2, T3, empty>::operator()
280 (const T1& a1, const T2& a2, const T3& a3)
281{
282 typename std::vector<Handler>::iterator it;
283 for (it = m_handlers.begin(); it != m_handlers.end(); ++it) {
284 (*it)(a1, a2, a3);
285 }
286}
287
288// four arguments
289
290template<typename T1, typename T2, typename T3, typename T4>
291inline void
292EventEmitter<T1, T2, T3, T4>::operator+=(Handler handler)
293{
294 m_handlers.push_back(handler);
295}
296
297template<typename T1, typename T2, typename T3, typename T4>
298inline bool
299EventEmitter<T1, T2, T3, T4>::isEmpty()
300{
301 return m_handlers.empty();
302}
303
304template<typename T1, typename T2, typename T3, typename T4>
305inline void
306EventEmitter<T1, T2, T3, T4>::clear()
307{
308 return m_handlers.clear();
309}
310
311template<typename T1, typename T2, typename T3, typename T4>
312inline void
313EventEmitter<T1, T2, T3, T4>::operator()
314 (const T1& a1, const T2& a2, const T3& a3, const T4& a4)
315{
316 typename std::vector<Handler>::iterator it;
317 for (it = m_handlers.begin(); it != m_handlers.end(); ++it) {
318 (*it)(a1, a2, a3, a4);
319 }
320}
321
322
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800323} // namespace nfd
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800324
325#endif // NFD_UTIL_EVENT_EMITTER_HPP