blob: ca6dcf14a19244d7b8703603aecd9618be4083e6 [file] [log] [blame]
Junxiao Shi8d71fdb2014-12-07 21:55:19 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyevaf99f462015-01-19 21:43:09 -08003 * Copyright (c) 2013-2015 Regents of the University of California.
Junxiao Shi8d71fdb2014-12-07 21:55:19 -07004 *
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.
20 */
21
22#ifndef NDN_UTIL_SIGNAL_SIGNAL_HPP
23#define NDN_UTIL_SIGNAL_SIGNAL_HPP
24
25#include "signal-connection.hpp"
26#include <list>
27
28namespace ndn {
29namespace util {
30namespace signal {
31
Junxiao Shi018e30d2014-12-25 19:42:35 -070032class DummyExtraArg;
33
Junxiao Shi8d71fdb2014-12-07 21:55:19 -070034/** \brief provides a lightweight signal / event system
35 *
36 * To declare a signal:
37 * public:
38 * Signal<Owner, T1, T2> signalName;
39 * To connect to a signal:
40 * owner->signalName.connect(f);
41 * Multiple functions can connect to the same signal.
42 * To emit a signal from owner:
43 * this->signalName(arg1, arg2);
44 *
45 * \tparam Owner the signal owner class; only this class can emit the signal
46 * \tparam TArgs types of signal arguments
47 * \sa signal-emit.hpp allows owner's derived classes to emit signals
48 */
49template<typename Owner, typename ...TArgs>
50class Signal : noncopyable
51{
52public: // API for anyone
53 /** \brief represents a function that can connect to the signal
54 */
55 typedef function<void(const TArgs&...)> Handler;
56
57 Signal();
58
Junxiao Shibbb24352015-02-28 21:23:51 -070059 ~Signal();
60
Junxiao Shi8d71fdb2014-12-07 21:55:19 -070061 /** \brief connects a handler to the signal
62 * \note If invoked from a handler, the new handler won't receive the current emitted signal.
Junxiao Shi2cec7072014-12-19 19:37:40 -070063 * \warning The handler is permitted to disconnect itself, but it must ensure its validity.
Junxiao Shi8d71fdb2014-12-07 21:55:19 -070064 */
65 Connection
66 connect(const Handler& handler);
67
Junxiao Shiecc57b52015-01-01 10:47:08 -070068 /** \brief connects a single-shot handler to the signal
69 *
70 * After the handler is executed once, it is automatically disconnected.
71 */
72 Connection
73 connectSingleShot(const Handler& handler);
74
Junxiao Shi8d71fdb2014-12-07 21:55:19 -070075private: // API for owner
76 /** \retval true if there is no connection
77 */
78 bool
79 isEmpty() const;
80
81 /** \brief emits a signal
82 * \param args arguments passed to all handlers
83 * \warning Emitting the signal from a handler is undefined behavior.
Junxiao Shibbb24352015-02-28 21:23:51 -070084 * \warning Destructing the Signal object during signal emission is undefined behavior.
Junxiao Shi8d71fdb2014-12-07 21:55:19 -070085 * \note If a handler throws, the exception will be propagated to the caller
86 * who emits this signal, and some handlers may not be executed.
87 */
88 void
89 operator()(const TArgs&...args);
90
Junxiao Shi018e30d2014-12-25 19:42:35 -070091 /** \brief (implementation detail) emits a signal
92 * \note This overload is used by signal-emit.hpp.
93 */
94 void
95 operator()(const TArgs&...args, const DummyExtraArg&);
96
Junxiao Shi8d71fdb2014-12-07 21:55:19 -070097 // make Owner a friend of Signal<Owner, ...> so that API for owner can be called
98#if NDN_CXX_HAVE_CXX_FRIEND_TYPENAME
99 friend Owner;
100#elif NDN_CXX_HAVE_CXX_FRIEND_TYPENAME_WRAPPER
101 template<typename T>
102 struct TypeWrapper
103 {
104 typedef T Type;
105 }; // http://stackoverflow.com/a/5608542/3729203
106 friend class TypeWrapper<Owner>::Type;
107#else
108# error "cannot declare Owner as friend"
109#endif
110
111private: // internal implementation
112 typedef Signal<Owner, TArgs...> Self;
113 struct Slot;
114
115 /** \brief stores slots
116 * \note std::list is used because iterators must not be invalidated
117 * when other slots are added or removed
118 */
119 typedef std::list<Slot> SlotList;
120
121 /** \brief stores a handler function, and a function to disconnect this handler
122 */
123 struct Slot
124 {
125 /** \brief the handler function who will receive emitted signals
126 */
127 Handler handler;
128
129 /** \brief the disconnect function which will disconnect this handler
130 *
Davide Pesaventobe7804f2015-10-23 00:53:47 +0200131 * In practice this is the Signal::disconnect method bound to an iterator
132 * pointing at this slot.
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700133 *
134 * This is the only shared_ptr to this function object.
135 * Connection class has a weak_ptr which references the same function object.
Davide Pesaventobe7804f2015-10-23 00:53:47 +0200136 * When the slot is erased or the signal is destructed, this function object is
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700137 * destructed, and the related Connections cannot disconnect this slot again.
138 */
139 shared_ptr<function<void()>> disconnect;
140 };
141
142 /** \brief stores slots
143 */
144 SlotList m_slots;
145
146 /** \brief is a signal handler executing?
147 */
148 bool m_isExecuting;
149
150 /** \brief iterator to current executing slot
151 * \note This field is meaningful when isExecuting==true
152 */
153 typename SlotList::iterator m_currentSlot;
154
155 /** \brief disconnects the handler in a slot
156 */
157 void
158 disconnect(typename SlotList::iterator it);
159};
160
161template<typename Owner, typename ...TArgs>
162Signal<Owner, TArgs...>::Signal()
163 : m_isExecuting(false)
164{
165}
166
167template<typename Owner, typename ...TArgs>
Junxiao Shibbb24352015-02-28 21:23:51 -0700168Signal<Owner, TArgs...>::~Signal()
169{
170 BOOST_ASSERT(!m_isExecuting);
171}
172
173template<typename Owner, typename ...TArgs>
Davide Pesaventobe7804f2015-10-23 00:53:47 +0200174Connection
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700175Signal<Owner, TArgs...>::connect(const Handler& handler)
176{
177 typename SlotList::iterator it = m_slots.insert(m_slots.end(), {handler, nullptr});
178 it->disconnect = make_shared<function<void()>>(bind(&Self::disconnect, this, it));
179
180 return signal::Connection(weak_ptr<function<void()>>(it->disconnect));
181}
182
183template<typename Owner, typename ...TArgs>
Davide Pesaventobe7804f2015-10-23 00:53:47 +0200184Connection
Junxiao Shiecc57b52015-01-01 10:47:08 -0700185Signal<Owner, TArgs...>::connectSingleShot(const Handler& handler)
186{
187 typename SlotList::iterator it = m_slots.insert(m_slots.end(), {nullptr, nullptr});
188 it->disconnect = make_shared<function<void()>>(bind(&Self::disconnect, this, it));
189 signal::Connection conn(weak_ptr<function<void()>>(it->disconnect));
190
191 it->handler = [conn, handler] (const TArgs&... args) mutable {
192 handler(args...);
193 conn.disconnect();
194 };
195
196 return conn;
197}
198
199template<typename Owner, typename ...TArgs>
Davide Pesaventobe7804f2015-10-23 00:53:47 +0200200void
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700201Signal<Owner, TArgs...>::disconnect(typename SlotList::iterator it)
202{
Davide Pesaventobe7804f2015-10-23 00:53:47 +0200203 // 'it' could be const_iterator, but gcc 4.6 doesn't support std::list::erase(const_iterator)
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700204
205 if (m_isExecuting) {
206 // during signal emission, only the currently executing handler can be disconnected
Davide Pesaventobe7804f2015-10-23 00:53:47 +0200207 BOOST_ASSERT_MSG(it == m_currentSlot, "cannot disconnect another handler from a handler");
208
209 // this serves to indicate that the current slot needs to be erased from the list
210 // after it finishes executing; we cannot do it here because of bug #2333
211 m_currentSlot = m_slots.end();
212
213 // expire all weak_ptrs, to prevent double disconnections
214 it->disconnect.reset();
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700215 }
Davide Pesaventobe7804f2015-10-23 00:53:47 +0200216 else {
217 m_slots.erase(it);
218 }
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700219}
220
221template<typename Owner, typename ...TArgs>
Davide Pesaventobe7804f2015-10-23 00:53:47 +0200222bool
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700223Signal<Owner, TArgs...>::isEmpty() const
224{
225 return !m_isExecuting && m_slots.empty();
226}
227
228template<typename Owner, typename ...TArgs>
Davide Pesaventobe7804f2015-10-23 00:53:47 +0200229void
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700230Signal<Owner, TArgs...>::operator()(const TArgs&... args)
231{
232 BOOST_ASSERT_MSG(!m_isExecuting, "cannot emit signal from a handler");
Davide Pesaventobe7804f2015-10-23 00:53:47 +0200233
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700234 if (m_slots.empty()) {
235 return;
236 }
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700237
Davide Pesaventobe7804f2015-10-23 00:53:47 +0200238 auto it = m_slots.begin();
239 auto last = std::prev(m_slots.end());
240 m_isExecuting = true;
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700241
242 try {
243 bool isLast = false;
244 while (!isLast) {
245 m_currentSlot = it;
246 isLast = it == last;
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700247
248 m_currentSlot->handler(args...);
Davide Pesaventobe7804f2015-10-23 00:53:47 +0200249
250 if (m_currentSlot == m_slots.end())
251 it = m_slots.erase(it);
252 else
253 ++it;
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700254 }
255 }
256 catch (...) {
257 m_isExecuting = false;
258 throw;
259 }
Davide Pesaventobe7804f2015-10-23 00:53:47 +0200260
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700261 m_isExecuting = false;
262}
263
Junxiao Shi018e30d2014-12-25 19:42:35 -0700264template<typename Owner, typename ...TArgs>
Davide Pesaventobe7804f2015-10-23 00:53:47 +0200265void
Junxiao Shi018e30d2014-12-25 19:42:35 -0700266Signal<Owner, TArgs...>::operator()(const TArgs&... args, const DummyExtraArg&)
267{
268 this->operator()(args...);
269}
270
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700271} // namespace signal
272
273// expose as ndn::util::Signal
274using signal::Signal;
275
276} // namespace util
277} // namespace ndn
278
279#endif // NDN_UTIL_SIGNAL_SIGNAL_HPP