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);
122 shared_ptr<DisconnectFunction> disconnect;
129 typedef std::list<Slot> SlotList;
139 typename SlotList::iterator m_currentSlot;
144 disconnect(
typename SlotList::iterator it);
147 template<
typename Owner,
typename ...TArgs>
149 : m_isExecuting(false)
153 template<
typename Owner,
typename ...TArgs>
156 BOOST_ASSERT(!m_isExecuting);
159 template<
typename Owner,
typename ...TArgs>
163 auto it = m_slots.insert(m_slots.end(), {
std::move(handler),
nullptr});
164 it->disconnect = make_shared<DisconnectFunction>([=] { disconnect(it); });
169 template<
typename Owner,
typename ...TArgs>
173 auto it = m_slots.insert(m_slots.end(), {
nullptr,
nullptr});
174 it->disconnect = make_shared<DisconnectFunction>([=] { disconnect(it); });
177 it->handler = [conn, handler =
std::move(handler)] (
const TArgs&... args)
mutable {
185 template<
typename Owner,
typename ...TArgs>
191 BOOST_ASSERT_MSG(it == m_currentSlot,
"cannot disconnect another handler from a handler");
195 m_currentSlot = m_slots.end();
198 it->disconnect.reset();
205 template<
typename Owner,
typename ...TArgs>
209 return !m_isExecuting && m_slots.empty();
212 template<
typename Owner,
typename ...TArgs>
216 BOOST_ASSERT_MSG(!m_isExecuting,
"cannot emit signal from a handler");
218 if (m_slots.empty()) {
222 auto it = m_slots.begin();
223 auto last = std::prev(m_slots.end());
224 m_isExecuting =
true;
232 m_currentSlot->handler(args...);
234 if (m_currentSlot == m_slots.end())
235 it = m_slots.erase(it);
241 m_isExecuting =
false;
245 m_isExecuting =
false;
248 template<
typename Owner,
typename ...TArgs>
252 this->operator()(args...);
263 #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