blob: 35925f98fa58882486eb0bda3d3b97df55328863 [file] [log] [blame]
Junxiao Shi1a2a8562014-01-23 10:07:59 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * Copyright (c) 2014 Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology
9 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
Junxiao Shi1a2a8562014-01-23 10:07:59 -070024
Junxiao Shi7f02c1e2014-01-29 22:57:01 -070025#ifndef NFD_CORE_EVENT_EMITTER_HPP
26#define NFD_CORE_EVENT_EMITTER_HPP
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080027
28#include "common.hpp"
29
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080030namespace nfd {
Junxiao Shi1a2a8562014-01-23 10:07:59 -070031
32struct empty {};
33
34/** \class EventEmitter
35 * \brief provides a lightweight event system
36 *
37 * To declare an event:
38 * EventEmitter<TArgs> m_eventName;
39 * To subscribe to an event:
40 * eventSource->m_eventName += eventHandler;
41 * Multiple functions can subscribe to the same event.
42 * To trigger an event:
43 * m_eventName(args);
44 * To clear event subscriptions:
45 * m_eventName.clear();
46 */
47
48// four arguments
49template<typename T1 = empty, typename T2 = empty,
50 typename T3 = empty, typename T4 = empty>
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080051class EventEmitter : noncopyable
Junxiao Shi1a2a8562014-01-23 10:07:59 -070052{
53public:
54 /// represents a handler that can subscribe to the event
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080055 typedef function<void(const T1&, const T2&,
Junxiao Shi1a2a8562014-01-23 10:07:59 -070056 const T3&, const T4&)> Handler;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070057
Junxiao Shi1a2a8562014-01-23 10:07:59 -070058 /// adds an subscription
59 void
60 operator+=(Handler handler);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070061
Junxiao Shi1a2a8562014-01-23 10:07:59 -070062 /// returns true if there is no subscription,
63 /// otherwise returns false
64 bool
65 isEmpty();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070066
Junxiao Shi1a2a8562014-01-23 10:07:59 -070067 /// clears all subscriptions
68 void
69 clear();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070070
Junxiao Shi1a2a8562014-01-23 10:07:59 -070071 /// triggers the event
72 void
73 operator()(const T1& a1, const T2& a2, const T3& a3, const T4& a4);
74
75private:
76 /// stores all subscribed handlers
77 std::vector<Handler> m_handlers;
78};
79
80// zero argument
81template<>
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080082class EventEmitter<empty, empty, empty, empty> : noncopyable
Junxiao Shi1a2a8562014-01-23 10:07:59 -070083{
84public:
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080085 typedef function<void()> Handler;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070086
Junxiao Shi1a2a8562014-01-23 10:07:59 -070087 void
88 operator+=(Handler handler);
89
90 bool
91 isEmpty();
92
93 void
94 clear();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070095
Junxiao Shi1a2a8562014-01-23 10:07:59 -070096 void
97 operator()();
98
99private:
100 std::vector<Handler> m_handlers;
101};
102
103
104// one argument
105template<typename T1>
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800106class EventEmitter<T1, empty, empty, empty> : noncopyable
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700107{
108public:
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800109 typedef function<void(const T1&)> Handler;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700110
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700111 void
112 operator+=(Handler handler);
113
114 bool
115 isEmpty();
116
117 void
118 clear();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700119
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700120 void
121 operator()(const T1& a1);
122
123private:
124 std::vector<Handler> m_handlers;
125};
126
127
128// two arguments
129template<typename T1, typename T2>
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800130class EventEmitter<T1, T2, empty, empty> : noncopyable
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700131{
132public:
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800133 typedef function<void(const T1&, const T2&)> Handler;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700134
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700135 void
136 operator+=(Handler handler);
137
138 bool
139 isEmpty();
140
141 void
142 clear();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700143
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700144 void
145 operator()(const T1& a1, const T2& a2);
146
147private:
148 std::vector<Handler> m_handlers;
149};
150
151
152// three arguments
153template<typename T1, typename T2, typename T3>
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800154class EventEmitter<T1, T2, T3, empty> : noncopyable
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700155{
156public:
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800157 typedef function<void(const T1&, const T2&, const T3&)> Handler;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700158
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700159 void
160 operator+=(Handler handler);
161
162 bool
163 isEmpty();
164
165 void
166 clear();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700167
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700168 void
169 operator()(const T1& a1, const T2& a2, const T3& a3);
170
171private:
172 std::vector<Handler> m_handlers;
173};
174
175
176// zero argument
177
178inline void
179EventEmitter<empty, empty, empty, empty>::operator+=(Handler handler)
180{
181 m_handlers.push_back(handler);
182}
183
184inline bool
185EventEmitter<empty, empty, empty, empty>::isEmpty()
186{
187 return m_handlers.empty();
188}
189
190inline void
191EventEmitter<empty, empty, empty, empty>::clear()
192{
193 return m_handlers.clear();
194}
195
196inline void
197EventEmitter<empty, empty, empty, empty>::operator()()
198{
199 std::vector<Handler>::iterator it;
200 for (it = m_handlers.begin(); it != m_handlers.end(); ++it) {
201 (*it)();
202 }
203}
204
205// one argument
206
207template<typename T1>
208inline void
209EventEmitter<T1, empty, empty, empty>::operator+=(Handler handler)
210{
211 m_handlers.push_back(handler);
212}
213
214template<typename T1>
215inline bool
216EventEmitter<T1, empty, empty, empty>::isEmpty()
217{
218 return m_handlers.empty();
219}
220
221template<typename T1>
222inline void
223EventEmitter<T1, empty, empty, empty>::clear()
224{
225 return m_handlers.clear();
226}
227
228template<typename T1>
229inline void
230EventEmitter<T1, empty, empty, empty>::operator()(const T1& a1)
231{
232 typename std::vector<Handler>::iterator it;
233 for (it = m_handlers.begin(); it != m_handlers.end(); ++it) {
234 (*it)(a1);
235 }
236}
237
238// two arguments
239
240template<typename T1, typename T2>
241inline void
242EventEmitter<T1, T2, empty, empty>::operator+=(Handler handler)
243{
244 m_handlers.push_back(handler);
245}
246
247template<typename T1, typename T2>
248inline bool
249EventEmitter<T1, T2, empty, empty>::isEmpty()
250{
251 return m_handlers.empty();
252}
253
254template<typename T1, typename T2>
255inline void
256EventEmitter<T1, T2, empty, empty>::clear()
257{
258 return m_handlers.clear();
259}
260
261template<typename T1, typename T2>
262inline void
263EventEmitter<T1, T2, empty, empty>::operator()
264 (const T1& a1, const T2& a2)
265{
266 typename std::vector<Handler>::iterator it;
267 for (it = m_handlers.begin(); it != m_handlers.end(); ++it) {
268 (*it)(a1, a2);
269 }
270}
271
272// three arguments
273
274template<typename T1, typename T2, typename T3>
275inline void
276EventEmitter<T1, T2, T3, empty>::operator+=(Handler handler)
277{
278 m_handlers.push_back(handler);
279}
280
281template<typename T1, typename T2, typename T3>
282inline bool
283EventEmitter<T1, T2, T3, empty>::isEmpty()
284{
285 return m_handlers.empty();
286}
287
288template<typename T1, typename T2, typename T3>
289inline void
290EventEmitter<T1, T2, T3, empty>::clear()
291{
292 return m_handlers.clear();
293}
294
295template<typename T1, typename T2, typename T3>
296inline void
297EventEmitter<T1, T2, T3, empty>::operator()
298 (const T1& a1, const T2& a2, const T3& a3)
299{
300 typename std::vector<Handler>::iterator it;
301 for (it = m_handlers.begin(); it != m_handlers.end(); ++it) {
302 (*it)(a1, a2, a3);
303 }
304}
305
306// four arguments
307
308template<typename T1, typename T2, typename T3, typename T4>
309inline void
310EventEmitter<T1, T2, T3, T4>::operator+=(Handler handler)
311{
312 m_handlers.push_back(handler);
313}
314
315template<typename T1, typename T2, typename T3, typename T4>
316inline bool
317EventEmitter<T1, T2, T3, T4>::isEmpty()
318{
319 return m_handlers.empty();
320}
321
322template<typename T1, typename T2, typename T3, typename T4>
323inline void
324EventEmitter<T1, T2, T3, T4>::clear()
325{
326 return m_handlers.clear();
327}
328
329template<typename T1, typename T2, typename T3, typename T4>
330inline void
331EventEmitter<T1, T2, T3, T4>::operator()
332 (const T1& a1, const T2& a2, const T3& a3, const T4& a4)
333{
334 typename std::vector<Handler>::iterator it;
335 for (it = m_handlers.begin(); it != m_handlers.end(); ++it) {
336 (*it)(a1, a2, a3, a4);
337 }
338}
339
340
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800341} // namespace nfd
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800342
Junxiao Shi7f02c1e2014-01-29 22:57:01 -0700343#endif // NFD_CORE_EVENT_EMITTER_HPP