13 Connection() noexcept : m_connected(false) {}
15 Connection(const Connection&) = delete;
16 Connection& operator=(const Connection&) = delete;
17 Connection(Connection&&) = default;
18 Connection& operator=(Connection&&) = default;
20 Connection(const std::function<void ()>& f) noexcept : m_disconnect(f), m_connected(true) {}
21 void disconnect() noexcept
29 std::function<void ()> m_disconnect;
33 class ScopedConnection
36 ScopedConnection() = default;
38 ScopedConnection(const ScopedConnection&) = delete;
39 ScopedConnection& operator=(const ScopedConnection&) = delete;
40 ScopedConnection(ScopedConnection&&) = default;
41 ScopedConnection& operator=(ScopedConnection&&) = default;
43 ScopedConnection(Connection c) noexcept : m_conn(std::move(c)) {}
44 ~ScopedConnection() { disconnect(); }
46 void disconnect() noexcept { m_conn.disconnect(); }
52 template <typename... Ts>
56 using Callback = std::function<void (const Ts&...)>;
57 using Callbacks = std::list<Callback>;
59 Connection makeConn(typename Callbacks::iterator i) noexcept
61 return Connection([this, i](){ remove(i); });
67 std::lock_guard lock(m_mutex);
68 return makeConn(m_callbacks.insert(m_callbacks.end(), Callback(std::forward<F>(f))));
71 template <typename C, typename... T2s>
72 Connection add(C& c, void (C::*m)(T2s...))
74 return add([&c, m](const Ts&... values){ (c.*m)(values...); });
77 void remove(typename Callbacks::iterator i)
79 std::lock_guard lock(m_mutex);
83 void notify(const Ts&... values)
85 std::lock_guard lock(m_mutex);
86 for (auto& cb : m_callbacks)
90 bool empty() const noexcept
92 std::lock_guard lock(m_mutex);
93 return m_callbacks.empty();
96 size_t size() const noexcept
98 std::lock_guard lock(m_mutex);
99 return m_callbacks.size();
103 mutable std::mutex m_mutex;
104 Callbacks m_callbacks;