22 #ifndef NDN_UTIL_SIGNAL_SIGNAL_HPP 23 #define NDN_UTIL_SIGNAL_SIGNAL_HPP 49 template<
typename Owner,
typename ...TArgs>
55 typedef function<void(
const TArgs&...)>
Handler;
66 connect(
const Handler& handler);
89 operator()(
const TArgs&... args);
108 typedef std::list<Slot> SlotList;
128 shared_ptr<function<void()>> disconnect;
142 typename SlotList::iterator m_currentSlot;
147 disconnect(
typename SlotList::iterator it);
150 template<
typename Owner,
typename ...TArgs>
152 : m_isExecuting(false)
156 template<
typename Owner,
typename ...TArgs>
159 BOOST_ASSERT(!m_isExecuting);
162 template<
typename Owner,
typename ...TArgs>
166 typename SlotList::iterator it = m_slots.insert(m_slots.end(), {handler,
nullptr});
167 it->disconnect = make_shared<function<void()>>(bind(&Self::disconnect,
this, it));
172 template<
typename Owner,
typename ...TArgs>
176 typename SlotList::iterator it = m_slots.insert(m_slots.end(), {
nullptr,
nullptr});
177 it->disconnect = make_shared<function<void()>>(bind(&Self::disconnect,
this, it));
180 it->handler = [conn, handler] (
const TArgs&... args)
mutable {
188 template<
typename Owner,
typename ...TArgs>
196 BOOST_ASSERT_MSG(it == m_currentSlot,
"cannot disconnect another handler from a handler");
200 m_currentSlot = m_slots.end();
203 it->disconnect.reset();
210 template<
typename Owner,
typename ...TArgs>
214 return !m_isExecuting && m_slots.empty();
217 template<
typename Owner,
typename ...TArgs>
221 BOOST_ASSERT_MSG(!m_isExecuting,
"cannot emit signal from a handler");
223 if (m_slots.empty()) {
227 auto it = m_slots.begin();
228 auto last = std::prev(m_slots.end());
229 m_isExecuting =
true;
237 m_currentSlot->handler(args...);
239 if (m_currentSlot == m_slots.end())
240 it = m_slots.erase(it);
246 m_isExecuting =
false;
250 m_isExecuting =
false;
253 template<
typename Owner,
typename ...TArgs>
257 this->operator()(args...);
268 #endif // NDN_UTIL_SIGNAL_SIGNAL_HPP Connection connectSingleShot(const Handler &handler)
connects a single-shot handler to the signal
Copyright (c) 2013-2017 Regents of the University of California.
represents a connection to a signal
provides a lightweight signal / event system
Connection connect(const Handler &handler)
connects a handler to the signal
function< void(const TArgs &...)> Handler
represents a function that can connect to the signal