blob: fa7de00f5f7a9bba15cfeebdbfa2973e7437f80e [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
7#ifndef NFD_UTIL_EVENT_EMITTER_H
8#define NFD_UTIL_EVENT_EMITTER_H
9#include <vector>
10#include <boost/utility.hpp>
11#include <boost/function.hpp>
12namespace ndn {
13
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>
33class EventEmitter : boost::noncopyable
34{
35public:
36 /// represents a handler that can subscribe to the event
37 typedef boost::function<void(const T1&, const T2&,
38 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<>
64class EventEmitter<empty, empty, empty, empty> : boost::noncopyable
65{
66public:
67 typedef boost::function<void()> Handler;
68
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>
88class EventEmitter<T1, empty, empty, empty> : boost::noncopyable
89{
90public:
91 typedef boost::function<void(const T1&)> Handler;
92
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>
112class EventEmitter<T1, T2, empty, empty> : boost::noncopyable
113{
114public:
115 typedef boost::function<void(const T1&, const T2&)> Handler;
116
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>
136class EventEmitter<T1, T2, T3, empty> : boost::noncopyable
137{
138public:
139 typedef boost::function<void(const T1&, const T2&,
140 const T3&)> Handler;
141
142 void
143 operator+=(Handler handler);
144
145 bool
146 isEmpty();
147
148 void
149 clear();
150
151 void
152 operator()(const T1& a1, const T2& a2, const T3& a3);
153
154private:
155 std::vector<Handler> m_handlers;
156};
157
158
159// zero argument
160
161inline void
162EventEmitter<empty, empty, empty, empty>::operator+=(Handler handler)
163{
164 m_handlers.push_back(handler);
165}
166
167inline bool
168EventEmitter<empty, empty, empty, empty>::isEmpty()
169{
170 return m_handlers.empty();
171}
172
173inline void
174EventEmitter<empty, empty, empty, empty>::clear()
175{
176 return m_handlers.clear();
177}
178
179inline void
180EventEmitter<empty, empty, empty, empty>::operator()()
181{
182 std::vector<Handler>::iterator it;
183 for (it = m_handlers.begin(); it != m_handlers.end(); ++it) {
184 (*it)();
185 }
186}
187
188// one argument
189
190template<typename T1>
191inline void
192EventEmitter<T1, empty, empty, empty>::operator+=(Handler handler)
193{
194 m_handlers.push_back(handler);
195}
196
197template<typename T1>
198inline bool
199EventEmitter<T1, empty, empty, empty>::isEmpty()
200{
201 return m_handlers.empty();
202}
203
204template<typename T1>
205inline void
206EventEmitter<T1, empty, empty, empty>::clear()
207{
208 return m_handlers.clear();
209}
210
211template<typename T1>
212inline void
213EventEmitter<T1, empty, empty, empty>::operator()(const T1& a1)
214{
215 typename std::vector<Handler>::iterator it;
216 for (it = m_handlers.begin(); it != m_handlers.end(); ++it) {
217 (*it)(a1);
218 }
219}
220
221// two arguments
222
223template<typename T1, typename T2>
224inline void
225EventEmitter<T1, T2, empty, empty>::operator+=(Handler handler)
226{
227 m_handlers.push_back(handler);
228}
229
230template<typename T1, typename T2>
231inline bool
232EventEmitter<T1, T2, empty, empty>::isEmpty()
233{
234 return m_handlers.empty();
235}
236
237template<typename T1, typename T2>
238inline void
239EventEmitter<T1, T2, empty, empty>::clear()
240{
241 return m_handlers.clear();
242}
243
244template<typename T1, typename T2>
245inline void
246EventEmitter<T1, T2, empty, empty>::operator()
247 (const T1& a1, const T2& a2)
248{
249 typename std::vector<Handler>::iterator it;
250 for (it = m_handlers.begin(); it != m_handlers.end(); ++it) {
251 (*it)(a1, a2);
252 }
253}
254
255// three arguments
256
257template<typename T1, typename T2, typename T3>
258inline void
259EventEmitter<T1, T2, T3, empty>::operator+=(Handler handler)
260{
261 m_handlers.push_back(handler);
262}
263
264template<typename T1, typename T2, typename T3>
265inline bool
266EventEmitter<T1, T2, T3, empty>::isEmpty()
267{
268 return m_handlers.empty();
269}
270
271template<typename T1, typename T2, typename T3>
272inline void
273EventEmitter<T1, T2, T3, empty>::clear()
274{
275 return m_handlers.clear();
276}
277
278template<typename T1, typename T2, typename T3>
279inline void
280EventEmitter<T1, T2, T3, empty>::operator()
281 (const T1& a1, const T2& a2, const T3& a3)
282{
283 typename std::vector<Handler>::iterator it;
284 for (it = m_handlers.begin(); it != m_handlers.end(); ++it) {
285 (*it)(a1, a2, a3);
286 }
287}
288
289// four arguments
290
291template<typename T1, typename T2, typename T3, typename T4>
292inline void
293EventEmitter<T1, T2, T3, T4>::operator+=(Handler handler)
294{
295 m_handlers.push_back(handler);
296}
297
298template<typename T1, typename T2, typename T3, typename T4>
299inline bool
300EventEmitter<T1, T2, T3, T4>::isEmpty()
301{
302 return m_handlers.empty();
303}
304
305template<typename T1, typename T2, typename T3, typename T4>
306inline void
307EventEmitter<T1, T2, T3, T4>::clear()
308{
309 return m_handlers.clear();
310}
311
312template<typename T1, typename T2, typename T3, typename T4>
313inline void
314EventEmitter<T1, T2, T3, T4>::operator()
315 (const T1& a1, const T2& a2, const T3& a3, const T4& a4)
316{
317 typename std::vector<Handler>::iterator it;
318 for (it = m_handlers.begin(); it != m_handlers.end(); ++it) {
319 (*it)(a1, a2, a3, a4);
320 }
321}
322
323
324};//namespace ndn
325#endif//NFD_UTIL_EVENT_EMITTER_H