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