Files
Tower-Defense2/include/td/misc/SlotGuard.h
2025-08-21 20:32:47 +02:00

34 lines
795 B
C++

#pragma once
#include <td/misc/Signal.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::unique_ptr<Connection>> m_Connections;
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::element_type& a_Callback) {
auto ptr = std::make_shared<typename Signal<Args...>::CallBack::element_type>(a_Callback);
m_Connections.push_back(a_Signal.ConnectSafe(ptr));
}
void Disconnect() {
m_Connections.clear();
}
};
} // namespace utils
} // namespace td