36 lines
775 B
C++
36 lines
775 B
C++
#pragma once
|
|
|
|
#include <td/misc/SlotGuard.h>
|
|
|
|
namespace td {
|
|
namespace utils {
|
|
|
|
/**
|
|
* \brief Wrapper class to automatically disconnect from a Signal on object destruction
|
|
* \note You should inherit this class privately
|
|
* \sa Signal
|
|
*/
|
|
class SlotGuard {
|
|
private:
|
|
std::vector<std::function<void()>> m_Deleters;
|
|
|
|
public:
|
|
/**
|
|
* \brief Connect a signal to a function (with the same signature)
|
|
*/
|
|
template <typename... Args>
|
|
void Connect(Signal<Args...>& a_Signal, const typename Signal<Args...>::CallBack& a_Callback) {
|
|
a_Signal.Connect(a_Callback);
|
|
m_Deleters.push_back([&a_Signal, &a_Callback]() { a_Signal.Disconnect(a_Callback); });
|
|
}
|
|
|
|
~SlotGuard() {
|
|
for (auto& deleter : m_Deleters) {
|
|
deleter();
|
|
}
|
|
}
|
|
};
|
|
|
|
} // namespace utils
|
|
} // namespace td
|