10 template <typename... Ts>
14 using Callback = std::function<void (Ts...)>;
15 using Callbacks = std::list<Callback>;
20 Connection(Subscriptions& s, typename Callbacks::iterator i) noexcept
21 : m_subscriptions(s), m_iterator(i), m_connected(true)
28 void disconnect() noexcept
32 m_subscriptions.remove(m_iterator);
36 Subscriptions& m_subscriptions;
37 typename Callbacks::iterator m_iterator;
44 std::lock_guard lock(m_mutex);
45 return Connection(*this, m_callbacks.insert(m_callbacks.end(), Callback(std::forward<F>(f))));
48 template <typename C, typename... T2s>
49 Connection add(C& c, void (C::*m)(T2s...))
51 return add([&c, m](Ts&&... values){ (c.*m)(std::forward<Ts>(values)...); });
54 void remove(typename Callbacks::iterator i)
56 std::lock_guard lock(m_mutex);
60 void notify(Ts&&... values)
62 std::lock_guard lock(m_mutex);
63 for (auto& cb : m_callbacks)
67 bool empty() const noexcept
69 std::lock_guard lock(m_mutex);
70 return m_callbacks.empty();
73 size_t size() const noexcept
75 std::lock_guard lock(m_mutex);
76 return m_callbacks.size();
80 mutable std::mutex m_mutex;
81 Callbacks m_callbacks;