13 Connection() = default;
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) {}
21 void disconnect() noexcept
29 std::function<void ()> m_disconnect;
32 class ScopedConnection
35 ScopedConnection() = default;
37 ScopedConnection(const ScopedConnection&) = delete;
38 ScopedConnection& operator=(const ScopedConnection&) = delete;
39 ScopedConnection(ScopedConnection&&) = default;
40 ScopedConnection& operator=(ScopedConnection&&) = default;
42 ScopedConnection(Connection c) noexcept : m_conn(std::move(c)) {}
43 ~ScopedConnection() { disconnect(); }
45 void disconnect() noexcept { m_conn.disconnect(); }
51 template <typename... Ts>
55 using Callback = std::function<void (const Ts&...)>;
56 using Callbacks = std::list<Callback>;
58 Connection makeConn(typename Callbacks::iterator i) noexcept
60 return Connection([this, i](){ remove(i); });
66 std::lock_guard lock(m_mutex);
67 return makeConn(m_callbacks.insert(m_callbacks.end(), Callback(std::forward<F>(f))));
70 template <typename C, typename... T2s>
71 Connection add(C& c, void (C::*m)(T2s...))
73 return add([&c, m](const Ts&... values){ (c.*m)(values...); });
76 void remove(typename Callbacks::iterator i)
78 std::lock_guard lock(m_mutex);
82 void notify(const Ts&... values)
84 std::lock_guard lock(m_mutex);
85 for (auto& cb : m_callbacks)
89 bool empty() const noexcept
91 std::lock_guard lock(m_mutex);
92 return m_callbacks.empty();
95 size_t size() const noexcept
97 std::lock_guard lock(m_mutex);
98 return m_callbacks.size();
102 mutable std::mutex m_mutex;
103 Callbacks m_callbacks;