Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 15b12e7 | 2014-08-09 19:56:24 -0700 | [diff] [blame^] | 3 | * 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 Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 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 Shi | 15b12e7 | 2014-08-09 19:56:24 -0700 | [diff] [blame^] | 24 | */ |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 25 | |
Junxiao Shi | 7f02c1e | 2014-01-29 22:57:01 -0700 | [diff] [blame] | 26 | #ifndef NFD_CORE_EVENT_EMITTER_HPP |
| 27 | #define NFD_CORE_EVENT_EMITTER_HPP |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 28 | |
| 29 | #include "common.hpp" |
| 30 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 31 | namespace nfd { |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 32 | |
Junxiao Shi | 15b12e7 | 2014-08-09 19:56:24 -0700 | [diff] [blame^] | 33 | struct empty |
| 34 | { |
| 35 | }; |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 36 | |
| 37 | /** \class EventEmitter |
| 38 | * \brief provides a lightweight event system |
| 39 | * |
| 40 | * To declare an event: |
Junxiao Shi | 15b12e7 | 2014-08-09 19:56:24 -0700 | [diff] [blame^] | 41 | * EventEmitter<TArgs> onEventName; |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 42 | * To subscribe to an event: |
Junxiao Shi | 15b12e7 | 2014-08-09 19:56:24 -0700 | [diff] [blame^] | 43 | * eventSource->onEventName += eventHandler; |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 44 | * Multiple functions can subscribe to the same event. |
| 45 | * To trigger an event: |
Junxiao Shi | 15b12e7 | 2014-08-09 19:56:24 -0700 | [diff] [blame^] | 46 | * onEventName(args); |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 47 | * To clear event subscriptions: |
Junxiao Shi | 15b12e7 | 2014-08-09 19:56:24 -0700 | [diff] [blame^] | 48 | * onEventName.clear(); |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 49 | */ |
| 50 | |
| 51 | // four arguments |
| 52 | template<typename T1 = empty, typename T2 = empty, |
Junxiao Shi | 15b12e7 | 2014-08-09 19:56:24 -0700 | [diff] [blame^] | 53 | typename T3 = empty, typename T4 = empty> |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 54 | class EventEmitter : noncopyable |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 55 | { |
| 56 | public: |
| 57 | /// represents a handler that can subscribe to the event |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 58 | typedef function<void(const T1&, const T2&, |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 59 | const T3&, const T4&)> Handler; |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 60 | |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 61 | /// adds an subscription |
| 62 | void |
| 63 | operator+=(Handler handler); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 64 | |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 65 | /// returns true if there is no subscription, |
| 66 | /// otherwise returns false |
| 67 | bool |
| 68 | isEmpty(); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 69 | |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 70 | /// clears all subscriptions |
| 71 | void |
| 72 | clear(); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 73 | |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 74 | /// triggers the event |
| 75 | void |
| 76 | operator()(const T1& a1, const T2& a2, const T3& a3, const T4& a4); |
| 77 | |
| 78 | private: |
| 79 | /// stores all subscribed handlers |
| 80 | std::vector<Handler> m_handlers; |
| 81 | }; |
| 82 | |
| 83 | // zero argument |
| 84 | template<> |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 85 | class EventEmitter<empty, empty, empty, empty> : noncopyable |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 86 | { |
| 87 | public: |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 88 | typedef function<void()> Handler; |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 89 | |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 90 | void |
| 91 | operator+=(Handler handler); |
| 92 | |
| 93 | bool |
| 94 | isEmpty(); |
| 95 | |
| 96 | void |
| 97 | clear(); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 98 | |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 99 | void |
| 100 | operator()(); |
| 101 | |
| 102 | private: |
| 103 | std::vector<Handler> m_handlers; |
| 104 | }; |
| 105 | |
| 106 | |
| 107 | // one argument |
| 108 | template<typename T1> |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 109 | class EventEmitter<T1, empty, empty, empty> : noncopyable |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 110 | { |
| 111 | public: |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 112 | typedef function<void(const T1&)> Handler; |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 113 | |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 114 | void |
| 115 | operator+=(Handler handler); |
| 116 | |
| 117 | bool |
| 118 | isEmpty(); |
| 119 | |
| 120 | void |
| 121 | clear(); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 122 | |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 123 | void |
| 124 | operator()(const T1& a1); |
| 125 | |
| 126 | private: |
| 127 | std::vector<Handler> m_handlers; |
| 128 | }; |
| 129 | |
| 130 | |
| 131 | // two arguments |
| 132 | template<typename T1, typename T2> |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 133 | class EventEmitter<T1, T2, empty, empty> : noncopyable |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 134 | { |
| 135 | public: |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 136 | typedef function<void(const T1&, const T2&)> Handler; |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 137 | |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 138 | void |
| 139 | operator+=(Handler handler); |
| 140 | |
| 141 | bool |
| 142 | isEmpty(); |
| 143 | |
| 144 | void |
| 145 | clear(); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 146 | |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 147 | void |
| 148 | operator()(const T1& a1, const T2& a2); |
| 149 | |
| 150 | private: |
| 151 | std::vector<Handler> m_handlers; |
| 152 | }; |
| 153 | |
| 154 | |
| 155 | // three arguments |
| 156 | template<typename T1, typename T2, typename T3> |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 157 | class EventEmitter<T1, T2, T3, empty> : noncopyable |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 158 | { |
| 159 | public: |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 160 | typedef function<void(const T1&, const T2&, const T3&)> Handler; |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 161 | |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 162 | void |
| 163 | operator+=(Handler handler); |
| 164 | |
| 165 | bool |
| 166 | isEmpty(); |
| 167 | |
| 168 | void |
| 169 | clear(); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 170 | |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 171 | void |
| 172 | operator()(const T1& a1, const T2& a2, const T3& a3); |
| 173 | |
| 174 | private: |
| 175 | std::vector<Handler> m_handlers; |
| 176 | }; |
| 177 | |
| 178 | |
| 179 | // zero argument |
| 180 | |
| 181 | inline void |
| 182 | EventEmitter<empty, empty, empty, empty>::operator+=(Handler handler) |
| 183 | { |
| 184 | m_handlers.push_back(handler); |
| 185 | } |
| 186 | |
| 187 | inline bool |
| 188 | EventEmitter<empty, empty, empty, empty>::isEmpty() |
| 189 | { |
| 190 | return m_handlers.empty(); |
| 191 | } |
| 192 | |
| 193 | inline void |
| 194 | EventEmitter<empty, empty, empty, empty>::clear() |
| 195 | { |
| 196 | return m_handlers.clear(); |
| 197 | } |
| 198 | |
| 199 | inline void |
| 200 | EventEmitter<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 Shi | 15b12e7 | 2014-08-09 19:56:24 -0700 | [diff] [blame^] | 205 | if (m_handlers.empty()) // .clear has been called |
| 206 | return; |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
| 210 | // one argument |
| 211 | |
| 212 | template<typename T1> |
| 213 | inline void |
| 214 | EventEmitter<T1, empty, empty, empty>::operator+=(Handler handler) |
| 215 | { |
| 216 | m_handlers.push_back(handler); |
| 217 | } |
| 218 | |
| 219 | template<typename T1> |
| 220 | inline bool |
| 221 | EventEmitter<T1, empty, empty, empty>::isEmpty() |
| 222 | { |
| 223 | return m_handlers.empty(); |
| 224 | } |
| 225 | |
| 226 | template<typename T1> |
| 227 | inline void |
| 228 | EventEmitter<T1, empty, empty, empty>::clear() |
| 229 | { |
| 230 | return m_handlers.clear(); |
| 231 | } |
| 232 | |
| 233 | template<typename T1> |
| 234 | inline void |
| 235 | EventEmitter<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 Shi | 15b12e7 | 2014-08-09 19:56:24 -0700 | [diff] [blame^] | 240 | if (m_handlers.empty()) // .clear has been called |
| 241 | return; |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
| 245 | // two arguments |
| 246 | |
| 247 | template<typename T1, typename T2> |
| 248 | inline void |
| 249 | EventEmitter<T1, T2, empty, empty>::operator+=(Handler handler) |
| 250 | { |
| 251 | m_handlers.push_back(handler); |
| 252 | } |
| 253 | |
| 254 | template<typename T1, typename T2> |
| 255 | inline bool |
| 256 | EventEmitter<T1, T2, empty, empty>::isEmpty() |
| 257 | { |
| 258 | return m_handlers.empty(); |
| 259 | } |
| 260 | |
| 261 | template<typename T1, typename T2> |
| 262 | inline void |
| 263 | EventEmitter<T1, T2, empty, empty>::clear() |
| 264 | { |
| 265 | return m_handlers.clear(); |
| 266 | } |
| 267 | |
| 268 | template<typename T1, typename T2> |
| 269 | inline void |
| 270 | EventEmitter<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 Shi | 15b12e7 | 2014-08-09 19:56:24 -0700 | [diff] [blame^] | 276 | if (m_handlers.empty()) // .clear has been called |
| 277 | return; |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
| 281 | // three arguments |
| 282 | |
| 283 | template<typename T1, typename T2, typename T3> |
| 284 | inline void |
| 285 | EventEmitter<T1, T2, T3, empty>::operator+=(Handler handler) |
| 286 | { |
| 287 | m_handlers.push_back(handler); |
| 288 | } |
| 289 | |
| 290 | template<typename T1, typename T2, typename T3> |
| 291 | inline bool |
| 292 | EventEmitter<T1, T2, T3, empty>::isEmpty() |
| 293 | { |
| 294 | return m_handlers.empty(); |
| 295 | } |
| 296 | |
| 297 | template<typename T1, typename T2, typename T3> |
| 298 | inline void |
| 299 | EventEmitter<T1, T2, T3, empty>::clear() |
| 300 | { |
| 301 | return m_handlers.clear(); |
| 302 | } |
| 303 | |
| 304 | template<typename T1, typename T2, typename T3> |
| 305 | inline void |
| 306 | EventEmitter<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 Shi | 15b12e7 | 2014-08-09 19:56:24 -0700 | [diff] [blame^] | 312 | if (m_handlers.empty()) // .clear has been called |
| 313 | return; |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | |
| 317 | // four arguments |
| 318 | |
| 319 | template<typename T1, typename T2, typename T3, typename T4> |
| 320 | inline void |
| 321 | EventEmitter<T1, T2, T3, T4>::operator+=(Handler handler) |
| 322 | { |
| 323 | m_handlers.push_back(handler); |
| 324 | } |
| 325 | |
| 326 | template<typename T1, typename T2, typename T3, typename T4> |
| 327 | inline bool |
| 328 | EventEmitter<T1, T2, T3, T4>::isEmpty() |
| 329 | { |
| 330 | return m_handlers.empty(); |
| 331 | } |
| 332 | |
| 333 | template<typename T1, typename T2, typename T3, typename T4> |
| 334 | inline void |
| 335 | EventEmitter<T1, T2, T3, T4>::clear() |
| 336 | { |
| 337 | return m_handlers.clear(); |
| 338 | } |
| 339 | |
| 340 | template<typename T1, typename T2, typename T3, typename T4> |
| 341 | inline void |
| 342 | EventEmitter<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 Shi | 15b12e7 | 2014-08-09 19:56:24 -0700 | [diff] [blame^] | 348 | if (m_handlers.empty()) // .clear has been called |
| 349 | return; |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
| 353 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 354 | } // namespace nfd |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 355 | |
Junxiao Shi | 7f02c1e | 2014-01-29 22:57:01 -0700 | [diff] [blame] | 356 | #endif // NFD_CORE_EVENT_EMITTER_HPP |