22 #ifndef NDN_CXX_UTIL_SIGNAL_SIGNAL_HPP
23 #define NDN_CXX_UTIL_SIGNAL_SIGNAL_HPP
49 template<
typename Owner,
typename... TArgs>
53 #ifndef BOOST_ASSERT_IS_VOID
56 BOOST_ASSERT(!m_isExecuting);
63 using Handler = std::function<void(
const TArgs&...)>;
93 operator()(
const TArgs&... args);
125 shared_ptr<DisconnectFunction> disconnect;
132 using SlotList = std::list<Slot>;
137 bool m_isExecuting =
false;
142 typename SlotList::iterator m_currentSlot;
147 disconnect(
typename SlotList::iterator it);
150 template<
typename Owner,
typename... TArgs>
154 auto it = m_slots.insert(m_slots.end(), {std::move(handler), nullptr});
155 it->
disconnect = make_shared<DisconnectFunction>([=] { disconnect(it); });
160 template<
typename Owner,
typename... TArgs>
164 auto it = m_slots.insert(m_slots.end(), {nullptr, nullptr});
165 it->
disconnect = make_shared<DisconnectFunction>([=] { disconnect(it); });
168 it->handler = [conn, handler = std::move(handler)] (
const TArgs&... args)
mutable {
176 template<
typename Owner,
typename... TArgs>
182 BOOST_ASSERT_MSG(it == m_currentSlot,
"cannot disconnect another handler from a handler");
186 m_currentSlot = m_slots.end();
189 it->disconnect.reset();
196 template<
typename Owner,
typename... TArgs>
198 Signal<Owner, TArgs...>::isEmpty()
const
200 return !m_isExecuting && m_slots.empty();
203 template<
typename Owner,
typename... TArgs>
205 Signal<Owner, TArgs...>::operator()(
const TArgs&... args)
207 BOOST_ASSERT_MSG(!m_isExecuting,
"cannot emit signal from a handler");
209 if (m_slots.empty()) {
213 auto guard = make_scope_exit([
this] { m_isExecuting =
false; });
214 m_isExecuting =
true;
216 auto it = m_slots.begin();
217 auto last = std::prev(m_slots.end());
223 m_currentSlot->handler(args...);
225 if (m_currentSlot == m_slots.end())
226 it = m_slots.erase(it);
232 template<
typename Owner,
typename... TArgs>
234 Signal<Owner, TArgs...>::operator()(
const TArgs&... args,
const DummyExtraArg&)
236 this->operator()(args...);
Represents a connection to a signal.
void disconnect()
Disconnects from the signal.
Provides a lightweight signal / event system.
Connection connect(Handler handler)
Connects a handler to the signal.
Connection connectSingleShot(Handler handler)
Connects a single-shot handler to the signal.
std::function< void(const TArgs &...)> Handler
Represents a function that can connect to the signal.