You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.1 KiB
C
55 lines
1.1 KiB
C
|
1 month ago
|
#pragma once
|
||
|
|
|
||
|
|
#include <functional>
|
||
|
|
#include <utility>
|
||
|
|
|
||
|
|
class ScopedConnection
|
||
|
|
{
|
||
|
|
public:
|
||
|
|
ScopedConnection() = default;
|
||
|
|
|
||
|
|
explicit ScopedConnection(std::function<void()> 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<bool>(m_disconnectFunc);
|
||
|
|
}
|
||
|
|
|
||
|
|
private:
|
||
|
|
std::function<void()> m_disconnectFunc;
|
||
|
|
};
|