#pragma once #include #include class ScopedConnection { public: ScopedConnection() = default; explicit ScopedConnection(std::function disconnectFunc) : m_disconnectFunc(std::move(disconnectFunc)) { } ScopedConnection(const ScopedConnection&) = delete; ScopedConnection& operator=(const ScopedConnection&) = delete; ScopedConnection(ScopedConnection&& other) noexcept : m_disconnectFunc(std::move(other.m_disconnectFunc)) { } ScopedConnection& operator=(ScopedConnection&& other) noexcept { if (this != &other) { Disconnect(); m_disconnectFunc = std::move(other.m_disconnectFunc); } return *this; } ~ScopedConnection() { Disconnect(); } void Disconnect() { if (m_disconnectFunc) { m_disconnectFunc(); m_disconnectFunc = nullptr; } } bool IsConnected() const { return static_cast(m_disconnectFunc); } private: std::function m_disconnectFunc; };