blob: bcb703b6e7b1309d93e965ca89770cb327bd60ca [file] [log] [blame]
Yingdi Yufe140152014-07-24 10:02:40 -07001/* -*- 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 Yufe140152014-07-24 10:02:40 -070020 */
Yingdi Yufe140152014-07-24 10:02:40 -070021
22#ifndef NDN_UTIL_EVENT_EMITTER_HPP
23#define NDN_UTIL_EVENT_EMITTER_HPP
24
Alexander Afanasyevd51cd582014-08-12 11:34:24 -070025#include "../common.hpp"
Yingdi Yufe140152014-07-24 10:02:40 -070026#include <vector>
27
28namespace ndn {
29namespace util {
30
Junxiao Shia93870d2014-11-05 22:35:31 -070031/** \brief provides a lightweight event system
Yingdi Yufe140152014-07-24 10:02:40 -070032 *
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 Shi8d71fdb2014-12-07 21:55:19 -070042 *
43 * \deprecated use Signal instead
Yingdi Yufe140152014-07-24 10:02:40 -070044 */
45
Junxiao Shia93870d2014-11-05 22:35:31 -070046template<typename ...TArgs>
Yingdi Yufe140152014-07-24 10:02:40 -070047class EventEmitter : noncopyable
48{
49public:
Junxiao Shia93870d2014-11-05 22:35:31 -070050 /** \brief represents a handler that can subscribe to the event
51 */
52 typedef function<void(const TArgs&...)> Handler;
Yingdi Yufe140152014-07-24 10:02:40 -070053
Junxiao Shia93870d2014-11-05 22:35:31 -070054 /** \brief subscribes to the event
55 */
Yingdi Yufe140152014-07-24 10:02:40 -070056 void
Junxiao Shia93870d2014-11-05 22:35:31 -070057 operator+=(const Handler& handler);
Yingdi Yufe140152014-07-24 10:02:40 -070058
Junxiao Shia93870d2014-11-05 22:35:31 -070059 /** \return true if there is no subscription, false otherwise
60 */
Yingdi Yufe140152014-07-24 10:02:40 -070061 bool
62 isEmpty() const;
63
Junxiao Shia93870d2014-11-05 22:35:31 -070064 /** \brief clears all subscriptions
65 */
Yingdi Yufe140152014-07-24 10:02:40 -070066 void
67 clear();
68
Junxiao Shia93870d2014-11-05 22:35:31 -070069 /** \brief triggers the event
70 */
Yingdi Yufe140152014-07-24 10:02:40 -070071 void
Junxiao Shia93870d2014-11-05 22:35:31 -070072 operator()(const TArgs&...args) const;
Yingdi Yufe140152014-07-24 10:02:40 -070073
74private:
75 std::vector<Handler> m_handlers;
76};
77
Junxiao Shia93870d2014-11-05 22:35:31 -070078template<typename ...TArgs>
Yingdi Yufe140152014-07-24 10:02:40 -070079inline void
Junxiao Shia93870d2014-11-05 22:35:31 -070080EventEmitter<TArgs...>::operator+=(const Handler& handler)
Yingdi Yufe140152014-07-24 10:02:40 -070081{
82 m_handlers.push_back(handler);
83}
84
Junxiao Shia93870d2014-11-05 22:35:31 -070085template<typename ...TArgs>
Yingdi Yufe140152014-07-24 10:02:40 -070086inline bool
Junxiao Shia93870d2014-11-05 22:35:31 -070087EventEmitter<TArgs...>::isEmpty() const
Yingdi Yufe140152014-07-24 10:02:40 -070088{
89 return m_handlers.empty();
90}
91
Junxiao Shia93870d2014-11-05 22:35:31 -070092template<typename ...TArgs>
Yingdi Yufe140152014-07-24 10:02:40 -070093inline void
Junxiao Shia93870d2014-11-05 22:35:31 -070094EventEmitter<TArgs...>::clear()
Yingdi Yufe140152014-07-24 10:02:40 -070095{
96 return m_handlers.clear();
97}
98
Junxiao Shia93870d2014-11-05 22:35:31 -070099template<typename ...TArgs>
Yingdi Yufe140152014-07-24 10:02:40 -0700100inline void
Junxiao Shia93870d2014-11-05 22:35:31 -0700101EventEmitter<TArgs...>::operator()(const TArgs&...args) const
Yingdi Yufe140152014-07-24 10:02:40 -0700102{
Junxiao Shia93870d2014-11-05 22:35:31 -0700103 for (const Handler& handler : m_handlers) {
104 handler(args...);
Yingdi Yufe140152014-07-24 10:02:40 -0700105 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