Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 20 | */ |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 21 | |
| 22 | #ifndef NDN_UTIL_EVENT_EMITTER_HPP |
| 23 | #define NDN_UTIL_EVENT_EMITTER_HPP |
| 24 | |
Alexander Afanasyev | d51cd58 | 2014-08-12 11:34:24 -0700 | [diff] [blame] | 25 | #include "../common.hpp" |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 26 | #include <vector> |
| 27 | |
| 28 | namespace ndn { |
| 29 | namespace util { |
| 30 | |
Junxiao Shi | a93870d | 2014-11-05 22:35:31 -0700 | [diff] [blame^] | 31 | /** \brief provides a lightweight event system |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 32 | * |
| 33 | * To declare an event: |
| 34 | * EventEmitter<TArgs> onEventName; |
| 35 | * To subscribe to an event: |
| 36 | * eventSource->onEventName += eventHandler; |
| 37 | * Multiple functions can subscribe to the same event. |
| 38 | * To trigger an event: |
| 39 | * onEventName(args); |
| 40 | * To clear event subscriptions: |
| 41 | * onEventName.clear(); |
| 42 | */ |
| 43 | |
Junxiao Shi | a93870d | 2014-11-05 22:35:31 -0700 | [diff] [blame^] | 44 | template<typename ...TArgs> |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 45 | class EventEmitter : noncopyable |
| 46 | { |
| 47 | public: |
Junxiao Shi | a93870d | 2014-11-05 22:35:31 -0700 | [diff] [blame^] | 48 | /** \brief represents a handler that can subscribe to the event |
| 49 | */ |
| 50 | typedef function<void(const TArgs&...)> Handler; |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 51 | |
Junxiao Shi | a93870d | 2014-11-05 22:35:31 -0700 | [diff] [blame^] | 52 | /** \brief subscribes to the event |
| 53 | */ |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 54 | void |
Junxiao Shi | a93870d | 2014-11-05 22:35:31 -0700 | [diff] [blame^] | 55 | operator+=(const Handler& handler); |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 56 | |
Junxiao Shi | a93870d | 2014-11-05 22:35:31 -0700 | [diff] [blame^] | 57 | /** \return true if there is no subscription, false otherwise |
| 58 | */ |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 59 | bool |
| 60 | isEmpty() const; |
| 61 | |
Junxiao Shi | a93870d | 2014-11-05 22:35:31 -0700 | [diff] [blame^] | 62 | /** \brief clears all subscriptions |
| 63 | */ |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 64 | void |
| 65 | clear(); |
| 66 | |
Junxiao Shi | a93870d | 2014-11-05 22:35:31 -0700 | [diff] [blame^] | 67 | /** \brief triggers the event |
| 68 | */ |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 69 | void |
Junxiao Shi | a93870d | 2014-11-05 22:35:31 -0700 | [diff] [blame^] | 70 | operator()(const TArgs&...args) const; |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 71 | |
| 72 | private: |
| 73 | std::vector<Handler> m_handlers; |
| 74 | }; |
| 75 | |
Junxiao Shi | a93870d | 2014-11-05 22:35:31 -0700 | [diff] [blame^] | 76 | template<typename ...TArgs> |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 77 | inline void |
Junxiao Shi | a93870d | 2014-11-05 22:35:31 -0700 | [diff] [blame^] | 78 | EventEmitter<TArgs...>::operator+=(const Handler& handler) |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 79 | { |
| 80 | m_handlers.push_back(handler); |
| 81 | } |
| 82 | |
Junxiao Shi | a93870d | 2014-11-05 22:35:31 -0700 | [diff] [blame^] | 83 | template<typename ...TArgs> |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 84 | inline bool |
Junxiao Shi | a93870d | 2014-11-05 22:35:31 -0700 | [diff] [blame^] | 85 | EventEmitter<TArgs...>::isEmpty() const |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 86 | { |
| 87 | return m_handlers.empty(); |
| 88 | } |
| 89 | |
Junxiao Shi | a93870d | 2014-11-05 22:35:31 -0700 | [diff] [blame^] | 90 | template<typename ...TArgs> |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 91 | inline void |
Junxiao Shi | a93870d | 2014-11-05 22:35:31 -0700 | [diff] [blame^] | 92 | EventEmitter<TArgs...>::clear() |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 93 | { |
| 94 | return m_handlers.clear(); |
| 95 | } |
| 96 | |
Junxiao Shi | a93870d | 2014-11-05 22:35:31 -0700 | [diff] [blame^] | 97 | template<typename ...TArgs> |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 98 | inline void |
Junxiao Shi | a93870d | 2014-11-05 22:35:31 -0700 | [diff] [blame^] | 99 | EventEmitter<TArgs...>::operator()(const TArgs&...args) const |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 100 | { |
Junxiao Shi | a93870d | 2014-11-05 22:35:31 -0700 | [diff] [blame^] | 101 | for (const Handler& handler : m_handlers) { |
| 102 | handler(args...); |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 103 | if (m_handlers.empty()) // .clear has been called |
| 104 | return; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | |
| 109 | } // namespace util |
| 110 | } // namespace ndn |
| 111 | |
| 112 | #endif // NDN_UTIL_EVENT_EMITTER_HPP |