Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 1 | /* -*- 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 | |
Junxiao Shi | 7f02c1e | 2014-01-29 22:57:01 -0700 | [diff] [blame^] | 7 | #ifndef NFD_CORE_EVENT_EMITTER_HPP |
| 8 | #define NFD_CORE_EVENT_EMITTER_HPP |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 9 | |
| 10 | #include "common.hpp" |
| 11 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 12 | namespace nfd { |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 13 | |
| 14 | struct 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 |
| 31 | template<typename T1 = empty, typename T2 = empty, |
| 32 | typename T3 = empty, typename T4 = empty> |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 33 | class EventEmitter : noncopyable |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 34 | { |
| 35 | public: |
| 36 | /// represents a handler that can subscribe to the event |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 37 | typedef function<void(const T1&, const T2&, |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 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 | |
| 57 | private: |
| 58 | /// stores all subscribed handlers |
| 59 | std::vector<Handler> m_handlers; |
| 60 | }; |
| 61 | |
| 62 | // zero argument |
| 63 | template<> |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 64 | class EventEmitter<empty, empty, empty, empty> : noncopyable |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 65 | { |
| 66 | public: |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 67 | typedef function<void()> Handler; |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 68 | |
| 69 | void |
| 70 | operator+=(Handler handler); |
| 71 | |
| 72 | bool |
| 73 | isEmpty(); |
| 74 | |
| 75 | void |
| 76 | clear(); |
| 77 | |
| 78 | void |
| 79 | operator()(); |
| 80 | |
| 81 | private: |
| 82 | std::vector<Handler> m_handlers; |
| 83 | }; |
| 84 | |
| 85 | |
| 86 | // one argument |
| 87 | template<typename T1> |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 88 | class EventEmitter<T1, empty, empty, empty> : noncopyable |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 89 | { |
| 90 | public: |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 91 | typedef function<void(const T1&)> Handler; |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 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 | |
| 105 | private: |
| 106 | std::vector<Handler> m_handlers; |
| 107 | }; |
| 108 | |
| 109 | |
| 110 | // two arguments |
| 111 | template<typename T1, typename T2> |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 112 | class EventEmitter<T1, T2, empty, empty> : noncopyable |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 113 | { |
| 114 | public: |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 115 | typedef function<void(const T1&, const T2&)> Handler; |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 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 | |
| 129 | private: |
| 130 | std::vector<Handler> m_handlers; |
| 131 | }; |
| 132 | |
| 133 | |
| 134 | // three arguments |
| 135 | template<typename T1, typename T2, typename T3> |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 136 | class EventEmitter<T1, T2, T3, empty> : noncopyable |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 137 | { |
| 138 | public: |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 139 | typedef function<void(const T1&, const T2&, const T3&)> Handler; |
Junxiao Shi | 1a2a856 | 2014-01-23 10:07:59 -0700 | [diff] [blame] | 140 | |
| 141 | void |
| 142 | operator+=(Handler handler); |
| 143 | |
| 144 | bool |
| 145 | isEmpty(); |
| 146 | |
| 147 | void |
| 148 | clear(); |
| 149 | |
| 150 | void |
| 151 | operator()(const T1& a1, const T2& a2, const T3& a3); |
| 152 | |
| 153 | private: |
| 154 | std::vector<Handler> m_handlers; |
| 155 | }; |
| 156 | |
| 157 | |
| 158 | // zero argument |
| 159 | |
| 160 | inline void |
| 161 | EventEmitter<empty, empty, empty, empty>::operator+=(Handler handler) |
| 162 | { |
| 163 | m_handlers.push_back(handler); |
| 164 | } |
| 165 | |
| 166 | inline bool |
| 167 | EventEmitter<empty, empty, empty, empty>::isEmpty() |
| 168 | { |
| 169 | return m_handlers.empty(); |
| 170 | } |
| 171 | |
| 172 | inline void |
| 173 | EventEmitter<empty, empty, empty, empty>::clear() |
| 174 | { |
| 175 | return m_handlers.clear(); |
| 176 | } |
| 177 | |
| 178 | inline void |
| 179 | EventEmitter<empty, empty, empty, empty>::operator()() |
| 180 | { |
| 181 | std::vector<Handler>::iterator it; |
| 182 | for (it = m_handlers.begin(); it != m_handlers.end(); ++it) { |
| 183 | (*it)(); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | // one argument |
| 188 | |
| 189 | template<typename T1> |
| 190 | inline void |
| 191 | EventEmitter<T1, empty, empty, empty>::operator+=(Handler handler) |
| 192 | { |
| 193 | m_handlers.push_back(handler); |
| 194 | } |
| 195 | |
| 196 | template<typename T1> |
| 197 | inline bool |
| 198 | EventEmitter<T1, empty, empty, empty>::isEmpty() |
| 199 | { |
| 200 | return m_handlers.empty(); |
| 201 | } |
| 202 | |
| 203 | template<typename T1> |
| 204 | inline void |
| 205 | EventEmitter<T1, empty, empty, empty>::clear() |
| 206 | { |
| 207 | return m_handlers.clear(); |
| 208 | } |
| 209 | |
| 210 | template<typename T1> |
| 211 | inline void |
| 212 | EventEmitter<T1, empty, empty, empty>::operator()(const T1& a1) |
| 213 | { |
| 214 | typename std::vector<Handler>::iterator it; |
| 215 | for (it = m_handlers.begin(); it != m_handlers.end(); ++it) { |
| 216 | (*it)(a1); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | // two arguments |
| 221 | |
| 222 | template<typename T1, typename T2> |
| 223 | inline void |
| 224 | EventEmitter<T1, T2, empty, empty>::operator+=(Handler handler) |
| 225 | { |
| 226 | m_handlers.push_back(handler); |
| 227 | } |
| 228 | |
| 229 | template<typename T1, typename T2> |
| 230 | inline bool |
| 231 | EventEmitter<T1, T2, empty, empty>::isEmpty() |
| 232 | { |
| 233 | return m_handlers.empty(); |
| 234 | } |
| 235 | |
| 236 | template<typename T1, typename T2> |
| 237 | inline void |
| 238 | EventEmitter<T1, T2, empty, empty>::clear() |
| 239 | { |
| 240 | return m_handlers.clear(); |
| 241 | } |
| 242 | |
| 243 | template<typename T1, typename T2> |
| 244 | inline void |
| 245 | EventEmitter<T1, T2, empty, empty>::operator() |
| 246 | (const T1& a1, const T2& a2) |
| 247 | { |
| 248 | typename std::vector<Handler>::iterator it; |
| 249 | for (it = m_handlers.begin(); it != m_handlers.end(); ++it) { |
| 250 | (*it)(a1, a2); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | // three arguments |
| 255 | |
| 256 | template<typename T1, typename T2, typename T3> |
| 257 | inline void |
| 258 | EventEmitter<T1, T2, T3, empty>::operator+=(Handler handler) |
| 259 | { |
| 260 | m_handlers.push_back(handler); |
| 261 | } |
| 262 | |
| 263 | template<typename T1, typename T2, typename T3> |
| 264 | inline bool |
| 265 | EventEmitter<T1, T2, T3, empty>::isEmpty() |
| 266 | { |
| 267 | return m_handlers.empty(); |
| 268 | } |
| 269 | |
| 270 | template<typename T1, typename T2, typename T3> |
| 271 | inline void |
| 272 | EventEmitter<T1, T2, T3, empty>::clear() |
| 273 | { |
| 274 | return m_handlers.clear(); |
| 275 | } |
| 276 | |
| 277 | template<typename T1, typename T2, typename T3> |
| 278 | inline void |
| 279 | EventEmitter<T1, T2, T3, empty>::operator() |
| 280 | (const T1& a1, const T2& a2, const T3& a3) |
| 281 | { |
| 282 | typename std::vector<Handler>::iterator it; |
| 283 | for (it = m_handlers.begin(); it != m_handlers.end(); ++it) { |
| 284 | (*it)(a1, a2, a3); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | // four arguments |
| 289 | |
| 290 | template<typename T1, typename T2, typename T3, typename T4> |
| 291 | inline void |
| 292 | EventEmitter<T1, T2, T3, T4>::operator+=(Handler handler) |
| 293 | { |
| 294 | m_handlers.push_back(handler); |
| 295 | } |
| 296 | |
| 297 | template<typename T1, typename T2, typename T3, typename T4> |
| 298 | inline bool |
| 299 | EventEmitter<T1, T2, T3, T4>::isEmpty() |
| 300 | { |
| 301 | return m_handlers.empty(); |
| 302 | } |
| 303 | |
| 304 | template<typename T1, typename T2, typename T3, typename T4> |
| 305 | inline void |
| 306 | EventEmitter<T1, T2, T3, T4>::clear() |
| 307 | { |
| 308 | return m_handlers.clear(); |
| 309 | } |
| 310 | |
| 311 | template<typename T1, typename T2, typename T3, typename T4> |
| 312 | inline void |
| 313 | EventEmitter<T1, T2, T3, T4>::operator() |
| 314 | (const T1& a1, const T2& a2, const T3& a3, const T4& a4) |
| 315 | { |
| 316 | typename std::vector<Handler>::iterator it; |
| 317 | for (it = m_handlers.begin(); it != m_handlers.end(); ++it) { |
| 318 | (*it)(a1, a2, a3, a4); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 323 | } // namespace nfd |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 324 | |
Junxiao Shi | 7f02c1e | 2014-01-29 22:57:01 -0700 | [diff] [blame^] | 325 | #endif // NFD_CORE_EVENT_EMITTER_HPP |