blob: e63e1111a3214d1c9c5c8c3398863f02ed552d1f [file] [log] [blame]
Junxiao Shi1a2a8562014-01-23 10:07:59 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi15b12e72014-08-09 19:56:24 -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 * The University of Memphis
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Junxiao Shi15b12e72014-08-09 19:56:24 -070024 */
Junxiao Shi1a2a8562014-01-23 10:07:59 -070025
Junxiao Shi7f02c1e2014-01-29 22:57:01 -070026#ifndef NFD_CORE_EVENT_EMITTER_HPP
27#define NFD_CORE_EVENT_EMITTER_HPP
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080028
29#include "common.hpp"
30
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080031namespace nfd {
Junxiao Shi1a2a8562014-01-23 10:07:59 -070032
Junxiao Shi15b12e72014-08-09 19:56:24 -070033struct empty
34{
35};
Junxiao Shi1a2a8562014-01-23 10:07:59 -070036
37/** \class EventEmitter
38 * \brief provides a lightweight event system
39 *
40 * To declare an event:
Junxiao Shi15b12e72014-08-09 19:56:24 -070041 * EventEmitter<TArgs> onEventName;
Junxiao Shi1a2a8562014-01-23 10:07:59 -070042 * To subscribe to an event:
Junxiao Shi15b12e72014-08-09 19:56:24 -070043 * eventSource->onEventName += eventHandler;
Junxiao Shi1a2a8562014-01-23 10:07:59 -070044 * Multiple functions can subscribe to the same event.
45 * To trigger an event:
Junxiao Shi15b12e72014-08-09 19:56:24 -070046 * onEventName(args);
Junxiao Shi1a2a8562014-01-23 10:07:59 -070047 * To clear event subscriptions:
Junxiao Shi15b12e72014-08-09 19:56:24 -070048 * onEventName.clear();
Junxiao Shi1a2a8562014-01-23 10:07:59 -070049 */
50
51// four arguments
52template<typename T1 = empty, typename T2 = empty,
Junxiao Shi15b12e72014-08-09 19:56:24 -070053 typename T3 = empty, typename T4 = empty>
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080054class EventEmitter : noncopyable
Junxiao Shi1a2a8562014-01-23 10:07:59 -070055{
56public:
57 /// represents a handler that can subscribe to the event
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080058 typedef function<void(const T1&, const T2&,
Junxiao Shi1a2a8562014-01-23 10:07:59 -070059 const T3&, const T4&)> Handler;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070060
Junxiao Shi1a2a8562014-01-23 10:07:59 -070061 /// adds an subscription
62 void
63 operator+=(Handler handler);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070064
Junxiao Shi1a2a8562014-01-23 10:07:59 -070065 /// returns true if there is no subscription,
66 /// otherwise returns false
67 bool
68 isEmpty();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070069
Junxiao Shi1a2a8562014-01-23 10:07:59 -070070 /// clears all subscriptions
71 void
72 clear();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070073
Junxiao Shi1a2a8562014-01-23 10:07:59 -070074 /// triggers the event
75 void
76 operator()(const T1& a1, const T2& a2, const T3& a3, const T4& a4);
77
78private:
79 /// stores all subscribed handlers
80 std::vector<Handler> m_handlers;
81};
82
83// zero argument
84template<>
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080085class EventEmitter<empty, empty, empty, empty> : noncopyable
Junxiao Shi1a2a8562014-01-23 10:07:59 -070086{
87public:
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080088 typedef function<void()> Handler;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070089
Junxiao Shi1a2a8562014-01-23 10:07:59 -070090 void
91 operator+=(Handler handler);
92
93 bool
94 isEmpty();
95
96 void
97 clear();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070098
Junxiao Shi1a2a8562014-01-23 10:07:59 -070099 void
100 operator()();
101
102private:
103 std::vector<Handler> m_handlers;
104};
105
106
107// one argument
108template<typename T1>
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800109class EventEmitter<T1, empty, empty, empty> : noncopyable
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700110{
111public:
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800112 typedef function<void(const T1&)> Handler;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700113
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700114 void
115 operator+=(Handler handler);
116
117 bool
118 isEmpty();
119
120 void
121 clear();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700122
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700123 void
124 operator()(const T1& a1);
125
126private:
127 std::vector<Handler> m_handlers;
128};
129
130
131// two arguments
132template<typename T1, typename T2>
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800133class EventEmitter<T1, T2, empty, empty> : noncopyable
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700134{
135public:
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800136 typedef function<void(const T1&, const T2&)> Handler;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700137
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700138 void
139 operator+=(Handler handler);
140
141 bool
142 isEmpty();
143
144 void
145 clear();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700146
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700147 void
148 operator()(const T1& a1, const T2& a2);
149
150private:
151 std::vector<Handler> m_handlers;
152};
153
154
155// three arguments
156template<typename T1, typename T2, typename T3>
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800157class EventEmitter<T1, T2, T3, empty> : noncopyable
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700158{
159public:
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800160 typedef function<void(const T1&, const T2&, const T3&)> Handler;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700161
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700162 void
163 operator+=(Handler handler);
164
165 bool
166 isEmpty();
167
168 void
169 clear();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700170
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700171 void
172 operator()(const T1& a1, const T2& a2, const T3& a3);
173
174private:
175 std::vector<Handler> m_handlers;
176};
177
178
179// zero argument
180
181inline void
182EventEmitter<empty, empty, empty, empty>::operator+=(Handler handler)
183{
184 m_handlers.push_back(handler);
185}
186
187inline bool
188EventEmitter<empty, empty, empty, empty>::isEmpty()
189{
190 return m_handlers.empty();
191}
192
193inline void
194EventEmitter<empty, empty, empty, empty>::clear()
195{
196 return m_handlers.clear();
197}
198
199inline void
200EventEmitter<empty, empty, empty, empty>::operator()()
201{
202 std::vector<Handler>::iterator it;
203 for (it = m_handlers.begin(); it != m_handlers.end(); ++it) {
204 (*it)();
Junxiao Shi15b12e72014-08-09 19:56:24 -0700205 if (m_handlers.empty()) // .clear has been called
206 return;
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700207 }
208}
209
210// one argument
211
212template<typename T1>
213inline void
214EventEmitter<T1, empty, empty, empty>::operator+=(Handler handler)
215{
216 m_handlers.push_back(handler);
217}
218
219template<typename T1>
220inline bool
221EventEmitter<T1, empty, empty, empty>::isEmpty()
222{
223 return m_handlers.empty();
224}
225
226template<typename T1>
227inline void
228EventEmitter<T1, empty, empty, empty>::clear()
229{
230 return m_handlers.clear();
231}
232
233template<typename T1>
234inline void
235EventEmitter<T1, empty, empty, empty>::operator()(const T1& a1)
236{
237 typename std::vector<Handler>::iterator it;
238 for (it = m_handlers.begin(); it != m_handlers.end(); ++it) {
239 (*it)(a1);
Junxiao Shi15b12e72014-08-09 19:56:24 -0700240 if (m_handlers.empty()) // .clear has been called
241 return;
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700242 }
243}
244
245// two arguments
246
247template<typename T1, typename T2>
248inline void
249EventEmitter<T1, T2, empty, empty>::operator+=(Handler handler)
250{
251 m_handlers.push_back(handler);
252}
253
254template<typename T1, typename T2>
255inline bool
256EventEmitter<T1, T2, empty, empty>::isEmpty()
257{
258 return m_handlers.empty();
259}
260
261template<typename T1, typename T2>
262inline void
263EventEmitter<T1, T2, empty, empty>::clear()
264{
265 return m_handlers.clear();
266}
267
268template<typename T1, typename T2>
269inline void
270EventEmitter<T1, T2, empty, empty>::operator()
271 (const T1& a1, const T2& a2)
272{
273 typename std::vector<Handler>::iterator it;
274 for (it = m_handlers.begin(); it != m_handlers.end(); ++it) {
275 (*it)(a1, a2);
Junxiao Shi15b12e72014-08-09 19:56:24 -0700276 if (m_handlers.empty()) // .clear has been called
277 return;
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700278 }
279}
280
281// three arguments
282
283template<typename T1, typename T2, typename T3>
284inline void
285EventEmitter<T1, T2, T3, empty>::operator+=(Handler handler)
286{
287 m_handlers.push_back(handler);
288}
289
290template<typename T1, typename T2, typename T3>
291inline bool
292EventEmitter<T1, T2, T3, empty>::isEmpty()
293{
294 return m_handlers.empty();
295}
296
297template<typename T1, typename T2, typename T3>
298inline void
299EventEmitter<T1, T2, T3, empty>::clear()
300{
301 return m_handlers.clear();
302}
303
304template<typename T1, typename T2, typename T3>
305inline void
306EventEmitter<T1, T2, T3, empty>::operator()
307 (const T1& a1, const T2& a2, const T3& a3)
308{
309 typename std::vector<Handler>::iterator it;
310 for (it = m_handlers.begin(); it != m_handlers.end(); ++it) {
311 (*it)(a1, a2, a3);
Junxiao Shi15b12e72014-08-09 19:56:24 -0700312 if (m_handlers.empty()) // .clear has been called
313 return;
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700314 }
315}
316
317// four arguments
318
319template<typename T1, typename T2, typename T3, typename T4>
320inline void
321EventEmitter<T1, T2, T3, T4>::operator+=(Handler handler)
322{
323 m_handlers.push_back(handler);
324}
325
326template<typename T1, typename T2, typename T3, typename T4>
327inline bool
328EventEmitter<T1, T2, T3, T4>::isEmpty()
329{
330 return m_handlers.empty();
331}
332
333template<typename T1, typename T2, typename T3, typename T4>
334inline void
335EventEmitter<T1, T2, T3, T4>::clear()
336{
337 return m_handlers.clear();
338}
339
340template<typename T1, typename T2, typename T3, typename T4>
341inline void
342EventEmitter<T1, T2, T3, T4>::operator()
343 (const T1& a1, const T2& a2, const T3& a3, const T4& a4)
344{
345 typename std::vector<Handler>::iterator it;
346 for (it = m_handlers.begin(); it != m_handlers.end(); ++it) {
347 (*it)(a1, a2, a3, a4);
Junxiao Shi15b12e72014-08-09 19:56:24 -0700348 if (m_handlers.empty()) // .clear has been called
349 return;
Junxiao Shi1a2a8562014-01-23 10:07:59 -0700350 }
351}
352
353
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800354} // namespace nfd
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800355
Junxiao Shi7f02c1e2014-01-29 22:57:01 -0700356#endif // NFD_CORE_EVENT_EMITTER_HPP