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(); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 42 | * |
| 43 | * \deprecated use Signal instead |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 44 | */ |
| 45 | |
Junxiao Shi | a93870d | 2014-11-05 22:35:31 -0700 | [diff] [blame] | 46 | template<typename ...TArgs> |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 47 | class EventEmitter : noncopyable |
| 48 | { |
| 49 | public: |
Junxiao Shi | a93870d | 2014-11-05 22:35:31 -0700 | [diff] [blame] | 50 | /** \brief represents a handler that can subscribe to the event |
| 51 | */ |
| 52 | typedef function<void(const TArgs&...)> Handler; |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 53 | |
Junxiao Shi | a93870d | 2014-11-05 22:35:31 -0700 | [diff] [blame] | 54 | /** \brief subscribes to the event |
| 55 | */ |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 56 | void |
Junxiao Shi | a93870d | 2014-11-05 22:35:31 -0700 | [diff] [blame] | 57 | operator+=(const Handler& handler); |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 58 | |
Junxiao Shi | a93870d | 2014-11-05 22:35:31 -0700 | [diff] [blame] | 59 | /** \return true if there is no subscription, false otherwise |
| 60 | */ |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 61 | bool |
| 62 | isEmpty() const; |
| 63 | |
Junxiao Shi | a93870d | 2014-11-05 22:35:31 -0700 | [diff] [blame] | 64 | /** \brief clears all subscriptions |
| 65 | */ |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 66 | void |
| 67 | clear(); |
| 68 | |
Junxiao Shi | a93870d | 2014-11-05 22:35:31 -0700 | [diff] [blame] | 69 | /** \brief triggers the event |
| 70 | */ |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 71 | void |
Junxiao Shi | a93870d | 2014-11-05 22:35:31 -0700 | [diff] [blame] | 72 | operator()(const TArgs&...args) const; |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 73 | |
| 74 | private: |
| 75 | std::vector<Handler> m_handlers; |
| 76 | }; |
| 77 | |
Junxiao Shi | a93870d | 2014-11-05 22:35:31 -0700 | [diff] [blame] | 78 | template<typename ...TArgs> |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 79 | inline void |
Junxiao Shi | a93870d | 2014-11-05 22:35:31 -0700 | [diff] [blame] | 80 | EventEmitter<TArgs...>::operator+=(const Handler& handler) |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 81 | { |
| 82 | m_handlers.push_back(handler); |
| 83 | } |
| 84 | |
Junxiao Shi | a93870d | 2014-11-05 22:35:31 -0700 | [diff] [blame] | 85 | template<typename ...TArgs> |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 86 | inline bool |
Junxiao Shi | a93870d | 2014-11-05 22:35:31 -0700 | [diff] [blame] | 87 | EventEmitter<TArgs...>::isEmpty() const |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 88 | { |
| 89 | return m_handlers.empty(); |
| 90 | } |
| 91 | |
Junxiao Shi | a93870d | 2014-11-05 22:35:31 -0700 | [diff] [blame] | 92 | template<typename ...TArgs> |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 93 | inline void |
Junxiao Shi | a93870d | 2014-11-05 22:35:31 -0700 | [diff] [blame] | 94 | EventEmitter<TArgs...>::clear() |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 95 | { |
| 96 | return m_handlers.clear(); |
| 97 | } |
| 98 | |
Junxiao Shi | a93870d | 2014-11-05 22:35:31 -0700 | [diff] [blame] | 99 | template<typename ...TArgs> |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 100 | inline void |
Junxiao Shi | a93870d | 2014-11-05 22:35:31 -0700 | [diff] [blame] | 101 | EventEmitter<TArgs...>::operator()(const TArgs&...args) const |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 102 | { |
Junxiao Shi | a93870d | 2014-11-05 22:35:31 -0700 | [diff] [blame] | 103 | for (const Handler& handler : m_handlers) { |
| 104 | handler(args...); |
Yingdi Yu | fe14015 | 2014-07-24 10:02:40 -0700 | [diff] [blame] | 105 | if (m_handlers.empty()) // .clear has been called |
| 106 | return; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | |
| 111 | } // namespace util |
| 112 | } // namespace ndn |
| 113 | |
| 114 | #endif // NDN_UTIL_EVENT_EMITTER_HPP |