Files
Blitz/include/blitz/misc/ObjectNotifier.h
2024-06-01 15:54:01 +02:00

53 lines
1.1 KiB
C++

#pragma once
/**
* \file ObjectNotifier.h
* \brief File containing the blitz::ObjectNotifier class
*/
#include <algorithm>
#include <functional>
#include <vector>
namespace blitz {
namespace utils {
/**
* \class ObjectNotifier
* \brief Class used to notify listeners
*/
template <typename Listener>
class ObjectNotifier {
protected:
std::vector<Listener*> m_Listeners;
public:
/**
* \brief Binds a listener to notify later
*/
void BindListener(Listener* listener) {
m_Listeners.push_back(listener);
}
/**
* \brief Unbinds a listener (in case the listener is destroyed for example)
*/
void UnbindListener(Listener* listener) {
m_Listeners.erase(std::remove(m_Listeners.begin(), m_Listeners.end(), listener), m_Listeners.end());
}
/**
* \brief Notify listeners that were bound
* \param function the function to call
* \param args the parameters of the function to call
*/
template <typename Func, typename... Args>
void NotifyListeners(Func function, Args... args) const {
for (Listener* listener : m_Listeners)
std::bind(function, listener, args...)();
}
};
} // namespace utils
} // namespace blitz