22 #ifndef NDN_UTIL_SIGNAL_SIGNAL_HPP 23 #define NDN_UTIL_SIGNAL_SIGNAL_HPP 50 template<
typename Owner,
typename ...TArgs>
56 typedef function<void(
const TArgs&...)>
Handler;
90 operator()(
const TArgs&... args);
109 typedef std::list<Slot> SlotList;
129 shared_ptr<function<void()>> disconnect;
143 typename SlotList::iterator m_currentSlot;
148 disconnect(
typename SlotList::iterator it);
151 template<
typename Owner,
typename ...TArgs>
153 : m_isExecuting(false)
157 template<
typename Owner,
typename ...TArgs>
160 BOOST_ASSERT(!m_isExecuting);
163 template<
typename Owner,
typename ...TArgs>
167 auto it = m_slots.insert(m_slots.end(), {
std::move(handler),
nullptr});
168 it->disconnect = make_shared<function<void()>>([=] { disconnect(it); });
173 template<
typename Owner,
typename ...TArgs>
177 auto it = m_slots.insert(m_slots.end(), {
nullptr,
nullptr});
178 it->disconnect = make_shared<function<void()>>([=] { disconnect(it); });
181 it->handler = [conn, handler =
std::move(handler)] (
const TArgs&... args)
mutable {
189 template<
typename Owner,
typename ...TArgs>
195 BOOST_ASSERT_MSG(it == m_currentSlot,
"cannot disconnect another handler from a handler");
199 m_currentSlot = m_slots.end();
202 it->disconnect.reset();
209 template<
typename Owner,
typename ...TArgs>
213 return !m_isExecuting && m_slots.empty();
216 template<
typename Owner,
typename ...TArgs>
220 BOOST_ASSERT_MSG(!m_isExecuting,
"cannot emit signal from a handler");
222 if (m_slots.empty()) {
226 auto it = m_slots.begin();
227 auto last = std::prev(m_slots.end());
228 m_isExecuting =
true;
236 m_currentSlot->handler(args...);
238 if (m_currentSlot == m_slots.end())
239 it = m_slots.erase(it);
245 m_isExecuting =
false;
249 m_isExecuting =
false;
252 template<
typename Owner,
typename ...TArgs>
256 this->operator()(args...);
267 #endif // NDN_UTIL_SIGNAL_SIGNAL_HPP Connection connect(Handler handler)
connects a handler to the signal
represents a connection to a signal
provides a lightweight signal / event system
Connection connectSingleShot(Handler handler)
connects a single-shot handler to the signal
function< void(const TArgs &...)> Handler
represents a function that can connect to the signal