Files
Tower-Defense2/include/td/misc/SlotGuard.h
2026-01-30 13:18:01 +01:00

34 lines
798 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::shared_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.emplace_back(a_Signal.ConnectSafe(ptr));
}
void Disconnect() {
m_Connections.clear();
}
};
} // namespace utils
} // namespace td