+class Connection
+{
+ public:
+ Connection() = default;
+
+ Connection(const Connection&) = delete;
+ Connection& operator=(const Connection&) = delete;
+ Connection(Connection&&) = default;
+ Connection& operator=(Connection&&) = default;
+
+ Connection(const std::function<void ()>& f) noexcept : m_disconnect(f) {}
+ void disconnect() noexcept
+ {
+ if (!m_disconnect)
+ return;
+ m_disconnect();
+ m_disconnect = {};
+ }
+ private:
+ std::function<void ()> m_disconnect;
+};
+
+class ScopedConnection
+{
+ public:
+ ScopedConnection() = default;
+
+ ScopedConnection(const ScopedConnection&) = delete;
+ ScopedConnection& operator=(const ScopedConnection&) = delete;
+ ScopedConnection(ScopedConnection&&) = default;
+ ScopedConnection& operator=(ScopedConnection&&) = default;
+
+ ScopedConnection(Connection c) noexcept : m_conn(std::move(c)) {}
+ ~ScopedConnection() { disconnect(); }
+
+ void disconnect() noexcept { m_conn.disconnect(); }
+
+ private:
+ Connection m_conn;
+};
+